diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index 28bf2af1b9..b38c48ea3d 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -151,9 +151,17 @@ var ReadOnly = &Config{ReadOnly: true} // nodeToHash computes the hash of the given node based on the tree structure. func nodeToHash(blob []byte, isVerkle bool) (common.Hash, error) { + // Compute the node hash in merkle tree manner if !isVerkle { + if len(blob) == 0 { + return types.EmptyRootHash, nil + } return crypto.Keccak256Hash(blob), nil } + // Compute the node hash in verkle tree manner + if len(blob) == 0 { + return types.EmptyVerkleHash, nil + } n, err := verkle.ParseNode(blob, 0) if err != nil { return common.Hash{}, err @@ -363,15 +371,9 @@ func (db *Database) Enable(root common.Hash) error { return errDatabaseReadOnly } // Ensure the provided state root matches the stored one. - var ( - err error - stored = types.EmptyRootHash - ) - if blob := rawdb.ReadAccountTrieNode(db.diskdb, nil); len(blob) > 0 { - stored, err = nodeToHash(blob, db.isVerkle) - if err != nil { - return err - } + stored, err := nodeToHash(rawdb.ReadAccountTrieNode(db.diskdb, nil), db.isVerkle) + if err != nil { + return err } root = types.TrieRootHash(root) if stored != root { @@ -521,9 +523,15 @@ func (db *Database) Size() (diffs common.StorageSize, nodes common.StorageSize) // Initialized returns an indicator if the state data is already // initialized in path-based scheme. func (db *Database) Initialized(genesisRoot common.Hash) bool { - var inited bool + var ( + inited bool + emptyRoot = types.EmptyRootHash + ) + if db.isVerkle { + emptyRoot = types.EmptyVerkleHash + } db.tree.forEach(func(layer layer) { - if layer.rootHash() != types.EmptyRootHash { + if layer.rootHash() != emptyRoot { inited = true } }) diff --git a/triedb/pathdb/journal.go b/triedb/pathdb/journal.go index 8a4e4adb4c..8e00aa5d86 100644 --- a/triedb/pathdb/journal.go +++ b/triedb/pathdb/journal.go @@ -92,15 +92,9 @@ func (db *Database) loadJournal(diskRoot common.Hash) (layer, error) { // loadLayers loads a pre-existing state layer backed by a key-value store. func (db *Database) loadLayers() layer { // Retrieve the root node of persistent state. - var ( - err error - root = types.EmptyRootHash - ) - if blob := rawdb.ReadAccountTrieNode(db.diskdb, nil); len(blob) > 0 { - root, err = nodeToHash(blob, db.isVerkle) - if err != nil { - log.Crit("Invalid root verkle node", "err", err) - } + root, err := nodeToHash(rawdb.ReadAccountTrieNode(db.diskdb, nil), db.isVerkle) + if err != nil { + log.Crit("Invalid root verkle node", "err", err) } // Load the layers by resolving the journal head, err := db.loadJournal(root) @@ -110,7 +104,11 @@ func (db *Database) loadLayers() layer { // journal is not matched(or missing) with the persistent state, discard // it. Display log for discarding journal, but try to avoid showing // useless information when the db is created from scratch. - if !(root == types.EmptyRootHash && errors.Is(err, errMissJournal)) { + emptyRoot := types.EmptyRootHash + if db.isVerkle { + emptyRoot = types.EmptyVerkleHash + } + if !(root == emptyRoot && errors.Is(err, errMissJournal)) { log.Info("Failed to load journal, discard it", "err", err) } // Return single layer with persistent state. @@ -271,12 +269,9 @@ func (db *Database) Journal(root common.Hash) error { } // Secondly write out the state root in disk, ensure all layers // on top are continuous with disk. - diskRoot := types.EmptyRootHash - if blob := rawdb.ReadAccountTrieNode(db.diskdb, nil); len(blob) > 0 { - diskRoot, err = nodeToHash(blob, db.isVerkle) - if err != nil { - return err - } + diskRoot, err := nodeToHash(rawdb.ReadAccountTrieNode(db.diskdb, nil), db.isVerkle) + if err != nil { + return err } if err := rlp.Encode(journal, diskRoot); err != nil { return err