Skip to content

Commit

Permalink
fix: trie tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Oct 1, 2024
1 parent d226deb commit aa50db8
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 62 deletions.
11 changes: 4 additions & 7 deletions core/types/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ var (
// EmptyZkTrieRootHash is the known root hash of an empty zktrie.
EmptyZkTrieRootHash = common.Hash{}

// EmptyLegacyTrieRootHash is the known root hash of an empty legacy trie.
EmptyLegacyTrieRootHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")

// EmptyRootHash is the known root hash of an empty trie.
EmptyRootHash = EmptyZkTrieRootHash
EmptyRootHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")

// EmptyUncleHash is the known hash of the empty uncle set.
EmptyUncleHash = rlpHash([]*Header(nil)) // 1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347
Expand All @@ -45,13 +42,13 @@ var (
EmptyPoseidonCodeHash = codehash.EmptyPoseidonCodeHash

// EmptyTxsHash is the known hash of the empty transaction set.
EmptyTxsHash = EmptyLegacyTrieRootHash
EmptyTxsHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")

// EmptyReceiptsHash is the known hash of the empty receipt set.
EmptyReceiptsHash = EmptyLegacyTrieRootHash
EmptyReceiptsHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")

// EmptyWithdrawalsHash is the known hash of the empty withdrawal set.
EmptyWithdrawalsHash = EmptyLegacyTrieRootHash
EmptyWithdrawalsHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
)

// TrieRootHash returns the hash itself if it's non-empty or the predefined
Expand Down
2 changes: 1 addition & 1 deletion tests/fuzzers/trie/trie-fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func runRandTest(rt randTest) error {
var (
triedb = trie.NewDatabase(rawdb.NewMemoryDatabase(), nil)
tr = trie.NewEmpty(triedb)
origin = types.EmptyLegacyTrieRootHash
origin = types.EmptyRootHash
values = make(map[string]string) // tracks content of the trie
)
for i, step := range rt {
Expand Down
2 changes: 1 addition & 1 deletion trie/database_supplement.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ func (db *Database) EmptyRoot() common.Hash {
if db.IsUsingZktrie() {
return types.EmptyZkTrieRootHash
} else {
return types.EmptyLegacyTrieRootHash
return types.EmptyRootHash
}
}
4 changes: 2 additions & 2 deletions trie/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (e seekError) Error() string {
}

func newNodeIterator(trie *Trie, start []byte) NodeIterator {
if trie.Hash() == types.EmptyLegacyTrieRootHash {
if trie.Hash() == types.EmptyRootHash {
return &nodeIterator{
trie: trie,
err: errIteratorEnd,
Expand Down Expand Up @@ -303,7 +303,7 @@ func (it *nodeIterator) seek(prefix []byte) error {
func (it *nodeIterator) init() (*nodeIteratorState, error) {
root := it.trie.Hash()
state := &nodeIteratorState{node: it.trie.root, index: -1}
if root != types.EmptyLegacyTrieRootHash {
if root != types.EmptyRootHash {
state.hash = root
}
return state, state.resolve(it, nil)
Expand Down
16 changes: 8 additions & 8 deletions trie/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestIterator(t *testing.T) {
trie.MustUpdate([]byte(val.k), []byte(val.v))
}
root, nodes, _ := trie.Commit(false)
db.Update(root, types.EmptyZkTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)

trie, _ = New(TrieID(root), db)
found := make(map[string]string)
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestDifferenceIterator(t *testing.T) {
triea.MustUpdate([]byte(val.k), []byte(val.v))
}
rootA, nodesA, _ := triea.Commit(false)
dba.Update(rootA, types.EmptyZkTrieRootHash, 0, trienode.NewWithNodeSet(nodesA), nil)
dba.Update(rootA, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodesA), nil)
triea, _ = New(TrieID(rootA), dba)

dbb := NewDatabase(rawdb.NewMemoryDatabase(), nil)
Expand All @@ -261,7 +261,7 @@ func TestDifferenceIterator(t *testing.T) {
trieb.MustUpdate([]byte(val.k), []byte(val.v))
}
rootB, nodesB, _ := trieb.Commit(false)
dbb.Update(rootB, types.EmptyZkTrieRootHash, 0, trienode.NewWithNodeSet(nodesB), nil)
dbb.Update(rootB, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodesB), nil)
trieb, _ = New(TrieID(rootB), dbb)

found := make(map[string]string)
Expand Down Expand Up @@ -294,7 +294,7 @@ func TestUnionIterator(t *testing.T) {
triea.MustUpdate([]byte(val.k), []byte(val.v))
}
rootA, nodesA, _ := triea.Commit(false)
dba.Update(rootA, types.EmptyZkTrieRootHash, 0, trienode.NewWithNodeSet(nodesA), nil)
dba.Update(rootA, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodesA), nil)
triea, _ = New(TrieID(rootA), dba)

dbb := NewDatabase(rawdb.NewMemoryDatabase(), nil)
Expand All @@ -303,7 +303,7 @@ func TestUnionIterator(t *testing.T) {
trieb.MustUpdate([]byte(val.k), []byte(val.v))
}
rootB, nodesB, _ := trieb.Commit(false)
dbb.Update(rootB, types.EmptyZkTrieRootHash, 0, trienode.NewWithNodeSet(nodesB), nil)
dbb.Update(rootB, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodesB), nil)
trieb, _ = New(TrieID(rootB), dbb)

di, _ := NewUnionIterator([]NodeIterator{triea.MustNodeIterator(nil), trieb.MustNodeIterator(nil)})
Expand Down Expand Up @@ -365,7 +365,7 @@ func testIteratorContinueAfterError(t *testing.T, memonly bool, scheme string) {
tr.MustUpdate([]byte(val.k), []byte(val.v))
}
root, nodes, _ := tr.Commit(false)
tdb.Update(root, types.EmptyZkTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
tdb.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
if !memonly {
tdb.Commit(root, false)
}
Expand Down Expand Up @@ -481,7 +481,7 @@ func testIteratorContinueAfterSeekError(t *testing.T, memonly bool, scheme strin
break
}
}
triedb.Update(root, types.EmptyZkTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
triedb.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
if !memonly {
triedb.Commit(root, false)
}
Expand Down Expand Up @@ -555,7 +555,7 @@ func testIteratorNodeBlob(t *testing.T, scheme string) {
trie.MustUpdate([]byte(val.k), []byte(val.v))
}
root, nodes, _ := trie.Commit(false)
triedb.Update(root, types.EmptyZkTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
triedb.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
triedb.Commit(root, false)

var found = make(map[common.Hash][]byte)
Expand Down
6 changes: 3 additions & 3 deletions trie/secure_trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ import (
)

func newEmptySecure() *StateTrie {
trie, _ := NewStateTrie(TrieID(types.EmptyZkTrieRootHash), NewDatabase(rawdb.NewMemoryDatabase(), nil))
trie, _ := NewStateTrie(TrieID(types.EmptyRootHash), NewDatabase(rawdb.NewMemoryDatabase(), nil))
return trie
}

// makeTestStateTrie creates a large enough secure trie for testing.
func makeTestStateTrie() (*Database, *StateTrie, map[string][]byte) {
// Create an empty trie
triedb := NewDatabase(rawdb.NewMemoryDatabase(), nil)
trie, _ := NewStateTrie(TrieID(types.EmptyZkTrieRootHash), triedb)
trie, _ := NewStateTrie(TrieID(types.EmptyRootHash), triedb)

// Fill it with some arbitrary data
content := make(map[string][]byte)
Expand All @@ -61,7 +61,7 @@ func makeTestStateTrie() (*Database, *StateTrie, map[string][]byte) {
}
}
root, nodes, _ := trie.Commit(false)
if err := triedb.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil); err != nil {
if err := triedb.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil); err != nil {
panic(fmt.Errorf("failed to commit db %v", err))
}
// Re-create the trie based on the new state
Expand Down
2 changes: 1 addition & 1 deletion trie/stacktrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (t *StackTrie) hash(st *stNode, path []byte) {
return

case emptyNode:
st.val = types.EmptyLegacyTrieRootHash.Bytes()
st.val = types.EmptyRootHash.Bytes()
st.key = st.key[:0]
st.typ = hashedNode
return
Expand Down
2 changes: 1 addition & 1 deletion trie/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func NewSync(root common.Hash, database ethdb.KeyValueReader, callback LeafCallb
// hex format and contain all the parent path if it's layered trie node.
func (s *Sync) AddSubTrie(root common.Hash, path []byte, parent common.Hash, parentPath []byte, callback LeafCallback) {
// Short circuit if the trie is empty or already known
if root == types.EmptyLegacyTrieRootHash {
if root == types.EmptyRootHash {
return
}
if s.membatch.hasNode(path) {
Expand Down
12 changes: 6 additions & 6 deletions trie/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func makeTestTrie(scheme string) (ethdb.Database, *Database, *StateTrie, map[str
// Create an empty trie
db := rawdb.NewMemoryDatabase()
triedb := newTestDatabase(db, scheme)
trie, _ := NewStateTrie(TrieID(types.EmptyZkTrieRootHash), triedb)
trie, _ := NewStateTrie(TrieID(types.EmptyRootHash), triedb)

// Fill it with some arbitrary data
content := make(map[string][]byte)
Expand All @@ -57,7 +57,7 @@ func makeTestTrie(scheme string) (ethdb.Database, *Database, *StateTrie, map[str
}
}
root, nodes, _ := trie.Commit(false)
if err := triedb.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil); err != nil {
if err := triedb.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil); err != nil {
panic(fmt.Errorf("failed to commit db %v", err))
}
if err := triedb.Commit(root, false); err != nil {
Expand Down Expand Up @@ -137,9 +137,9 @@ func TestEmptySync(t *testing.T) {
// dbD := newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.PathScheme)

emptyA := NewEmpty(dbA)
emptyB, _ := New(TrieID(types.EmptyZkTrieRootHash), dbB)
emptyB, _ := New(TrieID(types.EmptyRootHash), dbB)
// emptyC := NewEmpty(dbC)
// emptyD, _ := New(TrieID(types.EmptyLegacyTrieRootHash), dbD)
// emptyD, _ := New(TrieID(types.EmptyRootHash), dbD)

// for i, trie := range []*Trie{emptyA, emptyB, emptyC, emptyD} {
// sync := NewSync(trie.Hash(), memorydb.New(), nil, []*Database{dbA, dbB, dbC, dbD}[i].Scheme())
Expand Down Expand Up @@ -811,7 +811,7 @@ func testPivotMove(t *testing.T, scheme string, tiny bool) {
var (
srcDisk = rawdb.NewMemoryDatabase()
srcTrieDB = newTestDatabase(srcDisk, scheme)
srcTrie, _ = New(TrieID(types.EmptyZkTrieRootHash), srcTrieDB)
srcTrie, _ = New(TrieID(types.EmptyRootHash), srcTrieDB)

deleteFn = func(key []byte, tr *Trie, states map[string][]byte) {
tr.Delete(key)
Expand Down Expand Up @@ -845,7 +845,7 @@ func testPivotMove(t *testing.T, scheme string, tiny bool) {
writeFn([]byte{0x13, 0x44}, nil, srcTrie, stateA)

rootA, nodesA, _ := srcTrie.Commit(false)
if err := srcTrieDB.Update(rootA, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodesA), nil); err != nil {
if err := srcTrieDB.Update(rootA, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodesA), nil); err != nil {
panic(err)
}
if err := srcTrieDB.Commit(rootA, false); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions trie/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func testTrieTracer(t *testing.T, vals []struct{ k, v string }) {
insertSet := copySet(trie.tracer.inserts) // copy before commit
deleteSet := copySet(trie.tracer.deletes) // copy before commit
root, nodes, _ := trie.Commit(false)
db.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)

seen := setKeys(iterNodes(db, root))
if !compareSet(insertSet, seen) {
Expand Down Expand Up @@ -137,7 +137,7 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) {
trie.MustUpdate([]byte(val.k), []byte(val.v))
}
root, nodes, _ := trie.Commit(false)
db.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)

trie, _ = New(TrieID(root), db)
if err := verifyAccessList(orig, trie, nodes); err != nil {
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestAccessListLeak(t *testing.T) {
trie.MustUpdate([]byte(val.k), []byte(val.v))
}
root, nodes, _ := trie.Commit(false)
db.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)

var cases = []struct {
op func(tr *Trie)
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestTinyTree(t *testing.T) {
trie.MustUpdate([]byte(val.k), randBytes(32))
}
root, set, _ := trie.Commit(false)
db.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(set), nil)
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(set), nil)

parent := root
trie, _ = New(TrieID(root), db)
Expand Down
10 changes: 5 additions & 5 deletions trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func New(id *ID, db *Database) (*Trie, error) {
reader: reader,
tracer: newTracer(),
}
if id.Root != (common.Hash{}) && id.Root != types.EmptyLegacyTrieRootHash {
if id.Root != (common.Hash{}) && id.Root != types.EmptyRootHash {
rootnode, err := trie.resolveAndTrack(id.Root[:], nil)
if err != nil {
return nil, err
Expand All @@ -101,7 +101,7 @@ func New(id *ID, db *Database) (*Trie, error) {

// NewEmpty is a shortcut to create empty tree. It's mostly used in tests.
func NewEmpty(db *Database) *Trie {
tr, _ := New(TrieID(types.EmptyZkTrieRootHash), db)
tr, _ := New(TrieID(types.EmptyRootHash), db)
return tr
}

Expand Down Expand Up @@ -619,13 +619,13 @@ func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error)
if t.root == nil {
paths := t.tracer.deletedNodes()
if len(paths) == 0 {
return types.EmptyLegacyTrieRootHash, nil, nil // case (a)
return types.EmptyRootHash, nil, nil // case (a)
}
nodes := trienode.NewNodeSet(t.owner)
for _, path := range paths {
nodes.AddNode([]byte(path), trienode.NewDeleted())
}
return types.EmptyLegacyTrieRootHash, nodes, nil // case (b)
return types.EmptyRootHash, nodes, nil // case (b)
}
// Derive the hash for all dirty nodes first. We hold the assumption
// in the following procedure that all nodes are hashed.
Expand All @@ -650,7 +650,7 @@ func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error)
// hashRoot calculates the root hash of the given trie
func (t *Trie) hashRoot() (node, node) {
if t.root == nil {
return hashNode(types.EmptyLegacyTrieRootHash.Bytes()), nil
return hashNode(types.EmptyRootHash.Bytes()), nil
}
// If the number of changes is below 100, we let one thread handle it
h := newHasher(t.unhashed >= 100)
Expand Down
13 changes: 5 additions & 8 deletions trie/trie_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package trie
import (
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/trie/triestate"
)

Expand All @@ -45,14 +46,10 @@ type trieReader struct {

// newTrieReader initializes the trie reader with the given node reader.
func newTrieReader(stateRoot, owner common.Hash, db *Database) (*trieReader, error) {
// if stateRoot == (common.Hash{}) || stateRoot == types.EmptyRootHash {
// if stateRoot == (common.Hash{}) {
// log.Error("Zero state root hash!")
// }
// return &trieReader{owner: owner}, nil
// }
if stateRoot == types.EmptyZkTrieRootHash {
// log.Error("Zero state root hash!")
if stateRoot == (common.Hash{}) || stateRoot == types.EmptyRootHash {
if stateRoot == (common.Hash{}) {
log.Error("Zero state root hash!")
}
return &trieReader{owner: owner}, nil
}
reader, err := db.Reader(stateRoot)
Expand Down
Loading

0 comments on commit aa50db8

Please sign in to comment.