@aztec/validator-client 0.0.1-commit.b655e406 → 0.0.1-commit.b6e433891

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 (57) hide show
  1. package/README.md +326 -0
  2. package/dest/block_proposal_handler.d.ts +27 -15
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +441 -113
  5. package/dest/checkpoint_builder.d.ts +79 -0
  6. package/dest/checkpoint_builder.d.ts.map +1 -0
  7. package/dest/checkpoint_builder.js +251 -0
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +41 -7
  11. package/dest/duties/validation_service.d.ts +42 -13
  12. package/dest/duties/validation_service.d.ts.map +1 -1
  13. package/dest/duties/validation_service.js +107 -31
  14. package/dest/factory.d.ts +15 -8
  15. package/dest/factory.d.ts.map +1 -1
  16. package/dest/factory.js +4 -3
  17. package/dest/index.d.ts +2 -1
  18. package/dest/index.d.ts.map +1 -1
  19. package/dest/index.js +1 -0
  20. package/dest/key_store/ha_key_store.d.ts +99 -0
  21. package/dest/key_store/ha_key_store.d.ts.map +1 -0
  22. package/dest/key_store/ha_key_store.js +208 -0
  23. package/dest/key_store/index.d.ts +2 -1
  24. package/dest/key_store/index.d.ts.map +1 -1
  25. package/dest/key_store/index.js +1 -0
  26. package/dest/key_store/interface.d.ts +36 -6
  27. package/dest/key_store/interface.d.ts.map +1 -1
  28. package/dest/key_store/local_key_store.d.ts +10 -5
  29. package/dest/key_store/local_key_store.d.ts.map +1 -1
  30. package/dest/key_store/local_key_store.js +9 -5
  31. package/dest/key_store/node_keystore_adapter.d.ts +18 -5
  32. package/dest/key_store/node_keystore_adapter.d.ts.map +1 -1
  33. package/dest/key_store/node_keystore_adapter.js +18 -4
  34. package/dest/key_store/web3signer_key_store.d.ts +10 -11
  35. package/dest/key_store/web3signer_key_store.d.ts.map +1 -1
  36. package/dest/key_store/web3signer_key_store.js +9 -5
  37. package/dest/metrics.d.ts +12 -3
  38. package/dest/metrics.d.ts.map +1 -1
  39. package/dest/metrics.js +46 -30
  40. package/dest/validator.d.ts +77 -22
  41. package/dest/validator.d.ts.map +1 -1
  42. package/dest/validator.js +529 -66
  43. package/package.json +24 -14
  44. package/src/block_proposal_handler.ts +380 -91
  45. package/src/checkpoint_builder.ts +417 -0
  46. package/src/config.ts +41 -6
  47. package/src/duties/validation_service.ts +158 -38
  48. package/src/factory.ts +21 -8
  49. package/src/index.ts +1 -0
  50. package/src/key_store/ha_key_store.ts +269 -0
  51. package/src/key_store/index.ts +1 -0
  52. package/src/key_store/interface.ts +44 -5
  53. package/src/key_store/local_key_store.ts +14 -5
  54. package/src/key_store/node_keystore_adapter.ts +28 -5
  55. package/src/key_store/web3signer_key_store.ts +18 -5
  56. package/src/metrics.ts +63 -33
  57. package/src/validator.ts +705 -98
@@ -1,67 +1,149 @@
1
+ function _ts_add_disposable_resource(env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() {
16
+ try {
17
+ inner.call(this);
18
+ } catch (e) {
19
+ return Promise.reject(e);
20
+ }
21
+ };
22
+ env.stack.push({
23
+ value: value,
24
+ dispose: dispose,
25
+ async: async
26
+ });
27
+ } else if (async) {
28
+ env.stack.push({
29
+ async: true
30
+ });
31
+ }
32
+ return value;
33
+ }
34
+ function _ts_dispose_resources(env) {
35
+ var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
36
+ var e = new Error(message);
37
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
38
+ };
39
+ return (_ts_dispose_resources = function _ts_dispose_resources(env) {
40
+ function fail(e) {
41
+ env.error = env.hasError ? new _SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
42
+ env.hasError = true;
43
+ }
44
+ var r, s = 0;
45
+ function next() {
46
+ while(r = env.stack.pop()){
47
+ try {
48
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
49
+ if (r.dispose) {
50
+ var result = r.dispose.call(r.value);
51
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
52
+ fail(e);
53
+ return next();
54
+ });
55
+ } else s |= 1;
56
+ } catch (e) {
57
+ fail(e);
58
+ }
59
+ }
60
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
61
+ if (env.hasError) throw env.error;
62
+ }
63
+ return next();
64
+ })(env);
65
+ }
1
66
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
67
+ import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
68
+ import { pick } from '@aztec/foundation/collection';
69
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
70
  import { TimeoutError } from '@aztec/foundation/error';
3
- import { Fr } from '@aztec/foundation/fields';
4
71
  import { createLogger } from '@aztec/foundation/log';
5
72
  import { retryUntil } from '@aztec/foundation/retry';
6
73
  import { DateProvider, Timer } from '@aztec/foundation/timer';
7
- import { computeInHashFromL1ToL2Messages } from '@aztec/prover-client/helpers';
8
- import { getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
9
- import { ConsensusPayload } from '@aztec/stdlib/p2p';
10
- import { GlobalVariables } from '@aztec/stdlib/tx';
11
- import { ReExFailedTxsError, ReExStateMismatchError, ReExTimeoutError, TransactionsNotAvailableError } from '@aztec/stdlib/validators';
74
+ import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
75
+ import { Gas } from '@aztec/stdlib/gas';
76
+ import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
77
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
78
+ import { ReExFailedTxsError, ReExInitialStateMismatchError, ReExStateMismatchError, ReExTimeoutError, TransactionsNotAvailableError } from '@aztec/stdlib/validators';
12
79
  import { getTelemetryClient } from '@aztec/telemetry-client';
13
80
  export class BlockProposalHandler {
14
- blockBuilder;
81
+ checkpointsBuilder;
82
+ worldState;
15
83
  blockSource;
16
84
  l1ToL2MessageSource;
17
85
  txProvider;
18
86
  blockProposalValidator;
87
+ epochCache;
19
88
  config;
20
89
  metrics;
21
90
  dateProvider;
22
91
  log;
23
92
  tracer;
24
- constructor(blockBuilder, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, config, metrics, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator:block-proposal-handler')){
25
- this.blockBuilder = blockBuilder;
93
+ constructor(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, metrics, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator:block-proposal-handler')){
94
+ this.checkpointsBuilder = checkpointsBuilder;
95
+ this.worldState = worldState;
26
96
  this.blockSource = blockSource;
27
97
  this.l1ToL2MessageSource = l1ToL2MessageSource;
28
98
  this.txProvider = txProvider;
29
99
  this.blockProposalValidator = blockProposalValidator;
100
+ this.epochCache = epochCache;
30
101
  this.config = config;
31
102
  this.metrics = metrics;
32
103
  this.dateProvider = dateProvider;
33
104
  this.log = log;
105
+ if (config.fishermanMode) {
106
+ this.log = this.log.createChild('[FISHERMAN]');
107
+ }
34
108
  this.tracer = telemetry.getTracer('BlockProposalHandler');
35
109
  }
36
- registerForReexecution(p2pClient) {
110
+ register(p2pClient, shouldReexecute) {
111
+ // Non-validator handler that processes or re-executes for monitoring but does not attest.
112
+ // Returns boolean indicating whether the proposal was valid.
37
113
  const handler = async (proposal, proposalSender)=>{
38
114
  try {
39
- const result = await this.handleBlockProposal(proposal, proposalSender, true);
115
+ const { slotNumber, blockNumber } = proposal;
116
+ const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
40
117
  if (result.isValid) {
41
- this.log.info(`Non-validator reexecution completed for slot ${proposal.slotNumber.toBigInt()}`, {
118
+ this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
42
119
  blockNumber: result.blockNumber,
120
+ slotNumber,
43
121
  reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
44
122
  totalManaUsed: result.reexecutionResult?.totalManaUsed,
45
- numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0
123
+ numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
124
+ reexecuted: shouldReexecute
46
125
  });
126
+ return true;
47
127
  } else {
48
- this.log.warn(`Non-validator reexecution failed for slot ${proposal.slotNumber.toBigInt()}`, {
128
+ this.log.warn(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} failed processing with ${result.reason}`, {
49
129
  blockNumber: result.blockNumber,
130
+ slotNumber,
50
131
  reason: result.reason
51
132
  });
133
+ return false;
52
134
  }
53
135
  } catch (error) {
54
136
  this.log.error('Error processing block proposal in non-validator handler', error);
137
+ return false;
55
138
  }
56
- return undefined; // Non-validator nodes don't return attestations
57
139
  };
58
140
  p2pClient.registerBlockProposalHandler(handler);
59
141
  return this;
60
142
  }
61
143
  async handleBlockProposal(proposal, proposalSender, shouldReexecute) {
62
- const slotNumber = proposal.slotNumber.toBigInt();
144
+ const slotNumber = proposal.slotNumber;
63
145
  const proposer = proposal.getSender();
64
- const config = this.blockBuilder.getConfig();
146
+ const config = this.checkpointsBuilder.getConfig();
65
147
  // Reject proposals with invalid signatures
66
148
  if (!proposer) {
67
149
  this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
@@ -72,7 +154,9 @@ export class BlockProposalHandler {
72
154
  }
73
155
  const proposalInfo = {
74
156
  ...proposal.toBlockInfo(),
75
- proposer: proposer.toString()
157
+ proposer: proposer.toString(),
158
+ blockNumber: undefined,
159
+ checkpointNumber: undefined
76
160
  };
77
161
  this.log.info(`Processing proposal for slot ${slotNumber}`, {
78
162
  ...proposalInfo,
@@ -80,27 +164,46 @@ export class BlockProposalHandler {
80
164
  });
81
165
  // Check that the proposal is from the current proposer, or the next proposer
82
166
  // This should have been handled by the p2p layer, but we double check here out of caution
83
- const invalidProposal = await this.blockProposalValidator.validate(proposal);
84
- if (invalidProposal) {
167
+ const validationResult = await this.blockProposalValidator.validate(proposal);
168
+ if (validationResult.result !== 'accept') {
85
169
  this.log.warn(`Proposal is not valid, skipping processing`, proposalInfo);
86
170
  return {
87
171
  isValid: false,
88
172
  reason: 'invalid_proposal'
89
173
  };
90
174
  }
91
- // Check that the parent proposal is a block we know, otherwise reexecution would fail
92
- const parentBlockHeader = await this.getParentBlock(proposal);
93
- if (parentBlockHeader === undefined) {
175
+ // Ensure the block source is synced before checking for existing blocks,
176
+ // since a pending checkpoint prune may remove blocks we'd otherwise find.
177
+ // This affects mostly the block_number_already_exists check, since a pending
178
+ // checkpoint prune could remove a block that would conflict with this proposal.
179
+ // When pipelining is enabled, the proposer builds ahead of L1 submission, so the
180
+ // block source won't have synced to the proposed slot yet. Skip the sync wait to
181
+ // avoid eating into the attestation window.
182
+ if (!this.epochCache.isProposerPipeliningEnabled()) {
183
+ const blockSourceSync = await this.waitForBlockSourceSync(slotNumber);
184
+ if (!blockSourceSync) {
185
+ this.log.warn(`Block source is not synced, skipping processing`, proposalInfo);
186
+ return {
187
+ isValid: false,
188
+ reason: 'block_source_not_synced'
189
+ };
190
+ }
191
+ }
192
+ // Check that the parent proposal is a block we know, otherwise reexecution would fail.
193
+ // If we don't find it immediately, we keep retrying for a while; it may be we still
194
+ // need to process other block proposals to get to it.
195
+ const parentBlock = await this.getParentBlock(proposal);
196
+ if (parentBlock === undefined) {
94
197
  this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
95
198
  return {
96
199
  isValid: false,
97
200
  reason: 'parent_block_not_found'
98
201
  };
99
202
  }
100
- // Check that the parent block's slot is less than the proposal's slot (should not happen, but we check anyway)
101
- if (parentBlockHeader !== 'genesis' && parentBlockHeader.getSlot() >= slotNumber) {
102
- this.log.warn(`Parent block slot is greater than or equal to proposal slot, skipping processing`, {
103
- parentBlockSlot: parentBlockHeader.getSlot().toString(),
203
+ // Check that the parent block's slot is not greater than the proposal's slot.
204
+ if (parentBlock !== 'genesis' && parentBlock.header.getSlot() > slotNumber) {
205
+ this.log.warn(`Parent block slot is greater than proposal slot, skipping processing`, {
206
+ parentBlockSlot: parentBlock.header.getSlot().toString(),
104
207
  proposalSlot: slotNumber.toString(),
105
208
  ...proposalInfo
106
209
  });
@@ -110,7 +213,8 @@ export class BlockProposalHandler {
110
213
  };
111
214
  }
112
215
  // Compute the block number based on the parent block
113
- const blockNumber = parentBlockHeader === 'genesis' ? INITIAL_L2_BLOCK_NUM : parentBlockHeader.getBlockNumber() + 1;
216
+ const blockNumber = parentBlock === 'genesis' ? BlockNumber(INITIAL_L2_BLOCK_NUM) : BlockNumber(parentBlock.header.getBlockNumber() + 1);
217
+ proposalInfo.blockNumber = blockNumber;
114
218
  // Check that this block number does not exist already
115
219
  const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
116
220
  if (existingBlock) {
@@ -127,10 +231,29 @@ export class BlockProposalHandler {
127
231
  pinnedPeer: proposalSender,
128
232
  deadline: this.getReexecutionDeadline(slotNumber, config)
129
233
  });
234
+ // If reexecution is disabled, bail. We were just interested in triggering tx collection.
235
+ if (!shouldReexecute) {
236
+ this.log.info(`Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`, proposalInfo);
237
+ return {
238
+ isValid: true,
239
+ blockNumber
240
+ };
241
+ }
242
+ // Compute the checkpoint number for this block and validate checkpoint consistency
243
+ const checkpointResult = this.computeCheckpointNumber(proposal, parentBlock, proposalInfo);
244
+ if (checkpointResult.reason) {
245
+ return {
246
+ isValid: false,
247
+ blockNumber,
248
+ reason: checkpointResult.reason
249
+ };
250
+ }
251
+ const checkpointNumber = checkpointResult.checkpointNumber;
252
+ proposalInfo.checkpointNumber = checkpointNumber;
130
253
  // Check that I have the same set of l1ToL2Messages as the proposal
131
- const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(blockNumber);
132
- const computedInHash = await computeInHashFromL1ToL2Messages(l1ToL2Messages);
133
- const proposalInHash = proposal.payload.header.contentCommitment.inHash;
254
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
255
+ const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
256
+ const proposalInHash = proposal.inHash;
134
257
  if (!computedInHash.equals(proposalInHash)) {
135
258
  this.log.warn(`L1 to L2 messages in hash mismatch, skipping processing`, {
136
259
  proposalInHash: proposalInHash.toString(),
@@ -155,24 +278,32 @@ export class BlockProposalHandler {
155
278
  reason: 'txs_not_available'
156
279
  };
157
280
  }
281
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
282
+ const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
283
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch)).filter((c)=>c.checkpointNumber < checkpointNumber).map((c)=>c.checkpointOutHash);
158
284
  // Try re-executing the transactions in the proposal if needed
159
285
  let reexecutionResult;
160
- if (shouldReexecute) {
161
- try {
162
- this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
163
- reexecutionResult = await this.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
164
- } catch (error) {
165
- this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
166
- const reason = this.getReexecuteFailureReason(error);
167
- return {
168
- isValid: false,
169
- blockNumber,
170
- reason,
171
- reexecutionResult
172
- };
173
- }
286
+ try {
287
+ this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
288
+ reexecutionResult = await this.reexecuteTransactions(proposal, blockNumber, checkpointNumber, txs, l1ToL2Messages, previousCheckpointOutHashes);
289
+ } catch (error) {
290
+ this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
291
+ const reason = this.getReexecuteFailureReason(error);
292
+ return {
293
+ isValid: false,
294
+ blockNumber,
295
+ reason,
296
+ reexecutionResult
297
+ };
174
298
  }
175
- this.log.info(`Successfully processed proposal for slot ${slotNumber}`, proposalInfo);
299
+ // If we succeeded, push this block into the archiver (unless disabled)
300
+ if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
301
+ await this.blockSource.addBlock(reexecutionResult?.block);
302
+ }
303
+ this.log.info(`Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`, {
304
+ ...proposalInfo,
305
+ ...pick(reexecutionResult, 'reexecutionTimeMs', 'totalManaUsed')
306
+ });
176
307
  return {
177
308
  isValid: true,
178
309
  blockNumber,
@@ -180,9 +311,9 @@ export class BlockProposalHandler {
180
311
  };
181
312
  }
182
313
  async getParentBlock(proposal) {
183
- const parentArchive = proposal.payload.header.lastArchiveRoot;
184
- const slot = proposal.slotNumber.toBigInt();
185
- const config = this.blockBuilder.getConfig();
314
+ const parentArchive = proposal.blockHeader.lastArchive.root;
315
+ const slot = proposal.slotNumber;
316
+ const config = this.checkpointsBuilder.getConfig();
186
317
  const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
187
318
  if (parentArchive.equals(genesisArchiveRoot)) {
188
319
  return 'genesis';
@@ -191,7 +322,7 @@ export class BlockProposalHandler {
191
322
  const currentTime = this.dateProvider.now();
192
323
  const timeoutDurationMs = deadline.getTime() - currentTime;
193
324
  try {
194
- return await this.blockSource.getBlockHeaderByArchive(parentArchive) ?? (timeoutDurationMs <= 0 ? undefined : await retryUntil(()=>this.blockSource.syncImmediate().then(()=>this.blockSource.getBlockHeaderByArchive(parentArchive)), 'force archiver sync', timeoutDurationMs / 1000, 0.5));
325
+ return await this.blockSource.getBlockDataByArchive(parentArchive) ?? (timeoutDurationMs <= 0 ? undefined : await retryUntil(()=>this.blockSource.syncImmediate().then(()=>this.blockSource.getBlockDataByArchive(parentArchive)), 'force archiver sync', timeoutDurationMs / 1000, 0.5));
195
326
  } catch (err) {
196
327
  if (err instanceof TimeoutError) {
197
328
  this.log.debug(`Timed out getting parent block by archive root`, {
@@ -205,13 +336,171 @@ export class BlockProposalHandler {
205
336
  return undefined;
206
337
  }
207
338
  }
339
+ computeCheckpointNumber(proposal, parentBlock, proposalInfo) {
340
+ if (parentBlock === 'genesis') {
341
+ // First block is in checkpoint 1
342
+ if (proposal.indexWithinCheckpoint !== 0) {
343
+ this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
344
+ return {
345
+ reason: 'invalid_proposal'
346
+ };
347
+ }
348
+ return {
349
+ checkpointNumber: CheckpointNumber.INITIAL
350
+ };
351
+ }
352
+ if (proposal.indexWithinCheckpoint === 0) {
353
+ // If this is the first block in a new checkpoint, increment the checkpoint number
354
+ if (!(proposal.blockHeader.getSlot() > parentBlock.header.getSlot())) {
355
+ this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
356
+ return {
357
+ reason: 'invalid_proposal'
358
+ };
359
+ }
360
+ return {
361
+ checkpointNumber: CheckpointNumber(parentBlock.checkpointNumber + 1)
362
+ };
363
+ }
364
+ // Otherwise it should follow the previous block in the same checkpoint
365
+ if (proposal.indexWithinCheckpoint !== parentBlock.indexWithinCheckpoint + 1) {
366
+ this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
367
+ return {
368
+ reason: 'invalid_proposal'
369
+ };
370
+ }
371
+ if (proposal.blockHeader.getSlot() !== parentBlock.header.getSlot()) {
372
+ this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
373
+ return {
374
+ reason: 'invalid_proposal'
375
+ };
376
+ }
377
+ // For non-first blocks in a checkpoint, validate global variables match parent (except blockNumber)
378
+ const validationResult = this.validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo);
379
+ if (validationResult) {
380
+ return validationResult;
381
+ }
382
+ return {
383
+ checkpointNumber: parentBlock.checkpointNumber
384
+ };
385
+ }
386
+ /**
387
+ * Validates that a non-first block in a checkpoint has consistent global variables with its parent.
388
+ * For blocks with indexWithinCheckpoint > 0, all global variables except blockNumber must match the parent.
389
+ * @returns A failure result if validation fails, undefined if validation passes
390
+ */ validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo) {
391
+ const proposalGlobals = proposal.blockHeader.globalVariables;
392
+ const parentGlobals = parentBlock.header.globalVariables;
393
+ // All global variables except blockNumber should match the parent
394
+ // blockNumber naturally increments between blocks
395
+ if (!proposalGlobals.chainId.equals(parentGlobals.chainId)) {
396
+ this.log.warn(`Non-first block in checkpoint has mismatched chainId`, {
397
+ ...proposalInfo,
398
+ proposalChainId: proposalGlobals.chainId.toString(),
399
+ parentChainId: parentGlobals.chainId.toString()
400
+ });
401
+ return {
402
+ reason: 'global_variables_mismatch'
403
+ };
404
+ }
405
+ if (!proposalGlobals.version.equals(parentGlobals.version)) {
406
+ this.log.warn(`Non-first block in checkpoint has mismatched version`, {
407
+ ...proposalInfo,
408
+ proposalVersion: proposalGlobals.version.toString(),
409
+ parentVersion: parentGlobals.version.toString()
410
+ });
411
+ return {
412
+ reason: 'global_variables_mismatch'
413
+ };
414
+ }
415
+ if (proposalGlobals.slotNumber !== parentGlobals.slotNumber) {
416
+ this.log.warn(`Non-first block in checkpoint has mismatched slotNumber`, {
417
+ ...proposalInfo,
418
+ proposalSlotNumber: proposalGlobals.slotNumber,
419
+ parentSlotNumber: parentGlobals.slotNumber
420
+ });
421
+ return {
422
+ reason: 'global_variables_mismatch'
423
+ };
424
+ }
425
+ if (proposalGlobals.timestamp !== parentGlobals.timestamp) {
426
+ this.log.warn(`Non-first block in checkpoint has mismatched timestamp`, {
427
+ ...proposalInfo,
428
+ proposalTimestamp: proposalGlobals.timestamp.toString(),
429
+ parentTimestamp: parentGlobals.timestamp.toString()
430
+ });
431
+ return {
432
+ reason: 'global_variables_mismatch'
433
+ };
434
+ }
435
+ if (!proposalGlobals.coinbase.equals(parentGlobals.coinbase)) {
436
+ this.log.warn(`Non-first block in checkpoint has mismatched coinbase`, {
437
+ ...proposalInfo,
438
+ proposalCoinbase: proposalGlobals.coinbase.toString(),
439
+ parentCoinbase: parentGlobals.coinbase.toString()
440
+ });
441
+ return {
442
+ reason: 'global_variables_mismatch'
443
+ };
444
+ }
445
+ if (!proposalGlobals.feeRecipient.equals(parentGlobals.feeRecipient)) {
446
+ this.log.warn(`Non-first block in checkpoint has mismatched feeRecipient`, {
447
+ ...proposalInfo,
448
+ proposalFeeRecipient: proposalGlobals.feeRecipient.toString(),
449
+ parentFeeRecipient: parentGlobals.feeRecipient.toString()
450
+ });
451
+ return {
452
+ reason: 'global_variables_mismatch'
453
+ };
454
+ }
455
+ if (!proposalGlobals.gasFees.equals(parentGlobals.gasFees)) {
456
+ this.log.warn(`Non-first block in checkpoint has mismatched gasFees`, {
457
+ ...proposalInfo,
458
+ proposalGasFees: proposalGlobals.gasFees.toInspect(),
459
+ parentGasFees: parentGlobals.gasFees.toInspect()
460
+ });
461
+ return {
462
+ reason: 'global_variables_mismatch'
463
+ };
464
+ }
465
+ return undefined;
466
+ }
208
467
  getReexecutionDeadline(slot, config) {
209
- const nextSlotTimestampSeconds = Number(getTimestampForSlot(slot + 1n, config));
210
- const msNeededForPropagationAndPublishing = this.config.validatorReexecuteDeadlineMs;
211
- return new Date(nextSlotTimestampSeconds * 1000 - msNeededForPropagationAndPublishing);
468
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
469
+ return new Date(nextSlotTimestampSeconds * 1000);
470
+ }
471
+ /** Waits for the block source to sync L1 data up to at least the slot before the given one. */ async waitForBlockSourceSync(slot) {
472
+ const deadline = this.getReexecutionDeadline(slot, this.checkpointsBuilder.getConfig());
473
+ const timeoutMs = deadline.getTime() - this.dateProvider.now();
474
+ if (slot === 0) {
475
+ return true;
476
+ }
477
+ // Make a quick check before triggering an archiver sync
478
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
479
+ if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
480
+ return true;
481
+ }
482
+ try {
483
+ // Trigger an immediate sync of the block source, and wait until it reports being synced to the required slot
484
+ return await retryUntil(async ()=>{
485
+ await this.blockSource.syncImmediate();
486
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
487
+ return syncedSlot !== undefined && syncedSlot + 1 >= slot;
488
+ }, 'wait for block source sync', timeoutMs / 1000, 0.5);
489
+ } catch (err) {
490
+ if (err instanceof TimeoutError) {
491
+ this.log.warn(`Timed out waiting for block source to sync to slot ${slot}`);
492
+ return false;
493
+ } else {
494
+ throw err;
495
+ }
496
+ }
212
497
  }
213
498
  getReexecuteFailureReason(err) {
214
- if (err instanceof ReExStateMismatchError) {
499
+ if (err instanceof TransactionsNotAvailableError) {
500
+ return 'txs_not_available';
501
+ } else if (err instanceof ReExInitialStateMismatchError) {
502
+ return 'initial_state_mismatch';
503
+ } else if (err instanceof ReExStateMismatchError) {
215
504
  return 'state_mismatch';
216
505
  } else if (err instanceof ReExFailedTxsError) {
217
506
  return 'failed_txs';
@@ -221,66 +510,105 @@ export class BlockProposalHandler {
221
510
  return 'unknown_error';
222
511
  }
223
512
  }
224
- async reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages) {
225
- const { header } = proposal.payload;
226
- const { txHashes } = proposal;
227
- // If we do not have all of the transactions, then we should fail
228
- if (txs.length !== txHashes.length) {
229
- const foundTxHashes = txs.map((tx)=>tx.getTxHash());
230
- const missingTxHashes = txHashes.filter((txHash)=>!foundTxHashes.includes(txHash));
231
- throw new TransactionsNotAvailableError(missingTxHashes);
232
- }
233
- // Use the sequencer's block building logic to re-execute the transactions
234
- const timer = new Timer();
235
- const config = this.blockBuilder.getConfig();
236
- // We source most global variables from the proposal
237
- const globalVariables = GlobalVariables.from({
238
- slotNumber: proposal.payload.header.slotNumber,
239
- coinbase: proposal.payload.header.coinbase,
240
- feeRecipient: proposal.payload.header.feeRecipient,
241
- gasFees: proposal.payload.header.gasFees,
242
- blockNumber,
243
- timestamp: header.timestamp,
244
- chainId: new Fr(config.l1ChainId),
245
- version: new Fr(config.rollupVersion)
246
- });
247
- const { block, failedTxs } = await this.blockBuilder.buildBlock(txs, l1ToL2Messages, globalVariables, {
248
- deadline: this.getReexecutionDeadline(proposal.payload.header.slotNumber.toBigInt(), config)
249
- });
250
- const numFailedTxs = failedTxs.length;
251
- const slot = proposal.slotNumber.toBigInt();
252
- this.log.verbose(`Transaction re-execution complete for slot ${slot}`, {
253
- numFailedTxs,
254
- numProposalTxs: txHashes.length,
255
- numProcessedTxs: block.body.txEffects.length,
256
- slot
257
- });
258
- if (numFailedTxs > 0) {
259
- this.metrics?.recordFailedReexecution(proposal);
260
- throw new ReExFailedTxsError(numFailedTxs);
261
- }
262
- if (block.body.txEffects.length !== txHashes.length) {
263
- this.metrics?.recordFailedReexecution(proposal);
264
- throw new ReExTimeoutError();
265
- }
266
- // Throw a ReExStateMismatchError error if state updates do not match
267
- const blockPayload = ConsensusPayload.fromBlock(block);
268
- if (!blockPayload.equals(proposal.payload)) {
269
- this.log.warn(`Re-execution state mismatch for slot ${slot}`, {
270
- expected: blockPayload.toInspect(),
271
- actual: proposal.payload.toInspect()
513
+ async reexecuteTransactions(proposal, blockNumber, checkpointNumber, txs, l1ToL2Messages, previousCheckpointOutHashes) {
514
+ const env = {
515
+ stack: [],
516
+ error: void 0,
517
+ hasError: false
518
+ };
519
+ try {
520
+ const { blockHeader, txHashes } = proposal;
521
+ // If we do not have all of the transactions, then we should fail
522
+ if (txs.length !== txHashes.length) {
523
+ const foundTxHashes = txs.map((tx)=>tx.getTxHash());
524
+ const missingTxHashes = txHashes.filter((txHash)=>!foundTxHashes.includes(txHash));
525
+ throw new TransactionsNotAvailableError(missingTxHashes);
526
+ }
527
+ const timer = new Timer();
528
+ const slot = proposal.slotNumber;
529
+ const config = this.checkpointsBuilder.getConfig();
530
+ // Get prior blocks in this checkpoint (same slot before current block)
531
+ const allBlocksInSlot = await this.blockSource.getBlocksForSlot(slot);
532
+ const priorBlocks = allBlocksInSlot.filter((b)=>b.number < blockNumber && b.header.getSlot() === slot);
533
+ // Fork before the block to be built
534
+ const parentBlockNumber = BlockNumber(blockNumber - 1);
535
+ await this.worldState.syncImmediate(parentBlockNumber);
536
+ const fork = _ts_add_disposable_resource(env, await this.worldState.fork(parentBlockNumber), true);
537
+ // Verify the fork's archive root matches the proposal's expected last archive.
538
+ // If they don't match, our world state synced to a different chain and reexecution would fail.
539
+ const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
540
+ if (!forkArchiveRoot.equals(proposal.blockHeader.lastArchive.root)) {
541
+ throw new ReExInitialStateMismatchError(proposal.blockHeader.lastArchive.root, forkArchiveRoot);
542
+ }
543
+ // Build checkpoint constants from proposal (excludes blockNumber which is per-block)
544
+ const constants = {
545
+ chainId: new Fr(config.l1ChainId),
546
+ version: new Fr(config.rollupVersion),
547
+ slotNumber: slot,
548
+ timestamp: blockHeader.globalVariables.timestamp,
549
+ coinbase: blockHeader.globalVariables.coinbase,
550
+ feeRecipient: blockHeader.globalVariables.feeRecipient,
551
+ gasFees: blockHeader.globalVariables.gasFees
552
+ };
553
+ // Create checkpoint builder with prior blocks
554
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, 0n, l1ToL2Messages, previousCheckpointOutHashes, fork, priorBlocks, this.log.getBindings());
555
+ // Build the new block
556
+ const deadline = this.getReexecutionDeadline(slot, config);
557
+ const maxBlockGas = this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined ? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity) : undefined;
558
+ const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
559
+ isBuildingProposal: false,
560
+ minValidTxs: 0,
561
+ deadline,
562
+ expectedEndState: blockHeader.state,
563
+ maxTransactions: this.config.validateMaxTxsPerBlock,
564
+ maxBlockGas
565
+ });
566
+ const { block, failedTxs } = result;
567
+ const numFailedTxs = failedTxs.length;
568
+ this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
569
+ numFailedTxs,
570
+ numProposalTxs: txHashes.length,
571
+ numProcessedTxs: block.body.txEffects.length,
572
+ blockNumber,
573
+ slot
272
574
  });
273
- this.metrics?.recordFailedReexecution(proposal);
274
- throw new ReExStateMismatchError(proposal.archive, block.archive.root, proposal.payload.stateReference, block.header.state);
575
+ if (numFailedTxs > 0) {
576
+ this.metrics?.recordFailedReexecution(proposal);
577
+ throw new ReExFailedTxsError(numFailedTxs);
578
+ }
579
+ if (block.body.txEffects.length !== txHashes.length) {
580
+ this.metrics?.recordFailedReexecution(proposal);
581
+ throw new ReExTimeoutError();
582
+ }
583
+ // Throw a ReExStateMismatchError error if state updates do not match
584
+ // Compare the full block structure (archive and header) from the built block with the proposal
585
+ const archiveMatches = proposal.archive.equals(block.archive.root);
586
+ const headerMatches = proposal.blockHeader.equals(block.header);
587
+ if (!archiveMatches || !headerMatches) {
588
+ this.log.warn(`Re-execution state mismatch for slot ${slot}`, {
589
+ expectedArchive: block.archive.root.toString(),
590
+ actualArchive: proposal.archive.toString(),
591
+ expectedHeader: block.header.toInspect(),
592
+ actualHeader: proposal.blockHeader.toInspect()
593
+ });
594
+ this.metrics?.recordFailedReexecution(proposal);
595
+ throw new ReExStateMismatchError(proposal.archive, block.archive.root);
596
+ }
597
+ const reexecutionTimeMs = timer.ms();
598
+ const totalManaUsed = block.header.totalManaUsed.toNumber() / 1e6;
599
+ this.metrics?.recordReex(reexecutionTimeMs, txs.length, totalManaUsed);
600
+ return {
601
+ block,
602
+ failedTxs,
603
+ reexecutionTimeMs,
604
+ totalManaUsed
605
+ };
606
+ } catch (e) {
607
+ env.error = e;
608
+ env.hasError = true;
609
+ } finally{
610
+ const result = _ts_dispose_resources(env);
611
+ if (result) await result;
275
612
  }
276
- const reexecutionTimeMs = timer.ms();
277
- const totalManaUsed = block.header.totalManaUsed.toNumber() / 1e6;
278
- this.metrics?.recordReex(reexecutionTimeMs, txs.length, totalManaUsed);
279
- return {
280
- block,
281
- failedTxs,
282
- reexecutionTimeMs,
283
- totalManaUsed
284
- };
285
613
  }
286
614
  }