Skip to content

Commit b94c988

Browse files
committed
trie: remove the sync bloom, used by fast sync ethereum#24047
1 parent 128a1a0 commit b94c988

File tree

6 files changed

+34
-250
lines changed

6 files changed

+34
-250
lines changed

core/state/sync.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
)
2828

2929
// NewStateSync create a new state trie download scheduler.
30-
func NewStateSync(root common.Hash, database ethdb.KeyValueReader, bloom *trie.SyncBloom, onLeaf func(paths [][]byte, leaf []byte) error) *trie.Sync {
30+
func NewStateSync(root common.Hash, database ethdb.KeyValueReader, onLeaf func(paths [][]byte, leaf []byte) error) *trie.Sync {
3131
// Register the storage slot callback if the external callback is specified.
3232
var onSlot func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error
3333
if onLeaf != nil {
@@ -52,6 +52,6 @@ func NewStateSync(root common.Hash, database ethdb.KeyValueReader, bloom *trie.S
5252
syncer.AddCodeEntry(common.BytesToHash(obj.CodeHash), hexpath, parent)
5353
return nil
5454
}
55-
syncer = trie.NewSync(root, database, onAccount, bloom)
55+
syncer = trie.NewSync(root, database, onAccount)
5656
return syncer
5757
}

core/state/sync_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/XinFinOrg/XDPoSChain/core/types"
2727
"github.com/XinFinOrg/XDPoSChain/crypto"
2828
"github.com/XinFinOrg/XDPoSChain/ethdb"
29-
"github.com/XinFinOrg/XDPoSChain/ethdb/memorydb"
3029
"github.com/XinFinOrg/XDPoSChain/rlp"
3130
"github.com/XinFinOrg/XDPoSChain/trie"
3231
)
@@ -133,7 +132,7 @@ func checkStateConsistency(db ethdb.Database, root common.Hash) error {
133132

134133
// Tests that an empty state is not scheduled for syncing.
135134
func TestEmptyStateSync(t *testing.T) {
136-
sync := NewStateSync(types.EmptyRootHash, rawdb.NewMemoryDatabase(), trie.NewSyncBloom(1, memorydb.New()), nil)
135+
sync := NewStateSync(types.EmptyRootHash, rawdb.NewMemoryDatabase(), nil)
137136
if nodes, paths, codes := sync.Missing(1); len(nodes) != 0 || len(paths) != 0 || len(codes) != 0 {
138137
t.Errorf(" content requested for empty state: %v, %v, %v", nodes, paths, codes)
139138
}
@@ -170,7 +169,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
170169

171170
// Create a destination state and sync with the scheduler
172171
dstDb := rawdb.NewMemoryDatabase()
173-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
172+
sched := NewStateSync(srcRoot, dstDb, nil)
174173

175174
nodes, paths, codes := sched.Missing(count)
176175
var (
@@ -249,7 +248,7 @@ func TestIterativeDelayedStateSync(t *testing.T) {
249248

250249
// Create a destination state and sync with the scheduler
251250
dstDb := rawdb.NewMemoryDatabase()
252-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
251+
sched := NewStateSync(srcRoot, dstDb, nil)
253252

254253
nodes, _, codes := sched.Missing(0)
255254
queue := append(append([]common.Hash{}, nodes...), codes...)
@@ -297,7 +296,7 @@ func testIterativeRandomStateSync(t *testing.T, count int) {
297296

298297
// Create a destination state and sync with the scheduler
299298
dstDb := rawdb.NewMemoryDatabase()
300-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
299+
sched := NewStateSync(srcRoot, dstDb, nil)
301300

302301
queue := make(map[common.Hash]struct{})
303302
nodes, _, codes := sched.Missing(count)
@@ -347,7 +346,7 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) {
347346

348347
// Create a destination state and sync with the scheduler
349348
dstDb := rawdb.NewMemoryDatabase()
350-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
349+
sched := NewStateSync(srcRoot, dstDb, nil)
351350

352351
queue := make(map[common.Hash]struct{})
353352
nodes, _, codes := sched.Missing(0)
@@ -414,7 +413,7 @@ func TestIncompleteStateSync(t *testing.T) {
414413

415414
// Create a destination state and sync with the scheduler
416415
dstDb := rawdb.NewMemoryDatabase()
417-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
416+
sched := NewStateSync(srcRoot, dstDb, nil)
418417

419418
var added []common.Hash
420419

eth/downloader/statesync.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
2727
"github.com/XinFinOrg/XDPoSChain/core/state"
2828
"github.com/XinFinOrg/XDPoSChain/ethdb"
29-
"github.com/XinFinOrg/XDPoSChain/ethdb/memorydb"
3029
"github.com/XinFinOrg/XDPoSChain/log"
3130
"github.com/XinFinOrg/XDPoSChain/trie"
3231
"golang.org/x/crypto/sha3"
@@ -294,7 +293,7 @@ type codeTask struct {
294293
func newStateSync(d *Downloader, root common.Hash) *stateSync {
295294
return &stateSync{
296295
d: d,
297-
sched: state.NewStateSync(root, d.stateDB, trie.NewSyncBloom(1, memorydb.New()), nil),
296+
sched: state.NewStateSync(root, d.stateDB, nil),
298297
keccak: sha3.NewLegacyKeccak256(),
299298
trieTasks: make(map[common.Hash]*trieTask),
300299
codeTasks: make(map[common.Hash]*codeTask),

trie/sync.go

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,17 @@ type Sync struct {
129129
codeReqs map[common.Hash]*request // Pending requests pertaining to a code hash
130130
queue *prque.Prque[int64, any] // Priority queue with the pending requests
131131
fetches map[int]int // Number of active fetches per trie node depth
132-
bloom *SyncBloom // Bloom filter for fast state existence checks
133132
}
134133

135134
// NewSync creates a new trie data download scheduler.
136-
func NewSync(root common.Hash, database ethdb.KeyValueReader, callback LeafCallback, bloom *SyncBloom) *Sync {
135+
func NewSync(root common.Hash, database ethdb.KeyValueReader, callback LeafCallback) *Sync {
137136
ts := &Sync{
138137
database: database,
139138
membatch: newSyncMemBatch(),
140139
nodeReqs: make(map[common.Hash]*request),
141140
codeReqs: make(map[common.Hash]*request),
142141
queue: prque.New[int64, any](nil), // Ugh, can contain both string and hash, whyyy
143142
fetches: make(map[int]int),
144-
bloom: bloom,
145143
}
146144
ts.AddSubTrie(root, nil, common.Hash{}, callback)
147145
return ts
@@ -156,16 +154,11 @@ func (s *Sync) AddSubTrie(root common.Hash, path []byte, parent common.Hash, cal
156154
if s.membatch.hasNode(root) {
157155
return
158156
}
159-
if s.bloom == nil || s.bloom.Contains(root[:]) {
160-
// Bloom filter says this might be a duplicate, double check.
161-
// If database says yes, then at least the trie node is present
162-
// and we hold the assumption that it's NOT legacy contract code.
163-
blob := rawdb.ReadTrieNode(s.database, root)
164-
if len(blob) > 0 {
165-
return
166-
}
167-
// False positive, bump fault meter
168-
bloomFaultMeter.Mark(1)
157+
// If database says this is a duplicate, then at least the trie node is
158+
// present, and we hold the assumption that it's NOT legacy contract code.
159+
blob := rawdb.ReadTrieNode(s.database, root)
160+
if len(blob) > 0 {
161+
return
169162
}
170163
// Assemble the new sub-trie sync request
171164
req := &request{
@@ -196,18 +189,13 @@ func (s *Sync) AddCodeEntry(hash common.Hash, path []byte, parent common.Hash) {
196189
if s.membatch.hasCode(hash) {
197190
return
198191
}
199-
if s.bloom == nil || s.bloom.Contains(hash[:]) {
200-
// Bloom filter says this might be a duplicate, double check.
201-
// If database says yes, the blob is present for sure.
202-
// Note we only check the existence with new code scheme, fast
203-
// sync is expected to run with a fresh new node. Even there
204-
// exists the code with legacy format, fetch and store with
205-
// new scheme anyway.
206-
if blob := rawdb.ReadCodeWithPrefix(s.database, hash); len(blob) > 0 {
207-
return
208-
}
209-
// False positive, bump fault meter
210-
bloomFaultMeter.Mark(1)
192+
// If database says duplicate, the blob is present for sure.
193+
// Note we only check the existence with new code scheme, fast
194+
// sync is expected to run with a fresh new node. Even there
195+
// exists the code with legacy format, fetch and store with
196+
// new scheme anyway.
197+
if blob := rawdb.ReadCodeWithPrefix(s.database, hash); len(blob) > 0 {
198+
return
211199
}
212200
// Assemble the new sub-trie sync request
213201
req := &request{
@@ -314,15 +302,9 @@ func (s *Sync) Commit(dbw ethdb.Batch) error {
314302
// Dump the membatch into a database dbw
315303
for key, value := range s.membatch.nodes {
316304
rawdb.WriteTrieNode(dbw, key, value)
317-
if s.bloom != nil {
318-
s.bloom.Add(key[:])
319-
}
320305
}
321306
for key, value := range s.membatch.codes {
322307
rawdb.WriteCode(dbw, key, value)
323-
if s.bloom != nil {
324-
s.bloom.Add(key[:])
325-
}
326308
}
327309
// Drop the membatch data and return
328310
s.membatch = newSyncMemBatch()
@@ -418,15 +400,10 @@ func (s *Sync) children(req *request, object node) ([]*request, error) {
418400
if s.membatch.hasNode(hash) {
419401
continue
420402
}
421-
if s.bloom == nil || s.bloom.Contains(node) {
422-
// Bloom filter says this might be a duplicate, double check.
423-
// If database says yes, then at least the trie node is present
424-
// and we hold the assumption that it's NOT legacy contract code.
425-
if blob := rawdb.ReadTrieNode(s.database, common.BytesToHash(node)); len(blob) > 0 {
426-
continue
427-
}
428-
// False positive, bump fault meter
429-
bloomFaultMeter.Mark(1)
403+
// If database says duplicate, then at least the trie node is present
404+
// and we hold the assumption that it's NOT legacy contract code.
405+
if blob := rawdb.ReadTrieNode(s.database, hash); len(blob) > 0 {
406+
continue
430407
}
431408
// Locally unknown Node, schedule for retrieval
432409
requests = append(requests, &request{

trie/sync_bloom.go

Lines changed: 0 additions & 191 deletions
This file was deleted.

0 commit comments

Comments
 (0)