@@ -6,7 +6,7 @@ import { Event } from '../types'
66import { Synchronizer , SynchronizerOptions } from './sync'
77import { BlockFetcher } from './fetcher'
88import { VMExecution } from '../execution'
9- import type { Block , BlockHeader } from '@ethereumjs/block'
9+ import type { Block } from '@ethereumjs/block'
1010import type { TxPool } from '../service/txpool'
1111
1212interface FullSynchronizerOptions extends SynchronizerOptions {
@@ -39,7 +39,7 @@ export class FullSynchronizer extends Synchronizer {
3939 this . processBlocks = this . processBlocks . bind ( this )
4040 this . stop = this . stop . bind ( this )
4141
42- this . config . events . on ( Event . SYNC_FETCHER_FETCHED , this . processBlocks )
42+ this . config . events . on ( Event . SYNC_FETCHED_BLOCKS , this . processBlocks )
4343 this . config . events . on ( Event . SYNC_EXECUTION_VM_ERROR , this . stop )
4444
4545 void this . chain . update ( )
@@ -113,48 +113,50 @@ export class FullSynchronizer extends Synchronizer {
113113 }
114114
115115 /**
116- * Sync all blocks and state from peer starting from current height.
116+ * Called from `sync()` to sync blocks and state from peer starting from current height.
117117 * @param peer remote peer to sync with
118- * @return Resolves when sync completed
118+ * @returns a boolean if the setup was successful
119119 */
120- async syncWithPeer ( peer ?: Peer ) : Promise < BlockFetcher | null > {
121- let latest
122- if ( peer && ( latest = await this . latest ( peer ) ) ) {
123- const height = latest . number
124- if ( ! this . config . syncTargetHeight || this . config . syncTargetHeight . lt ( latest . number ) ) {
125- this . config . syncTargetHeight = height
126- this . config . logger . info ( `New sync target height=${ height } hash=${ short ( latest . hash ( ) ) } ` )
127- }
120+ async syncWithPeer ( peer ?: Peer ) : Promise < boolean > {
121+ const latest = peer ? await this . latest ( peer ) : undefined
122+ if ( ! latest ) return false
123+
124+ const height = latest . number
125+ if ( ! this . config . syncTargetHeight || this . config . syncTargetHeight . lt ( latest . number ) ) {
126+ this . config . syncTargetHeight = height
127+ this . config . logger . info ( `New sync target height=${ height } hash=${ short ( latest . hash ( ) ) } ` )
128+ }
128129
129- // Start fetcher from a safe distance behind because if the previous fetcher exited
130- // due to a reorg, it would make sense to step back and refetch.
131- const first = BN . max (
132- this . chain . blocks . height . addn ( 1 ) . subn ( this . config . safeReorgDistance ) ,
133- new BN ( 1 )
134- )
135- const count = height . sub ( first ) . addn ( 1 )
136- if ( count . gtn ( 0 ) ) {
137- if ( ! this . fetcher || this . fetcher . errored ) {
138- return new BlockFetcher ( {
139- config : this . config ,
140- pool : this . pool ,
141- chain : this . chain ,
142- interval : this . interval ,
143- first,
144- count,
145- destroyWhenDone : false ,
146- } )
147- } else {
148- const fetcherHeight = this . fetcher . first . add ( this . fetcher . count ) . subn ( 1 )
149- if ( height . gt ( fetcherHeight ) ) this . fetcher . count . iadd ( height . sub ( fetcherHeight ) )
150- this . config . logger . info ( `peer=${ peer } updated fetcher target to height=${ height } ` )
151- }
152- }
130+ // Start fetcher from a safe distance behind because if the previous fetcher exited
131+ // due to a reorg, it would make sense to step back and refetch.
132+ const first = BN . max (
133+ this . chain . blocks . height . addn ( 1 ) . subn ( this . config . safeReorgDistance ) ,
134+ new BN ( 1 )
135+ )
136+ const count = height . sub ( first ) . addn ( 1 )
137+ if ( count . lten ( 0 ) ) return false
138+ if ( ! this . fetcher || this . fetcher . errored ) {
139+ this . fetcher = new BlockFetcher ( {
140+ config : this . config ,
141+ pool : this . pool ,
142+ chain : this . chain ,
143+ interval : this . interval ,
144+ first,
145+ count,
146+ destroyWhenDone : false ,
147+ } )
148+ } else {
149+ const fetcherHeight = this . fetcher . first . add ( this . fetcher . count ) . subn ( 1 )
150+ if ( height . gt ( fetcherHeight ) ) this . fetcher . count . iadd ( height . sub ( fetcherHeight ) )
151+ this . config . logger . info ( `Updated fetcher target to height=${ height } peer=${ peer } ` )
153152 }
154- return null
153+ return true
155154 }
156155
157- async processBlocks ( blocks : Block [ ] | BlockHeader [ ] ) {
156+ /**
157+ * Process blocks fetched from the fetcher.
158+ */
159+ async processBlocks ( blocks : Block [ ] ) {
158160 if ( this . config . chainCommon . gteHardfork ( Hardfork . Merge ) ) {
159161 if ( this . fetcher !== null ) {
160162 // If we are beyond the merge block we should stop the fetcher
@@ -170,7 +172,6 @@ export class FullSynchronizer extends Synchronizer {
170172 return
171173 }
172174
173- blocks = blocks as Block [ ]
174175 const first = blocks [ 0 ] . header . number
175176 const last = blocks [ blocks . length - 1 ] . header . number
176177 const hash = short ( blocks [ 0 ] . hash ( ) )
@@ -305,38 +306,33 @@ export class FullSynchronizer extends Synchronizer {
305306 let min = new BN ( - 1 )
306307 let newSyncHeight : [ Buffer , BN ] | undefined
307308 const blockNumberList : BN [ ] = [ ]
308- data . forEach ( ( value ) => {
309+ for ( const value of data ) {
309310 const blockNumber = value [ 1 ]
310311 blockNumberList . push ( blockNumber )
311312 if ( min . eqn ( - 1 ) || blockNumber . lt ( min ) ) {
312313 min = blockNumber
313314 }
314315
315316 // Check if new sync target height can be set
316- if (
317- ( ! newSyncHeight &&
318- ( ! this . config . syncTargetHeight || blockNumber . gt ( this . config . syncTargetHeight ) ) ) ||
319- ( newSyncHeight && blockNumber . gt ( newSyncHeight [ 1 ] ) )
320- ) {
321- newSyncHeight = value
322- }
323- } )
317+ if ( newSyncHeight && blockNumber . lte ( newSyncHeight [ 1 ] ) ) continue
318+ if ( this . config . syncTargetHeight && blockNumber . lte ( this . config . syncTargetHeight ) ) continue
319+ newSyncHeight = value
320+ }
324321
325322 if ( ! newSyncHeight ) return
326323 const [ hash , height ] = newSyncHeight
327324 this . config . syncTargetHeight = height
328325 this . config . logger . info ( `New sync target height number=${ height } hash=${ short ( hash ) } ` )
329- // enqueue if we are close enough to chain head
326+ // Enqueue if we are close enough to chain head
330327 if ( min . lt ( this . chain . headers . height . addn ( 3000 ) ) ) {
331328 this . fetcher . enqueueByNumberList ( blockNumberList , min , height )
332329 }
333330 }
334331
335332 async stop ( ) : Promise < boolean > {
336- this . config . events . removeListener ( Event . SYNC_FETCHER_FETCHED , this . processBlocks )
333+ this . config . events . removeListener ( Event . SYNC_FETCHED_BLOCKS , this . processBlocks )
337334 this . config . events . removeListener ( Event . SYNC_EXECUTION_VM_ERROR , this . stop )
338-
339- return await super . stop ( )
335+ return super . stop ( )
340336 }
341337
342338 /**
0 commit comments