@aztec/validator-client 0.0.0-test.1 → 0.0.1-commit.1142ef1

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 (74) hide show
  1. package/README.md +256 -0
  2. package/dest/block_proposal_handler.d.ts +63 -0
  3. package/dest/block_proposal_handler.d.ts.map +1 -0
  4. package/dest/block_proposal_handler.js +551 -0
  5. package/dest/checkpoint_builder.d.ts +70 -0
  6. package/dest/checkpoint_builder.d.ts.map +1 -0
  7. package/dest/checkpoint_builder.js +155 -0
  8. package/dest/config.d.ts +3 -14
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +46 -7
  11. package/dest/duties/validation_service.d.ts +36 -12
  12. package/dest/duties/validation_service.d.ts.map +1 -1
  13. package/dest/duties/validation_service.js +69 -16
  14. package/dest/factory.d.ts +27 -5
  15. package/dest/factory.d.ts.map +1 -1
  16. package/dest/factory.js +13 -6
  17. package/dest/index.d.ts +6 -2
  18. package/dest/index.d.ts.map +1 -1
  19. package/dest/index.js +5 -1
  20. package/dest/key_store/index.d.ts +3 -1
  21. package/dest/key_store/index.d.ts.map +1 -1
  22. package/dest/key_store/index.js +2 -0
  23. package/dest/key_store/interface.d.ts +55 -6
  24. package/dest/key_store/interface.d.ts.map +1 -1
  25. package/dest/key_store/interface.js +3 -3
  26. package/dest/key_store/local_key_store.d.ts +41 -11
  27. package/dest/key_store/local_key_store.d.ts.map +1 -1
  28. package/dest/key_store/local_key_store.js +64 -17
  29. package/dest/key_store/node_keystore_adapter.d.ts +138 -0
  30. package/dest/key_store/node_keystore_adapter.d.ts.map +1 -0
  31. package/dest/key_store/node_keystore_adapter.js +316 -0
  32. package/dest/key_store/web3signer_key_store.d.ts +61 -0
  33. package/dest/key_store/web3signer_key_store.d.ts.map +1 -0
  34. package/dest/key_store/web3signer_key_store.js +152 -0
  35. package/dest/metrics.d.ts +12 -5
  36. package/dest/metrics.d.ts.map +1 -1
  37. package/dest/metrics.js +36 -24
  38. package/dest/tx_validator/index.d.ts +3 -0
  39. package/dest/tx_validator/index.d.ts.map +1 -0
  40. package/dest/tx_validator/index.js +2 -0
  41. package/dest/tx_validator/nullifier_cache.d.ts +14 -0
  42. package/dest/tx_validator/nullifier_cache.d.ts.map +1 -0
  43. package/dest/tx_validator/nullifier_cache.js +24 -0
  44. package/dest/tx_validator/tx_validator_factory.d.ts +18 -0
  45. package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -0
  46. package/dest/tx_validator/tx_validator_factory.js +53 -0
  47. package/dest/validator.d.ts +73 -58
  48. package/dest/validator.d.ts.map +1 -1
  49. package/dest/validator.js +557 -166
  50. package/package.json +33 -21
  51. package/src/block_proposal_handler.ts +556 -0
  52. package/src/checkpoint_builder.ts +267 -0
  53. package/src/config.ts +58 -22
  54. package/src/duties/validation_service.ts +124 -18
  55. package/src/factory.ts +64 -11
  56. package/src/index.ts +5 -1
  57. package/src/key_store/index.ts +2 -0
  58. package/src/key_store/interface.ts +61 -5
  59. package/src/key_store/local_key_store.ts +68 -18
  60. package/src/key_store/node_keystore_adapter.ts +375 -0
  61. package/src/key_store/web3signer_key_store.ts +192 -0
  62. package/src/metrics.ts +48 -24
  63. package/src/tx_validator/index.ts +2 -0
  64. package/src/tx_validator/nullifier_cache.ts +30 -0
  65. package/src/tx_validator/tx_validator_factory.ts +133 -0
  66. package/src/validator.ts +749 -218
  67. package/dest/errors/index.d.ts +0 -2
  68. package/dest/errors/index.d.ts.map +0 -1
  69. package/dest/errors/index.js +0 -1
  70. package/dest/errors/validator.error.d.ts +0 -29
  71. package/dest/errors/validator.error.d.ts.map +0 -1
  72. package/dest/errors/validator.error.js +0 -45
  73. package/src/errors/index.ts +0 -1
  74. package/src/errors/validator.error.ts +0 -55
@@ -0,0 +1,551 @@
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
+ }
66
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
67
+ import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
68
+ import { Fr } from '@aztec/foundation/curves/bn254';
69
+ import { TimeoutError } from '@aztec/foundation/error';
70
+ import { createLogger } from '@aztec/foundation/log';
71
+ import { retryUntil } from '@aztec/foundation/retry';
72
+ import { DateProvider, Timer } from '@aztec/foundation/timer';
73
+ import { getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
74
+ import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
75
+ import { ReExFailedTxsError, ReExStateMismatchError, ReExTimeoutError, TransactionsNotAvailableError } from '@aztec/stdlib/validators';
76
+ import { getTelemetryClient } from '@aztec/telemetry-client';
77
+ export class BlockProposalHandler {
78
+ checkpointsBuilder;
79
+ worldState;
80
+ blockSource;
81
+ l1ToL2MessageSource;
82
+ txProvider;
83
+ blockProposalValidator;
84
+ config;
85
+ metrics;
86
+ dateProvider;
87
+ log;
88
+ tracer;
89
+ constructor(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, config, metrics, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator:block-proposal-handler')){
90
+ this.checkpointsBuilder = checkpointsBuilder;
91
+ this.worldState = worldState;
92
+ this.blockSource = blockSource;
93
+ this.l1ToL2MessageSource = l1ToL2MessageSource;
94
+ this.txProvider = txProvider;
95
+ this.blockProposalValidator = blockProposalValidator;
96
+ this.config = config;
97
+ this.metrics = metrics;
98
+ this.dateProvider = dateProvider;
99
+ this.log = log;
100
+ if (config.fishermanMode) {
101
+ this.log = this.log.createChild('[FISHERMAN]');
102
+ }
103
+ this.tracer = telemetry.getTracer('BlockProposalHandler');
104
+ }
105
+ registerForReexecution(p2pClient) {
106
+ // Non-validator handler that re-executes for monitoring but does not attest.
107
+ // Returns boolean indicating whether the proposal was valid.
108
+ const handler = async (proposal, proposalSender)=>{
109
+ try {
110
+ const result = await this.handleBlockProposal(proposal, proposalSender, true);
111
+ if (result.isValid) {
112
+ this.log.info(`Non-validator reexecution completed for slot ${proposal.slotNumber}`, {
113
+ blockNumber: result.blockNumber,
114
+ reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
115
+ totalManaUsed: result.reexecutionResult?.totalManaUsed,
116
+ numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0
117
+ });
118
+ return true;
119
+ } else {
120
+ this.log.warn(`Non-validator reexecution failed for slot ${proposal.slotNumber}`, {
121
+ blockNumber: result.blockNumber,
122
+ reason: result.reason
123
+ });
124
+ return false;
125
+ }
126
+ } catch (error) {
127
+ this.log.error('Error processing block proposal in non-validator handler', error);
128
+ return false;
129
+ }
130
+ };
131
+ p2pClient.registerBlockProposalHandler(handler);
132
+ return this;
133
+ }
134
+ async handleBlockProposal(proposal, proposalSender, shouldReexecute) {
135
+ const slotNumber = proposal.slotNumber;
136
+ const proposer = proposal.getSender();
137
+ const config = this.checkpointsBuilder.getConfig();
138
+ // Reject proposals with invalid signatures
139
+ if (!proposer) {
140
+ this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
141
+ return {
142
+ isValid: false,
143
+ reason: 'invalid_proposal'
144
+ };
145
+ }
146
+ const proposalInfo = {
147
+ ...proposal.toBlockInfo(),
148
+ proposer: proposer.toString()
149
+ };
150
+ this.log.info(`Processing proposal for slot ${slotNumber}`, {
151
+ ...proposalInfo,
152
+ txHashes: proposal.txHashes.map((t)=>t.toString())
153
+ });
154
+ // Check that the proposal is from the current proposer, or the next proposer
155
+ // This should have been handled by the p2p layer, but we double check here out of caution
156
+ const invalidProposal = await this.blockProposalValidator.validate(proposal);
157
+ if (invalidProposal) {
158
+ this.log.warn(`Proposal is not valid, skipping processing`, proposalInfo);
159
+ return {
160
+ isValid: false,
161
+ reason: 'invalid_proposal'
162
+ };
163
+ }
164
+ // Check that the parent proposal is a block we know, otherwise reexecution would fail
165
+ const parentBlockHeader = await this.getParentBlock(proposal);
166
+ if (parentBlockHeader === undefined) {
167
+ this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
168
+ return {
169
+ isValid: false,
170
+ reason: 'parent_block_not_found'
171
+ };
172
+ }
173
+ // Check that the parent block's slot is less than the proposal's slot (should not happen, but we check anyway)
174
+ if (parentBlockHeader !== 'genesis' && parentBlockHeader.getSlot() >= slotNumber) {
175
+ this.log.warn(`Parent block slot is greater than or equal to proposal slot, skipping processing`, {
176
+ parentBlockSlot: parentBlockHeader.getSlot().toString(),
177
+ proposalSlot: slotNumber.toString(),
178
+ ...proposalInfo
179
+ });
180
+ return {
181
+ isValid: false,
182
+ reason: 'parent_block_wrong_slot'
183
+ };
184
+ }
185
+ // Compute the block number based on the parent block
186
+ const blockNumber = parentBlockHeader === 'genesis' ? BlockNumber(INITIAL_L2_BLOCK_NUM) : BlockNumber(parentBlockHeader.getBlockNumber() + 1);
187
+ // Check that this block number does not exist already
188
+ const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
189
+ if (existingBlock) {
190
+ this.log.warn(`Block number ${blockNumber} already exists, skipping processing`, proposalInfo);
191
+ return {
192
+ isValid: false,
193
+ blockNumber,
194
+ reason: 'block_number_already_exists'
195
+ };
196
+ }
197
+ // Collect txs from the proposal. We start doing this as early as possible,
198
+ // and we do it even if we don't plan to re-execute the txs, so that we have them if another node needs them.
199
+ const { txs, missingTxs } = await this.txProvider.getTxsForBlockProposal(proposal, blockNumber, {
200
+ pinnedPeer: proposalSender,
201
+ deadline: this.getReexecutionDeadline(slotNumber, config)
202
+ });
203
+ // Compute the checkpoint number for this block and validate checkpoint consistency
204
+ const checkpointResult = await this.computeCheckpointNumber(proposal, parentBlockHeader, proposalInfo);
205
+ if (checkpointResult.reason) {
206
+ return {
207
+ isValid: false,
208
+ blockNumber,
209
+ reason: checkpointResult.reason
210
+ };
211
+ }
212
+ const checkpointNumber = checkpointResult.checkpointNumber;
213
+ // Check that I have the same set of l1ToL2Messages as the proposal
214
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
215
+ const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
216
+ const proposalInHash = proposal.inHash;
217
+ if (!computedInHash.equals(proposalInHash)) {
218
+ this.log.warn(`L1 to L2 messages in hash mismatch, skipping processing`, {
219
+ proposalInHash: proposalInHash.toString(),
220
+ computedInHash: computedInHash.toString(),
221
+ ...proposalInfo
222
+ });
223
+ return {
224
+ isValid: false,
225
+ blockNumber,
226
+ reason: 'in_hash_mismatch'
227
+ };
228
+ }
229
+ // Check that all of the transactions in the proposal are available
230
+ if (missingTxs.length > 0) {
231
+ this.log.warn(`Missing ${missingTxs.length} txs to process proposal`, {
232
+ ...proposalInfo,
233
+ missingTxs
234
+ });
235
+ return {
236
+ isValid: false,
237
+ blockNumber,
238
+ reason: 'txs_not_available'
239
+ };
240
+ }
241
+ // Try re-executing the transactions in the proposal if needed
242
+ let reexecutionResult;
243
+ if (shouldReexecute) {
244
+ try {
245
+ this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
246
+ reexecutionResult = await this.reexecuteTransactions(proposal, blockNumber, checkpointNumber, txs, l1ToL2Messages);
247
+ } catch (error) {
248
+ this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
249
+ const reason = this.getReexecuteFailureReason(error);
250
+ return {
251
+ isValid: false,
252
+ blockNumber,
253
+ reason,
254
+ reexecutionResult
255
+ };
256
+ }
257
+ }
258
+ // If we succeeded, push this block into the archiver (unless disabled)
259
+ // TODO(palla/mbps): Change default to false once block sync is stable.
260
+ if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
261
+ await this.blockSource.addBlock(reexecutionResult?.block);
262
+ }
263
+ this.log.info(`Successfully processed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`, proposalInfo);
264
+ return {
265
+ isValid: true,
266
+ blockNumber,
267
+ reexecutionResult
268
+ };
269
+ }
270
+ async getParentBlock(proposal) {
271
+ const parentArchive = proposal.blockHeader.lastArchive.root;
272
+ const slot = proposal.slotNumber;
273
+ const config = this.checkpointsBuilder.getConfig();
274
+ const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
275
+ if (parentArchive.equals(genesisArchiveRoot)) {
276
+ return 'genesis';
277
+ }
278
+ const deadline = this.getReexecutionDeadline(slot, config);
279
+ const currentTime = this.dateProvider.now();
280
+ const timeoutDurationMs = deadline.getTime() - currentTime;
281
+ try {
282
+ 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));
283
+ } catch (err) {
284
+ if (err instanceof TimeoutError) {
285
+ this.log.debug(`Timed out getting parent block by archive root`, {
286
+ parentArchive
287
+ });
288
+ } else {
289
+ this.log.error('Error getting parent block by archive root', err, {
290
+ parentArchive
291
+ });
292
+ }
293
+ return undefined;
294
+ }
295
+ }
296
+ async computeCheckpointNumber(proposal, parentBlockHeader, proposalInfo) {
297
+ if (parentBlockHeader === 'genesis') {
298
+ // First block is in checkpoint 1
299
+ if (proposal.indexWithinCheckpoint !== 0) {
300
+ this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
301
+ return {
302
+ reason: 'invalid_proposal'
303
+ };
304
+ }
305
+ return {
306
+ checkpointNumber: CheckpointNumber.INITIAL
307
+ };
308
+ }
309
+ // Get the parent block to find its checkpoint number
310
+ // TODO(palla/mbps): The block header should include the checkpoint number to avoid this lookup,
311
+ // or at least the L2BlockSource should return a different struct that includes it.
312
+ const parentBlockNumber = parentBlockHeader.getBlockNumber();
313
+ const parentBlock = await this.blockSource.getL2BlockNew(parentBlockNumber);
314
+ if (!parentBlock) {
315
+ this.log.warn(`Parent block ${parentBlockNumber} not found in archiver`, proposalInfo);
316
+ return {
317
+ reason: 'invalid_proposal'
318
+ };
319
+ }
320
+ if (proposal.indexWithinCheckpoint === 0) {
321
+ // If this is the first block in a new checkpoint, increment the checkpoint number
322
+ if (!(proposal.blockHeader.getSlot() > parentBlockHeader.getSlot())) {
323
+ this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
324
+ return {
325
+ reason: 'invalid_proposal'
326
+ };
327
+ }
328
+ return {
329
+ checkpointNumber: CheckpointNumber(parentBlock.checkpointNumber + 1)
330
+ };
331
+ }
332
+ // Otherwise it should follow the previous block in the same checkpoint
333
+ if (proposal.indexWithinCheckpoint !== parentBlock.indexWithinCheckpoint + 1) {
334
+ this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
335
+ return {
336
+ reason: 'invalid_proposal'
337
+ };
338
+ }
339
+ if (proposal.blockHeader.getSlot() !== parentBlockHeader.getSlot()) {
340
+ this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
341
+ return {
342
+ reason: 'invalid_proposal'
343
+ };
344
+ }
345
+ // For non-first blocks in a checkpoint, validate global variables match parent (except blockNumber)
346
+ const validationResult = this.validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo);
347
+ if (validationResult) {
348
+ return validationResult;
349
+ }
350
+ return {
351
+ checkpointNumber: parentBlock.checkpointNumber
352
+ };
353
+ }
354
+ /**
355
+ * Validates that a non-first block in a checkpoint has consistent global variables with its parent.
356
+ * For blocks with indexWithinCheckpoint > 0, all global variables except blockNumber must match the parent.
357
+ * @returns A failure result if validation fails, undefined if validation passes
358
+ */ validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo) {
359
+ const proposalGlobals = proposal.blockHeader.globalVariables;
360
+ const parentGlobals = parentBlock.header.globalVariables;
361
+ // All global variables except blockNumber should match the parent
362
+ // blockNumber naturally increments between blocks
363
+ if (!proposalGlobals.chainId.equals(parentGlobals.chainId)) {
364
+ this.log.warn(`Non-first block in checkpoint has mismatched chainId`, {
365
+ ...proposalInfo,
366
+ proposalChainId: proposalGlobals.chainId.toString(),
367
+ parentChainId: parentGlobals.chainId.toString()
368
+ });
369
+ return {
370
+ reason: 'global_variables_mismatch'
371
+ };
372
+ }
373
+ if (!proposalGlobals.version.equals(parentGlobals.version)) {
374
+ this.log.warn(`Non-first block in checkpoint has mismatched version`, {
375
+ ...proposalInfo,
376
+ proposalVersion: proposalGlobals.version.toString(),
377
+ parentVersion: parentGlobals.version.toString()
378
+ });
379
+ return {
380
+ reason: 'global_variables_mismatch'
381
+ };
382
+ }
383
+ if (proposalGlobals.slotNumber !== parentGlobals.slotNumber) {
384
+ this.log.warn(`Non-first block in checkpoint has mismatched slotNumber`, {
385
+ ...proposalInfo,
386
+ proposalSlotNumber: proposalGlobals.slotNumber,
387
+ parentSlotNumber: parentGlobals.slotNumber
388
+ });
389
+ return {
390
+ reason: 'global_variables_mismatch'
391
+ };
392
+ }
393
+ if (proposalGlobals.timestamp !== parentGlobals.timestamp) {
394
+ this.log.warn(`Non-first block in checkpoint has mismatched timestamp`, {
395
+ ...proposalInfo,
396
+ proposalTimestamp: proposalGlobals.timestamp.toString(),
397
+ parentTimestamp: parentGlobals.timestamp.toString()
398
+ });
399
+ return {
400
+ reason: 'global_variables_mismatch'
401
+ };
402
+ }
403
+ if (!proposalGlobals.coinbase.equals(parentGlobals.coinbase)) {
404
+ this.log.warn(`Non-first block in checkpoint has mismatched coinbase`, {
405
+ ...proposalInfo,
406
+ proposalCoinbase: proposalGlobals.coinbase.toString(),
407
+ parentCoinbase: parentGlobals.coinbase.toString()
408
+ });
409
+ return {
410
+ reason: 'global_variables_mismatch'
411
+ };
412
+ }
413
+ if (!proposalGlobals.feeRecipient.equals(parentGlobals.feeRecipient)) {
414
+ this.log.warn(`Non-first block in checkpoint has mismatched feeRecipient`, {
415
+ ...proposalInfo,
416
+ proposalFeeRecipient: proposalGlobals.feeRecipient.toString(),
417
+ parentFeeRecipient: parentGlobals.feeRecipient.toString()
418
+ });
419
+ return {
420
+ reason: 'global_variables_mismatch'
421
+ };
422
+ }
423
+ if (!proposalGlobals.gasFees.equals(parentGlobals.gasFees)) {
424
+ this.log.warn(`Non-first block in checkpoint has mismatched gasFees`, {
425
+ ...proposalInfo,
426
+ proposalGasFees: proposalGlobals.gasFees.toInspect(),
427
+ parentGasFees: parentGlobals.gasFees.toInspect()
428
+ });
429
+ return {
430
+ reason: 'global_variables_mismatch'
431
+ };
432
+ }
433
+ return undefined;
434
+ }
435
+ getReexecutionDeadline(slot, config) {
436
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
437
+ const msNeededForPropagationAndPublishing = this.config.validatorReexecuteDeadlineMs;
438
+ return new Date(nextSlotTimestampSeconds * 1000 - msNeededForPropagationAndPublishing);
439
+ }
440
+ /**
441
+ * Gets all prior blocks in the same checkpoint (same slot and checkpoint number) up to but not including upToBlockNumber.
442
+ */ async getBlocksInCheckpoint(slot, upToBlockNumber, checkpointNumber) {
443
+ const blocks = [];
444
+ let currentBlockNumber = BlockNumber(upToBlockNumber - 1);
445
+ while(currentBlockNumber >= INITIAL_L2_BLOCK_NUM){
446
+ const block = await this.blockSource.getL2BlockNew(currentBlockNumber);
447
+ if (!block || block.header.getSlot() !== slot || block.checkpointNumber !== checkpointNumber) {
448
+ break;
449
+ }
450
+ blocks.unshift(block);
451
+ currentBlockNumber = BlockNumber(currentBlockNumber - 1);
452
+ }
453
+ return blocks;
454
+ }
455
+ getReexecuteFailureReason(err) {
456
+ if (err instanceof ReExStateMismatchError) {
457
+ return 'state_mismatch';
458
+ } else if (err instanceof ReExFailedTxsError) {
459
+ return 'failed_txs';
460
+ } else if (err instanceof ReExTimeoutError) {
461
+ return 'timeout';
462
+ } else {
463
+ return 'unknown_error';
464
+ }
465
+ }
466
+ async reexecuteTransactions(proposal, blockNumber, checkpointNumber, txs, l1ToL2Messages) {
467
+ const env = {
468
+ stack: [],
469
+ error: void 0,
470
+ hasError: false
471
+ };
472
+ try {
473
+ const { blockHeader, txHashes } = proposal;
474
+ // If we do not have all of the transactions, then we should fail
475
+ if (txs.length !== txHashes.length) {
476
+ const foundTxHashes = txs.map((tx)=>tx.getTxHash());
477
+ const missingTxHashes = txHashes.filter((txHash)=>!foundTxHashes.includes(txHash));
478
+ throw new TransactionsNotAvailableError(missingTxHashes);
479
+ }
480
+ const timer = new Timer();
481
+ const slot = proposal.slotNumber;
482
+ const config = this.checkpointsBuilder.getConfig();
483
+ // Get prior blocks in this checkpoint (same slot and checkpoint number)
484
+ const priorBlocks = await this.getBlocksInCheckpoint(slot, blockNumber, checkpointNumber);
485
+ // Fork before the block to be built
486
+ const parentBlockNumber = BlockNumber(blockNumber - 1);
487
+ const fork = _ts_add_disposable_resource(env, await this.worldState.fork(parentBlockNumber), false);
488
+ // Build checkpoint constants from proposal (excludes blockNumber and timestamp which are per-block)
489
+ const constants = {
490
+ chainId: new Fr(config.l1ChainId),
491
+ version: new Fr(config.rollupVersion),
492
+ slotNumber: slot,
493
+ coinbase: blockHeader.globalVariables.coinbase,
494
+ feeRecipient: blockHeader.globalVariables.feeRecipient,
495
+ gasFees: blockHeader.globalVariables.gasFees
496
+ };
497
+ // Create checkpoint builder with prior blocks
498
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, l1ToL2Messages, fork, priorBlocks);
499
+ // Build the new block
500
+ const deadline = this.getReexecutionDeadline(slot, config);
501
+ const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
502
+ deadline,
503
+ expectedEndState: blockHeader.state
504
+ });
505
+ const { block, failedTxs } = result;
506
+ const numFailedTxs = failedTxs.length;
507
+ this.log.verbose(`Transaction re-execution complete for slot ${slot}`, {
508
+ numFailedTxs,
509
+ numProposalTxs: txHashes.length,
510
+ numProcessedTxs: block.body.txEffects.length,
511
+ slot
512
+ });
513
+ if (numFailedTxs > 0) {
514
+ this.metrics?.recordFailedReexecution(proposal);
515
+ throw new ReExFailedTxsError(numFailedTxs);
516
+ }
517
+ if (block.body.txEffects.length !== txHashes.length) {
518
+ this.metrics?.recordFailedReexecution(proposal);
519
+ throw new ReExTimeoutError();
520
+ }
521
+ // Throw a ReExStateMismatchError error if state updates do not match
522
+ // Compare the full block structure (archive and header) from the built block with the proposal
523
+ const archiveMatches = proposal.archive.equals(block.archive.root);
524
+ const headerMatches = proposal.blockHeader.equals(block.header);
525
+ if (!archiveMatches || !headerMatches) {
526
+ this.log.warn(`Re-execution state mismatch for slot ${slot}`, {
527
+ expectedArchive: block.archive.root.toString(),
528
+ actualArchive: proposal.archive.toString(),
529
+ expectedHeader: block.header.toInspect(),
530
+ actualHeader: proposal.blockHeader.toInspect()
531
+ });
532
+ this.metrics?.recordFailedReexecution(proposal);
533
+ throw new ReExStateMismatchError(proposal.archive, block.archive.root);
534
+ }
535
+ const reexecutionTimeMs = timer.ms();
536
+ const totalManaUsed = block.header.totalManaUsed.toNumber() / 1e6;
537
+ this.metrics?.recordReex(reexecutionTimeMs, txs.length, totalManaUsed);
538
+ return {
539
+ block,
540
+ failedTxs,
541
+ reexecutionTimeMs,
542
+ totalManaUsed
543
+ };
544
+ } catch (e) {
545
+ env.error = e;
546
+ env.hasError = true;
547
+ } finally{
548
+ _ts_dispose_resources(env);
549
+ }
550
+ }
551
+ }
@@ -0,0 +1,70 @@
1
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import { DateProvider, Timer } from '@aztec/foundation/timer';
4
+ import { LightweightCheckpointBuilder } from '@aztec/prover-client/light';
5
+ import { PublicProcessor } from '@aztec/simulator/server';
6
+ import { L2BlockNew } from '@aztec/stdlib/block';
7
+ import { Checkpoint } from '@aztec/stdlib/checkpoint';
8
+ import type { ContractDataSource } from '@aztec/stdlib/contract';
9
+ import { Gas } from '@aztec/stdlib/gas';
10
+ import { type FullNodeBlockBuilderConfig, type MerkleTreeWriteOperations, type PublicProcessorLimits } from '@aztec/stdlib/interfaces/server';
11
+ import { type CheckpointGlobalVariables, type FailedTx, GlobalVariables, StateReference, Tx } from '@aztec/stdlib/tx';
12
+ import { type TelemetryClient } from '@aztec/telemetry-client';
13
+ export interface BuildBlockInCheckpointResult {
14
+ block: L2BlockNew;
15
+ publicGas: Gas;
16
+ publicProcessorDuration: number;
17
+ numTxs: number;
18
+ failedTxs: FailedTx[];
19
+ blockBuildingTimer: Timer;
20
+ usedTxs: Tx[];
21
+ }
22
+ /**
23
+ * Builder for a single checkpoint. Handles building blocks within the checkpoint
24
+ * and completing it.
25
+ */
26
+ export declare class CheckpointBuilder {
27
+ private checkpointBuilder;
28
+ private fork;
29
+ private config;
30
+ private contractDataSource;
31
+ private dateProvider;
32
+ private telemetryClient;
33
+ constructor(checkpointBuilder: LightweightCheckpointBuilder, fork: MerkleTreeWriteOperations, config: FullNodeBlockBuilderConfig, contractDataSource: ContractDataSource, dateProvider: DateProvider, telemetryClient: TelemetryClient);
34
+ getConstantData(): CheckpointGlobalVariables;
35
+ /**
36
+ * Builds a single block within this checkpoint.
37
+ */
38
+ buildBlock(pendingTxs: Iterable<Tx> | AsyncIterable<Tx>, blockNumber: BlockNumber, timestamp: bigint, opts: PublicProcessorLimits & {
39
+ expectedEndState?: StateReference;
40
+ }): Promise<BuildBlockInCheckpointResult>;
41
+ /** Completes the checkpoint and returns it. */
42
+ completeCheckpoint(): Promise<Checkpoint>;
43
+ /** Gets the checkpoint currently in progress. */
44
+ getCheckpoint(): Promise<Checkpoint>;
45
+ protected makeBlockBuilderDeps(globalVariables: GlobalVariables, fork: MerkleTreeWriteOperations): Promise<{
46
+ processor: PublicProcessor;
47
+ validator: import("@aztec/stdlib/interfaces/server").PublicProcessorValidator;
48
+ }>;
49
+ }
50
+ /**
51
+ * Factory for creating checkpoint builders.
52
+ */
53
+ export declare class FullNodeCheckpointsBuilder {
54
+ private config;
55
+ private contractDataSource;
56
+ private dateProvider;
57
+ private telemetryClient;
58
+ constructor(config: FullNodeBlockBuilderConfig, contractDataSource: ContractDataSource, dateProvider: DateProvider, telemetryClient?: TelemetryClient);
59
+ getConfig(): FullNodeBlockBuilderConfig;
60
+ updateConfig(config: Partial<FullNodeBlockBuilderConfig>): void;
61
+ /**
62
+ * Starts a new checkpoint and returns a CheckpointBuilder to build blocks within it.
63
+ */
64
+ startCheckpoint(checkpointNumber: CheckpointNumber, constants: CheckpointGlobalVariables, l1ToL2Messages: Fr[], fork: MerkleTreeWriteOperations): Promise<CheckpointBuilder>;
65
+ /**
66
+ * Opens a checkpoint, either starting fresh or resuming from existing blocks.
67
+ */
68
+ openCheckpoint(checkpointNumber: CheckpointNumber, constants: CheckpointGlobalVariables, l1ToL2Messages: Fr[], fork: MerkleTreeWriteOperations, existingBlocks?: L2BlockNew[]): Promise<CheckpointBuilder>;
69
+ }
70
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2hlY2twb2ludF9idWlsZGVyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvY2hlY2twb2ludF9idWlsZGVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxXQUFXLEVBQUUsZ0JBQWdCLEVBQUUsTUFBTSxpQ0FBaUMsQ0FBQztBQUVoRixPQUFPLEVBQUUsRUFBRSxFQUFFLE1BQU0sZ0NBQWdDLENBQUM7QUFHcEQsT0FBTyxFQUFFLFlBQVksRUFBRSxLQUFLLEVBQVcsTUFBTSx5QkFBeUIsQ0FBQztBQUV2RSxPQUFPLEVBQUUsNEJBQTRCLEVBQUUsTUFBTSw0QkFBNEIsQ0FBQztBQUMxRSxPQUFPLEVBR0wsZUFBZSxFQUVoQixNQUFNLHlCQUF5QixDQUFDO0FBQ2pDLE9BQU8sRUFBRSxVQUFVLEVBQUUsTUFBTSxxQkFBcUIsQ0FBQztBQUNqRCxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sMEJBQTBCLENBQUM7QUFDdEQsT0FBTyxLQUFLLEVBQUUsa0JBQWtCLEVBQUUsTUFBTSx3QkFBd0IsQ0FBQztBQUNqRSxPQUFPLEVBQUUsR0FBRyxFQUFFLE1B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