mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
core/state/snapshot: better test coverage
This commit is contained in:
parent
2fe10a2e60
commit
7d5fd3aab0
2 changed files with 44 additions and 4 deletions
|
|
@ -50,6 +50,29 @@ func randomAccount() []byte {
|
||||||
return data
|
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
|
// randomAccountSet generates a set of random accounts with the given strings as
|
||||||
// the account address hashes.
|
// the account address hashes.
|
||||||
func randomAccountSet(hashes ...string) map[common.Hash][]byte {
|
func randomAccountSet(hashes ...string) map[common.Hash][]byte {
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ func BenchmarkTrieGeneration(b *testing.B) {
|
||||||
for i := 0; i < num; i++ {
|
for i := 0; i < num; i++ {
|
||||||
h := common.Hash{}
|
h := common.Hash{}
|
||||||
binary.BigEndian.PutUint64(h[:], uint64(i+1))
|
binary.BigEndian.PutUint64(h[:], uint64(i+1))
|
||||||
accounts[h] = randomAccount()
|
accounts[h] = randomAccountWithSmall()
|
||||||
}
|
}
|
||||||
return accounts
|
return accounts
|
||||||
}
|
}
|
||||||
|
|
@ -139,26 +139,43 @@ func BenchmarkTrieGeneration(b *testing.B) {
|
||||||
b.Run("standard", func(b *testing.B) {
|
b.Run("standard", func(b *testing.B) {
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
|
var got common.Hash
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
it := head.(*diffLayer).AccountIterator(common.HexToHash("0x00"))
|
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.Run("pruning", func(b *testing.B) {
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
|
var got common.Hash
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
it := head.(*diffLayer).AccountIterator(common.HexToHash("0x00"))
|
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.Run("stack", func(b *testing.B) {
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
|
var got common.Hash
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
it := head.(*diffLayer).AccountIterator(common.HexToHash("0x00"))
|
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) {
|
b.Run("10K", func(b *testing.B) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue