@aztec/validator-client 0.0.1-commit.cf93bcc56 → 0.0.1-commit.d0fcfb7f

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 (48) hide show
  1. package/README.md +42 -0
  2. package/dest/block_proposal_handler.d.ts +4 -3
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +104 -30
  5. package/dest/checkpoint_builder.d.ts +14 -3
  6. package/dest/checkpoint_builder.d.ts.map +1 -1
  7. package/dest/checkpoint_builder.js +103 -37
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +20 -0
  11. package/dest/duties/validation_service.d.ts +1 -1
  12. package/dest/duties/validation_service.d.ts.map +1 -1
  13. package/dest/duties/validation_service.js +3 -9
  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.js +1 -1
  21. package/dest/metrics.d.ts +9 -1
  22. package/dest/metrics.d.ts.map +1 -1
  23. package/dest/metrics.js +12 -0
  24. package/dest/validator.d.ts +3 -1
  25. package/dest/validator.d.ts.map +1 -1
  26. package/dest/validator.js +36 -8
  27. package/package.json +19 -19
  28. package/src/block_proposal_handler.ts +126 -37
  29. package/src/checkpoint_builder.ts +122 -39
  30. package/src/config.ts +20 -0
  31. package/src/duties/validation_service.ts +3 -9
  32. package/src/factory.ts +1 -0
  33. package/src/index.ts +0 -1
  34. package/src/key_store/ha_key_store.ts +1 -1
  35. package/src/metrics.ts +18 -0
  36. package/src/validator.ts +35 -6
  37. package/dest/tx_validator/index.d.ts +0 -3
  38. package/dest/tx_validator/index.d.ts.map +0 -1
  39. package/dest/tx_validator/index.js +0 -2
  40. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  41. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  42. package/dest/tx_validator/nullifier_cache.js +0 -24
  43. package/dest/tx_validator/tx_validator_factory.d.ts +0 -19
  44. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  45. package/dest/tx_validator/tx_validator_factory.js +0 -54
  46. package/src/tx_validator/index.ts +0 -2
  47. package/src/tx_validator/nullifier_cache.ts +0 -30
  48. package/src/tx_validator/tx_validator_factory.ts +0 -154
package/dest/validator.js CHANGED
@@ -9,6 +9,7 @@ import { sleep } from '@aztec/foundation/sleep';
9
9
  import { DateProvider } from '@aztec/foundation/timer';
10
10
  import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
11
11
  import { OffenseType, WANT_TO_SLASH_EVENT } from '@aztec/slasher';
12
+ import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
12
13
  import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
13
14
  import { accumulateCheckpointOutHashes } from '@aztec/stdlib/messaging';
14
15
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
@@ -54,10 +55,11 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
54
55
  /** Tracks the last checkpoint proposal we created. */ lastProposedCheckpoint;
55
56
  lastEpochForCommitteeUpdateLoop;
56
57
  epochCacheUpdateLoop;
58
+ /** Tracks the last epoch in which each attester successfully submitted at least one attestation. */ lastAttestedEpochByAttester;
57
59
  proposersOfInvalidBlocks;
58
60
  /** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */ lastAttestedProposal;
59
61
  constructor(keyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, haSigner, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
60
- super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.blockSource = blockSource, this.checkpointsBuilder = checkpointsBuilder, this.worldState = worldState, this.l1ToL2MessageSource = l1ToL2MessageSource, this.config = config, this.blobClient = blobClient, this.haSigner = haSigner, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.proposersOfInvalidBlocks = new Set();
62
+ super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.blockSource = blockSource, this.checkpointsBuilder = checkpointsBuilder, this.worldState = worldState, this.l1ToL2MessageSource = l1ToL2MessageSource, this.config = config, this.blobClient = blobClient, this.haSigner = haSigner, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.lastAttestedEpochByAttester = new Map(), this.proposersOfInvalidBlocks = new Set();
61
63
  // Create child logger with fisherman prefix if in fisherman mode
62
64
  this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
63
65
  this.tracer = telemetry.getTracer('Validator');
@@ -96,6 +98,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
96
98
  this.log.trace(`No committee found for slot`);
97
99
  return;
98
100
  }
101
+ this.metrics.setCurrentEpoch(epoch);
99
102
  if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
100
103
  const me = this.getValidatorAddresses();
101
104
  const committeeSet = new Set(committee.map((v)=>v.toString()));
@@ -114,7 +117,8 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
114
117
  static async new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
115
118
  const metrics = new ValidatorMetrics(telemetry);
116
119
  const blockProposalValidator = new BlockProposalValidator(epochCache, {
117
- txsPermitted: !config.disableTransactions
120
+ txsPermitted: !config.disableTransactions,
121
+ maxTxsPerBlock: config.validateMaxTxsPerBlock
118
122
  });
119
123
  const blockProposalHandler = new BlockProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, metrics, dateProvider, telemetry);
120
124
  const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
@@ -233,7 +237,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
233
237
  }
234
238
  // Ignore proposals from ourselves (may happen in HA setups)
235
239
  if (this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
236
- this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
240
+ this.log.debug(`Ignoring block proposal from self for slot ${slotNumber}`, {
237
241
  proposer: proposer.toString(),
238
242
  slotNumber
239
243
  });
@@ -257,8 +261,8 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
257
261
  const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
258
262
  const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute && !escapeHatchOpen);
259
263
  if (!validationResult.isValid) {
260
- this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
261
264
  const reason = validationResult.reason || 'unknown';
265
+ this.log.warn(`Block proposal validation failed: ${reason}`, proposalInfo);
262
266
  // Classify failure reason: bad proposal vs node issue
263
267
  const badProposalReasons = [
264
268
  'invalid_proposal',
@@ -312,7 +316,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
312
316
  }
313
317
  // Ignore proposals from ourselves (may happen in HA setups)
314
318
  if (this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
315
- this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
319
+ this.log.debug(`Ignoring block proposal from self for slot ${slotNumber}`, {
316
320
  proposer: proposer.toString(),
317
321
  slotNumber
318
322
  });
@@ -329,12 +333,10 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
329
333
  const proposalInfo = {
330
334
  slotNumber,
331
335
  archive: proposal.archive.toString(),
332
- proposer: proposer.toString(),
333
- txCount: proposal.txHashes.length
336
+ proposer: proposer.toString()
334
337
  };
335
338
  this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
336
339
  ...proposalInfo,
337
- txHashes: proposal.txHashes.map((t)=>t.toString()),
338
340
  fishermanMode: this.config.fishermanMode || false
339
341
  });
340
342
  // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
@@ -364,6 +366,16 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
364
366
  fishermanMode: this.config.fishermanMode || false
365
367
  });
366
368
  this.metrics.incSuccessfulAttestations(inCommittee.length);
369
+ // Track epoch participation per attester: count each (attester, epoch) pair at most once
370
+ const proposalEpoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
371
+ for (const attester of inCommittee){
372
+ const key = attester.toString();
373
+ const lastEpoch = this.lastAttestedEpochByAttester.get(key);
374
+ if (lastEpoch === undefined || proposalEpoch > lastEpoch) {
375
+ this.lastAttestedEpochByAttester.set(key, proposalEpoch);
376
+ this.metrics.incAttestedEpochCount(attester);
377
+ }
378
+ }
367
379
  // Determine which validators should attest
368
380
  let attestors;
369
381
  if (partOfCommittee) {
@@ -534,6 +546,22 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
534
546
  reason: 'out_hash_mismatch'
535
547
  };
536
548
  }
549
+ // Final round of validations on the checkpoint, just in case.
550
+ try {
551
+ validateCheckpoint(computedCheckpoint, {
552
+ rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
553
+ maxDABlockGas: this.config.validateMaxDABlockGas,
554
+ maxL2BlockGas: this.config.validateMaxL2BlockGas,
555
+ maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
556
+ maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint
557
+ });
558
+ } catch (err) {
559
+ this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
560
+ return {
561
+ isValid: false,
562
+ reason: 'checkpoint_validation_failed'
563
+ };
564
+ }
537
565
  this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
538
566
  return {
539
567
  isValid: true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/validator-client",
3
- "version": "0.0.1-commit.cf93bcc56",
3
+ "version": "0.0.1-commit.d0fcfb7f",
4
4
  "main": "dest/index.js",
5
5
  "type": "module",
6
6
  "exports": {
@@ -64,30 +64,30 @@
64
64
  ]
65
65
  },
66
66
  "dependencies": {
67
- "@aztec/blob-client": "0.0.1-commit.cf93bcc56",
68
- "@aztec/blob-lib": "0.0.1-commit.cf93bcc56",
69
- "@aztec/constants": "0.0.1-commit.cf93bcc56",
70
- "@aztec/epoch-cache": "0.0.1-commit.cf93bcc56",
71
- "@aztec/ethereum": "0.0.1-commit.cf93bcc56",
72
- "@aztec/foundation": "0.0.1-commit.cf93bcc56",
73
- "@aztec/node-keystore": "0.0.1-commit.cf93bcc56",
74
- "@aztec/noir-protocol-circuits-types": "0.0.1-commit.cf93bcc56",
75
- "@aztec/p2p": "0.0.1-commit.cf93bcc56",
76
- "@aztec/protocol-contracts": "0.0.1-commit.cf93bcc56",
77
- "@aztec/prover-client": "0.0.1-commit.cf93bcc56",
78
- "@aztec/simulator": "0.0.1-commit.cf93bcc56",
79
- "@aztec/slasher": "0.0.1-commit.cf93bcc56",
80
- "@aztec/stdlib": "0.0.1-commit.cf93bcc56",
81
- "@aztec/telemetry-client": "0.0.1-commit.cf93bcc56",
82
- "@aztec/validator-ha-signer": "0.0.1-commit.cf93bcc56",
67
+ "@aztec/blob-client": "0.0.1-commit.d0fcfb7f",
68
+ "@aztec/blob-lib": "0.0.1-commit.d0fcfb7f",
69
+ "@aztec/constants": "0.0.1-commit.d0fcfb7f",
70
+ "@aztec/epoch-cache": "0.0.1-commit.d0fcfb7f",
71
+ "@aztec/ethereum": "0.0.1-commit.d0fcfb7f",
72
+ "@aztec/foundation": "0.0.1-commit.d0fcfb7f",
73
+ "@aztec/node-keystore": "0.0.1-commit.d0fcfb7f",
74
+ "@aztec/noir-protocol-circuits-types": "0.0.1-commit.d0fcfb7f",
75
+ "@aztec/p2p": "0.0.1-commit.d0fcfb7f",
76
+ "@aztec/protocol-contracts": "0.0.1-commit.d0fcfb7f",
77
+ "@aztec/prover-client": "0.0.1-commit.d0fcfb7f",
78
+ "@aztec/simulator": "0.0.1-commit.d0fcfb7f",
79
+ "@aztec/slasher": "0.0.1-commit.d0fcfb7f",
80
+ "@aztec/stdlib": "0.0.1-commit.d0fcfb7f",
81
+ "@aztec/telemetry-client": "0.0.1-commit.d0fcfb7f",
82
+ "@aztec/validator-ha-signer": "0.0.1-commit.d0fcfb7f",
83
83
  "koa": "^2.16.1",
84
84
  "koa-router": "^13.1.1",
85
85
  "tslib": "^2.4.0",
86
86
  "viem": "npm:@aztec/viem@2.38.2"
87
87
  },
88
88
  "devDependencies": {
89
- "@aztec/archiver": "0.0.1-commit.cf93bcc56",
90
- "@aztec/world-state": "0.0.1-commit.cf93bcc56",
89
+ "@aztec/archiver": "0.0.1-commit.d0fcfb7f",
90
+ "@aztec/world-state": "0.0.1-commit.d0fcfb7f",
91
91
  "@electric-sql/pglite": "^0.3.14",
92
92
  "@jest/globals": "^30.0.0",
93
93
  "@types/jest": "^30.0.0",
@@ -1,6 +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 { pick } from '@aztec/foundation/collection';
4
5
  import { Fr } from '@aztec/foundation/curves/bn254';
5
6
  import { TimeoutError } from '@aztec/foundation/error';
6
7
  import { createLogger } from '@aztec/foundation/log';
@@ -10,12 +11,15 @@ import type { P2P, PeerId } from '@aztec/p2p';
10
11
  import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
11
12
  import type { BlockData, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
12
13
  import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
14
+ import { Gas } from '@aztec/stdlib/gas';
13
15
  import type { ITxProvider, ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
14
16
  import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
15
17
  import type { BlockProposal } from '@aztec/stdlib/p2p';
18
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
16
19
  import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
17
20
  import {
18
21
  ReExFailedTxsError,
22
+ ReExInitialStateMismatchError,
19
23
  ReExStateMismatchError,
20
24
  ReExTimeoutError,
21
25
  TransactionsNotAvailableError,
@@ -28,6 +32,7 @@ import type { ValidatorMetrics } from './metrics.js';
28
32
  export type BlockProposalValidationFailureReason =
29
33
  | 'invalid_proposal'
30
34
  | 'parent_block_not_found'
35
+ | 'block_source_not_synced'
31
36
  | 'parent_block_wrong_slot'
32
37
  | 'in_hash_mismatch'
33
38
  | 'global_variables_mismatch'
@@ -35,6 +40,7 @@ export type BlockProposalValidationFailureReason =
35
40
  | 'txs_not_available'
36
41
  | 'state_mismatch'
37
42
  | 'failed_txs'
43
+ | 'initial_state_mismatch'
38
44
  | 'timeout'
39
45
  | 'unknown_error';
40
46
 
@@ -87,25 +93,28 @@ export class BlockProposalHandler {
87
93
  this.tracer = telemetry.getTracer('BlockProposalHandler');
88
94
  }
89
95
 
90
- registerForReexecution(p2pClient: P2P): BlockProposalHandler {
91
- // Non-validator handler that re-executes for monitoring but does not attest.
96
+ register(p2pClient: P2P, shouldReexecute: boolean): BlockProposalHandler {
97
+ // Non-validator handler that processes or re-executes for monitoring but does not attest.
92
98
  // Returns boolean indicating whether the proposal was valid.
93
99
  const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
94
100
  try {
95
- const result = await this.handleBlockProposal(proposal, proposalSender, true);
101
+ const { slotNumber, blockNumber } = proposal;
102
+ const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
96
103
  if (result.isValid) {
97
- this.log.info(`Non-validator reexecution completed for slot ${proposal.slotNumber}`, {
104
+ this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
98
105
  blockNumber: result.blockNumber,
106
+ slotNumber,
99
107
  reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
100
108
  totalManaUsed: result.reexecutionResult?.totalManaUsed,
101
109
  numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
110
+ reexecuted: shouldReexecute,
102
111
  });
103
112
  return true;
104
113
  } else {
105
- this.log.warn(`Non-validator reexecution failed for slot ${proposal.slotNumber}`, {
106
- blockNumber: result.blockNumber,
107
- reason: result.reason,
108
- });
114
+ this.log.warn(
115
+ `Non-validator block proposal ${blockNumber} at slot ${slotNumber} failed processing with ${result.reason}`,
116
+ { blockNumber: result.blockNumber, slotNumber, reason: result.reason },
117
+ );
109
118
  return false;
110
119
  }
111
120
  } catch (error) {
@@ -133,7 +142,13 @@ export class BlockProposalHandler {
133
142
  return { isValid: false, reason: 'invalid_proposal' };
134
143
  }
135
144
 
136
- const proposalInfo = { ...proposal.toBlockInfo(), proposer: proposer.toString() };
145
+ const proposalInfo = {
146
+ ...proposal.toBlockInfo(),
147
+ proposer: proposer.toString(),
148
+ blockNumber: undefined as BlockNumber | undefined,
149
+ checkpointNumber: undefined as CheckpointNumber | undefined,
150
+ };
151
+
137
152
  this.log.info(`Processing proposal for slot ${slotNumber}`, {
138
153
  ...proposalInfo,
139
154
  txHashes: proposal.txHashes.map(t => t.toString()),
@@ -147,7 +162,20 @@ export class BlockProposalHandler {
147
162
  return { isValid: false, reason: 'invalid_proposal' };
148
163
  }
149
164
 
150
- // Check that the parent proposal is a block we know, otherwise reexecution would fail
165
+ // Ensure the block source is synced before checking for existing blocks,
166
+ // since a pending checkpoint prune may remove blocks we'd otherwise find.
167
+ // This affects mostly the block_number_already_exists check, since a pending
168
+ // checkpoint prune could remove a block that would conflict with this proposal.
169
+ // TODO(@Maddiaa0): This may break staggered slots.
170
+ const blockSourceSync = await this.waitForBlockSourceSync(slotNumber);
171
+ if (!blockSourceSync) {
172
+ this.log.warn(`Block source is not synced, skipping processing`, proposalInfo);
173
+ return { isValid: false, reason: 'block_source_not_synced' };
174
+ }
175
+
176
+ // Check that the parent proposal is a block we know, otherwise reexecution would fail.
177
+ // If we don't find it immediately, we keep retrying for a while; it may be we still
178
+ // need to process other block proposals to get to it.
151
179
  const parentBlock = await this.getParentBlock(proposal);
152
180
  if (parentBlock === undefined) {
153
181
  this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
@@ -169,6 +197,7 @@ export class BlockProposalHandler {
169
197
  parentBlock === 'genesis'
170
198
  ? BlockNumber(INITIAL_L2_BLOCK_NUM)
171
199
  : BlockNumber(parentBlock.header.getBlockNumber() + 1);
200
+ proposalInfo.blockNumber = blockNumber;
172
201
 
173
202
  // Check that this block number does not exist already
174
203
  const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
@@ -184,12 +213,22 @@ export class BlockProposalHandler {
184
213
  deadline: this.getReexecutionDeadline(slotNumber, config),
185
214
  });
186
215
 
216
+ // If reexecution is disabled, bail. We were just interested in triggering tx collection.
217
+ if (!shouldReexecute) {
218
+ this.log.info(
219
+ `Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
220
+ proposalInfo,
221
+ );
222
+ return { isValid: true, blockNumber };
223
+ }
224
+
187
225
  // Compute the checkpoint number for this block and validate checkpoint consistency
188
226
  const checkpointResult = this.computeCheckpointNumber(proposal, parentBlock, proposalInfo);
189
227
  if (checkpointResult.reason) {
190
228
  return { isValid: false, blockNumber, reason: checkpointResult.reason };
191
229
  }
192
230
  const checkpointNumber = checkpointResult.checkpointNumber;
231
+ proposalInfo.checkpointNumber = checkpointNumber;
193
232
 
194
233
  // Check that I have the same set of l1ToL2Messages as the proposal
195
234
  const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
@@ -210,30 +249,28 @@ export class BlockProposalHandler {
210
249
  return { isValid: false, blockNumber, reason: 'txs_not_available' };
211
250
  }
212
251
 
252
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
253
+ const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
254
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
255
+ .filter(c => c.checkpointNumber < checkpointNumber)
256
+ .map(c => c.checkpointOutHash);
257
+
213
258
  // Try re-executing the transactions in the proposal if needed
214
259
  let reexecutionResult;
215
- if (shouldReexecute) {
216
- // Collect the out hashes of all the checkpoints before this one in the same epoch
217
- const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
218
- const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
219
- .filter(c => c.checkpointNumber < checkpointNumber)
220
- .map(c => c.checkpointOutHash);
221
-
222
- try {
223
- this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
224
- reexecutionResult = await this.reexecuteTransactions(
225
- proposal,
226
- blockNumber,
227
- checkpointNumber,
228
- txs,
229
- l1ToL2Messages,
230
- previousCheckpointOutHashes,
231
- );
232
- } catch (error) {
233
- this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
234
- const reason = this.getReexecuteFailureReason(error);
235
- return { isValid: false, blockNumber, reason, reexecutionResult };
236
- }
260
+ try {
261
+ this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
262
+ reexecutionResult = await this.reexecuteTransactions(
263
+ proposal,
264
+ blockNumber,
265
+ checkpointNumber,
266
+ txs,
267
+ l1ToL2Messages,
268
+ previousCheckpointOutHashes,
269
+ );
270
+ } catch (error) {
271
+ this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
272
+ const reason = this.getReexecuteFailureReason(error);
273
+ return { isValid: false, blockNumber, reason, reexecutionResult };
237
274
  }
238
275
 
239
276
  // If we succeeded, push this block into the archiver (unless disabled)
@@ -242,8 +279,8 @@ export class BlockProposalHandler {
242
279
  }
243
280
 
244
281
  this.log.info(
245
- `Successfully processed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
246
- proposalInfo,
282
+ `Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
283
+ { ...proposalInfo, ...pick(reexecutionResult, 'reexecutionTimeMs', 'totalManaUsed') },
247
284
  );
248
285
 
249
286
  return { isValid: true, blockNumber, reexecutionResult };
@@ -413,8 +450,46 @@ export class BlockProposalHandler {
413
450
  return new Date(nextSlotTimestampSeconds * 1000);
414
451
  }
415
452
 
416
- private getReexecuteFailureReason(err: any) {
417
- if (err instanceof ReExStateMismatchError) {
453
+ /** Waits for the block source to sync L1 data up to at least the slot before the given one. */
454
+ private async waitForBlockSourceSync(slot: SlotNumber): Promise<boolean> {
455
+ const deadline = this.getReexecutionDeadline(slot, this.checkpointsBuilder.getConfig());
456
+ const timeoutMs = deadline.getTime() - this.dateProvider.now();
457
+ if (slot === 0) {
458
+ return true;
459
+ }
460
+
461
+ // Make a quick check before triggering an archiver sync
462
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
463
+ if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
464
+ return true;
465
+ }
466
+
467
+ try {
468
+ // Trigger an immediate sync of the block source, and wait until it reports being synced to the required slot
469
+ return await retryUntil(
470
+ async () => {
471
+ await this.blockSource.syncImmediate();
472
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
473
+ return syncedSlot !== undefined && syncedSlot + 1 >= slot;
474
+ },
475
+ 'wait for block source sync',
476
+ timeoutMs / 1000,
477
+ 0.5,
478
+ );
479
+ } catch (err) {
480
+ if (err instanceof TimeoutError) {
481
+ this.log.warn(`Timed out waiting for block source to sync to slot ${slot}`);
482
+ return false;
483
+ } else {
484
+ throw err;
485
+ }
486
+ }
487
+ }
488
+
489
+ private getReexecuteFailureReason(err: any): BlockProposalValidationFailureReason {
490
+ if (err instanceof ReExInitialStateMismatchError) {
491
+ return 'initial_state_mismatch';
492
+ } else if (err instanceof ReExStateMismatchError) {
418
493
  return 'state_mismatch';
419
494
  } else if (err instanceof ReExFailedTxsError) {
420
495
  return 'failed_txs';
@@ -455,6 +530,13 @@ export class BlockProposalHandler {
455
530
  await this.worldState.syncImmediate(parentBlockNumber);
456
531
  await using fork = await this.worldState.fork(parentBlockNumber);
457
532
 
533
+ // Verify the fork's archive root matches the proposal's expected last archive.
534
+ // If they don't match, our world state synced to a different chain and reexecution would fail.
535
+ const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
536
+ if (!forkArchiveRoot.equals(proposal.blockHeader.lastArchive.root)) {
537
+ throw new ReExInitialStateMismatchError(proposal.blockHeader.lastArchive.root, forkArchiveRoot);
538
+ }
539
+
458
540
  // Build checkpoint constants from proposal (excludes blockNumber which is per-block)
459
541
  const constants: CheckpointGlobalVariables = {
460
542
  chainId: new Fr(config.l1ChainId),
@@ -480,18 +562,25 @@ export class BlockProposalHandler {
480
562
 
481
563
  // Build the new block
482
564
  const deadline = this.getReexecutionDeadline(slot, config);
565
+ const maxBlockGas =
566
+ this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined
567
+ ? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity)
568
+ : undefined;
483
569
  const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
484
570
  deadline,
485
571
  expectedEndState: blockHeader.state,
572
+ maxTransactions: this.config.validateMaxTxsPerBlock,
573
+ maxBlockGas,
486
574
  });
487
575
 
488
576
  const { block, failedTxs } = result;
489
577
  const numFailedTxs = failedTxs.length;
490
578
 
491
- this.log.verbose(`Transaction re-execution complete for slot ${slot}`, {
579
+ this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
492
580
  numFailedTxs,
493
581
  numProposalTxs: txHashes.length,
494
582
  numProcessedTxs: block.body.txEffects.length,
583
+ blockNumber,
495
584
  slot,
496
585
  });
497
586