core/state/snapshot: better test coverage

This commit is contained in:
Martin Holst Swende 2020-02-18 14:04:14 +01:00
parent 2fe10a2e60
commit 7d5fd3aab0
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 44 additions and 4 deletions

View file

@ -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 {

View file

@ -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) {