@@ -101,34 +101,6 @@ export async function importBlock(
101
101
this . metrics ?. importBlock . bySource . inc ( { source} ) ;
102
102
this . logger . verbose ( "Added block to forkchoice and state cache" , { slot : blockSlot , root : blockRootHex } ) ;
103
103
104
- // We want to import block asap so call all event handler in the next event loop
105
- callInNextEventLoop ( async ( ) => {
106
- this . emitter . emit ( routes . events . EventType . block , {
107
- block : blockRootHex ,
108
- slot : blockSlot ,
109
- executionOptimistic : blockSummary != null && isOptimisticBlock ( blockSummary ) ,
110
- } ) ;
111
-
112
- // dataPromise will not end up here, but preDeneb could. In future we might also allow syncing
113
- // out of data range blocks and import then in forkchoice although one would not be able to
114
- // attest and propose with such head similar to optimistic sync
115
- if ( blockInput . type === BlockInputType . availableData ) {
116
- const { blobsSource, blobs} = blockInput . blockData ;
117
-
118
- this . metrics ?. importBlock . blobsBySource . inc ( { blobsSource} ) ;
119
- for ( const blobSidecar of blobs ) {
120
- const { index, kzgCommitment} = blobSidecar ;
121
- this . emitter . emit ( routes . events . EventType . blobSidecar , {
122
- blockRoot : blockRootHex ,
123
- slot : blockSlot ,
124
- index,
125
- kzgCommitment : toHexString ( kzgCommitment ) ,
126
- versionedHash : toHexString ( kzgCommitmentToVersionedHash ( kzgCommitment ) ) ,
127
- } ) ;
128
- }
129
- }
130
- } ) ;
131
-
132
104
// 3. Import attestations to fork choice
133
105
//
134
106
// - For each attestation
@@ -413,32 +385,58 @@ export async function importBlock(
413
385
// Send block events, only for recent enough blocks
414
386
415
387
if ( this . clock . currentSlot - blockSlot < EVENTSTREAM_EMIT_RECENT_BLOCK_SLOTS ) {
416
- // NOTE: Skip looping if there are no listeners from the API
417
- if ( this . emitter . listenerCount ( routes . events . EventType . voluntaryExit ) ) {
418
- for ( const voluntaryExit of block . message . body . voluntaryExits ) {
419
- this . emitter . emit ( routes . events . EventType . voluntaryExit , voluntaryExit ) ;
388
+ // We want to import block asap so call all event handler in the next event loop
389
+ callInNextEventLoop ( ( ) => {
390
+ // NOTE: Skip emitting if there are no listeners from the API
391
+ if ( this . emitter . listenerCount ( routes . events . EventType . block ) ) {
392
+ this . emitter . emit ( routes . events . EventType . block , {
393
+ block : blockRootHex ,
394
+ slot : blockSlot ,
395
+ executionOptimistic : blockSummary != null && isOptimisticBlock ( blockSummary ) ,
396
+ } ) ;
420
397
}
421
- }
422
- if ( this . emitter . listenerCount ( routes . events . EventType . blsToExecutionChange ) ) {
423
- for ( const blsToExecutionChange of ( block . message . body as capella . BeaconBlockBody ) . blsToExecutionChanges ?? [ ] ) {
424
- this . emitter . emit ( routes . events . EventType . blsToExecutionChange , blsToExecutionChange ) ;
398
+ if ( this . emitter . listenerCount ( routes . events . EventType . voluntaryExit ) ) {
399
+ for ( const voluntaryExit of block . message . body . voluntaryExits ) {
400
+ this . emitter . emit ( routes . events . EventType . voluntaryExit , voluntaryExit ) ;
401
+ }
425
402
}
426
- }
427
- if ( this . emitter . listenerCount ( routes . events . EventType . attestation ) ) {
428
- for ( const attestation of block . message . body . attestations ) {
429
- this . emitter . emit ( routes . events . EventType . attestation , attestation ) ;
403
+ if ( this . emitter . listenerCount ( routes . events . EventType . blsToExecutionChange ) ) {
404
+ for ( const blsToExecutionChange of ( block . message as capella . BeaconBlock ) . body . blsToExecutionChanges ?? [ ] ) {
405
+ this . emitter . emit ( routes . events . EventType . blsToExecutionChange , blsToExecutionChange ) ;
406
+ }
430
407
}
431
- }
432
- if ( this . emitter . listenerCount ( routes . events . EventType . attesterSlashing ) ) {
433
- for ( const attesterSlashing of block . message . body . attesterSlashings ) {
434
- this . emitter . emit ( routes . events . EventType . attesterSlashing , attesterSlashing ) ;
408
+ if ( this . emitter . listenerCount ( routes . events . EventType . attestation ) ) {
409
+ for ( const attestation of block . message . body . attestations ) {
410
+ this . emitter . emit ( routes . events . EventType . attestation , attestation ) ;
411
+ }
435
412
}
436
- }
437
- if ( this . emitter . listenerCount ( routes . events . EventType . proposerSlashing ) ) {
438
- for ( const proposerSlashing of block . message . body . proposerSlashings ) {
439
- this . emitter . emit ( routes . events . EventType . proposerSlashing , proposerSlashing ) ;
413
+ if ( this . emitter . listenerCount ( routes . events . EventType . attesterSlashing ) ) {
414
+ for ( const attesterSlashing of block . message . body . attesterSlashings ) {
415
+ this . emitter . emit ( routes . events . EventType . attesterSlashing , attesterSlashing ) ;
416
+ }
440
417
}
441
- }
418
+ if ( this . emitter . listenerCount ( routes . events . EventType . proposerSlashing ) ) {
419
+ for ( const proposerSlashing of block . message . body . proposerSlashings ) {
420
+ this . emitter . emit ( routes . events . EventType . proposerSlashing , proposerSlashing ) ;
421
+ }
422
+ }
423
+ if (
424
+ blockInput . type === BlockInputType . availableData &&
425
+ this . emitter . listenerCount ( routes . events . EventType . blobSidecar )
426
+ ) {
427
+ const { blobs} = blockInput . blockData ;
428
+ for ( const blobSidecar of blobs ) {
429
+ const { index, kzgCommitment} = blobSidecar ;
430
+ this . emitter . emit ( routes . events . EventType . blobSidecar , {
431
+ blockRoot : blockRootHex ,
432
+ slot : blockSlot ,
433
+ index,
434
+ kzgCommitment : toHexString ( kzgCommitment ) ,
435
+ versionedHash : toHexString ( kzgCommitmentToVersionedHash ( kzgCommitment ) ) ,
436
+ } ) ;
437
+ }
438
+ }
439
+ } ) ;
442
440
}
443
441
444
442
// Register stat metrics about the block after importing it
@@ -452,6 +450,13 @@ export async function importBlock(
452
450
fullyVerifiedBlock . postState . epochCtx . currentSyncCommitteeIndexed . validatorIndices
453
451
) ;
454
452
}
453
+ // dataPromise will not end up here, but preDeneb could. In future we might also allow syncing
454
+ // out of data range blocks and import then in forkchoice although one would not be able to
455
+ // attest and propose with such head similar to optimistic sync
456
+ if ( blockInput . type === BlockInputType . availableData ) {
457
+ const { blobsSource} = blockInput . blockData ;
458
+ this . metrics ?. importBlock . blobsBySource . inc ( { blobsSource} ) ;
459
+ }
455
460
456
461
const advancedSlot = this . clock . slotWithFutureTolerance ( REPROCESS_MIN_TIME_TO_NEXT_SLOT_SEC ) ;
457
462
0 commit comments