Skip to content

Commit

Permalink
feat: emit slashing events
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluard committed Feb 5, 2024
1 parent 00dfa63 commit e16a6e5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/api/src/beacon/routes/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export enum EventType {
attestation = "attestation",
/** The node has received a valid voluntary exit (from P2P or API) */
voluntaryExit = "voluntary_exit",
/** The node has received a ProposerSlashing (from P2P or API) that passes validation rules of the `proposer_slashing` topic */
proposerSlashing = "proposer_slashing",
/** The node has received an AttesterSlashing (from P2P or API) that passes validation rules of the `attester_slashing` topic */
attesterSlashing = "attester_slashing",
/** The node has received a valid blsToExecutionChange (from P2P or API) */
blsToExecutionChange = "bls_to_execution_change",
/** Finalized checkpoint has been updated */
Expand All @@ -58,6 +62,8 @@ export const eventTypes: {[K in EventType]: K} = {
[EventType.block]: EventType.block,
[EventType.attestation]: EventType.attestation,
[EventType.voluntaryExit]: EventType.voluntaryExit,
[EventType.proposerSlashing]: EventType.proposerSlashing,
[EventType.attesterSlashing]: EventType.attesterSlashing,
[EventType.blsToExecutionChange]: EventType.blsToExecutionChange,
[EventType.finalizedCheckpoint]: EventType.finalizedCheckpoint,
[EventType.chainReorg]: EventType.chainReorg,
Expand Down Expand Up @@ -85,6 +91,8 @@ export type EventData = {
};
[EventType.attestation]: phase0.Attestation;
[EventType.voluntaryExit]: phase0.SignedVoluntaryExit;
[EventType.proposerSlashing]: phase0.ProposerSlashing;
[EventType.attesterSlashing]: phase0.AttesterSlashing;
[EventType.blsToExecutionChange]: capella.SignedBLSToExecutionChange;
[EventType.finalizedCheckpoint]: {
block: RootHex;
Expand Down Expand Up @@ -174,6 +182,8 @@ export function getTypeByEvent(): {[K in EventType]: TypeJson<EventData[K]>} {

[EventType.attestation]: ssz.phase0.Attestation,
[EventType.voluntaryExit]: ssz.phase0.SignedVoluntaryExit,
[EventType.proposerSlashing]: ssz.phase0.ProposerSlashing,
[EventType.attesterSlashing]: ssz.phase0.AttesterSlashing,
[EventType.blsToExecutionChange]: ssz.capella.SignedBLSToExecutionChange,

[EventType.finalizedCheckpoint]: new ContainerType(
Expand Down
62 changes: 62 additions & 0 deletions packages/api/test/unit/beacon/testData/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,68 @@ export const eventTestData: EventData = {
signature:
"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505",
}),
[EventType.proposerSlashing]: ssz.phase0.ProposerSlashing.fromJson({
signed_header_1: {
message: {
slot: "0",
proposer_index: "0",
parent_root: "0x0000000000000000000000000000000000000000000000000000000000000000",
state_root: "0x0000000000000000000000000000000000000000000000000000000000000000",
body_root: "0x0000000000000000000000000000000000000000000000000000000000000000",
},
signature:
"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
},
signed_header_2: {
message: {
slot: "0",
proposer_index: "0",
parent_root: "0x0000000000000000000000000000000000000000000000000000000000000000",
state_root: "0x0000000000000000000000000000000000000000000000000000000000000000",
body_root: "0x0000000000000000000000000000000000000000000000000000000000000000",
},
signature:
"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
},
}),
[EventType.attesterSlashing]: ssz.phase0.AttesterSlashing.fromJson({
attestation_1: {
attesting_indices: ["0", "1"],
data: {
slot: "0",
index: "0",
beacon_block_root: "0x0000000000000000000000000000000000000000000000000000000000000000",
source: {
epoch: "0",
root: "0x0000000000000000000000000000000000000000000000000000000000000000",
},
target: {
epoch: "0",
root: "0x0000000000000000000000000000000000000000000000000000000000000000",
},
},
signature:
"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
},
attestation_2: {
attesting_indices: ["0", "1"],
data: {
slot: "0",
index: "0",
beacon_block_root: "0x0000000000000000000000000000000000000000000000000000000000000000",
source: {
epoch: "0",
root: "0x0000000000000000000000000000000000000000000000000000000000000000",
},
target: {
epoch: "0",
root: "0x0000000000000000000000000000000000000000000000000000000000000000",
},
},
signature:
"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
},
}),
[EventType.blsToExecutionChange]: ssz.capella.SignedBLSToExecutionChange.fromJson({
message: {
validator_index: "1",
Expand Down
10 changes: 10 additions & 0 deletions packages/beacon-node/src/chain/blocks/importBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,16 @@ export async function importBlock(
this.emitter.emit(routes.events.EventType.attestation, attestation);
}
}
if (this.emitter.listenerCount(routes.events.EventType.attesterSlashing)) {
for (const attesterSlashing of block.message.body.attesterSlashings) {
this.emitter.emit(routes.events.EventType.attesterSlashing, attesterSlashing);
}
}
if (this.emitter.listenerCount(routes.events.EventType.proposerSlashing)) {
for (const proposerSlashing of block.message.body.proposerSlashings) {
this.emitter.emit(routes.events.EventType.proposerSlashing, proposerSlashing);
}
}
}

// Register stat metrics about the block after importing it
Expand Down
4 changes: 4 additions & 0 deletions packages/beacon-node/src/network/processor/gossipHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ function getDefaultHandlers(modules: ValidatorFnsModules, options: GossipHandler
} catch (e) {
logger.error("Error adding attesterSlashing to pool", {}, e as Error);
}

chain.emitter.emit(routes.events.EventType.attesterSlashing, attesterSlashing);
},

[GossipType.proposer_slashing]: async ({
Expand All @@ -470,6 +472,8 @@ function getDefaultHandlers(modules: ValidatorFnsModules, options: GossipHandler
} catch (e) {
logger.error("Error adding attesterSlashing to pool", {}, e as Error);
}

chain.emitter.emit(routes.events.EventType.proposerSlashing, proposerSlashing);
},

[GossipType.voluntary_exit]: async ({gossipData, topic}: GossipHandlerParamGeneric<GossipType.voluntary_exit>) => {
Expand Down

0 comments on commit e16a6e5

Please sign in to comment.