Skip to content

Commit

Permalink
trie: remove owner and binary marshaling from stacktrie (#28291)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
holiman authored and Francesco4203 committed Oct 30, 2024
1 parent d17f6d7 commit 408dc2e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 205 deletions.
4 changes: 2 additions & 2 deletions core/state/snapshot/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,11 @@ func stackTrieGenerate(db ethdb.KeyValueWriter, scheme string, owner common.Hash
var nodeWriter trie.NodeWriteFunc
// Implement nodeWriter in case db is existed otherwise let it be nil.
if db != nil {
nodeWriter = func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
nodeWriter = func(path []byte, hash common.Hash, blob []byte) {
rawdb.WriteTrieNode(db, owner, path, hash, blob, scheme)
}
}
t := trie.NewStackTrieWithOwner(nodeWriter, owner)
t := trie.NewStackTrie(nodeWriter)
for leaf := range in {
t.TryUpdate(leaf.key[:], leaf.value)
}
Expand Down
29 changes: 16 additions & 13 deletions eth/protocols/snap/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,8 @@ func (s *Syncer) loadSyncStatus() {
s.accountBytes += common.StorageSize(len(key) + len(value))
},
}
task.genTrie = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, val []byte) {
rawdb.WriteTrieNode(task.genBatch, owner, path, hash, val, s.scheme)
task.genTrie = trie.NewStackTrie(func(path []byte, hash common.Hash, val []byte) {
rawdb.WriteTrieNode(task.genBatch, common.Hash{}, path, hash, val, s.scheme)
})

for accountHash, subtasks := range task.SubTasks {
Expand All @@ -735,9 +735,10 @@ func (s *Syncer) loadSyncStatus() {
s.storageBytes += common.StorageSize(len(key) + len(value))
},
}
subtask.genTrie = trie.NewStackTrieWithOwner(func(owner common.Hash, path []byte, hash common.Hash, val []byte) {
owner := accountHash // local assignment for stacktrie writer closure
subtask.genTrie = trie.NewStackTrie(func(path []byte, hash common.Hash, val []byte) {
rawdb.WriteTrieNode(subtask.genBatch, owner, path, hash, val, s.scheme)
}, accountHash)
})
}
}
}
Expand Down Expand Up @@ -791,8 +792,8 @@ func (s *Syncer) loadSyncStatus() {
Last: last,
SubTasks: make(map[common.Hash][]*storageTask),
genBatch: batch,
genTrie: trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, val []byte) {
rawdb.WriteTrieNode(batch, owner, path, hash, val, s.scheme)
genTrie: trie.NewStackTrie(func(path []byte, hash common.Hash, val []byte) {
rawdb.WriteTrieNode(batch, common.Hash{}, path, hash, val, s.scheme)
}),
})
log.Debug("Created account sync task", "from", next, "last", last)
Expand Down Expand Up @@ -1973,14 +1974,15 @@ func (s *Syncer) processStorageResponse(res *storageResponse) {
s.storageBytes += common.StorageSize(len(key) + len(value))
},
}
owner := account // local assignment for stacktrie writer closure
tasks = append(tasks, &storageTask{
Next: common.Hash{},
Last: r.End(),
root: acc.Root,
genBatch: batch,
genTrie: trie.NewStackTrieWithOwner(func(owner common.Hash, path []byte, hash common.Hash, val []byte) {
genTrie: trie.NewStackTrie(func(path []byte, hash common.Hash, val []byte) {
rawdb.WriteTrieNode(batch, owner, path, hash, val, s.scheme)
}, account),
}),
})
for r.Next() {
batch := ethdb.HookedBatch{
Expand All @@ -1994,9 +1996,9 @@ func (s *Syncer) processStorageResponse(res *storageResponse) {
Last: r.End(),
root: acc.Root,
genBatch: batch,
genTrie: trie.NewStackTrieWithOwner(func(owner common.Hash, path []byte, hash common.Hash, val []byte) {
genTrie: trie.NewStackTrie(func(path []byte, hash common.Hash, val []byte) {
rawdb.WriteTrieNode(batch, owner, path, hash, val, s.scheme)
}, account),
}),
})
}
for _, task := range tasks {
Expand Down Expand Up @@ -2041,9 +2043,10 @@ func (s *Syncer) processStorageResponse(res *storageResponse) {
slots += len(res.hashes[i])

if i < len(res.hashes)-1 || res.subTask == nil {
tr := trie.NewStackTrieWithOwner(func(owner common.Hash, path []byte, hash common.Hash, val []byte) {
rawdb.WriteTrieNode(batch, owner, path, hash, val, s.scheme)
}, account)
// no need to make local reassignment of account: this closure does not outlive the loop
tr := trie.NewStackTrie(func(path []byte, hash common.Hash, val []byte) {
rawdb.WriteTrieNode(batch, account, path, hash, val, s.scheme)
})
for j := 0; j < len(res.hashes[i]); j++ {
tr.Update(res.hashes[i][j][:], res.slots[i][j])
}
Expand Down
9 changes: 3 additions & 6 deletions tests/fuzzers/stacktrie/trie_fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ func (f *fuzzer) fuzz() int {
trieA = trie.NewEmpty(dbA)
spongeB = &spongeDb{sponge: sha3.NewLegacyKeccak256()}
dbB = trie.NewDatabase(rawdb.NewDatabase(spongeB), nil)
trieB = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
rawdb.WriteTrieNode(spongeB, owner, path, hash, blob, dbB.Scheme())
trieB = trie.NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
rawdb.WriteTrieNode(spongeB, common.Hash{}, path, hash, blob, dbB.Scheme())
})
vals kvs
useful bool
Expand Down Expand Up @@ -218,13 +218,10 @@ func (f *fuzzer) fuzz() int {
// Need tracked deleted nodes.
var (
nodeset = make(map[string][]byte) // path -> blob
trieC = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
trieC = trie.NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
if crypto.Keccak256Hash(blob) != hash {
panic("invalid node blob")
}
if owner != (common.Hash{}) {
panic("invalid node owner")
}
nodeset[string(path)] = common.CopyBytes(blob)
})
checked int
Expand Down
15 changes: 3 additions & 12 deletions trie/stacktrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ var (

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

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

// NewStackTrieWithOwner allocates and initializes an empty trie, but with
// the additional owner field.
func NewStackTrieWithOwner(writeFn NodeWriteFunc, owner common.Hash) *StackTrie {
stack := NewStackTrie(writeFn)
stack.owner = owner
return stack
}

func (t *StackTrie) Update(key, value []byte) {
if err := t.TryUpdate(key, value); err != nil {
log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
Expand Down Expand Up @@ -376,7 +367,7 @@ func (t *StackTrie) hash(st *stNode, path []byte) {
st.val = t.h.hashData(encodedNode)

if t.writeFn != nil {
t.writeFn(t.owner, path, common.BytesToHash(st.val), encodedNode)
t.writeFn(path, common.BytesToHash(st.val), encodedNode)
}
}

Expand Down Expand Up @@ -419,7 +410,7 @@ func (t *StackTrie) Commit() (common.Hash, error) {
// hash st.val -> ret
t.h.sha.Write(st.val)
t.h.sha.Read(ret)
t.writeFn(t.owner, nil, common.BytesToHash(ret), st.val)
t.writeFn(nil, common.BytesToHash(ret), st.val)
return common.BytesToHash(ret), nil
}
return common.BytesToHash(st.val), nil
Expand Down
120 changes: 0 additions & 120 deletions trie/stacktrie_marshalling.go

This file was deleted.

48 changes: 0 additions & 48 deletions trie/stacktrie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,51 +348,3 @@ func TestStacktrieNotModifyValues(t *testing.T) {

}
}

// TestStacktrieSerialization tests that the stacktrie works well if we
// serialize/unserialize it a lot
func TestStacktrieSerialization(t *testing.T) {
var (
st = NewStackTrieWithOwner(nil, common.Hash{0x12})
nt = NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
keyB = big.NewInt(1)
keyDelta = big.NewInt(1)
vals [][]byte
keys [][]byte
)
getValue := func(i int) []byte {
if i%2 == 0 { // large
return crypto.Keccak256(big.NewInt(int64(i)).Bytes())
} else { //small
return big.NewInt(int64(i)).Bytes()
}
}
for i := 0; i < 10; i++ {
vals = append(vals, getValue(i))
keys = append(keys, common.BigToHash(keyB).Bytes())
keyB = keyB.Add(keyB, keyDelta)
keyDelta.Add(keyDelta, common.Big1)
}
for i, k := range keys {
nt.TryUpdate(k, common.CopyBytes(vals[i]))
}

for i, k := range keys {
blob, err := st.MarshalBinary()
if err != nil {
t.Fatal(err)
}
newSt, err := NewFromBinaryV2(blob)
if err != nil {
t.Fatal(err)
}
st = newSt
st.TryUpdate(k, common.CopyBytes(vals[i]))
}
if have, want := st.Hash(), nt.Hash(); have != want {
t.Fatalf("have %#x want %#x", have, want)
}
if have, want := st.owner, (common.Hash{0x12}); have != want {
t.Fatalf("have %#x want %#x", have, want)
}
}
8 changes: 4 additions & 4 deletions trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,8 @@ func TestCommitSequenceStackTrie(t *testing.T) {
trie := NewEmpty(db)
// Another sponge is used for the stacktrie commits
stackTrieSponge := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "b"}
writeFn := func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
rawdb.WriteTrieNode(stackTrieSponge, owner, path, hash, blob, db.Scheme())
writeFn := func(path []byte, hash common.Hash, blob []byte) {
rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme())
}
stTrie := NewStackTrie(writeFn)
// Fill the trie with elements, should start 0, otherwise nodes will be nil in the first time.
Expand Down Expand Up @@ -953,8 +953,8 @@ func TestCommitSequenceSmallRoot(t *testing.T) {
trie := NewEmpty(db)
// Another sponge is used for the stacktrie commits
stackTrieSponge := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "b"}
writeFn := func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
rawdb.WriteTrieNode(stackTrieSponge, owner, path, hash, blob, db.Scheme())
writeFn := func(path []byte, hash common.Hash, blob []byte) {
rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme())
}
stTrie := NewStackTrie(writeFn)
// Add a single small-element to the trie(s)
Expand Down

0 comments on commit 408dc2e

Please sign in to comment.