Skip to content

Commit

Permalink
fix trie
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmountaintop committed Jun 1, 2024
1 parent d36698b commit 5538f1e
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 45 deletions.
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.EmptyRootHash
origin = types.EmptyLegacyTrieRootHash
values = make(map[string]string) // tracks content of the trie
)
for i, step := range rt {
Expand Down
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.EmptyRootHash {
if trie.Hash() == types.EmptyLegacyTrieRootHash {
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.EmptyRootHash {
if root != types.EmptyLegacyTrieRootHash {
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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodesA), nil)
dba.Update(rootA, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodesB), nil)
dbb.Update(rootB, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodesA), nil)
dba.Update(rootA, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodesB), nil)
dbb.Update(rootB, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
tdb.Update(root, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
triedb.Update(root, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
triedb.Update(root, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash), NewDatabase(rawdb.NewMemoryDatabase(), nil))
trie, _ := NewStateTrie(TrieID(types.EmptyLegacyTrieRootHash), 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.EmptyRootHash), triedb)
trie, _ := NewStateTrie(TrieID(types.EmptyLegacyTrieRootHash), 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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil); err != nil {
if err := triedb.Update(root, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash.Bytes()
st.val = types.EmptyLegacyTrieRootHash.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.EmptyRootHash {
if root == types.EmptyLegacyTrieRootHash {
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.EmptyRootHash), triedb)
trie, _ := NewStateTrie(TrieID(types.EmptyLegacyTrieRootHash), 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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil); err != nil {
if err := triedb.Update(root, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash), dbB)
emptyB, _ := New(TrieID(types.EmptyLegacyTrieRootHash), dbB)
emptyC := NewEmpty(dbC)
emptyD, _ := New(TrieID(types.EmptyRootHash), dbD)
emptyD, _ := New(TrieID(types.EmptyLegacyTrieRootHash), 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 @@ -809,7 +809,7 @@ func testPivotMove(t *testing.T, scheme string, tiny bool) {
var (
srcDisk = rawdb.NewMemoryDatabase()
srcTrieDB = newTestDatabase(srcDisk, scheme)
srcTrie, _ = New(TrieID(types.EmptyRootHash), srcTrieDB)
srcTrie, _ = New(TrieID(types.EmptyLegacyTrieRootHash), srcTrieDB)

deleteFn = func(key []byte, tr *Trie, states map[string][]byte) {
tr.Delete(key)
Expand Down Expand Up @@ -843,7 +843,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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodesA), nil); err != nil {
if err := srcTrieDB.Update(rootA, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash, 0, trienode.NewWithNodeSet(set), nil)
db.Update(root, types.EmptyLegacyTrieRootHash, 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.EmptyRootHash {
if id.Root != (common.Hash{}) && id.Root != types.EmptyLegacyTrieRootHash {
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.EmptyRootHash), db)
tr, _ := New(TrieID(types.EmptyLegacyTrieRootHash), 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.EmptyRootHash, nil, nil // case (a)
return types.EmptyLegacyTrieRootHash, nil, nil // case (a)
}
nodes := trienode.NewNodeSet(t.owner)
for _, path := range paths {
nodes.AddNode([]byte(path), trienode.NewDeleted())
}
return types.EmptyRootHash, nodes, nil // case (b)
return types.EmptyLegacyTrieRootHash, 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.EmptyRootHash.Bytes()), nil
return hashNode(types.EmptyLegacyTrieRootHash.Bytes()), nil
}
// If the number of changes is below 100, we let one thread handle it
h := newHasher(t.unhashed >= 100)
Expand Down
2 changes: 1 addition & 1 deletion trie/trie_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ 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{}) || stateRoot == types.EmptyLegacyTrieRootHash {
if stateRoot == (common.Hash{}) {
log.Error("Zero state root hash!")
}
Expand Down
26 changes: 13 additions & 13 deletions trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func init() {
func TestEmptyTrie(t *testing.T) {
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
res := trie.Hash()
exp := types.EmptyRootHash
exp := types.EmptyLegacyTrieRootHash
if res != exp {
t.Errorf("expected %x got %x", exp, res)
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func testMissingNode(t *testing.T, memonly bool, scheme string) {
updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer")
updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf")
root, nodes, _ := trie.Commit(false)
triedb.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
triedb.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)

if !memonly {
triedb.Commit(root, false)
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestGet(t *testing.T) {
return
}
root, nodes, _ := trie.Commit(false)
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
trie, _ = New(TrieID(root), db)
}
}
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestReplication(t *testing.T) {
updateString(trie, val.k, val.v)
}
root, nodes, _ := trie.Commit(false)
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)

// create a new trie on top of the database and check that lookups work.
trie2, err := New(TrieID(root), db)
Expand All @@ -300,7 +300,7 @@ func TestReplication(t *testing.T) {

// recreate the trie after commit
if nodes != nil {
db.Update(hash, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(hash, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
}
trie2, err = New(TrieID(hash), db)
if err != nil {
Expand Down Expand Up @@ -467,7 +467,7 @@ func runRandTest(rt randTest) bool {
scheme = rawdb.PathScheme
}
var (
origin = types.EmptyRootHash
origin = types.EmptyLegacyTrieRootHash
triedb = newTestDatabase(rawdb.NewMemoryDatabase(), scheme)
tr = NewEmpty(triedb)
values = make(map[string]string) // tracks content of the trie
Expand All @@ -492,7 +492,7 @@ func runRandTest(rt randTest) bool {
}
case opProve:
hash := tr.Hash()
if hash == types.EmptyRootHash {
if hash == types.EmptyLegacyTrieRootHash {
continue
}
proofDb := rawdb.NewMemoryDatabase()
Expand Down Expand Up @@ -764,7 +764,7 @@ func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) {
for i := 0; i < len(accounts); i++ {
var (
nonce = uint64(random.Int63())
root = types.EmptyRootHash
root = types.EmptyLegacyTrieRootHash
codekeccak = codehash.EmptyKeccakCodeHash
codeposeidon = codehash.EmptyPoseidonCodeHash
)
Expand Down Expand Up @@ -862,7 +862,7 @@ func TestCommitSequence(t *testing.T) {
}
// Flush trie -> database
root, nodes, _ := trie.Commit(false)
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
// Flush memdb -> disk (sponge)
db.Commit(root, false)
if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) {
Expand Down Expand Up @@ -903,7 +903,7 @@ func TestCommitSequenceRandomBlobs(t *testing.T) {
}
// Flush trie -> database
root, nodes, _ := trie.Commit(false)
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
// Flush memdb -> disk (sponge)
db.Commit(root, false)
if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) {
Expand Down Expand Up @@ -946,7 +946,7 @@ func TestCommitSequenceStackTrie(t *testing.T) {
// Flush trie -> database
root, nodes, _ := trie.Commit(false)
// Flush memdb -> disk (sponge)
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Commit(root, false)
// And flush stacktrie -> disk
stRoot := stTrie.Commit()
Expand Down Expand Up @@ -994,7 +994,7 @@ func TestCommitSequenceSmallRoot(t *testing.T) {
// Flush trie -> database
root, nodes, _ := trie.Commit(false)
// Flush memdb -> disk (sponge)
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
db.Commit(root, false)
// And flush stacktrie -> disk
stRoot := stTrie.Commit()
Expand Down Expand Up @@ -1163,7 +1163,7 @@ func benchmarkDerefRootFixedSize(b *testing.B, addresses [][20]byte, accounts []
}
h := trie.Hash()
root, nodes, _ := trie.Commit(false)
triedb.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
triedb.Update(root, types.EmptyLegacyTrieRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
b.StartTimer()
triedb.Dereference(h)
b.StopTimer()
Expand Down

0 comments on commit 5538f1e

Please sign in to comment.