cmd/geth: support string (non-hex) keys in db get/put/delete (#23744)

Adds suppor for passing regular strings to db `put`/`get`/`delete`, to avoid having to hex-encode when operating on fixed-key items like `SnapshotSyncStatus`, `SnapshotRecovery`  etc.


Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
This commit is contained in:
Maxim Zhiburt 2021-10-18 13:18:49 +03:00 committed by GitHub
parent c641cff51a
commit eaa24a8a15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,6 +17,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -335,14 +336,15 @@ func dbGet(ctx *cli.Context) error {
db := utils.MakeChainDatabase(ctx, stack, true) db := utils.MakeChainDatabase(ctx, stack, true)
defer db.Close() defer db.Close()
key, err := hexutil.Decode(ctx.Args().Get(0)) key, err := parseHexOrString(ctx.Args().Get(0))
if err != nil { if err != nil {
log.Info("Could not decode the key", "error", err) log.Info("Could not decode the key", "error", err)
return err return err
} }
data, err := db.Get(key) data, err := db.Get(key)
if err != nil { if err != nil {
log.Info("Get operation failed", "error", err) log.Info("Get operation failed", "key", fmt.Sprintf("0x%#x", key), "error", err)
return err return err
} }
fmt.Printf("key %#x: %#x\n", key, data) fmt.Printf("key %#x: %#x\n", key, data)
@ -360,7 +362,7 @@ func dbDelete(ctx *cli.Context) error {
db := utils.MakeChainDatabase(ctx, stack, false) db := utils.MakeChainDatabase(ctx, stack, false)
defer db.Close() defer db.Close()
key, err := hexutil.Decode(ctx.Args().Get(0)) key, err := parseHexOrString(ctx.Args().Get(0))
if err != nil { if err != nil {
log.Info("Could not decode the key", "error", err) log.Info("Could not decode the key", "error", err)
return err return err
@ -370,7 +372,7 @@ func dbDelete(ctx *cli.Context) error {
fmt.Printf("Previous value: %#x\n", data) fmt.Printf("Previous value: %#x\n", data)
} }
if err = db.Delete(key); err != nil { if err = db.Delete(key); err != nil {
log.Info("Delete operation returned an error", "error", err) log.Info("Delete operation returned an error", "key", fmt.Sprintf("0x%#x", key), "error", err)
return err return err
} }
return nil return nil
@ -393,7 +395,7 @@ func dbPut(ctx *cli.Context) error {
data []byte data []byte
err error err error
) )
key, err = hexutil.Decode(ctx.Args().Get(0)) key, err = parseHexOrString(ctx.Args().Get(0))
if err != nil { if err != nil {
log.Info("Could not decode the key", "error", err) log.Info("Could not decode the key", "error", err)
return err return err
@ -499,3 +501,12 @@ func freezerInspect(ctx *cli.Context) error {
} }
return nil return nil
} }
// ParseHexOrString tries to hexdecode b, but if the prefix is missing, it instead just returns the raw bytes
func parseHexOrString(str string) ([]byte, error) {
b, err := hexutil.Decode(str)
if errors.Is(err, hexutil.ErrMissingPrefix) {
return []byte(str), nil
}
return b, err
}