@aztec/slasher 0.0.1-commit.21caa21

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 (82) hide show
  1. package/README.md +218 -0
  2. package/dest/config.d.ts +6 -0
  3. package/dest/config.d.ts.map +1 -0
  4. package/dest/config.js +134 -0
  5. package/dest/empire_slasher_client.d.ts +190 -0
  6. package/dest/empire_slasher_client.d.ts.map +1 -0
  7. package/dest/empire_slasher_client.js +572 -0
  8. package/dest/factory/create_facade.d.ts +15 -0
  9. package/dest/factory/create_facade.d.ts.map +1 -0
  10. package/dest/factory/create_facade.js +23 -0
  11. package/dest/factory/create_implementation.d.ts +17 -0
  12. package/dest/factory/create_implementation.d.ts.map +1 -0
  13. package/dest/factory/create_implementation.js +73 -0
  14. package/dest/factory/get_settings.d.ts +4 -0
  15. package/dest/factory/get_settings.d.ts.map +1 -0
  16. package/dest/factory/get_settings.js +36 -0
  17. package/dest/factory/index.d.ts +3 -0
  18. package/dest/factory/index.d.ts.map +1 -0
  19. package/dest/factory/index.js +2 -0
  20. package/dest/index.d.ts +11 -0
  21. package/dest/index.d.ts.map +1 -0
  22. package/dest/index.js +10 -0
  23. package/dest/null_slasher_client.d.ts +17 -0
  24. package/dest/null_slasher_client.d.ts.map +1 -0
  25. package/dest/null_slasher_client.js +33 -0
  26. package/dest/slash_offenses_collector.d.ts +45 -0
  27. package/dest/slash_offenses_collector.d.ts.map +1 -0
  28. package/dest/slash_offenses_collector.js +94 -0
  29. package/dest/slash_round_monitor.d.ts +30 -0
  30. package/dest/slash_round_monitor.d.ts.map +1 -0
  31. package/dest/slash_round_monitor.js +52 -0
  32. package/dest/slasher_client_facade.d.ts +44 -0
  33. package/dest/slasher_client_facade.d.ts.map +1 -0
  34. package/dest/slasher_client_facade.js +76 -0
  35. package/dest/slasher_client_interface.d.ts +39 -0
  36. package/dest/slasher_client_interface.d.ts.map +1 -0
  37. package/dest/slasher_client_interface.js +4 -0
  38. package/dest/stores/offenses_store.d.ts +37 -0
  39. package/dest/stores/offenses_store.d.ts.map +1 -0
  40. package/dest/stores/offenses_store.js +105 -0
  41. package/dest/stores/payloads_store.d.ts +29 -0
  42. package/dest/stores/payloads_store.d.ts.map +1 -0
  43. package/dest/stores/payloads_store.js +125 -0
  44. package/dest/stores/schema_version.d.ts +2 -0
  45. package/dest/stores/schema_version.d.ts.map +1 -0
  46. package/dest/stores/schema_version.js +1 -0
  47. package/dest/tally_slasher_client.d.ts +125 -0
  48. package/dest/tally_slasher_client.d.ts.map +1 -0
  49. package/dest/tally_slasher_client.js +349 -0
  50. package/dest/test/dummy_watcher.d.ts +11 -0
  51. package/dest/test/dummy_watcher.d.ts.map +1 -0
  52. package/dest/test/dummy_watcher.js +14 -0
  53. package/dest/watcher.d.ts +21 -0
  54. package/dest/watcher.d.ts.map +1 -0
  55. package/dest/watcher.js +1 -0
  56. package/dest/watchers/attestations_block_watcher.d.ts +33 -0
  57. package/dest/watchers/attestations_block_watcher.d.ts.map +1 -0
  58. package/dest/watchers/attestations_block_watcher.js +136 -0
  59. package/dest/watchers/epoch_prune_watcher.d.ts +37 -0
  60. package/dest/watchers/epoch_prune_watcher.d.ts.map +1 -0
  61. package/dest/watchers/epoch_prune_watcher.js +135 -0
  62. package/package.json +90 -0
  63. package/src/config.ts +157 -0
  64. package/src/empire_slasher_client.ts +657 -0
  65. package/src/factory/create_facade.ts +52 -0
  66. package/src/factory/create_implementation.ts +159 -0
  67. package/src/factory/get_settings.ts +58 -0
  68. package/src/factory/index.ts +2 -0
  69. package/src/index.ts +10 -0
  70. package/src/null_slasher_client.ts +41 -0
  71. package/src/slash_offenses_collector.ts +118 -0
  72. package/src/slash_round_monitor.ts +62 -0
  73. package/src/slasher_client_facade.ts +101 -0
  74. package/src/slasher_client_interface.ts +46 -0
  75. package/src/stores/offenses_store.ts +145 -0
  76. package/src/stores/payloads_store.ts +146 -0
  77. package/src/stores/schema_version.ts +1 -0
  78. package/src/tally_slasher_client.ts +442 -0
  79. package/src/test/dummy_watcher.ts +21 -0
  80. package/src/watcher.ts +27 -0
  81. package/src/watchers/attestations_block_watcher.ts +181 -0
  82. package/src/watchers/epoch_prune_watcher.ts +193 -0
@@ -0,0 +1,52 @@
1
+ import { EpochCache } from '@aztec/epoch-cache';
2
+ import type { L1ReaderConfig, ViemClient } from '@aztec/ethereum';
3
+ import { RollupContract } from '@aztec/ethereum/contracts';
4
+ import { unique } from '@aztec/foundation/collection';
5
+ import { EthAddress } from '@aztec/foundation/eth-address';
6
+ import { createLogger } from '@aztec/foundation/log';
7
+ import { DateProvider } from '@aztec/foundation/timer';
8
+ import type { DataStoreConfig } from '@aztec/kv-store/config';
9
+ import { createStore } from '@aztec/kv-store/lmdb-v2';
10
+ import type { SlasherConfig } from '@aztec/stdlib/interfaces/server';
11
+
12
+ import { SlasherClientFacade } from '../slasher_client_facade.js';
13
+ import type { SlasherClientInterface } from '../slasher_client_interface.js';
14
+ import { SCHEMA_VERSION } from '../stores/schema_version.js';
15
+ import type { Watcher } from '../watcher.js';
16
+
17
+ /** Creates a slasher client facade that updates itself whenever the rollup slasher changes */
18
+ export async function createSlasherFacade(
19
+ config: SlasherConfig & DataStoreConfig & { ethereumSlotDuration: number },
20
+ l1Contracts: Pick<L1ReaderConfig['l1Contracts'], 'rollupAddress' | 'slashFactoryAddress'>,
21
+ l1Client: ViemClient,
22
+ watchers: Watcher[],
23
+ dateProvider: DateProvider,
24
+ epochCache: EpochCache,
25
+ /** List of own validator addresses to add to the slashValidatorNever list unless slashSelfAllowed is true */
26
+ validatorAddresses: EthAddress[] = [],
27
+ logger = createLogger('slasher'),
28
+ ): Promise<SlasherClientInterface> {
29
+ if (!l1Contracts.rollupAddress || l1Contracts.rollupAddress.equals(EthAddress.ZERO)) {
30
+ throw new Error('Cannot initialize SlasherClient without a Rollup address');
31
+ }
32
+
33
+ const kvStore = await createStore('slasher', SCHEMA_VERSION, config, createLogger('slasher:lmdb'));
34
+ const rollup = new RollupContract(l1Client, l1Contracts.rollupAddress);
35
+
36
+ const slashValidatorsNever = config.slashSelfAllowed
37
+ ? config.slashValidatorsNever
38
+ : unique([...config.slashValidatorsNever, ...validatorAddresses].map(a => a.toString())).map(EthAddress.fromString);
39
+ const updatedConfig = { ...config, slashValidatorsNever };
40
+
41
+ return new SlasherClientFacade(
42
+ updatedConfig,
43
+ rollup,
44
+ l1Client,
45
+ l1Contracts.slashFactoryAddress,
46
+ watchers,
47
+ epochCache,
48
+ dateProvider,
49
+ kvStore,
50
+ logger,
51
+ );
52
+ }
@@ -0,0 +1,159 @@
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';
9
+ import { createLogger } from '@aztec/foundation/log';
10
+ import { DateProvider } from '@aztec/foundation/timer';
11
+ import type { DataStoreConfig } from '@aztec/kv-store/config';
12
+ import { AztecLMDBStoreV2 } from '@aztec/kv-store/lmdb-v2';
13
+ import type { SlasherConfig } from '@aztec/stdlib/interfaces/server';
14
+ import { SlashFactoryContract } from '@aztec/stdlib/l1-contracts';
15
+
16
+ import { EmpireSlasherClient, type EmpireSlasherSettings } from '../empire_slasher_client.js';
17
+ import { NullSlasherClient } from '../null_slasher_client.js';
18
+ import { SlasherOffensesStore } from '../stores/offenses_store.js';
19
+ import { SlasherPayloadsStore } from '../stores/payloads_store.js';
20
+ import { TallySlasherClient } from '../tally_slasher_client.js';
21
+ import type { Watcher } from '../watcher.js';
22
+ import { getTallySlasherSettings } from './get_settings.js';
23
+
24
+ /** Creates a slasher client implementation (either tally or empire) based on the slasher proposer type in the rollup */
25
+ export async function createSlasherImplementation(
26
+ config: SlasherConfig & DataStoreConfig & { ethereumSlotDuration: number },
27
+ rollup: RollupContract,
28
+ l1Client: ViemClient,
29
+ slashFactoryAddress: EthAddress | undefined,
30
+ watchers: Watcher[],
31
+ epochCache: EpochCache,
32
+ dateProvider: DateProvider,
33
+ kvStore: AztecLMDBStoreV2,
34
+ logger = createLogger('slasher'),
35
+ ) {
36
+ const proposer = await rollup.getSlashingProposer();
37
+ if (!proposer) {
38
+ return new NullSlasherClient(config);
39
+ } else if (proposer.type === 'tally') {
40
+ return createTallySlasher(config, rollup, proposer, watchers, dateProvider, epochCache, kvStore, logger);
41
+ } 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);
47
+ }
48
+ }
49
+
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(
126
+ config: SlasherConfig & DataStoreConfig,
127
+ rollup: RollupContract,
128
+ slashingProposer: TallySlashingProposerContract,
129
+ watchers: Watcher[],
130
+ dateProvider: DateProvider,
131
+ epochCache: EpochCache,
132
+ kvStore: AztecLMDBStoreV2,
133
+ 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);
140
+ const slasher = await rollup.getSlasherContract();
141
+
142
+ const offensesStore = new SlasherOffensesStore(kvStore, {
143
+ ...settings,
144
+ slashOffenseExpirationRounds: config.slashOffenseExpirationRounds,
145
+ });
146
+
147
+ return new TallySlasherClient(
148
+ config,
149
+ settings,
150
+ slashingProposer,
151
+ slasher!,
152
+ rollup,
153
+ watchers,
154
+ epochCache,
155
+ dateProvider,
156
+ offensesStore,
157
+ logger,
158
+ );
159
+ }
@@ -0,0 +1,58 @@
1
+ import type { RollupContract, TallySlashingProposerContract } from '@aztec/ethereum';
2
+
3
+ import type { TallySlasherSettings } from '../tally_slasher_client.js';
4
+
5
+ export async function getTallySlasherSettings(
6
+ rollup: RollupContract,
7
+ slashingProposer?: TallySlashingProposerContract,
8
+ ): Promise<TallySlasherSettings> {
9
+ if (!slashingProposer) {
10
+ const rollupSlashingProposer = await rollup.getSlashingProposer();
11
+ if (!rollupSlashingProposer || rollupSlashingProposer.type !== 'tally') {
12
+ throw new Error('Rollup slashing proposer is not of type tally');
13
+ }
14
+ slashingProposer = rollupSlashingProposer;
15
+ }
16
+
17
+ const [
18
+ slashingExecutionDelayInRounds,
19
+ slashingRoundSize,
20
+ slashingRoundSizeInEpochs,
21
+ slashingLifetimeInRounds,
22
+ slashingOffsetInRounds,
23
+ slashingAmounts,
24
+ slashingQuorumSize,
25
+ epochDuration,
26
+ l1GenesisTime,
27
+ slotDuration,
28
+ targetCommitteeSize,
29
+ ] = await Promise.all([
30
+ slashingProposer.getExecutionDelayInRounds(),
31
+ slashingProposer.getRoundSize(),
32
+ slashingProposer.getRoundSizeInEpochs(),
33
+ slashingProposer.getLifetimeInRounds(),
34
+ slashingProposer.getSlashOffsetInRounds(),
35
+ slashingProposer.getSlashingAmounts(),
36
+ slashingProposer.getQuorumSize(),
37
+ rollup.getEpochDuration(),
38
+ rollup.getL1GenesisTime(),
39
+ rollup.getSlotDuration(),
40
+ rollup.getTargetCommitteeSize(),
41
+ ]);
42
+
43
+ const settings: TallySlasherSettings = {
44
+ slashingExecutionDelayInRounds: Number(slashingExecutionDelayInRounds),
45
+ slashingRoundSize: Number(slashingRoundSize),
46
+ slashingRoundSizeInEpochs: Number(slashingRoundSizeInEpochs),
47
+ slashingLifetimeInRounds: Number(slashingLifetimeInRounds),
48
+ slashingQuorumSize: Number(slashingQuorumSize),
49
+ epochDuration: Number(epochDuration),
50
+ l1GenesisTime: l1GenesisTime,
51
+ slotDuration: Number(slotDuration),
52
+ slashingOffsetInRounds: Number(slashingOffsetInRounds),
53
+ slashingAmounts,
54
+ targetCommitteeSize: Number(targetCommitteeSize),
55
+ };
56
+
57
+ return settings;
58
+ }
@@ -0,0 +1,2 @@
1
+ export { createSlasherFacade as createSlasher } from './create_facade.js';
2
+ export { getTallySlasherSettings } from './get_settings.js';
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ export * from './config.js';
2
+ export * from './watchers/epoch_prune_watcher.js';
3
+ export * from './watchers/attestations_block_watcher.js';
4
+ export * from './empire_slasher_client.js';
5
+ export * from './tally_slasher_client.js';
6
+ export * from './slash_offenses_collector.js';
7
+ export * from './slasher_client_interface.js';
8
+ export * from './factory/index.js';
9
+ export * from './watcher.js';
10
+ export * from '@aztec/stdlib/slashing';
@@ -0,0 +1,41 @@
1
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
2
+ import type { Offense, ProposerSlashAction, SlashPayloadRound } from '@aztec/stdlib/slashing';
3
+
4
+ import type { SlasherConfig } from './config.js';
5
+ import type { SlasherClientInterface } from './slasher_client_interface.js';
6
+
7
+ export class NullSlasherClient implements SlasherClientInterface {
8
+ constructor(private config: SlasherConfig) {}
9
+
10
+ public start(): Promise<void> {
11
+ return Promise.resolve();
12
+ }
13
+
14
+ public stop(): Promise<void> {
15
+ return Promise.resolve();
16
+ }
17
+
18
+ public getSlashPayloads(): Promise<SlashPayloadRound[]> {
19
+ return Promise.resolve([]);
20
+ }
21
+
22
+ public gatherOffensesForRound(_round?: bigint): Promise<Offense[]> {
23
+ return Promise.resolve([]);
24
+ }
25
+
26
+ public getPendingOffenses(): Promise<Offense[]> {
27
+ return Promise.resolve([]);
28
+ }
29
+
30
+ public updateConfig(config: Partial<SlasherConfig>): void {
31
+ this.config = { ...this.config, ...config };
32
+ }
33
+
34
+ public getProposerActions(_slotNumber: SlotNumber): Promise<ProposerSlashAction[]> {
35
+ return Promise.resolve([]);
36
+ }
37
+
38
+ public getConfig(): SlasherConfig {
39
+ return this.config;
40
+ }
41
+ }
@@ -0,0 +1,118 @@
1
+ import { createLogger } from '@aztec/foundation/log';
2
+ import type { Prettify } from '@aztec/foundation/types';
3
+ import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
4
+ import type { SlasherConfig } from '@aztec/stdlib/interfaces/server';
5
+ import { type Offense, type OffenseIdentifier, getSlotForOffense } from '@aztec/stdlib/slashing';
6
+
7
+ import type { SlasherOffensesStore } from './stores/offenses_store.js';
8
+ import { WANT_TO_SLASH_EVENT, type WantToSlashArgs, type Watcher } from './watcher.js';
9
+
10
+ export type SlashOffensesCollectorConfig = Prettify<Pick<SlasherConfig, 'slashGracePeriodL2Slots'>>;
11
+ export type SlashOffensesCollectorSettings = Prettify<
12
+ Pick<L1RollupConstants, 'epochDuration'> & { slashingAmounts: [bigint, bigint, bigint] | undefined }
13
+ >;
14
+
15
+ /**
16
+ * Collects and manages slashable offenses from watchers.
17
+ * This class handles the common logic for subscribing to slash watcher events,
18
+ * storing offenses, and retrieving pending offenses for slashing.
19
+ */
20
+ export class SlashOffensesCollector {
21
+ private readonly unwatchCallbacks: (() => void)[] = [];
22
+
23
+ constructor(
24
+ private readonly config: SlashOffensesCollectorConfig,
25
+ private readonly settings: SlashOffensesCollectorSettings,
26
+ private readonly watchers: Watcher[],
27
+ private readonly offensesStore: SlasherOffensesStore,
28
+ private readonly log = createLogger('slasher:offenses-collector'),
29
+ ) {}
30
+
31
+ public start() {
32
+ this.log.debug('Starting SlashOffensesCollector...');
33
+
34
+ // Subscribe to watchers WANT_TO_SLASH_EVENT
35
+ for (const watcher of this.watchers) {
36
+ const wantToSlashCallback = (args: WantToSlashArgs[]) =>
37
+ void this.handleWantToSlash(args).catch(err => this.log.error('Error handling wantToSlash', err));
38
+ watcher.on(WANT_TO_SLASH_EVENT, wantToSlashCallback);
39
+ this.unwatchCallbacks.push(() => watcher.removeListener(WANT_TO_SLASH_EVENT, wantToSlashCallback));
40
+ }
41
+
42
+ this.log.info('Started SlashOffensesCollector');
43
+ return Promise.resolve();
44
+ }
45
+
46
+ public stop() {
47
+ this.log.debug('Stopping SlashOffensesCollector...');
48
+
49
+ for (const unwatchCallback of this.unwatchCallbacks) {
50
+ unwatchCallback();
51
+ }
52
+
53
+ this.log.info('SlashOffensesCollector stopped');
54
+ return Promise.resolve();
55
+ }
56
+
57
+ /**
58
+ * Called when a slash watcher emits WANT_TO_SLASH_EVENT.
59
+ * Stores pending offenses instead of creating payloads immediately.
60
+ * @param args - the arguments from the watcher, including the validators, amounts, and offenses
61
+ */
62
+ public async handleWantToSlash(args: WantToSlashArgs[]) {
63
+ for (const arg of args) {
64
+ const pendingOffense: Offense = {
65
+ validator: arg.validator,
66
+ amount: arg.amount,
67
+ offenseType: arg.offenseType,
68
+ epochOrSlot: arg.epochOrSlot,
69
+ };
70
+
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);
78
+ continue;
79
+ }
80
+
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}`);
85
+ }
86
+ }
87
+
88
+ this.log.info(`Adding pending offense for validator ${arg.validator}`, pendingOffense);
89
+ await this.offensesStore.addPendingOffense(pendingOffense);
90
+ }
91
+ }
92
+
93
+ /**
94
+ * Triggered on a time basis when we enter a new slashing round.
95
+ * Clears expired offenses from stores.
96
+ */
97
+ public async handleNewRound(round: bigint) {
98
+ const cleared = await this.offensesStore.clearExpiredOffenses(round);
99
+ if (cleared && cleared > 0) {
100
+ this.log.debug(`Cleared ${cleared} expired offenses for round ${round}`);
101
+ }
102
+ }
103
+
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 */
114
+ private shouldSkipOffense(offense: Offense): boolean {
115
+ const offenseSlot = getSlotForOffense(offense, this.settings);
116
+ return offenseSlot < this.config.slashGracePeriodL2Slots;
117
+ }
118
+ }
@@ -0,0 +1,62 @@
1
+ import { SlotNumber } from '@aztec/foundation/branded-types';
2
+ import { createLogger } from '@aztec/foundation/log';
3
+ import type { DateProvider } from '@aztec/foundation/timer';
4
+ import type { Prettify } from '@aztec/foundation/types';
5
+ import { type L1RollupConstants, getSlotAtTimestamp } from '@aztec/stdlib/epoch-helpers';
6
+ import { getRoundForSlot } from '@aztec/stdlib/slashing';
7
+
8
+ export type SlashRoundMonitorSettings = Prettify<
9
+ Pick<L1RollupConstants, 'epochDuration' | 'l1GenesisTime' | 'slotDuration'> & { slashingRoundSize: number }
10
+ >;
11
+
12
+ export class SlashRoundMonitor {
13
+ private currentRound: bigint = 0n;
14
+ private intervalId: NodeJS.Timeout | undefined = undefined;
15
+ private handler: ((round: bigint) => Promise<void>) | undefined = undefined;
16
+
17
+ constructor(
18
+ private settings: SlashRoundMonitorSettings,
19
+ private dateProvider: DateProvider,
20
+ private log = createLogger('slasher:round-monitor'),
21
+ ) {}
22
+
23
+ public start() {
24
+ // Check for round changes
25
+ this.currentRound = this.getCurrentRound().round;
26
+ this.intervalId = setInterval(() => {
27
+ const round = this.getCurrentRound().round;
28
+ if (round !== this.currentRound) {
29
+ this.currentRound = round;
30
+ if (this.handler) {
31
+ void this.handler(round).catch(err => this.log.error('Error handling new round', err));
32
+ }
33
+ }
34
+ }, 500);
35
+ }
36
+
37
+ public stop() {
38
+ if (this.intervalId) {
39
+ clearInterval(this.intervalId);
40
+ this.intervalId = undefined;
41
+ }
42
+ }
43
+
44
+ public listenToNewRound(handler: (round: bigint) => Promise<void>): () => void {
45
+ this.handler = handler;
46
+ return () => {
47
+ this.handler = undefined;
48
+ };
49
+ }
50
+
51
+ /** Returns the slashing round number and the voting slot within the round based on the L2 chain slot */
52
+ public getRoundForSlot(slotNumber: SlotNumber): { round: bigint; votingSlot: SlotNumber } {
53
+ return getRoundForSlot(slotNumber, this.settings);
54
+ }
55
+
56
+ /** Returns the current slashing round and voting slot within the round */
57
+ public getCurrentRound(): { round: bigint; votingSlot: SlotNumber } {
58
+ const now = this.dateProvider.nowInSeconds();
59
+ const currentSlot = getSlotAtTimestamp(BigInt(now), this.settings);
60
+ return this.getRoundForSlot(currentSlot);
61
+ }
62
+ }
@@ -0,0 +1,101 @@
1
+ import { EpochCache } from '@aztec/epoch-cache';
2
+ import type { ViemClient } from '@aztec/ethereum';
3
+ import { RollupContract } from '@aztec/ethereum/contracts';
4
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
5
+ import { EthAddress } from '@aztec/foundation/eth-address';
6
+ import { createLogger } from '@aztec/foundation/log';
7
+ import { DateProvider } from '@aztec/foundation/timer';
8
+ import type { DataStoreConfig } from '@aztec/kv-store/config';
9
+ import { AztecLMDBStoreV2 } from '@aztec/kv-store/lmdb-v2';
10
+ import type { SlasherConfig } from '@aztec/stdlib/interfaces/server';
11
+ import type { Offense, ProposerSlashAction, SlashPayloadRound } from '@aztec/stdlib/slashing';
12
+
13
+ import { createSlasherImplementation } from './factory/create_implementation.js';
14
+ import type { SlasherClientInterface } from './slasher_client_interface.js';
15
+ import type { Watcher } from './watcher.js';
16
+
17
+ /**
18
+ * Facade for the Slasher client. This class forwards all requests to the actual Slasher client implementation.
19
+ * This class also monitors via the rollup contract when the underlying slasher proposer contract changes, and when it
20
+ * does, it stops the current slasher client, recreates a new one with the new contract address, and starts it again.
21
+ */
22
+ export class SlasherClientFacade implements SlasherClientInterface {
23
+ private client: SlasherClientInterface | undefined;
24
+ private unwatch: (() => void) | undefined;
25
+
26
+ constructor(
27
+ private config: SlasherConfig & DataStoreConfig & { ethereumSlotDuration: number },
28
+ private rollup: RollupContract,
29
+ private l1Client: ViemClient,
30
+ private slashFactoryAddress: EthAddress | undefined,
31
+ private watchers: Watcher[],
32
+ private epochCache: EpochCache,
33
+ private dateProvider: DateProvider,
34
+ private kvStore: AztecLMDBStoreV2,
35
+ private logger = createLogger('slasher'),
36
+ ) {}
37
+
38
+ public async start(): Promise<void> {
39
+ this.client = await this.createSlasherClient();
40
+ await this.client?.start();
41
+
42
+ this.unwatch = this.rollup.listenToSlasherChanged(() => {
43
+ void this.handleSlasherChange().catch(error => {
44
+ this.logger.error('Error handling slasher change', error);
45
+ });
46
+ });
47
+ }
48
+
49
+ public async stop(): Promise<void> {
50
+ await this.client?.stop();
51
+ this.unwatch?.();
52
+ this.unwatch = undefined;
53
+ }
54
+
55
+ public getConfig(): SlasherConfig {
56
+ return this.config;
57
+ }
58
+
59
+ public updateConfig(config: Partial<SlasherConfig>): void {
60
+ this.config = { ...this.config, ...config };
61
+ this.client?.updateConfig(config);
62
+ this.watchers.forEach(watcher => watcher.updateConfig?.(config));
63
+ }
64
+
65
+ public getSlashPayloads(): Promise<SlashPayloadRound[]> {
66
+ return this.client?.getSlashPayloads() ?? Promise.reject(new Error('Slasher client not initialized'));
67
+ }
68
+
69
+ public gatherOffensesForRound(round?: bigint): Promise<Offense[]> {
70
+ return this.client?.gatherOffensesForRound(round) ?? Promise.reject(new Error('Slasher client not initialized'));
71
+ }
72
+
73
+ public getPendingOffenses(): Promise<Offense[]> {
74
+ return this.client?.getPendingOffenses() ?? Promise.reject(new Error('Slasher client not initialized'));
75
+ }
76
+
77
+ public getProposerActions(slotNumber: SlotNumber): Promise<ProposerSlashAction[]> {
78
+ return this.client?.getProposerActions(slotNumber) ?? Promise.reject(new Error('Slasher client not initialized'));
79
+ }
80
+
81
+ private createSlasherClient() {
82
+ return createSlasherImplementation(
83
+ this.config,
84
+ this.rollup,
85
+ this.l1Client,
86
+ this.slashFactoryAddress,
87
+ this.watchers,
88
+ this.epochCache,
89
+ this.dateProvider,
90
+ this.kvStore,
91
+ this.logger,
92
+ );
93
+ }
94
+
95
+ private async handleSlasherChange() {
96
+ this.logger.warn('Slasher contract changed, recreating slasher client');
97
+ await this.client?.stop();
98
+ this.client = await this.createSlasherClient();
99
+ await this.client?.start();
100
+ }
101
+ }
@@ -0,0 +1,46 @@
1
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
2
+ import type { SlasherConfig } from '@aztec/stdlib/interfaces/server';
3
+ import type { Offense, ProposerSlashAction, SlashPayloadRound } from '@aztec/stdlib/slashing';
4
+
5
+ /**
6
+ * Common interface for slasher clients used by the Aztec node.
7
+ * Both Empire and Consensus slasher clients implement this interface.
8
+ */
9
+ export interface SlasherClientInterface {
10
+ /** Start the slasher client */
11
+ start(): Promise<void>;
12
+
13
+ /** Stop the slasher client */
14
+ stop(): Promise<void>;
15
+
16
+ /**
17
+ * Get slash payloads for the Empire model.
18
+ * The Consensus model should throw an error when this is called.
19
+ */
20
+ getSlashPayloads(): Promise<SlashPayloadRound[]>;
21
+
22
+ /**
23
+ * Gather offenses for a given round, defaults to current.
24
+ * Used by both Empire and Consensus models.
25
+ */
26
+ gatherOffensesForRound(round?: bigint): Promise<Offense[]>;
27
+
28
+ /** Returns all pending offenses */
29
+ getPendingOffenses(): Promise<Offense[]>;
30
+
31
+ /**
32
+ * Update the configuration.
33
+ * Used by both Empire and Consensus models.
34
+ */
35
+ updateConfig(config: Partial<SlasherConfig>): void;
36
+
37
+ /**
38
+ * Get the actions the proposer should take for slashing.
39
+ * @param slotNumber - The current slot number
40
+ * @returns The actions to take
41
+ */
42
+ getProposerActions(slotNumber: SlotNumber): Promise<ProposerSlashAction[]>;
43
+
44
+ /** Returns the current config */
45
+ getConfig(): SlasherConfig;
46
+ }