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: tooling to verify integrity of trie nodes #24840

Merged
merged 4 commits into from
May 17, 2022
Merged
Changes from 2 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
73 changes: 73 additions & 0 deletions cmd/geth/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state/snapshot"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/trie"
Expand Down Expand Up @@ -71,6 +72,7 @@ Remove blockchain and state databases`,
dbExportCmd,
dbMetadataCmd,
dbMigrateFreezerCmd,
dbCheckStateContentCmd,
},
}
dbInspectCmd = cli.Command{
Expand All @@ -83,6 +85,16 @@ Remove blockchain and state databases`,
Usage: "Inspect the storage size for each type of data in the database",
Description: `This commands iterates the entire database. If the optional 'prefix' and 'start' arguments are provided, then the iteration is limited to the given subset of data.`,
}
dbCheckStateContentCmd = cli.Command{
Action: utils.MigrateFlags(checkStateContent),
Name: "check-state-content",
ArgsUsage: "<prefix> <start>",
Flags: utils.GroupFlags(utils.NetworkFlags, utils.DatabasePathFlags),
Usage: "Verify that state data is cryptographically correct",
Description: `This command iterates the entire database for 32-byte keys, looking for rlp-encoded trie nodes.
For each trie node encountered, it checks that the key corresponds to the keccak256(value). If this is not true, this indicates
a data corruption.`,
}
dbStatCmd = cli.Command{
Action: utils.MigrateFlags(dbStats),
Name: "stats",
Expand Down Expand Up @@ -289,6 +301,67 @@ func inspect(ctx *cli.Context) error {
return rawdb.InspectDatabase(db, prefix, start)
}

func checkStateContent(ctx *cli.Context) error {
var (
prefix []byte
start []byte
)
if ctx.NArg() > 2 {
holiman marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("Max 2 arguments: %v", ctx.Command.ArgsUsage)
holiman marked this conversation as resolved.
Show resolved Hide resolved
}
if ctx.NArg() >= 1 {
if d, err := hexutil.Decode(ctx.Args().Get(0)); err != nil {
return fmt.Errorf("failed to hex-decode 'prefix': %v", err)
} else {
prefix = d
}
}
if ctx.NArg() >= 2 {
if d, err := hexutil.Decode(ctx.Args().Get(1)); err != nil {
return fmt.Errorf("failed to hex-decode 'start': %v", err)
} else {
start = d
}
}
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, true)
defer db.Close()
var (
it = rawdb.NewKeyLengthIterator(db.NewIterator(prefix, start), 32)
hasher = crypto.NewKeccakState()
got = make([]byte, 32)
errs int
count int
startTime = time.Now()
lastLog = time.Now()
)
for it.Next() {
count++
v := it.Value()
k := it.Key()
hasher.Reset()
hasher.Write(v)
hasher.Read(got)
if !bytes.Equal(k, got) {
errs++
fmt.Printf("Error at 0x%x\n", k)
fmt.Printf(" Hash: 0x%x\n", got)
fmt.Printf(" Data: 0x%x\n", v)
}
if time.Since(lastLog) > 8*time.Second {
log.Info("Iterating the database", "at", fmt.Sprintf("%#x", k), "elapsed", time.Since(startTime))
holiman marked this conversation as resolved.
Show resolved Hide resolved
lastLog = time.Now()
}
}
if err := it.Error(); err != nil {
return err
}
log.Info("Iterated the state content", "errors", errs, "items", count)
return nil
}

func showLeveldbStats(db ethdb.KeyValueStater) {
if stats, err := db.Stat("leveldb.stats"); err != nil {
log.Warn("Failed to read database stats", "error", err)
Expand Down