@aztec/slasher 0.0.1-commit.24de95ac

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 +189 -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 +16 -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 +29 -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 +43 -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 +38 -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 +129 -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 +135 -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 +89 -0
  63. package/src/config.ts +157 -0
  64. package/src/empire_slasher_client.ts +656 -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 +40 -0
  71. package/src/slash_offenses_collector.ts +118 -0
  72. package/src/slash_round_monitor.ts +61 -0
  73. package/src/slasher_client_facade.ts +100 -0
  74. package/src/slasher_client_interface.ts +45 -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 +436 -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 +180 -0
  82. package/src/watchers/epoch_prune_watcher.ts +192 -0
@@ -0,0 +1,192 @@
1
+ import { EpochCache } from '@aztec/epoch-cache';
2
+ import { merge, pick } from '@aztec/foundation/collection';
3
+ import { type Logger, createLogger } from '@aztec/foundation/log';
4
+ import {
5
+ EthAddress,
6
+ L2Block,
7
+ type L2BlockPruneEvent,
8
+ type L2BlockSourceEventEmitter,
9
+ L2BlockSourceEvents,
10
+ } from '@aztec/stdlib/block';
11
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
12
+ import type {
13
+ IFullNodeBlockBuilder,
14
+ ITxProvider,
15
+ MerkleTreeWriteOperations,
16
+ SlasherConfig,
17
+ } from '@aztec/stdlib/interfaces/server';
18
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
19
+ import { OffenseType, getOffenseTypeName } from '@aztec/stdlib/slashing';
20
+ import {
21
+ ReExFailedTxsError,
22
+ ReExStateMismatchError,
23
+ TransactionsNotAvailableError,
24
+ ValidatorError,
25
+ } from '@aztec/stdlib/validators';
26
+
27
+ import EventEmitter from 'node:events';
28
+
29
+ import { WANT_TO_SLASH_EVENT, type WantToSlashArgs, type Watcher, type WatcherEmitter } from '../watcher.js';
30
+
31
+ const EpochPruneWatcherPenaltiesConfigKeys = ['slashPrunePenalty', 'slashDataWithholdingPenalty'] as const;
32
+
33
+ type EpochPruneWatcherPenalties = Pick<SlasherConfig, (typeof EpochPruneWatcherPenaltiesConfigKeys)[number]>;
34
+
35
+ /**
36
+ * This watcher is responsible for detecting chain prunes and creating slashing arguments for the committee.
37
+ * It only wants to slash if:
38
+ * - the transactions are not available
39
+ * - OR the archive roots match when re-building all the blocks in the epoch (i.e. the epoch *could* have been proven)
40
+ */
41
+ export class EpochPruneWatcher extends (EventEmitter as new () => WatcherEmitter) implements Watcher {
42
+ private log: Logger = createLogger('epoch-prune-watcher');
43
+
44
+ // Store bound function reference for proper listener removal
45
+ private boundHandlePruneL2Blocks = this.handlePruneL2Blocks.bind(this);
46
+
47
+ private penalties: EpochPruneWatcherPenalties;
48
+
49
+ constructor(
50
+ private l2BlockSource: L2BlockSourceEventEmitter,
51
+ private l1ToL2MessageSource: L1ToL2MessageSource,
52
+ private epochCache: EpochCache,
53
+ private txProvider: Pick<ITxProvider, 'getAvailableTxs'>,
54
+ private blockBuilder: IFullNodeBlockBuilder,
55
+ penalties: EpochPruneWatcherPenalties,
56
+ ) {
57
+ super();
58
+ this.penalties = pick(penalties, ...EpochPruneWatcherPenaltiesConfigKeys);
59
+ this.log.verbose(
60
+ `EpochPruneWatcher initialized with penalties: valid epoch pruned=${penalties.slashPrunePenalty} data withholding=${penalties.slashDataWithholdingPenalty}`,
61
+ );
62
+ }
63
+
64
+ public start() {
65
+ this.l2BlockSource.on(L2BlockSourceEvents.L2PruneDetected, this.boundHandlePruneL2Blocks);
66
+ return Promise.resolve();
67
+ }
68
+
69
+ public stop() {
70
+ this.l2BlockSource.removeListener(L2BlockSourceEvents.L2PruneDetected, this.boundHandlePruneL2Blocks);
71
+ return Promise.resolve();
72
+ }
73
+
74
+ public updateConfig(config: Partial<SlasherConfig>): void {
75
+ this.penalties = merge(this.penalties, pick(config, ...EpochPruneWatcherPenaltiesConfigKeys));
76
+ this.log.verbose('EpochPruneWatcher config updated', this.penalties);
77
+ }
78
+
79
+ private handlePruneL2Blocks(event: L2BlockPruneEvent): void {
80
+ const { blocks, epochNumber } = event;
81
+ void this.processPruneL2Blocks(blocks, epochNumber).catch(err =>
82
+ this.log.error('Error processing pruned L2 blocks', err, { epochNumber }),
83
+ );
84
+ }
85
+
86
+ private async emitSlashForEpoch(offense: OffenseType, epochNumber: bigint): Promise<void> {
87
+ const validators = await this.getValidatorsForEpoch(epochNumber);
88
+ if (validators.length === 0) {
89
+ this.log.warn(`No validators found for epoch ${epochNumber} (cannot slash for ${getOffenseTypeName(offense)})`);
90
+ return;
91
+ }
92
+ const args = this.validatorsToSlashingArgs(validators, offense, BigInt(epochNumber));
93
+ this.log.verbose(`Created slash for ${getOffenseTypeName(offense)} at epoch ${epochNumber}`, args);
94
+ this.emit(WANT_TO_SLASH_EVENT, args);
95
+ }
96
+
97
+ private async processPruneL2Blocks(blocks: L2Block[], epochNumber: bigint): Promise<void> {
98
+ try {
99
+ const l1Constants = this.epochCache.getL1Constants();
100
+ const epochBlocks = blocks.filter(b => getEpochAtSlot(b.slot, l1Constants) === epochNumber);
101
+ this.log.info(
102
+ `Detected chain prune. Validating epoch ${epochNumber} with blocks ${epochBlocks[0]?.number} to ${epochBlocks[epochBlocks.length - 1]?.number}.`,
103
+ { blocks: epochBlocks.map(b => b.toBlockInfo()) },
104
+ );
105
+
106
+ await this.validateBlocks(epochBlocks);
107
+ this.log.info(`Pruned epoch ${epochNumber} was valid. Want to slash committee for not having it proven.`);
108
+ await this.emitSlashForEpoch(OffenseType.VALID_EPOCH_PRUNED, epochNumber);
109
+ } catch (error) {
110
+ if (error instanceof TransactionsNotAvailableError) {
111
+ this.log.info(`Data for pruned epoch ${epochNumber} was not available. Will want to slash.`, {
112
+ message: error.message,
113
+ });
114
+ await this.emitSlashForEpoch(OffenseType.DATA_WITHHOLDING, epochNumber);
115
+ } else {
116
+ this.log.error(`Error while validating pruned epoch ${epochNumber}. Will not want to slash.`, error);
117
+ }
118
+ }
119
+ }
120
+
121
+ public async validateBlocks(blocks: L2Block[]): Promise<void> {
122
+ if (blocks.length === 0) {
123
+ return;
124
+ }
125
+ const fork = await this.blockBuilder.getFork(blocks[0].header.globalVariables.blockNumber - 1);
126
+ try {
127
+ for (const block of blocks) {
128
+ await this.validateBlock(block, fork);
129
+ }
130
+ } finally {
131
+ await fork.close();
132
+ }
133
+ }
134
+
135
+ public async validateBlock(blockFromL1: L2Block, fork: MerkleTreeWriteOperations): Promise<void> {
136
+ this.log.debug(`Validating pruned block ${blockFromL1.header.globalVariables.blockNumber}`);
137
+ const txHashes = blockFromL1.body.txEffects.map(txEffect => txEffect.txHash);
138
+ // We load txs from the mempool directly, since the TxCollector running in the background has already been
139
+ // trying to fetch them from nodes or via reqresp. If we haven't managed to collect them by now,
140
+ // it's likely that they are not available in the network at all.
141
+ const { txs, missingTxs } = await this.txProvider.getAvailableTxs(txHashes);
142
+
143
+ if (missingTxs && missingTxs.length > 0) {
144
+ throw new TransactionsNotAvailableError(missingTxs);
145
+ }
146
+
147
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(blockFromL1.number);
148
+ const { block, failedTxs, numTxs } = await this.blockBuilder.buildBlock(
149
+ txs,
150
+ l1ToL2Messages,
151
+ blockFromL1.header.globalVariables,
152
+ {},
153
+ fork,
154
+ );
155
+ if (numTxs !== txs.length) {
156
+ // This should be detected by state mismatch, but this makes it easier to debug.
157
+ throw new ValidatorError(`Built block with ${numTxs} txs, expected ${txs.length}`);
158
+ }
159
+ if (failedTxs.length > 0) {
160
+ throw new ReExFailedTxsError(failedTxs.length);
161
+ }
162
+ if (!block.archive.root.equals(blockFromL1.archive.root)) {
163
+ throw new ReExStateMismatchError(blockFromL1.archive.root, block.archive.root);
164
+ }
165
+ }
166
+
167
+ private async getValidatorsForEpoch(epochNumber: bigint): Promise<EthAddress[]> {
168
+ const { committee } = await this.epochCache.getCommitteeForEpoch(epochNumber);
169
+ if (!committee) {
170
+ this.log.trace(`No committee found for epoch ${epochNumber}`);
171
+ return [];
172
+ }
173
+ return committee;
174
+ }
175
+
176
+ private validatorsToSlashingArgs(
177
+ validators: EthAddress[],
178
+ offenseType: OffenseType,
179
+ epochOrSlot: bigint,
180
+ ): WantToSlashArgs[] {
181
+ const penalty =
182
+ offenseType === OffenseType.DATA_WITHHOLDING
183
+ ? this.penalties.slashDataWithholdingPenalty
184
+ : this.penalties.slashPrunePenalty;
185
+ return validators.map(v => ({
186
+ validator: v,
187
+ amount: penalty,
188
+ offenseType,
189
+ epochOrSlot,
190
+ }));
191
+ }
192
+ }