@aztec/validator-client 0.0.1-commit.29c6b1a3 → 0.0.1-commit.2c85e299c

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 (50) hide show
  1. package/README.md +63 -18
  2. package/dest/block_proposal_handler.d.ts +3 -3
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +63 -58
  5. package/dest/checkpoint_builder.d.ts +20 -11
  6. package/dest/checkpoint_builder.d.ts.map +1 -1
  7. package/dest/checkpoint_builder.js +106 -38
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +26 -1
  11. package/dest/duties/validation_service.d.ts +2 -2
  12. package/dest/duties/validation_service.d.ts.map +1 -1
  13. package/dest/duties/validation_service.js +5 -11
  14. package/dest/factory.d.ts +1 -1
  15. package/dest/factory.d.ts.map +1 -1
  16. package/dest/factory.js +2 -1
  17. package/dest/index.d.ts +1 -2
  18. package/dest/index.d.ts.map +1 -1
  19. package/dest/index.js +0 -1
  20. package/dest/key_store/ha_key_store.d.ts +1 -1
  21. package/dest/key_store/ha_key_store.d.ts.map +1 -1
  22. package/dest/key_store/ha_key_store.js +2 -2
  23. package/dest/metrics.d.ts +12 -3
  24. package/dest/metrics.d.ts.map +1 -1
  25. package/dest/metrics.js +46 -5
  26. package/dest/validator.d.ts +35 -8
  27. package/dest/validator.d.ts.map +1 -1
  28. package/dest/validator.js +195 -33
  29. package/package.json +19 -19
  30. package/src/block_proposal_handler.ts +76 -76
  31. package/src/checkpoint_builder.ts +143 -37
  32. package/src/config.ts +26 -1
  33. package/src/duties/validation_service.ts +11 -10
  34. package/src/factory.ts +1 -0
  35. package/src/index.ts +0 -1
  36. package/src/key_store/ha_key_store.ts +2 -2
  37. package/src/metrics.ts +63 -6
  38. package/src/validator.ts +244 -38
  39. package/dest/tx_validator/index.d.ts +0 -3
  40. package/dest/tx_validator/index.d.ts.map +0 -1
  41. package/dest/tx_validator/index.js +0 -2
  42. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  43. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  44. package/dest/tx_validator/nullifier_cache.js +0 -24
  45. package/dest/tx_validator/tx_validator_factory.d.ts +0 -18
  46. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  47. package/dest/tx_validator/tx_validator_factory.js +0 -54
  48. package/src/tx_validator/index.ts +0 -2
  49. package/src/tx_validator/nullifier_cache.ts +0 -30
  50. package/src/tx_validator/tx_validator_factory.ts +0 -135
@@ -1,7 +1,7 @@
1
1
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
2
  import type { EpochCache } from '@aztec/epoch-cache';
3
3
  import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
- import { chunkBy } from '@aztec/foundation/collection';
4
+ import { pick } from '@aztec/foundation/collection';
5
5
  import { Fr } from '@aztec/foundation/curves/bn254';
6
6
  import { TimeoutError } from '@aztec/foundation/error';
7
7
  import { createLogger } from '@aztec/foundation/log';
@@ -9,16 +9,13 @@ import { retryUntil } from '@aztec/foundation/retry';
9
9
  import { DateProvider, Timer } from '@aztec/foundation/timer';
10
10
  import type { P2P, PeerId } from '@aztec/p2p';
11
11
  import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
12
- import type { L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
12
+ import type { BlockData, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
13
13
  import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
14
+ import { Gas } from '@aztec/stdlib/gas';
14
15
  import type { ITxProvider, ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
15
- import {
16
- type L1ToL2MessageSource,
17
- computeCheckpointOutHash,
18
- computeInHashFromL1ToL2Messages,
19
- } from '@aztec/stdlib/messaging';
16
+ import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
20
17
  import type { BlockProposal } from '@aztec/stdlib/p2p';
21
- import { BlockHeader, type CheckpointGlobalVariables, type FailedTx, type Tx } from '@aztec/stdlib/tx';
18
+ import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
22
19
  import {
23
20
  ReExFailedTxsError,
24
21
  ReExStateMismatchError,
@@ -92,25 +89,28 @@ export class BlockProposalHandler {
92
89
  this.tracer = telemetry.getTracer('BlockProposalHandler');
93
90
  }
94
91
 
95
- registerForReexecution(p2pClient: P2P): BlockProposalHandler {
96
- // Non-validator handler that re-executes for monitoring but does not attest.
92
+ register(p2pClient: P2P, shouldReexecute: boolean): BlockProposalHandler {
93
+ // Non-validator handler that processes or re-executes for monitoring but does not attest.
97
94
  // Returns boolean indicating whether the proposal was valid.
98
95
  const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
99
96
  try {
100
- const result = await this.handleBlockProposal(proposal, proposalSender, true);
97
+ const { slotNumber, blockNumber } = proposal;
98
+ const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
101
99
  if (result.isValid) {
102
- this.log.info(`Non-validator reexecution completed for slot ${proposal.slotNumber}`, {
100
+ this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
103
101
  blockNumber: result.blockNumber,
102
+ slotNumber,
104
103
  reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
105
104
  totalManaUsed: result.reexecutionResult?.totalManaUsed,
106
105
  numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
106
+ reexecuted: shouldReexecute,
107
107
  });
108
108
  return true;
109
109
  } else {
110
- this.log.warn(`Non-validator reexecution failed for slot ${proposal.slotNumber}`, {
111
- blockNumber: result.blockNumber,
112
- reason: result.reason,
113
- });
110
+ this.log.warn(
111
+ `Non-validator block proposal ${blockNumber} at slot ${slotNumber} failed processing with ${result.reason}`,
112
+ { blockNumber: result.blockNumber, slotNumber, reason: result.reason },
113
+ );
114
114
  return false;
115
115
  }
116
116
  } catch (error) {
@@ -153,16 +153,16 @@ export class BlockProposalHandler {
153
153
  }
154
154
 
155
155
  // Check that the parent proposal is a block we know, otherwise reexecution would fail
156
- const parentBlockHeader = await this.getParentBlock(proposal);
157
- if (parentBlockHeader === undefined) {
156
+ const parentBlock = await this.getParentBlock(proposal);
157
+ if (parentBlock === undefined) {
158
158
  this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
159
159
  return { isValid: false, reason: 'parent_block_not_found' };
160
160
  }
161
161
 
162
162
  // Check that the parent block's slot is not greater than the proposal's slot.
163
- if (parentBlockHeader !== 'genesis' && parentBlockHeader.getSlot() > slotNumber) {
163
+ if (parentBlock !== 'genesis' && parentBlock.header.getSlot() > slotNumber) {
164
164
  this.log.warn(`Parent block slot is greater than proposal slot, skipping processing`, {
165
- parentBlockSlot: parentBlockHeader.getSlot().toString(),
165
+ parentBlockSlot: parentBlock.header.getSlot().toString(),
166
166
  proposalSlot: slotNumber.toString(),
167
167
  ...proposalInfo,
168
168
  });
@@ -171,9 +171,9 @@ export class BlockProposalHandler {
171
171
 
172
172
  // Compute the block number based on the parent block
173
173
  const blockNumber =
174
- parentBlockHeader === 'genesis'
174
+ parentBlock === 'genesis'
175
175
  ? BlockNumber(INITIAL_L2_BLOCK_NUM)
176
- : BlockNumber(parentBlockHeader.getBlockNumber() + 1);
176
+ : BlockNumber(parentBlock.header.getBlockNumber() + 1);
177
177
 
178
178
  // Check that this block number does not exist already
179
179
  const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
@@ -189,8 +189,17 @@ export class BlockProposalHandler {
189
189
  deadline: this.getReexecutionDeadline(slotNumber, config),
190
190
  });
191
191
 
192
+ // If reexecution is disabled, bail. We are just interested in triggering tx collection.
193
+ if (!shouldReexecute) {
194
+ this.log.info(
195
+ `Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
196
+ proposalInfo,
197
+ );
198
+ return { isValid: true, blockNumber };
199
+ }
200
+
192
201
  // Compute the checkpoint number for this block and validate checkpoint consistency
193
- const checkpointResult = await this.computeCheckpointNumber(proposal, parentBlockHeader, proposalInfo);
202
+ const checkpointResult = this.computeCheckpointNumber(proposal, parentBlock, proposalInfo);
194
203
  if (checkpointResult.reason) {
195
204
  return { isValid: false, blockNumber, reason: checkpointResult.reason };
196
205
  }
@@ -215,36 +224,28 @@ export class BlockProposalHandler {
215
224
  return { isValid: false, blockNumber, reason: 'txs_not_available' };
216
225
  }
217
226
 
227
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
228
+ const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
229
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
230
+ .filter(c => c.checkpointNumber < checkpointNumber)
231
+ .map(c => c.checkpointOutHash);
232
+
218
233
  // Try re-executing the transactions in the proposal if needed
219
234
  let reexecutionResult;
220
- if (shouldReexecute) {
221
- // Compute the previous checkpoint out hashes for the epoch.
222
- // TODO(leila/mbps): There can be a more efficient way to get the previous checkpoint out
223
- // hashes without having to fetch all the blocks.
224
- const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
225
- const checkpointedBlocks = (await this.blockSource.getCheckpointedBlocksForEpoch(epoch))
226
- .filter(b => b.block.number < blockNumber)
227
- .sort((a, b) => a.block.number - b.block.number);
228
- const blocksByCheckpoint = chunkBy(checkpointedBlocks, b => b.checkpointNumber);
229
- const previousCheckpointOutHashes = blocksByCheckpoint.map(checkpointBlocks =>
230
- computeCheckpointOutHash(checkpointBlocks.map(b => b.block.body.txEffects.map(tx => tx.l2ToL1Msgs))),
235
+ try {
236
+ this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
237
+ reexecutionResult = await this.reexecuteTransactions(
238
+ proposal,
239
+ blockNumber,
240
+ checkpointNumber,
241
+ txs,
242
+ l1ToL2Messages,
243
+ previousCheckpointOutHashes,
231
244
  );
232
-
233
- try {
234
- this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
235
- reexecutionResult = await this.reexecuteTransactions(
236
- proposal,
237
- blockNumber,
238
- checkpointNumber,
239
- txs,
240
- l1ToL2Messages,
241
- previousCheckpointOutHashes,
242
- );
243
- } catch (error) {
244
- this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
245
- const reason = this.getReexecuteFailureReason(error);
246
- return { isValid: false, blockNumber, reason, reexecutionResult };
247
- }
245
+ } catch (error) {
246
+ this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
247
+ const reason = this.getReexecuteFailureReason(error);
248
+ return { isValid: false, blockNumber, reason, reexecutionResult };
248
249
  }
249
250
 
250
251
  // If we succeeded, push this block into the archiver (unless disabled)
@@ -253,14 +254,14 @@ export class BlockProposalHandler {
253
254
  }
254
255
 
255
256
  this.log.info(
256
- `Successfully processed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
257
- proposalInfo,
257
+ `Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
258
+ { ...proposalInfo, ...pick(reexecutionResult, 'reexecutionTimeMs', 'totalManaUsed') },
258
259
  );
259
260
 
260
261
  return { isValid: true, blockNumber, reexecutionResult };
261
262
  }
262
263
 
263
- private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockHeader | undefined> {
264
+ private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockData | undefined> {
264
265
  const parentArchive = proposal.blockHeader.lastArchive.root;
265
266
  const slot = proposal.slotNumber;
266
267
  const config = this.checkpointsBuilder.getConfig();
@@ -276,12 +277,11 @@ export class BlockProposalHandler {
276
277
 
277
278
  try {
278
279
  return (
279
- (await this.blockSource.getBlockHeaderByArchive(parentArchive)) ??
280
+ (await this.blockSource.getBlockDataByArchive(parentArchive)) ??
280
281
  (timeoutDurationMs <= 0
281
282
  ? undefined
282
283
  : await retryUntil(
283
- () =>
284
- this.blockSource.syncImmediate().then(() => this.blockSource.getBlockHeaderByArchive(parentArchive)),
284
+ () => this.blockSource.syncImmediate().then(() => this.blockSource.getBlockDataByArchive(parentArchive)),
285
285
  'force archiver sync',
286
286
  timeoutDurationMs / 1000,
287
287
  0.5,
@@ -297,12 +297,12 @@ export class BlockProposalHandler {
297
297
  }
298
298
  }
299
299
 
300
- private async computeCheckpointNumber(
300
+ private computeCheckpointNumber(
301
301
  proposal: BlockProposal,
302
- parentBlockHeader: 'genesis' | BlockHeader,
302
+ parentBlock: 'genesis' | BlockData,
303
303
  proposalInfo: object,
304
- ): Promise<CheckpointComputationResult> {
305
- if (parentBlockHeader === 'genesis') {
304
+ ): CheckpointComputationResult {
305
+ if (parentBlock === 'genesis') {
306
306
  // First block is in checkpoint 1
307
307
  if (proposal.indexWithinCheckpoint !== 0) {
308
308
  this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
@@ -311,19 +311,9 @@ export class BlockProposalHandler {
311
311
  return { checkpointNumber: CheckpointNumber.INITIAL };
312
312
  }
313
313
 
314
- // Get the parent block to find its checkpoint number
315
- // TODO(palla/mbps): The block header should include the checkpoint number to avoid this lookup,
316
- // or at least the L2BlockSource should return a different struct that includes it.
317
- const parentBlockNumber = parentBlockHeader.getBlockNumber();
318
- const parentBlock = await this.blockSource.getL2Block(parentBlockNumber);
319
- if (!parentBlock) {
320
- this.log.warn(`Parent block ${parentBlockNumber} not found in archiver`, proposalInfo);
321
- return { reason: 'invalid_proposal' };
322
- }
323
-
324
314
  if (proposal.indexWithinCheckpoint === 0) {
325
315
  // If this is the first block in a new checkpoint, increment the checkpoint number
326
- if (!(proposal.blockHeader.getSlot() > parentBlockHeader.getSlot())) {
316
+ if (!(proposal.blockHeader.getSlot() > parentBlock.header.getSlot())) {
327
317
  this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
328
318
  return { reason: 'invalid_proposal' };
329
319
  }
@@ -335,7 +325,7 @@ export class BlockProposalHandler {
335
325
  this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
336
326
  return { reason: 'invalid_proposal' };
337
327
  }
338
- if (proposal.blockHeader.getSlot() !== parentBlockHeader.getSlot()) {
328
+ if (proposal.blockHeader.getSlot() !== parentBlock.header.getSlot()) {
339
329
  this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
340
330
  return { reason: 'invalid_proposal' };
341
331
  }
@@ -356,7 +346,7 @@ export class BlockProposalHandler {
356
346
  */
357
347
  private validateNonFirstBlockInCheckpoint(
358
348
  proposal: BlockProposal,
359
- parentBlock: L2Block,
349
+ parentBlock: BlockData,
360
350
  proposalInfo: object,
361
351
  ): CheckpointComputationResult | undefined {
362
352
  const proposalGlobals = proposal.blockHeader.globalVariables;
@@ -475,13 +465,14 @@ export class BlockProposalHandler {
475
465
  // Fork before the block to be built
476
466
  const parentBlockNumber = BlockNumber(blockNumber - 1);
477
467
  await this.worldState.syncImmediate(parentBlockNumber);
478
- using fork = await this.worldState.fork(parentBlockNumber);
468
+ await using fork = await this.worldState.fork(parentBlockNumber);
479
469
 
480
- // Build checkpoint constants from proposal (excludes blockNumber and timestamp which are per-block)
470
+ // Build checkpoint constants from proposal (excludes blockNumber which is per-block)
481
471
  const constants: CheckpointGlobalVariables = {
482
472
  chainId: new Fr(config.l1ChainId),
483
473
  version: new Fr(config.rollupVersion),
484
474
  slotNumber: slot,
475
+ timestamp: blockHeader.globalVariables.timestamp,
485
476
  coinbase: blockHeader.globalVariables.coinbase,
486
477
  feeRecipient: blockHeader.globalVariables.feeRecipient,
487
478
  gasFees: blockHeader.globalVariables.gasFees,
@@ -491,26 +482,35 @@ export class BlockProposalHandler {
491
482
  const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
492
483
  checkpointNumber,
493
484
  constants,
485
+ 0n, // only takes effect in the following checkpoint.
494
486
  l1ToL2Messages,
495
487
  previousCheckpointOutHashes,
496
488
  fork,
497
489
  priorBlocks,
490
+ this.log.getBindings(),
498
491
  );
499
492
 
500
493
  // Build the new block
501
494
  const deadline = this.getReexecutionDeadline(slot, config);
495
+ const maxBlockGas =
496
+ this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined
497
+ ? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity)
498
+ : undefined;
502
499
  const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
503
500
  deadline,
504
501
  expectedEndState: blockHeader.state,
502
+ maxTransactions: this.config.validateMaxTxsPerBlock,
503
+ maxBlockGas,
505
504
  });
506
505
 
507
506
  const { block, failedTxs } = result;
508
507
  const numFailedTxs = failedTxs.length;
509
508
 
510
- this.log.verbose(`Transaction re-execution complete for slot ${slot}`, {
509
+ this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
511
510
  numFailedTxs,
512
511
  numProposalTxs: txHashes.length,
513
512
  numProcessedTxs: block.body.txEffects.length,
513
+ blockNumber,
514
514
  slot,
515
515
  });
516
516