From c79ecf49c262e028a7fd78cd0e815c82009cff67 Mon Sep 17 00:00:00 2001 From: Francesco4203 Date: Thu, 26 Sep 2024 11:48:25 +0700 Subject: [PATCH] fuzzers/stacktrie: enable test --- tests/fuzzers/stacktrie/trie_fuzzer.go | 83 +++++++++++++------------- trie/utils_test.go | 5 +- 2 files changed, 43 insertions(+), 45 deletions(-) diff --git a/tests/fuzzers/stacktrie/trie_fuzzer.go b/tests/fuzzers/stacktrie/trie_fuzzer.go index 074e7b1c3..a0ba68e21 100644 --- a/tests/fuzzers/stacktrie/trie_fuzzer.go +++ b/tests/fuzzers/stacktrie/trie_fuzzer.go @@ -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" @@ -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 } diff --git a/trie/utils_test.go b/trie/utils_test.go index d9e229544..011d93967 100644 --- a/trie/utils_test.go +++ b/trie/utils_test.go @@ -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) }