@aztec/slasher 0.0.1-commit.e3c1de76 → 0.0.1-commit.e588bc7e5

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 (37) hide show
  1. package/README.md +24 -14
  2. package/dest/config.d.ts +1 -1
  3. package/dest/config.d.ts.map +1 -1
  4. package/dest/config.js +13 -1
  5. package/dest/factory/create_facade.d.ts +3 -3
  6. package/dest/factory/create_facade.d.ts.map +1 -1
  7. package/dest/factory/create_facade.js +25 -2
  8. package/dest/factory/create_implementation.d.ts +4 -3
  9. package/dest/factory/create_implementation.d.ts.map +1 -1
  10. package/dest/factory/create_implementation.js +11 -7
  11. package/dest/factory/get_settings.d.ts +2 -2
  12. package/dest/factory/get_settings.d.ts.map +1 -1
  13. package/dest/generated/slasher-defaults.d.ts +4 -2
  14. package/dest/generated/slasher-defaults.d.ts.map +1 -1
  15. package/dest/generated/slasher-defaults.js +3 -1
  16. package/dest/slash_offenses_collector.d.ts +5 -2
  17. package/dest/slash_offenses_collector.d.ts.map +1 -1
  18. package/dest/slash_offenses_collector.js +3 -7
  19. package/dest/slasher_client_facade.d.ts +4 -3
  20. package/dest/slasher_client_facade.d.ts.map +1 -1
  21. package/dest/slasher_client_facade.js +4 -2
  22. package/dest/tally_slasher_client.d.ts +2 -2
  23. package/dest/tally_slasher_client.d.ts.map +1 -1
  24. package/dest/tally_slasher_client.js +6 -6
  25. package/dest/watchers/epoch_prune_watcher.d.ts +6 -5
  26. package/dest/watchers/epoch_prune_watcher.d.ts.map +1 -1
  27. package/dest/watchers/epoch_prune_watcher.js +47 -26
  28. package/package.json +9 -9
  29. package/src/config.ts +15 -1
  30. package/src/factory/create_facade.ts +32 -3
  31. package/src/factory/create_implementation.ts +29 -4
  32. package/src/factory/get_settings.ts +2 -2
  33. package/src/generated/slasher-defaults.ts +3 -1
  34. package/src/slash_offenses_collector.ts +9 -8
  35. package/src/slasher_client_facade.ts +3 -1
  36. package/src/tally_slasher_client.ts +8 -7
  37. package/src/watchers/epoch_prune_watcher.ts +61 -26
@@ -1,6 +1,6 @@
1
1
  import { EpochCache } from '@aztec/epoch-cache';
2
- import { BlockNumber, CheckpointNumber, EpochNumber } from '@aztec/foundation/branded-types';
3
- import { merge, pick } from '@aztec/foundation/collection';
2
+ import { BlockNumber, EpochNumber } from '@aztec/foundation/branded-types';
3
+ import { chunkBy, merge, pick } from '@aztec/foundation/collection';
4
4
  import type { Fr } from '@aztec/foundation/curves/bn254';
5
5
  import { type Logger, createLogger } from '@aztec/foundation/log';
6
6
  import {
@@ -12,6 +12,7 @@ import {
12
12
  } from '@aztec/stdlib/block';
13
13
  import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
14
14
  import type {
15
+ ICheckpointBlockBuilder,
15
16
  ICheckpointsBuilder,
16
17
  ITxProvider,
17
18
  MerkleTreeWriteOperations,
@@ -106,7 +107,7 @@ export class EpochPruneWatcher extends (EventEmitter as new () => WatcherEmitter
106
107
  { blocks: epochBlocks.map(b => b.toBlockInfo()) },
107
108
  );
108
109
 
109
- await this.validateBlocks(epochBlocks);
110
+ await this.validateBlocks(epochBlocks, epochNumber);
110
111
  this.log.info(`Pruned epoch ${epochNumber} was valid. Want to slash committee for not having it proven.`);
111
112
  await this.emitSlashForEpoch(OffenseType.VALID_EPOCH_PRUNED, epochNumber);
112
113
  } catch (error) {
@@ -121,19 +122,32 @@ export class EpochPruneWatcher extends (EventEmitter as new () => WatcherEmitter
121
122
  }
122
123
  }
123
124
 
124
- public async validateBlocks(blocks: L2Block[]): Promise<void> {
125
+ public async validateBlocks(blocks: L2Block[], epochNumber: EpochNumber): Promise<void> {
125
126
  if (blocks.length === 0) {
126
127
  return;
127
128
  }
128
129
 
129
- let previousCheckpointOutHashes: Fr[] = [];
130
- const fork = await this.checkpointsBuilder.getFork(BlockNumber(blocks[0].header.globalVariables.blockNumber - 1));
130
+ // Sort blocks by block number and group by checkpoint
131
+ const sortedBlocks = [...blocks].sort((a, b) => a.number - b.number);
132
+ const blocksByCheckpoint = chunkBy(sortedBlocks, b => b.checkpointNumber);
133
+
134
+ // Get prior checkpoints in the epoch (in case this was a partial prune) to extract the out hashes
135
+ const priorCheckpointOutHashes = (await this.l2BlockSource.getCheckpointsDataForEpoch(epochNumber))
136
+ .filter(c => c.checkpointNumber < sortedBlocks[0].checkpointNumber)
137
+ .map(c => c.checkpointOutHash);
138
+ let previousCheckpointOutHashes: Fr[] = [...priorCheckpointOutHashes];
139
+
140
+ const fork = await this.checkpointsBuilder.getFork(
141
+ BlockNumber(sortedBlocks[0].header.globalVariables.blockNumber - 1),
142
+ );
131
143
  try {
132
- for (const block of blocks) {
133
- await this.validateBlock(block, previousCheckpointOutHashes, fork);
144
+ for (const checkpointBlocks of blocksByCheckpoint) {
145
+ await this.validateCheckpoint(checkpointBlocks, previousCheckpointOutHashes, fork);
134
146
 
135
- // TODO(mbps): This assumes one block per checkpoint, which is only true for now.
136
- const checkpointOutHash = computeCheckpointOutHash([block.body.txEffects.map(tx => tx.l2ToL1Msgs)]);
147
+ // Compute checkpoint out hash from all blocks in this checkpoint
148
+ const checkpointOutHash = computeCheckpointOutHash(
149
+ checkpointBlocks.map(b => b.body.txEffects.map(tx => tx.l2ToL1Msgs)),
150
+ );
137
151
  previousCheckpointOutHashes = [...previousCheckpointOutHashes, checkpointOutHash];
138
152
  }
139
153
  } finally {
@@ -141,45 +155,66 @@ export class EpochPruneWatcher extends (EventEmitter as new () => WatcherEmitter
141
155
  }
142
156
  }
143
157
 
144
- public async validateBlock(
145
- blockFromL1: L2Block,
158
+ private async validateCheckpoint(
159
+ checkpointBlocks: L2Block[],
146
160
  previousCheckpointOutHashes: Fr[],
147
161
  fork: MerkleTreeWriteOperations,
148
162
  ): Promise<void> {
149
- this.log.debug(`Validating pruned block ${blockFromL1.header.globalVariables.blockNumber}`);
150
- const txHashes = blockFromL1.body.txEffects.map(txEffect => txEffect.txHash);
151
- // We load txs from the mempool directly, since the TxCollector running in the background has already been
152
- // trying to fetch them from nodes or via reqresp. If we haven't managed to collect them by now,
153
- // it's likely that they are not available in the network at all.
154
- const { txs, missingTxs } = await this.txProvider.getAvailableTxs(txHashes);
155
-
156
- if (missingTxs && missingTxs.length > 0) {
157
- throw new TransactionsNotAvailableError(missingTxs);
158
- }
163
+ const checkpointNumber = checkpointBlocks[0].checkpointNumber;
164
+ this.log.debug(`Validating pruned checkpoint ${checkpointNumber} with ${checkpointBlocks.length} blocks`);
159
165
 
160
- const checkpointNumber = CheckpointNumber.fromBlockNumber(blockFromL1.number);
166
+ // Get L1ToL2Messages once for the entire checkpoint
161
167
  const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
162
- const gv = blockFromL1.header.globalVariables;
168
+
169
+ // Build checkpoint constants from first block's global variables
170
+ const gv = checkpointBlocks[0].header.globalVariables;
163
171
  const constants: CheckpointGlobalVariables = {
164
172
  chainId: gv.chainId,
165
173
  version: gv.version,
166
174
  slotNumber: gv.slotNumber,
175
+ timestamp: gv.timestamp,
167
176
  coinbase: gv.coinbase,
168
177
  feeRecipient: gv.feeRecipient,
169
178
  gasFees: gv.gasFees,
170
179
  };
171
180
 
172
- // Use checkpoint builder to validate the block
181
+ // Start checkpoint builder once for all blocks in this checkpoint
173
182
  const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(
174
183
  checkpointNumber,
175
184
  constants,
185
+ 0n, // feeAssetPriceModifier is not used for validation of the checkpoint content
176
186
  l1ToL2Messages,
177
187
  previousCheckpointOutHashes,
178
188
  fork,
179
189
  this.log.getBindings(),
180
190
  );
181
191
 
182
- const { block, failedTxs, numTxs } = await checkpointBuilder.buildBlock(txs, gv.blockNumber, gv.timestamp, {});
192
+ // Validate all blocks in the checkpoint sequentially
193
+ for (const block of checkpointBlocks) {
194
+ await this.validateBlockInCheckpoint(block, checkpointBuilder);
195
+ }
196
+ }
197
+
198
+ private async validateBlockInCheckpoint(
199
+ blockFromL1: L2Block,
200
+ checkpointBuilder: ICheckpointBlockBuilder,
201
+ ): Promise<void> {
202
+ this.log.debug(`Validating pruned block ${blockFromL1.header.globalVariables.blockNumber}`);
203
+ const txHashes = blockFromL1.body.txEffects.map(txEffect => txEffect.txHash);
204
+ // We load txs from the mempool directly, since the TxCollector running in the background has already been
205
+ // trying to fetch them from nodes or via reqresp. If we haven't managed to collect them by now,
206
+ // it's likely that they are not available in the network at all.
207
+ const { txs, missingTxs } = await this.txProvider.getAvailableTxs(txHashes);
208
+
209
+ if (missingTxs && missingTxs.length > 0) {
210
+ throw new TransactionsNotAvailableError(missingTxs);
211
+ }
212
+
213
+ const gv = blockFromL1.header.globalVariables;
214
+ const { block, failedTxs, numTxs } = await checkpointBuilder.buildBlock(txs, gv.blockNumber, gv.timestamp, {
215
+ isBuildingProposal: false,
216
+ minValidTxs: 0,
217
+ });
183
218
 
184
219
  if (numTxs !== txs.length) {
185
220
  // This should be detected by state mismatch, but this makes it easier to debug.