Skip to content

Commit 3ad4335

Browse files
holimankaralabe
authored andcommitted
core/state/snapshot: node behavioural difference on bloom content
1 parent fd39f72 commit 3ad4335

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

‎core/state/snapshot/difflayer.go‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/binary"
2121
"fmt"
2222
"math"
23+
"math/rand"
2324
"sort"
2425
"sync"
2526
"time"
@@ -63,8 +64,20 @@ var (
6364
// bloom filter to keep its size to a minimum (given it's size and maximum
6465
// entry count).
6566
bloomFuncs = math.Round((bloomSize / float64(aggregatorItemLimit)) * math.Log(2))
67+
68+
// bloomHashesOffset is a runtime constant which determines which part of the
69+
// the account/storage hash the hasher functions looks at, to determine the
70+
// bloom key for an account/slot. This is randomized at init(), so that the
71+
// global population of nodes do not all display the exact same behaviour with
72+
// regards to bloom content
73+
bloomHasherOffset = 0
6674
)
6775

76+
func init() {
77+
// Init bloomHasherOffset in the range [0:24] (requires 8 bytes)
78+
bloomHasherOffset = rand.Intn(25)
79+
}
80+
6881
// diffLayer represents a collection of modifications made to a state snapshot
6982
// after running a block on top. It contains one sorted list for the account trie
7083
// and one-one list for each storage tries.
@@ -100,7 +113,7 @@ func (h accountBloomHasher) Reset() { panic("not impl
100113
func (h accountBloomHasher) BlockSize() int { panic("not implemented") }
101114
func (h accountBloomHasher) Size() int { return 8 }
102115
func (h accountBloomHasher) Sum64() uint64 {
103-
return binary.BigEndian.Uint64(h[:8])
116+
return binary.BigEndian.Uint64(h[bloomHasherOffset : bloomHasherOffset+8])
104117
}
105118

106119
// storageBloomHasher is a wrapper around a [2]common.Hash to satisfy the interface
@@ -114,7 +127,8 @@ func (h storageBloomHasher) Reset() { panic("not impl
114127
func (h storageBloomHasher) BlockSize() int { panic("not implemented") }
115128
func (h storageBloomHasher) Size() int { return 8 }
116129
func (h storageBloomHasher) Sum64() uint64 {
117-
return binary.BigEndian.Uint64(h[0][:8]) ^ binary.BigEndian.Uint64(h[1][:8])
130+
return binary.BigEndian.Uint64(h[0][bloomHasherOffset:bloomHasherOffset+8]) ^
131+
binary.BigEndian.Uint64(h[1][bloomHasherOffset:bloomHasherOffset+8])
118132
}
119133

120134
// newDiffLayer creates a new diff on top of an existing snapshot, whether that's a low
@@ -205,7 +219,6 @@ func (dl *diffLayer) rebloom(origin *diskLayer) {
205219
k := float64(dl.diffed.K())
206220
n := float64(dl.diffed.N())
207221
m := float64(dl.diffed.M())
208-
209222
snapshotBloomErrorGauge.Update(math.Pow(1.0-math.Exp((-k)*(n+0.5)/(m-1)), k))
210223
}
211224

‎core/state/snapshot/difflayer_test.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/VictoriaMetrics/fastcache"
2626
"github.com/ethereum/go-ethereum/common"
27+
"github.com/ethereum/go-ethereum/crypto"
2728
"github.com/ethereum/go-ethereum/ethdb/memorydb"
2829
"github.com/ethereum/go-ethereum/rlp"
2930
)
@@ -216,7 +217,7 @@ func BenchmarkSearch(b *testing.B) {
216217
layer = fill(layer)
217218
}
218219

219-
key := common.Hash{}
220+
key := crypto.Keccak256Hash([]byte{0x13, 0x38})
220221
b.ResetTimer()
221222
for i := 0; i < b.N; i++ {
222223
layer.AccountRLP(key)
@@ -229,10 +230,12 @@ func BenchmarkSearch(b *testing.B) {
229230
// BenchmarkSearchSlot-6 100000 14554 ns/op
230231
// BenchmarkSearchSlot-6 100000 22254 ns/op (when checking parent root using mutex)
231232
// BenchmarkSearchSlot-6 100000 14551 ns/op (when checking parent number using atomic)
233+
// With bloom filter:
234+
// BenchmarkSearchSlot-6 3467835 351 ns/op
232235
func BenchmarkSearchSlot(b *testing.B) {
233236
// First, we set up 128 diff layers, with 1K items each
234-
accountKey := common.Hash{}
235-
storageKey := common.HexToHash("0x1337")
237+
accountKey := crypto.Keccak256Hash([]byte{0x13, 0x37})
238+
storageKey := crypto.Keccak256Hash([]byte{0x13, 0x37})
236239
accountRLP := randomAccount()
237240
fill := func(parent snapshot) *diffLayer {
238241
accounts := make(map[common.Hash][]byte)

0 commit comments

Comments
 (0)