Skip to content

Commit 4d5cf4c

Browse files
fynnssMariusVanDerWijden
authored andcommitted
dbcmd: fix review tips
1 parent 6ed387b commit 4d5cf4c

File tree

3 files changed

+118
-137
lines changed

3 files changed

+118
-137
lines changed

cmd/geth/dbcmd.go

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@ Remove blockchain and state databases`,
102102
ArgsUsage: "<blocknum> <jobnum>",
103103
Flags: []cli.Flag{
104104
utils.DataDirFlag,
105-
utils.SyncModeFlag,
106105
},
107-
Usage: "Inspect the MPT tree of the account and contract.",
108-
Description: `This commands iterates the entrie WorldState.`,
106+
Usage: "Print detailed trie information about the structure of account trie and storage tries.",
107+
Description: `This commands iterates the entrie trie-backed state. If the 'blocknum' is not specified,
108+
the latest block number will be used by default. 'jobnum' indicates the number of coroutines concurrently traversing
109+
the account and storage trie.`,
109110
}
110111
dbCheckStateContentCmd = &cli.Command{
111112
Action: checkStateContent,
@@ -320,8 +321,8 @@ func inspectTrie(ctx *cli.Context) error {
320321
return fmt.Errorf("required arguments: %v", ctx.Command.ArgsUsage)
321322
}
322323

323-
if ctx.NArg() > 3 {
324-
return fmt.Errorf("Max 3 arguments: %v", ctx.Command.ArgsUsage)
324+
if ctx.NArg() > 2 {
325+
return fmt.Errorf("excessive number of arguments: %v", ctx.Command.ArgsUsage)
325326
}
326327

327328
var (
@@ -337,67 +338,65 @@ func inspectTrie(ctx *cli.Context) error {
337338
defer db.Close()
338339

339340
var headerBlockHash common.Hash
340-
if ctx.NArg() >= 1 {
341-
if ctx.Args().Get(0) == "latest" {
342-
headerHash := rawdb.ReadHeadHeaderHash(db)
343-
blockNumber = *(rawdb.ReadHeaderNumber(db, headerHash))
344-
} else if ctx.Args().Get(0) == "snapshot" {
345-
trieRootHash = rawdb.ReadSnapshotRoot(db)
346-
blockNumber = math.MaxUint64
347-
} else {
348-
var err error
349-
blockNumber, err = strconv.ParseUint(ctx.Args().Get(0), 10, 64)
350-
if err != nil {
351-
return fmt.Errorf("failed to Parse blocknum, Args[0]: %v, err: %v", ctx.Args().Get(0), err)
352-
}
341+
if ctx.Args().Get(0) == "latest" {
342+
headerHash := rawdb.ReadHeadHeaderHash(db)
343+
blockNumber = *(rawdb.ReadHeaderNumber(db, headerHash))
344+
} else if ctx.Args().Get(0) == "snapshot" {
345+
trieRootHash = rawdb.ReadSnapshotRoot(db)
346+
blockNumber = math.MaxUint64
347+
} else {
348+
var err error
349+
blockNumber, err = strconv.ParseUint(ctx.Args().Get(0), 10, 64)
350+
if err != nil {
351+
return fmt.Errorf("failed to parse blocknum, Args[0]: %v, err: %v", ctx.Args().Get(0), err)
353352
}
353+
}
354354

355-
if ctx.NArg() == 1 {
356-
jobnum = 1000
357-
} else {
358-
var err error
359-
jobnum, err = strconv.ParseUint(ctx.Args().Get(1), 10, 64)
360-
if err != nil {
361-
return fmt.Errorf("failed to Parse jobnum, Args[1]: %v, err: %v", ctx.Args().Get(1), err)
362-
}
355+
if ctx.NArg() == 1 {
356+
jobnum = 1000
357+
} else {
358+
var err error
359+
jobnum, err = strconv.ParseUint(ctx.Args().Get(1), 10, 64)
360+
if err != nil {
361+
return fmt.Errorf("failed to Parse jobnum, Args[1]: %v, err: %v", ctx.Args().Get(1), err)
363362
}
363+
}
364364

365-
if blockNumber != math.MaxUint64 {
366-
headerBlockHash = rawdb.ReadCanonicalHash(db, blockNumber)
367-
if headerBlockHash == (common.Hash{}) {
368-
return fmt.Errorf("ReadHeadBlockHash empry hash")
369-
}
370-
blockHeader := rawdb.ReadHeader(db, headerBlockHash, blockNumber)
371-
trieRootHash = blockHeader.Root
372-
}
373-
if (trieRootHash == common.Hash{}) {
374-
log.Error("Empty root hash")
365+
if blockNumber != math.MaxUint64 {
366+
headerBlockHash = rawdb.ReadCanonicalHash(db, blockNumber)
367+
if headerBlockHash == (common.Hash{}) {
368+
return fmt.Errorf("canonical hash for block %d not found", blockNumber)
375369
}
376-
fmt.Printf("ReadBlockHeader, root: %v, blocknum: %v\n", trieRootHash, blockNumber)
370+
blockHeader := rawdb.ReadHeader(db, headerBlockHash, blockNumber)
371+
trieRootHash = blockHeader.Root
372+
}
373+
if (trieRootHash == common.Hash{}) {
374+
log.Error("Empty root hash")
375+
}
376+
fmt.Printf("ReadBlockHeader, root: %v, blocknum: %v\n", trieRootHash, blockNumber)
377377

378-
dbScheme := rawdb.ReadStateScheme(db)
379-
var config *trie.Config
380-
if dbScheme == rawdb.PathScheme {
381-
config = &trie.Config{
382-
PathDB: pathdb.ReadOnly,
383-
}
384-
} else if dbScheme == rawdb.HashScheme {
385-
config = trie.HashDefaults
378+
dbScheme := rawdb.ReadStateScheme(db)
379+
var config *trie.Config
380+
if dbScheme == rawdb.PathScheme {
381+
config = &trie.Config{
382+
PathDB: pathdb.ReadOnly,
386383
}
384+
} else if dbScheme == rawdb.HashScheme {
385+
config = trie.HashDefaults
386+
}
387387

388-
triedb := trie.NewDatabase(db, config)
389-
theTrie, err := trie.New(trie.TrieID(trieRootHash), triedb)
390-
if err != nil {
391-
fmt.Printf("fail to new trie tree, err: %v, rootHash: %v\n", err, trieRootHash.String())
392-
return err
393-
}
394-
theInspect, err := trie.NewInspector(theTrie, triedb, trieRootHash, blockNumber, jobnum)
395-
if err != nil {
396-
return err
397-
}
398-
theInspect.Run()
399-
theInspect.DisplayResult()
388+
triedb := trie.NewDatabase(db, config)
389+
theTrie, err := trie.New(trie.TrieID(trieRootHash), triedb)
390+
if err != nil {
391+
fmt.Printf("fail to new trie tree, err: %v, rootHash: %v\n", err, trieRootHash.String())
392+
return err
393+
}
394+
theInspect, err := trie.NewInspector(theTrie, triedb, trieRootHash, blockNumber, jobnum)
395+
if err != nil {
396+
return err
400397
}
398+
theInspect.Run()
399+
theInspect.DisplayResult()
401400
return nil
402401
}
403402

0 commit comments

Comments
 (0)