@aztec/slasher 0.0.1-commit.b655e406 → 0.0.1-commit.b8a057fa

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/README.md +90 -76
  2. package/dest/config.d.ts +1 -1
  3. package/dest/config.d.ts.map +1 -1
  4. package/dest/config.js +53 -41
  5. package/dest/factory/create_facade.d.ts +5 -4
  6. package/dest/factory/create_facade.d.ts.map +1 -1
  7. package/dest/factory/create_facade.js +26 -3
  8. package/dest/factory/create_implementation.d.ts +7 -8
  9. package/dest/factory/create_implementation.d.ts.map +1 -1
  10. package/dest/factory/create_implementation.js +8 -56
  11. package/dest/factory/get_settings.d.ts +4 -4
  12. package/dest/factory/get_settings.d.ts.map +1 -1
  13. package/dest/factory/get_settings.js +3 -3
  14. package/dest/factory/index.d.ts +2 -2
  15. package/dest/factory/index.d.ts.map +1 -1
  16. package/dest/factory/index.js +1 -1
  17. package/dest/generated/slasher-defaults.d.ts +21 -0
  18. package/dest/generated/slasher-defaults.d.ts.map +1 -0
  19. package/dest/generated/slasher-defaults.js +21 -0
  20. package/dest/index.d.ts +6 -4
  21. package/dest/index.d.ts.map +1 -1
  22. package/dest/index.js +5 -3
  23. package/dest/metrics.d.ts +7 -0
  24. package/dest/metrics.d.ts.map +1 -0
  25. package/dest/metrics.js +11 -0
  26. package/dest/null_slasher_client.d.ts +5 -5
  27. package/dest/null_slasher_client.d.ts.map +1 -1
  28. package/dest/null_slasher_client.js +1 -4
  29. package/dest/slash_offenses_collector.d.ts +10 -9
  30. package/dest/slash_offenses_collector.d.ts.map +1 -1
  31. package/dest/slash_offenses_collector.js +50 -30
  32. package/dest/slash_round_monitor.d.ts +5 -4
  33. package/dest/slash_round_monitor.d.ts.map +1 -1
  34. package/dest/slasher_client.d.ts +114 -0
  35. package/dest/slasher_client.d.ts.map +1 -0
  36. package/dest/{tally_slasher_client.js → slasher_client.js} +55 -45
  37. package/dest/slasher_client_facade.d.ts +9 -10
  38. package/dest/slasher_client_facade.d.ts.map +1 -1
  39. package/dest/slasher_client_facade.js +6 -9
  40. package/dest/slasher_client_interface.d.ts +9 -22
  41. package/dest/slasher_client_interface.d.ts.map +1 -1
  42. package/dest/slasher_client_interface.js +1 -4
  43. package/dest/stores/offenses_store.d.ts +12 -12
  44. package/dest/stores/offenses_store.d.ts.map +1 -1
  45. package/dest/stores/offenses_store.js +64 -39
  46. package/dest/stores/schema_version.d.ts +1 -1
  47. package/dest/test/dummy_watcher.d.ts +1 -1
  48. package/dest/test/dummy_watcher.d.ts.map +1 -1
  49. package/dest/watcher.d.ts +8 -1
  50. package/dest/watcher.d.ts.map +1 -1
  51. package/dest/watcher.js +1 -0
  52. package/dest/watchers/attestations_block_watcher.d.ts +29 -15
  53. package/dest/watchers/attestations_block_watcher.d.ts.map +1 -1
  54. package/dest/watchers/attestations_block_watcher.js +91 -69
  55. package/dest/watchers/attested_invalid_proposal_watcher.d.ts +42 -0
  56. package/dest/watchers/attested_invalid_proposal_watcher.d.ts.map +1 -0
  57. package/dest/watchers/attested_invalid_proposal_watcher.js +117 -0
  58. package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.d.ts +38 -0
  59. package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.d.ts.map +1 -0
  60. package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.js +138 -0
  61. package/dest/watchers/checkpoint_equivocation_watcher.d.ts +30 -0
  62. package/dest/watchers/checkpoint_equivocation_watcher.d.ts.map +1 -0
  63. package/dest/watchers/checkpoint_equivocation_watcher.js +69 -0
  64. package/dest/watchers/data_withholding_watcher.d.ts +63 -0
  65. package/dest/watchers/data_withholding_watcher.d.ts.map +1 -0
  66. package/dest/watchers/data_withholding_watcher.js +193 -0
  67. package/package.json +18 -15
  68. package/src/config.ts +61 -41
  69. package/src/factory/create_facade.ts +35 -6
  70. package/src/factory/create_implementation.ts +25 -106
  71. package/src/factory/get_settings.ts +8 -8
  72. package/src/factory/index.ts +1 -1
  73. package/src/generated/slasher-defaults.ts +23 -0
  74. package/src/index.ts +5 -3
  75. package/src/metrics.ts +19 -0
  76. package/src/null_slasher_client.ts +4 -7
  77. package/src/slash_offenses_collector.ts +70 -32
  78. package/src/slash_round_monitor.ts +3 -2
  79. package/src/{tally_slasher_client.ts → slasher_client.ts} +82 -58
  80. package/src/slasher_client_facade.ts +9 -13
  81. package/src/slasher_client_interface.ts +8 -22
  82. package/src/stores/offenses_store.ts +76 -48
  83. package/src/watcher.ts +9 -1
  84. package/src/watchers/attestations_block_watcher.ts +108 -88
  85. package/src/watchers/attested_invalid_proposal_watcher.ts +168 -0
  86. package/src/watchers/broadcasted_invalid_checkpoint_proposal_watcher.ts +192 -0
  87. package/src/watchers/checkpoint_equivocation_watcher.ts +96 -0
  88. package/src/watchers/data_withholding_watcher.ts +225 -0
  89. package/dest/empire_slasher_client.d.ts +0 -189
  90. package/dest/empire_slasher_client.d.ts.map +0 -1
  91. package/dest/empire_slasher_client.js +0 -572
  92. package/dest/stores/payloads_store.d.ts +0 -29
  93. package/dest/stores/payloads_store.d.ts.map +0 -1
  94. package/dest/stores/payloads_store.js +0 -125
  95. package/dest/tally_slasher_client.d.ts +0 -129
  96. package/dest/tally_slasher_client.d.ts.map +0 -1
  97. package/dest/watchers/epoch_prune_watcher.d.ts +0 -37
  98. package/dest/watchers/epoch_prune_watcher.d.ts.map +0 -1
  99. package/dest/watchers/epoch_prune_watcher.js +0 -135
  100. package/src/empire_slasher_client.ts +0 -656
  101. package/src/stores/payloads_store.ts +0 -146
  102. package/src/watchers/epoch_prune_watcher.ts +0 -192
@@ -1,10 +1,10 @@
1
1
  import { EthAddress } from '@aztec/aztec.js/addresses';
2
2
  import type { EpochCache } from '@aztec/epoch-cache';
3
- import { RollupContract, SlasherContract, TallySlashingProposerContract } from '@aztec/ethereum/contracts';
3
+ import { RollupContract, SlasherContract, SlashingProposerContract } from '@aztec/ethereum/contracts';
4
4
  import { maxBigint } from '@aztec/foundation/bigint';
5
+ import { SlotNumber } from '@aztec/foundation/branded-types';
5
6
  import { compactArray, partition, times } from '@aztec/foundation/collection';
6
7
  import { createLogger } from '@aztec/foundation/log';
7
- import { sleep } from '@aztec/foundation/sleep';
8
8
  import type { DateProvider } from '@aztec/foundation/timer';
9
9
  import type { Prettify } from '@aztec/foundation/types';
10
10
  import type { SlasherConfig } from '@aztec/stdlib/interfaces/server';
@@ -13,13 +13,15 @@ import {
13
13
  OffenseType,
14
14
  type ProposerSlashAction,
15
15
  type ProposerSlashActionProvider,
16
- type SlashPayloadRound,
17
16
  getEpochsForRound,
17
+ getOffenseTypeName,
18
18
  getSlashConsensusVotesFromOffenses,
19
19
  } from '@aztec/stdlib/slashing';
20
+ import { getTelemetryClient } from '@aztec/telemetry-client';
20
21
 
21
22
  import type { Hex } from 'viem';
22
23
 
24
+ import { SlasherMetrics } from './metrics.js';
23
25
  import {
24
26
  SlashOffensesCollector,
25
27
  type SlashOffensesCollectorConfig,
@@ -30,8 +32,8 @@ import type { SlasherClientInterface } from './slasher_client_interface.js';
30
32
  import type { SlasherOffensesStore } from './stores/offenses_store.js';
31
33
  import type { Watcher } from './watcher.js';
32
34
 
33
- /** Settings used in the tally slasher client, loaded from the L1 contracts during initialization */
34
- export type TallySlasherSettings = Prettify<
35
+ /** Settings used in the slasher client, loaded from the L1 contracts during initialization */
36
+ export type SlasherSettings = Prettify<
35
37
  SlashRoundMonitorSettings &
36
38
  SlashOffensesCollectorSettings & {
37
39
  slashingLifetimeInRounds: number;
@@ -45,11 +47,22 @@ export type TallySlasherSettings = Prettify<
45
47
  }
46
48
  >;
47
49
 
48
- export type TallySlasherClientConfig = SlashOffensesCollectorConfig &
49
- Pick<SlasherConfig, 'slashValidatorsAlways' | 'slashValidatorsNever' | 'slashExecuteRoundsLookBack'>;
50
+ export type SlasherClientConfig = SlashOffensesCollectorConfig &
51
+ Pick<
52
+ SlasherConfig,
53
+ 'slashValidatorsAlways' | 'slashValidatorsNever' | 'slashExecuteRoundsLookBack' | 'slashMaxPayloadSize'
54
+ >;
55
+
56
+ type AlwaysSlashOffense = {
57
+ validator: EthAddress;
58
+ amount: bigint;
59
+ offenseType: OffenseType.UNKNOWN;
60
+ };
61
+
62
+ type SlashVoteOffense = Offense | AlwaysSlashOffense;
50
63
 
51
64
  /**
52
- * The Tally Slasher client is responsible for managing slashable offenses using
65
+ * The Slasher client is responsible for managing slashable offenses using
53
66
  * the consensus-based slashing model where proposers vote on individual validator offenses.
54
67
  *
55
68
  * The client subscribes to several slash watchers that emit offenses and tracks them. When the slasher is the
@@ -73,22 +86,16 @@ export type TallySlasherClientConfig = SlashOffensesCollectorConfig &
73
86
  * - Validators that reach the quorum threshold are slashed. A vote for slashing N units is also considered
74
87
  * a vote for slashing N-1, N-2, ..., 1 units. The system slashes for the largest amount that reaches quorum.
75
88
  * - The client monitors executable rounds and triggers execution when appropriate.
76
- *
77
- * Differences from Empire model
78
- * - No fixed slash payloads - votes are for individual validator offenses encoded in bytes
79
- * - The L1 contract determines which offenses reach quorum rather than nodes agreeing on a payload
80
- * - Proposers vote directly on which validators to slash and by how much
81
- * - Uses a slash offset to vote on validators from past rounds (e.g., round N votes on round N-2)
82
89
  */
83
- export class TallySlasherClient implements ProposerSlashActionProvider, SlasherClientInterface {
90
+ export class SlasherClient implements ProposerSlashActionProvider, SlasherClientInterface {
84
91
  protected unwatchCallbacks: (() => void)[] = [];
85
92
  protected roundMonitor: SlashRoundMonitor;
86
93
  protected offensesCollector: SlashOffensesCollector;
87
94
 
88
95
  constructor(
89
- private config: TallySlasherClientConfig,
90
- private settings: TallySlasherSettings,
91
- private tallySlashingProposer: TallySlashingProposerContract,
96
+ private config: SlasherClientConfig,
97
+ private settings: SlasherSettings,
98
+ private slashingProposer: SlashingProposerContract,
92
99
  private slasher: SlasherContract,
93
100
  private rollup: RollupContract,
94
101
  watchers: Watcher[],
@@ -96,20 +103,21 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
96
103
  private dateProvider: DateProvider,
97
104
  private offensesStore: SlasherOffensesStore,
98
105
  private log = createLogger('slasher:consensus'),
106
+ private readonly metrics = new SlasherMetrics(getTelemetryClient()),
99
107
  ) {
100
108
  this.roundMonitor = new SlashRoundMonitor(settings, dateProvider);
101
109
  this.offensesCollector = new SlashOffensesCollector(config, settings, watchers, offensesStore);
102
110
  }
103
111
 
104
112
  public async start() {
105
- this.log.debug('Starting Tally Slasher client...');
113
+ this.log.debug('Starting slasher client...');
106
114
 
107
115
  this.roundMonitor.start();
108
116
  await this.offensesCollector.start();
109
117
 
110
118
  // Listen for RoundExecuted events
111
119
  this.unwatchCallbacks.push(
112
- this.tallySlashingProposer.listenToRoundExecuted(
120
+ this.slashingProposer.listenToRoundExecuted(
113
121
  ({ round, slashCount, l1BlockHash }) =>
114
122
  void this.handleRoundExecuted(round, slashCount, l1BlockHash).catch(err =>
115
123
  this.log.error('Error handling round executed', err),
@@ -120,15 +128,13 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
120
128
  // Check for round changes
121
129
  this.unwatchCallbacks.push(this.roundMonitor.listenToNewRound(round => this.handleNewRound(round)));
122
130
 
123
- this.log.info(`Started tally slasher client`);
131
+ this.log.info(`Started slasher client`);
124
132
  return Promise.resolve();
125
133
  }
126
134
 
127
- /**
128
- * Stop the tally slasher client
129
- */
135
+ /** Stop the slasher client */
130
136
  public async stop() {
131
- this.log.debug('Stopping Tally Slasher client...');
137
+ this.log.debug('Stopping slasher client...');
132
138
 
133
139
  for (const unwatchCallback of this.unwatchCallbacks) {
134
140
  unwatchCallback();
@@ -137,9 +143,7 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
137
143
  this.roundMonitor.stop();
138
144
  await this.offensesCollector.stop();
139
145
 
140
- // Sleeping to sidestep viem issue with unwatching events
141
- await sleep(2000);
142
- this.log.info('Tally Slasher client stopped');
146
+ this.log.info('Slasher client stopped');
143
147
  }
144
148
 
145
149
  /** Returns the current config */
@@ -154,12 +158,13 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
154
158
 
155
159
  /** Triggered on a time basis when we enter a new slashing round. Clears expired offenses. */
156
160
  protected async handleNewRound(round: bigint) {
157
- this.log.info(`Starting new tally slashing round ${round}`);
161
+ this.log.info(`Starting new slashing round ${round}`);
158
162
  await this.offensesCollector.handleNewRound(round);
159
163
  }
160
164
 
161
- /** Called when we see a RoundExecuted event on the TallySlashingProposer (just for logging). */
165
+ /** Called when we see a RoundExecuted event on the SlashingProposer (just for logging). */
162
166
  protected async handleRoundExecuted(round: bigint, slashCount: bigint, l1BlockHash: Hex) {
167
+ this.metrics.recordRoundExecuted();
163
168
  const slashes = await this.rollup.getSlashEvents(l1BlockHash);
164
169
  this.log.info(`Slashing round ${round} has been executed with ${slashCount} slashes`, { slashes });
165
170
  }
@@ -169,7 +174,7 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
169
174
  * @param slotNumber - The current slot number
170
175
  * @returns The actions to take
171
176
  */
172
- public async getProposerActions(slotNumber: bigint): Promise<ProposerSlashAction[]> {
177
+ public async getProposerActions(slotNumber: SlotNumber): Promise<ProposerSlashAction[]> {
173
178
  const [executeAction, voteAction] = await Promise.all([
174
179
  this.getExecuteSlashAction(slotNumber),
175
180
  this.getVoteOffensesAction(slotNumber),
@@ -182,7 +187,7 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
182
187
  * Returns an execute slash action if there are any rounds ready to be executed.
183
188
  * Returns the oldest slash action if there are multiple rounds pending execution.
184
189
  */
185
- protected async getExecuteSlashAction(slotNumber: bigint): Promise<ProposerSlashAction | undefined> {
190
+ protected async getExecuteSlashAction(slotNumber: SlotNumber): Promise<ProposerSlashAction | undefined> {
186
191
  const { round: currentRound } = this.roundMonitor.getRoundForSlot(slotNumber);
187
192
  const slashingExecutionDelayInRounds = BigInt(this.settings.slashingExecutionDelayInRounds);
188
193
  const executableRound = currentRound - slashingExecutionDelayInRounds - 1n;
@@ -233,13 +238,13 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
233
238
  */
234
239
  private async tryGetRoundExecuteAction(
235
240
  executableRound: bigint,
236
- slotNumber: bigint,
241
+ slotNumber: SlotNumber,
237
242
  ): Promise<ProposerSlashAction | undefined> {
238
243
  let logData: Record<string, unknown> = { executableRound, slotNumber };
239
244
  this.log.debug(`Testing if slashing round ${executableRound} is executable`, logData);
240
245
 
241
246
  try {
242
- const roundInfo = await this.tallySlashingProposer.getRound(executableRound);
247
+ const roundInfo = await this.slashingProposer.getRound(executableRound);
243
248
  logData = { ...logData, roundInfo };
244
249
  if (roundInfo.isExecuted) {
245
250
  this.log.verbose(`Round ${executableRound} has already been executed`, logData);
@@ -253,7 +258,7 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
253
258
  }
254
259
 
255
260
  // Check if round is ready to execute at the given slot
256
- const isReadyToExecute = await this.tallySlashingProposer.isRoundReadyToExecute(executableRound, slotNumber);
261
+ const isReadyToExecute = await this.slashingProposer.isRoundReadyToExecute(executableRound, slotNumber);
257
262
  if (!isReadyToExecute) {
258
263
  this.log.warn(
259
264
  `Round ${executableRound} is not ready to execute at slot ${slotNumber} according to contract check`,
@@ -263,14 +268,14 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
263
268
  }
264
269
 
265
270
  // Check if the round yields any slashing at all
266
- const { actions: slashActions, committees } = await this.tallySlashingProposer.getTally(executableRound);
271
+ const { actions: slashActions, committees } = await this.slashingProposer.getTally(executableRound);
267
272
  if (slashActions.length === 0) {
268
273
  this.log.verbose(`Round ${executableRound} does not resolve in any slashing`, logData);
269
274
  return undefined;
270
275
  }
271
276
 
272
277
  // Check if the slash payload is vetoed
273
- const payload = await this.tallySlashingProposer.getPayload(executableRound);
278
+ const payload = await this.slashingProposer.getPayload(executableRound);
274
279
  const isVetoed = await this.slasher.isPayloadVetoed(payload.address);
275
280
  if (isVetoed) {
276
281
  this.log.warn(`Round ${executableRound} payload is vetoed (skipping execution)`, {
@@ -280,8 +285,12 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
280
285
  return undefined;
281
286
  }
282
287
 
288
+ const slashActionsWithAmounts = slashActions.map(action => ({
289
+ validator: action.validator.toString(),
290
+ slashAmount: action.slashAmount.toString(),
291
+ }));
283
292
  this.log.info(`Round ${executableRound} is ready to execute with ${slashActions.length} slashes`, {
284
- slashActions,
293
+ slashActions: slashActionsWithAmounts,
285
294
  payloadAddress: payload.address.toString(),
286
295
  ...logData,
287
296
  });
@@ -302,7 +311,7 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
302
311
  }
303
312
 
304
313
  /** Returns a vote action based on offenses from the target round (with offset applied) */
305
- protected async getVoteOffensesAction(slotNumber: bigint): Promise<ProposerSlashAction | undefined> {
314
+ protected async getVoteOffensesAction(slotNumber: SlotNumber): Promise<ProposerSlashAction | undefined> {
306
315
  // Compute what round we are in based on the slot number and what round will be slashed
307
316
  const { round: currentRound } = this.roundMonitor.getRoundForSlot(slotNumber);
308
317
  const slashedRound = this.getSlashedRound(currentRound);
@@ -313,7 +322,7 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
313
322
  // Compute offenses to slash, by loading the offenses for this round, adding synthetic offenses
314
323
  // for validators that should always be slashed, and removing the ones that should never be slashed.
315
324
  const offensesForRound = await this.gatherOffensesForRound(currentRound);
316
- const offensesFromAlwaysSlash = (this.config.slashValidatorsAlways ?? []).map(validator => ({
325
+ const offensesFromAlwaysSlash: AlwaysSlashOffense[] = (this.config.slashValidatorsAlways ?? []).map(validator => ({
317
326
  validator,
318
327
  amount: this.settings.slashingAmounts[2],
319
328
  offenseType: OffenseType.UNKNOWN,
@@ -327,7 +336,7 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
327
336
  slotNumber,
328
337
  currentRound,
329
338
  slashedRound,
330
- offensesToForgive,
339
+ offensesFromAlwaysSlash: offensesFromAlwaysSlash.map(getOffenseLogData),
331
340
  slashValidatorsAlways: this.config.slashValidatorsAlways,
332
341
  });
333
342
  }
@@ -337,7 +346,7 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
337
346
  slotNumber,
338
347
  currentRound,
339
348
  slashedRound,
340
- offensesToForgive,
349
+ offensesToForgive: offensesToForgive.map(getOffenseLogData),
341
350
  slashValidatorsNever: this.config.slashValidatorsNever,
342
351
  });
343
352
  }
@@ -347,27 +356,42 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
347
356
  return undefined;
348
357
  }
349
358
 
350
- this.log.info(`Voting to slash ${offensesToSlash.length} offenses`, {
359
+ this.log.debug(`Computing slash votes for ${offensesToSlash.length} offenses`, {
351
360
  slotNumber,
352
361
  currentRound,
353
362
  slashedRound,
354
- offensesToSlash,
363
+ offensesToSlash: offensesToSlash.map(getOffenseLogData),
355
364
  });
356
365
 
357
366
  const committees = await this.collectCommitteesActiveDuringRound(slashedRound);
358
367
  const epochsForCommittees = getEpochsForRound(slashedRound, this.settings);
359
- const votes = getSlashConsensusVotesFromOffenses(offensesToSlash, committees, epochsForCommittees, this.settings);
368
+ const { slashMaxPayloadSize } = this.config;
369
+ const votes = getSlashConsensusVotesFromOffenses(
370
+ offensesToSlash,
371
+ committees,
372
+ epochsForCommittees.map(e => BigInt(e)),
373
+ { ...this.settings, maxSlashedValidators: slashMaxPayloadSize },
374
+ this.log,
375
+ );
360
376
  if (votes.every(v => v === 0)) {
361
377
  this.log.warn(`Computed votes for offenses are all zero. Skipping vote.`, {
362
378
  slotNumber,
363
379
  currentRound,
364
380
  slashedRound,
365
- offensesToSlash,
381
+ offensesToSlash: offensesToSlash.map(getOffenseLogData),
366
382
  committees,
367
383
  });
368
384
  return undefined;
369
385
  }
370
386
 
387
+ this.log.info(`Voting to slash ${offensesToSlash.length} offenses`, {
388
+ slotNumber,
389
+ slashedRound,
390
+ currentRound,
391
+ votes,
392
+ offensesToSlash: offensesToSlash.map(getOffenseLogData),
393
+ });
394
+
371
395
  this.log.debug(`Computed votes for slashing ${offensesToSlash.length} offenses`, {
372
396
  slashedRound,
373
397
  currentRound,
@@ -393,17 +417,9 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
393
417
  );
394
418
  }
395
419
 
396
- /**
397
- * Get slash payloads is NOT SUPPORTED in tally model
398
- * @throws Error indicating this operation is not supported
399
- */
400
- public getSlashPayloads(): Promise<SlashPayloadRound[]> {
401
- return Promise.reject(new Error('Tally slashing model does not support slash payloads'));
402
- }
403
-
404
420
  /**
405
421
  * Gather offenses to be slashed on a given round.
406
- * In tally slashing, round N slashes validators from round N - slashOffsetInRounds.
422
+ * Round N slashes validators from round N - slashOffsetInRounds.
407
423
  * @param round - The round to get offenses for, defaults to current round
408
424
  * @returns Array of pending offenses for the round with offset applied
409
425
  */
@@ -416,9 +432,9 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
416
432
  return await this.offensesStore.getOffensesForRound(targetRound);
417
433
  }
418
434
 
419
- /** Returns all pending offenses stored */
420
- public getPendingOffenses(): Promise<Offense[]> {
421
- return this.offensesStore.getPendingOffenses();
435
+ /** Returns all offenses stored */
436
+ public getOffenses(): Promise<Offense[]> {
437
+ return this.offensesStore.getOffenses();
422
438
  }
423
439
 
424
440
  /**
@@ -434,3 +450,11 @@ export class TallySlasherClient implements ProposerSlashActionProvider, SlasherC
434
450
  return round - BigInt(this.settings.slashingOffsetInRounds);
435
451
  }
436
452
  }
453
+
454
+ function getOffenseLogData(offense: SlashVoteOffense) {
455
+ return {
456
+ ...offense,
457
+ validator: offense.validator.toString(),
458
+ offenseType: getOffenseTypeName(offense.offenseType),
459
+ };
460
+ }
@@ -1,13 +1,13 @@
1
1
  import { EpochCache } from '@aztec/epoch-cache';
2
- import type { ViemClient } from '@aztec/ethereum';
3
2
  import { RollupContract } from '@aztec/ethereum/contracts';
4
- import { EthAddress } from '@aztec/foundation/eth-address';
3
+ import type { ViemClient } from '@aztec/ethereum/types';
4
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
5
5
  import { createLogger } from '@aztec/foundation/log';
6
6
  import { DateProvider } from '@aztec/foundation/timer';
7
- import type { DataStoreConfig } from '@aztec/kv-store/config';
8
7
  import { AztecLMDBStoreV2 } from '@aztec/kv-store/lmdb-v2';
9
8
  import type { SlasherConfig } from '@aztec/stdlib/interfaces/server';
10
- import type { Offense, ProposerSlashAction, SlashPayloadRound } from '@aztec/stdlib/slashing';
9
+ import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
10
+ import type { Offense, ProposerSlashAction } from '@aztec/stdlib/slashing';
11
11
 
12
12
  import { createSlasherImplementation } from './factory/create_implementation.js';
13
13
  import type { SlasherClientInterface } from './slasher_client_interface.js';
@@ -26,11 +26,11 @@ export class SlasherClientFacade implements SlasherClientInterface {
26
26
  private config: SlasherConfig & DataStoreConfig & { ethereumSlotDuration: number },
27
27
  private rollup: RollupContract,
28
28
  private l1Client: ViemClient,
29
- private slashFactoryAddress: EthAddress | undefined,
30
29
  private watchers: Watcher[],
31
30
  private epochCache: EpochCache,
32
31
  private dateProvider: DateProvider,
33
32
  private kvStore: AztecLMDBStoreV2,
33
+ private rollupRegisteredAtL2Slot: SlotNumber,
34
34
  private logger = createLogger('slasher'),
35
35
  ) {}
36
36
 
@@ -61,19 +61,15 @@ export class SlasherClientFacade implements SlasherClientInterface {
61
61
  this.watchers.forEach(watcher => watcher.updateConfig?.(config));
62
62
  }
63
63
 
64
- public getSlashPayloads(): Promise<SlashPayloadRound[]> {
65
- return this.client?.getSlashPayloads() ?? Promise.reject(new Error('Slasher client not initialized'));
66
- }
67
-
68
64
  public gatherOffensesForRound(round?: bigint): Promise<Offense[]> {
69
65
  return this.client?.gatherOffensesForRound(round) ?? Promise.reject(new Error('Slasher client not initialized'));
70
66
  }
71
67
 
72
- public getPendingOffenses(): Promise<Offense[]> {
73
- return this.client?.getPendingOffenses() ?? Promise.reject(new Error('Slasher client not initialized'));
68
+ public getOffenses(): Promise<Offense[]> {
69
+ return this.client?.getOffenses() ?? Promise.reject(new Error('Slasher client not initialized'));
74
70
  }
75
71
 
76
- public getProposerActions(slotNumber: bigint): Promise<ProposerSlashAction[]> {
72
+ public getProposerActions(slotNumber: SlotNumber): Promise<ProposerSlashAction[]> {
77
73
  return this.client?.getProposerActions(slotNumber) ?? Promise.reject(new Error('Slasher client not initialized'));
78
74
  }
79
75
 
@@ -82,11 +78,11 @@ export class SlasherClientFacade implements SlasherClientInterface {
82
78
  this.config,
83
79
  this.rollup,
84
80
  this.l1Client,
85
- this.slashFactoryAddress,
86
81
  this.watchers,
87
82
  this.epochCache,
88
83
  this.dateProvider,
89
84
  this.kvStore,
85
+ this.rollupRegisteredAtL2Slot,
90
86
  this.logger,
91
87
  );
92
88
  }
@@ -1,10 +1,8 @@
1
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
1
2
  import type { SlasherConfig } from '@aztec/stdlib/interfaces/server';
2
- import type { Offense, ProposerSlashAction, SlashPayloadRound } from '@aztec/stdlib/slashing';
3
+ import type { Offense, ProposerSlashAction } from '@aztec/stdlib/slashing';
3
4
 
4
- /**
5
- * Common interface for slasher clients used by the Aztec node.
6
- * Both Empire and Consensus slasher clients implement this interface.
7
- */
5
+ /** Common interface for slasher clients used by the Aztec node. */
8
6
  export interface SlasherClientInterface {
9
7
  /** Start the slasher client */
10
8
  start(): Promise<void>;
@@ -12,25 +10,13 @@ export interface SlasherClientInterface {
12
10
  /** Stop the slasher client */
13
11
  stop(): Promise<void>;
14
12
 
15
- /**
16
- * Get slash payloads for the Empire model.
17
- * The Consensus model should throw an error when this is called.
18
- */
19
- getSlashPayloads(): Promise<SlashPayloadRound[]>;
20
-
21
- /**
22
- * Gather offenses for a given round, defaults to current.
23
- * Used by both Empire and Consensus models.
24
- */
13
+ /** Gather offenses for a given round, defaults to current. */
25
14
  gatherOffensesForRound(round?: bigint): Promise<Offense[]>;
26
15
 
27
- /** Returns all pending offenses */
28
- getPendingOffenses(): Promise<Offense[]>;
16
+ /** Returns all offenses */
17
+ getOffenses(): Promise<Offense[]>;
29
18
 
30
- /**
31
- * Update the configuration.
32
- * Used by both Empire and Consensus models.
33
- */
19
+ /** Update the configuration. */
34
20
  updateConfig(config: Partial<SlasherConfig>): void;
35
21
 
36
22
  /**
@@ -38,7 +24,7 @@ export interface SlasherClientInterface {
38
24
  * @param slotNumber - The current slot number
39
25
  * @returns The actions to take
40
26
  */
41
- getProposerActions(slotNumber: bigint): Promise<ProposerSlashAction[]>;
27
+ getProposerActions(slotNumber: SlotNumber): Promise<ProposerSlashAction[]>;
42
28
 
43
29
  /** Returns the current config */
44
30
  getConfig(): SlasherConfig;
@@ -1,5 +1,5 @@
1
1
  import { createLogger } from '@aztec/aztec.js/log';
2
- import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncMultiMap, AztecAsyncSet } from '@aztec/kv-store';
2
+ import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncMultiMap } from '@aztec/kv-store';
3
3
  import {
4
4
  type Offense,
5
5
  type OffenseIdentifier,
@@ -10,14 +10,15 @@ import {
10
10
 
11
11
  export const SCHEMA_VERSION = 1;
12
12
 
13
+ type ClearOffensesFilter = Pick<Offense, 'offenseType' | 'epochOrSlot'> & {
14
+ validators?: Offense['validator'][];
15
+ };
16
+
13
17
  export class SlasherOffensesStore {
14
18
  /** Map from offense key to offense data */
15
19
  private offenses: AztecAsyncMap<string, Buffer>;
16
20
 
17
- /** Map from offense key to whether the offense has been executed (only used for empire based slashing) */
18
- private offensesSlashed: AztecAsyncSet<string>;
19
-
20
- /** Multimap from round to offense keys (only used for consensus based slashing) */
21
+ /** Multimap from round to offense keys */
21
22
  private roundsOffenses: AztecAsyncMultiMap<string, string>;
22
23
 
23
24
  private log = createLogger('slasher:store:offenses');
@@ -32,18 +33,13 @@ export class SlasherOffensesStore {
32
33
  ) {
33
34
  this.offenses = kvStore.openMap('offenses');
34
35
  this.roundsOffenses = kvStore.openMultiMap('rounds-offenses');
35
- this.offensesSlashed = kvStore.openSet('offenses-slashed');
36
36
  }
37
37
 
38
- /** Returns all offenses not marked as slashed */
39
- public async getPendingOffenses(): Promise<Offense[]> {
38
+ /** Returns all offenses */
39
+ public async getOffenses(): Promise<Offense[]> {
40
40
  const offenses: Offense[] = [];
41
- for await (const [key, buffer] of this.offenses.entriesAsync()) {
42
- if (await this.offensesSlashed.hasAsync(key)) {
43
- continue; // Skip executed offenses
44
- }
45
- const offense = deserializeOffense(buffer);
46
- offenses.push(offense);
41
+ for await (const [, buffer] of this.offenses.entriesAsync()) {
42
+ offenses.push(deserializeOffense(buffer));
47
43
  }
48
44
  return offenses;
49
45
  }
@@ -61,34 +57,68 @@ export class SlasherOffensesStore {
61
57
  return offenses;
62
58
  }
63
59
 
64
- /** Returns whether an offense is pending (ie not marked as slashed) */
65
- public async hasPendingOffense(offense: OffenseIdentifier): Promise<boolean> {
66
- const key = this.getOffenseKey(offense);
67
- return (await this.offenses.getAsync(key)) !== undefined && !(await this.offensesSlashed.hasAsync(key));
68
- }
69
-
70
60
  /** Returns whether we have seen this offense */
71
61
  public async hasOffense(offense: OffenseIdentifier): Promise<boolean> {
72
62
  const key = this.getOffenseKey(offense);
73
63
  return (await this.offenses.getAsync(key)) !== undefined;
74
64
  }
75
65
 
76
- /** Adds a new offense (defaults to pending, but will be slashed if markAsSlashed had been called for it) */
77
- public async addPendingOffense(offense: Offense): Promise<void> {
66
+ /** Adds a new offense. Returns false if the offense is already pending. */
67
+ public async addOffense(offense: Offense): Promise<boolean> {
78
68
  const key = this.getOffenseKey(offense);
79
- await this.offenses.set(key, serializeOffense(offense));
80
69
  const round = getRoundForOffense(offense, this.settings);
81
- await this.roundsOffenses.set(this.getRoundKey(round), key);
82
- this.log.trace(`Adding pending offense ${key} for round ${round}`);
70
+ const added = await this.kvStore.transactionAsync(async () => {
71
+ if ((await this.offenses.getAsync(key)) !== undefined) {
72
+ return false;
73
+ }
74
+
75
+ await this.offenses.set(key, serializeOffense(offense));
76
+ await this.roundsOffenses.set(this.getRoundKey(round), key);
77
+ return true;
78
+ });
79
+
80
+ if (added) {
81
+ this.log.trace(`Adding pending offense ${key} for round ${round}`);
82
+ }
83
+
84
+ return added;
83
85
  }
84
86
 
85
- /** Marks the given offenses as slashed (regardless of whether they are known or not) */
86
- public async markAsSlashed(offenses: OffenseIdentifier[]): Promise<void> {
87
- await this.kvStore.transactionAsync(async () => {
88
- for (const offense of offenses) {
89
- const key = this.getOffenseKey(offense);
90
- await this.offensesSlashed.add(key);
87
+ /** Removes pending offenses matching the given offense type, epoch/slot, and optional validators. */
88
+ public async clearOffenses(filter: ClearOffensesFilter): Promise<number> {
89
+ return await this.kvStore.transactionAsync(async () => {
90
+ const offensesToClear = new Map<string, Offense>();
91
+
92
+ if (filter.validators && filter.validators.length > 0) {
93
+ for (const validator of filter.validators) {
94
+ const identifier = { validator, offenseType: filter.offenseType, epochOrSlot: filter.epochOrSlot };
95
+ const key = this.getOffenseKey(identifier);
96
+ const buffer = await this.offenses.getAsync(key);
97
+ if (buffer) {
98
+ offensesToClear.set(key, deserializeOffense(buffer));
99
+ }
100
+ }
101
+ } else {
102
+ for await (const [key, buffer] of this.offenses.entriesAsync()) {
103
+ const offense = deserializeOffense(buffer);
104
+ if (offense.offenseType === filter.offenseType && offense.epochOrSlot === filter.epochOrSlot) {
105
+ offensesToClear.set(key, offense);
106
+ }
107
+ }
91
108
  }
109
+
110
+ if (offensesToClear.size === 0) {
111
+ return 0;
112
+ }
113
+
114
+ for (const [key, offense] of offensesToClear) {
115
+ const round = getRoundForOffense(offense, this.settings);
116
+ await this.offenses.delete(key);
117
+ await this.roundsOffenses.deleteValue(this.getRoundKey(round), key);
118
+ this.log.trace(`Cleared pending offense ${key} for round ${round}`);
119
+ }
120
+
121
+ return offensesToClear.size;
92
122
  });
93
123
  }
94
124
 
@@ -104,34 +134,32 @@ export class SlasherOffensesStore {
104
134
  return 0; // Not enough rounds have passed to expire anything
105
135
  }
106
136
 
107
- // Collect expired offenses and rounds
108
- const expiredRoundKeys = new Set<string>();
109
- const expiredOffenseKeys = new Set<string>();
110
- for await (const [roundKey, offenseKey] of this.roundsOffenses.entriesAsync({
111
- end: this.getRoundKey(expiredBefore),
112
- })) {
113
- expiredOffenseKeys.add(offenseKey);
114
- expiredRoundKeys.add(roundKey);
115
- }
137
+ return await this.kvStore.transactionAsync(async () => {
138
+ // Collect expired offenses and rounds
139
+ const expiredRoundKeys = new Set<string>();
140
+ const expiredOffenseKeys = new Set<string>();
141
+ for await (const [roundKey, offenseKey] of this.roundsOffenses.entriesAsync({
142
+ end: this.getRoundKey(expiredBefore),
143
+ })) {
144
+ expiredOffenseKeys.add(offenseKey);
145
+ expiredRoundKeys.add(roundKey);
146
+ }
116
147
 
117
- if (expiredOffenseKeys.size === 0 && expiredRoundKeys.size === 0) {
118
- return 0; // Nothing to clean up
119
- }
148
+ if (expiredOffenseKeys.size === 0 && expiredRoundKeys.size === 0) {
149
+ return 0; // Nothing to clean up
150
+ }
120
151
 
121
- // Remove expired stuff in a transaction
122
- await this.kvStore.transactionAsync(async () => {
123
152
  for (const key of expiredOffenseKeys) {
124
153
  this.log.trace(`Deleting offense ${key}`);
125
154
  await this.offenses.delete(key);
126
- await this.offensesSlashed.delete(key);
127
155
  }
128
156
  for (const roundKey of expiredRoundKeys) {
129
157
  this.log.trace(`Deleting round info for ${roundKey}`);
130
158
  await this.roundsOffenses.delete(roundKey);
131
159
  }
132
- });
133
160
 
134
- return expiredOffenseKeys.size;
161
+ return expiredOffenseKeys.size;
162
+ });
135
163
  }
136
164
 
137
165
  /** Generate a unique key for an offense */