@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.
Files changed (95) hide show
  1. package/README.md +90 -76
  2. package/dest/config.d.ts +1 -1
  3. package/dest/config.d.ts.map +1 -1
  4. package/dest/config.js +53 -41
  5. package/dest/factory/create_facade.d.ts +5 -4
  6. package/dest/factory/create_facade.d.ts.map +1 -1
  7. package/dest/factory/create_facade.js +26 -3
  8. package/dest/factory/create_implementation.d.ts +7 -8
  9. package/dest/factory/create_implementation.d.ts.map +1 -1
  10. package/dest/factory/create_implementation.js +8 -56
  11. package/dest/factory/get_settings.d.ts +4 -4
  12. package/dest/factory/get_settings.d.ts.map +1 -1
  13. package/dest/factory/get_settings.js +3 -3
  14. package/dest/factory/index.d.ts +2 -2
  15. package/dest/factory/index.d.ts.map +1 -1
  16. package/dest/factory/index.js +1 -1
  17. package/dest/generated/slasher-defaults.d.ts +21 -0
  18. package/dest/generated/slasher-defaults.d.ts.map +1 -0
  19. package/dest/generated/slasher-defaults.js +21 -0
  20. package/dest/index.d.ts +6 -4
  21. package/dest/index.d.ts.map +1 -1
  22. package/dest/index.js +5 -3
  23. package/dest/metrics.d.ts +7 -0
  24. package/dest/metrics.d.ts.map +1 -0
  25. package/dest/metrics.js +11 -0
  26. package/dest/null_slasher_client.d.ts +3 -4
  27. package/dest/null_slasher_client.d.ts.map +1 -1
  28. package/dest/null_slasher_client.js +1 -4
  29. package/dest/slash_offenses_collector.d.ts +10 -9
  30. package/dest/slash_offenses_collector.d.ts.map +1 -1
  31. package/dest/slash_offenses_collector.js +50 -30
  32. package/dest/{tally_slasher_client.d.ts → slasher_client.d.ts} +17 -28
  33. package/dest/slasher_client.d.ts.map +1 -0
  34. package/dest/{tally_slasher_client.js → slasher_client.js} +55 -45
  35. package/dest/slasher_client_facade.d.ts +7 -9
  36. package/dest/slasher_client_facade.d.ts.map +1 -1
  37. package/dest/slasher_client_facade.js +6 -9
  38. package/dest/slasher_client_interface.d.ts +7 -21
  39. package/dest/slasher_client_interface.d.ts.map +1 -1
  40. package/dest/slasher_client_interface.js +1 -4
  41. package/dest/stores/offenses_store.d.ts +12 -12
  42. package/dest/stores/offenses_store.d.ts.map +1 -1
  43. package/dest/stores/offenses_store.js +64 -39
  44. package/dest/watcher.d.ts +8 -1
  45. package/dest/watcher.d.ts.map +1 -1
  46. package/dest/watcher.js +1 -0
  47. package/dest/watchers/attestations_block_watcher.d.ts +29 -15
  48. package/dest/watchers/attestations_block_watcher.d.ts.map +1 -1
  49. package/dest/watchers/attestations_block_watcher.js +90 -69
  50. package/dest/watchers/attested_invalid_proposal_watcher.d.ts +42 -0
  51. package/dest/watchers/attested_invalid_proposal_watcher.d.ts.map +1 -0
  52. package/dest/watchers/attested_invalid_proposal_watcher.js +117 -0
  53. package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.d.ts +38 -0
  54. package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.d.ts.map +1 -0
  55. package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.js +138 -0
  56. package/dest/watchers/checkpoint_equivocation_watcher.d.ts +30 -0
  57. package/dest/watchers/checkpoint_equivocation_watcher.d.ts.map +1 -0
  58. package/dest/watchers/checkpoint_equivocation_watcher.js +69 -0
  59. package/dest/watchers/data_withholding_watcher.d.ts +63 -0
  60. package/dest/watchers/data_withholding_watcher.d.ts.map +1 -0
  61. package/dest/watchers/data_withholding_watcher.js +193 -0
  62. package/package.json +15 -13
  63. package/src/config.ts +61 -41
  64. package/src/factory/create_facade.ts +35 -6
  65. package/src/factory/create_implementation.ts +25 -106
  66. package/src/factory/get_settings.ts +8 -8
  67. package/src/factory/index.ts +1 -1
  68. package/src/generated/slasher-defaults.ts +23 -0
  69. package/src/index.ts +5 -3
  70. package/src/metrics.ts +19 -0
  71. package/src/null_slasher_client.ts +2 -6
  72. package/src/slash_offenses_collector.ts +70 -32
  73. package/src/{tally_slasher_client.ts → slasher_client.ts} +72 -54
  74. package/src/slasher_client_facade.ts +7 -12
  75. package/src/slasher_client_interface.ts +6 -21
  76. package/src/stores/offenses_store.ts +76 -48
  77. package/src/watcher.ts +8 -0
  78. package/src/watchers/attestations_block_watcher.ts +107 -88
  79. package/src/watchers/attested_invalid_proposal_watcher.ts +168 -0
  80. package/src/watchers/broadcasted_invalid_checkpoint_proposal_watcher.ts +192 -0
  81. package/src/watchers/checkpoint_equivocation_watcher.ts +96 -0
  82. package/src/watchers/data_withholding_watcher.ts +225 -0
  83. package/dest/empire_slasher_client.d.ts +0 -190
  84. package/dest/empire_slasher_client.d.ts.map +0 -1
  85. package/dest/empire_slasher_client.js +0 -572
  86. package/dest/stores/payloads_store.d.ts +0 -29
  87. package/dest/stores/payloads_store.d.ts.map +0 -1
  88. package/dest/stores/payloads_store.js +0 -125
  89. package/dest/tally_slasher_client.d.ts.map +0 -1
  90. package/dest/watchers/epoch_prune_watcher.d.ts +0 -37
  91. package/dest/watchers/epoch_prune_watcher.d.ts.map +0 -1
  92. package/dest/watchers/epoch_prune_watcher.js +0 -137
  93. package/src/empire_slasher_client.ts +0 -657
  94. package/src/stores/payloads_store.ts +0 -146
  95. package/src/watchers/epoch_prune_watcher.ts +0 -194
@@ -1,142 +1,61 @@
1
1
  import { EpochCache } from '@aztec/epoch-cache';
2
- import type { ViemClient } from '@aztec/ethereum';
3
- import {
4
- EmpireSlashingProposerContract,
5
- RollupContract,
6
- TallySlashingProposerContract,
7
- } from '@aztec/ethereum/contracts';
8
- import { EthAddress } from '@aztec/foundation/eth-address';
2
+ import { RollupContract, SlashingProposerContract } from '@aztec/ethereum/contracts';
3
+ import type { ViemClient } from '@aztec/ethereum/types';
4
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
9
5
  import { createLogger } from '@aztec/foundation/log';
10
6
  import { DateProvider } from '@aztec/foundation/timer';
11
- import type { DataStoreConfig } from '@aztec/kv-store/config';
12
7
  import { AztecLMDBStoreV2 } from '@aztec/kv-store/lmdb-v2';
13
8
  import type { SlasherConfig } from '@aztec/stdlib/interfaces/server';
14
- import { SlashFactoryContract } from '@aztec/stdlib/l1-contracts';
9
+ import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
15
10
 
16
- import { EmpireSlasherClient, type EmpireSlasherSettings } from '../empire_slasher_client.js';
17
11
  import { NullSlasherClient } from '../null_slasher_client.js';
12
+ import { SlasherClient } from '../slasher_client.js';
18
13
  import { SlasherOffensesStore } from '../stores/offenses_store.js';
19
- import { SlasherPayloadsStore } from '../stores/payloads_store.js';
20
- import { TallySlasherClient } from '../tally_slasher_client.js';
21
14
  import type { Watcher } from '../watcher.js';
22
- import { getTallySlasherSettings } from './get_settings.js';
15
+ import { getSlasherSettings } from './get_settings.js';
23
16
 
24
- /** Creates a slasher client implementation (either tally or empire) based on the slasher proposer type in the rollup */
17
+ /** Creates a slasher client implementation based on the slasher proposer type in the rollup */
25
18
  export async function createSlasherImplementation(
26
19
  config: SlasherConfig & DataStoreConfig & { ethereumSlotDuration: number },
27
20
  rollup: RollupContract,
28
21
  l1Client: ViemClient,
29
- slashFactoryAddress: EthAddress | undefined,
30
22
  watchers: Watcher[],
31
23
  epochCache: EpochCache,
32
24
  dateProvider: DateProvider,
33
25
  kvStore: AztecLMDBStoreV2,
26
+ rollupRegisteredAtL2Slot: SlotNumber,
34
27
  logger = createLogger('slasher'),
35
28
  ) {
36
29
  const proposer = await rollup.getSlashingProposer();
37
30
  if (!proposer) {
38
31
  return new NullSlasherClient(config);
39
- } else if (proposer.type === 'tally') {
40
- return createTallySlasher(config, rollup, proposer, watchers, dateProvider, epochCache, kvStore, logger);
41
32
  } else {
42
- if (!slashFactoryAddress || slashFactoryAddress.equals(EthAddress.ZERO)) {
43
- throw new Error('Cannot initialize an empire-based SlasherClient without a SlashFactory address');
44
- }
45
- const slashFactory = new SlashFactoryContract(l1Client, slashFactoryAddress.toString());
46
- return createEmpireSlasher(config, rollup, proposer, slashFactory, watchers, dateProvider, kvStore, logger);
33
+ return createSlasher(
34
+ config,
35
+ rollup,
36
+ proposer,
37
+ watchers,
38
+ dateProvider,
39
+ epochCache,
40
+ kvStore,
41
+ rollupRegisteredAtL2Slot,
42
+ logger,
43
+ );
47
44
  }
48
45
  }
49
46
 
50
- async function createEmpireSlasher(
51
- config: SlasherConfig & DataStoreConfig & { ethereumSlotDuration: number },
52
- rollup: RollupContract,
53
- slashingProposer: EmpireSlashingProposerContract,
54
- slashFactoryContract: SlashFactoryContract,
55
- watchers: Watcher[],
56
- dateProvider: DateProvider,
57
- kvStore: AztecLMDBStoreV2,
58
- logger = createLogger('slasher'),
59
- ): Promise<EmpireSlasherClient> {
60
- if (slashingProposer.type !== 'empire') {
61
- throw new Error('Slashing proposer contract is not of type Empire');
62
- }
63
-
64
- const [
65
- slashingExecutionDelayInRounds,
66
- slashingPayloadLifetimeInRounds,
67
- slashingRoundSize,
68
- slashingQuorumSize,
69
- epochDuration,
70
- proofSubmissionEpochs,
71
- l1GenesisTime,
72
- slotDuration,
73
- l1StartBlock,
74
- slasher,
75
- ] = await Promise.all([
76
- slashingProposer.getExecutionDelayInRounds(),
77
- slashingProposer.getLifetimeInRounds(),
78
- slashingProposer.getRoundSize(),
79
- slashingProposer.getQuorumSize(),
80
- rollup.getEpochDuration(),
81
- rollup.getProofSubmissionEpochs(),
82
- rollup.getL1GenesisTime(),
83
- rollup.getSlotDuration(),
84
- rollup.getL1StartBlock(),
85
- rollup.getSlasherContract(),
86
- ]);
87
-
88
- const settings: EmpireSlasherSettings = {
89
- slashingExecutionDelayInRounds: Number(slashingExecutionDelayInRounds),
90
- slashingPayloadLifetimeInRounds: Number(slashingPayloadLifetimeInRounds),
91
- slashingRoundSize: Number(slashingRoundSize),
92
- slashingQuorumSize: Number(slashingQuorumSize),
93
- epochDuration: Number(epochDuration),
94
- proofSubmissionEpochs: Number(proofSubmissionEpochs),
95
- l1GenesisTime: l1GenesisTime,
96
- slotDuration: Number(slotDuration),
97
- l1StartBlock,
98
- ethereumSlotDuration: config.ethereumSlotDuration,
99
- slashingAmounts: undefined,
100
- };
101
-
102
- const payloadsStore = new SlasherPayloadsStore(kvStore, {
103
- slashingPayloadLifetimeInRounds: settings.slashingPayloadLifetimeInRounds,
104
- });
105
- const offensesStore = new SlasherOffensesStore(kvStore, {
106
- ...settings,
107
- slashOffenseExpirationRounds: config.slashOffenseExpirationRounds,
108
- });
109
-
110
- return new EmpireSlasherClient(
111
- config,
112
- settings,
113
- slashFactoryContract,
114
- slashingProposer,
115
- slasher!,
116
- rollup,
117
- watchers,
118
- dateProvider,
119
- offensesStore,
120
- payloadsStore,
121
- logger,
122
- );
123
- }
124
-
125
- async function createTallySlasher(
47
+ async function createSlasher(
126
48
  config: SlasherConfig & DataStoreConfig,
127
49
  rollup: RollupContract,
128
- slashingProposer: TallySlashingProposerContract,
50
+ slashingProposer: SlashingProposerContract,
129
51
  watchers: Watcher[],
130
52
  dateProvider: DateProvider,
131
53
  epochCache: EpochCache,
132
54
  kvStore: AztecLMDBStoreV2,
55
+ rollupRegisteredAtL2Slot: SlotNumber,
133
56
  logger = createLogger('slasher'),
134
- ): Promise<TallySlasherClient> {
135
- if (slashingProposer.type !== 'tally') {
136
- throw new Error('Slashing proposer contract is not of type tally');
137
- }
138
-
139
- const settings = await getTallySlasherSettings(rollup, slashingProposer);
57
+ ): Promise<SlasherClient> {
58
+ const settings = { ...(await getSlasherSettings(rollup, slashingProposer)), rollupRegisteredAtL2Slot };
140
59
  const slasher = await rollup.getSlasherContract();
141
60
 
142
61
  const offensesStore = new SlasherOffensesStore(kvStore, {
@@ -144,7 +63,7 @@ async function createTallySlasher(
144
63
  slashOffenseExpirationRounds: config.slashOffenseExpirationRounds,
145
64
  });
146
65
 
147
- return new TallySlasherClient(
66
+ return new SlasherClient(
148
67
  config,
149
68
  settings,
150
69
  slashingProposer,
@@ -1,15 +1,15 @@
1
- import type { RollupContract, TallySlashingProposerContract } from '@aztec/ethereum';
1
+ import type { RollupContract, SlashingProposerContract } from '@aztec/ethereum/contracts';
2
2
 
3
- import type { TallySlasherSettings } from '../tally_slasher_client.js';
3
+ import type { SlasherSettings } from '../slasher_client.js';
4
4
 
5
- export async function getTallySlasherSettings(
5
+ export async function getSlasherSettings(
6
6
  rollup: RollupContract,
7
- slashingProposer?: TallySlashingProposerContract,
8
- ): Promise<TallySlasherSettings> {
7
+ slashingProposer?: SlashingProposerContract,
8
+ ): Promise<Omit<SlasherSettings, 'rollupRegisteredAtL2Slot'>> {
9
9
  if (!slashingProposer) {
10
10
  const rollupSlashingProposer = await rollup.getSlashingProposer();
11
- if (!rollupSlashingProposer || rollupSlashingProposer.type !== 'tally') {
12
- throw new Error('Rollup slashing proposer is not of type tally');
11
+ if (!rollupSlashingProposer) {
12
+ throw new Error('Rollup slashing proposer not found');
13
13
  }
14
14
  slashingProposer = rollupSlashingProposer;
15
15
  }
@@ -40,7 +40,7 @@ export async function getTallySlasherSettings(
40
40
  rollup.getTargetCommitteeSize(),
41
41
  ]);
42
42
 
43
- const settings: TallySlasherSettings = {
43
+ const settings: Omit<SlasherSettings, 'rollupRegisteredAtL2Slot'> = {
44
44
  slashingExecutionDelayInRounds: Number(slashingExecutionDelayInRounds),
45
45
  slashingRoundSize: Number(slashingRoundSize),
46
46
  slashingRoundSizeInEpochs: Number(slashingRoundSizeInEpochs),
@@ -1,2 +1,2 @@
1
1
  export { createSlasherFacade as createSlasher } from './create_facade.js';
2
- export { getTallySlasherSettings } from './get_settings.js';
2
+ export { getSlasherSettings } from './get_settings.js';
@@ -0,0 +1,23 @@
1
+ // Auto-generated from spartan/environments/network-defaults.yml
2
+ // Do not edit manually - run yarn generate to regenerate
3
+
4
+ /** Default slasher configuration values from network-defaults.yml */
5
+ export const slasherDefaultEnv = {
6
+ SLASH_OFFENSE_EXPIRATION_ROUNDS: 4,
7
+ SLASH_MAX_PAYLOAD_SIZE: 80,
8
+ SLASH_EXECUTE_ROUNDS_LOOK_BACK: 4,
9
+ SLASH_DATA_WITHHOLDING_PENALTY: 10000000000000000000,
10
+ SLASH_DATA_WITHHOLDING_TOLERANCE_SLOTS: 3,
11
+ SLASH_INACTIVITY_TARGET_PERCENTAGE: 0.9,
12
+ SLASH_INACTIVITY_CONSECUTIVE_EPOCH_THRESHOLD: 1,
13
+ SLASH_INACTIVITY_PENALTY: 10000000000000000000,
14
+ SLASH_PROPOSE_INVALID_ATTESTATIONS_PENALTY: 10000000000000000000,
15
+ SLASH_PROPOSE_DESCENDANT_OF_CHECKPOINT_WITH_INVALID_ATTESTATIONS_PENALTY: 10000000000000000000,
16
+ SLASH_ATTEST_INVALID_CHECKPOINT_PROPOSAL_PENALTY: 10000000000000000000,
17
+ SLASH_DUPLICATE_PROPOSAL_PENALTY: 0,
18
+ SLASH_DUPLICATE_ATTESTATION_PENALTY: 0,
19
+ SLASH_UNKNOWN_PENALTY: 10000000000000000000,
20
+ SLASH_INVALID_BLOCK_PENALTY: 10000000000000000000,
21
+ SLASH_INVALID_CHECKPOINT_PROPOSAL_PENALTY: 0,
22
+ SLASH_GRACE_PERIOD_L2_SLOTS: 0,
23
+ } as const;
package/src/index.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  export * from './config.js';
2
- export * from './watchers/epoch_prune_watcher.js';
2
+ export * from './watchers/data_withholding_watcher.js';
3
3
  export * from './watchers/attestations_block_watcher.js';
4
- export * from './empire_slasher_client.js';
5
- export * from './tally_slasher_client.js';
4
+ export * from './watchers/attested_invalid_proposal_watcher.js';
5
+ export * from './watchers/broadcasted_invalid_checkpoint_proposal_watcher.js';
6
+ export * from './watchers/checkpoint_equivocation_watcher.js';
7
+ export * from './slasher_client.js';
6
8
  export * from './slash_offenses_collector.js';
7
9
  export * from './slasher_client_interface.js';
8
10
  export * from './factory/index.js';
package/src/metrics.ts ADDED
@@ -0,0 +1,19 @@
1
+ import {
2
+ Metrics,
3
+ type TelemetryClient,
4
+ type UpDownCounter,
5
+ createUpDownCounterWithDefault,
6
+ } from '@aztec/telemetry-client';
7
+
8
+ export class SlasherMetrics {
9
+ private readonly roundExecuted: UpDownCounter;
10
+
11
+ constructor(client: TelemetryClient, name = 'Slasher') {
12
+ const meter = client.getMeter(name);
13
+ this.roundExecuted = createUpDownCounterWithDefault(meter, Metrics.SLASHER_ROUND_EXECUTED_COUNT);
14
+ }
15
+
16
+ public recordRoundExecuted(): void {
17
+ this.roundExecuted.add(1);
18
+ }
19
+ }
@@ -1,5 +1,5 @@
1
1
  import type { SlotNumber } from '@aztec/foundation/branded-types';
2
- import type { Offense, ProposerSlashAction, SlashPayloadRound } from '@aztec/stdlib/slashing';
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 getPendingOffenses(): Promise<Offense[]> {
22
+ public getOffenses(): Promise<Offense[]> {
27
23
  return Promise.resolve([]);
28
24
  }
29
25
 
@@ -1,15 +1,27 @@
1
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
1
2
  import { createLogger } from '@aztec/foundation/log';
3
+ import { SerialQueue } from '@aztec/foundation/queue';
2
4
  import type { Prettify } from '@aztec/foundation/types';
3
5
  import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
4
6
  import type { SlasherConfig } from '@aztec/stdlib/interfaces/server';
5
- import { type Offense, type OffenseIdentifier, getSlotForOffense } from '@aztec/stdlib/slashing';
7
+ import { type Offense, getOffenseTypeName, getSlotForOffense } from '@aztec/stdlib/slashing';
6
8
 
7
9
  import type { SlasherOffensesStore } from './stores/offenses_store.js';
8
- import { WANT_TO_SLASH_EVENT, type WantToSlashArgs, type Watcher } from './watcher.js';
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';
9
17
 
10
18
  export type SlashOffensesCollectorConfig = Prettify<Pick<SlasherConfig, 'slashGracePeriodL2Slots'>>;
11
19
  export type SlashOffensesCollectorSettings = Prettify<
12
- Pick<L1RollupConstants, 'epochDuration'> & { slashingAmounts: [bigint, bigint, bigint] | undefined }
20
+ Pick<L1RollupConstants, 'epochDuration'> & {
21
+ slashingAmounts: [bigint, bigint, bigint] | undefined;
22
+ /** L2 slot at which the rollup was registered as canonical in the Registry. Used to anchor the slash grace period. */
23
+ rollupRegisteredAtL2Slot: SlotNumber;
24
+ }
13
25
  >;
14
26
 
15
27
  /**
@@ -19,6 +31,7 @@ export type SlashOffensesCollectorSettings = Prettify<
19
31
  */
20
32
  export class SlashOffensesCollector {
21
33
  private readonly unwatchCallbacks: (() => void)[] = [];
34
+ private readonly storeMutationQueue = new SerialQueue();
22
35
 
23
36
  constructor(
24
37
  private readonly config: SlashOffensesCollectorConfig,
@@ -30,28 +43,35 @@ export class SlashOffensesCollector {
30
43
 
31
44
  public start() {
32
45
  this.log.debug('Starting SlashOffensesCollector...');
46
+ this.storeMutationQueue.start();
33
47
 
34
- // Subscribe to watchers WANT_TO_SLASH_EVENT
48
+ // Subscribe to watcher slashing events.
35
49
  for (const watcher of this.watchers) {
36
50
  const wantToSlashCallback = (args: WantToSlashArgs[]) =>
37
- void this.handleWantToSlash(args).catch(err => this.log.error('Error handling wantToSlash', err));
51
+ this.enqueueStoreMutation('wantToSlash', () => this.handleWantToSlash(args));
38
52
  watcher.on(WANT_TO_SLASH_EVENT, wantToSlashCallback);
39
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));
40
59
  }
41
60
 
42
61
  this.log.info('Started SlashOffensesCollector');
43
62
  return Promise.resolve();
44
63
  }
45
64
 
46
- public stop() {
65
+ public async stop() {
47
66
  this.log.debug('Stopping SlashOffensesCollector...');
48
67
 
49
68
  for (const unwatchCallback of this.unwatchCallbacks) {
50
69
  unwatchCallback();
51
70
  }
52
71
 
72
+ await this.storeMutationQueue.end();
73
+
53
74
  this.log.info('SlashOffensesCollector stopped');
54
- return Promise.resolve();
55
75
  }
56
76
 
57
77
  /**
@@ -61,32 +81,47 @@ export class SlashOffensesCollector {
61
81
  */
62
82
  public async handleWantToSlash(args: WantToSlashArgs[]) {
63
83
  for (const arg of args) {
64
- const pendingOffense: Offense = {
84
+ const offense: Offense = {
65
85
  validator: arg.validator,
66
86
  amount: arg.amount,
67
87
  offenseType: arg.offenseType,
68
88
  epochOrSlot: arg.epochOrSlot,
69
89
  };
70
90
 
71
- if (this.shouldSkipOffense(pendingOffense)) {
72
- this.log.verbose('Skipping offense during grace period', pendingOffense);
73
- continue;
74
- }
75
-
76
- if (await this.offensesStore.hasOffense(pendingOffense)) {
77
- this.log.debug('Skipping repeated offense', pendingOffense);
91
+ if (this.shouldSkipOffense(offense)) {
92
+ this.log.verbose('Skipping offense during grace period', this.getOffenseLogData(offense));
78
93
  continue;
79
94
  }
80
95
 
81
- if (this.settings.slashingAmounts) {
82
- const minSlash = this.settings.slashingAmounts[0];
83
- if (arg.amount < minSlash) {
84
- this.log.warn(`Offense amount ${arg.amount} is below minimum slashing amount ${minSlash}`);
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
+ }
85
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));
86
111
  }
112
+ }
113
+ }
87
114
 
88
- this.log.info(`Adding pending offense for validator ${arg.validator}`, pendingOffense);
89
- await this.offensesStore.addPendingOffense(pendingOffense);
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
+ }
90
125
  }
91
126
  }
92
127
 
@@ -101,18 +136,21 @@ export class SlashOffensesCollector {
101
136
  }
102
137
  }
103
138
 
104
- /**
105
- * Marks offenses as slashed (no longer pending)
106
- * @param offenses - The offenses to mark as slashed
107
- */
108
- public markAsSlashed(offenses: OffenseIdentifier[]) {
109
- this.log.verbose(`Marking offenses as slashed`, { offenses });
110
- return this.offensesStore.markAsSlashed(offenses);
111
- }
112
-
113
- /** Returns whether to skip an offense if it happened during the grace period at the beginning of the chain */
139
+ /** Returns whether to skip an offense if it happened during the grace period after the network upgrade */
114
140
  private shouldSkipOffense(offense: Offense): boolean {
115
141
  const offenseSlot = getSlotForOffense(offense, this.settings);
116
- return offenseSlot < this.config.slashGracePeriodL2Slots;
142
+ return offenseSlot < this.settings.rollupRegisteredAtL2Slot + this.config.slashGracePeriodL2Slots;
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));
117
155
  }
118
156
  }