@aztec/slasher 0.0.1-commit.85d7d01 → 0.0.1-commit.8655d4a

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 (91) hide show
  1. package/README.md +76 -79
  2. package/dest/config.d.ts +1 -1
  3. package/dest/config.d.ts.map +1 -1
  4. package/dest/config.js +29 -29
  5. package/dest/factory/create_facade.d.ts +3 -3
  6. package/dest/factory/create_facade.d.ts.map +1 -1
  7. package/dest/factory/create_facade.js +25 -2
  8. package/dest/factory/create_implementation.d.ts +6 -7
  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 +5 -5
  18. package/dest/generated/slasher-defaults.js +5 -5
  19. package/dest/index.d.ts +6 -4
  20. package/dest/index.d.ts.map +1 -1
  21. package/dest/index.js +5 -3
  22. package/dest/null_slasher_client.d.ts +3 -4
  23. package/dest/null_slasher_client.d.ts.map +1 -1
  24. package/dest/null_slasher_client.js +1 -4
  25. package/dest/slash_offenses_collector.d.ts +10 -9
  26. package/dest/slash_offenses_collector.d.ts.map +1 -1
  27. package/dest/slash_offenses_collector.js +50 -30
  28. package/dest/slasher_client.d.ts +112 -0
  29. package/dest/slasher_client.d.ts.map +1 -0
  30. package/dest/{tally_slasher_client.js → slasher_client.js} +45 -45
  31. package/dest/slasher_client_facade.d.ts +6 -8
  32. package/dest/slasher_client_facade.d.ts.map +1 -1
  33. package/dest/slasher_client_facade.js +6 -9
  34. package/dest/slasher_client_interface.d.ts +7 -21
  35. package/dest/slasher_client_interface.d.ts.map +1 -1
  36. package/dest/slasher_client_interface.js +1 -4
  37. package/dest/stores/offenses_store.d.ts +12 -12
  38. package/dest/stores/offenses_store.d.ts.map +1 -1
  39. package/dest/stores/offenses_store.js +61 -38
  40. package/dest/watcher.d.ts +8 -1
  41. package/dest/watcher.d.ts.map +1 -1
  42. package/dest/watcher.js +1 -0
  43. package/dest/watchers/attestations_block_watcher.d.ts +26 -13
  44. package/dest/watchers/attestations_block_watcher.d.ts.map +1 -1
  45. package/dest/watchers/attestations_block_watcher.js +76 -61
  46. package/dest/watchers/attested_invalid_proposal_watcher.d.ts +42 -0
  47. package/dest/watchers/attested_invalid_proposal_watcher.d.ts.map +1 -0
  48. package/dest/watchers/attested_invalid_proposal_watcher.js +117 -0
  49. package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.d.ts +38 -0
  50. package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.d.ts.map +1 -0
  51. package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.js +138 -0
  52. package/dest/watchers/checkpoint_equivocation_watcher.d.ts +30 -0
  53. package/dest/watchers/checkpoint_equivocation_watcher.d.ts.map +1 -0
  54. package/dest/watchers/checkpoint_equivocation_watcher.js +69 -0
  55. package/dest/watchers/data_withholding_watcher.d.ts +63 -0
  56. package/dest/watchers/data_withholding_watcher.d.ts.map +1 -0
  57. package/dest/watchers/data_withholding_watcher.js +193 -0
  58. package/package.json +10 -10
  59. package/src/config.ts +35 -29
  60. package/src/factory/create_facade.ts +32 -4
  61. package/src/factory/create_implementation.ts +24 -105
  62. package/src/factory/get_settings.ts +8 -8
  63. package/src/factory/index.ts +1 -1
  64. package/src/generated/slasher-defaults.ts +5 -5
  65. package/src/index.ts +5 -3
  66. package/src/null_slasher_client.ts +2 -6
  67. package/src/slash_offenses_collector.ts +70 -32
  68. package/src/{tally_slasher_client.ts → slasher_client.ts} +63 -54
  69. package/src/slasher_client_facade.ts +6 -11
  70. package/src/slasher_client_interface.ts +6 -21
  71. package/src/stores/offenses_store.ts +73 -47
  72. package/src/watcher.ts +8 -0
  73. package/src/watchers/attestations_block_watcher.ts +88 -82
  74. package/src/watchers/attested_invalid_proposal_watcher.ts +168 -0
  75. package/src/watchers/broadcasted_invalid_checkpoint_proposal_watcher.ts +192 -0
  76. package/src/watchers/checkpoint_equivocation_watcher.ts +96 -0
  77. package/src/watchers/data_withholding_watcher.ts +225 -0
  78. package/dest/empire_slasher_client.d.ts +0 -190
  79. package/dest/empire_slasher_client.d.ts.map +0 -1
  80. package/dest/empire_slasher_client.js +0 -564
  81. package/dest/stores/payloads_store.d.ts +0 -29
  82. package/dest/stores/payloads_store.d.ts.map +0 -1
  83. package/dest/stores/payloads_store.js +0 -128
  84. package/dest/tally_slasher_client.d.ts +0 -125
  85. package/dest/tally_slasher_client.d.ts.map +0 -1
  86. package/dest/watchers/epoch_prune_watcher.d.ts +0 -39
  87. package/dest/watchers/epoch_prune_watcher.d.ts.map +0 -1
  88. package/dest/watchers/epoch_prune_watcher.js +0 -176
  89. package/src/empire_slasher_client.ts +0 -649
  90. package/src/stores/payloads_store.ts +0 -149
  91. package/src/watchers/epoch_prune_watcher.ts +0 -253
@@ -1,15 +1,16 @@
1
1
  import { EpochCache } from '@aztec/epoch-cache';
2
- import { SlotNumber } from '@aztec/foundation/branded-types';
2
+ import { EpochNumber } from '@aztec/foundation/branded-types';
3
3
  import { merge, pick } from '@aztec/foundation/collection';
4
- import { type Logger, createLogger } from '@aztec/foundation/log';
4
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
5
5
  import {
6
+ type DescendentOfInvalidAttestationsCheckpointEvent,
6
7
  type InvalidCheckpointDetectedEvent,
7
8
  type L2BlockSourceEventEmitter,
8
9
  L2BlockSourceEvents,
9
10
  type ValidateCheckpointNegativeResult,
10
11
  } from '@aztec/stdlib/block';
11
- import type { CheckpointInfo } from '@aztec/stdlib/checkpoint';
12
- import { OffenseType } from '@aztec/stdlib/slashing';
12
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
13
+ import { OffenseType, getOffenseTypeName } from '@aztec/stdlib/slashing';
13
14
 
14
15
  import EventEmitter from 'node:events';
15
16
 
@@ -17,27 +18,31 @@ import type { SlasherConfig } from '../config.js';
17
18
  import { WANT_TO_SLASH_EVENT, type WantToSlashArgs, type Watcher, type WatcherEmitter } from '../watcher.js';
18
19
 
19
20
  const AttestationsBlockWatcherConfigKeys = [
20
- 'slashAttestDescendantOfInvalidPenalty',
21
+ 'slashProposeDescendantOfCheckpointWithInvalidAttestationsPenalty',
21
22
  'slashProposeInvalidAttestationsPenalty',
22
23
  ] as const;
23
24
 
24
25
  type AttestationsBlockWatcherConfig = Pick<SlasherConfig, (typeof AttestationsBlockWatcherConfigKeys)[number]>;
25
26
 
26
27
  /**
27
- * This watcher is responsible for detecting invalid blocks and creating slashing arguments for offenders.
28
- * An invalid block is one that doesn't have enough attestations or has incorrect attestations.
29
- * The proposer of an invalid block should be slashed.
30
- * If there's another block consecutive to the invalid one, its proposer and attestors should also be slashed.
28
+ * Watches the archiver for checkpoints whose publication is itself a slashable offense.
29
+ *
30
+ * Two cases are handled, both targeting the proposer of the offending checkpoint:
31
+ *
32
+ * - Invalid-attestations checkpoint: the proposer published a checkpoint to L1 whose
33
+ * attestations are either insufficient (below quorum) or incorrect (signature from a
34
+ * non-committee member, malformed signature, etc.). Slashed via
35
+ * {@link OffenseType.PROPOSED_INSUFFICIENT_ATTESTATIONS} or
36
+ * {@link OffenseType.PROPOSED_INCORRECT_ATTESTATIONS}.
37
+ *
38
+ * - Descendant of an invalid checkpoint: the proposer published a checkpoint that extends a
39
+ * previously-rejected one. The descendant may itself have valid attestations, but it is still
40
+ * unusable. Triggered by the archiver's `CheckpointBuiltOnInvalidAncestorDetected` event
41
+ * when the descendant has valid attestations (skipped before ingestion). Slashes the descendant's
42
+ * proposer via {@link OffenseType.PROPOSED_DESCENDANT_OF_CHECKPOINT_WITH_INVALID_ATTESTATIONS}.
31
43
  */
32
44
  export class AttestationsBlockWatcher extends (EventEmitter as new () => WatcherEmitter) implements Watcher {
33
- private log: Logger = createLogger('attestations-block-watcher');
34
-
35
- // Only keep track of the last N invalid checkpoints
36
- private maxInvalidCheckpoints = 100;
37
-
38
- // All invalid archive roots seen
39
- private invalidArchiveRoots: Set<string> = new Set();
40
-
45
+ private log: Logger;
41
46
  private config: AttestationsBlockWatcherConfig;
42
47
 
43
48
  private boundHandleInvalidCheckpoint = (event: InvalidCheckpointDetectedEvent) => {
@@ -51,12 +56,23 @@ export class AttestationsBlockWatcher extends (EventEmitter as new () => Watcher
51
56
  }
52
57
  };
53
58
 
59
+ private boundHandleDescendantOfInvalid = (event: DescendentOfInvalidAttestationsCheckpointEvent) => {
60
+ this.handleDescendantOfInvalid(event).catch(err => {
61
+ this.log.error('Error handling descendant of invalid checkpoint', err, {
62
+ checkpointNumber: event.checkpoint.checkpointNumber,
63
+ ancestorCheckpointNumber: event.ancestorCheckpointNumber,
64
+ });
65
+ });
66
+ };
67
+
54
68
  constructor(
55
69
  private l2BlockSource: L2BlockSourceEventEmitter,
56
70
  private epochCache: EpochCache,
57
71
  config: AttestationsBlockWatcherConfig,
72
+ bindings?: LoggerBindings,
58
73
  ) {
59
74
  super();
75
+ this.log = createLogger('slasher:attestations-block-watcher', bindings);
60
76
  this.config = pick(config, ...AttestationsBlockWatcherConfigKeys);
61
77
  this.log.info('AttestationsBlockWatcher initialized');
62
78
  }
@@ -71,6 +87,10 @@ export class AttestationsBlockWatcher extends (EventEmitter as new () => Watcher
71
87
  L2BlockSourceEvents.InvalidAttestationsCheckpointDetected,
72
88
  this.boundHandleInvalidCheckpoint,
73
89
  );
90
+ this.l2BlockSource.events.on(
91
+ L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected,
92
+ this.boundHandleDescendantOfInvalid,
93
+ );
74
94
  return Promise.resolve();
75
95
  }
76
96
 
@@ -79,91 +99,87 @@ export class AttestationsBlockWatcher extends (EventEmitter as new () => Watcher
79
99
  L2BlockSourceEvents.InvalidAttestationsCheckpointDetected,
80
100
  this.boundHandleInvalidCheckpoint,
81
101
  );
102
+ this.l2BlockSource.events.removeListener(
103
+ L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected,
104
+ this.boundHandleDescendantOfInvalid,
105
+ );
82
106
  return Promise.resolve();
83
107
  }
84
108
 
85
109
  /** Event handler for invalid checkpoints as reported by the archiver. Public for testing purposes. */
86
110
  public handleInvalidCheckpoint(event: InvalidCheckpointDetectedEvent): void {
87
111
  const { validationResult } = event;
88
- const checkpoint = validationResult.checkpoint;
89
-
90
- // Check if we already have processed this checkpoint, archiver may emit the same event multiple times
91
- if (this.invalidArchiveRoots.has(checkpoint.archive.toString())) {
92
- this.log.trace(`Already processed invalid checkpoint ${checkpoint.checkpointNumber}`);
93
- return;
94
- }
112
+ const { reason, checkpoint } = validationResult;
95
113
 
96
114
  this.log.verbose(`Detected invalid checkpoint ${checkpoint.checkpointNumber}`, {
97
115
  ...checkpoint,
98
116
  reason: validationResult.valid === false ? validationResult.reason : 'unknown',
99
117
  });
100
118
 
101
- // Store the invalid checkpoint
102
- this.addInvalidCheckpoint(event.validationResult.checkpoint);
119
+ const { checkpointNumber, slotNumber: slot } = checkpoint;
120
+ const epochCommitteeInfo = { ...validationResult, isEscapeHatchOpen: false };
121
+ const proposer = this.epochCache.getProposerFromEpochCommittee(epochCommitteeInfo, slot);
103
122
 
104
- // Slash the proposer of the invalid checkpoint
105
- this.slashProposer(event.validationResult);
123
+ if (!proposer) {
124
+ this.log.warn(`No proposer found for checkpoint ${checkpointNumber} at slot ${slot}`);
125
+ return;
126
+ }
106
127
 
107
- // Check if the parent of this checkpoint is invalid as well, if so, we will slash its attestors as well
108
- this.slashAttestorsOnAncestorInvalid(event.validationResult);
109
- }
128
+ const offense = this.getOffenseFromInvalidationReason(reason);
129
+ const amount = this.config.slashProposeInvalidAttestationsPenalty;
130
+ const args: WantToSlashArgs = {
131
+ validator: proposer,
132
+ amount,
133
+ offenseType: offense,
134
+ epochOrSlot: BigInt(slot),
135
+ };
110
136
 
111
- private slashAttestorsOnAncestorInvalid(validationResult: ValidateCheckpointNegativeResult) {
112
- const checkpoint = validationResult.checkpoint;
113
-
114
- const parentArchive = checkpoint.lastArchive.toString();
115
- if (this.invalidArchiveRoots.has(parentArchive)) {
116
- const attestors = validationResult.attestors;
117
- this.log.info(
118
- `Want to slash attestors of checkpoint ${checkpoint.checkpointNumber} built on invalid checkpoint`,
119
- {
120
- ...checkpoint,
121
- ...attestors,
122
- parentArchive,
123
- },
124
- );
137
+ this.log.info(`Detected invalid attestations checkpoint proposer offense`, {
138
+ ...checkpoint,
139
+ reason,
140
+ validator: args.validator.toString(),
141
+ amount: args.amount,
142
+ offenseType: getOffenseTypeName(args.offenseType),
143
+ epochOrSlot: args.epochOrSlot,
144
+ });
125
145
 
126
- this.emit(
127
- WANT_TO_SLASH_EVENT,
128
- attestors.map(attestor => ({
129
- validator: attestor,
130
- amount: this.config.slashAttestDescendantOfInvalidPenalty,
131
- offenseType: OffenseType.ATTESTED_DESCENDANT_OF_INVALID,
132
- epochOrSlot: BigInt(SlotNumber(checkpoint.slotNumber)),
133
- })),
134
- );
135
- }
146
+ this.emit(WANT_TO_SLASH_EVENT, [args]);
136
147
  }
137
148
 
138
- private slashProposer(validationResult: ValidateCheckpointNegativeResult) {
139
- const { reason, checkpoint } = validationResult;
140
- const checkpointNumber = checkpoint.checkpointNumber;
149
+ /**
150
+ * Event handler for valid-attestations checkpoints that build on a previously-rejected ancestor.
151
+ * The archiver emits this when ingesting the descendant, and we slash its proposer.
152
+ */
153
+ public async handleDescendantOfInvalid(event: DescendentOfInvalidAttestationsCheckpointEvent): Promise<void> {
154
+ const { checkpoint, ancestorCheckpointNumber, ancestorArchiveRoot } = event;
155
+
141
156
  const slot = checkpoint.slotNumber;
142
- const epochCommitteeInfo = {
143
- committee: validationResult.committee,
144
- seed: validationResult.seed,
145
- epoch: validationResult.epoch,
146
- isEscapeHatchOpen: false,
147
- };
148
- const proposer = this.epochCache.getProposerFromEpochCommittee(epochCommitteeInfo, slot);
157
+ const epoch = EpochNumber(getEpochAtSlot(slot, this.epochCache.getL1Constants()));
158
+ const epochCommitteeInfo = await this.epochCache.getCommitteeForEpoch(epoch);
159
+ const proposer = this.epochCache.getProposerFromEpochCommittee({ ...epochCommitteeInfo, epoch }, slot);
149
160
 
150
161
  if (!proposer) {
151
- this.log.warn(`No proposer found for checkpoint ${checkpointNumber} at slot ${slot}`);
162
+ this.log.warn(
163
+ `No proposer found for invalid descendant checkpoint ${checkpoint.checkpointNumber} at slot ${slot}`,
164
+ );
152
165
  return;
153
166
  }
154
167
 
155
- const offense = this.getOffenseFromInvalidationReason(reason);
156
- const amount = this.config.slashProposeInvalidAttestationsPenalty;
157
168
  const args: WantToSlashArgs = {
158
169
  validator: proposer,
159
- amount,
160
- offenseType: offense,
170
+ amount: this.config.slashProposeDescendantOfCheckpointWithInvalidAttestationsPenalty,
171
+ offenseType: OffenseType.PROPOSED_DESCENDANT_OF_CHECKPOINT_WITH_INVALID_ATTESTATIONS,
161
172
  epochOrSlot: BigInt(slot),
162
173
  };
163
174
 
164
- this.log.info(`Want to slash proposer of checkpoint ${checkpointNumber} due to ${reason}`, {
175
+ this.log.info(`Detected invalid descendant checkpoint proposer offense`, {
165
176
  ...checkpoint,
166
- ...args,
177
+ ancestorCheckpointNumber,
178
+ ancestorArchiveRoot: ancestorArchiveRoot.toString(),
179
+ validator: args.validator.toString(),
180
+ amount: args.amount,
181
+ offenseType: getOffenseTypeName(args.offenseType),
182
+ epochOrSlot: args.epochOrSlot,
167
183
  });
168
184
 
169
185
  this.emit(WANT_TO_SLASH_EVENT, [args]);
@@ -181,14 +197,4 @@ export class AttestationsBlockWatcher extends (EventEmitter as new () => Watcher
181
197
  }
182
198
  }
183
199
  }
184
-
185
- private addInvalidCheckpoint(checkpoint: CheckpointInfo) {
186
- this.invalidArchiveRoots.add(checkpoint.archive.toString());
187
-
188
- // Prune old entries if we exceed the maximum
189
- if (this.invalidArchiveRoots.size > this.maxInvalidCheckpoints) {
190
- const oldestKey = this.invalidArchiveRoots.keys().next().value!;
191
- this.invalidArchiveRoots.delete(oldestKey);
192
- }
193
- }
194
200
  }
@@ -0,0 +1,168 @@
1
+ import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
+ import { SlotNumber } from '@aztec/foundation/branded-types';
3
+ import { merge, pick } from '@aztec/foundation/collection';
4
+ import type { EthAddress } from '@aztec/foundation/eth-address';
5
+ import { FifoSet } from '@aztec/foundation/fifo-set';
6
+ import { type Logger, createLogger } from '@aztec/foundation/log';
7
+ import { RunningPromise } from '@aztec/foundation/running-promise';
8
+ import type { L2BlockSource } from '@aztec/stdlib/block';
9
+ import type { P2PClient, SlasherConfig } from '@aztec/stdlib/interfaces/server';
10
+ import type { CheckpointAttestation } from '@aztec/stdlib/p2p';
11
+ import { OffenseType, getOffenseTypeName } from '@aztec/stdlib/slashing';
12
+
13
+ import EventEmitter from 'node:events';
14
+
15
+ import { WANT_TO_SLASH_EVENT, type WantToSlashArgs, type Watcher, type WatcherEmitter } from '../watcher.js';
16
+
17
+ const AttestedInvalidProposalWatcherConfigKeys = ['slashAttestInvalidCheckpointProposalPenalty'] as const;
18
+
19
+ const SCAN_SLOT_LAG = 1;
20
+ const DEFAULT_SCAN_SLOT_LOOKBACK = 4;
21
+ const MAX_TRACKED_BAD_ATTESTATIONS = 10_000;
22
+
23
+ type AttestedInvalidProposalWatcherConfig = Pick<
24
+ SlasherConfig,
25
+ (typeof AttestedInvalidProposalWatcherConfigKeys)[number]
26
+ >;
27
+
28
+ type P2PCheckpointAttestationSource = Pick<P2PClient, 'getCheckpointAttestationsForSlot'>;
29
+
30
+ type AttestedInvalidProposalWatcherOptions = {
31
+ scanSlotLookback?: number;
32
+ log?: Logger;
33
+ };
34
+
35
+ export type InvalidProposalSlotSource = {
36
+ hasInvalidProposals(slot: SlotNumber): boolean;
37
+ hasProposalEquivocation(slot: SlotNumber): boolean;
38
+ };
39
+
40
+ export class AttestedInvalidProposalWatcher extends (EventEmitter as new () => WatcherEmitter) implements Watcher {
41
+ private readonly log: Logger;
42
+ private readonly runningPromise: RunningPromise;
43
+ private readonly emittedOffenses = FifoSet.withLimit<string>(MAX_TRACKED_BAD_ATTESTATIONS);
44
+ private readonly scanSlotLookback: number;
45
+ private config: AttestedInvalidProposalWatcherConfig;
46
+ private lastScannedSlot: SlotNumber | undefined;
47
+
48
+ constructor(
49
+ private readonly p2pClient: P2PCheckpointAttestationSource,
50
+ private readonly invalidProposalSlotSource: InvalidProposalSlotSource,
51
+ private readonly l2BlockSource: Pick<L2BlockSource, 'getSyncedL2SlotNumber'>,
52
+ private readonly epochCache: Pick<EpochCacheInterface, 'getSlotNow' | 'getL1Constants'>,
53
+ config: AttestedInvalidProposalWatcherConfig,
54
+ options: AttestedInvalidProposalWatcherOptions = {},
55
+ ) {
56
+ super();
57
+ const constants = epochCache.getL1Constants();
58
+ this.log = options.log ?? createLogger('attested-invalid-proposal-watcher');
59
+ this.config = pick(config, ...AttestedInvalidProposalWatcherConfigKeys);
60
+ this.scanSlotLookback = Math.max(1, options.scanSlotLookback ?? DEFAULT_SCAN_SLOT_LOOKBACK);
61
+
62
+ const intervalMs = Math.max(1000, (constants.ethereumSlotDuration * 1000) / 4);
63
+ this.runningPromise = new RunningPromise(() => this.scan(), this.log, intervalMs);
64
+ this.log.info('AttestedInvalidProposalWatcher initialized', { scanSlotLookback: this.scanSlotLookback });
65
+ }
66
+
67
+ public updateConfig(config: Partial<SlasherConfig>): void {
68
+ this.config = merge(this.config, pick(config, ...AttestedInvalidProposalWatcherConfigKeys));
69
+ this.log.verbose('AttestedInvalidProposalWatcher config updated', this.config);
70
+ }
71
+
72
+ public start(): Promise<void> {
73
+ this.runningPromise.start();
74
+ return Promise.resolve();
75
+ }
76
+
77
+ public stop(): Promise<void> {
78
+ return this.runningPromise.stop();
79
+ }
80
+
81
+ public async scan(): Promise<void> {
82
+ const currentSlot = (await this.l2BlockSource.getSyncedL2SlotNumber()) ?? this.epochCache.getSlotNow();
83
+ // genesis
84
+ if (currentSlot <= SlotNumber(SCAN_SLOT_LAG)) {
85
+ return;
86
+ }
87
+
88
+ const newestSlotToConsider = SlotNumber(currentSlot - SCAN_SLOT_LAG);
89
+ const oldestSlot =
90
+ this.lastScannedSlot === undefined
91
+ ? SlotNumber(Math.max(0, newestSlotToConsider - this.scanSlotLookback + 1))
92
+ : SlotNumber(this.lastScannedSlot + 1);
93
+ if (oldestSlot > newestSlotToConsider) {
94
+ return;
95
+ }
96
+
97
+ for (let slot = oldestSlot; slot <= newestSlotToConsider; slot++) {
98
+ await this.scanSlot(slot);
99
+ }
100
+
101
+ this.lastScannedSlot = newestSlotToConsider;
102
+ }
103
+
104
+ /** Scans a single invalid-proposal slot. */
105
+ public async scanSlot(slot: SlotNumber): Promise<void> {
106
+ if (
107
+ this.invalidProposalSlotSource.hasProposalEquivocation(slot) ||
108
+ !this.invalidProposalSlotSource.hasInvalidProposals(slot)
109
+ ) {
110
+ return;
111
+ }
112
+
113
+ let attestations: CheckpointAttestation[];
114
+ try {
115
+ attestations = await this.p2pClient.getCheckpointAttestationsForSlot(slot);
116
+ } catch (err) {
117
+ this.log.warn('Error getting checkpoint attestations for invalid proposal slot', { err, slot });
118
+ return;
119
+ }
120
+
121
+ const slashArgs = attestations
122
+ .map(attestation => this.getSlashArgs(slot, attestation))
123
+ .filter((args): args is WantToSlashArgs => args !== undefined)
124
+ .filter(args => this.markAsNewOffense(args));
125
+
126
+ if (slashArgs.length === 0) {
127
+ return;
128
+ }
129
+
130
+ this.log.info('Detected attestations to invalid checkpoint proposal', {
131
+ slot,
132
+ offenses: slashArgs.map(args => ({
133
+ validator: args.validator.toString(),
134
+ amount: args.amount,
135
+ offenseType: getOffenseTypeName(args.offenseType),
136
+ epochOrSlot: args.epochOrSlot,
137
+ })),
138
+ });
139
+ this.emit(WANT_TO_SLASH_EVENT, slashArgs);
140
+ }
141
+
142
+ private getSlashArgs(slot: SlotNumber, attestation: CheckpointAttestation): WantToSlashArgs | undefined {
143
+ const attester = attestation.getSender();
144
+ if (!attester) {
145
+ this.log.warn('Cannot slash checkpoint attestation with invalid signature', {
146
+ slot,
147
+ archive: attestation.archive.toString(),
148
+ });
149
+ return undefined;
150
+ }
151
+
152
+ return this.getSlashArgsForAttester(slot, attester);
153
+ }
154
+
155
+ private getSlashArgsForAttester(slot: SlotNumber, attester: EthAddress): WantToSlashArgs {
156
+ return {
157
+ validator: attester,
158
+ amount: this.config.slashAttestInvalidCheckpointProposalPenalty,
159
+ offenseType: OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL,
160
+ epochOrSlot: BigInt(slot),
161
+ };
162
+ }
163
+
164
+ private markAsNewOffense(args: WantToSlashArgs): boolean {
165
+ const key = `${args.validator.toString()}-${args.offenseType}-${args.epochOrSlot}`;
166
+ return this.emittedOffenses.addIfAbsent(key);
167
+ }
168
+ }
@@ -0,0 +1,192 @@
1
+ import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
+ import { SlotNumber } from '@aztec/foundation/branded-types';
3
+ import { merge, pick } from '@aztec/foundation/collection';
4
+ import type { EthAddress } from '@aztec/foundation/eth-address';
5
+ import { FifoSet } from '@aztec/foundation/fifo-set';
6
+ import { type Logger, createLogger } from '@aztec/foundation/log';
7
+ import { RunningPromise } from '@aztec/foundation/running-promise';
8
+ import type { L2BlockSource } from '@aztec/stdlib/block';
9
+ import type { P2PClient, SlasherConfig } from '@aztec/stdlib/interfaces/server';
10
+ import type { BlockProposal, CheckpointProposalCore } from '@aztec/stdlib/p2p';
11
+ import { OffenseType, getOffenseTypeName } from '@aztec/stdlib/slashing';
12
+
13
+ import EventEmitter from 'node:events';
14
+
15
+ import { WANT_TO_SLASH_EVENT, type WantToSlashArgs, type Watcher, type WatcherEmitter } from '../watcher.js';
16
+
17
+ const BroadcastedInvalidCheckpointProposalWatcherConfigKeys = [
18
+ 'slashBroadcastedInvalidCheckpointProposalPenalty',
19
+ ] as const;
20
+
21
+ const SCAN_SLOT_LAG = 1;
22
+ const DEFAULT_SCAN_SLOT_LOOKBACK = 4;
23
+
24
+ type BroadcastedInvalidCheckpointProposalWatcherConfig = Pick<
25
+ SlasherConfig,
26
+ (typeof BroadcastedInvalidCheckpointProposalWatcherConfigKeys)[number]
27
+ >;
28
+
29
+ type ProposalsForSlot = Awaited<ReturnType<P2PClient['getProposalsForSlot']>>;
30
+ type P2PProposalsForSlotSource = Pick<P2PClient, 'getProposalsForSlot'>;
31
+
32
+ type SignedBlockProposal = {
33
+ proposal: BlockProposal;
34
+ signer: EthAddress;
35
+ };
36
+
37
+ /** Detects truncated-checkpoint proposal offenses from retained signed P2P proposals. */
38
+ export class BroadcastedInvalidCheckpointProposalWatcher
39
+ extends (EventEmitter as new () => WatcherEmitter)
40
+ implements Watcher
41
+ {
42
+ private readonly log: Logger = createLogger('broadcasted-invalid-checkpoint-proposal-watcher');
43
+ private readonly runningPromise: RunningPromise;
44
+ private readonly emittedOffenses: FifoSet<string>;
45
+ private readonly scanSlotLookback: number;
46
+ private config: BroadcastedInvalidCheckpointProposalWatcherConfig;
47
+ private lastScannedSlot: SlotNumber | undefined;
48
+
49
+ constructor(
50
+ private readonly p2pClient: P2PProposalsForSlotSource,
51
+ private readonly l2BlockSource: Pick<L2BlockSource, 'getSyncedL2SlotNumber'>,
52
+ private readonly epochCache: Pick<EpochCacheInterface, 'getSlotNow' | 'getL1Constants'>,
53
+ config: BroadcastedInvalidCheckpointProposalWatcherConfig,
54
+ scanSlotLookback = DEFAULT_SCAN_SLOT_LOOKBACK,
55
+ ) {
56
+ super();
57
+ const constants = epochCache.getL1Constants();
58
+ this.config = pick(config, ...BroadcastedInvalidCheckpointProposalWatcherConfigKeys);
59
+ this.scanSlotLookback = Math.max(1, scanSlotLookback);
60
+
61
+ // Bound emitted offenses to the number of slots we rescan. This watcher currently tracks one offense type,
62
+ // and at most one offense of that type can be emitted per slot.
63
+ const offenseTypes = 1;
64
+ this.emittedOffenses = FifoSet.withLimit<string>(offenseTypes * this.scanSlotLookback);
65
+
66
+ const intervalMs = Math.max(1000, (constants.ethereumSlotDuration * 1000) / 4);
67
+ this.runningPromise = new RunningPromise(() => this.scan(), this.log, intervalMs);
68
+ this.log.info('BroadcastedInvalidCheckpointProposalWatcher initialized', {
69
+ scanSlotLookback: this.scanSlotLookback,
70
+ });
71
+ }
72
+
73
+ public updateConfig(config: Partial<BroadcastedInvalidCheckpointProposalWatcherConfig>): void {
74
+ this.config = merge(this.config, pick(config, ...BroadcastedInvalidCheckpointProposalWatcherConfigKeys));
75
+ this.log.verbose('BroadcastedInvalidCheckpointProposalWatcher config updated', this.config);
76
+ }
77
+
78
+ public start(): Promise<void> {
79
+ this.runningPromise.start();
80
+ return Promise.resolve();
81
+ }
82
+
83
+ public stop(): Promise<void> {
84
+ return this.runningPromise.stop();
85
+ }
86
+
87
+ /**
88
+ * Scans newly closed slots, plus a small lookback for late-arriving proposals. Anchors
89
+ * `currentSlot` at the archiver's last synced L2 slot.
90
+ */
91
+ public async scan(): Promise<void> {
92
+ const currentSlot = (await this.l2BlockSource.getSyncedL2SlotNumber()) ?? this.epochCache.getSlotNow();
93
+ if (currentSlot <= SlotNumber(SCAN_SLOT_LAG)) {
94
+ return;
95
+ }
96
+
97
+ const newestSlotToConsider = SlotNumber(currentSlot - 1 - SCAN_SLOT_LAG);
98
+ const oldestLookbackSlot = SlotNumber(Math.max(0, newestSlotToConsider - this.scanSlotLookback + 1));
99
+ const oldestUnscannedSlot =
100
+ this.lastScannedSlot === undefined ? oldestLookbackSlot : SlotNumber(this.lastScannedSlot + 1);
101
+ const oldestSlot = SlotNumber(Math.min(oldestLookbackSlot, oldestUnscannedSlot));
102
+ for (let slot = oldestSlot; slot <= newestSlotToConsider; slot++) {
103
+ await this.scanSlot(SlotNumber(slot));
104
+ }
105
+ this.lastScannedSlot = newestSlotToConsider;
106
+ }
107
+
108
+ /** Scans a single slot. Public for tests. */
109
+ public async scanSlot(slot: SlotNumber): Promise<void> {
110
+ const proposals = await this.p2pClient.getProposalsForSlot(slot);
111
+ const slashArgs = this.getSlashArgsForProposals(slot, proposals).filter(args => this.markAsNewOffense(args));
112
+ if (slashArgs.length === 0) {
113
+ return;
114
+ }
115
+
116
+ this.log.info(`Detected broadcasted invalid checkpoint proposal offense`, {
117
+ slot,
118
+ offenses: slashArgs.map(args => ({
119
+ validator: args.validator.toString(),
120
+ amount: args.amount,
121
+ offenseType: getOffenseTypeName(args.offenseType),
122
+ epochOrSlot: args.epochOrSlot,
123
+ })),
124
+ });
125
+ this.emit(WANT_TO_SLASH_EVENT, slashArgs);
126
+ }
127
+
128
+ private getSlashArgsForProposals(slot: SlotNumber, proposals: ProposalsForSlot): WantToSlashArgs[] {
129
+ const offenders = this.findOffenders(proposals.blockProposals, proposals.checkpointProposals);
130
+ // we expect one proposer per slot today.
131
+ return [...offenders.values()].map(validator => ({
132
+ validator,
133
+ amount: this.config.slashBroadcastedInvalidCheckpointProposalPenalty,
134
+ offenseType: OffenseType.BROADCASTED_INVALID_CHECKPOINT_PROPOSAL,
135
+ epochOrSlot: BigInt(slot),
136
+ }));
137
+ }
138
+
139
+ private findOffenders(blockProposals: BlockProposal[], checkpointProposals: CheckpointProposalCore[]) {
140
+ const blocksBySigner = this.getSignedBlocksBySigner(blockProposals);
141
+ const offenders = new Map<string, EthAddress>();
142
+
143
+ for (const checkpoint of checkpointProposals) {
144
+ const checkpointSigner = checkpoint.getSender();
145
+ if (!checkpointSigner) {
146
+ continue;
147
+ }
148
+
149
+ const signerKey = checkpointSigner.toString();
150
+ const signerBlocks = blocksBySigner.get(signerKey) ?? [];
151
+ const terminalBlocks = signerBlocks.filter(
152
+ ({ proposal }) => proposal.slotNumber === checkpoint.slotNumber && proposal.archive.equals(checkpoint.archive),
153
+ );
154
+ if (terminalBlocks.length === 0) {
155
+ continue;
156
+ }
157
+
158
+ const hasTruncatedHigherBlock = terminalBlocks.some(terminalBlock =>
159
+ signerBlocks.some(
160
+ ({ proposal }) =>
161
+ proposal.slotNumber === checkpoint.slotNumber &&
162
+ proposal.indexWithinCheckpoint > terminalBlock.proposal.indexWithinCheckpoint,
163
+ ),
164
+ );
165
+ if (hasTruncatedHigherBlock) {
166
+ offenders.set(signerKey, checkpointSigner);
167
+ }
168
+ }
169
+
170
+ return offenders;
171
+ }
172
+
173
+ private getSignedBlocksBySigner(blockProposals: BlockProposal[]): Map<string, SignedBlockProposal[]> {
174
+ const blocksBySigner = new Map<string, SignedBlockProposal[]>();
175
+ for (const proposal of blockProposals) {
176
+ const signer = proposal.getSender();
177
+ if (!signer) {
178
+ continue;
179
+ }
180
+ const signerKey = signer.toString();
181
+ const signerBlocks = blocksBySigner.get(signerKey) ?? [];
182
+ signerBlocks.push({ proposal, signer });
183
+ blocksBySigner.set(signerKey, signerBlocks);
184
+ }
185
+ return blocksBySigner;
186
+ }
187
+
188
+ private markAsNewOffense(args: WantToSlashArgs): boolean {
189
+ const key = `${args.validator.toString()}-${args.offenseType}-${args.epochOrSlot}`;
190
+ return this.emittedOffenses.addIfAbsent(key);
191
+ }
192
+ }