@aztec/slasher 0.0.1-commit.e558bd1c → 0.0.1-commit.e57c76e

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 (92) hide show
  1. package/README.md +78 -78
  2. package/dest/config.d.ts +1 -1
  3. package/dest/config.d.ts.map +1 -1
  4. package/dest/config.js +35 -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 +8 -7
  18. package/dest/generated/slasher-defaults.d.ts.map +1 -1
  19. package/dest/generated/slasher-defaults.js +7 -6
  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/null_slasher_client.d.ts +3 -4
  24. package/dest/null_slasher_client.d.ts.map +1 -1
  25. package/dest/null_slasher_client.js +1 -4
  26. package/dest/slash_offenses_collector.d.ts +10 -9
  27. package/dest/slash_offenses_collector.d.ts.map +1 -1
  28. package/dest/slash_offenses_collector.js +50 -34
  29. package/dest/slasher_client.d.ts +112 -0
  30. package/dest/slasher_client.d.ts.map +1 -0
  31. package/dest/{tally_slasher_client.js → slasher_client.js} +45 -45
  32. package/dest/slasher_client_facade.d.ts +6 -8
  33. package/dest/slasher_client_facade.d.ts.map +1 -1
  34. package/dest/slasher_client_facade.js +6 -9
  35. package/dest/slasher_client_interface.d.ts +7 -21
  36. package/dest/slasher_client_interface.d.ts.map +1 -1
  37. package/dest/slasher_client_interface.js +1 -4
  38. package/dest/stores/offenses_store.d.ts +12 -12
  39. package/dest/stores/offenses_store.d.ts.map +1 -1
  40. package/dest/stores/offenses_store.js +61 -38
  41. package/dest/watcher.d.ts +8 -1
  42. package/dest/watcher.d.ts.map +1 -1
  43. package/dest/watcher.js +1 -0
  44. package/dest/watchers/attestations_block_watcher.d.ts +26 -13
  45. package/dest/watchers/attestations_block_watcher.d.ts.map +1 -1
  46. package/dest/watchers/attestations_block_watcher.js +76 -61
  47. package/dest/watchers/attested_invalid_proposal_watcher.d.ts +42 -0
  48. package/dest/watchers/attested_invalid_proposal_watcher.d.ts.map +1 -0
  49. package/dest/watchers/attested_invalid_proposal_watcher.js +117 -0
  50. package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.d.ts +38 -0
  51. package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.d.ts.map +1 -0
  52. package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.js +138 -0
  53. package/dest/watchers/checkpoint_equivocation_watcher.d.ts +30 -0
  54. package/dest/watchers/checkpoint_equivocation_watcher.d.ts.map +1 -0
  55. package/dest/watchers/checkpoint_equivocation_watcher.js +69 -0
  56. package/dest/watchers/data_withholding_watcher.d.ts +63 -0
  57. package/dest/watchers/data_withholding_watcher.d.ts.map +1 -0
  58. package/dest/watchers/data_withholding_watcher.js +193 -0
  59. package/package.json +10 -10
  60. package/src/config.ts +42 -29
  61. package/src/factory/create_facade.ts +32 -4
  62. package/src/factory/create_implementation.ts +24 -105
  63. package/src/factory/get_settings.ts +8 -8
  64. package/src/factory/index.ts +1 -1
  65. package/src/generated/slasher-defaults.ts +7 -6
  66. package/src/index.ts +5 -3
  67. package/src/null_slasher_client.ts +2 -6
  68. package/src/slash_offenses_collector.ts +70 -36
  69. package/src/{tally_slasher_client.ts → slasher_client.ts} +63 -54
  70. package/src/slasher_client_facade.ts +6 -11
  71. package/src/slasher_client_interface.ts +6 -21
  72. package/src/stores/offenses_store.ts +73 -47
  73. package/src/watcher.ts +8 -0
  74. package/src/watchers/attestations_block_watcher.ts +88 -82
  75. package/src/watchers/attested_invalid_proposal_watcher.ts +168 -0
  76. package/src/watchers/broadcasted_invalid_checkpoint_proposal_watcher.ts +192 -0
  77. package/src/watchers/checkpoint_equivocation_watcher.ts +96 -0
  78. package/src/watchers/data_withholding_watcher.ts +225 -0
  79. package/dest/empire_slasher_client.d.ts +0 -190
  80. package/dest/empire_slasher_client.d.ts.map +0 -1
  81. package/dest/empire_slasher_client.js +0 -564
  82. package/dest/stores/payloads_store.d.ts +0 -29
  83. package/dest/stores/payloads_store.d.ts.map +0 -1
  84. package/dest/stores/payloads_store.js +0 -128
  85. package/dest/tally_slasher_client.d.ts +0 -125
  86. package/dest/tally_slasher_client.d.ts.map +0 -1
  87. package/dest/watchers/epoch_prune_watcher.d.ts +0 -39
  88. package/dest/watchers/epoch_prune_watcher.d.ts.map +0 -1
  89. package/dest/watchers/epoch_prune_watcher.js +0 -175
  90. package/src/empire_slasher_client.ts +0 -649
  91. package/src/stores/payloads_store.ts +0 -149
  92. package/src/watchers/epoch_prune_watcher.ts +0 -251
@@ -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,36 +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
69
  const round = getRoundForOffense(offense, this.settings);
80
- await this.kvStore.transactionAsync(async () => {
70
+ const added = await this.kvStore.transactionAsync(async () => {
71
+ if ((await this.offenses.getAsync(key)) !== undefined) {
72
+ return false;
73
+ }
74
+
81
75
  await this.offenses.set(key, serializeOffense(offense));
82
76
  await this.roundsOffenses.set(this.getRoundKey(round), key);
77
+ return true;
83
78
  });
84
- this.log.trace(`Adding pending offense ${key} for round ${round}`);
79
+
80
+ if (added) {
81
+ this.log.trace(`Adding pending offense ${key} for round ${round}`);
82
+ }
83
+
84
+ return added;
85
85
  }
86
86
 
87
- /** Marks the given offenses as slashed (regardless of whether they are known or not) */
88
- public async markAsSlashed(offenses: OffenseIdentifier[]): Promise<void> {
89
- await this.kvStore.transactionAsync(async () => {
90
- for (const offense of offenses) {
91
- const key = this.getOffenseKey(offense);
92
- 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
+ }
93
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;
94
122
  });
95
123
  }
96
124
 
@@ -106,34 +134,32 @@ export class SlasherOffensesStore {
106
134
  return 0; // Not enough rounds have passed to expire anything
107
135
  }
108
136
 
109
- // Collect expired offenses and rounds
110
- const expiredRoundKeys = new Set<string>();
111
- const expiredOffenseKeys = new Set<string>();
112
- for await (const [roundKey, offenseKey] of this.roundsOffenses.entriesAsync({
113
- end: this.getRoundKey(expiredBefore),
114
- })) {
115
- expiredOffenseKeys.add(offenseKey);
116
- expiredRoundKeys.add(roundKey);
117
- }
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
+ }
118
147
 
119
- if (expiredOffenseKeys.size === 0 && expiredRoundKeys.size === 0) {
120
- return 0; // Nothing to clean up
121
- }
148
+ if (expiredOffenseKeys.size === 0 && expiredRoundKeys.size === 0) {
149
+ return 0; // Nothing to clean up
150
+ }
122
151
 
123
- // Remove expired stuff in a transaction
124
- await this.kvStore.transactionAsync(async () => {
125
152
  for (const key of expiredOffenseKeys) {
126
153
  this.log.trace(`Deleting offense ${key}`);
127
154
  await this.offenses.delete(key);
128
- await this.offensesSlashed.delete(key);
129
155
  }
130
156
  for (const roundKey of expiredRoundKeys) {
131
157
  this.log.trace(`Deleting round info for ${roundKey}`);
132
158
  await this.roundsOffenses.delete(roundKey);
133
159
  }
134
- });
135
160
 
136
- return expiredOffenseKeys.size;
161
+ return expiredOffenseKeys.size;
162
+ });
137
163
  }
138
164
 
139
165
  /** Generate a unique key for an offense */
package/src/watcher.ts CHANGED
@@ -5,6 +5,7 @@ import { OffenseType } from '@aztec/stdlib/slashing';
5
5
  import type { SlasherConfig } from './config.js';
6
6
 
7
7
  export const WANT_TO_SLASH_EVENT = 'want-to-slash' as const;
8
+ export const WANT_TO_CLEAR_SLASH_EVENT = 'want-to-clear-slash' as const;
8
9
 
9
10
  export interface WantToSlashArgs {
10
11
  validator: EthAddress;
@@ -13,9 +14,16 @@ export interface WantToSlashArgs {
13
14
  epochOrSlot: bigint; // Epoch number for epoch-based offenses, slot number for slot-based
14
15
  }
15
16
 
17
+ export interface WantToClearSlashArgs {
18
+ offenseType: OffenseType;
19
+ epochOrSlot: bigint; // Epoch number for epoch-based offenses, slot number for slot-based
20
+ validators?: EthAddress[];
21
+ }
22
+
16
23
  // Event map for specific, known events of a watcher
17
24
  export interface WatcherEventMap {
18
25
  [WANT_TO_SLASH_EVENT]: (args: WantToSlashArgs[]) => void;
26
+ [WANT_TO_CLEAR_SLASH_EVENT]: (args: WantToClearSlashArgs[]) => void;
19
27
  }
20
28
 
21
29
  export type WatcherEmitter = TypedEventEmitter<WatcherEventMap>;
@@ -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
+ }