diff --git a/core/state/snapshot/snapshot_test.go b/core/state/snapshot/snapshot_test.go index 9109238412..d223fa0496 100644 --- a/core/state/snapshot/snapshot_test.go +++ b/core/state/snapshot/snapshot_test.go @@ -50,6 +50,29 @@ func randomAccount() []byte { return data } +func randomAccountWithSmall() []byte { + root := randomHash() + var a Account + // we want some accounts to be small + if rand.Intn(100) == 15 { + a = Account{ + Balance: new(big.Int), + Nonce: uint64(1), + Root: nil, + CodeHash: nil, + } + } else { + a = Account{ + Balance: big.NewInt(rand.Int63()), + Nonce: rand.Uint64(), + Root: root[:], + CodeHash: emptyCode[:], + } + } + data, _ := rlp.EncodeToBytes(a) + return data +} + // randomAccountSet generates a set of random accounts with the given strings as // the account address hashes. func randomAccountSet(hashes ...string) map[common.Hash][]byte { diff --git a/core/state/snapshot/trie_generator_test.go b/core/state/snapshot/trie_generator_test.go index 28727f90f9..e6738beaea 100644 --- a/core/state/snapshot/trie_generator_test.go +++ b/core/state/snapshot/trie_generator_test.go @@ -115,7 +115,7 @@ func BenchmarkTrieGeneration(b *testing.B) { for i := 0; i < num; i++ { h := common.Hash{} binary.BigEndian.PutUint64(h[:], uint64(i+1)) - accounts[h] = randomAccount() + accounts[h] = randomAccountWithSmall() } return accounts } @@ -139,26 +139,43 @@ func BenchmarkTrieGeneration(b *testing.B) { b.Run("standard", func(b *testing.B) { b.ResetTimer() b.ReportAllocs() + var got common.Hash for i := 0; i < b.N; i++ { it := head.(*diffLayer).AccountIterator(common.HexToHash("0x00")) - generateTrie(it, StdGenerate) + got = generateTrie(it, StdGenerate) + } + b.StopTimer() + if exp := common.HexToHash("fecc4e1fce05c888c8acc8baa2d7677a531714668b7a09b5ede6e3e110be266b"); got != exp{ + b.Fatalf("Error: got %x exp %x", got, exp) } }) b.Run("pruning", func(b *testing.B) { b.ResetTimer() b.ReportAllocs() + var got common.Hash for i := 0; i < b.N; i++ { it := head.(*diffLayer).AccountIterator(common.HexToHash("0x00")) - generateTrie(it, PruneGenerate) + got = generateTrie(it, PruneGenerate) } + b.StopTimer() + if exp := common.HexToHash("fecc4e1fce05c888c8acc8baa2d7677a531714668b7a09b5ede6e3e110be266b"); got != exp{ + b.Fatalf("Error: got %x exp %x", got, exp) + } + }) b.Run("stack", func(b *testing.B) { b.ResetTimer() b.ReportAllocs() + var got common.Hash for i := 0; i < b.N; i++ { it := head.(*diffLayer).AccountIterator(common.HexToHash("0x00")) - generateTrie(it, StackGenerate) + got = generateTrie(it, StackGenerate) } + b.StopTimer() + if exp := common.HexToHash("fecc4e1fce05c888c8acc8baa2d7677a531714668b7a09b5ede6e3e110be266b"); got != exp{ + b.Fatalf("Error: got %x exp %x", got, exp) + } + }) }) b.Run("10K", func(b *testing.B) {