@aztec/slasher 0.0.1-commit.d3ec352c → 0.0.1-commit.d58ff9d0
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 +3 -4
- 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/{tally_slasher_client.d.ts → slasher_client.d.ts} +17 -28
- 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 +7 -9
- 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 +7 -21
- 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/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 +90 -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 +15 -13
- 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 +2 -6
- package/src/slash_offenses_collector.ts +70 -32
- package/src/{tally_slasher_client.ts → slasher_client.ts} +72 -54
- package/src/slasher_client_facade.ts +7 -12
- package/src/slasher_client_interface.ts +6 -21
- package/src/stores/offenses_store.ts +76 -48
- package/src/watcher.ts +8 -0
- package/src/watchers/attestations_block_watcher.ts +107 -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 -190
- 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.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 -137
- package/src/empire_slasher_client.ts +0 -657
- package/src/stores/payloads_store.ts +0 -146
- package/src/watchers/epoch_prune_watcher.ts +0 -194
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { EpochCache } from '@aztec/epoch-cache';
|
|
2
|
-
import {
|
|
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
|
|
7
|
-
type
|
|
6
|
+
type DescendentOfInvalidAttestationsCheckpointEvent,
|
|
7
|
+
type InvalidCheckpointDetectedEvent,
|
|
8
8
|
type L2BlockSourceEventEmitter,
|
|
9
9
|
L2BlockSourceEvents,
|
|
10
|
-
type
|
|
10
|
+
type ValidateCheckpointNegativeResult,
|
|
11
11
|
} from '@aztec/stdlib/block';
|
|
12
|
-
import {
|
|
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,46 +18,61 @@ 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
|
-
'
|
|
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
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
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
|
|
34
|
-
|
|
35
|
-
// Only keep track of the last N invalid blocks
|
|
36
|
-
private maxInvalidBlocks = 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
|
-
private
|
|
48
|
+
private boundHandleInvalidCheckpoint = (event: InvalidCheckpointDetectedEvent) => {
|
|
44
49
|
try {
|
|
45
|
-
this.
|
|
50
|
+
this.handleInvalidCheckpoint(event);
|
|
46
51
|
} catch (err) {
|
|
47
|
-
this.log.error('Error handling invalid
|
|
52
|
+
this.log.error('Error handling invalid checkpoint', err, {
|
|
48
53
|
...event.validationResult,
|
|
49
54
|
reason: event.validationResult.reason,
|
|
50
55
|
});
|
|
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,96 +83,109 @@ export class AttestationsBlockWatcher extends (EventEmitter as new () => Watcher
|
|
|
67
83
|
}
|
|
68
84
|
|
|
69
85
|
public start() {
|
|
70
|
-
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
|
+
);
|
|
71
94
|
return Promise.resolve();
|
|
72
95
|
}
|
|
73
96
|
|
|
74
97
|
public stop() {
|
|
75
|
-
this.l2BlockSource.removeListener(
|
|
76
|
-
L2BlockSourceEvents.
|
|
77
|
-
this.
|
|
98
|
+
this.l2BlockSource.events.removeListener(
|
|
99
|
+
L2BlockSourceEvents.InvalidAttestationsCheckpointDetected,
|
|
100
|
+
this.boundHandleInvalidCheckpoint,
|
|
101
|
+
);
|
|
102
|
+
this.l2BlockSource.events.removeListener(
|
|
103
|
+
L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected,
|
|
104
|
+
this.boundHandleDescendantOfInvalid,
|
|
78
105
|
);
|
|
79
106
|
return Promise.resolve();
|
|
80
107
|
}
|
|
81
108
|
|
|
82
|
-
|
|
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
|
|
112
|
+
const { reason, checkpoint } = validationResult;
|
|
85
113
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
this.log.trace(`Already processed invalid block ${block.blockNumber}`);
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
this.log.verbose(`Detected invalid block ${block.blockNumber}`, {
|
|
93
|
-
...block,
|
|
114
|
+
this.log.verbose(`Detected invalid checkpoint ${checkpoint.checkpointNumber}`, {
|
|
115
|
+
...checkpoint,
|
|
94
116
|
reason: validationResult.valid === false ? validationResult.reason : 'unknown',
|
|
95
117
|
});
|
|
96
118
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
// Slash the proposer of the invalid block
|
|
101
|
-
this.slashProposer(event.validationResult);
|
|
119
|
+
const { checkpointNumber, slotNumber: slot } = checkpoint;
|
|
120
|
+
const epochCommitteeInfo = { ...validationResult, isEscapeHatchOpen: false };
|
|
121
|
+
const proposer = this.epochCache.getProposerFromEpochCommittee(epochCommitteeInfo, slot);
|
|
102
122
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
123
|
+
if (!proposer) {
|
|
124
|
+
this.log.warn(`No proposer found for checkpoint ${checkpointNumber} at slot ${slot}`);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
106
127
|
|
|
107
|
-
|
|
108
|
-
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
|
+
};
|
|
109
136
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
+
});
|
|
118
145
|
|
|
119
|
-
|
|
120
|
-
WANT_TO_SLASH_EVENT,
|
|
121
|
-
attestors.map(attestor => ({
|
|
122
|
-
validator: attestor,
|
|
123
|
-
amount: this.config.slashAttestDescendantOfInvalidPenalty,
|
|
124
|
-
offenseType: OffenseType.ATTESTED_DESCENDANT_OF_INVALID,
|
|
125
|
-
epochOrSlot: BigInt(SlotNumber(block.slotNumber)),
|
|
126
|
-
})),
|
|
127
|
-
);
|
|
128
|
-
}
|
|
146
|
+
this.emit(WANT_TO_SLASH_EVENT, [args]);
|
|
129
147
|
}
|
|
130
148
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
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);
|
|
136
160
|
|
|
137
161
|
if (!proposer) {
|
|
138
|
-
this.log.warn(
|
|
162
|
+
this.log.warn(
|
|
163
|
+
`No proposer found for invalid descendant checkpoint ${checkpoint.checkpointNumber} at slot ${slot}`,
|
|
164
|
+
);
|
|
139
165
|
return;
|
|
140
166
|
}
|
|
141
167
|
|
|
142
|
-
const offense = this.getOffenseFromInvalidationReason(reason);
|
|
143
|
-
const amount = this.config.slashProposeInvalidAttestationsPenalty;
|
|
144
168
|
const args: WantToSlashArgs = {
|
|
145
169
|
validator: proposer,
|
|
146
|
-
amount,
|
|
147
|
-
offenseType:
|
|
170
|
+
amount: this.config.slashProposeDescendantOfCheckpointWithInvalidAttestationsPenalty,
|
|
171
|
+
offenseType: OffenseType.PROPOSED_DESCENDANT_OF_CHECKPOINT_WITH_INVALID_ATTESTATIONS,
|
|
148
172
|
epochOrSlot: BigInt(slot),
|
|
149
173
|
};
|
|
150
174
|
|
|
151
|
-
this.log.info(`
|
|
152
|
-
...
|
|
153
|
-
|
|
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,
|
|
154
183
|
});
|
|
155
184
|
|
|
156
185
|
this.emit(WANT_TO_SLASH_EVENT, [args]);
|
|
157
186
|
}
|
|
158
187
|
|
|
159
|
-
private getOffenseFromInvalidationReason(reason:
|
|
188
|
+
private getOffenseFromInvalidationReason(reason: ValidateCheckpointNegativeResult['reason']): OffenseType {
|
|
160
189
|
switch (reason) {
|
|
161
190
|
case 'invalid-attestation':
|
|
162
191
|
return OffenseType.PROPOSED_INCORRECT_ATTESTATIONS;
|
|
@@ -168,14 +197,4 @@ export class AttestationsBlockWatcher extends (EventEmitter as new () => Watcher
|
|
|
168
197
|
}
|
|
169
198
|
}
|
|
170
199
|
}
|
|
171
|
-
|
|
172
|
-
private addInvalidBlock(block: L2BlockInfo) {
|
|
173
|
-
this.invalidArchiveRoots.add(block.archive.toString());
|
|
174
|
-
|
|
175
|
-
// Prune old entries if we exceed the maximum
|
|
176
|
-
if (this.invalidArchiveRoots.size > this.maxInvalidBlocks) {
|
|
177
|
-
const oldestKey = this.invalidArchiveRoots.keys().next().value!;
|
|
178
|
-
this.invalidArchiveRoots.delete(oldestKey);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
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
|
+
}
|