Skip to content

Commit b9e74ac

Browse files
holimantcrypt25519
authored andcommitted
trie: remove owner and binary marshaling from stacktrie (ethereum#28291)
This change - Removes the owner-notion from a stacktrie; the owner is only ever needed for comitting to the database, but the commit-function, the `writeFn` is provided by the caller, so the caller can just set the owner into the `writeFn` instead of having it passed through the stacktrie. - Removes the `encoding.BinaryMarshaler`/`encoding.BinaryUnmarshaler` interface from stacktrie. We're not using it, and it is doubtful whether anyone downstream is either.
1 parent a9a4421 commit b9e74ac

File tree

8 files changed

+29
-206
lines changed

8 files changed

+29
-206
lines changed

core/state/snapshot/conversion.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,11 @@ func generateTrieRoot(db ethdb.KeyValueWriter, scheme string, it Iterator, accou
364364
func stackTrieGenerate(db ethdb.KeyValueWriter, scheme string, owner common.Hash, in chan trieKV, out chan common.Hash) {
365365
var nodeWriter trie.NodeWriteFunc
366366
if db != nil {
367-
nodeWriter = func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
367+
nodeWriter = func(path []byte, hash common.Hash, blob []byte) {
368368
rawdb.WriteTrieNode(db, owner, path, hash, blob, scheme)
369369
}
370370
}
371-
t := trie.NewStackTrieWithOwner(nodeWriter, owner)
371+
t := trie.NewStackTrie(nodeWriter)
372372
for leaf := range in {
373373
t.Update(leaf.key[:], leaf.value)
374374
}

core/state/statedb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ func (s *StateDB) fastDeleteStorage(addrHash common.Hash, root common.Hash) (boo
964964
nodes = trienode.NewNodeSet(addrHash)
965965
slots = make(map[common.Hash][]byte)
966966
)
967-
stack := trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
967+
stack := trie.NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
968968
nodes.AddNode(path, trienode.NewDeleted())
969969
size += common.StorageSize(len(path))
970970
})

eth/protocols/snap/sync.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,8 @@ func (s *Syncer) loadSyncStatus() {
738738
s.accountBytes += common.StorageSize(len(key) + len(value))
739739
},
740740
}
741-
task.genTrie = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, val []byte) {
742-
rawdb.WriteTrieNode(task.genBatch, owner, path, hash, val, s.scheme)
741+
task.genTrie = trie.NewStackTrie(func(path []byte, hash common.Hash, val []byte) {
742+
rawdb.WriteTrieNode(task.genBatch, common.Hash{}, path, hash, val, s.scheme)
743743
})
744744
for accountHash, subtasks := range task.SubTasks {
745745
for _, subtask := range subtasks {
@@ -751,9 +751,10 @@ func (s *Syncer) loadSyncStatus() {
751751
s.storageBytes += common.StorageSize(len(key) + len(value))
752752
},
753753
}
754-
subtask.genTrie = trie.NewStackTrieWithOwner(func(owner common.Hash, path []byte, hash common.Hash, val []byte) {
754+
owner := accountHash // local assignment for stacktrie writer closure
755+
subtask.genTrie = trie.NewStackTrie(func(path []byte, hash common.Hash, val []byte) {
755756
rawdb.WriteTrieNode(subtask.genBatch, owner, path, hash, val, s.scheme)
756-
}, accountHash)
757+
})
757758
}
758759
}
759760
}
@@ -810,8 +811,8 @@ func (s *Syncer) loadSyncStatus() {
810811
Last: last,
811812
SubTasks: make(map[common.Hash][]*storageTask),
812813
genBatch: batch,
813-
genTrie: trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, val []byte) {
814-
rawdb.WriteTrieNode(batch, owner, path, hash, val, s.scheme)
814+
genTrie: trie.NewStackTrie(func(path []byte, hash common.Hash, val []byte) {
815+
rawdb.WriteTrieNode(batch, common.Hash{}, path, hash, val, s.scheme)
815816
}),
816817
})
817818
log.Debug("Created account sync task", "from", next, "last", last)
@@ -2004,14 +2005,15 @@ func (s *Syncer) processStorageResponse(res *storageResponse) {
20042005
s.storageBytes += common.StorageSize(len(key) + len(value))
20052006
},
20062007
}
2008+
owner := account // local assignment for stacktrie writer closure
20072009
tasks = append(tasks, &storageTask{
20082010
Next: common.Hash{},
20092011
Last: r.End(),
20102012
root: acc.Root,
20112013
genBatch: batch,
2012-
genTrie: trie.NewStackTrieWithOwner(func(owner common.Hash, path []byte, hash common.Hash, val []byte) {
2014+
genTrie: trie.NewStackTrie(func(path []byte, hash common.Hash, val []byte) {
20132015
rawdb.WriteTrieNode(batch, owner, path, hash, val, s.scheme)
2014-
}, account),
2016+
}),
20152017
})
20162018
for r.Next() {
20172019
batch := ethdb.HookedBatch{
@@ -2025,9 +2027,9 @@ func (s *Syncer) processStorageResponse(res *storageResponse) {
20252027
Last: r.End(),
20262028
root: acc.Root,
20272029
genBatch: batch,
2028-
genTrie: trie.NewStackTrieWithOwner(func(owner common.Hash, path []byte, hash common.Hash, val []byte) {
2030+
genTrie: trie.NewStackTrie(func(path []byte, hash common.Hash, val []byte) {
20292031
rawdb.WriteTrieNode(batch, owner, path, hash, val, s.scheme)
2030-
}, account),
2032+
}),
20312033
})
20322034
}
20332035
for _, task := range tasks {
@@ -2072,9 +2074,10 @@ func (s *Syncer) processStorageResponse(res *storageResponse) {
20722074
slots += len(res.hashes[i])
20732075

20742076
if i < len(res.hashes)-1 || res.subTask == nil {
2075-
tr := trie.NewStackTrieWithOwner(func(owner common.Hash, path []byte, hash common.Hash, val []byte) {
2076-
rawdb.WriteTrieNode(batch, owner, path, hash, val, s.scheme)
2077-
}, account)
2077+
// no need to make local reassignment of account: this closure does not outlive the loop
2078+
tr := trie.NewStackTrie(func(path []byte, hash common.Hash, val []byte) {
2079+
rawdb.WriteTrieNode(batch, account, path, hash, val, s.scheme)
2080+
})
20782081
for j := 0; j < len(res.hashes[i]); j++ {
20792082
tr.Update(res.hashes[i][j][:], res.slots[i][j])
20802083
}

tests/fuzzers/stacktrie/trie_fuzzer.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ func (f *fuzzer) fuzz() int {
140140
trieA = trie.NewEmpty(dbA)
141141
spongeB = &spongeDb{sponge: sha3.NewLegacyKeccak256()}
142142
dbB = trie.NewDatabase(rawdb.NewDatabase(spongeB), nil)
143-
trieB = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
144-
rawdb.WriteTrieNode(spongeB, owner, path, hash, blob, dbB.Scheme())
143+
trieB = trie.NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
144+
rawdb.WriteTrieNode(spongeB, common.Hash{}, path, hash, blob, dbB.Scheme())
145145
})
146146
vals []kv
147147
useful bool
@@ -205,13 +205,10 @@ func (f *fuzzer) fuzz() int {
205205
// Ensure all the nodes are persisted correctly
206206
var (
207207
nodeset = make(map[string][]byte) // path -> blob
208-
trieC = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
208+
trieC = trie.NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
209209
if crypto.Keccak256Hash(blob) != hash {
210210
panic("invalid node blob")
211211
}
212-
if owner != (common.Hash{}) {
213-
panic("invalid node owner")
214-
}
215212
nodeset[string(path)] = common.CopyBytes(blob)
216213
})
217214
checked int

trie/stacktrie.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ var (
3333

3434
// NodeWriteFunc is used to provide all information of a dirty node for committing
3535
// so that callers can flush nodes into database with desired scheme.
36-
type NodeWriteFunc = func(owner common.Hash, path []byte, hash common.Hash, blob []byte)
36+
type NodeWriteFunc = func(path []byte, hash common.Hash, blob []byte)
3737

3838
// StackTrie is a trie implementation that expects keys to be inserted
3939
// in order. Once it determines that a subtree will no longer be inserted
4040
// into, it will hash it and free up the memory it uses.
4141
type StackTrie struct {
42-
owner common.Hash // the owner of the trie
4342
writeFn NodeWriteFunc // function for committing nodes, can be nil
4443
root *stNode
4544
h *hasher
@@ -54,14 +53,6 @@ func NewStackTrie(writeFn NodeWriteFunc) *StackTrie {
5453
}
5554
}
5655

57-
// NewStackTrieWithOwner allocates and initializes an empty trie, but with
58-
// the additional owner field.
59-
func NewStackTrieWithOwner(writeFn NodeWriteFunc, owner common.Hash) *StackTrie {
60-
stack := NewStackTrie(writeFn)
61-
stack.owner = owner
62-
return stack
63-
}
64-
6556
// Update inserts a (key, value) pair into the stack trie.
6657
func (t *StackTrie) Update(key, value []byte) error {
6758
k := keybytesToHex(key)
@@ -371,7 +362,7 @@ func (t *StackTrie) hash(st *stNode, path []byte) {
371362
// input values
372363
st.val = t.h.hashData(encodedNode)
373364
if t.writeFn != nil {
374-
t.writeFn(t.owner, path, common.BytesToHash(st.val), encodedNode)
365+
t.writeFn(path, common.BytesToHash(st.val), encodedNode)
375366
}
376367
}
377368

@@ -416,6 +407,6 @@ func (t *StackTrie) Commit() (h common.Hash, err error) {
416407
t.h.sha.Write(st.val)
417408
t.h.sha.Read(h[:])
418409

419-
t.writeFn(t.owner, nil, h, st.val)
410+
t.writeFn(nil, h, st.val)
420411
return h, nil
421412
}

trie/stacktrie_marshalling.go

Lines changed: 0 additions & 120 deletions
This file was deleted.

trie/stacktrie_test.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -376,51 +376,3 @@ func TestStacktrieNotModifyValues(t *testing.T) {
376376
}
377377
}
378378
}
379-
380-
// TestStacktrieSerialization tests that the stacktrie works well if we
381-
// serialize/unserialize it a lot
382-
func TestStacktrieSerialization(t *testing.T) {
383-
var (
384-
st = NewStackTrieWithOwner(nil, common.Hash{0x12})
385-
nt = NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
386-
keyB = big.NewInt(1)
387-
keyDelta = big.NewInt(1)
388-
vals [][]byte
389-
keys [][]byte
390-
)
391-
getValue := func(i int) []byte {
392-
if i%2 == 0 { // large
393-
return crypto.Keccak256(big.NewInt(int64(i)).Bytes())
394-
} else { //small
395-
return big.NewInt(int64(i)).Bytes()
396-
}
397-
}
398-
for i := 0; i < 10; i++ {
399-
vals = append(vals, getValue(i))
400-
keys = append(keys, common.BigToHash(keyB).Bytes())
401-
keyB = keyB.Add(keyB, keyDelta)
402-
keyDelta.Add(keyDelta, common.Big1)
403-
}
404-
for i, k := range keys {
405-
nt.Update(k, common.CopyBytes(vals[i]))
406-
}
407-
408-
for i, k := range keys {
409-
blob, err := st.MarshalBinary()
410-
if err != nil {
411-
t.Fatal(err)
412-
}
413-
newSt, err := NewFromBinaryV2(blob)
414-
if err != nil {
415-
t.Fatal(err)
416-
}
417-
st = newSt
418-
st.Update(k, common.CopyBytes(vals[i]))
419-
}
420-
if have, want := st.Hash(), nt.Hash(); have != want {
421-
t.Fatalf("have %#x want %#x", have, want)
422-
}
423-
if have, want := st.owner, (common.Hash{0x12}); have != want {
424-
t.Fatalf("have %#x want %#x", have, want)
425-
}
426-
}

trie/trie_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -912,8 +912,8 @@ func TestCommitSequenceStackTrie(t *testing.T) {
912912
trie := NewEmpty(db)
913913
// Another sponge is used for the stacktrie commits
914914
stackTrieSponge := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "b"}
915-
stTrie := NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
916-
rawdb.WriteTrieNode(stackTrieSponge, owner, path, hash, blob, db.Scheme())
915+
stTrie := NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
916+
rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme())
917917
})
918918
// Fill the trie with elements
919919
for i := 0; i < count; i++ {
@@ -971,8 +971,8 @@ func TestCommitSequenceSmallRoot(t *testing.T) {
971971
trie := NewEmpty(db)
972972
// Another sponge is used for the stacktrie commits
973973
stackTrieSponge := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "b"}
974-
stTrie := NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
975-
rawdb.WriteTrieNode(stackTrieSponge, owner, path, hash, blob, db.Scheme())
974+
stTrie := NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
975+
rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme())
976976
})
977977
// Add a single small-element to the trie(s)
978978
key := make([]byte, 5)

0 commit comments

Comments
 (0)