From a95948bcd6e7457fb12aa8f0d112db48e9ad7707 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Wed, 17 Apr 2024 22:08:34 +0800 Subject: [PATCH] allow zero state root --- trie_by_cid/trie/trie_reader.go | 9 ++++----- trie_by_cid/triedb/hashdb/database.go | 7 +++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/trie_by_cid/trie/trie_reader.go b/trie_by_cid/trie/trie_reader.go index 091b0a1..09aa7e7 100644 --- a/trie_by_cid/trie/trie_reader.go +++ b/trie_by_cid/trie/trie_reader.go @@ -19,7 +19,6 @@ package trie import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/log" "github.com/cerc-io/ipld-eth-statedb/trie_by_cid/trie/triestate" "github.com/cerc-io/ipld-eth-statedb/trie_by_cid/triedb/database" @@ -35,12 +34,12 @@ type trieReader struct { // newTrieReader initializes the trie reader with the given node reader. func newTrieReader(stateRoot, owner common.Hash, db database.Database) (*trieReader, error) { - if stateRoot == (common.Hash{}) || stateRoot == types.EmptyRootHash { - if stateRoot == (common.Hash{}) { - log.Error("Zero state root hash!") - } + if stateRoot == types.EmptyRootHash { return &trieReader{owner: owner}, nil } + // Originally, passing a zero state root also caused the reader to not be set + // We allow it for storage tries for simplicity (ipld-eth-statedb change) + reader, err := db.Reader(stateRoot) if err != nil { return nil, &MissingNodeError{Owner: owner, NodeHash: stateRoot, err: err} diff --git a/trie_by_cid/triedb/hashdb/database.go b/trie_by_cid/triedb/hashdb/database.go index bd3a191..68f539f 100644 --- a/trie_by_cid/triedb/hashdb/database.go +++ b/trie_by_cid/triedb/hashdb/database.go @@ -641,8 +641,11 @@ func (db *Database) Scheme() string { // Reader retrieves a node reader belonging to the given state root. // An error will be returned if the requested state is not available. func (db *Database) Reader(root common.Hash) (*reader, error) { - if _, err := db.node(root, internal.StateTrieCodec); err != nil { - return nil, fmt.Errorf("state %#x is not available, %v", root, err) + // (ipld-eth-statedb change) Allow zero state roots for storage tries + if root != (common.Hash{}) { + if _, err := db.node(root, internal.StateTrieCodec); err != nil { + return nil, fmt.Errorf("state %#x is not available, %v", root, err) + } } return &reader{db: db}, nil }