Skip to content

Commit 6113dce

Browse files
authored
Merge 7cd8b56 into 3fb7f60
2 parents 3fb7f60 + 7cd8b56 commit 6113dce

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

packages/beacon-node/src/chain/blocks/importBlock.ts

+54-49
Original file line numberDiff line numberDiff line change
@@ -101,34 +101,6 @@ export async function importBlock(
101101
this.metrics?.importBlock.bySource.inc({source});
102102
this.logger.verbose("Added block to forkchoice and state cache", {slot: blockSlot, root: blockRootHex});
103103

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-
132104
// 3. Import attestations to fork choice
133105
//
134106
// - For each attestation
@@ -413,32 +385,58 @@ export async function importBlock(
413385
// Send block events, only for recent enough blocks
414386

415387
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+
});
420397
}
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+
}
425402
}
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+
}
430407
}
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+
}
435412
}
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+
}
440417
}
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+
});
442440
}
443441

444442
// Register stat metrics about the block after importing it
@@ -452,6 +450,13 @@ export async function importBlock(
452450
fullyVerifiedBlock.postState.epochCtx.currentSyncCommitteeIndexed.validatorIndices
453451
);
454452
}
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+
}
455460

456461
const advancedSlot = this.clock.slotWithFutureTolerance(REPROCESS_MIN_TIME_TO_NEXT_SLOT_SEC);
457462

0 commit comments

Comments
 (0)