Skip to content

Commit

Permalink
cmd: check parseDumpConfig for path mode
Browse files Browse the repository at this point in the history
  • Loading branch information
VM committed Jan 17, 2024
1 parent 8ed5d24 commit ccd79af
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 19 deletions.
80 changes: 64 additions & 16 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import (
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/triedb/pathdb"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -595,30 +597,76 @@ func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, eth
if ctx.NArg() > 1 {
return nil, nil, common.Hash{}, fmt.Errorf("expected 1 argument (number or hash), got %d", ctx.NArg())
}
if ctx.NArg() == 1 {
arg := ctx.Args().First()
if hashish(arg) {
hash := common.HexToHash(arg)
if number := rawdb.ReadHeaderNumber(db, hash); number != nil {
header = rawdb.ReadHeader(db, hash, *number)
scheme, err := utils.ParseStateScheme(ctx, db)
if err != nil {
utils.Fatalf("%v", err)
}
if scheme == rawdb.PathScheme {
triedb := trie.NewDatabase(db, &trie.Config{PathDB: pathdb.Defaults})
if ctx.NArg() == 1 {
arg := ctx.Args().First()
if hashish(arg) {
hash := common.HexToHash(arg)
if contain := triedb.ContainDiffLayer(hash); !contain {
log.Crit("PBSS doesn't contain specified hash", "hash", arg)
}
if number := rawdb.ReadHeaderNumber(db, hash); number != nil {
header = rawdb.ReadHeader(db, hash, *number)
} else {
return nil, nil, common.Hash{}, fmt.Errorf("block %x not found", hash)
}
} else {
return nil, nil, common.Hash{}, fmt.Errorf("block %x not found", hash)
number, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
return nil, nil, common.Hash{}, err
}
if hash := rawdb.ReadCanonicalHash(db, number); hash != (common.Hash{}) {
if contain := triedb.ContainDiffLayer(hash); !contain {
log.Crit("PBSS doesn't contain specified block number", "number", number)
}
header = rawdb.ReadHeader(db, hash, number)
} else {
return nil, nil, common.Hash{}, fmt.Errorf("header for block %d not found", number)
}
}
} else {
number, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
return nil, nil, common.Hash{}, err
}
if hash := rawdb.ReadCanonicalHash(db, number); hash != (common.Hash{}) {
header = rawdb.ReadHeader(db, hash, number)
if hash := triedb.Head(); hash != (common.Hash{}) {
if number := rawdb.ReadHeaderNumber(db, hash); number != nil {
header = rawdb.ReadHeader(db, hash, *number)
} else {
return nil, nil, common.Hash{}, fmt.Errorf("block %x not found", hash)
}
} else {
return nil, nil, common.Hash{}, fmt.Errorf("header for block %d not found", number)
return nil, nil, common.Hash{}, fmt.Errorf("no top root hash in path db")
}
}
} else {
// Use latest
header = rawdb.ReadHeadHeader(db)
if ctx.NArg() == 1 {
arg := ctx.Args().First()
if hashish(arg) {
hash := common.HexToHash(arg)
if number := rawdb.ReadHeaderNumber(db, hash); number != nil {
header = rawdb.ReadHeader(db, hash, *number)
} else {
return nil, nil, common.Hash{}, fmt.Errorf("block %x not found", hash)
}
} else {
number, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
return nil, nil, common.Hash{}, err
}
if hash := rawdb.ReadCanonicalHash(db, number); hash != (common.Hash{}) {
header = rawdb.ReadHeader(db, hash, number)
} else {
return nil, nil, common.Hash{}, fmt.Errorf("header for block %d not found", number)
}
}
} else {
// Use latest
header = rawdb.ReadHeadHeader(db)
}
}

if header == nil {
return nil, nil, common.Hash{}, errors.New("no head block found")
}
Expand Down
10 changes: 10 additions & 0 deletions trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,13 @@ func (db *Database) Head() common.Hash {
}
return pdb.Head()
}

// ContainDiffLayer returns whether root is existent.
func (db *Database) ContainDiffLayer(root common.Hash) bool {
pdb, ok := db.backend.(*pathdb.Database)
if !ok {
log.Error("not supported")
return false
}
return pdb.ContainDiffLayer(root)
}
2 changes: 1 addition & 1 deletion trie/triedb/pathdb/asyncnodebuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (nc *nodecache) node(owner common.Hash, path []byte, hash common.Hash) (*tr
}
if n.Hash != hash {
dirtyFalseMeter.Mark(1)
log.Error("Unexpected trie node in node buffer", "owner", owner, "path", path, "expect", hash, "got", n.Hash)
log.Error("Unexpected trie node in async node buffer", "owner", owner, "path", path, "expect", hash, "got", n.Hash)
return nil, newUnexpectedNodeError("dirty", hash, n.Hash, owner, path, n.Blob)
}
return n, nil
Expand Down
24 changes: 24 additions & 0 deletions trie/triedb/pathdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,27 @@ func (db *Database) Head() common.Hash {
defer db.lock.Unlock()
return db.tree.front()
}

// ContainDiffLayer returns whether root is existent.
func (db *Database) ContainDiffLayer(root common.Hash) bool {
db.lock.Lock()
defer db.lock.Unlock()

log.Info("print all layer tree", "all", db.tree.layers)
bottom := db.tree.bottom()
if l := db.tree.get(root); l != nil {
if l.rootHash() == bottom.rootHash() {
log.Info("root hash is equal to disk layer root")
} else {
log.Info("root hash locates in diff layer")
}
return true
}
_, err := bottom.Node(common.Hash{}, []byte(""), root)
if err != nil {
log.Error("Failed to retrieve root hash in disk layer", "hash", root, "error", err)
return false
}
log.Info("root hash locates in disk layer")
return true
}
7 changes: 5 additions & 2 deletions trie/triedb/pathdb/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,17 @@ func (dl *diskLayer) journal(w io.Writer) error {
for owner, subset := range bufferNodes {
entry := journalNodes{Owner: owner}
for path, node := range subset {
if owner == (common.Hash{}) && path == "" {
log.Info("node is in disk layer node buffer", "owner", owner, "path", path, "node hash", node.Hash.String())
}
entry.Nodes = append(entry.Nodes, journalNode{Path: []byte(path), Blob: node.Blob})
}
nodes = append(nodes, entry)
}
if err := rlp.Encode(w, nodes); err != nil {
return err
}
log.Debug("Journaled pathdb disk layer", "root", dl.root, "nodes", len(bufferNodes))
log.Info("Journaled pathdb disk layer", "root", dl.root, "nodes", len(bufferNodes))
return nil
}

Expand Down Expand Up @@ -329,7 +332,7 @@ func (dl *diffLayer) journal(w io.Writer) error {
if err := rlp.Encode(w, storage); err != nil {
return err
}
log.Debug("Journaled pathdb diff layer", "root", dl.root, "parent", dl.parent.rootHash(), "id", dl.stateID(), "block", dl.block, "nodes", len(dl.nodes))
log.Info("Journaled pathdb diff layer", "root", dl.root, "parent", dl.parent.rootHash(), "id", dl.stateID(), "block", dl.block, "nodes", len(dl.nodes))
return nil
}

Expand Down

0 comments on commit ccd79af

Please sign in to comment.