From c5942c0d3d347f387ee898230a7dcd17748597fb Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 10 Dec 2020 11:04:53 +0100 Subject: [PATCH] cmd/geth: add ability to get/put raw db values --- cmd/geth/chaincmd.go | 75 ++++++++++++++++++++++++++++++++++++++++++++ cmd/geth/main.go | 2 ++ 2 files changed, 77 insertions(+) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 6418f909579a..024ff3c454d5 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -19,6 +19,7 @@ package main import ( "encoding/json" "fmt" + "github.com/ethereum/go-ethereum/common/hexutil" "os" "path/filepath" "runtime" @@ -219,6 +220,24 @@ Use "ethereum dump 0" to dump the genesis block.`, }, Category: "BLOCKCHAIN COMMANDS", } + dbGetCommand = cli.Command{ + Action: utils.MigrateFlags(dbGet), + Name: "db.get", + Usage: "Show the value of a database key", + ArgsUsage: "", + Flags: []cli.Flag{ + utils.DataDirFlag, + }, + } + dbPutCommand = cli.Command{ + Action: utils.MigrateFlags(dbPut), + Name: "db.put", + Usage: "Set the value of a database key", + ArgsUsage: " ", + Flags: []cli.Flag{ + utils.DataDirFlag, + }, + } ) // initGenesis will initialise the given JSON format genesis file and writes it as @@ -611,3 +630,59 @@ func hashish(x string) bool { _, err := strconv.Atoi(x) return err != nil } + +// dbGet shows the value of a given database key +func dbGet(ctx *cli.Context) error { + if len(ctx.Args()) < 1 { + utils.Fatalf("This command requires a key as an argument.") + } + stack, _ := makeConfigNode(ctx) + defer stack.Close() + db := utils.MakeChainDatabase(ctx, stack) + defer db.Close() + key, err := hexutil.Decode(ctx.Args().Get(0)) + if err != nil { + log.Info("Could not decode the key", "error", err) + return err + } + data, err := db.Get(key) + if err != nil { + log.Info("Get operation failed", "error", err) + return err + } + fmt.Printf("key %x:\n\t%x\n", key, data) + return nil +} + +// dbPut overwrite a value in the database +func dbPut(ctx *cli.Context) error { + if len(ctx.Args()) < 2 { + utils.Fatalf("This command requires a key and a value as arguments.") + } + stack, _ := makeConfigNode(ctx) + defer stack.Close() + db := utils.MakeChainDatabase(ctx, stack) + defer db.Close() + var ( + key []byte + value []byte + data []byte + err error + ) + key, err = hexutil.Decode(ctx.Args().Get(0)) + if err != nil { + log.Info("Could not decode the key", "error", err) + return err + } + value, err = hexutil.Decode(ctx.Args().Get(1)) + if err != nil { + log.Info("Could not decode the value", "error", err) + return err + } + data, err = db.Get(key) + if err == nil { + fmt.Printf("Previous value:\n%x\n", data) + } + err = db.Put(key, value) + return nil +} diff --git a/cmd/geth/main.go b/cmd/geth/main.go index e587a5f86dd6..d2086fe90775 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -246,6 +246,8 @@ func init() { licenseCommand, // See config.go dumpConfigCommand, + dbGetCommand, + dbPutCommand, // See cmd/utils/flags_legacy.go utils.ShowDeprecated, }