Skip to content

Commit

Permalink
fuzzers/stacktrie: enable test
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco4203 committed Sep 26, 2024
1 parent a30d530 commit c79ecf4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 45 deletions.
83 changes: 42 additions & 41 deletions tests/fuzzers/stacktrie/trie_fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/trie"
"golang.org/x/crypto/sha3"
Expand Down Expand Up @@ -213,47 +214,47 @@ func (f *fuzzer) fuzz() int {
}
// Ensure all the nodes are persisted correctly
// 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) {
// 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
// )
// for _, kv := range vals {
// trieC.Update(kv.k, kv.v)
// }
// rootC, _ := trieC.Commit()
// if rootA != rootC {
// panic(fmt.Sprintf("roots differ: (trie) %x != %x (stacktrie)", rootA, rootC))
// }
// trieA, _ = trie.New(trie.TrieID(rootA), dbA)
// iterA := trieA.NodeIterator(nil)
// for iterA.Next(true) {
// if iterA.Hash() == (common.Hash{}) {
// if _, present := nodeset[string(iterA.Path())]; present {
// panic("unexpected tiny node")
// }
// continue
// }
// nodeBlob, present := nodeset[string(iterA.Path())]
// if !present {
// panic("missing node")
// }
// if !bytes.Equal(nodeBlob, iterA.NodeBlob()) {
// panic("node blob is not matched")
// }
// checked += 1
// }
// if checked != len(nodeset) {
// panic("node number is not matched")
// }
var (
nodeset = make(map[string][]byte) // path -> blob
trieC = trie.NewStackTrie(func(owner common.Hash, 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
)
for _, kv := range vals {
trieC.Update(kv.k, kv.v)
}
rootC, _ := trieC.Commit()
if rootA != rootC {
panic(fmt.Sprintf("roots differ: (trie) %x != %x (stacktrie)", rootA, rootC))
}
trieA, _ = trie.New(trie.TrieID(rootA), dbA)
iterA := trieA.NodeIterator(nil)
for iterA.Next(true) {
if iterA.Hash() == (common.Hash{}) {
if _, present := nodeset[string(iterA.Path())]; present {
panic("unexpected tiny node")
}
continue
}
nodeBlob, present := nodeset[string(iterA.Path())]
if !present {
panic("missing node")
}
if !bytes.Equal(nodeBlob, iterA.NodeBlob()) {
panic("node blob is not matched")
}
checked += 1
}
if checked != len(nodeset) {
panic("node number is not matched")
}

return 1
}
5 changes: 1 addition & 4 deletions trie/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ func TestTrieTracePrevValue(t *testing.T) {
if iter.Hash() == (common.Hash{}) {
continue
}
blob, err := trie.reader.nodeBlob(iter.Path(), iter.Hash())
if err != nil {
t.Fatal(err)
}
blob := iter.NodeBlob()
seen[string(iter.Path())] = common.CopyBytes(blob)
}

Expand Down

0 comments on commit c79ecf4

Please sign in to comment.