Skip to content

Commit 9de404b

Browse files
committed
emit proposer_slashing/attester_slashing SSE on beacon-API
Add support for slashings on the beacon-API event stream for compat with beacon-API specs. - ethereum/beacon-APIs#376
1 parent a208152 commit 9de404b

File tree

7 files changed

+48
-5
lines changed

7 files changed

+48
-5
lines changed

beacon_chain/beacon_node.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type
4545
attestQueue*: AsyncEventQueue[Attestation]
4646
exitQueue*: AsyncEventQueue[SignedVoluntaryExit]
4747
blsToExecQueue*: AsyncEventQueue[SignedBLSToExecutionChange]
48+
propSlashQueue*: AsyncEventQueue[ProposerSlashing]
49+
attSlashQueue*: AsyncEventQueue[AttesterSlashing]
4850
finalQueue*: AsyncEventQueue[FinalizationInfoObject]
4951
reorgQueue*: AsyncEventQueue[ReorgInfoObject]
5052
contribQueue*: AsyncEventQueue[SignedContributionAndProof]

beacon_chain/consensus_object_pools/exit_pool.nim

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ type
3232
proc(data: SignedVoluntaryExit) {.gcsafe, raises: [].}
3333
OnBLSToExecutionChangeCallback =
3434
proc(data: SignedBLSToExecutionChange) {.gcsafe, raises: [].}
35+
OnProposerSlashingCallback =
36+
proc(data: ProposerSlashing) {.gcsafe, raises: [].}
37+
OnAttesterSlashingCallback =
38+
proc(data: AttesterSlashing) {.gcsafe, raises: [].}
3539

3640
ValidatorChangePool* = object
3741
## The validator change pool tracks attester slashings, proposer slashings,
@@ -69,11 +73,15 @@ type
6973
attestationPool: ref AttestationPool
7074
onVoluntaryExitReceived*: OnVoluntaryExitCallback
7175
onBLSToExecutionChangeReceived*: OnBLSToExecutionChangeCallback
76+
onProposerSlashingReceived*: OnProposerSlashingCallback
77+
onAttesterSlashingReceived*: OnAttesterSlashingCallback
7278

7379
func init*(T: type ValidatorChangePool, dag: ChainDAGRef,
7480
attestationPool: ref AttestationPool = nil,
7581
onVoluntaryExit: OnVoluntaryExitCallback = nil,
76-
onBLSToExecutionChange: OnBLSToExecutionChangeCallback = nil): T =
82+
onBLSToExecutionChange: OnBLSToExecutionChangeCallback = nil,
83+
onProposerSlashing: OnProposerSlashingCallback = nil,
84+
onAttesterSlashing: OnAttesterSlashingCallback = nil): T =
7785
## Initialize an ValidatorChangePool from the dag `headState`
7886
T(
7987
# Allow filtering some validator change messages during block production
@@ -96,7 +104,9 @@ func init*(T: type ValidatorChangePool, dag: ChainDAGRef,
96104
dag: dag,
97105
attestationPool: attestationPool,
98106
onVoluntaryExitReceived: onVoluntaryExit,
99-
onBLSToExecutionChangeReceived: onBLSToExecutionChange)
107+
onBLSToExecutionChangeReceived: onBLSToExecutionChange,
108+
onProposerSlashingReceived: onProposerSlashing,
109+
onAttesterSlashingReceived: onAttesterSlashing)
100110
101111
func addValidatorChangeMessage(
102112
subpool: var auto, seenpool: var auto, validatorChangeMessage: auto,

beacon_chain/gossip_processing/gossip_validation.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,10 @@ proc validateAttesterSlashing*(
10751075
if attester_slashing_validity.isErr:
10761076
return pool.checkedReject(attester_slashing_validity.error)
10771077

1078+
# Send notification about new attester slashing via callback
1079+
if not(isNil(pool.onAttesterSlashingReceived)):
1080+
pool.onAttesterSlashingReceived(attester_slashing)
1081+
10781082
ok()
10791083

10801084
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.4/specs/phase0/p2p-interface.md#proposer_slashing
@@ -1099,6 +1103,10 @@ proc validateProposerSlashing*(
10991103
if proposer_slashing_validity.isErr:
11001104
return pool.checkedReject(proposer_slashing_validity.error)
11011105

1106+
# Send notification about new proposer slashing via callback
1107+
if not(isNil(pool.onProposerSlashingReceived)):
1108+
pool.onProposerSlashingReceived(proposer_slashing)
1109+
11021110
ok()
11031111

11041112
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/phase0/p2p-interface.md#voluntary_exit

beacon_chain/nimbus_beacon_node.nim

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ proc initFullNode(
289289
node.eventBus.exitQueue.emit(data)
290290
proc onBLSToExecutionChangeAdded(data: SignedBLSToExecutionChange) =
291291
node.eventBus.blsToExecQueue.emit(data)
292+
proc onProposerSlashingAdded(data: ProposerSlashing) =
293+
node.eventBus.propSlashQueue.emit(data)
294+
proc onAttesterSlashingAdded(data: AttesterSlashing) =
295+
node.eventBus.attSlashQueue.emit(data)
292296
proc onBlockAdded(data: ForkedTrustedSignedBeaconBlock) =
293297
let optimistic =
294298
if node.currentSlot().epoch() >= dag.cfg.BELLATRIX_FORK_EPOCH:
@@ -367,7 +371,8 @@ proc initFullNode(
367371
lightClientPool = newClone(
368372
LightClientPool())
369373
validatorChangePool = newClone(ValidatorChangePool.init(
370-
dag, attestationPool, onVoluntaryExitAdded, onBLSToExecutionChangeAdded))
374+
dag, attestationPool, onVoluntaryExitAdded, onBLSToExecutionChangeAdded,
375+
onProposerSlashingAdded, onAttesterSlashingAdded))
371376
blobQuarantine = newClone(BlobQuarantine())
372377
consensusManager = ConsensusManager.new(
373378
dag, attestationPool, quarantine, node.elManager,
@@ -546,6 +551,8 @@ proc init*(T: type BeaconNode,
546551
attestQueue: newAsyncEventQueue[Attestation](),
547552
exitQueue: newAsyncEventQueue[SignedVoluntaryExit](),
548553
blsToExecQueue: newAsyncEventQueue[SignedBLSToExecutionChange](),
554+
propSlashQueue: newAsyncEventQueue[ProposerSlashing](),
555+
attSlashQueue: newAsyncEventQueue[AttesterSlashing](),
549556
finalQueue: newAsyncEventQueue[FinalizationInfoObject](),
550557
reorgQueue: newAsyncEventQueue[ReorgInfoObject](),
551558
contribQueue: newAsyncEventQueue[SignedContributionAndProof](),

beacon_chain/rpc/rest_event_api.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ proc installEventApiHandlers*(router: var RestRouter, node: BeaconNode) =
137137
let handler = response.eventHandler(node.eventBus.blsToExecQueue,
138138
"bls_to_execution_change")
139139
res.add(handler)
140+
if EventTopic.ProposerSlashing in eventTopics:
141+
let handler = response.eventHandler(node.eventBus.propSlashQueue,
142+
"proposer_slashing")
143+
res.add(handler)
144+
if EventTopic.AttesterSlashing in eventTopics:
145+
let handler = response.eventHandler(node.eventBus.attSlashQueue,
146+
"attester_slashing")
147+
res.add(handler)
140148
if EventTopic.FinalizedCheckpoint in eventTopics:
141149
let handler = response.eventHandler(node.eventBus.finalQueue,
142150
"finalized_checkpoint")

beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4141,6 +4141,10 @@ proc decodeString*(t: typedesc[EventTopic],
41414141
ok(EventTopic.VoluntaryExit)
41424142
of "bls_to_execution_change":
41434143
ok(EventTopic.BLSToExecutionChange)
4144+
of "proposer_slashing":
4145+
ok(EventTopic.ProposerSlashing)
4146+
of "attester_slashing":
4147+
ok(EventTopic.AttesterSlashing)
41444148
of "finalized_checkpoint":
41454149
ok(EventTopic.FinalizedCheckpoint)
41464150
of "chain_reorg":
@@ -4166,6 +4170,10 @@ proc encodeString*(value: set[EventTopic]): Result[string, cstring] =
41664170
res.add("voluntary_exit,")
41674171
if EventTopic.BLSToExecutionChange in value:
41684172
res.add("bls_to_execution_change,")
4173+
if EventTopic.ProposerSlashing in value:
4174+
res.add("proposer_slashing,")
4175+
if EventTopic.AttesterSlashing in value:
4176+
res.add("attester_slashing,")
41694177
if EventTopic.FinalizedCheckpoint in value:
41704178
res.add("finalized_checkpoint,")
41714179
if EventTopic.ChainReorg in value:

beacon_chain/spec/eth2_apis/rest_types.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ type
5555
# https://github.com/ethereum/beacon-APIs/blob/v2.4.2/apis/eventstream/index.yaml
5656
EventTopic* {.pure.} = enum
5757
Head, Block, Attestation, VoluntaryExit, BLSToExecutionChange,
58-
FinalizedCheckpoint, ChainReorg, ContributionAndProof,
59-
LightClientFinalityUpdate, LightClientOptimisticUpdate
58+
ProposerSlashing, AttesterSlashing, FinalizedCheckpoint, ChainReorg,
59+
ContributionAndProof, LightClientFinalityUpdate, LightClientOptimisticUpdate
6060

6161
EventTopics* = set[EventTopic]
6262

0 commit comments

Comments
 (0)