Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/geth: add ability to get/put raw db values #21991

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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: "<hex-encoded key>",
Flags: []cli.Flag{
utils.DataDirFlag,
},
}
dbPutCommand = cli.Command{
Action: utils.MigrateFlags(dbPut),
Name: "db.put",
Usage: "Set the value of a database key",
ArgsUsage: "<hex-encoded key> <hex-encoded value>",
Flags: []cli.Flag{
utils.DataDirFlag,
},
}
)

// initGenesis will initialise the given JSON format genesis file and writes it as
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ func init() {
licenseCommand,
// See config.go
dumpConfigCommand,
dbGetCommand,
dbPutCommand,
// See cmd/utils/flags_legacy.go
utils.ShowDeprecated,
}
Expand Down