@aztec/validator-client 0.0.1-commit.f2ce05ee → 0.0.1-commit.f5a9928

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 (56) hide show
  1. package/README.md +54 -10
  2. package/dest/checkpoint_builder.d.ts +32 -11
  3. package/dest/checkpoint_builder.d.ts.map +1 -1
  4. package/dest/checkpoint_builder.js +136 -47
  5. package/dest/config.d.ts +9 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +45 -7
  8. package/dest/duties/validation_service.d.ts +12 -13
  9. package/dest/duties/validation_service.d.ts.map +1 -1
  10. package/dest/duties/validation_service.js +33 -45
  11. package/dest/factory.d.ts +11 -5
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +8 -7
  14. package/dest/index.d.ts +2 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +1 -2
  17. package/dest/key_store/ha_key_store.js +1 -1
  18. package/dest/key_store/web3signer_key_store.d.ts +10 -2
  19. package/dest/key_store/web3signer_key_store.d.ts.map +1 -1
  20. package/dest/key_store/web3signer_key_store.js +32 -41
  21. package/dest/metrics.d.ts +14 -2
  22. package/dest/metrics.d.ts.map +1 -1
  23. package/dest/metrics.js +27 -1
  24. package/dest/proposal_handler.d.ts +173 -0
  25. package/dest/proposal_handler.d.ts.map +1 -0
  26. package/dest/proposal_handler.js +1302 -0
  27. package/dest/validator.d.ts +62 -26
  28. package/dest/validator.d.ts.map +1 -1
  29. package/dest/validator.js +347 -238
  30. package/package.json +19 -19
  31. package/src/checkpoint_builder.ts +156 -42
  32. package/src/config.ts +53 -9
  33. package/src/duties/validation_service.ts +59 -54
  34. package/src/factory.ts +20 -7
  35. package/src/index.ts +1 -2
  36. package/src/key_store/ha_key_store.ts +1 -1
  37. package/src/key_store/web3signer_key_store.ts +43 -59
  38. package/src/metrics.ts +39 -1
  39. package/src/proposal_handler.ts +1418 -0
  40. package/src/validator.ts +480 -286
  41. package/dest/block_proposal_handler.d.ts +0 -63
  42. package/dest/block_proposal_handler.d.ts.map +0 -1
  43. package/dest/block_proposal_handler.js +0 -546
  44. package/dest/tx_validator/index.d.ts +0 -3
  45. package/dest/tx_validator/index.d.ts.map +0 -1
  46. package/dest/tx_validator/index.js +0 -2
  47. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  48. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  49. package/dest/tx_validator/nullifier_cache.js +0 -24
  50. package/dest/tx_validator/tx_validator_factory.d.ts +0 -19
  51. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  52. package/dest/tx_validator/tx_validator_factory.js +0 -54
  53. package/src/block_proposal_handler.ts +0 -555
  54. package/src/tx_validator/index.ts +0 -2
  55. package/src/tx_validator/nullifier_cache.ts +0 -30
  56. package/src/tx_validator/tx_validator_factory.ts +0 -154
@@ -0,0 +1,1302 @@
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 { encodeCheckpointBlobDataFromBlocks, getBlobsPerL1Block } from '@aztec/blob-lib';
67
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
68
+ import { validateFeeAssetPriceModifier } from '@aztec/ethereum/contracts';
69
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
70
+ import { pick } from '@aztec/foundation/collection';
71
+ import { Fr } from '@aztec/foundation/curves/bn254';
72
+ import { TimeoutError } from '@aztec/foundation/error';
73
+ import { FifoSet } from '@aztec/foundation/fifo-set';
74
+ import { createLogger } from '@aztec/foundation/log';
75
+ import { retryUntil } from '@aztec/foundation/retry';
76
+ import { DateProvider, Timer } from '@aztec/foundation/timer';
77
+ import { isErrorClass } from '@aztec/foundation/types';
78
+ import { getPreviousCheckpointOutHashes, validateCheckpoint } from '@aztec/stdlib/checkpoint';
79
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
80
+ import { Gas } from '@aztec/stdlib/gas';
81
+ import { accumulateCheckpointOutHashes, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
82
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
83
+ import { InvalidBlockProposalTxsError, ReExFailedTxsError, ReExInitialStateMismatchError, ReExStateMismatchError, ReExTimeoutError, TransactionsNotAvailableError } from '@aztec/stdlib/validators';
84
+ import { getTelemetryClient } from '@aztec/telemetry-client';
85
+ /**
86
+ * Mapping from a checkpoint-proposal validation failure reason to the tracker outcome that
87
+ * `handleCheckpointProposal` should record. `undefined` means do not record (signature
88
+ * couldn't be verified, or the checkpoint is already on L1 so the question is moot).
89
+ */ /* eslint-disable camelcase */ const CHECKPOINT_VALIDATION_REASON_TO_OUTCOME = {
90
+ invalid_signature: undefined,
91
+ invalid_fee_asset_price_modifier: 'invalid',
92
+ checkpoint_already_published: undefined,
93
+ last_block_not_found: 'unvalidated',
94
+ block_fetch_error: 'unvalidated',
95
+ world_state_not_synced: 'unvalidated',
96
+ initial_archive_mismatch: 'unvalidated',
97
+ no_blocks_for_slot: 'unvalidated',
98
+ last_block_archive_mismatch: 'invalid',
99
+ too_many_blocks_in_checkpoint: 'invalid',
100
+ checkpoint_header_mismatch: 'invalid',
101
+ archive_mismatch: 'invalid',
102
+ out_hash_mismatch: 'invalid',
103
+ checkpoint_validation_failed: 'invalid'
104
+ };
105
+ const MAX_TRACKED_INVALID_PROPOSAL_SLOTS = 1000;
106
+ /** Block-proposal validation failures that constitute a slashable invalid-block offense. */ export const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
107
+ 'state_mismatch',
108
+ 'failed_txs',
109
+ 'global_variables_mismatch',
110
+ 'invalid_proposal',
111
+ 'parent_block_wrong_slot',
112
+ 'in_hash_mismatch',
113
+ 'duplicate_txs',
114
+ 'invalid_embedded_txs'
115
+ ];
116
+ /** Checkpoint-proposal validation failures that constitute a slashable invalid-checkpoint offense. */ export const SLASHABLE_CHECKPOINT_PROPOSAL_VALIDATION_RESULT = {
117
+ // enabled
118
+ ['invalid_fee_asset_price_modifier']: true,
119
+ ['checkpoint_header_mismatch']: true,
120
+ // These late mismatches should normally be caught by earlier checks, but if reached after validating the local
121
+ // checkpoint inputs, the proposer-signed payload disagrees with deterministic recomputation.
122
+ ['archive_mismatch']: true,
123
+ ['out_hash_mismatch']: true,
124
+ ['no_blocks_for_slot']: true,
125
+ ['too_many_blocks_in_checkpoint']: true,
126
+ ['checkpoint_validation_failed']: true,
127
+ ['last_block_archive_mismatch']: true,
128
+ // disabled
129
+ ['invalid_signature']: false,
130
+ ['last_block_not_found']: false,
131
+ ['block_fetch_error']: false,
132
+ ['world_state_not_synced']: false,
133
+ // A reorg / divergent local chain, not a proposer offense (mirrors the block path's initial_state_mismatch).
134
+ ['initial_archive_mismatch']: false,
135
+ ['checkpoint_already_published']: false
136
+ };
137
+ /**
138
+ * Handles block and checkpoint proposals for both validator and non-validator nodes. Also tracks which slots
139
+ * had a slashable invalid proposal or a proposal equivocation, exposing them via the
140
+ * `InvalidProposalSlotSource` interface consumed by the attested-invalid-proposal slashing watcher. The
141
+ * tracking is populated as a side effect of validating/re-executing proposals, so any node that re-executes
142
+ * proposals (the default) can serve it — not only validators.
143
+ */ export class ProposalHandler {
144
+ checkpointsBuilder;
145
+ worldState;
146
+ blockSource;
147
+ l1ToL2MessageSource;
148
+ txProvider;
149
+ epochCache;
150
+ timetable;
151
+ config;
152
+ blobClient;
153
+ reexecutionTracker;
154
+ metrics;
155
+ dateProvider;
156
+ log;
157
+ tracer;
158
+ /** Cached last checkpoint validation result to avoid double-validation on validator nodes.
159
+ * Keyed by signed-payload hash so two proposals at the same (slot, archive) but with a
160
+ * different `feeAssetPriceModifier` (or any other signed field) are validated independently. */ lastCheckpointValidationResult;
161
+ /** Archiver reference for setting proposed checkpoints (pipelining). Set via register(). */ archiver;
162
+ /** Returns current validator addresses for own-proposal detection. Set via register(). */ getOwnValidatorAddresses;
163
+ /** P2P proposal pool access for deciding when retained proposals should block archiver processing. */ p2pClient;
164
+ checkpointProposalValidationFailureCallback;
165
+ /** Slots at which a slashable invalid block or checkpoint proposal was observed. */ slotsWithInvalidProposals;
166
+ /** Slots at which a proposal equivocation was observed; suppresses attested-to-invalid-proposal slashing. */ slotsWithProposalEquivocation;
167
+ constructor(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, epochCache, timetable, config, blobClient, reexecutionTracker, metrics, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator:proposal-handler')){
168
+ this.checkpointsBuilder = checkpointsBuilder;
169
+ this.worldState = worldState;
170
+ this.blockSource = blockSource;
171
+ this.l1ToL2MessageSource = l1ToL2MessageSource;
172
+ this.txProvider = txProvider;
173
+ this.epochCache = epochCache;
174
+ this.timetable = timetable;
175
+ this.config = config;
176
+ this.blobClient = blobClient;
177
+ this.reexecutionTracker = reexecutionTracker;
178
+ this.metrics = metrics;
179
+ this.dateProvider = dateProvider;
180
+ this.log = log;
181
+ this.slotsWithInvalidProposals = FifoSet.withLimit(MAX_TRACKED_INVALID_PROPOSAL_SLOTS);
182
+ this.slotsWithProposalEquivocation = FifoSet.withLimit(MAX_TRACKED_INVALID_PROPOSAL_SLOTS);
183
+ if (config.fishermanMode) {
184
+ this.log = this.log.createChild('[FISHERMAN]');
185
+ }
186
+ this.tracer = telemetry.getTracer('ProposalHandler');
187
+ }
188
+ updateConfig(config) {
189
+ this.config = {
190
+ ...this.config,
191
+ ...config
192
+ };
193
+ }
194
+ setCheckpointProposalValidationFailureCallback(callback) {
195
+ this.checkpointProposalValidationFailureCallback = callback;
196
+ }
197
+ /**
198
+ * Records the proposer's own checkpoint proposal as a `valid` outcome in the re-execution
199
+ * tracker. Without this, the node's own checkpoint proposals never flow through
200
+ * `handleCheckpointProposal` (proposers don't validate their own proposals), so its sentinel
201
+ * sees no outcome for slots where it was the proposer and reports itself as inactive.
202
+ *
203
+ * `archive` should be the locally-computed archive (NOT the broadcast archive, which may have
204
+ * been deliberately corrupted in tests via `broadcastInvalidBlockProposal` /
205
+ * `broadcastInvalidCheckpointProposalOnly`). Recording the local archive correctly models the
206
+ * proposer's own view of its own work.
207
+ */ recordOwnCheckpointProposalAsValid(slot, archive, checkpointNumber) {
208
+ this.reexecutionTracker.recordOutcome(slot, archive, 'valid', checkpointNumber);
209
+ }
210
+ /** Whether a slashable invalid block or checkpoint proposal was observed at the given slot (InvalidProposalSlotSource). */ hasInvalidProposals(slotNumber) {
211
+ return this.slotsWithInvalidProposals.has(slotNumber);
212
+ }
213
+ /** Whether a proposal equivocation was observed at the given slot (InvalidProposalSlotSource). */ hasProposalEquivocation(slotNumber) {
214
+ return this.slotsWithProposalEquivocation.has(slotNumber);
215
+ }
216
+ /** Records a slot as having a slashable invalid proposal, for offense observers (sentinel/slasher watchers). */ markInvalidProposalSlot(slotNumber) {
217
+ this.slotsWithInvalidProposals.add(slotNumber);
218
+ }
219
+ /** Records a slot as having a proposal equivocation, which suppresses attested-to-invalid-proposal slashing. */ markProposalEquivocation(slotNumber) {
220
+ this.slotsWithProposalEquivocation.add(slotNumber);
221
+ }
222
+ /**
223
+ * Registers handlers for block and checkpoint proposals on the p2p client.
224
+ * Records the p2p client so validation can inspect retained proposals.
225
+ * Block proposals are registered for non-validator nodes (validators register their own enhanced handler).
226
+ * The all-nodes checkpoint proposal handler is always registered for validation, caching, and pipelining.
227
+ * @param archiver - Archiver reference for setting proposed checkpoints (pipelining)
228
+ * @param getOwnValidatorAddresses - Returns current validator addresses for own-proposal detection
229
+ */ register(p2pClient, shouldReexecute, archiver, getOwnValidatorAddresses) {
230
+ this.p2pClient = p2pClient;
231
+ this.archiver = archiver;
232
+ this.getOwnValidatorAddresses = getOwnValidatorAddresses;
233
+ // Non-validator handler that processes or re-executes for monitoring but does not attest.
234
+ // Returns boolean indicating whether the proposal was valid.
235
+ const blockHandler = async (proposal, proposalSender)=>{
236
+ try {
237
+ const { slotNumber, blockNumber } = proposal;
238
+ const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
239
+ if (result.isValid) {
240
+ this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
241
+ blockNumber: result.blockNumber,
242
+ slotNumber,
243
+ reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
244
+ totalManaUsed: result.reexecutionResult?.totalManaUsed,
245
+ numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
246
+ reexecuted: shouldReexecute
247
+ });
248
+ return true;
249
+ } else {
250
+ // Track invalid proposals / equivocations so offense observers (the attested-invalid-proposal
251
+ // watcher) work on non-validator nodes too. Validators populate these via their own handlers.
252
+ // Skip invalid-proposal marking while the escape hatch is open, matching the validator path,
253
+ // which intentionally disables invalid-block slashing then.
254
+ if (result.reason === 'checkpoint_proposal_equivocation') {
255
+ this.markProposalEquivocation(slotNumber);
256
+ } else if (SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(result.reason) && !await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
257
+ this.markInvalidProposalSlot(slotNumber);
258
+ }
259
+ this.log.warn(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} failed processing with ${result.reason}`, {
260
+ blockNumber: result.blockNumber,
261
+ slotNumber,
262
+ reason: result.reason
263
+ });
264
+ return false;
265
+ }
266
+ } catch (error) {
267
+ this.log.error('Error processing block proposal in non-validator handler', error);
268
+ return false;
269
+ }
270
+ };
271
+ p2pClient.registerBlockProposalHandler(blockHandler);
272
+ // p2p detects duplicate (equivocated) proposals without routing them through the handlers above, so mark
273
+ // the slot as equivocated here. This suppresses false-positive attested-to-invalid-proposal slashing on
274
+ // non-validator offense collectors. Validators overwrite this with their own richer handler.
275
+ p2pClient.registerDuplicateProposalCallback((info)=>this.markProposalEquivocation(info.slot));
276
+ // All-nodes checkpoint proposal handler: validates, caches, and sets proposed checkpoint for pipelining.
277
+ // Runs for all nodes (validators and non-validators). Validators get the cached result in the
278
+ // validator-specific callback (attestToCheckpointProposal) which runs after this one.
279
+ const checkpointHandler = async (proposal, _sender)=>{
280
+ try {
281
+ const pipeliningTimer = new Timer();
282
+ const proposalInfo = {
283
+ slot: proposal.slotNumber,
284
+ archive: proposal.archive.toString(),
285
+ proposer: proposal.getSender()?.toString()
286
+ };
287
+ if (this.config.skipCheckpointProposalValidation) {
288
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${proposal.slotNumber}`, proposalInfo);
289
+ return undefined;
290
+ }
291
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(proposal.slotNumber)) {
292
+ this.log.warn(`Escape hatch open for slot ${proposal.slotNumber}, skipping checkpoint proposal validation`, proposalInfo);
293
+ return undefined;
294
+ }
295
+ // A proposal is "own" when it was signed by a validator key this node also owns. The true local
296
+ // proposer already built, validated, and stored this checkpoint before broadcasting, so a matching
297
+ // proposed checkpoint is already in its archiver — skip the redundant re-validation. An HA peer that
298
+ // shares the proposer's keys sees the same "own" proposal over gossip but never built it, so it has
299
+ // nothing stored; it falls through to the normal validate-and-persist path below to hydrate the
300
+ // proposed-checkpoint metadata it needs to build the next slot on top of this checkpoint.
301
+ const proposer = proposal.getSender();
302
+ const ownAddresses = this.getOwnValidatorAddresses?.();
303
+ const isOwnProposal = proposer && ownAddresses?.some((addr)=>addr === proposer.toString());
304
+ if (isOwnProposal) {
305
+ const existing = await this.archiver?.getProposedCheckpointData({
306
+ slot: proposal.slotNumber
307
+ });
308
+ if (existing?.archive.root.equals(proposal.archive)) {
309
+ this.log.debug(`Skipping sync for existing own checkpoint proposal at slot ${proposal.slotNumber}`);
310
+ return undefined;
311
+ }
312
+ }
313
+ const result = await this.handleCheckpointProposal(proposal, proposalInfo);
314
+ if (!result.isValid) {
315
+ // Track invalid checkpoint proposals so offense observers (the attested-invalid-proposal watcher)
316
+ // work on non-validator nodes too. This handler runs for all nodes; validators also mark via the
317
+ // failure callback below (idempotent).
318
+ if (SLASHABLE_CHECKPOINT_PROPOSAL_VALIDATION_RESULT[result.reason]) {
319
+ this.markInvalidProposalSlot(proposal.slotNumber);
320
+ }
321
+ await this.checkpointProposalValidationFailureCallback?.(proposal, result, proposalInfo);
322
+ } else if (this.archiver) {
323
+ const set = await this.setProposedCheckpoint(proposal);
324
+ if (set) {
325
+ this.metrics?.recordCheckpointProposalToPipelinedStateDuration(pipeliningTimer.ms());
326
+ }
327
+ }
328
+ } catch (err) {
329
+ this.log.warn(`Error handling checkpoint proposal for slot ${proposal.slotNumber}`, {
330
+ err
331
+ });
332
+ }
333
+ return undefined;
334
+ };
335
+ p2pClient.registerAllNodesCheckpointProposalHandler(checkpointHandler);
336
+ return this;
337
+ }
338
+ /**
339
+ * Processes a block proposal: collects its txs and, if requested, re-executes them to check the resulting
340
+ * block against the proposal. Expects the proposal to have already passed p2p ingress validation (signature
341
+ * context, signature, expected proposer, index within checkpoint, tx field checks, and the receive-window
342
+ * timeliness check) — none of those are re-applied here, and only deterministic properties of the payload
343
+ * are validated before processing.
344
+ */ async handleBlockProposal(proposal, proposalSender, shouldReexecute) {
345
+ const slotNumber = proposal.slotNumber;
346
+ const proposer = proposal.getSender();
347
+ // Reject proposals with invalid signatures
348
+ if (!proposer) {
349
+ this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
350
+ return {
351
+ isValid: false,
352
+ reason: 'invalid_signature'
353
+ };
354
+ }
355
+ const proposalInfo = {
356
+ ...proposal.toBlockInfo(),
357
+ proposer: proposer.toString(),
358
+ blockNumber: undefined,
359
+ checkpointNumber: undefined
360
+ };
361
+ this.log.info(`Processing proposal for slot ${slotNumber}`, {
362
+ ...proposalInfo,
363
+ txHashes: proposal.txHashes.map((t)=>t.toString())
364
+ });
365
+ // The receive-window check from p2p ingress is deliberately not re-applied here: its outcome depends on
366
+ // the wall clock at evaluation time, so re-running it turned node-local processing latency into an
367
+ // invalid-proposal verdict against an honest proposer, which then fed the invalid-block slashing path.
368
+ // A tx can only appear once in a block: the second copy would emit nullifiers already emitted by the
369
+ // first. This is not a relaying-peer fault, so it passes gossip validation and is classified here as
370
+ // proposer misbehavior. Tx collection also reconciles a deduplicated hash set against the full list,
371
+ // so it must not be handed a proposal with repeated hashes.
372
+ const uniqueTxHashes = new Set(proposal.txHashes.map((txHash)=>txHash.toString()));
373
+ if (uniqueTxHashes.size !== proposal.txHashes.length) {
374
+ this.log.warn(`Proposal lists duplicate tx hashes, skipping processing`, {
375
+ ...proposalInfo,
376
+ txCount: proposal.txHashes.length,
377
+ uniqueTxCount: uniqueTxHashes.size
378
+ });
379
+ return {
380
+ isValid: false,
381
+ reason: 'duplicate_txs'
382
+ };
383
+ }
384
+ const retainedSlotValidation = await this.validateNewBlockInSlot(proposal);
385
+ if (!retainedSlotValidation.isValid) {
386
+ this.log.info(`Block proposal conflicts with retained proposals, skipping archiver processing`, {
387
+ ...proposalInfo,
388
+ indexWithinCheckpoint: proposal.indexWithinCheckpoint,
389
+ reason: retainedSlotValidation.reason
390
+ });
391
+ return {
392
+ isValid: false,
393
+ blockNumber: proposal.blockNumber,
394
+ reason: retainedSlotValidation.reason
395
+ };
396
+ }
397
+ // The proposer builds ahead of L1 submission under pipelining, so the block source won't have
398
+ // synced to the proposed slot yet. We deliberately do not wait for it to sync here, to avoid
399
+ // eating into the attestation window.
400
+ // Check that the parent proposal is a block we know, otherwise reexecution would fail.
401
+ // If we don't find it immediately, we keep retrying for a while; it may be we still
402
+ // need to process other block proposals to get to it.
403
+ const parentBlock = await this.getParentBlock(proposal);
404
+ if (parentBlock === undefined) {
405
+ this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
406
+ return {
407
+ isValid: false,
408
+ reason: 'parent_block_not_found'
409
+ };
410
+ }
411
+ // Check that the parent block's slot is not greater than the proposal's slot.
412
+ if (parentBlock !== 'genesis' && parentBlock.header.getSlot() > slotNumber) {
413
+ this.log.warn(`Parent block slot is greater than proposal slot, skipping processing`, {
414
+ parentBlockSlot: parentBlock.header.getSlot().toString(),
415
+ proposalSlot: slotNumber.toString(),
416
+ ...proposalInfo
417
+ });
418
+ return {
419
+ isValid: false,
420
+ reason: 'parent_block_wrong_slot'
421
+ };
422
+ }
423
+ // Compute the block number based on the parent block
424
+ const blockNumber = parentBlock === 'genesis' ? BlockNumber(INITIAL_L2_BLOCK_NUM) : BlockNumber(parentBlock.header.getBlockNumber() + 1);
425
+ proposalInfo.blockNumber = blockNumber;
426
+ // Check that this block number does not exist already. During a reorg the archiver can still hold a
427
+ // stale block at this number (a different archive, about to be pruned) while the proposal carries the
428
+ // rebuilt replacement; resolveExistingBlockAtNumber waits for the local prune in that case so the
429
+ // rebuilt block is processed in time to attest, rather than being permanently dropped on a bare
430
+ // number collision.
431
+ const existingBlock = await this.resolveExistingBlockAtNumber(blockNumber, proposal.archive, slotNumber);
432
+ if (existingBlock) {
433
+ this.log.warn(`Block number ${blockNumber} already exists, skipping processing`, proposalInfo);
434
+ return {
435
+ isValid: false,
436
+ blockNumber,
437
+ reason: 'block_number_already_exists'
438
+ };
439
+ }
440
+ // Collect txs from the proposal. We start doing this as early as possible,
441
+ // 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.
442
+ const collected = await this.collectProposalTxs(proposal, blockNumber, proposalSender, proposalInfo);
443
+ if (collected === 'invalid_embedded_txs') {
444
+ return {
445
+ isValid: false,
446
+ blockNumber,
447
+ reason: collected
448
+ };
449
+ }
450
+ const { txs, missingTxs } = collected;
451
+ // Record the tx-collection outcome on the re-execution tracker
452
+ this.reexecutionTracker.recordTxsCollected(slotNumber, proposal.indexWithinCheckpoint, missingTxs.length === 0);
453
+ // If reexecution is disabled, bail. We were just interested in triggering tx collection.
454
+ if (!shouldReexecute) {
455
+ this.log.info(`Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`, proposalInfo);
456
+ return {
457
+ isValid: true,
458
+ blockNumber
459
+ };
460
+ }
461
+ // Compute the checkpoint number for this block and validate checkpoint consistency
462
+ const checkpointResult = this.computeCheckpointNumber(proposal, parentBlock, proposalInfo);
463
+ if (checkpointResult.reason) {
464
+ return {
465
+ isValid: false,
466
+ blockNumber,
467
+ reason: checkpointResult.reason
468
+ };
469
+ }
470
+ const checkpointNumber = checkpointResult.checkpointNumber;
471
+ proposalInfo.checkpointNumber = checkpointNumber;
472
+ // Check that I have the same set of l1ToL2Messages as the proposal
473
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
474
+ const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
475
+ const proposalInHash = proposal.inHash;
476
+ if (!computedInHash.equals(proposalInHash)) {
477
+ this.log.warn(`L1 to L2 messages in hash mismatch, skipping processing`, {
478
+ proposalInHash: proposalInHash.toString(),
479
+ computedInHash: computedInHash.toString(),
480
+ ...proposalInfo
481
+ });
482
+ return {
483
+ isValid: false,
484
+ blockNumber,
485
+ reason: 'in_hash_mismatch'
486
+ };
487
+ }
488
+ // Check that all of the transactions in the proposal are available
489
+ if (missingTxs.length > 0) {
490
+ this.log.warn(`Missing ${missingTxs.length} txs to process proposal`, {
491
+ ...proposalInfo,
492
+ missingTxs
493
+ });
494
+ return {
495
+ isValid: false,
496
+ blockNumber,
497
+ reason: 'txs_not_available'
498
+ };
499
+ }
500
+ // Collect the out hashes of all the checkpoints before this one in the same epoch.
501
+ // Mirror the proposer-side fallback: under pipelining the immediately-preceding cp may not
502
+ // yet be on L1, in which case the helper grafts the locally-known proposed cp's outHash.
503
+ const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
504
+ const previousCheckpointOutHashes = await getPreviousCheckpointOutHashes({
505
+ blockSource: this.blockSource,
506
+ epoch,
507
+ checkpointNumber,
508
+ l1Constants: this.epochCache.getL1Constants(),
509
+ pipeliningEnabled: true,
510
+ log: this.log
511
+ });
512
+ // Try re-executing the transactions in the proposal if needed
513
+ let reexecutionResult;
514
+ try {
515
+ this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
516
+ reexecutionResult = await this.reexecuteTransactions(proposal, blockNumber, checkpointNumber, txs, l1ToL2Messages, previousCheckpointOutHashes);
517
+ } catch (error) {
518
+ this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
519
+ const reason = this.getReexecuteFailureReason(error);
520
+ return {
521
+ isValid: false,
522
+ blockNumber,
523
+ reason,
524
+ reexecutionResult
525
+ };
526
+ }
527
+ // If we succeeded, push this block into the archiver (unless disabled)
528
+ if (reexecutionResult?.block && !this.config.skipPushProposedBlocksToArchiver) {
529
+ await this.blockSource.addBlock(reexecutionResult.block);
530
+ }
531
+ this.log.info(`Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`, {
532
+ ...proposalInfo,
533
+ ...pick(reexecutionResult, 'reexecutionTimeMs', 'totalManaUsed')
534
+ });
535
+ return {
536
+ isValid: true,
537
+ blockNumber,
538
+ reexecutionResult
539
+ };
540
+ }
541
+ /**
542
+ * Collects the txs for a proposal, returning `invalid_embedded_txs` if the proposal carries a tx that fails
543
+ * minimum integrity validation. That is proposer misbehavior — the proposal signs both the tx hashes and the
544
+ * tx objects — so the caller turns it into an invalid-proposal result that reaches slashing and invalid-slot
545
+ * accounting, rather than letting it escape as an exception. Any other collection error is a local failure
546
+ * and keeps propagating.
547
+ */ async collectProposalTxs(proposal, blockNumber, proposalSender, proposalInfo) {
548
+ try {
549
+ return await this.txProvider.getTxsForBlockProposal(proposal, blockNumber, {
550
+ pinnedPeer: proposalSender,
551
+ deadline: this.getReexecutionDeadline(proposal.slotNumber)
552
+ });
553
+ } catch (error) {
554
+ if (!isErrorClass(error, InvalidBlockProposalTxsError)) {
555
+ throw error;
556
+ }
557
+ this.log.warn(`Block proposal carries ${error.invalidTxs.length} invalid txs`, {
558
+ ...proposalInfo,
559
+ invalidTxs: error.invalidTxs.map(({ txHash, reasons })=>({
560
+ txHash: txHash.toString(),
561
+ reasons
562
+ }))
563
+ });
564
+ return 'invalid_embedded_txs';
565
+ }
566
+ }
567
+ async validateNewBlockInSlot(blockProposal) {
568
+ if (!this.p2pClient) {
569
+ return {
570
+ isValid: true
571
+ };
572
+ }
573
+ const { blockProposals, checkpointProposals } = await this.p2pClient.getProposalsForSlot(blockProposal.slotNumber);
574
+ if (checkpointProposals.length === 0) {
575
+ return {
576
+ isValid: true
577
+ };
578
+ } else if (checkpointProposals.length > 1) {
579
+ return {
580
+ isValid: false,
581
+ reason: 'checkpoint_proposal_equivocation'
582
+ };
583
+ } else {
584
+ const checkpointProposal = checkpointProposals[0];
585
+ const terminalBlock = blockProposals.find((block)=>block.archive.equals(checkpointProposal.archive));
586
+ return terminalBlock !== undefined && blockProposal.indexWithinCheckpoint > terminalBlock.indexWithinCheckpoint ? {
587
+ isValid: false,
588
+ reason: 'block_proposal_beyond_checkpoint'
589
+ } : {
590
+ isValid: true
591
+ };
592
+ }
593
+ }
594
+ async getParentBlock(proposal) {
595
+ const parentArchive = proposal.blockHeader.lastArchive.root;
596
+ const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
597
+ if (parentArchive.equals(genesisArchiveRoot)) {
598
+ return 'genesis';
599
+ }
600
+ const deadline = this.getReexecutionDeadline(proposal.slotNumber);
601
+ const timeoutDurationMs = deadline.getTime() - this.dateProvider.now();
602
+ try {
603
+ return await this.blockSource.getBlockData({
604
+ archive: parentArchive
605
+ }) ?? (timeoutDurationMs <= 0 ? undefined : await retryUntil(()=>this.blockSource.syncImmediate().then(()=>this.blockSource.getBlockData({
606
+ archive: parentArchive
607
+ })), 'force archiver sync', {
608
+ deadline,
609
+ dateProvider: this.dateProvider
610
+ }, 0.5));
611
+ } catch (err) {
612
+ if (err instanceof TimeoutError) {
613
+ this.log.debug(`Timed out getting parent block by archive root`, {
614
+ parentArchive
615
+ });
616
+ } else {
617
+ this.log.error('Error getting parent block by archive root', err, {
618
+ parentArchive
619
+ });
620
+ }
621
+ return undefined;
622
+ }
623
+ }
624
+ /**
625
+ * Resolves whether a block genuinely already exists at `blockNumber`. Returns the existing block only if
626
+ * it is a true duplicate of the proposal (matching archive). During a reorg the archiver can still hold a
627
+ * stale fork at this number (different archive) that is about to be pruned; in that case this forces L1
628
+ * sync and waits, bounded by the re-execution deadline, for the prune to land, then returns `undefined` so
629
+ * the rebuilt block proposal can be processed in time to attest. If the prune does not complete before the
630
+ * deadline it returns the stale block, so the caller falls back to the safe `block_number_already_exists`
631
+ * rejection.
632
+ */ async resolveExistingBlockAtNumber(blockNumber, proposalArchive, slotNumber) {
633
+ const existingBlock = await this.blockSource.getBlockData({
634
+ number: blockNumber
635
+ });
636
+ if (!existingBlock || existingBlock.archive.root.equals(proposalArchive)) {
637
+ return existingBlock;
638
+ }
639
+ // A different block already occupies this number: it may be a stale fork being pruned during a reorg, not a
640
+ // genuine duplicate. Wait for the local prune rather than permanently rejecting the proposal.
641
+ const deadline = this.getReexecutionDeadline(slotNumber);
642
+ if (deadline.getTime() - this.dateProvider.now() <= 0) {
643
+ return existingBlock;
644
+ }
645
+ this.log.warn(`Block number ${blockNumber} already exists, awaiting potential prune`, {
646
+ blockNumber,
647
+ existingArchive: existingBlock.archive.root.toString(),
648
+ proposalArchive: proposalArchive.toString()
649
+ });
650
+ try {
651
+ const { block } = await retryUntil(async ()=>{
652
+ await this.blockSource.syncImmediate();
653
+ const block = await this.blockSource.getBlockData({
654
+ number: blockNumber
655
+ });
656
+ // Resolve once the existing block is gone (pruned) or has been replaced by one matching the
657
+ // proposal — the same condition as the early return above. A matching block is returned so the
658
+ // caller still treats it as a genuine duplicate; an `undefined` (pruned) block lets the proposal
659
+ // be processed. Wrap in an object so the `undefined` case is still a truthy retry result.
660
+ return block === undefined || block.archive.root.equals(proposalArchive) ? {
661
+ block
662
+ } : undefined;
663
+ }, `prune of stale block ${blockNumber}`, {
664
+ deadline,
665
+ dateProvider: this.dateProvider
666
+ }, 0.5);
667
+ return block;
668
+ } catch (err) {
669
+ if (err instanceof TimeoutError) {
670
+ this.log.warn(`Timed out waiting for stale block ${blockNumber} to be pruned`, {
671
+ blockNumber
672
+ });
673
+ return existingBlock;
674
+ }
675
+ throw err;
676
+ }
677
+ }
678
+ computeCheckpointNumber(proposal, parentBlock, proposalInfo) {
679
+ if (parentBlock === 'genesis') {
680
+ // First block is in checkpoint 1
681
+ if (proposal.indexWithinCheckpoint !== 0) {
682
+ this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
683
+ return {
684
+ reason: 'invalid_proposal'
685
+ };
686
+ }
687
+ return {
688
+ checkpointNumber: CheckpointNumber.INITIAL
689
+ };
690
+ }
691
+ if (proposal.indexWithinCheckpoint === 0) {
692
+ // If this is the first block in a new checkpoint, increment the checkpoint number
693
+ if (!(proposal.blockHeader.getSlot() > parentBlock.header.getSlot())) {
694
+ this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
695
+ return {
696
+ reason: 'invalid_proposal'
697
+ };
698
+ }
699
+ return {
700
+ checkpointNumber: CheckpointNumber(parentBlock.checkpointNumber + 1)
701
+ };
702
+ }
703
+ // Otherwise it should follow the previous block in the same checkpoint
704
+ if (proposal.indexWithinCheckpoint !== parentBlock.indexWithinCheckpoint + 1) {
705
+ this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
706
+ return {
707
+ reason: 'invalid_proposal'
708
+ };
709
+ }
710
+ if (proposal.blockHeader.getSlot() !== parentBlock.header.getSlot()) {
711
+ this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
712
+ return {
713
+ reason: 'invalid_proposal'
714
+ };
715
+ }
716
+ // For non-first blocks in a checkpoint, validate global variables match parent (except blockNumber)
717
+ const validationResult = this.validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo);
718
+ if (validationResult) {
719
+ return validationResult;
720
+ }
721
+ return {
722
+ checkpointNumber: parentBlock.checkpointNumber
723
+ };
724
+ }
725
+ /**
726
+ * Validates that a non-first block in a checkpoint has consistent global variables with its parent.
727
+ * For blocks with indexWithinCheckpoint > 0, all global variables except blockNumber must match the parent.
728
+ * @returns A failure result if validation fails, undefined if validation passes
729
+ */ validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo) {
730
+ const proposalGlobals = proposal.blockHeader.globalVariables;
731
+ const parentGlobals = parentBlock.header.globalVariables;
732
+ // All global variables except blockNumber should match the parent
733
+ // blockNumber naturally increments between blocks
734
+ if (!proposalGlobals.chainId.equals(parentGlobals.chainId)) {
735
+ this.log.warn(`Non-first block in checkpoint has mismatched chainId`, {
736
+ ...proposalInfo,
737
+ proposalChainId: proposalGlobals.chainId.toString(),
738
+ parentChainId: parentGlobals.chainId.toString()
739
+ });
740
+ return {
741
+ reason: 'global_variables_mismatch'
742
+ };
743
+ }
744
+ if (!proposalGlobals.version.equals(parentGlobals.version)) {
745
+ this.log.warn(`Non-first block in checkpoint has mismatched version`, {
746
+ ...proposalInfo,
747
+ proposalVersion: proposalGlobals.version.toString(),
748
+ parentVersion: parentGlobals.version.toString()
749
+ });
750
+ return {
751
+ reason: 'global_variables_mismatch'
752
+ };
753
+ }
754
+ if (proposalGlobals.slotNumber !== parentGlobals.slotNumber) {
755
+ this.log.warn(`Non-first block in checkpoint has mismatched slotNumber`, {
756
+ ...proposalInfo,
757
+ proposalSlotNumber: proposalGlobals.slotNumber,
758
+ parentSlotNumber: parentGlobals.slotNumber
759
+ });
760
+ return {
761
+ reason: 'global_variables_mismatch'
762
+ };
763
+ }
764
+ if (proposalGlobals.timestamp !== parentGlobals.timestamp) {
765
+ this.log.warn(`Non-first block in checkpoint has mismatched timestamp`, {
766
+ ...proposalInfo,
767
+ proposalTimestamp: proposalGlobals.timestamp.toString(),
768
+ parentTimestamp: parentGlobals.timestamp.toString()
769
+ });
770
+ return {
771
+ reason: 'global_variables_mismatch'
772
+ };
773
+ }
774
+ if (!proposalGlobals.coinbase.equals(parentGlobals.coinbase)) {
775
+ this.log.warn(`Non-first block in checkpoint has mismatched coinbase`, {
776
+ ...proposalInfo,
777
+ proposalCoinbase: proposalGlobals.coinbase.toString(),
778
+ parentCoinbase: parentGlobals.coinbase.toString()
779
+ });
780
+ return {
781
+ reason: 'global_variables_mismatch'
782
+ };
783
+ }
784
+ if (!proposalGlobals.feeRecipient.equals(parentGlobals.feeRecipient)) {
785
+ this.log.warn(`Non-first block in checkpoint has mismatched feeRecipient`, {
786
+ ...proposalInfo,
787
+ proposalFeeRecipient: proposalGlobals.feeRecipient.toString(),
788
+ parentFeeRecipient: parentGlobals.feeRecipient.toString()
789
+ });
790
+ return {
791
+ reason: 'global_variables_mismatch'
792
+ };
793
+ }
794
+ if (!proposalGlobals.gasFees.equals(parentGlobals.gasFees)) {
795
+ this.log.warn(`Non-first block in checkpoint has mismatched gasFees`, {
796
+ ...proposalInfo,
797
+ proposalGasFees: proposalGlobals.gasFees.toInspect(),
798
+ parentGasFees: parentGlobals.gasFees.toInspect()
799
+ });
800
+ return {
801
+ reason: 'global_variables_mismatch'
802
+ };
803
+ }
804
+ return undefined;
805
+ }
806
+ /**
807
+ * Hard re-execution/validation deadline for any block or checkpoint proposal targeting `slotNumber`:
808
+ * the single consensus `attestation_deadline` (`target_slot_start + S - 2E`). This is the latest the
809
+ * checkpoint can land on L1 in the target slot; all nodes agree on it. Loosened from the previous
810
+ * next-wall-clock-slot-boundary bound (see the timetable spec / refactor notes).
811
+ */ getReexecutionDeadline(slotNumber) {
812
+ return new Date(this.timetable.getAttestationDeadline(slotNumber) * 1000);
813
+ }
814
+ getReexecuteFailureReason(err) {
815
+ if (err instanceof TransactionsNotAvailableError) {
816
+ return 'txs_not_available';
817
+ } else if (err instanceof ReExInitialStateMismatchError) {
818
+ return 'initial_state_mismatch';
819
+ } else if (err instanceof ReExStateMismatchError) {
820
+ return 'state_mismatch';
821
+ } else if (err instanceof ReExFailedTxsError) {
822
+ return 'failed_txs';
823
+ } else if (err instanceof ReExTimeoutError) {
824
+ return 'timeout';
825
+ } else {
826
+ return 'unknown_error';
827
+ }
828
+ }
829
+ async reexecuteTransactions(proposal, blockNumber, checkpointNumber, txs, l1ToL2Messages, previousCheckpointOutHashes) {
830
+ const env = {
831
+ stack: [],
832
+ error: void 0,
833
+ hasError: false
834
+ };
835
+ try {
836
+ const { blockHeader, txHashes } = proposal;
837
+ // If we do not have all of the transactions, then we should fail
838
+ if (txs.length !== txHashes.length) {
839
+ const foundTxHashes = txs.map((tx)=>tx.getTxHash());
840
+ const missingTxHashes = txHashes.filter((txHash)=>!foundTxHashes.some((h)=>h.equals(txHash)));
841
+ throw new TransactionsNotAvailableError(missingTxHashes);
842
+ }
843
+ const timer = new Timer();
844
+ const slot = proposal.slotNumber;
845
+ const config = this.checkpointsBuilder.getConfig();
846
+ // Get prior blocks in this checkpoint (same slot before current block)
847
+ const allBlocksInSlot = await this.blockSource.getBlocksForSlot(slot);
848
+ const priorBlocks = allBlocksInSlot.filter((b)=>b.number < blockNumber && b.header.getSlot() === slot);
849
+ // Fork before the block to be built
850
+ const parentBlockNumber = BlockNumber(blockNumber - 1);
851
+ await this.worldState.syncImmediate(parentBlockNumber);
852
+ const fork = _ts_add_disposable_resource(env, await this.worldState.fork(parentBlockNumber), true);
853
+ // Verify the fork's archive root matches the proposal's expected last archive.
854
+ // If they don't match, our world state synced to a different chain and reexecution would fail.
855
+ const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
856
+ if (!forkArchiveRoot.equals(proposal.blockHeader.lastArchive.root)) {
857
+ throw new ReExInitialStateMismatchError(proposal.blockHeader.lastArchive.root, forkArchiveRoot);
858
+ }
859
+ // Build checkpoint constants from proposal (excludes blockNumber which is per-block)
860
+ const constants = {
861
+ chainId: new Fr(config.l1ChainId),
862
+ version: new Fr(config.rollupVersion),
863
+ slotNumber: slot,
864
+ timestamp: blockHeader.globalVariables.timestamp,
865
+ coinbase: blockHeader.globalVariables.coinbase,
866
+ feeRecipient: blockHeader.globalVariables.feeRecipient,
867
+ gasFees: blockHeader.globalVariables.gasFees
868
+ };
869
+ // Create checkpoint builder with prior blocks
870
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, 0n, l1ToL2Messages, previousCheckpointOutHashes, fork, priorBlocks, this.log.getBindings());
871
+ // Build the new block
872
+ const deadline = this.getReexecutionDeadline(slot);
873
+ const maxBlockGas = this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined ? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity) : undefined;
874
+ const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
875
+ isBuildingProposal: false,
876
+ minValidTxs: 0,
877
+ deadline,
878
+ expectedEndState: blockHeader.state,
879
+ maxTransactions: this.config.validateMaxTxsPerBlock,
880
+ maxBlockGas
881
+ });
882
+ const { block, failedTxs } = result;
883
+ const numFailedTxs = failedTxs.length;
884
+ this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
885
+ numFailedTxs,
886
+ numProposalTxs: txHashes.length,
887
+ numProcessedTxs: block.body.txEffects.length,
888
+ blockNumber,
889
+ slot
890
+ });
891
+ if (numFailedTxs > 0) {
892
+ this.metrics?.recordFailedReexecution(proposal);
893
+ throw new ReExFailedTxsError(numFailedTxs);
894
+ }
895
+ if (block.body.txEffects.length !== txHashes.length) {
896
+ this.metrics?.recordFailedReexecution(proposal);
897
+ throw new ReExTimeoutError();
898
+ }
899
+ // Throw a ReExStateMismatchError error if state updates do not match
900
+ // Compare the full block structure (archive and header) from the built block with the proposal
901
+ const archiveMatches = proposal.archive.equals(block.archive.root);
902
+ const headerMatches = proposal.blockHeader.equals(block.header);
903
+ if (!archiveMatches || !headerMatches) {
904
+ this.log.warn(`Re-execution state mismatch for slot ${slot}`, {
905
+ expectedArchive: block.archive.root.toString(),
906
+ actualArchive: proposal.archive.toString(),
907
+ expectedHeader: block.header.toInspect(),
908
+ actualHeader: proposal.blockHeader.toInspect()
909
+ });
910
+ this.metrics?.recordFailedReexecution(proposal);
911
+ throw new ReExStateMismatchError(proposal.archive, block.archive.root);
912
+ }
913
+ const reexecutionTimeMs = timer.ms();
914
+ const totalManaUsed = block.header.totalManaUsed.toNumber() / 1e6;
915
+ this.metrics?.recordReex(reexecutionTimeMs, txs.length, totalManaUsed);
916
+ return {
917
+ block,
918
+ failedTxs,
919
+ reexecutionTimeMs,
920
+ totalManaUsed
921
+ };
922
+ } catch (e) {
923
+ env.error = e;
924
+ env.hasError = true;
925
+ } finally{
926
+ const result = _ts_dispose_resources(env);
927
+ if (result) await result;
928
+ }
929
+ }
930
+ /**
931
+ * Validates a checkpoint proposal, caches the result, and uploads blobs if configured.
932
+ * Returns a cached result if the same proposal (archive + slot) was already validated.
933
+ * Used by both the all-nodes callback (via register) and the validator client (via delegation).
934
+ * Expects the proposal to have already passed p2p ingress validation (expected proposer and receive-window
935
+ * timeliness); only deterministic properties of the signed payload are checked here.
936
+ */ async handleCheckpointProposal(proposal, proposalInfo) {
937
+ const slot = proposal.slotNumber;
938
+ const payloadHash = proposal.getPayloadHash();
939
+ // Check cache: same signed-payload hash means we already validated this exact proposal.
940
+ if (this.lastCheckpointValidationResult && this.lastCheckpointValidationResult.payloadHash === payloadHash) {
941
+ this.log.debug(`Returning cached validation result for checkpoint proposal at slot ${slot}`, proposalInfo);
942
+ return this.lastCheckpointValidationResult.result;
943
+ }
944
+ const proposer = proposal.getSender();
945
+ let result;
946
+ if (!proposer) {
947
+ this.log.warn(`Received checkpoint proposal with invalid signature for slot ${proposal.slotNumber}`);
948
+ result = {
949
+ isValid: false,
950
+ reason: 'invalid_signature'
951
+ };
952
+ } else if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
953
+ this.log.warn(`Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${proposal.slotNumber}`);
954
+ result = {
955
+ isValid: false,
956
+ reason: 'invalid_fee_asset_price_modifier'
957
+ };
958
+ } else {
959
+ result = await this.validateCheckpointProposal(proposal, proposalInfo);
960
+ }
961
+ this.lastCheckpointValidationResult = {
962
+ payloadHash,
963
+ result
964
+ };
965
+ // Record the outcome on the re-execution tracker.
966
+ const outcome = result.isValid ? 'valid' : CHECKPOINT_VALIDATION_REASON_TO_OUTCOME[result.reason];
967
+ if (outcome !== undefined) {
968
+ this.reexecutionTracker.recordOutcome(slot, proposal.archive, outcome, result.checkpointNumber);
969
+ }
970
+ // Drop tracker entries for checkpoints that have reached L1 finality.
971
+ try {
972
+ const tips = await this.blockSource.getL2Tips();
973
+ const finalizedCheckpointNumber = tips.finalized.checkpoint.number;
974
+ if (finalizedCheckpointNumber > 0) {
975
+ this.reexecutionTracker.removeBefore(CheckpointNumber(finalizedCheckpointNumber + 1));
976
+ }
977
+ } catch (err) {
978
+ this.log.error(`Error pruning reexecution tracker`, err, proposalInfo);
979
+ }
980
+ // Upload blobs to filestore if validation passed (fire and forget)
981
+ if (result.isValid) {
982
+ this.tryUploadBlobsForCheckpoint(proposal, proposalInfo);
983
+ }
984
+ return result;
985
+ }
986
+ /**
987
+ * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
988
+ * @returns Validation result with isValid flag and reason if invalid.
989
+ */ async validateCheckpointProposal(proposal, proposalInfo) {
990
+ const env = {
991
+ stack: [],
992
+ error: void 0,
993
+ hasError: false
994
+ };
995
+ try {
996
+ const slot = proposal.slotNumber;
997
+ // Block-sync/validation deadline = the single consensus attestation_deadline (target_slot_start + S
998
+ // - 2E): the latest moment the proposer can submit this checkpoint and still have it land on L1 in
999
+ // the target slot. Keeping validation/attestation alive until then lets validators keep attesting
1000
+ // right up to the proposer's real publish cutoff.
1001
+ const deadline = this.getReexecutionDeadline(slot);
1002
+ // Wait for last block to sync by archive. The deadline is passed to retryUntil as an absolute date so
1003
+ // the remaining budget is derived from the date provider; a deadline already in the past times out
1004
+ // after a single attempt instead of looping (the immediate-timeout semantics of the deadline overload).
1005
+ let lastBlockData;
1006
+ try {
1007
+ lastBlockData = await retryUntil(async ()=>{
1008
+ await this.blockSource.syncImmediate();
1009
+ return await this.blockSource.getBlockData({
1010
+ archive: proposal.archive
1011
+ });
1012
+ }, `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`, {
1013
+ deadline,
1014
+ dateProvider: this.dateProvider
1015
+ }, 0.5);
1016
+ } catch (err) {
1017
+ if (err instanceof TimeoutError) {
1018
+ this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
1019
+ return {
1020
+ isValid: false,
1021
+ reason: 'last_block_not_found'
1022
+ };
1023
+ }
1024
+ this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
1025
+ return {
1026
+ isValid: false,
1027
+ reason: 'block_fetch_error'
1028
+ };
1029
+ }
1030
+ if (!lastBlockData) {
1031
+ this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
1032
+ return {
1033
+ isValid: false,
1034
+ reason: 'last_block_not_found'
1035
+ };
1036
+ }
1037
+ // Refuse to attest if the block's enclosing checkpoint has already been published to L1.
1038
+ const existingCheckpoint = await this.blockSource.getCheckpointData({
1039
+ number: lastBlockData.checkpointNumber
1040
+ });
1041
+ if (existingCheckpoint) {
1042
+ this.log.warn(`Refusing to attest to checkpoint proposal whose checkpoint is already on L1`, {
1043
+ ...proposalInfo,
1044
+ checkpointNumber: lastBlockData.checkpointNumber
1045
+ });
1046
+ return {
1047
+ isValid: false,
1048
+ reason: 'checkpoint_already_published',
1049
+ checkpointNumber: lastBlockData.checkpointNumber
1050
+ };
1051
+ }
1052
+ // Get all full blocks for the slot and checkpoint
1053
+ const blocks = await this.blockSource.getBlocksForSlot(slot);
1054
+ if (blocks.length === 0) {
1055
+ this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
1056
+ return {
1057
+ isValid: false,
1058
+ reason: 'no_blocks_for_slot',
1059
+ checkpointNumber: lastBlockData.checkpointNumber
1060
+ };
1061
+ }
1062
+ // Ensure the last block for this slot matches the archive in the checkpoint proposal
1063
+ if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
1064
+ this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
1065
+ return {
1066
+ isValid: false,
1067
+ reason: 'last_block_archive_mismatch',
1068
+ checkpointNumber: lastBlockData.checkpointNumber
1069
+ };
1070
+ }
1071
+ // Note this condition should never trigger, since we dont process block proposals that exceed indexWithinCheckpoint
1072
+ const maxBlocksPerCheckpoint = this.config.maxBlocksPerCheckpoint;
1073
+ if (maxBlocksPerCheckpoint !== undefined && blocks.length > maxBlocksPerCheckpoint) {
1074
+ this.log.warn(`Checkpoint proposal exceeds maxBlocksPerCheckpoint`, {
1075
+ ...proposalInfo,
1076
+ blocksInProposal: blocks.length,
1077
+ maxBlocksPerCheckpoint
1078
+ });
1079
+ return {
1080
+ isValid: false,
1081
+ reason: 'too_many_blocks_in_checkpoint',
1082
+ checkpointNumber: lastBlockData.checkpointNumber
1083
+ };
1084
+ }
1085
+ this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
1086
+ ...proposalInfo,
1087
+ blockNumbers: blocks.map((b)=>b.number)
1088
+ });
1089
+ // Get checkpoint constants from first block
1090
+ const firstBlock = blocks[0];
1091
+ const constants = this.extractCheckpointConstants(firstBlock);
1092
+ const checkpointNumber = firstBlock.checkpointNumber;
1093
+ // Get L1-to-L2 messages for this checkpoint
1094
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
1095
+ // Collect the out hashes of all the checkpoints before this one in the same epoch.
1096
+ // See note on the analogous block-proposal site: the helper handles pipelining lag.
1097
+ const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
1098
+ const previousCheckpointOutHashes = await getPreviousCheckpointOutHashes({
1099
+ blockSource: this.blockSource,
1100
+ epoch,
1101
+ checkpointNumber,
1102
+ l1Constants: this.epochCache.getL1Constants(),
1103
+ pipeliningEnabled: true,
1104
+ log: this.log
1105
+ });
1106
+ // Fork world state at the block before the first block. getFork syncs world state to the parent block
1107
+ // first (see its doc): the block source (archiver) can already hold the block while world state still
1108
+ // trails it by one, and forking a not-yet-applied block throws a raw tree error that would otherwise
1109
+ // escape as an uncaught gossipsub error. We pass the parent's expected block hash so the sync detects a
1110
+ // world-state reorg (undefined for the genesis parent, where no block exists to pin). On failure we map
1111
+ // to a clean validation result rather than letting it escape.
1112
+ const parentBlockNumber = BlockNumber(firstBlock.number - 1);
1113
+ let forkResult;
1114
+ try {
1115
+ const parentBlockHash = (await this.blockSource.getBlockData({
1116
+ number: parentBlockNumber
1117
+ }))?.blockHash;
1118
+ forkResult = await this.checkpointsBuilder.getFork(parentBlockNumber, parentBlockHash);
1119
+ } catch (err) {
1120
+ this.log.warn(`Failed to fork world state at block ${parentBlockNumber} for checkpoint proposal`, {
1121
+ ...proposalInfo,
1122
+ parentBlockNumber,
1123
+ err
1124
+ });
1125
+ return {
1126
+ isValid: false,
1127
+ reason: 'world_state_not_synced',
1128
+ checkpointNumber
1129
+ };
1130
+ }
1131
+ const fork = _ts_add_disposable_resource(env, forkResult, true);
1132
+ // Verify the fork's archive root matches the checkpoint's expected starting archive (the archive after
1133
+ // the parent block). A mismatch means world state forked from a different chain than the proposal was
1134
+ // built on (e.g. a reorg), so recomputing the checkpoint against it would be meaningless. This mirrors
1135
+ // the block-proposal re-execution check and fails fast with a clean, non-slashable result instead of a
1136
+ // confusing downstream mismatch.
1137
+ const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
1138
+ if (!forkArchiveRoot.equals(proposal.checkpointHeader.lastArchiveRoot)) {
1139
+ this.log.warn(`Fork archive root does not match checkpoint proposal's last archive`, {
1140
+ ...proposalInfo,
1141
+ forkArchiveRoot: forkArchiveRoot.toString(),
1142
+ expectedLastArchiveRoot: proposal.checkpointHeader.lastArchiveRoot.toString()
1143
+ });
1144
+ return {
1145
+ isValid: false,
1146
+ reason: 'initial_archive_mismatch',
1147
+ checkpointNumber
1148
+ };
1149
+ }
1150
+ // Create checkpoint builder with all existing blocks
1151
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, proposal.feeAssetPriceModifier, l1ToL2Messages, previousCheckpointOutHashes, fork, blocks, this.log.getBindings());
1152
+ // Complete the checkpoint to get computed values
1153
+ const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
1154
+ // Compare checkpoint header with proposal
1155
+ if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
1156
+ this.log.warn(`Checkpoint header mismatch`, {
1157
+ ...proposalInfo,
1158
+ computed: computedCheckpoint.header.toInspect(),
1159
+ proposal: proposal.checkpointHeader.toInspect()
1160
+ });
1161
+ return {
1162
+ isValid: false,
1163
+ reason: 'checkpoint_header_mismatch',
1164
+ checkpointNumber
1165
+ };
1166
+ }
1167
+ // Compare archive root with proposal
1168
+ if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
1169
+ this.log.warn(`Archive root mismatch`, {
1170
+ ...proposalInfo,
1171
+ computed: computedCheckpoint.archive.root.toString(),
1172
+ proposal: proposal.archive.toString()
1173
+ });
1174
+ return {
1175
+ isValid: false,
1176
+ reason: 'archive_mismatch',
1177
+ checkpointNumber
1178
+ };
1179
+ }
1180
+ // Check that the accumulated epoch out hash matches the value in the proposal.
1181
+ // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
1182
+ const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
1183
+ const computedEpochOutHash = accumulateCheckpointOutHashes([
1184
+ ...previousCheckpointOutHashes,
1185
+ checkpointOutHash
1186
+ ]);
1187
+ const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
1188
+ if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
1189
+ this.log.warn(`Epoch out hash mismatch`, {
1190
+ proposalEpochOutHash: proposalEpochOutHash.toString(),
1191
+ computedEpochOutHash: computedEpochOutHash.toString(),
1192
+ checkpointOutHash: checkpointOutHash.toString(),
1193
+ previousCheckpointOutHashes: previousCheckpointOutHashes.map((h)=>h.toString()),
1194
+ ...proposalInfo
1195
+ });
1196
+ return {
1197
+ isValid: false,
1198
+ reason: 'out_hash_mismatch',
1199
+ checkpointNumber
1200
+ };
1201
+ }
1202
+ // Final round of validations on the checkpoint, just in case.
1203
+ try {
1204
+ validateCheckpoint(computedCheckpoint, {
1205
+ rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
1206
+ maxDABlockGas: this.config.validateMaxDABlockGas,
1207
+ maxL2BlockGas: this.config.validateMaxL2BlockGas,
1208
+ maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
1209
+ maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint
1210
+ });
1211
+ } catch (err) {
1212
+ this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
1213
+ return {
1214
+ isValid: false,
1215
+ reason: 'checkpoint_validation_failed',
1216
+ checkpointNumber
1217
+ };
1218
+ }
1219
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
1220
+ return {
1221
+ isValid: true,
1222
+ checkpointNumber
1223
+ };
1224
+ } catch (e) {
1225
+ env.error = e;
1226
+ env.hasError = true;
1227
+ } finally{
1228
+ const result = _ts_dispose_resources(env);
1229
+ if (result) await result;
1230
+ }
1231
+ }
1232
+ /** Extracts checkpoint global variables from a block. */ extractCheckpointConstants(block) {
1233
+ const gv = block.header.globalVariables;
1234
+ return {
1235
+ chainId: gv.chainId,
1236
+ version: gv.version,
1237
+ slotNumber: gv.slotNumber,
1238
+ timestamp: gv.timestamp,
1239
+ coinbase: gv.coinbase,
1240
+ feeRecipient: gv.feeRecipient,
1241
+ gasFees: gv.gasFees
1242
+ };
1243
+ }
1244
+ /** Triggers blob upload for a checkpoint if the blob client can upload (fire and forget). */ tryUploadBlobsForCheckpoint(proposal, proposalInfo) {
1245
+ if (this.blobClient.canUpload()) {
1246
+ void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
1247
+ }
1248
+ }
1249
+ /** Uploads blobs for a checkpoint to the filestore. */ async uploadBlobsForCheckpoint(proposal, proposalInfo) {
1250
+ try {
1251
+ const lastBlockHeader = (await this.blockSource.getBlockData({
1252
+ archive: proposal.archive
1253
+ }))?.header;
1254
+ if (!lastBlockHeader) {
1255
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
1256
+ return;
1257
+ }
1258
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
1259
+ if (blocks.length === 0) {
1260
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
1261
+ return;
1262
+ }
1263
+ const blockBlobData = blocks.map((b)=>b.toBlockBlobData());
1264
+ const blobFields = encodeCheckpointBlobDataFromBlocks(blockBlobData);
1265
+ const blobs = await getBlobsPerL1Block(blobFields);
1266
+ await this.blobClient.sendBlobsToFilestore(blobs);
1267
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
1268
+ ...proposalInfo,
1269
+ numBlobs: blobs.length
1270
+ });
1271
+ } catch (err) {
1272
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
1273
+ }
1274
+ }
1275
+ /**
1276
+ * Derives proposed checkpoint data from validated blocks and sets it on the archiver, so this node can
1277
+ * pipeline building on top of the checkpoint. Does not retry, since validation already waited for the
1278
+ * last block to sync.
1279
+ */ async setProposedCheckpoint(proposal) {
1280
+ if (!this.archiver) {
1281
+ return false;
1282
+ }
1283
+ const blockData = await this.blockSource.getBlockData({
1284
+ archive: proposal.archive
1285
+ });
1286
+ if (!blockData) {
1287
+ this.log.debug(`Block data not found for checkpoint proposal archive, cannot set proposed checkpoint`, {
1288
+ archive: proposal.archive.toString()
1289
+ });
1290
+ return false;
1291
+ }
1292
+ await this.archiver.addProposedCheckpoint({
1293
+ header: proposal.checkpointHeader,
1294
+ checkpointNumber: blockData.checkpointNumber,
1295
+ startBlock: BlockNumber(blockData.header.getBlockNumber() - blockData.indexWithinCheckpoint),
1296
+ blockCount: blockData.indexWithinCheckpoint + 1,
1297
+ totalManaUsed: proposal.checkpointHeader.totalManaUsed.toBigInt(),
1298
+ feeAssetPriceModifier: proposal.feeAssetPriceModifier
1299
+ });
1300
+ return true;
1301
+ }
1302
+ }