@aztec/validator-client 0.0.1-fake-c83136db25 → 0.0.1-private.d0bedffd

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 +327 -0
  2. package/dest/block_proposal_handler.d.ts +26 -15
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +374 -109
  5. package/dest/checkpoint_builder.d.ts +76 -0
  6. package/dest/checkpoint_builder.d.ts.map +1 -0
  7. package/dest/checkpoint_builder.js +228 -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 +13 -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 +13 -10
  37. package/dest/metrics.d.ts +12 -3
  38. package/dest/metrics.d.ts.map +1 -1
  39. package/dest/metrics.js +52 -34
  40. package/dest/validator.d.ts +77 -22
  41. package/dest/validator.d.ts.map +1 -1
  42. package/dest/validator.js +537 -82
  43. package/package.json +24 -14
  44. package/src/block_proposal_handler.ts +298 -87
  45. package/src/checkpoint_builder.ts +390 -0
  46. package/src/config.ts +41 -6
  47. package/src/duties/validation_service.ts +158 -38
  48. package/src/factory.ts +18 -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 +22 -12
  56. package/src/metrics.ts +65 -33
  57. package/src/validator.ts +705 -115
@@ -1,67 +1,148 @@
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';
74
+ import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
75
+ import { Gas } from '@aztec/stdlib/gas';
76
+ import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
11
77
  import { ReExFailedTxsError, ReExStateMismatchError, ReExTimeoutError, TransactionsNotAvailableError } from '@aztec/stdlib/validators';
12
78
  import { getTelemetryClient } from '@aztec/telemetry-client';
13
79
  export class BlockProposalHandler {
14
- blockBuilder;
80
+ checkpointsBuilder;
81
+ worldState;
15
82
  blockSource;
16
83
  l1ToL2MessageSource;
17
84
  txProvider;
18
85
  blockProposalValidator;
86
+ epochCache;
19
87
  config;
20
88
  metrics;
21
89
  dateProvider;
22
90
  log;
23
91
  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;
92
+ constructor(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, metrics, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator:block-proposal-handler')){
93
+ this.checkpointsBuilder = checkpointsBuilder;
94
+ this.worldState = worldState;
26
95
  this.blockSource = blockSource;
27
96
  this.l1ToL2MessageSource = l1ToL2MessageSource;
28
97
  this.txProvider = txProvider;
29
98
  this.blockProposalValidator = blockProposalValidator;
99
+ this.epochCache = epochCache;
30
100
  this.config = config;
31
101
  this.metrics = metrics;
32
102
  this.dateProvider = dateProvider;
33
103
  this.log = log;
104
+ if (config.fishermanMode) {
105
+ this.log = this.log.createChild('[FISHERMAN]');
106
+ }
34
107
  this.tracer = telemetry.getTracer('BlockProposalHandler');
35
108
  }
36
- registerForReexecution(p2pClient) {
109
+ register(p2pClient, shouldReexecute) {
110
+ // Non-validator handler that processes or re-executes for monitoring but does not attest.
111
+ // Returns boolean indicating whether the proposal was valid.
37
112
  const handler = async (proposal, proposalSender)=>{
38
113
  try {
39
- const result = await this.handleBlockProposal(proposal, proposalSender, true);
114
+ const { slotNumber, blockNumber } = proposal;
115
+ const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
40
116
  if (result.isValid) {
41
- this.log.info(`Non-validator reexecution completed for slot ${proposal.slotNumber.toBigInt()}`, {
117
+ this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
42
118
  blockNumber: result.blockNumber,
119
+ slotNumber,
43
120
  reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
44
121
  totalManaUsed: result.reexecutionResult?.totalManaUsed,
45
- numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0
122
+ numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
123
+ reexecuted: shouldReexecute
46
124
  });
125
+ return true;
47
126
  } else {
48
- this.log.warn(`Non-validator reexecution failed for slot ${proposal.slotNumber.toBigInt()}`, {
127
+ this.log.warn(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} failed processing with ${result.reason}`, {
49
128
  blockNumber: result.blockNumber,
129
+ slotNumber,
50
130
  reason: result.reason
51
131
  });
132
+ return false;
52
133
  }
53
134
  } catch (error) {
54
135
  this.log.error('Error processing block proposal in non-validator handler', error);
136
+ return false;
55
137
  }
56
- return undefined; // Non-validator nodes don't return attestations
57
138
  };
58
139
  p2pClient.registerBlockProposalHandler(handler);
59
140
  return this;
60
141
  }
61
142
  async handleBlockProposal(proposal, proposalSender, shouldReexecute) {
62
- const slotNumber = proposal.slotNumber.toBigInt();
143
+ const slotNumber = proposal.slotNumber;
63
144
  const proposer = proposal.getSender();
64
- const config = this.blockBuilder.getConfig();
145
+ const config = this.checkpointsBuilder.getConfig();
65
146
  // Reject proposals with invalid signatures
66
147
  if (!proposer) {
67
148
  this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
@@ -80,8 +161,8 @@ export class BlockProposalHandler {
80
161
  });
81
162
  // Check that the proposal is from the current proposer, or the next proposer
82
163
  // 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) {
164
+ const validationResult = await this.blockProposalValidator.validate(proposal);
165
+ if (validationResult.result !== 'accept') {
85
166
  this.log.warn(`Proposal is not valid, skipping processing`, proposalInfo);
86
167
  return {
87
168
  isValid: false,
@@ -89,18 +170,18 @@ export class BlockProposalHandler {
89
170
  };
90
171
  }
91
172
  // 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) {
173
+ const parentBlock = await this.getParentBlock(proposal);
174
+ if (parentBlock === undefined) {
94
175
  this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
95
176
  return {
96
177
  isValid: false,
97
178
  reason: 'parent_block_not_found'
98
179
  };
99
180
  }
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(),
181
+ // Check that the parent block's slot is not greater than the proposal's slot.
182
+ if (parentBlock !== 'genesis' && parentBlock.header.getSlot() > slotNumber) {
183
+ this.log.warn(`Parent block slot is greater than proposal slot, skipping processing`, {
184
+ parentBlockSlot: parentBlock.header.getSlot().toString(),
104
185
  proposalSlot: slotNumber.toString(),
105
186
  ...proposalInfo
106
187
  });
@@ -110,7 +191,7 @@ export class BlockProposalHandler {
110
191
  };
111
192
  }
112
193
  // Compute the block number based on the parent block
113
- const blockNumber = parentBlockHeader === 'genesis' ? INITIAL_L2_BLOCK_NUM : parentBlockHeader.getBlockNumber() + 1;
194
+ const blockNumber = parentBlock === 'genesis' ? BlockNumber(INITIAL_L2_BLOCK_NUM) : BlockNumber(parentBlock.header.getBlockNumber() + 1);
114
195
  // Check that this block number does not exist already
115
196
  const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
116
197
  if (existingBlock) {
@@ -127,10 +208,28 @@ export class BlockProposalHandler {
127
208
  pinnedPeer: proposalSender,
128
209
  deadline: this.getReexecutionDeadline(slotNumber, config)
129
210
  });
211
+ // If reexecution is disabled, bail. We are just interested in triggering tx collection.
212
+ if (!shouldReexecute) {
213
+ this.log.info(`Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`, proposalInfo);
214
+ return {
215
+ isValid: true,
216
+ blockNumber
217
+ };
218
+ }
219
+ // Compute the checkpoint number for this block and validate checkpoint consistency
220
+ const checkpointResult = this.computeCheckpointNumber(proposal, parentBlock, proposalInfo);
221
+ if (checkpointResult.reason) {
222
+ return {
223
+ isValid: false,
224
+ blockNumber,
225
+ reason: checkpointResult.reason
226
+ };
227
+ }
228
+ const checkpointNumber = checkpointResult.checkpointNumber;
130
229
  // 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;
230
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
231
+ const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
232
+ const proposalInHash = proposal.inHash;
134
233
  if (!computedInHash.equals(proposalInHash)) {
135
234
  this.log.warn(`L1 to L2 messages in hash mismatch, skipping processing`, {
136
235
  proposalInHash: proposalInHash.toString(),
@@ -155,24 +254,32 @@ export class BlockProposalHandler {
155
254
  reason: 'txs_not_available'
156
255
  };
157
256
  }
257
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
258
+ const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
259
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch)).filter((c)=>c.checkpointNumber < checkpointNumber).map((c)=>c.checkpointOutHash);
158
260
  // Try re-executing the transactions in the proposal if needed
159
261
  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
- }
262
+ try {
263
+ this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
264
+ reexecutionResult = await this.reexecuteTransactions(proposal, blockNumber, checkpointNumber, txs, l1ToL2Messages, previousCheckpointOutHashes);
265
+ } catch (error) {
266
+ this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
267
+ const reason = this.getReexecuteFailureReason(error);
268
+ return {
269
+ isValid: false,
270
+ blockNumber,
271
+ reason,
272
+ reexecutionResult
273
+ };
174
274
  }
175
- this.log.info(`Successfully processed proposal for slot ${slotNumber}`, proposalInfo);
275
+ // If we succeeded, push this block into the archiver (unless disabled)
276
+ if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
277
+ await this.blockSource.addBlock(reexecutionResult?.block);
278
+ }
279
+ this.log.info(`Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`, {
280
+ ...proposalInfo,
281
+ ...pick(reexecutionResult, 'reexecutionTimeMs', 'totalManaUsed')
282
+ });
176
283
  return {
177
284
  isValid: true,
178
285
  blockNumber,
@@ -180,9 +287,9 @@ export class BlockProposalHandler {
180
287
  };
181
288
  }
182
289
  async getParentBlock(proposal) {
183
- const parentArchive = proposal.payload.header.lastArchiveRoot;
184
- const slot = proposal.slotNumber.toBigInt();
185
- const config = this.blockBuilder.getConfig();
290
+ const parentArchive = proposal.blockHeader.lastArchive.root;
291
+ const slot = proposal.slotNumber;
292
+ const config = this.checkpointsBuilder.getConfig();
186
293
  const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
187
294
  if (parentArchive.equals(genesisArchiveRoot)) {
188
295
  return 'genesis';
@@ -191,7 +298,7 @@ export class BlockProposalHandler {
191
298
  const currentTime = this.dateProvider.now();
192
299
  const timeoutDurationMs = deadline.getTime() - currentTime;
193
300
  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));
301
+ 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
302
  } catch (err) {
196
303
  if (err instanceof TimeoutError) {
197
304
  this.log.debug(`Timed out getting parent block by archive root`, {
@@ -205,10 +312,137 @@ export class BlockProposalHandler {
205
312
  return undefined;
206
313
  }
207
314
  }
315
+ computeCheckpointNumber(proposal, parentBlock, proposalInfo) {
316
+ if (parentBlock === 'genesis') {
317
+ // First block is in checkpoint 1
318
+ if (proposal.indexWithinCheckpoint !== 0) {
319
+ this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
320
+ return {
321
+ reason: 'invalid_proposal'
322
+ };
323
+ }
324
+ return {
325
+ checkpointNumber: CheckpointNumber.INITIAL
326
+ };
327
+ }
328
+ if (proposal.indexWithinCheckpoint === 0) {
329
+ // If this is the first block in a new checkpoint, increment the checkpoint number
330
+ if (!(proposal.blockHeader.getSlot() > parentBlock.header.getSlot())) {
331
+ this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
332
+ return {
333
+ reason: 'invalid_proposal'
334
+ };
335
+ }
336
+ return {
337
+ checkpointNumber: CheckpointNumber(parentBlock.checkpointNumber + 1)
338
+ };
339
+ }
340
+ // Otherwise it should follow the previous block in the same checkpoint
341
+ if (proposal.indexWithinCheckpoint !== parentBlock.indexWithinCheckpoint + 1) {
342
+ this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
343
+ return {
344
+ reason: 'invalid_proposal'
345
+ };
346
+ }
347
+ if (proposal.blockHeader.getSlot() !== parentBlock.header.getSlot()) {
348
+ this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
349
+ return {
350
+ reason: 'invalid_proposal'
351
+ };
352
+ }
353
+ // For non-first blocks in a checkpoint, validate global variables match parent (except blockNumber)
354
+ const validationResult = this.validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo);
355
+ if (validationResult) {
356
+ return validationResult;
357
+ }
358
+ return {
359
+ checkpointNumber: parentBlock.checkpointNumber
360
+ };
361
+ }
362
+ /**
363
+ * Validates that a non-first block in a checkpoint has consistent global variables with its parent.
364
+ * For blocks with indexWithinCheckpoint > 0, all global variables except blockNumber must match the parent.
365
+ * @returns A failure result if validation fails, undefined if validation passes
366
+ */ validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo) {
367
+ const proposalGlobals = proposal.blockHeader.globalVariables;
368
+ const parentGlobals = parentBlock.header.globalVariables;
369
+ // All global variables except blockNumber should match the parent
370
+ // blockNumber naturally increments between blocks
371
+ if (!proposalGlobals.chainId.equals(parentGlobals.chainId)) {
372
+ this.log.warn(`Non-first block in checkpoint has mismatched chainId`, {
373
+ ...proposalInfo,
374
+ proposalChainId: proposalGlobals.chainId.toString(),
375
+ parentChainId: parentGlobals.chainId.toString()
376
+ });
377
+ return {
378
+ reason: 'global_variables_mismatch'
379
+ };
380
+ }
381
+ if (!proposalGlobals.version.equals(parentGlobals.version)) {
382
+ this.log.warn(`Non-first block in checkpoint has mismatched version`, {
383
+ ...proposalInfo,
384
+ proposalVersion: proposalGlobals.version.toString(),
385
+ parentVersion: parentGlobals.version.toString()
386
+ });
387
+ return {
388
+ reason: 'global_variables_mismatch'
389
+ };
390
+ }
391
+ if (proposalGlobals.slotNumber !== parentGlobals.slotNumber) {
392
+ this.log.warn(`Non-first block in checkpoint has mismatched slotNumber`, {
393
+ ...proposalInfo,
394
+ proposalSlotNumber: proposalGlobals.slotNumber,
395
+ parentSlotNumber: parentGlobals.slotNumber
396
+ });
397
+ return {
398
+ reason: 'global_variables_mismatch'
399
+ };
400
+ }
401
+ if (proposalGlobals.timestamp !== parentGlobals.timestamp) {
402
+ this.log.warn(`Non-first block in checkpoint has mismatched timestamp`, {
403
+ ...proposalInfo,
404
+ proposalTimestamp: proposalGlobals.timestamp.toString(),
405
+ parentTimestamp: parentGlobals.timestamp.toString()
406
+ });
407
+ return {
408
+ reason: 'global_variables_mismatch'
409
+ };
410
+ }
411
+ if (!proposalGlobals.coinbase.equals(parentGlobals.coinbase)) {
412
+ this.log.warn(`Non-first block in checkpoint has mismatched coinbase`, {
413
+ ...proposalInfo,
414
+ proposalCoinbase: proposalGlobals.coinbase.toString(),
415
+ parentCoinbase: parentGlobals.coinbase.toString()
416
+ });
417
+ return {
418
+ reason: 'global_variables_mismatch'
419
+ };
420
+ }
421
+ if (!proposalGlobals.feeRecipient.equals(parentGlobals.feeRecipient)) {
422
+ this.log.warn(`Non-first block in checkpoint has mismatched feeRecipient`, {
423
+ ...proposalInfo,
424
+ proposalFeeRecipient: proposalGlobals.feeRecipient.toString(),
425
+ parentFeeRecipient: parentGlobals.feeRecipient.toString()
426
+ });
427
+ return {
428
+ reason: 'global_variables_mismatch'
429
+ };
430
+ }
431
+ if (!proposalGlobals.gasFees.equals(parentGlobals.gasFees)) {
432
+ this.log.warn(`Non-first block in checkpoint has mismatched gasFees`, {
433
+ ...proposalInfo,
434
+ proposalGasFees: proposalGlobals.gasFees.toInspect(),
435
+ parentGasFees: parentGlobals.gasFees.toInspect()
436
+ });
437
+ return {
438
+ reason: 'global_variables_mismatch'
439
+ };
440
+ }
441
+ return undefined;
442
+ }
208
443
  getReexecutionDeadline(slot, config) {
209
- const nextSlotTimestampSeconds = Number(getTimestampForSlot(slot + 1n, config));
210
- const msNeededForPropagationAndPublishing = this.config.validatorReexecuteDeadlineMs;
211
- return new Date(nextSlotTimestampSeconds * 1000 - msNeededForPropagationAndPublishing);
444
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
445
+ return new Date(nextSlotTimestampSeconds * 1000);
212
446
  }
213
447
  getReexecuteFailureReason(err) {
214
448
  if (err instanceof ReExStateMismatchError) {
@@ -221,66 +455,97 @@ export class BlockProposalHandler {
221
455
  return 'unknown_error';
222
456
  }
223
457
  }
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()
458
+ async reexecuteTransactions(proposal, blockNumber, checkpointNumber, txs, l1ToL2Messages, previousCheckpointOutHashes) {
459
+ const env = {
460
+ stack: [],
461
+ error: void 0,
462
+ hasError: false
463
+ };
464
+ try {
465
+ const { blockHeader, txHashes } = proposal;
466
+ // If we do not have all of the transactions, then we should fail
467
+ if (txs.length !== txHashes.length) {
468
+ const foundTxHashes = txs.map((tx)=>tx.getTxHash());
469
+ const missingTxHashes = txHashes.filter((txHash)=>!foundTxHashes.includes(txHash));
470
+ throw new TransactionsNotAvailableError(missingTxHashes);
471
+ }
472
+ const timer = new Timer();
473
+ const slot = proposal.slotNumber;
474
+ const config = this.checkpointsBuilder.getConfig();
475
+ // Get prior blocks in this checkpoint (same slot before current block)
476
+ const allBlocksInSlot = await this.blockSource.getBlocksForSlot(slot);
477
+ const priorBlocks = allBlocksInSlot.filter((b)=>b.number < blockNumber && b.header.getSlot() === slot);
478
+ // Fork before the block to be built
479
+ const parentBlockNumber = BlockNumber(blockNumber - 1);
480
+ await this.worldState.syncImmediate(parentBlockNumber);
481
+ const fork = _ts_add_disposable_resource(env, await this.worldState.fork(parentBlockNumber), true);
482
+ // Build checkpoint constants from proposal (excludes blockNumber which is per-block)
483
+ const constants = {
484
+ chainId: new Fr(config.l1ChainId),
485
+ version: new Fr(config.rollupVersion),
486
+ slotNumber: slot,
487
+ timestamp: blockHeader.globalVariables.timestamp,
488
+ coinbase: blockHeader.globalVariables.coinbase,
489
+ feeRecipient: blockHeader.globalVariables.feeRecipient,
490
+ gasFees: blockHeader.globalVariables.gasFees
491
+ };
492
+ // Create checkpoint builder with prior blocks
493
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, 0n, l1ToL2Messages, previousCheckpointOutHashes, fork, priorBlocks, this.log.getBindings());
494
+ // Build the new block
495
+ const deadline = this.getReexecutionDeadline(slot, config);
496
+ const maxBlockGas = this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined ? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity) : undefined;
497
+ const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
498
+ deadline,
499
+ expectedEndState: blockHeader.state,
500
+ maxTransactions: this.config.validateMaxTxsPerBlock,
501
+ maxBlockGas
272
502
  });
273
- this.metrics?.recordFailedReexecution(proposal);
274
- throw new ReExStateMismatchError(proposal.archive, block.archive.root, proposal.payload.stateReference, block.header.state);
503
+ const { block, failedTxs } = result;
504
+ const numFailedTxs = failedTxs.length;
505
+ this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
506
+ numFailedTxs,
507
+ numProposalTxs: txHashes.length,
508
+ numProcessedTxs: block.body.txEffects.length,
509
+ blockNumber,
510
+ slot
511
+ });
512
+ if (numFailedTxs > 0) {
513
+ this.metrics?.recordFailedReexecution(proposal);
514
+ throw new ReExFailedTxsError(numFailedTxs);
515
+ }
516
+ if (block.body.txEffects.length !== txHashes.length) {
517
+ this.metrics?.recordFailedReexecution(proposal);
518
+ throw new ReExTimeoutError();
519
+ }
520
+ // Throw a ReExStateMismatchError error if state updates do not match
521
+ // Compare the full block structure (archive and header) from the built block with the proposal
522
+ const archiveMatches = proposal.archive.equals(block.archive.root);
523
+ const headerMatches = proposal.blockHeader.equals(block.header);
524
+ if (!archiveMatches || !headerMatches) {
525
+ this.log.warn(`Re-execution state mismatch for slot ${slot}`, {
526
+ expectedArchive: block.archive.root.toString(),
527
+ actualArchive: proposal.archive.toString(),
528
+ expectedHeader: block.header.toInspect(),
529
+ actualHeader: proposal.blockHeader.toInspect()
530
+ });
531
+ this.metrics?.recordFailedReexecution(proposal);
532
+ throw new ReExStateMismatchError(proposal.archive, block.archive.root);
533
+ }
534
+ const reexecutionTimeMs = timer.ms();
535
+ const totalManaUsed = block.header.totalManaUsed.toNumber() / 1e6;
536
+ this.metrics?.recordReex(reexecutionTimeMs, txs.length, totalManaUsed);
537
+ return {
538
+ block,
539
+ failedTxs,
540
+ reexecutionTimeMs,
541
+ totalManaUsed
542
+ };
543
+ } catch (e) {
544
+ env.error = e;
545
+ env.hasError = true;
546
+ } finally{
547
+ const result = _ts_dispose_resources(env);
548
+ if (result) await result;
275
549
  }
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
550
  }
286
551
  }