Skip to content

Commit 8045da7

Browse files
authored
Enable SSE block_gossip event (#7054)
* feat: add BlockGossipObject for sse event * feat: enable block_gossip topic * feat: create async queue and emit event * chore: copyright year * fix: check before onAdded exists and remove decodeString
1 parent a73abb4 commit 8045da7

File tree

7 files changed

+39
-2
lines changed

7 files changed

+39
-2
lines changed

beacon_chain/beacon_node.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type
4747
EventBus* = object
4848
headQueue*: AsyncEventQueue[HeadChangeInfoObject]
4949
blocksQueue*: AsyncEventQueue[EventBeaconBlockObject]
50+
blockGossipQueue*: AsyncEventQueue[EventBeaconBlockGossipObject]
5051
phase0AttestQueue*: AsyncEventQueue[phase0.Attestation]
5152
singleAttestQueue*: AsyncEventQueue[SingleAttestation]
5253
exitQueue*: AsyncEventQueue[SignedVoluntaryExit]

beacon_chain/consensus_object_pools/block_pools_types.nim

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ type
5151

5252
OnBlockCallback* =
5353
proc(data: ForkedTrustedSignedBeaconBlock) {.gcsafe, raises: [].}
54+
OnBlockGossipCallback* =
55+
proc(data: ForkedSignedBeaconBlock) {.gcsafe, raises: [].}
5456
OnHeadCallback* =
5557
proc(data: HeadChangeInfoObject) {.gcsafe, raises: [].}
5658
OnReorgCallback* =
@@ -233,6 +235,8 @@ type
233235

234236
onBlockAdded*: OnBlockCallback
235237
## On block added callback
238+
onBlockGossipAdded*: OnBlockGossipCallback
239+
## On block gossip added callback
236240
onHeadChanged*: OnHeadCallback
237241
## On head changed callback
238242
onReorgHappened*: OnReorgCallback
@@ -341,6 +345,10 @@ type
341345
block_root* {.serializedFieldName: "block".}: Eth2Digest
342346
optimistic* {.serializedFieldName: "execution_optimistic".}: Option[bool]
343347

348+
EventBeaconBlockGossipObject* = object
349+
slot*: Slot
350+
block_root* {.serializedFieldName: "block".}: Eth2Digest
351+
344352
template OnBlockAddedCallback*(kind: static ConsensusFork): auto =
345353
when kind == ConsensusFork.Fulu:
346354
typedesc[OnFuluBlockAdded]
@@ -401,6 +409,9 @@ template setFinalizationCb*(dag: ChainDAGRef, cb: OnFinalizedCallback) =
401409
template setBlockCb*(dag: ChainDAGRef, cb: OnBlockCallback) =
402410
dag.onBlockAdded = cb
403411

412+
template setBlockGossipCb*(dag: ChainDAGRef, cb: OnBlockGossipCallback) =
413+
dag.onBlockGossipAdded = cb
414+
404415
template setHeadCb*(dag: ChainDAGRef, cb: OnHeadCallback) =
405416
dag.onHeadChanged = cb
406417

@@ -476,3 +487,11 @@ func init*(t: typedesc[EventBeaconBlockObject],
476487
block_root: forkyBlck.root,
477488
optimistic: optimistic
478489
)
490+
491+
func init*(t: typedesc[EventBeaconBlockGossipObject],
492+
v: ForkedSignedBeaconBlock): EventBeaconBlockGossipObject =
493+
withBlck(v):
494+
EventBeaconBlockGossipObject(
495+
slot: forkyBlck.message.slot,
496+
block_root: forkyBlck.root
497+
)

beacon_chain/gossip_processing/eth2_processor.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# beacon_chain
2-
# Copyright (c) 2018-2024 Status Research & Development GmbH
2+
# Copyright (c) 2018-2025 Status Research & Development GmbH
33
# Licensed and distributed under either of
44
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
55
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@@ -236,6 +236,9 @@ proc processSignedBeaconBlock*(
236236
# propagation of seemingly good blocks
237237
trace "Block validated"
238238

239+
if not(isNil(self.dag.onBlockGossipAdded)):
240+
self.dag.onBlockGossipAdded(ForkedSignedBeaconBlock.init(signedBlock))
241+
239242
let blobs =
240243
when typeof(signedBlock).kind >= ConsensusFork.Deneb:
241244
if self.blobQuarantine[].hasBlobs(signedBlock):

beacon_chain/nimbus_beacon_node.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ proc initFullNode(
317317
none[bool]()
318318
node.eventBus.blocksQueue.emit(
319319
EventBeaconBlockObject.init(data, optimistic))
320+
proc onBlockGossipAdded(data: ForkedSignedBeaconBlock) =
321+
node.eventBus.blockGossipQueue.emit(
322+
EventBeaconBlockGossipObject.init(data))
320323
proc onHeadChanged(data: HeadChangeInfoObject) =
321324
let eventData =
322325
if node.currentSlot().epoch() >= dag.cfg.BELLATRIX_FORK_EPOCH:
@@ -594,6 +597,7 @@ proc initFullNode(
594597

595598
dag.setFinalizationCb makeOnFinalizationCb(node.eventBus, node.elManager)
596599
dag.setBlockCb(onBlockAdded)
600+
dag.setBlockGossipCb(onBlockGossipAdded)
597601
dag.setHeadCb(onHeadChanged)
598602
dag.setReorgCb(onChainReorg)
599603

@@ -739,6 +743,7 @@ proc init*(T: type BeaconNode,
739743
eventBus = EventBus(
740744
headQueue: newAsyncEventQueue[HeadChangeInfoObject](),
741745
blocksQueue: newAsyncEventQueue[EventBeaconBlockObject](),
746+
blockGossipQueue: newAsyncEventQueue[EventBeaconBlockGossipObject](),
742747
phase0AttestQueue: newAsyncEventQueue[phase0.Attestation](),
743748
singleAttestQueue: newAsyncEventQueue[SingleAttestation](),
744749
exitQueue: newAsyncEventQueue[SignedVoluntaryExit](),

beacon_chain/rpc/rest_event_api.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ proc installEventApiHandlers*(router: var RestRouter, node: BeaconNode) =
125125
let handler = response.eventHandler(node.eventBus.blocksQueue,
126126
"block")
127127
res.add(handler)
128+
if EventTopic.BlockGossip in eventTopics:
129+
let handler = response.eventHandler(node.eventBus.blockGossipQueue,
130+
"block_gossip")
131+
res.add(handler)
128132
if EventTopic.Attestation in eventTopics:
129133
let handler = response.eventHandler(node.eventBus.phase0AttestQueue,
130134
"attestation")

beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ RestJson.useDefaultSerializationFor(
6969
EmptyBody,
7070
Eth1Data,
7171
EventBeaconBlockObject,
72+
EventBeaconBlockGossipObject,
7273
ExecutionRequests,
7374
Fork,
7475
FuluSignedBlockContents,
@@ -3722,6 +3723,8 @@ func decodeString*(t: typedesc[EventTopic],
37223723
ok(EventTopic.Head)
37233724
of "block":
37243725
ok(EventTopic.Block)
3726+
of "block_gossip":
3727+
ok(EventTopic.BlockGossip)
37253728
of "attestation":
37263729
ok(EventTopic.Attestation)
37273730
of "single_attestation":
@@ -3755,6 +3758,8 @@ func encodeString*(value: set[EventTopic]): Result[string, cstring] =
37553758
res.add("head,")
37563759
if EventTopic.Block in value:
37573760
res.add("block,")
3761+
if EventTopic.BlockGossip in value:
3762+
res.add("block_gossip,")
37583763
if EventTopic.Attestation in value:
37593764
res.add("attestation,")
37603765
if EventTopic.SingleAttestation in value:

beacon_chain/spec/eth2_apis/rest_types.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static:
5656
type
5757
# https://github.com/ethereum/beacon-APIs/blob/v2.4.2/apis/eventstream/index.yaml
5858
EventTopic* {.pure.} = enum
59-
Head, Block, Attestation, VoluntaryExit, BLSToExecutionChange,
59+
Head, Block, Attestation, BlockGossip, VoluntaryExit, BLSToExecutionChange,
6060
ProposerSlashing, AttesterSlashing, BlobSidecar, SingleAttestation,
6161
FinalizedCheckpoint, ChainReorg, ContributionAndProof,
6262
LightClientFinalityUpdate, LightClientOptimisticUpdate

0 commit comments

Comments
 (0)