Skip to content

Commit

Permalink
db/geth: fix the hash2path error
Browse files Browse the repository at this point in the history
  • Loading branch information
fynnss committed Sep 22, 2023
1 parent ac45e9d commit ac71133
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 14 deletions.
82 changes: 79 additions & 3 deletions cmd/geth/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ corruption if it is aborted during execution'!`,
},
Description: "This command looks up the specified trie node key from the database.",
}
dbTrieDeleteCmd = &cli.Command{
Action: dbTrieDelete,
Name: "trieget",
Usage: "delete the specify trie node",
ArgsUsage: "[trie owner] <hash-base key> | <path-base key>",
Flags: []cli.Flag{
utils.DataDirFlag,
utils.SyncModeFlag,
utils.MainnetFlag,
utils.StateSchemeFlag,
},
Description: "This command looks up the specified trie node key from the database.",
}
dbDeleteCmd = &cli.Command{
Action: dbDelete,
Name: "delete",
Expand Down Expand Up @@ -595,13 +608,16 @@ func dbTrieGet(ctx *cli.Context) error {
} else if scheme == rawdb.HashScheme {
if ctx.NArg() == 1 {
hashKey, err := hexutil.Decode(ctx.Args().Get(0))
hash := common.BytesToHash(hashKey)
if err != nil {
log.Info("Could not decode the value", "error", err)
return err
}
val := rawdb.ReadLegacyTrieNode(db, hash)
fmt.Println("HashKey: ", hash.String(), "node: ", trie.NodeString(hash.Bytes(), val))
val, err := db.Get(hashKey)
if err != nil {
fmt.Println("db get error: ", err)
return err
}
fmt.Println("HashKey: ", common.BytesToHash(hashKey), "node: ", trie.NodeString(hashKey, val))
} else {
fmt.Println("args too much")
}
Expand All @@ -610,6 +626,66 @@ func dbTrieGet(ctx *cli.Context) error {
return nil
}

// dbTrieGet shows the value of a given database key
func dbTrieDelete(ctx *cli.Context) error {
if ctx.NArg() < 1 || ctx.NArg() > 2 {
return fmt.Errorf("required arguments: %v", ctx.Command.ArgsUsage)
}
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, false, false)
defer db.Close()

scheme := ctx.String(utils.StateSchemeFlag.Name)
if scheme == "" {
scheme = rawdb.HashScheme
}

if scheme == rawdb.PathScheme {
var (
pathKey []byte
owner []byte
err error
)
if ctx.NArg() == 1 {
pathKey, err = hexutil.Decode(ctx.Args().Get(0))
if err != nil {
log.Info("Could not decode the value", "error", err)
return err
}
rawdb.DeleteAccountTrieNode(db, pathKey)
} else if ctx.NArg() == 2 {
owner, err = hexutil.Decode(ctx.Args().Get(0))
if err != nil {
log.Info("Could not decode the value", "error", err)
return err
}
pathKey, err = hexutil.Decode(ctx.Args().Get(1))
if err != nil {
log.Info("Could not decode the value", "error", err)
return err
}
rawdb.DeleteStorageTrieNode(db, common.BytesToHash(owner), pathKey)
}
} else if scheme == rawdb.HashScheme {
if ctx.NArg() == 1 {
hashKey, err := hexutil.Decode(ctx.Args().Get(0))
if err != nil {
log.Info("Could not decode the value", "error", err)
return err
}
err = db.Delete(hashKey)
if err != nil {
fmt.Println("db delete failed, err: ", err)
}
} else {
fmt.Println("args too much")
}
}
return nil
}

// dbDelete deletes a key from the database
func dbDelete(ctx *cli.Context) error {
if ctx.NArg() != 1 {
Expand Down
46 changes: 44 additions & 2 deletions trie/hash2path.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ func (h2p *Hash2Path) ConcurrentTraversal(theTrie *Trie, theNode node, path []by
case *fullNode:
// copy from trie/Committer (*committer).commit
collapsed := current.copy()
collapsed.Children = h2p.commitChildren(path, current)
var hash, _ = collapsed.cache()
nodebytes := nodeToBytes(collapsed)
if common.BytesToHash(hash) != common.BytesToHash(crypto.Keccak256(nodebytes)) {
fmt.Println("Hash is inconsistent, hash: ", common.BytesToHash(hash), "node hash: ", common.BytesToHash(crypto.Keccak256(nodebytes)), "node: ", collapsed.fstring(""))
panic("hash inconsistent.")
}

h2p.writeNode(path, trienode.New(common.BytesToHash(hash), nodeToBytes(collapsed)), theTrie.owner)

for idx, child := range current.Children {
Expand Down Expand Up @@ -146,7 +151,6 @@ func (h2p *Hash2Path) ConcurrentTraversal(theTrie *Trie, theNode node, path []by
if total_num%100000 == 0 {
fmt.Printf("Complete progress: %v, go routines Num: %v, h2p concurrentQueue: %v\n", total_num, runtime.NumGoroutine(), len(h2p.concurrentQueue))
}

return
case valueNode:
if !hasTerm(path) {
Expand Down Expand Up @@ -192,10 +196,48 @@ func (h2p *Hash2Path) commitChildren(path []byte, n *fullNode) [17]node {
children[i] = hn
continue
}

children[i] = h2p.commit(append(path, byte(i)), child)
}
// For the 17th child, it's possible the type is valuenode.
if n.Children[16] != nil {
children[16] = n.Children[16]
}
return children
}

// commit collapses a node down into a hash node and returns it.
func (h2p *Hash2Path) commit(path []byte, n node) node {
// if this path is clean, use available cached data
hash, dirty := n.cache()
if hash != nil && !dirty {
return hash
}
// Commit children, then parent, and remove the dirty flag.
switch cn := n.(type) {
case *shortNode:
// Commit child
collapsed := cn.copy()

// If the child is fullNode, recursively commit,
// otherwise it can only be hashNode or valueNode.
if _, ok := cn.Val.(*fullNode); ok {
collapsed.Val = h2p.commit(append(path, cn.Key...), cn.Val)
}
// The key needs to be copied, since we're adding it to the
// modified nodeset.
collapsed.Key = hexToCompact(cn.Key)
return collapsed
case *fullNode:
hashedKids := h2p.commitChildren(path, cn)
collapsed := cn.copy()
collapsed.Children = hashedKids

return collapsed
case hashNode:
return cn
default:
// nil, valuenode shouldn't be committed
panic(fmt.Sprintf("%T: invalid node: %v", n, n))
}
}
10 changes: 1 addition & 9 deletions trie/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,7 @@ func (n rawNode) EncodeRLP(w io.Writer) error {

func NodeString(hash, buf []byte) string {
node := mustDecodeNode(hash, buf)
switch current := (node).(type) {
case *shortNode:
case *fullNode:
case *hashNode:
case *valueNode:
return current.String()
default:
}
return ""
return node.fstring("NodeString: ")
}

// mustDecodeNode is a wrapper of decodeNode and panic if any error is encountered.
Expand Down

0 comments on commit ac71133

Please sign in to comment.