@aztec/slasher 0.0.1-commit.c2eed6949 → 0.0.1-commit.c52d6e7
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 +83 -79
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +28 -28
- package/dest/factory/create_facade.d.ts +6 -3
- package/dest/factory/create_facade.d.ts.map +1 -1
- package/dest/factory/create_facade.js +5 -2
- package/dest/factory/create_implementation.d.ts +5 -6
- package/dest/factory/create_implementation.d.ts.map +1 -1
- package/dest/factory/create_implementation.js +7 -59
- 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 +4 -4
- package/dest/generated/slasher-defaults.js +4 -4
- 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 +23 -0
- package/dest/metrics.d.ts.map +1 -0
- package/dest/metrics.js +38 -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/own_validator_slash_monitor.d.ts +66 -0
- package/dest/own_validator_slash_monitor.d.ts.map +1 -0
- package/dest/own_validator_slash_monitor.js +163 -0
- package/dest/slash_offenses_collector.d.ts +6 -8
- package/dest/slash_offenses_collector.d.ts.map +1 -1
- package/dest/slash_offenses_collector.js +48 -28
- package/dest/slasher_client.d.ts +118 -0
- package/dest/slasher_client.d.ts.map +1 -0
- package/dest/{tally_slasher_client.js → slasher_client.js} +59 -40
- package/dest/slasher_client_facade.d.ts +6 -7
- 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 +61 -38
- 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 +26 -13
- package/dest/watchers/attestations_block_watcher.d.ts.map +1 -1
- package/dest/watchers/attestations_block_watcher.js +76 -61
- 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 +10 -10
- package/src/config.ts +33 -28
- package/src/factory/create_facade.ts +6 -3
- package/src/factory/create_implementation.ts +16 -117
- package/src/factory/get_settings.ts +8 -8
- package/src/factory/index.ts +1 -1
- package/src/generated/slasher-defaults.ts +4 -4
- package/src/index.ts +5 -3
- package/src/metrics.ts +59 -0
- package/src/null_slasher_client.ts +2 -6
- package/src/own_validator_slash_monitor.ts +184 -0
- package/src/slash_offenses_collector.ts +62 -29
- package/src/{tally_slasher_client.ts → slasher_client.ts} +78 -48
- package/src/slasher_client_facade.ts +6 -10
- package/src/slasher_client_interface.ts +6 -21
- package/src/stores/offenses_store.ts +73 -47
- package/src/watcher.ts +8 -0
- package/src/watchers/attestations_block_watcher.ts +88 -82
- 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 -564
- 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 -128
- package/dest/tally_slasher_client.d.ts +0 -125
- package/dest/tally_slasher_client.d.ts.map +0 -1
- package/dest/watchers/epoch_prune_watcher.d.ts +0 -39
- package/dest/watchers/epoch_prune_watcher.d.ts.map +0 -1
- package/dest/watchers/epoch_prune_watcher.js +0 -179
- package/src/empire_slasher_client.ts +0 -649
- package/src/stores/payloads_store.ts +0 -149
- package/src/watchers/epoch_prune_watcher.ts +0 -256
package/src/metrics.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Gauge,
|
|
3
|
+
Metrics,
|
|
4
|
+
type TelemetryClient,
|
|
5
|
+
type UpDownCounter,
|
|
6
|
+
createUpDownCounterWithDefault,
|
|
7
|
+
} from '@aztec/telemetry-client';
|
|
8
|
+
|
|
9
|
+
import { formatEther } from 'viem/utils';
|
|
10
|
+
|
|
11
|
+
export class SlasherMetrics {
|
|
12
|
+
private readonly roundExecuted: UpDownCounter;
|
|
13
|
+
private readonly ownValidatorTargeted: UpDownCounter;
|
|
14
|
+
private readonly ownValidatorSlashedCount: UpDownCounter;
|
|
15
|
+
private readonly ownValidatorSlashedAmount: UpDownCounter;
|
|
16
|
+
private readonly ownValidatorCurrentRoundVotesMax: Gauge;
|
|
17
|
+
private readonly quorumSize: Gauge;
|
|
18
|
+
|
|
19
|
+
constructor(client: TelemetryClient, name = 'Slasher') {
|
|
20
|
+
const meter = client.getMeter(name);
|
|
21
|
+
this.roundExecuted = createUpDownCounterWithDefault(meter, Metrics.SLASHER_ROUND_EXECUTED_COUNT);
|
|
22
|
+
this.ownValidatorTargeted = createUpDownCounterWithDefault(meter, Metrics.SLASHER_OWN_VALIDATOR_TARGETED_COUNT);
|
|
23
|
+
this.ownValidatorSlashedCount = createUpDownCounterWithDefault(meter, Metrics.SLASHER_OWN_VALIDATOR_SLASHED_COUNT);
|
|
24
|
+
this.ownValidatorSlashedAmount = createUpDownCounterWithDefault(
|
|
25
|
+
meter,
|
|
26
|
+
Metrics.SLASHER_OWN_VALIDATOR_SLASHED_AMOUNT,
|
|
27
|
+
);
|
|
28
|
+
this.ownValidatorCurrentRoundVotesMax = meter.createGauge(Metrics.SLASHER_OWN_VALIDATOR_CURRENT_ROUND_VOTES_MAX);
|
|
29
|
+
this.quorumSize = meter.createGauge(Metrics.SLASHER_QUORUM_SIZE);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public recordRoundExecuted(): void {
|
|
33
|
+
this.roundExecuted.add(1);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Records the quorum a validator must reach in a round to be slashed, so dashboards can plot the threshold. */
|
|
37
|
+
public recordQuorumSize(quorum: number): void {
|
|
38
|
+
this.quorumSize.record(quorum);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Records that an onchain slashing vote named one of the node's own validators as a target. */
|
|
42
|
+
public recordOwnValidatorTargeted(): void {
|
|
43
|
+
this.ownValidatorTargeted.add(1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Records how close the most-voted committee position held by the node's own validators is to quorum this round.
|
|
48
|
+
* Recorded as an absolute value rather than a delta so a vote seen across a round rollover cannot make it drift.
|
|
49
|
+
*/
|
|
50
|
+
public recordCurrentRoundVotesMax(votes: number): void {
|
|
51
|
+
this.ownValidatorCurrentRoundVotesMax.record(votes);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Records an executed slash against one of the node's own validators. */
|
|
55
|
+
public recordOwnValidatorSlashed(amount: bigint): void {
|
|
56
|
+
this.ownValidatorSlashedCount.add(1);
|
|
57
|
+
this.ownValidatorSlashedAmount.add(parseFloat(formatEther(amount)));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
|
-
import type { Offense, ProposerSlashAction
|
|
2
|
+
import type { Offense, ProposerSlashAction } from '@aztec/stdlib/slashing';
|
|
3
3
|
|
|
4
4
|
import type { SlasherConfig } from './config.js';
|
|
5
5
|
import type { SlasherClientInterface } from './slasher_client_interface.js';
|
|
@@ -15,15 +15,11 @@ export class NullSlasherClient implements SlasherClientInterface {
|
|
|
15
15
|
return Promise.resolve();
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
public getSlashPayloads(): Promise<SlashPayloadRound[]> {
|
|
19
|
-
return Promise.resolve([]);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
18
|
public gatherOffensesForRound(_round?: bigint): Promise<Offense[]> {
|
|
23
19
|
return Promise.resolve([]);
|
|
24
20
|
}
|
|
25
21
|
|
|
26
|
-
public
|
|
22
|
+
public getOffenses(): Promise<Offense[]> {
|
|
27
23
|
return Promise.resolve([]);
|
|
28
24
|
}
|
|
29
25
|
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { SlashVoteTarget, SlashingProposerContract } from '@aztec/ethereum/contracts';
|
|
2
|
+
import { uniqueBy } from '@aztec/foundation/collection';
|
|
3
|
+
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
4
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
5
|
+
import { SerialQueue } from '@aztec/foundation/queue';
|
|
6
|
+
|
|
7
|
+
import type { Hex } from 'viem';
|
|
8
|
+
|
|
9
|
+
import type { SlasherMetrics } from './metrics.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Watches slashing activity that targets the node's own validators: warns and counts on every vote naming one of
|
|
13
|
+
* them, tracks how close the current round's tally is to quorum, and reports executed slashes. Does nothing when
|
|
14
|
+
* the node runs no validators. Lifecycle calls must be sequential: start() before events are wired, and stop()
|
|
15
|
+
* awaited before any restart.
|
|
16
|
+
*/
|
|
17
|
+
export class OwnValidatorSlashMonitor {
|
|
18
|
+
/**
|
|
19
|
+
* Tally for the single round being tracked (votes are only ever cast for the current round). `nextVoteIndex` is
|
|
20
|
+
* the cursor: the index of the first vote in the round not yet processed, advanced from the indices the VoteCast
|
|
21
|
+
* events carry. It is undefined until the first event of a round that started mid-round, whose own vote is then
|
|
22
|
+
* the first one counted, so votes cast before the node subscribed are never replayed into the cumulative metrics.
|
|
23
|
+
* The tally is per flattened committee position because that is the unit the contract tallies quorum by.
|
|
24
|
+
*/
|
|
25
|
+
private state: { round: bigint; nextVoteIndex: bigint | undefined; countByPosition: Map<number, number> } = {
|
|
26
|
+
round: -1n,
|
|
27
|
+
nextVoteIndex: undefined,
|
|
28
|
+
countByPosition: new Map(),
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* All vote processing runs through this queue, one job at a time in arrival order. Serialization is what makes
|
|
33
|
+
* the cursor sound: without it, concurrent event handlers could process the same index twice or emit warnings
|
|
34
|
+
* with out-of-order running tallies. Replaced on every start(), since a queue that has been ended cannot run
|
|
35
|
+
* jobs again.
|
|
36
|
+
*/
|
|
37
|
+
private queue = new SerialQueue();
|
|
38
|
+
|
|
39
|
+
private stopped = false;
|
|
40
|
+
|
|
41
|
+
constructor(
|
|
42
|
+
private readonly slashingProposer: SlashingProposerContract,
|
|
43
|
+
private readonly settings: { slashingQuorumSize: number },
|
|
44
|
+
private readonly ownValidators: EthAddress[],
|
|
45
|
+
private readonly metrics: SlasherMetrics,
|
|
46
|
+
private readonly log = createLogger('slasher:own-validators'),
|
|
47
|
+
) {
|
|
48
|
+
this.queue.start();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private get enabled(): boolean {
|
|
52
|
+
return this.ownValidators.length > 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Starts tracking at the given round, leaving the cursor unset so the round's votes already cast are skipped:
|
|
57
|
+
* replaying them would warn about votes the operator can no longer react to sooner, and would double-count
|
|
58
|
+
* cumulative counters across restarts.
|
|
59
|
+
*/
|
|
60
|
+
public start(currentRound: bigint): void {
|
|
61
|
+
if (!this.enabled) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
this.stopped = false;
|
|
65
|
+
this.queue = new SerialQueue();
|
|
66
|
+
this.queue.start();
|
|
67
|
+
this.state = { round: currentRound, nextVoteIndex: undefined, countByPosition: new Map() };
|
|
68
|
+
this.metrics.recordQuorumSize(this.settings.slashingQuorumSize);
|
|
69
|
+
this.metrics.recordCurrentRoundVotesMax(0);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Stops processing and waits for any in-flight drain, so no warning or metric is emitted after shutdown. */
|
|
73
|
+
public async stop(): Promise<void> {
|
|
74
|
+
this.stopped = true;
|
|
75
|
+
// Jobs already queued are no-ops now that the flag is set, so end() runs through them at no cost while still
|
|
76
|
+
// settling the promises their callers hold, where cancel() would discard them and leave those promises pending.
|
|
77
|
+
await this.queue.end();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Called by the round monitor clock. Strictly greater so the clock catching up to a round an event already rolled
|
|
82
|
+
* to keeps the tally.
|
|
83
|
+
*/
|
|
84
|
+
public handleNewRound(round: bigint): void {
|
|
85
|
+
if (this.enabled && round > this.state.round) {
|
|
86
|
+
this.rollTo(round);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Called on each VoteCast event with the index the event carries. Reads every vote from the cursor up to that
|
|
92
|
+
* index, so an event delivery that never arrived is healed by the next one.
|
|
93
|
+
*/
|
|
94
|
+
public handleVoteCast(round: bigint, voteIndex: bigint): Promise<void> {
|
|
95
|
+
if (!this.enabled || this.stopped) {
|
|
96
|
+
return Promise.resolve();
|
|
97
|
+
}
|
|
98
|
+
// Errors are logged rather than propagated: callers fire and forget, and a failed read is retried by the drain
|
|
99
|
+
// the next event triggers.
|
|
100
|
+
return this.queue
|
|
101
|
+
.put(() => this.drainVotesTo(round, voteIndex))
|
|
102
|
+
.catch(err => this.log.error('Error processing slashing votes', err));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Reports executed slashes against own validators. Called with the Slashed events already fetched by the client. */
|
|
106
|
+
public handleSlashes(round: bigint, slashes: { attester: EthAddress; amount: bigint }[], l1BlockHash: Hex): void {
|
|
107
|
+
if (!this.enabled || this.stopped) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
for (const { attester, amount } of slashes.filter(slash => this.isOwnValidator(slash.attester))) {
|
|
111
|
+
this.log.warn(`Own validator ${attester} was slashed for ${amount}`, {
|
|
112
|
+
round,
|
|
113
|
+
validator: attester.toString(),
|
|
114
|
+
amount,
|
|
115
|
+
l1BlockHash,
|
|
116
|
+
});
|
|
117
|
+
this.metrics.recordOwnValidatorSlashed(amount);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Processes votes [cursor, voteIndex] of a round, in order, advancing the cursor after each success. */
|
|
122
|
+
private async drainVotesTo(round: bigint, voteIndex: bigint): Promise<void> {
|
|
123
|
+
if (this.stopped) {
|
|
124
|
+
return; // enqueued before stop() but not yet started: emit nothing and read nothing after shutdown
|
|
125
|
+
}
|
|
126
|
+
if (round < this.state.round) {
|
|
127
|
+
return; // a vote for a round that already closed can no longer reach quorum
|
|
128
|
+
}
|
|
129
|
+
if (round > this.state.round) {
|
|
130
|
+
this.rollTo(round); // the event beat the round-monitor clock to the boundary
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// The first event of a round tracked from mid-round counts its own vote only: earlier ones predate the subscription
|
|
134
|
+
const from = this.state.nextVoteIndex ?? voteIndex;
|
|
135
|
+
if (voteIndex < from) {
|
|
136
|
+
return; // a duplicate or out-of-order delivery of a vote already processed
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
for (let index = from; index <= voteIndex; index++) {
|
|
140
|
+
const vote = await this.slashingProposer.getVoteAt(round, index);
|
|
141
|
+
if (this.stopped || round !== this.state.round) {
|
|
142
|
+
return; // stopped, or rolled mid-drain: either way this vote must no longer be counted
|
|
143
|
+
}
|
|
144
|
+
this.processVote(round, vote);
|
|
145
|
+
// Advanced only after successful processing, so a failed read is retried on the next event
|
|
146
|
+
this.state.nextVoteIndex = index + 1n;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private processVote(round: bigint, vote: SlashVoteTarget[]): void {
|
|
151
|
+
const own = vote.filter(target => this.isOwnValidator(target.validator));
|
|
152
|
+
for (const { position } of own) {
|
|
153
|
+
this.state.countByPosition.set(position, (this.state.countByPosition.get(position) ?? 0) + 1);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Warn once per (vote, validator) at the validator's highest position tally: the operator cares about the
|
|
157
|
+
// validator, not which of its committee seats is being voted on. No slash amount (the amount voted is not
|
|
158
|
+
// necessarily the amount slashed) and no proposer (a catch-up drain cannot attribute votes to proposers).
|
|
159
|
+
const quorum = this.settings.slashingQuorumSize;
|
|
160
|
+
for (const { validator } of uniqueBy(own, target => target.validator.toString())) {
|
|
161
|
+
const votes = Math.max(
|
|
162
|
+
...own.filter(t => t.validator.equals(validator)).map(t => this.state.countByPosition.get(t.position)!),
|
|
163
|
+
);
|
|
164
|
+
this.metrics.recordOwnValidatorTargeted();
|
|
165
|
+
this.log.warn(
|
|
166
|
+
`Own validator ${validator} targeted by slashing vote (${votes} of ${quorum} votes needed to slash)`,
|
|
167
|
+
{ round, validator: validator.toString(), votes, quorum },
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
this.metrics.recordCurrentRoundVotesMax(Math.max(0, ...this.state.countByPosition.values()));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Starts a fresh tally, zeroing the gauge so a quiet round does not keep the previous round's value. */
|
|
175
|
+
private rollTo(round: bigint): void {
|
|
176
|
+
this.state = { round, nextVoteIndex: 0n, countByPosition: new Map() };
|
|
177
|
+
this.metrics.recordCurrentRoundVotesMax(0);
|
|
178
|
+
this.metrics.recordQuorumSize(this.settings.slashingQuorumSize); // keeps the gauge fresh across export cycles
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private isOwnValidator(address: EthAddress): boolean {
|
|
182
|
+
return this.ownValidators.some(validator => validator.equals(address));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import type { SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
2
|
import { createLogger } from '@aztec/foundation/log';
|
|
3
|
+
import { SerialQueue } from '@aztec/foundation/queue';
|
|
3
4
|
import type { Prettify } from '@aztec/foundation/types';
|
|
4
5
|
import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
|
|
5
6
|
import type { SlasherConfig } from '@aztec/stdlib/interfaces/server';
|
|
6
|
-
import { type Offense,
|
|
7
|
+
import { type Offense, getOffenseTypeName, getSlotForOffense } from '@aztec/stdlib/slashing';
|
|
7
8
|
|
|
8
9
|
import type { SlasherOffensesStore } from './stores/offenses_store.js';
|
|
9
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
WANT_TO_CLEAR_SLASH_EVENT,
|
|
12
|
+
WANT_TO_SLASH_EVENT,
|
|
13
|
+
type WantToClearSlashArgs,
|
|
14
|
+
type WantToSlashArgs,
|
|
15
|
+
type Watcher,
|
|
16
|
+
} from './watcher.js';
|
|
10
17
|
|
|
11
18
|
export type SlashOffensesCollectorConfig = Prettify<Pick<SlasherConfig, 'slashGracePeriodL2Slots'>>;
|
|
12
19
|
export type SlashOffensesCollectorSettings = Prettify<
|
|
@@ -24,6 +31,7 @@ export type SlashOffensesCollectorSettings = Prettify<
|
|
|
24
31
|
*/
|
|
25
32
|
export class SlashOffensesCollector {
|
|
26
33
|
private readonly unwatchCallbacks: (() => void)[] = [];
|
|
34
|
+
private readonly storeMutationQueue = new SerialQueue();
|
|
27
35
|
|
|
28
36
|
constructor(
|
|
29
37
|
private readonly config: SlashOffensesCollectorConfig,
|
|
@@ -35,28 +43,35 @@ export class SlashOffensesCollector {
|
|
|
35
43
|
|
|
36
44
|
public start() {
|
|
37
45
|
this.log.debug('Starting SlashOffensesCollector...');
|
|
46
|
+
this.storeMutationQueue.start();
|
|
38
47
|
|
|
39
|
-
// Subscribe to
|
|
48
|
+
// Subscribe to watcher slashing events.
|
|
40
49
|
for (const watcher of this.watchers) {
|
|
41
50
|
const wantToSlashCallback = (args: WantToSlashArgs[]) =>
|
|
42
|
-
|
|
51
|
+
this.enqueueStoreMutation('wantToSlash', () => this.handleWantToSlash(args));
|
|
43
52
|
watcher.on(WANT_TO_SLASH_EVENT, wantToSlashCallback);
|
|
44
53
|
this.unwatchCallbacks.push(() => watcher.removeListener(WANT_TO_SLASH_EVENT, wantToSlashCallback));
|
|
54
|
+
|
|
55
|
+
const wantToClearSlashCallback = (args: WantToClearSlashArgs[]) =>
|
|
56
|
+
this.enqueueStoreMutation('wantToClearSlash', () => this.handleWantToClearSlash(args));
|
|
57
|
+
watcher.on(WANT_TO_CLEAR_SLASH_EVENT, wantToClearSlashCallback);
|
|
58
|
+
this.unwatchCallbacks.push(() => watcher.removeListener(WANT_TO_CLEAR_SLASH_EVENT, wantToClearSlashCallback));
|
|
45
59
|
}
|
|
46
60
|
|
|
47
61
|
this.log.info('Started SlashOffensesCollector');
|
|
48
62
|
return Promise.resolve();
|
|
49
63
|
}
|
|
50
64
|
|
|
51
|
-
public stop() {
|
|
65
|
+
public async stop() {
|
|
52
66
|
this.log.debug('Stopping SlashOffensesCollector...');
|
|
53
67
|
|
|
54
68
|
for (const unwatchCallback of this.unwatchCallbacks) {
|
|
55
69
|
unwatchCallback();
|
|
56
70
|
}
|
|
57
71
|
|
|
72
|
+
await this.storeMutationQueue.end();
|
|
73
|
+
|
|
58
74
|
this.log.info('SlashOffensesCollector stopped');
|
|
59
|
-
return Promise.resolve();
|
|
60
75
|
}
|
|
61
76
|
|
|
62
77
|
/**
|
|
@@ -66,32 +81,47 @@ export class SlashOffensesCollector {
|
|
|
66
81
|
*/
|
|
67
82
|
public async handleWantToSlash(args: WantToSlashArgs[]) {
|
|
68
83
|
for (const arg of args) {
|
|
69
|
-
const
|
|
84
|
+
const offense: Offense = {
|
|
70
85
|
validator: arg.validator,
|
|
71
86
|
amount: arg.amount,
|
|
72
87
|
offenseType: arg.offenseType,
|
|
73
88
|
epochOrSlot: arg.epochOrSlot,
|
|
74
89
|
};
|
|
75
90
|
|
|
76
|
-
if (this.shouldSkipOffense(
|
|
77
|
-
this.log.verbose('Skipping offense during grace period',
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (await this.offensesStore.hasOffense(pendingOffense)) {
|
|
82
|
-
this.log.debug('Skipping repeated offense', pendingOffense);
|
|
91
|
+
if (this.shouldSkipOffense(offense)) {
|
|
92
|
+
this.log.verbose('Skipping offense during grace period', this.getOffenseLogData(offense));
|
|
83
93
|
continue;
|
|
84
94
|
}
|
|
85
95
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (
|
|
89
|
-
this.
|
|
96
|
+
const added = await this.offensesStore.addOffense(offense);
|
|
97
|
+
if (added) {
|
|
98
|
+
if (this.settings.slashingAmounts) {
|
|
99
|
+
const minSlash = this.settings.slashingAmounts[0];
|
|
100
|
+
if (arg.amount < minSlash) {
|
|
101
|
+
this.log.warn(
|
|
102
|
+
`Offense amount ${arg.amount} is below minimum slashing amount ${minSlash}`,
|
|
103
|
+
this.getOffenseLogData(offense),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
90
106
|
}
|
|
107
|
+
|
|
108
|
+
this.log.info(`Adding pending offense for validator ${arg.validator}`, this.getOffenseLogData(offense));
|
|
109
|
+
} else {
|
|
110
|
+
this.log.debug('Skipping repeated offense', this.getOffenseLogData(offense));
|
|
91
111
|
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
92
114
|
|
|
93
|
-
|
|
94
|
-
|
|
115
|
+
public async handleWantToClearSlash(args: WantToClearSlashArgs[]) {
|
|
116
|
+
for (const arg of args) {
|
|
117
|
+
const cleared = await this.offensesStore.clearOffenses(arg);
|
|
118
|
+
if (cleared > 0) {
|
|
119
|
+
this.log.info(`Cleared ${cleared} pending offenses`, {
|
|
120
|
+
offenseType: getOffenseTypeName(arg.offenseType),
|
|
121
|
+
epochOrSlot: arg.epochOrSlot,
|
|
122
|
+
validators: arg.validators?.map(validator => validator.toString()),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
95
125
|
}
|
|
96
126
|
}
|
|
97
127
|
|
|
@@ -106,18 +136,21 @@ export class SlashOffensesCollector {
|
|
|
106
136
|
}
|
|
107
137
|
}
|
|
108
138
|
|
|
109
|
-
/**
|
|
110
|
-
* Marks offenses as slashed (no longer pending)
|
|
111
|
-
* @param offenses - The offenses to mark as slashed
|
|
112
|
-
*/
|
|
113
|
-
public markAsSlashed(offenses: OffenseIdentifier[]) {
|
|
114
|
-
this.log.verbose(`Marking offenses as slashed`, { offenses });
|
|
115
|
-
return this.offensesStore.markAsSlashed(offenses);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
139
|
/** Returns whether to skip an offense if it happened during the grace period after the network upgrade */
|
|
119
140
|
private shouldSkipOffense(offense: Offense): boolean {
|
|
120
141
|
const offenseSlot = getSlotForOffense(offense, this.settings);
|
|
121
142
|
return offenseSlot < this.settings.rollupRegisteredAtL2Slot + this.config.slashGracePeriodL2Slots;
|
|
122
143
|
}
|
|
144
|
+
|
|
145
|
+
private getOffenseLogData(offense: Offense) {
|
|
146
|
+
return {
|
|
147
|
+
...offense,
|
|
148
|
+
validator: offense.validator.toString(),
|
|
149
|
+
offenseType: getOffenseTypeName(offense.offenseType),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private enqueueStoreMutation(label: string, callback: () => Promise<void>) {
|
|
154
|
+
void this.storeMutationQueue.put(callback).catch(err => this.log.error(`Error handling ${label}`, err));
|
|
155
|
+
}
|
|
123
156
|
}
|