Skip to content

Commit 88fe521

Browse files
fix: build error of fuzz package (#71)
* fix: fix build error of fuzz package * fix: test short fail * chore: chagne lib * chore: reverted the changes to the library * fix: deleted duplicated commitment --------- Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de>
1 parent 05a81ec commit 88fe521

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

trie/stacktrie_fuzzer_test.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/ethereum/go-ethereum/core/types"
2929
"github.com/ethereum/go-ethereum/crypto"
3030
"github.com/ethereum/go-ethereum/trie/trienode"
31-
"golang.org/x/crypto/sha3"
3231
)
3332

3433
func FuzzStackTrie(f *testing.F) {
@@ -38,16 +37,13 @@ func FuzzStackTrie(f *testing.F) {
3837
}
3938

4039
func fuzz(data []byte, debugging bool) {
41-
// This spongeDb is used to check the sequence of disk-db-writes
4240
var (
43-
input = bytes.NewReader(data)
44-
spongeA = &spongeDb{sponge: sha3.NewLegacyKeccak256()}
45-
dbA = newTestDatabase(rawdb.NewDatabase(spongeA), rawdb.HashScheme)
46-
trieA = NewEmpty(dbA)
47-
spongeB = &spongeDb{sponge: sha3.NewLegacyKeccak256()}
48-
dbB = newTestDatabase(rawdb.NewDatabase(spongeB), rawdb.HashScheme)
49-
trieB = NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
50-
rawdb.WriteTrieNode(spongeB, common.Hash{}, path, hash, blob, dbB.Scheme())
41+
input = bytes.NewReader(data)
42+
dbA = newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)
43+
trieA = NewEmpty(dbA)
44+
memDB = rawdb.NewMemoryDatabase()
45+
trieB = NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
46+
rawdb.WriteTrieNode(memDB, common.Hash{}, path, hash, blob, rawdb.HashScheme)
5147
})
5248
vals []*kv
5349
maxElements = 10000
@@ -56,13 +52,17 @@ func fuzz(data []byte, debugging bool) {
5652
)
5753
// Fill the trie with elements
5854
for i := 0; input.Len() > 0 && i < maxElements; i++ {
55+
// Build the key
5956
k := make([]byte, 32)
6057
input.Read(k)
58+
59+
// Build the val
6160
var a uint16
6261
binary.Read(input, binary.LittleEndian, &a)
6362
a = 1 + a%100
6463
v := make([]byte, a)
6564
input.Read(v)
65+
6666
if input.Len() == 0 {
6767
// If it was exhausted while reading, the value may be all zeroes,
6868
// thus 'deletion' which is not supported on stacktrie
@@ -74,6 +74,7 @@ func fuzz(data []byte, debugging bool) {
7474
}
7575
keys[string(k)] = struct{}{}
7676
vals = append(vals, &kv{k: k, v: v})
77+
7778
trieA.MustUpdate(k, v)
7879
}
7980
if len(vals) == 0 {
@@ -103,11 +104,6 @@ func fuzz(data []byte, debugging bool) {
103104
if rootA != rootB {
104105
panic(fmt.Sprintf("roots differ: (trie) %x != %x (stacktrie)", rootA, rootB))
105106
}
106-
sumA := spongeA.sponge.Sum(nil)
107-
sumB := spongeB.sponge.Sum(nil)
108-
if !bytes.Equal(sumA, sumB) {
109-
panic(fmt.Sprintf("sequence differ: (trie) %x != %x (stacktrie)", sumA, sumB))
110-
}
111107

112108
// Ensure all the nodes are persisted correctly
113109
var (

trie/trie_test.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) {
781781
func makeAccountsWithExtra(size int, withExtra bool) (addresses [][20]byte, accounts [][]byte) {
782782
// Make the random benchmark deterministic
783783
random := rand.New(rand.NewSource(0))
784+
784785
// Create a realistic account trie to hash
785786
addresses = make([][20]byte, size)
786787
for i := 0; i < len(addresses); i++ {
@@ -801,7 +802,7 @@ func makeAccountsWithExtra(size int, withExtra bool) (addresses [][20]byte, acco
801802
}
802803
// The big.Rand function is not deterministic with regards to 64 vs 32 bit systems,
803804
// and will consume different amount of data from the rand source.
804-
//balance = new(big.Int).Rand(random, new(big.Int).Exp(common.Big2, common.Big256, nil))
805+
// balance = new(big.Int).Rand(random, new(big.Int).Exp(common.Big2, common.Big256, nil))
805806
// Therefore, we instead just read via byte buffer
806807
numBytes := random.Uint32() % 33 // [0, 32] bytes
807808
balanceBytes := make([]byte, numBytes)
@@ -862,6 +863,7 @@ func (s *spongeDb) Flush() {
862863
s.sponge.Write([]byte(key))
863864
s.sponge.Write([]byte(s.values[key]))
864865
}
866+
fmt.Println(len(s.keys))
865867
}
866868

867869
// spongeBatch is a dummy batch which immediately writes to the underlying spongedb
@@ -879,10 +881,12 @@ func (b *spongeBatch) Write() error { return nil }
879881
func (b *spongeBatch) Reset() {}
880882
func (b *spongeBatch) Replay(w ethdb.KeyValueWriter) error { return nil }
881883

882-
// TestCommitSequence tests that the trie.Commit operation writes the elements of the trie
883-
// in the expected order.
884-
// The test data was based on the 'master' code, and is basically random. It can be used
885-
// to check whether changes to the trie modifies the write order or data in any way.
884+
// TestCommitSequence tests that the trie.Commit operation writes the elements
885+
// of the trie in the expected order.
886+
//
887+
// The test data was based on the 'master' code, and is basically random.
888+
// It can be used to check whether changes to the trie modifies the write order
889+
// or data in any way.
886890
func TestCommitSequence(t *testing.T) {
887891
for i, tc := range []struct {
888892
count int
@@ -896,16 +900,19 @@ func TestCommitSequence(t *testing.T) {
896900
// This spongeDb is used to check the sequence of disk-db-writes
897901
s := &spongeDb{sponge: sha3.NewLegacyKeccak256()}
898902
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme)
899-
trie := NewEmpty(db)
903+
900904
// Fill the trie with elements
905+
trie := NewEmpty(db)
901906
for i := 0; i < tc.count; i++ {
902907
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
903908
}
904909
// Flush trie -> database
905910
root, nodes, _ := trie.Commit(false)
906911
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
912+
907913
// Flush memdb -> disk (sponge)
908914
db.Commit(root)
915+
909916
if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) {
910917
t.Errorf("test %d, disk write sequence wrong:\ngot %x exp %x\n", i, got, exp)
911918
}
@@ -927,8 +934,9 @@ func TestCommitSequenceRandomBlobs(t *testing.T) {
927934
// This spongeDb is used to check the sequence of disk-db-writes
928935
s := &spongeDb{sponge: sha3.NewLegacyKeccak256()}
929936
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme)
930-
trie := NewEmpty(db)
937+
931938
// Fill the trie with elements
939+
trie := NewEmpty(db)
932940
for i := 0; i < tc.count; i++ {
933941
key := make([]byte, 32)
934942
var val []byte
@@ -945,6 +953,7 @@ func TestCommitSequenceRandomBlobs(t *testing.T) {
945953
// Flush trie -> database
946954
root, nodes, _ := trie.Commit(false)
947955
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
956+
948957
// Flush memdb -> disk (sponge)
949958
db.Commit(root)
950959
if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) {
@@ -980,14 +989,16 @@ func TestCommitSequenceStackTrie(t *testing.T) {
980989
// For the stack trie, we need to do inserts in proper order
981990
key := make([]byte, 32)
982991
binary.BigEndian.PutUint64(key, uint64(i))
983-
var val []byte
992+
984993
// 50% short elements, 50% large elements
994+
var val []byte
985995
if prng.Intn(2) == 0 {
986996
val = make([]byte, 1+prng.Intn(32))
987997
} else {
988998
val = make([]byte, 1+prng.Intn(1024))
989999
}
9901000
prng.Read(val)
1001+
9911002
trie.Update(key, val)
9921003
stTrie.Update(key, val)
9931004
}

0 commit comments

Comments
 (0)