@@ -34,6 +34,7 @@ import (
3434 "github.com/ethereum/go-ethereum/log"
3535 "github.com/ethereum/go-ethereum/metrics"
3636 "github.com/ethereum/go-ethereum/params"
37+ "github.com/ethereum/go-ethereum/trie"
3738)
3839
3940var (
@@ -104,7 +105,9 @@ type Downloader struct {
104105 genesis uint64 // Genesis block number to limit sync to (e.g. light client CHT)
105106 queue * queue // Scheduler for selecting the hashes to download
106107 peers * peerSet // Set of active peers from which download can proceed
107- stateDB ethdb.Database
108+
109+ stateDB ethdb.Database // Database to state sync into (and deduplicate via)
110+ stateBloom * trie.SyncBloom // Bloom filter for fast trie node existence checks
108111
109112 rttEstimate uint64 // Round trip time to target for download requests
110113 rttConfidence uint64 // Confidence in the estimated RTT (unit: millionths to allow atomic ops)
@@ -207,13 +210,13 @@ type BlockChain interface {
207210}
208211
209212// New creates a new downloader to fetch hashes and blocks from remote peers.
210- func New (mode SyncMode , checkpoint uint64 , stateDb ethdb.Database , mux * event.TypeMux , chain BlockChain , lightchain LightChain , dropPeer peerDropFn ) * Downloader {
213+ func New (checkpoint uint64 , stateDb ethdb.Database , stateBloom * trie. SyncBloom , mux * event.TypeMux , chain BlockChain , lightchain LightChain , dropPeer peerDropFn ) * Downloader {
211214 if lightchain == nil {
212215 lightchain = chain
213216 }
214217 dl := & Downloader {
215- mode : mode ,
216218 stateDB : stateDb ,
219+ stateBloom : stateBloom ,
217220 mux : mux ,
218221 checkpoint : checkpoint ,
219222 queue : newQueue (),
@@ -255,13 +258,15 @@ func (d *Downloader) Progress() ethereum.SyncProgress {
255258 defer d .syncStatsLock .RUnlock ()
256259
257260 current := uint64 (0 )
258- switch d . mode {
259- case FullSync :
261+ switch {
262+ case d . blockchain != nil && d . mode == FullSync :
260263 current = d .blockchain .CurrentBlock ().NumberU64 ()
261- case FastSync :
264+ case d . blockchain != nil && d . mode == FastSync :
262265 current = d .blockchain .CurrentFastBlock ().NumberU64 ()
263- case LightSync :
266+ case d . lightchain != nil :
264267 current = d .lightchain .CurrentHeader ().Number .Uint64 ()
268+ default :
269+ log .Error ("Unknown downloader chain/mode combo" , "light" , d .lightchain != nil , "full" , d .blockchain != nil , "mode" , d .mode )
265270 }
266271 return ethereum.SyncProgress {
267272 StartingBlock : d .syncStatsChainOrigin ,
@@ -363,6 +368,12 @@ func (d *Downloader) synchronise(id string, hash common.Hash, td *big.Int, mode
363368 if atomic .CompareAndSwapInt32 (& d .notified , 0 , 1 ) {
364369 log .Info ("Block synchronisation started" )
365370 }
371+ // If we are already full syncing, but have a fast-sync bloom filter laying
372+ // around, make sure it does't use memory any more. This is a special case
373+ // when the user attempts to fast sync a new empty network.
374+ if mode == FullSync && d .stateBloom != nil {
375+ d .stateBloom .Close ()
376+ }
366377 // Reset the queue, peer set and wake channels to clean any internal leftover state
367378 d .queue .Reset ()
368379 d .peers .Reset ()
@@ -1662,13 +1673,24 @@ func (d *Downloader) commitFastSyncData(results []*fetchResult, stateSync *state
16621673func (d * Downloader ) commitPivotBlock (result * fetchResult ) error {
16631674 block := types .NewBlockWithHeader (result .Header ).WithBody (result .Transactions , result .Uncles )
16641675 log .Debug ("Committing fast sync pivot as new head" , "number" , block .Number (), "hash" , block .Hash ())
1676+
1677+ // Commit the pivot block as the new head, will require full sync from here on
16651678 if _ , err := d .blockchain .InsertReceiptChain ([]* types.Block {block }, []types.Receipts {result .Receipts }); err != nil {
16661679 return err
16671680 }
16681681 if err := d .blockchain .FastSyncCommitHead (block .Hash ()); err != nil {
16691682 return err
16701683 }
16711684 atomic .StoreInt32 (& d .committed , 1 )
1685+
1686+ // If we had a bloom filter for the state sync, deallocate it now. Note, we only
1687+ // deallocate internally, but keep the empty wrapper. This ensures that if we do
1688+ // a rollback after committing the pivot and restarting fast sync, we don't end
1689+ // up using a nil bloom. Empty bloom is fine, it just returns that it does not
1690+ // have the info we need, so reach down to the database instead.
1691+ if d .stateBloom != nil {
1692+ d .stateBloom .Close ()
1693+ }
16721694 return nil
16731695}
16741696
0 commit comments