@aztec/slasher 0.0.1-commit.b655e406 → 0.0.1-commit.b8a057fa
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -76
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +53 -41
- package/dest/factory/create_facade.d.ts +5 -4
- package/dest/factory/create_facade.d.ts.map +1 -1
- package/dest/factory/create_facade.js +26 -3
- package/dest/factory/create_implementation.d.ts +7 -8
- package/dest/factory/create_implementation.d.ts.map +1 -1
- package/dest/factory/create_implementation.js +8 -56
- package/dest/factory/get_settings.d.ts +4 -4
- package/dest/factory/get_settings.d.ts.map +1 -1
- package/dest/factory/get_settings.js +3 -3
- package/dest/factory/index.d.ts +2 -2
- package/dest/factory/index.d.ts.map +1 -1
- package/dest/factory/index.js +1 -1
- package/dest/generated/slasher-defaults.d.ts +21 -0
- package/dest/generated/slasher-defaults.d.ts.map +1 -0
- package/dest/generated/slasher-defaults.js +21 -0
- package/dest/index.d.ts +6 -4
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +5 -3
- package/dest/metrics.d.ts +7 -0
- package/dest/metrics.d.ts.map +1 -0
- package/dest/metrics.js +11 -0
- package/dest/null_slasher_client.d.ts +5 -5
- package/dest/null_slasher_client.d.ts.map +1 -1
- package/dest/null_slasher_client.js +1 -4
- package/dest/slash_offenses_collector.d.ts +10 -9
- package/dest/slash_offenses_collector.d.ts.map +1 -1
- package/dest/slash_offenses_collector.js +50 -30
- package/dest/slash_round_monitor.d.ts +5 -4
- package/dest/slash_round_monitor.d.ts.map +1 -1
- package/dest/slasher_client.d.ts +114 -0
- package/dest/slasher_client.d.ts.map +1 -0
- package/dest/{tally_slasher_client.js → slasher_client.js} +55 -45
- package/dest/slasher_client_facade.d.ts +9 -10
- package/dest/slasher_client_facade.d.ts.map +1 -1
- package/dest/slasher_client_facade.js +6 -9
- package/dest/slasher_client_interface.d.ts +9 -22
- package/dest/slasher_client_interface.d.ts.map +1 -1
- package/dest/slasher_client_interface.js +1 -4
- package/dest/stores/offenses_store.d.ts +12 -12
- package/dest/stores/offenses_store.d.ts.map +1 -1
- package/dest/stores/offenses_store.js +64 -39
- package/dest/stores/schema_version.d.ts +1 -1
- package/dest/test/dummy_watcher.d.ts +1 -1
- package/dest/test/dummy_watcher.d.ts.map +1 -1
- package/dest/watcher.d.ts +8 -1
- package/dest/watcher.d.ts.map +1 -1
- package/dest/watcher.js +1 -0
- package/dest/watchers/attestations_block_watcher.d.ts +29 -15
- package/dest/watchers/attestations_block_watcher.d.ts.map +1 -1
- package/dest/watchers/attestations_block_watcher.js +91 -69
- package/dest/watchers/attested_invalid_proposal_watcher.d.ts +42 -0
- package/dest/watchers/attested_invalid_proposal_watcher.d.ts.map +1 -0
- package/dest/watchers/attested_invalid_proposal_watcher.js +117 -0
- package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.d.ts +38 -0
- package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.d.ts.map +1 -0
- package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.js +138 -0
- package/dest/watchers/checkpoint_equivocation_watcher.d.ts +30 -0
- package/dest/watchers/checkpoint_equivocation_watcher.d.ts.map +1 -0
- package/dest/watchers/checkpoint_equivocation_watcher.js +69 -0
- package/dest/watchers/data_withholding_watcher.d.ts +63 -0
- package/dest/watchers/data_withholding_watcher.d.ts.map +1 -0
- package/dest/watchers/data_withholding_watcher.js +193 -0
- package/package.json +18 -15
- package/src/config.ts +61 -41
- package/src/factory/create_facade.ts +35 -6
- package/src/factory/create_implementation.ts +25 -106
- package/src/factory/get_settings.ts +8 -8
- package/src/factory/index.ts +1 -1
- package/src/generated/slasher-defaults.ts +23 -0
- package/src/index.ts +5 -3
- package/src/metrics.ts +19 -0
- package/src/null_slasher_client.ts +4 -7
- package/src/slash_offenses_collector.ts +70 -32
- package/src/slash_round_monitor.ts +3 -2
- package/src/{tally_slasher_client.ts → slasher_client.ts} +82 -58
- package/src/slasher_client_facade.ts +9 -13
- package/src/slasher_client_interface.ts +8 -22
- package/src/stores/offenses_store.ts +76 -48
- package/src/watcher.ts +9 -1
- package/src/watchers/attestations_block_watcher.ts +108 -88
- package/src/watchers/attested_invalid_proposal_watcher.ts +168 -0
- package/src/watchers/broadcasted_invalid_checkpoint_proposal_watcher.ts +192 -0
- package/src/watchers/checkpoint_equivocation_watcher.ts +96 -0
- package/src/watchers/data_withholding_watcher.ts +225 -0
- package/dest/empire_slasher_client.d.ts +0 -189
- package/dest/empire_slasher_client.d.ts.map +0 -1
- package/dest/empire_slasher_client.js +0 -572
- package/dest/stores/payloads_store.d.ts +0 -29
- package/dest/stores/payloads_store.d.ts.map +0 -1
- package/dest/stores/payloads_store.js +0 -125
- package/dest/tally_slasher_client.d.ts +0 -129
- package/dest/tally_slasher_client.d.ts.map +0 -1
- package/dest/watchers/epoch_prune_watcher.d.ts +0 -37
- package/dest/watchers/epoch_prune_watcher.d.ts.map +0 -1
- package/dest/watchers/epoch_prune_watcher.js +0 -135
- package/src/empire_slasher_client.ts +0 -656
- package/src/stores/payloads_store.ts +0 -146
- package/src/watchers/epoch_prune_watcher.ts +0 -192
package/src/watcher.ts
CHANGED
|
@@ -5,17 +5,25 @@ 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;
|
|
11
12
|
amount: bigint;
|
|
12
13
|
offenseType: OffenseType;
|
|
13
|
-
epochOrSlot: bigint; // Epoch number for epoch-based offenses,
|
|
14
|
+
epochOrSlot: bigint; // Epoch number for epoch-based offenses, slot number for slot-based
|
|
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[];
|
|
14
21
|
}
|
|
15
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,14 +1,16 @@
|
|
|
1
1
|
import { EpochCache } from '@aztec/epoch-cache';
|
|
2
|
+
import { EpochNumber } from '@aztec/foundation/branded-types';
|
|
2
3
|
import { merge, pick } from '@aztec/foundation/collection';
|
|
3
|
-
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
4
|
+
import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
|
|
4
5
|
import {
|
|
5
|
-
type
|
|
6
|
-
type
|
|
6
|
+
type DescendentOfInvalidAttestationsCheckpointEvent,
|
|
7
|
+
type InvalidCheckpointDetectedEvent,
|
|
7
8
|
type L2BlockSourceEventEmitter,
|
|
8
9
|
L2BlockSourceEvents,
|
|
9
|
-
type
|
|
10
|
+
type ValidateCheckpointNegativeResult,
|
|
10
11
|
} from '@aztec/stdlib/block';
|
|
11
|
-
import {
|
|
12
|
+
import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
13
|
+
import { OffenseType, getOffenseTypeName } from '@aztec/stdlib/slashing';
|
|
12
14
|
|
|
13
15
|
import EventEmitter from 'node:events';
|
|
14
16
|
|
|
@@ -16,46 +18,61 @@ import type { SlasherConfig } from '../config.js';
|
|
|
16
18
|
import { WANT_TO_SLASH_EVENT, type WantToSlashArgs, type Watcher, type WatcherEmitter } from '../watcher.js';
|
|
17
19
|
|
|
18
20
|
const AttestationsBlockWatcherConfigKeys = [
|
|
19
|
-
'
|
|
21
|
+
'slashProposeDescendantOfCheckpointWithInvalidAttestationsPenalty',
|
|
20
22
|
'slashProposeInvalidAttestationsPenalty',
|
|
21
23
|
] as const;
|
|
22
24
|
|
|
23
25
|
type AttestationsBlockWatcherConfig = Pick<SlasherConfig, (typeof AttestationsBlockWatcherConfigKeys)[number]>;
|
|
24
26
|
|
|
25
27
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
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}.
|
|
30
43
|
*/
|
|
31
44
|
export class AttestationsBlockWatcher extends (EventEmitter as new () => WatcherEmitter) implements Watcher {
|
|
32
|
-
private log: Logger
|
|
33
|
-
|
|
34
|
-
// Only keep track of the last N invalid blocks
|
|
35
|
-
private maxInvalidBlocks = 100;
|
|
36
|
-
|
|
37
|
-
// All invalid archive roots seen
|
|
38
|
-
private invalidArchiveRoots: Set<string> = new Set();
|
|
39
|
-
|
|
45
|
+
private log: Logger;
|
|
40
46
|
private config: AttestationsBlockWatcherConfig;
|
|
41
47
|
|
|
42
|
-
private
|
|
48
|
+
private boundHandleInvalidCheckpoint = (event: InvalidCheckpointDetectedEvent) => {
|
|
43
49
|
try {
|
|
44
|
-
this.
|
|
50
|
+
this.handleInvalidCheckpoint(event);
|
|
45
51
|
} catch (err) {
|
|
46
|
-
this.log.error('Error handling invalid
|
|
52
|
+
this.log.error('Error handling invalid checkpoint', err, {
|
|
47
53
|
...event.validationResult,
|
|
48
54
|
reason: event.validationResult.reason,
|
|
49
55
|
});
|
|
50
56
|
}
|
|
51
57
|
};
|
|
52
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
|
+
|
|
53
68
|
constructor(
|
|
54
69
|
private l2BlockSource: L2BlockSourceEventEmitter,
|
|
55
70
|
private epochCache: EpochCache,
|
|
56
71
|
config: AttestationsBlockWatcherConfig,
|
|
72
|
+
bindings?: LoggerBindings,
|
|
57
73
|
) {
|
|
58
74
|
super();
|
|
75
|
+
this.log = createLogger('slasher:attestations-block-watcher', bindings);
|
|
59
76
|
this.config = pick(config, ...AttestationsBlockWatcherConfigKeys);
|
|
60
77
|
this.log.info('AttestationsBlockWatcher initialized');
|
|
61
78
|
}
|
|
@@ -66,96 +83,109 @@ export class AttestationsBlockWatcher extends (EventEmitter as new () => Watcher
|
|
|
66
83
|
}
|
|
67
84
|
|
|
68
85
|
public start() {
|
|
69
|
-
this.l2BlockSource.on(
|
|
86
|
+
this.l2BlockSource.events.on(
|
|
87
|
+
L2BlockSourceEvents.InvalidAttestationsCheckpointDetected,
|
|
88
|
+
this.boundHandleInvalidCheckpoint,
|
|
89
|
+
);
|
|
90
|
+
this.l2BlockSource.events.on(
|
|
91
|
+
L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected,
|
|
92
|
+
this.boundHandleDescendantOfInvalid,
|
|
93
|
+
);
|
|
70
94
|
return Promise.resolve();
|
|
71
95
|
}
|
|
72
96
|
|
|
73
97
|
public stop() {
|
|
74
|
-
this.l2BlockSource.removeListener(
|
|
75
|
-
L2BlockSourceEvents.
|
|
76
|
-
this.
|
|
98
|
+
this.l2BlockSource.events.removeListener(
|
|
99
|
+
L2BlockSourceEvents.InvalidAttestationsCheckpointDetected,
|
|
100
|
+
this.boundHandleInvalidCheckpoint,
|
|
101
|
+
);
|
|
102
|
+
this.l2BlockSource.events.removeListener(
|
|
103
|
+
L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected,
|
|
104
|
+
this.boundHandleDescendantOfInvalid,
|
|
77
105
|
);
|
|
78
106
|
return Promise.resolve();
|
|
79
107
|
}
|
|
80
108
|
|
|
81
|
-
|
|
109
|
+
/** Event handler for invalid checkpoints as reported by the archiver. Public for testing purposes. */
|
|
110
|
+
public handleInvalidCheckpoint(event: InvalidCheckpointDetectedEvent): void {
|
|
82
111
|
const { validationResult } = event;
|
|
83
|
-
const
|
|
112
|
+
const { reason, checkpoint } = validationResult;
|
|
84
113
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
this.log.trace(`Already processed invalid block ${block.blockNumber}`);
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
this.log.verbose(`Detected invalid block ${block.blockNumber}`, {
|
|
92
|
-
...block,
|
|
114
|
+
this.log.verbose(`Detected invalid checkpoint ${checkpoint.checkpointNumber}`, {
|
|
115
|
+
...checkpoint,
|
|
93
116
|
reason: validationResult.valid === false ? validationResult.reason : 'unknown',
|
|
94
117
|
});
|
|
95
118
|
|
|
96
|
-
|
|
97
|
-
|
|
119
|
+
const { checkpointNumber, slotNumber: slot } = checkpoint;
|
|
120
|
+
const epochCommitteeInfo = { ...validationResult, isEscapeHatchOpen: false };
|
|
121
|
+
const proposer = this.epochCache.getProposerFromEpochCommittee(epochCommitteeInfo, slot);
|
|
98
122
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
this.slashAttestorsOnAncestorInvalid(event.validationResult);
|
|
104
|
-
}
|
|
123
|
+
if (!proposer) {
|
|
124
|
+
this.log.warn(`No proposer found for checkpoint ${checkpointNumber} at slot ${slot}`);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
105
127
|
|
|
106
|
-
|
|
107
|
-
const
|
|
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
|
+
};
|
|
108
136
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
+
});
|
|
117
145
|
|
|
118
|
-
|
|
119
|
-
WANT_TO_SLASH_EVENT,
|
|
120
|
-
attestors.map(attestor => ({
|
|
121
|
-
validator: attestor,
|
|
122
|
-
amount: this.config.slashAttestDescendantOfInvalidPenalty,
|
|
123
|
-
offenseType: OffenseType.ATTESTED_DESCENDANT_OF_INVALID,
|
|
124
|
-
epochOrSlot: BigInt(block.slotNumber),
|
|
125
|
-
})),
|
|
126
|
-
);
|
|
127
|
-
}
|
|
146
|
+
this.emit(WANT_TO_SLASH_EVENT, [args]);
|
|
128
147
|
}
|
|
129
148
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
+
|
|
156
|
+
const slot = checkpoint.slotNumber;
|
|
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);
|
|
135
160
|
|
|
136
161
|
if (!proposer) {
|
|
137
|
-
this.log.warn(
|
|
162
|
+
this.log.warn(
|
|
163
|
+
`No proposer found for invalid descendant checkpoint ${checkpoint.checkpointNumber} at slot ${slot}`,
|
|
164
|
+
);
|
|
138
165
|
return;
|
|
139
166
|
}
|
|
140
167
|
|
|
141
|
-
const offense = this.getOffenseFromInvalidationReason(reason);
|
|
142
|
-
const amount = this.config.slashProposeInvalidAttestationsPenalty;
|
|
143
168
|
const args: WantToSlashArgs = {
|
|
144
169
|
validator: proposer,
|
|
145
|
-
amount,
|
|
146
|
-
offenseType:
|
|
147
|
-
epochOrSlot: slot,
|
|
170
|
+
amount: this.config.slashProposeDescendantOfCheckpointWithInvalidAttestationsPenalty,
|
|
171
|
+
offenseType: OffenseType.PROPOSED_DESCENDANT_OF_CHECKPOINT_WITH_INVALID_ATTESTATIONS,
|
|
172
|
+
epochOrSlot: BigInt(slot),
|
|
148
173
|
};
|
|
149
174
|
|
|
150
|
-
this.log.info(`
|
|
151
|
-
...
|
|
152
|
-
|
|
175
|
+
this.log.info(`Detected invalid descendant checkpoint proposer offense`, {
|
|
176
|
+
...checkpoint,
|
|
177
|
+
ancestorCheckpointNumber,
|
|
178
|
+
ancestorArchiveRoot: ancestorArchiveRoot.toString(),
|
|
179
|
+
validator: args.validator.toString(),
|
|
180
|
+
amount: args.amount,
|
|
181
|
+
offenseType: getOffenseTypeName(args.offenseType),
|
|
182
|
+
epochOrSlot: args.epochOrSlot,
|
|
153
183
|
});
|
|
154
184
|
|
|
155
185
|
this.emit(WANT_TO_SLASH_EVENT, [args]);
|
|
156
186
|
}
|
|
157
187
|
|
|
158
|
-
private getOffenseFromInvalidationReason(reason:
|
|
188
|
+
private getOffenseFromInvalidationReason(reason: ValidateCheckpointNegativeResult['reason']): OffenseType {
|
|
159
189
|
switch (reason) {
|
|
160
190
|
case 'invalid-attestation':
|
|
161
191
|
return OffenseType.PROPOSED_INCORRECT_ATTESTATIONS;
|
|
@@ -167,14 +197,4 @@ export class AttestationsBlockWatcher extends (EventEmitter as new () => Watcher
|
|
|
167
197
|
}
|
|
168
198
|
}
|
|
169
199
|
}
|
|
170
|
-
|
|
171
|
-
private addInvalidBlock(block: L2BlockInfo) {
|
|
172
|
-
this.invalidArchiveRoots.add(block.archive.toString());
|
|
173
|
-
|
|
174
|
-
// Prune old entries if we exceed the maximum
|
|
175
|
-
if (this.invalidArchiveRoots.size > this.maxInvalidBlocks) {
|
|
176
|
-
const oldestKey = this.invalidArchiveRoots.keys().next().value!;
|
|
177
|
-
this.invalidArchiveRoots.delete(oldestKey);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
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
|
+
}
|