@aztec/validator-client 0.0.1-commit.8afd444 → 0.0.1-commit.8ee97c858

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 +62 -18
  2. package/dest/block_proposal_handler.d.ts +5 -4
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +130 -62
  5. package/dest/checkpoint_builder.d.ts +21 -8
  6. package/dest/checkpoint_builder.d.ts.map +1 -1
  7. package/dest/checkpoint_builder.js +124 -46
  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 +6 -12
  14. package/dest/factory.d.ts +3 -1
  15. package/dest/factory.d.ts.map +1 -1
  16. package/dest/factory.js +3 -2
  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 +37 -10
  25. package/dest/validator.d.ts.map +1 -1
  26. package/dest/validator.js +214 -47
  27. package/package.json +19 -19
  28. package/src/block_proposal_handler.ts +157 -80
  29. package/src/checkpoint_builder.ts +142 -39
  30. package/src/config.ts +26 -1
  31. package/src/duties/validation_service.ts +12 -11
  32. package/src/factory.ts +4 -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 +276 -57
  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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/validator-client",
3
- "version": "0.0.1-commit.8afd444",
3
+ "version": "0.0.1-commit.8ee97c858",
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.8afd444",
68
- "@aztec/blob-lib": "0.0.1-commit.8afd444",
69
- "@aztec/constants": "0.0.1-commit.8afd444",
70
- "@aztec/epoch-cache": "0.0.1-commit.8afd444",
71
- "@aztec/ethereum": "0.0.1-commit.8afd444",
72
- "@aztec/foundation": "0.0.1-commit.8afd444",
73
- "@aztec/node-keystore": "0.0.1-commit.8afd444",
74
- "@aztec/noir-protocol-circuits-types": "0.0.1-commit.8afd444",
75
- "@aztec/p2p": "0.0.1-commit.8afd444",
76
- "@aztec/protocol-contracts": "0.0.1-commit.8afd444",
77
- "@aztec/prover-client": "0.0.1-commit.8afd444",
78
- "@aztec/simulator": "0.0.1-commit.8afd444",
79
- "@aztec/slasher": "0.0.1-commit.8afd444",
80
- "@aztec/stdlib": "0.0.1-commit.8afd444",
81
- "@aztec/telemetry-client": "0.0.1-commit.8afd444",
82
- "@aztec/validator-ha-signer": "0.0.1-commit.8afd444",
67
+ "@aztec/blob-client": "0.0.1-commit.8ee97c858",
68
+ "@aztec/blob-lib": "0.0.1-commit.8ee97c858",
69
+ "@aztec/constants": "0.0.1-commit.8ee97c858",
70
+ "@aztec/epoch-cache": "0.0.1-commit.8ee97c858",
71
+ "@aztec/ethereum": "0.0.1-commit.8ee97c858",
72
+ "@aztec/foundation": "0.0.1-commit.8ee97c858",
73
+ "@aztec/node-keystore": "0.0.1-commit.8ee97c858",
74
+ "@aztec/noir-protocol-circuits-types": "0.0.1-commit.8ee97c858",
75
+ "@aztec/p2p": "0.0.1-commit.8ee97c858",
76
+ "@aztec/protocol-contracts": "0.0.1-commit.8ee97c858",
77
+ "@aztec/prover-client": "0.0.1-commit.8ee97c858",
78
+ "@aztec/simulator": "0.0.1-commit.8ee97c858",
79
+ "@aztec/slasher": "0.0.1-commit.8ee97c858",
80
+ "@aztec/stdlib": "0.0.1-commit.8ee97c858",
81
+ "@aztec/telemetry-client": "0.0.1-commit.8ee97c858",
82
+ "@aztec/validator-ha-signer": "0.0.1-commit.8ee97c858",
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.8afd444",
90
- "@aztec/world-state": "0.0.1-commit.8afd444",
89
+ "@aztec/archiver": "0.0.1-commit.8ee97c858",
90
+ "@aztec/world-state": "0.0.1-commit.8ee97c858",
91
91
  "@electric-sql/pglite": "^0.3.14",
92
92
  "@jest/globals": "^30.0.0",
93
93
  "@types/jest": "^30.0.0",
@@ -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,18 +9,17 @@ 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 { MerkleTreeId } from '@aztec/stdlib/trees';
19
+ import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
22
20
  import {
23
21
  ReExFailedTxsError,
22
+ ReExInitialStateMismatchError,
24
23
  ReExStateMismatchError,
25
24
  ReExTimeoutError,
26
25
  TransactionsNotAvailableError,
@@ -33,6 +32,7 @@ import type { ValidatorMetrics } from './metrics.js';
33
32
  export type BlockProposalValidationFailureReason =
34
33
  | 'invalid_proposal'
35
34
  | 'parent_block_not_found'
35
+ | 'block_source_not_synced'
36
36
  | 'parent_block_wrong_slot'
37
37
  | 'in_hash_mismatch'
38
38
  | 'global_variables_mismatch'
@@ -40,6 +40,7 @@ export type BlockProposalValidationFailureReason =
40
40
  | 'txs_not_available'
41
41
  | 'state_mismatch'
42
42
  | 'failed_txs'
43
+ | 'initial_state_mismatch'
43
44
  | 'timeout'
44
45
  | 'unknown_error';
45
46
 
@@ -92,25 +93,28 @@ export class BlockProposalHandler {
92
93
  this.tracer = telemetry.getTracer('BlockProposalHandler');
93
94
  }
94
95
 
95
- registerForReexecution(p2pClient: P2P): BlockProposalHandler {
96
- // 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.
97
98
  // Returns boolean indicating whether the proposal was valid.
98
99
  const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
99
100
  try {
100
- const result = await this.handleBlockProposal(proposal, proposalSender, true);
101
+ const { slotNumber, blockNumber } = proposal;
102
+ const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
101
103
  if (result.isValid) {
102
- this.log.info(`Non-validator reexecution completed for slot ${proposal.slotNumber}`, {
104
+ this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
103
105
  blockNumber: result.blockNumber,
106
+ slotNumber,
104
107
  reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
105
108
  totalManaUsed: result.reexecutionResult?.totalManaUsed,
106
109
  numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
110
+ reexecuted: shouldReexecute,
107
111
  });
108
112
  return true;
109
113
  } else {
110
- this.log.warn(`Non-validator reexecution failed for slot ${proposal.slotNumber}`, {
111
- blockNumber: result.blockNumber,
112
- reason: result.reason,
113
- });
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
+ );
114
118
  return false;
115
119
  }
116
120
  } catch (error) {
@@ -138,7 +142,13 @@ export class BlockProposalHandler {
138
142
  return { isValid: false, reason: 'invalid_proposal' };
139
143
  }
140
144
 
141
- 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
+
142
152
  this.log.info(`Processing proposal for slot ${slotNumber}`, {
143
153
  ...proposalInfo,
144
154
  txHashes: proposal.txHashes.map(t => t.toString()),
@@ -152,17 +162,34 @@ export class BlockProposalHandler {
152
162
  return { isValid: false, reason: 'invalid_proposal' };
153
163
  }
154
164
 
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) {
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
+ // When pipelining is enabled, the proposer builds ahead of L1 submission, so the
170
+ // block source won't have synced to the proposed slot yet. Skip the sync wait to
171
+ // avoid eating into the attestation window.
172
+ if (!this.epochCache.isProposerPipeliningEnabled()) {
173
+ const blockSourceSync = await this.waitForBlockSourceSync(slotNumber);
174
+ if (!blockSourceSync) {
175
+ this.log.warn(`Block source is not synced, skipping processing`, proposalInfo);
176
+ return { isValid: false, reason: 'block_source_not_synced' };
177
+ }
178
+ }
179
+
180
+ // Check that the parent proposal is a block we know, otherwise reexecution would fail.
181
+ // If we don't find it immediately, we keep retrying for a while; it may be we still
182
+ // need to process other block proposals to get to it.
183
+ const parentBlock = await this.getParentBlock(proposal);
184
+ if (parentBlock === undefined) {
158
185
  this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
159
186
  return { isValid: false, reason: 'parent_block_not_found' };
160
187
  }
161
188
 
162
189
  // Check that the parent block's slot is not greater than the proposal's slot.
163
- if (parentBlockHeader !== 'genesis' && parentBlockHeader.getSlot() > slotNumber) {
190
+ if (parentBlock !== 'genesis' && parentBlock.header.getSlot() > slotNumber) {
164
191
  this.log.warn(`Parent block slot is greater than proposal slot, skipping processing`, {
165
- parentBlockSlot: parentBlockHeader.getSlot().toString(),
192
+ parentBlockSlot: parentBlock.header.getSlot().toString(),
166
193
  proposalSlot: slotNumber.toString(),
167
194
  ...proposalInfo,
168
195
  });
@@ -171,9 +198,10 @@ export class BlockProposalHandler {
171
198
 
172
199
  // Compute the block number based on the parent block
173
200
  const blockNumber =
174
- parentBlockHeader === 'genesis'
201
+ parentBlock === 'genesis'
175
202
  ? BlockNumber(INITIAL_L2_BLOCK_NUM)
176
- : BlockNumber(parentBlockHeader.getBlockNumber() + 1);
203
+ : BlockNumber(parentBlock.header.getBlockNumber() + 1);
204
+ proposalInfo.blockNumber = blockNumber;
177
205
 
178
206
  // Check that this block number does not exist already
179
207
  const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
@@ -189,12 +217,22 @@ export class BlockProposalHandler {
189
217
  deadline: this.getReexecutionDeadline(slotNumber, config),
190
218
  });
191
219
 
220
+ // If reexecution is disabled, bail. We were just interested in triggering tx collection.
221
+ if (!shouldReexecute) {
222
+ this.log.info(
223
+ `Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
224
+ proposalInfo,
225
+ );
226
+ return { isValid: true, blockNumber };
227
+ }
228
+
192
229
  // Compute the checkpoint number for this block and validate checkpoint consistency
193
- const checkpointResult = await this.computeCheckpointNumber(proposal, parentBlockHeader, proposalInfo);
230
+ const checkpointResult = this.computeCheckpointNumber(proposal, parentBlock, proposalInfo);
194
231
  if (checkpointResult.reason) {
195
232
  return { isValid: false, blockNumber, reason: checkpointResult.reason };
196
233
  }
197
234
  const checkpointNumber = checkpointResult.checkpointNumber;
235
+ proposalInfo.checkpointNumber = checkpointNumber;
198
236
 
199
237
  // Check that I have the same set of l1ToL2Messages as the proposal
200
238
  const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
@@ -215,36 +253,28 @@ export class BlockProposalHandler {
215
253
  return { isValid: false, blockNumber, reason: 'txs_not_available' };
216
254
  }
217
255
 
256
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
257
+ const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
258
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
259
+ .filter(c => c.checkpointNumber < checkpointNumber)
260
+ .map(c => c.checkpointOutHash);
261
+
218
262
  // Try re-executing the transactions in the proposal if needed
219
263
  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))),
264
+ try {
265
+ this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
266
+ reexecutionResult = await this.reexecuteTransactions(
267
+ proposal,
268
+ blockNumber,
269
+ checkpointNumber,
270
+ txs,
271
+ l1ToL2Messages,
272
+ previousCheckpointOutHashes,
231
273
  );
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
- }
274
+ } catch (error) {
275
+ this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
276
+ const reason = this.getReexecuteFailureReason(error);
277
+ return { isValid: false, blockNumber, reason, reexecutionResult };
248
278
  }
249
279
 
250
280
  // If we succeeded, push this block into the archiver (unless disabled)
@@ -253,14 +283,14 @@ export class BlockProposalHandler {
253
283
  }
254
284
 
255
285
  this.log.info(
256
- `Successfully processed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
257
- proposalInfo,
286
+ `Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
287
+ { ...proposalInfo, ...pick(reexecutionResult, 'reexecutionTimeMs', 'totalManaUsed') },
258
288
  );
259
289
 
260
290
  return { isValid: true, blockNumber, reexecutionResult };
261
291
  }
262
292
 
263
- private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockHeader | undefined> {
293
+ private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockData | undefined> {
264
294
  const parentArchive = proposal.blockHeader.lastArchive.root;
265
295
  const slot = proposal.slotNumber;
266
296
  const config = this.checkpointsBuilder.getConfig();
@@ -276,12 +306,11 @@ export class BlockProposalHandler {
276
306
 
277
307
  try {
278
308
  return (
279
- (await this.blockSource.getBlockHeaderByArchive(parentArchive)) ??
309
+ (await this.blockSource.getBlockDataByArchive(parentArchive)) ??
280
310
  (timeoutDurationMs <= 0
281
311
  ? undefined
282
312
  : await retryUntil(
283
- () =>
284
- this.blockSource.syncImmediate().then(() => this.blockSource.getBlockHeaderByArchive(parentArchive)),
313
+ () => this.blockSource.syncImmediate().then(() => this.blockSource.getBlockDataByArchive(parentArchive)),
285
314
  'force archiver sync',
286
315
  timeoutDurationMs / 1000,
287
316
  0.5,
@@ -297,12 +326,12 @@ export class BlockProposalHandler {
297
326
  }
298
327
  }
299
328
 
300
- private async computeCheckpointNumber(
329
+ private computeCheckpointNumber(
301
330
  proposal: BlockProposal,
302
- parentBlockHeader: 'genesis' | BlockHeader,
331
+ parentBlock: 'genesis' | BlockData,
303
332
  proposalInfo: object,
304
- ): Promise<CheckpointComputationResult> {
305
- if (parentBlockHeader === 'genesis') {
333
+ ): CheckpointComputationResult {
334
+ if (parentBlock === 'genesis') {
306
335
  // First block is in checkpoint 1
307
336
  if (proposal.indexWithinCheckpoint !== 0) {
308
337
  this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
@@ -311,19 +340,9 @@ export class BlockProposalHandler {
311
340
  return { checkpointNumber: CheckpointNumber.INITIAL };
312
341
  }
313
342
 
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
343
  if (proposal.indexWithinCheckpoint === 0) {
325
344
  // If this is the first block in a new checkpoint, increment the checkpoint number
326
- if (!(proposal.blockHeader.getSlot() > parentBlockHeader.getSlot())) {
345
+ if (!(proposal.blockHeader.getSlot() > parentBlock.header.getSlot())) {
327
346
  this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
328
347
  return { reason: 'invalid_proposal' };
329
348
  }
@@ -335,7 +354,7 @@ export class BlockProposalHandler {
335
354
  this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
336
355
  return { reason: 'invalid_proposal' };
337
356
  }
338
- if (proposal.blockHeader.getSlot() !== parentBlockHeader.getSlot()) {
357
+ if (proposal.blockHeader.getSlot() !== parentBlock.header.getSlot()) {
339
358
  this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
340
359
  return { reason: 'invalid_proposal' };
341
360
  }
@@ -356,7 +375,7 @@ export class BlockProposalHandler {
356
375
  */
357
376
  private validateNonFirstBlockInCheckpoint(
358
377
  proposal: BlockProposal,
359
- parentBlock: L2Block,
378
+ parentBlock: BlockData,
360
379
  proposalInfo: object,
361
380
  ): CheckpointComputationResult | undefined {
362
381
  const proposalGlobals = proposal.blockHeader.globalVariables;
@@ -435,8 +454,48 @@ export class BlockProposalHandler {
435
454
  return new Date(nextSlotTimestampSeconds * 1000);
436
455
  }
437
456
 
438
- private getReexecuteFailureReason(err: any) {
439
- if (err instanceof ReExStateMismatchError) {
457
+ /** Waits for the block source to sync L1 data up to at least the slot before the given one. */
458
+ private async waitForBlockSourceSync(slot: SlotNumber): Promise<boolean> {
459
+ const deadline = this.getReexecutionDeadline(slot, this.checkpointsBuilder.getConfig());
460
+ const timeoutMs = deadline.getTime() - this.dateProvider.now();
461
+ if (slot === 0) {
462
+ return true;
463
+ }
464
+
465
+ // Make a quick check before triggering an archiver sync
466
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
467
+ if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
468
+ return true;
469
+ }
470
+
471
+ try {
472
+ // Trigger an immediate sync of the block source, and wait until it reports being synced to the required slot
473
+ return await retryUntil(
474
+ async () => {
475
+ await this.blockSource.syncImmediate();
476
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
477
+ return syncedSlot !== undefined && syncedSlot + 1 >= slot;
478
+ },
479
+ 'wait for block source sync',
480
+ timeoutMs / 1000,
481
+ 0.5,
482
+ );
483
+ } catch (err) {
484
+ if (err instanceof TimeoutError) {
485
+ this.log.warn(`Timed out waiting for block source to sync to slot ${slot}`);
486
+ return false;
487
+ } else {
488
+ throw err;
489
+ }
490
+ }
491
+ }
492
+
493
+ private getReexecuteFailureReason(err: any): BlockProposalValidationFailureReason {
494
+ if (err instanceof TransactionsNotAvailableError) {
495
+ return 'txs_not_available';
496
+ } else if (err instanceof ReExInitialStateMismatchError) {
497
+ return 'initial_state_mismatch';
498
+ } else if (err instanceof ReExStateMismatchError) {
440
499
  return 'state_mismatch';
441
500
  } else if (err instanceof ReExFailedTxsError) {
442
501
  return 'failed_txs';
@@ -475,13 +534,21 @@ export class BlockProposalHandler {
475
534
  // Fork before the block to be built
476
535
  const parentBlockNumber = BlockNumber(blockNumber - 1);
477
536
  await this.worldState.syncImmediate(parentBlockNumber);
478
- using fork = await this.worldState.fork(parentBlockNumber);
537
+ await using fork = await this.worldState.fork(parentBlockNumber);
538
+
539
+ // Verify the fork's archive root matches the proposal's expected last archive.
540
+ // If they don't match, our world state synced to a different chain and reexecution would fail.
541
+ const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
542
+ if (!forkArchiveRoot.equals(proposal.blockHeader.lastArchive.root)) {
543
+ throw new ReExInitialStateMismatchError(proposal.blockHeader.lastArchive.root, forkArchiveRoot);
544
+ }
479
545
 
480
- // Build checkpoint constants from proposal (excludes blockNumber and timestamp which are per-block)
546
+ // Build checkpoint constants from proposal (excludes blockNumber which is per-block)
481
547
  const constants: CheckpointGlobalVariables = {
482
548
  chainId: new Fr(config.l1ChainId),
483
549
  version: new Fr(config.rollupVersion),
484
550
  slotNumber: slot,
551
+ timestamp: blockHeader.globalVariables.timestamp,
485
552
  coinbase: blockHeader.globalVariables.coinbase,
486
553
  feeRecipient: blockHeader.globalVariables.feeRecipient,
487
554
  gasFees: blockHeader.globalVariables.gasFees,
@@ -491,6 +558,7 @@ export class BlockProposalHandler {
491
558
  const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
492
559
  checkpointNumber,
493
560
  constants,
561
+ 0n, // only takes effect in the following checkpoint.
494
562
  l1ToL2Messages,
495
563
  previousCheckpointOutHashes,
496
564
  fork,
@@ -500,18 +568,27 @@ export class BlockProposalHandler {
500
568
 
501
569
  // Build the new block
502
570
  const deadline = this.getReexecutionDeadline(slot, config);
571
+ const maxBlockGas =
572
+ this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined
573
+ ? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity)
574
+ : undefined;
503
575
  const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
576
+ isBuildingProposal: false,
577
+ minValidTxs: 0,
504
578
  deadline,
505
579
  expectedEndState: blockHeader.state,
580
+ maxTransactions: this.config.validateMaxTxsPerBlock,
581
+ maxBlockGas,
506
582
  });
507
583
 
508
584
  const { block, failedTxs } = result;
509
585
  const numFailedTxs = failedTxs.length;
510
586
 
511
- this.log.verbose(`Transaction re-execution complete for slot ${slot}`, {
587
+ this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
512
588
  numFailedTxs,
513
589
  numProposalTxs: txHashes.length,
514
590
  numProcessedTxs: block.body.txEffects.length,
591
+ blockNumber,
515
592
  slot,
516
593
  });
517
594