@aztec/slasher 0.0.1-commit.96bb3f7 → 0.0.1-commit.993d240

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 +83 -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 +3 -3
  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 +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 +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/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 -30
  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} +50 -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 +64 -39
  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 +28 -14
  45. package/dest/watchers/attestations_block_watcher.d.ts.map +1 -1
  46. package/dest/watchers/attestations_block_watcher.js +80 -64
  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 +15 -13
  60. package/src/config.ts +61 -41
  61. package/src/factory/create_facade.ts +33 -5
  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 +23 -0
  66. package/src/index.ts +5 -3
  67. package/src/null_slasher_client.ts +2 -6
  68. package/src/slash_offenses_collector.ts +70 -32
  69. package/src/{tally_slasher_client.ts → slasher_client.ts} +68 -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 +76 -48
  73. package/src/watcher.ts +8 -0
  74. package/src/watchers/attestations_block_watcher.ts +95 -84
  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 -572
  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 -125
  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 -37
  88. package/dest/watchers/epoch_prune_watcher.d.ts.map +0 -1
  89. package/dest/watchers/epoch_prune_watcher.js +0 -137
  90. package/src/empire_slasher_client.ts +0 -657
  91. package/src/stores/payloads_store.ts +0 -146
  92. package/src/watchers/epoch_prune_watcher.ts +0 -194
@@ -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 */
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
  }
@@ -67,98 +83,103 @@ export class AttestationsBlockWatcher extends (EventEmitter as new () => Watcher
67
83
  }
68
84
 
69
85
  public start() {
70
- this.l2BlockSource.on(L2BlockSourceEvents.InvalidAttestationsCheckpointDetected, this.boundHandleInvalidCheckpoint);
86
+ this.l2BlockSource.events.on(
87
+ L2BlockSourceEvents.InvalidAttestationsCheckpointDetected,
88
+ this.boundHandleInvalidCheckpoint,
89
+ );
90
+ this.l2BlockSource.events.on(
91
+ L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected,
92
+ this.boundHandleDescendantOfInvalid,
93
+ );
71
94
  return Promise.resolve();
72
95
  }
73
96
 
74
97
  public stop() {
75
- this.l2BlockSource.removeListener(
98
+ this.l2BlockSource.events.removeListener(
76
99
  L2BlockSourceEvents.InvalidAttestationsCheckpointDetected,
77
100
  this.boundHandleInvalidCheckpoint,
78
101
  );
102
+ this.l2BlockSource.events.removeListener(
103
+ L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected,
104
+ this.boundHandleDescendantOfInvalid,
105
+ );
79
106
  return Promise.resolve();
80
107
  }
81
108
 
82
- private handleInvalidCheckpoint(event: InvalidCheckpointDetectedEvent): void {
109
+ /** Event handler for invalid checkpoints as reported by the archiver. Public for testing purposes. */
110
+ public handleInvalidCheckpoint(event: InvalidCheckpointDetectedEvent): void {
83
111
  const { validationResult } = event;
84
- const checkpoint = validationResult.checkpoint;
85
-
86
- // Check if we already have processed this checkpoint, archiver may emit the same event multiple times
87
- if (this.invalidArchiveRoots.has(checkpoint.archive.toString())) {
88
- this.log.trace(`Already processed invalid checkpoint ${checkpoint.checkpointNumber}`);
89
- return;
90
- }
112
+ const { reason, checkpoint } = validationResult;
91
113
 
92
114
  this.log.verbose(`Detected invalid checkpoint ${checkpoint.checkpointNumber}`, {
93
115
  ...checkpoint,
94
116
  reason: validationResult.valid === false ? validationResult.reason : 'unknown',
95
117
  });
96
118
 
97
- // Store the invalid checkpoint
98
- 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);
99
122
 
100
- // Slash the proposer of the invalid checkpoint
101
- this.slashProposer(event.validationResult);
123
+ if (!proposer) {
124
+ this.log.warn(`No proposer found for checkpoint ${checkpointNumber} at slot ${slot}`);
125
+ return;
126
+ }
102
127
 
103
- // Check if the parent of this checkpoint is invalid as well, if so, we will slash its attestors as well
104
- this.slashAttestorsOnAncestorInvalid(event.validationResult);
105
- }
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
+ };
106
136
 
107
- private slashAttestorsOnAncestorInvalid(validationResult: ValidateCheckpointNegativeResult) {
108
- const checkpoint = validationResult.checkpoint;
109
-
110
- const parentArchive = checkpoint.lastArchive.toString();
111
- if (this.invalidArchiveRoots.has(parentArchive)) {
112
- const attestors = validationResult.attestors;
113
- this.log.info(
114
- `Want to slash attestors of checkpoint ${checkpoint.checkpointNumber} built on invalid checkpoint`,
115
- {
116
- ...checkpoint,
117
- ...attestors,
118
- parentArchive,
119
- },
120
- );
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
+ });
121
145
 
122
- this.emit(
123
- WANT_TO_SLASH_EVENT,
124
- attestors.map(attestor => ({
125
- validator: attestor,
126
- amount: this.config.slashAttestDescendantOfInvalidPenalty,
127
- offenseType: OffenseType.ATTESTED_DESCENDANT_OF_INVALID,
128
- epochOrSlot: BigInt(SlotNumber(checkpoint.slotNumber)),
129
- })),
130
- );
131
- }
146
+ this.emit(WANT_TO_SLASH_EVENT, [args]);
132
147
  }
133
148
 
134
- private slashProposer(validationResult: ValidateCheckpointNegativeResult) {
135
- const { reason, checkpoint } = validationResult;
136
- 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
+
137
156
  const slot = checkpoint.slotNumber;
138
- const epochCommitteeInfo = {
139
- committee: validationResult.committee,
140
- seed: validationResult.seed,
141
- epoch: validationResult.epoch,
142
- };
143
- 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);
144
160
 
145
161
  if (!proposer) {
146
- 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
+ );
147
165
  return;
148
166
  }
149
167
 
150
- const offense = this.getOffenseFromInvalidationReason(reason);
151
- const amount = this.config.slashProposeInvalidAttestationsPenalty;
152
168
  const args: WantToSlashArgs = {
153
169
  validator: proposer,
154
- amount,
155
- offenseType: offense,
170
+ amount: this.config.slashProposeDescendantOfCheckpointWithInvalidAttestationsPenalty,
171
+ offenseType: OffenseType.PROPOSED_DESCENDANT_OF_CHECKPOINT_WITH_INVALID_ATTESTATIONS,
156
172
  epochOrSlot: BigInt(slot),
157
173
  };
158
174
 
159
- this.log.info(`Want to slash proposer of checkpoint ${checkpointNumber} due to ${reason}`, {
175
+ this.log.info(`Detected invalid descendant checkpoint proposer offense`, {
160
176
  ...checkpoint,
161
- ...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,
162
183
  });
163
184
 
164
185
  this.emit(WANT_TO_SLASH_EVENT, [args]);
@@ -176,14 +197,4 @@ export class AttestationsBlockWatcher extends (EventEmitter as new () => Watcher
176
197
  }
177
198
  }
178
199
  }
179
-
180
- private addInvalidCheckpoint(checkpoint: CheckpointInfo) {
181
- this.invalidArchiveRoots.add(checkpoint.archive.toString());
182
-
183
- // Prune old entries if we exceed the maximum
184
- if (this.invalidArchiveRoots.size > this.maxInvalidCheckpoints) {
185
- const oldestKey = this.invalidArchiveRoots.keys().next().value!;
186
- this.invalidArchiveRoots.delete(oldestKey);
187
- }
188
- }
189
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
+ }