triedb/pathdb: use correct empty root hash for verkle

This commit is contained in:
Gary Rong 2024-12-09 16:03:20 +08:00
parent 2d4243082a
commit a49e72564d
2 changed files with 30 additions and 27 deletions

View file

@ -151,9 +151,17 @@ var ReadOnly = &Config{ReadOnly: true}
// nodeToHash computes the hash of the given node based on the tree structure. // nodeToHash computes the hash of the given node based on the tree structure.
func nodeToHash(blob []byte, isVerkle bool) (common.Hash, error) { func nodeToHash(blob []byte, isVerkle bool) (common.Hash, error) {
// Compute the node hash in merkle tree manner
if !isVerkle { if !isVerkle {
if len(blob) == 0 {
return types.EmptyRootHash, nil
}
return crypto.Keccak256Hash(blob), 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) n, err := verkle.ParseNode(blob, 0)
if err != nil { if err != nil {
return common.Hash{}, err return common.Hash{}, err
@ -363,15 +371,9 @@ func (db *Database) Enable(root common.Hash) error {
return errDatabaseReadOnly return errDatabaseReadOnly
} }
// Ensure the provided state root matches the stored one. // Ensure the provided state root matches the stored one.
var ( stored, err := nodeToHash(rawdb.ReadAccountTrieNode(db.diskdb, nil), db.isVerkle)
err error if err != nil {
stored = types.EmptyRootHash return err
)
if blob := rawdb.ReadAccountTrieNode(db.diskdb, nil); len(blob) > 0 {
stored, err = nodeToHash(blob, db.isVerkle)
if err != nil {
return err
}
} }
root = types.TrieRootHash(root) root = types.TrieRootHash(root)
if stored != 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 returns an indicator if the state data is already
// initialized in path-based scheme. // initialized in path-based scheme.
func (db *Database) Initialized(genesisRoot common.Hash) bool { 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) { db.tree.forEach(func(layer layer) {
if layer.rootHash() != types.EmptyRootHash { if layer.rootHash() != emptyRoot {
inited = true inited = true
} }
}) })

View file

@ -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. // loadLayers loads a pre-existing state layer backed by a key-value store.
func (db *Database) loadLayers() layer { func (db *Database) loadLayers() layer {
// Retrieve the root node of persistent state. // Retrieve the root node of persistent state.
var ( root, err := nodeToHash(rawdb.ReadAccountTrieNode(db.diskdb, nil), db.isVerkle)
err error if err != nil {
root = types.EmptyRootHash log.Crit("Invalid root verkle node", "err", err)
)
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)
}
} }
// Load the layers by resolving the journal // Load the layers by resolving the journal
head, err := db.loadJournal(root) 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 // journal is not matched(or missing) with the persistent state, discard
// it. Display log for discarding journal, but try to avoid showing // it. Display log for discarding journal, but try to avoid showing
// useless information when the db is created from scratch. // 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) log.Info("Failed to load journal, discard it", "err", err)
} }
// Return single layer with persistent state. // 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 // Secondly write out the state root in disk, ensure all layers
// on top are continuous with disk. // on top are continuous with disk.
diskRoot := types.EmptyRootHash diskRoot, err := nodeToHash(rawdb.ReadAccountTrieNode(db.diskdb, nil), db.isVerkle)
if blob := rawdb.ReadAccountTrieNode(db.diskdb, nil); len(blob) > 0 { if err != nil {
diskRoot, err = nodeToHash(blob, db.isVerkle) return err
if err != nil {
return err
}
} }
if err := rlp.Encode(journal, diskRoot); err != nil { if err := rlp.Encode(journal, diskRoot); err != nil {
return err return err