@aztec/validator-client 0.0.1-commit.dbf9cec → 0.0.1-commit.df81a97b5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/validator-client",
3
- "version": "0.0.1-commit.dbf9cec",
3
+ "version": "0.0.1-commit.df81a97b5",
4
4
  "main": "dest/index.js",
5
5
  "type": "module",
6
6
  "exports": {
@@ -64,30 +64,30 @@
64
64
  ]
65
65
  },
66
66
  "dependencies": {
67
- "@aztec/blob-client": "0.0.1-commit.dbf9cec",
68
- "@aztec/blob-lib": "0.0.1-commit.dbf9cec",
69
- "@aztec/constants": "0.0.1-commit.dbf9cec",
70
- "@aztec/epoch-cache": "0.0.1-commit.dbf9cec",
71
- "@aztec/ethereum": "0.0.1-commit.dbf9cec",
72
- "@aztec/foundation": "0.0.1-commit.dbf9cec",
73
- "@aztec/node-keystore": "0.0.1-commit.dbf9cec",
74
- "@aztec/noir-protocol-circuits-types": "0.0.1-commit.dbf9cec",
75
- "@aztec/p2p": "0.0.1-commit.dbf9cec",
76
- "@aztec/protocol-contracts": "0.0.1-commit.dbf9cec",
77
- "@aztec/prover-client": "0.0.1-commit.dbf9cec",
78
- "@aztec/simulator": "0.0.1-commit.dbf9cec",
79
- "@aztec/slasher": "0.0.1-commit.dbf9cec",
80
- "@aztec/stdlib": "0.0.1-commit.dbf9cec",
81
- "@aztec/telemetry-client": "0.0.1-commit.dbf9cec",
82
- "@aztec/validator-ha-signer": "0.0.1-commit.dbf9cec",
67
+ "@aztec/blob-client": "0.0.1-commit.df81a97b5",
68
+ "@aztec/blob-lib": "0.0.1-commit.df81a97b5",
69
+ "@aztec/constants": "0.0.1-commit.df81a97b5",
70
+ "@aztec/epoch-cache": "0.0.1-commit.df81a97b5",
71
+ "@aztec/ethereum": "0.0.1-commit.df81a97b5",
72
+ "@aztec/foundation": "0.0.1-commit.df81a97b5",
73
+ "@aztec/node-keystore": "0.0.1-commit.df81a97b5",
74
+ "@aztec/noir-protocol-circuits-types": "0.0.1-commit.df81a97b5",
75
+ "@aztec/p2p": "0.0.1-commit.df81a97b5",
76
+ "@aztec/protocol-contracts": "0.0.1-commit.df81a97b5",
77
+ "@aztec/prover-client": "0.0.1-commit.df81a97b5",
78
+ "@aztec/simulator": "0.0.1-commit.df81a97b5",
79
+ "@aztec/slasher": "0.0.1-commit.df81a97b5",
80
+ "@aztec/stdlib": "0.0.1-commit.df81a97b5",
81
+ "@aztec/telemetry-client": "0.0.1-commit.df81a97b5",
82
+ "@aztec/validator-ha-signer": "0.0.1-commit.df81a97b5",
83
83
  "koa": "^2.16.1",
84
84
  "koa-router": "^13.1.1",
85
85
  "tslib": "^2.4.0",
86
86
  "viem": "npm:@aztec/viem@2.38.2"
87
87
  },
88
88
  "devDependencies": {
89
- "@aztec/archiver": "0.0.1-commit.dbf9cec",
90
- "@aztec/world-state": "0.0.1-commit.dbf9cec",
89
+ "@aztec/archiver": "0.0.1-commit.df81a97b5",
90
+ "@aztec/world-state": "0.0.1-commit.df81a97b5",
91
91
  "@electric-sql/pglite": "^0.3.14",
92
92
  "@jest/globals": "^30.0.0",
93
93
  "@types/jest": "^30.0.0",
@@ -1,6 +1,7 @@
1
1
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
2
  import type { EpochCache } from '@aztec/epoch-cache';
3
3
  import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
+ import { pick } from '@aztec/foundation/collection';
4
5
  import { Fr } from '@aztec/foundation/curves/bn254';
5
6
  import { TimeoutError } from '@aztec/foundation/error';
6
7
  import { createLogger } from '@aztec/foundation/log';
@@ -10,12 +11,15 @@ import type { P2P, PeerId } from '@aztec/p2p';
10
11
  import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
11
12
  import type { BlockData, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
12
13
  import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
14
+ import { Gas } from '@aztec/stdlib/gas';
13
15
  import type { ITxProvider, ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
14
16
  import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
15
17
  import type { BlockProposal } from '@aztec/stdlib/p2p';
18
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
16
19
  import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
17
20
  import {
18
21
  ReExFailedTxsError,
22
+ ReExInitialStateMismatchError,
19
23
  ReExStateMismatchError,
20
24
  ReExTimeoutError,
21
25
  TransactionsNotAvailableError,
@@ -28,6 +32,7 @@ import type { ValidatorMetrics } from './metrics.js';
28
32
  export type BlockProposalValidationFailureReason =
29
33
  | 'invalid_proposal'
30
34
  | 'parent_block_not_found'
35
+ | 'block_source_not_synced'
31
36
  | 'parent_block_wrong_slot'
32
37
  | 'in_hash_mismatch'
33
38
  | 'global_variables_mismatch'
@@ -35,6 +40,7 @@ export type BlockProposalValidationFailureReason =
35
40
  | 'txs_not_available'
36
41
  | 'state_mismatch'
37
42
  | 'failed_txs'
43
+ | 'initial_state_mismatch'
38
44
  | 'timeout'
39
45
  | 'unknown_error';
40
46
 
@@ -87,25 +93,28 @@ export class BlockProposalHandler {
87
93
  this.tracer = telemetry.getTracer('BlockProposalHandler');
88
94
  }
89
95
 
90
- registerForReexecution(p2pClient: P2P): BlockProposalHandler {
91
- // Non-validator handler that re-executes for monitoring but does not attest.
96
+ register(p2pClient: P2P, shouldReexecute: boolean): BlockProposalHandler {
97
+ // Non-validator handler that processes or re-executes for monitoring but does not attest.
92
98
  // Returns boolean indicating whether the proposal was valid.
93
99
  const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
94
100
  try {
95
- const result = await this.handleBlockProposal(proposal, proposalSender, true);
101
+ const { slotNumber, blockNumber } = proposal;
102
+ const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
96
103
  if (result.isValid) {
97
- this.log.info(`Non-validator reexecution completed for slot ${proposal.slotNumber}`, {
104
+ this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
98
105
  blockNumber: result.blockNumber,
106
+ slotNumber,
99
107
  reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
100
108
  totalManaUsed: result.reexecutionResult?.totalManaUsed,
101
109
  numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
110
+ reexecuted: shouldReexecute,
102
111
  });
103
112
  return true;
104
113
  } else {
105
- this.log.warn(`Non-validator reexecution failed for slot ${proposal.slotNumber}`, {
106
- blockNumber: result.blockNumber,
107
- reason: result.reason,
108
- });
114
+ this.log.warn(
115
+ `Non-validator block proposal ${blockNumber} at slot ${slotNumber} failed processing with ${result.reason}`,
116
+ { blockNumber: result.blockNumber, slotNumber, reason: result.reason },
117
+ );
109
118
  return false;
110
119
  }
111
120
  } catch (error) {
@@ -133,7 +142,13 @@ export class BlockProposalHandler {
133
142
  return { isValid: false, reason: 'invalid_proposal' };
134
143
  }
135
144
 
136
- const proposalInfo = { ...proposal.toBlockInfo(), proposer: proposer.toString() };
145
+ const proposalInfo = {
146
+ ...proposal.toBlockInfo(),
147
+ proposer: proposer.toString(),
148
+ blockNumber: undefined as BlockNumber | undefined,
149
+ checkpointNumber: undefined as CheckpointNumber | undefined,
150
+ };
151
+
137
152
  this.log.info(`Processing proposal for slot ${slotNumber}`, {
138
153
  ...proposalInfo,
139
154
  txHashes: proposal.txHashes.map(t => t.toString()),
@@ -147,7 +162,24 @@ export class BlockProposalHandler {
147
162
  return { isValid: false, reason: 'invalid_proposal' };
148
163
  }
149
164
 
150
- // Check that the parent proposal is a block we know, otherwise reexecution would fail
165
+ // Ensure the block source is synced before checking for existing blocks,
166
+ // since a pending checkpoint prune may remove blocks we'd otherwise find.
167
+ // This affects mostly the block_number_already_exists check, since a pending
168
+ // checkpoint prune could remove a block that would conflict with this proposal.
169
+ // When pipelining is enabled, the proposer builds ahead of L1 submission, so the
170
+ // block source won't have synced to the proposed slot yet. Skip the sync wait to
171
+ // avoid eating into the attestation window.
172
+ if (!this.epochCache.isProposerPipeliningEnabled()) {
173
+ const blockSourceSync = await this.waitForBlockSourceSync(slotNumber);
174
+ if (!blockSourceSync) {
175
+ this.log.warn(`Block source is not synced, skipping processing`, proposalInfo);
176
+ return { isValid: false, reason: 'block_source_not_synced' };
177
+ }
178
+ }
179
+
180
+ // Check that the parent proposal is a block we know, otherwise reexecution would fail.
181
+ // If we don't find it immediately, we keep retrying for a while; it may be we still
182
+ // need to process other block proposals to get to it.
151
183
  const parentBlock = await this.getParentBlock(proposal);
152
184
  if (parentBlock === undefined) {
153
185
  this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
@@ -169,6 +201,7 @@ export class BlockProposalHandler {
169
201
  parentBlock === 'genesis'
170
202
  ? BlockNumber(INITIAL_L2_BLOCK_NUM)
171
203
  : BlockNumber(parentBlock.header.getBlockNumber() + 1);
204
+ proposalInfo.blockNumber = blockNumber;
172
205
 
173
206
  // Check that this block number does not exist already
174
207
  const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
@@ -184,12 +217,22 @@ export class BlockProposalHandler {
184
217
  deadline: this.getReexecutionDeadline(slotNumber, config),
185
218
  });
186
219
 
220
+ // If reexecution is disabled, bail. We were just interested in triggering tx collection.
221
+ if (!shouldReexecute) {
222
+ this.log.info(
223
+ `Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
224
+ proposalInfo,
225
+ );
226
+ return { isValid: true, blockNumber };
227
+ }
228
+
187
229
  // Compute the checkpoint number for this block and validate checkpoint consistency
188
230
  const checkpointResult = this.computeCheckpointNumber(proposal, parentBlock, proposalInfo);
189
231
  if (checkpointResult.reason) {
190
232
  return { isValid: false, blockNumber, reason: checkpointResult.reason };
191
233
  }
192
234
  const checkpointNumber = checkpointResult.checkpointNumber;
235
+ proposalInfo.checkpointNumber = checkpointNumber;
193
236
 
194
237
  // Check that I have the same set of l1ToL2Messages as the proposal
195
238
  const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
@@ -210,30 +253,28 @@ export class BlockProposalHandler {
210
253
  return { isValid: false, blockNumber, reason: 'txs_not_available' };
211
254
  }
212
255
 
256
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
257
+ const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
258
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
259
+ .filter(c => c.checkpointNumber < checkpointNumber)
260
+ .map(c => c.checkpointOutHash);
261
+
213
262
  // Try re-executing the transactions in the proposal if needed
214
263
  let reexecutionResult;
215
- if (shouldReexecute) {
216
- // Collect the out hashes of all the checkpoints before this one in the same epoch
217
- const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
218
- const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
219
- .filter(c => c.checkpointNumber < checkpointNumber)
220
- .map(c => c.checkpointOutHash);
221
-
222
- try {
223
- this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
224
- reexecutionResult = await this.reexecuteTransactions(
225
- proposal,
226
- blockNumber,
227
- checkpointNumber,
228
- txs,
229
- l1ToL2Messages,
230
- previousCheckpointOutHashes,
231
- );
232
- } catch (error) {
233
- this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
234
- const reason = this.getReexecuteFailureReason(error);
235
- return { isValid: false, blockNumber, reason, reexecutionResult };
236
- }
264
+ try {
265
+ this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
266
+ reexecutionResult = await this.reexecuteTransactions(
267
+ proposal,
268
+ blockNumber,
269
+ checkpointNumber,
270
+ txs,
271
+ l1ToL2Messages,
272
+ previousCheckpointOutHashes,
273
+ );
274
+ } catch (error) {
275
+ this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
276
+ const reason = this.getReexecuteFailureReason(error);
277
+ return { isValid: false, blockNumber, reason, reexecutionResult };
237
278
  }
238
279
 
239
280
  // If we succeeded, push this block into the archiver (unless disabled)
@@ -242,8 +283,8 @@ export class BlockProposalHandler {
242
283
  }
243
284
 
244
285
  this.log.info(
245
- `Successfully processed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
246
- proposalInfo,
286
+ `Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
287
+ { ...proposalInfo, ...pick(reexecutionResult, 'reexecutionTimeMs', 'totalManaUsed') },
247
288
  );
248
289
 
249
290
  return { isValid: true, blockNumber, reexecutionResult };
@@ -413,8 +454,48 @@ export class BlockProposalHandler {
413
454
  return new Date(nextSlotTimestampSeconds * 1000);
414
455
  }
415
456
 
416
- private getReexecuteFailureReason(err: any) {
417
- if (err instanceof ReExStateMismatchError) {
457
+ /** Waits for the block source to sync L1 data up to at least the slot before the given one. */
458
+ private async waitForBlockSourceSync(slot: SlotNumber): Promise<boolean> {
459
+ const deadline = this.getReexecutionDeadline(slot, this.checkpointsBuilder.getConfig());
460
+ const timeoutMs = deadline.getTime() - this.dateProvider.now();
461
+ if (slot === 0) {
462
+ return true;
463
+ }
464
+
465
+ // Make a quick check before triggering an archiver sync
466
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
467
+ if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
468
+ return true;
469
+ }
470
+
471
+ try {
472
+ // Trigger an immediate sync of the block source, and wait until it reports being synced to the required slot
473
+ return await retryUntil(
474
+ async () => {
475
+ await this.blockSource.syncImmediate();
476
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
477
+ return syncedSlot !== undefined && syncedSlot + 1 >= slot;
478
+ },
479
+ 'wait for block source sync',
480
+ timeoutMs / 1000,
481
+ 0.5,
482
+ );
483
+ } catch (err) {
484
+ if (err instanceof TimeoutError) {
485
+ this.log.warn(`Timed out waiting for block source to sync to slot ${slot}`);
486
+ return false;
487
+ } else {
488
+ throw err;
489
+ }
490
+ }
491
+ }
492
+
493
+ private getReexecuteFailureReason(err: any): BlockProposalValidationFailureReason {
494
+ if (err instanceof TransactionsNotAvailableError) {
495
+ return 'txs_not_available';
496
+ } else if (err instanceof ReExInitialStateMismatchError) {
497
+ return 'initial_state_mismatch';
498
+ } else if (err instanceof ReExStateMismatchError) {
418
499
  return 'state_mismatch';
419
500
  } else if (err instanceof ReExFailedTxsError) {
420
501
  return 'failed_txs';
@@ -455,6 +536,13 @@ export class BlockProposalHandler {
455
536
  await this.worldState.syncImmediate(parentBlockNumber);
456
537
  await using fork = await this.worldState.fork(parentBlockNumber);
457
538
 
539
+ // Verify the fork's archive root matches the proposal's expected last archive.
540
+ // If they don't match, our world state synced to a different chain and reexecution would fail.
541
+ const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
542
+ if (!forkArchiveRoot.equals(proposal.blockHeader.lastArchive.root)) {
543
+ throw new ReExInitialStateMismatchError(proposal.blockHeader.lastArchive.root, forkArchiveRoot);
544
+ }
545
+
458
546
  // Build checkpoint constants from proposal (excludes blockNumber which is per-block)
459
547
  const constants: CheckpointGlobalVariables = {
460
548
  chainId: new Fr(config.l1ChainId),
@@ -480,18 +568,27 @@ export class BlockProposalHandler {
480
568
 
481
569
  // Build the new block
482
570
  const deadline = this.getReexecutionDeadline(slot, config);
571
+ const maxBlockGas =
572
+ this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined
573
+ ? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity)
574
+ : undefined;
483
575
  const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
576
+ isBuildingProposal: false,
577
+ minValidTxs: 0,
484
578
  deadline,
485
579
  expectedEndState: blockHeader.state,
580
+ maxTransactions: this.config.validateMaxTxsPerBlock,
581
+ maxBlockGas,
486
582
  });
487
583
 
488
584
  const { block, failedTxs } = result;
489
585
  const numFailedTxs = failedTxs.length;
490
586
 
491
- this.log.verbose(`Transaction re-execution complete for slot ${slot}`, {
587
+ this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
492
588
  numFailedTxs,
493
589
  numProposalTxs: txHashes.length,
494
590
  numProcessedTxs: block.body.txEffects.length,
591
+ blockNumber,
495
592
  slot,
496
593
  });
497
594
 
@@ -1,5 +1,7 @@
1
+ import { NUM_CHECKPOINT_END_MARKER_FIELDS, getNumBlockEndBlobFields } from '@aztec/blob-lib/encoding';
2
+ import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB, MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT } from '@aztec/constants';
1
3
  import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
- import { merge, pick } from '@aztec/foundation/collection';
4
+ import { merge, pick, sum } from '@aztec/foundation/collection';
3
5
  import { Fr } from '@aztec/foundation/curves/bn254';
4
6
  import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
5
7
  import { bufferToHex } from '@aztec/foundation/string';
@@ -18,13 +20,14 @@ import type { ContractDataSource } from '@aztec/stdlib/contract';
18
20
  import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
19
21
  import { Gas } from '@aztec/stdlib/gas';
20
22
  import {
23
+ type BlockBuilderOptions,
21
24
  type BuildBlockInCheckpointResult,
22
25
  type FullNodeBlockBuilderConfig,
23
26
  FullNodeBlockBuilderConfigKeys,
24
27
  type ICheckpointBlockBuilder,
25
28
  type ICheckpointsBuilder,
29
+ InsufficientValidTxsError,
26
30
  type MerkleTreeWriteOperations,
27
- NoValidTxsError,
28
31
  type PublicProcessorLimits,
29
32
  type WorldStateSynchronizer,
30
33
  } from '@aztec/stdlib/interfaces/server';
@@ -32,6 +35,7 @@ import { type DebugLogStore, NullDebugLogStore } from '@aztec/stdlib/logs';
32
35
  import { MerkleTreeId } from '@aztec/stdlib/trees';
33
36
  import { type CheckpointGlobalVariables, GlobalVariables, StateReference, Tx } from '@aztec/stdlib/tx';
34
37
  import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
38
+ import { ForkCheckpoint } from '@aztec/world-state';
35
39
 
36
40
  // Re-export for backward compatibility
37
41
  export type { BuildBlockInCheckpointResult } from '@aztec/stdlib/interfaces/server';
@@ -43,6 +47,9 @@ export type { BuildBlockInCheckpointResult } from '@aztec/stdlib/interfaces/serv
43
47
  export class CheckpointBuilder implements ICheckpointBlockBuilder {
44
48
  private log: Logger;
45
49
 
50
+ /** Persistent contracts DB shared across all blocks in this checkpoint. */
51
+ protected contractsDB: PublicContractsDB;
52
+
46
53
  constructor(
47
54
  private checkpointBuilder: LightweightCheckpointBuilder,
48
55
  private fork: MerkleTreeWriteOperations,
@@ -57,6 +64,7 @@ export class CheckpointBuilder implements ICheckpointBlockBuilder {
57
64
  ...bindings,
58
65
  instanceId: `checkpoint-${checkpointBuilder.checkpointNumber}`,
59
66
  });
67
+ this.contractsDB = new PublicContractsDB(this.contractDataSource, this.log.getBindings());
60
68
  }
61
69
 
62
70
  getConstantData(): CheckpointGlobalVariables {
@@ -65,12 +73,13 @@ export class CheckpointBuilder implements ICheckpointBlockBuilder {
65
73
 
66
74
  /**
67
75
  * Builds a single block within this checkpoint.
76
+ * Automatically caps gas and blob field limits based on checkpoint-level budgets and prior blocks.
68
77
  */
69
78
  async buildBlock(
70
79
  pendingTxs: Iterable<Tx> | AsyncIterable<Tx>,
71
80
  blockNumber: BlockNumber,
72
81
  timestamp: bigint,
73
- opts: PublicProcessorLimits & { expectedEndState?: StateReference } = {},
82
+ opts: BlockBuilderOptions & { expectedEndState?: StateReference },
74
83
  ): Promise<BuildBlockInCheckpointResult> {
75
84
  const slot = this.checkpointBuilder.constants.slotNumber;
76
85
 
@@ -94,39 +103,60 @@ export class CheckpointBuilder implements ICheckpointBlockBuilder {
94
103
  });
95
104
  const { processor, validator } = await this.makeBlockBuilderDeps(globalVariables, this.fork);
96
105
 
97
- const [publicProcessorDuration, [processedTxs, failedTxs, usedTxs, _, usedTxBlobFields]] = await elapsed(() =>
98
- processor.process(pendingTxs, opts, validator),
99
- );
100
-
101
- // Throw if we didn't collect a single valid tx and we're not allowed to build empty blocks
102
- // (only the first block in a checkpoint can be empty)
103
- if (processedTxs.length === 0 && this.checkpointBuilder.getBlockCount() > 0) {
104
- throw new NoValidTxsError(failedTxs);
105
- }
106
-
107
- // Add block to checkpoint
108
- const { block } = await this.checkpointBuilder.addBlock(globalVariables, processedTxs, {
109
- expectedEndState: opts.expectedEndState,
110
- });
106
+ // Cap gas limits amd available blob fields by remaining checkpoint-level budgets
107
+ const cappedOpts: PublicProcessorLimits & { expectedEndState?: StateReference } = {
108
+ ...opts,
109
+ ...this.capLimitsByCheckpointBudgets(opts),
110
+ };
111
111
 
112
- // How much public gas was processed
113
- const publicGas = processedTxs.reduce((acc, tx) => acc.add(tx.gasUsed.publicGas), Gas.empty());
112
+ // Create a block-level checkpoint on the contracts DB so we can roll back on failure
113
+ this.contractsDB.createCheckpoint();
114
+ // We execute all merkle tree operations on a world state fork checkpoint
115
+ // This enables us to discard all modifications in the event that we fail to successfully process sufficient transactions
116
+ const forkCheckpoint = await ForkCheckpoint.new(this.fork);
114
117
 
115
- this.log.debug('Built block within checkpoint', {
116
- header: block.header.toInspect(),
117
- processedTxs: processedTxs.map(tx => tx.hash.toString()),
118
- failedTxs: failedTxs.map(tx => tx.tx.txHash.toString()),
119
- });
118
+ try {
119
+ const [publicProcessorDuration, [processedTxs, failedTxs, usedTxs]] = await elapsed(() =>
120
+ processor.process(pendingTxs, cappedOpts, validator),
121
+ );
120
122
 
121
- return {
122
- block,
123
- publicGas,
124
- publicProcessorDuration,
125
- numTxs: processedTxs.length,
126
- failedTxs,
127
- usedTxs,
128
- usedTxBlobFields,
129
- };
123
+ // Throw before updating state if we don't have enough valid txs
124
+ const minValidTxs = opts.minValidTxs ?? 0;
125
+ if (processedTxs.length < minValidTxs) {
126
+ throw new InsufficientValidTxsError(processedTxs.length, minValidTxs, failedTxs);
127
+ }
128
+
129
+ // Commit the fork checkpoint
130
+ await forkCheckpoint.commit();
131
+
132
+ // Add block to checkpoint
133
+ const { block } = await this.checkpointBuilder.addBlock(globalVariables, processedTxs, {
134
+ expectedEndState: opts.expectedEndState,
135
+ });
136
+
137
+ this.contractsDB.commitCheckpoint();
138
+
139
+ this.log.debug('Built block within checkpoint', {
140
+ header: block.header.toInspect(),
141
+ processedTxs: processedTxs.map(tx => tx.hash.toString()),
142
+ failedTxs: failedTxs.map(tx => tx.tx.txHash.toString()),
143
+ });
144
+
145
+ return {
146
+ block,
147
+ publicProcessorDuration,
148
+ numTxs: processedTxs.length,
149
+ failedTxs,
150
+ usedTxs,
151
+ };
152
+ } catch (err) {
153
+ // Revert all changes to contracts db
154
+ this.contractsDB.revertCheckpoint();
155
+ // If we reached the point of committing the checkpoint, this does nothing
156
+ // Otherwise it reverts any changes made to the fork for this failed block
157
+ await forkCheckpoint.revert();
158
+ throw err;
159
+ }
130
160
  }
131
161
 
132
162
  /** Completes the checkpoint and returns it. */
@@ -147,9 +177,68 @@ export class CheckpointBuilder implements ICheckpointBlockBuilder {
147
177
  return this.checkpointBuilder.clone().completeCheckpoint();
148
178
  }
149
179
 
180
+ /**
181
+ * Caps per-block gas and blob field limits by remaining checkpoint-level budgets.
182
+ * When building a proposal (isBuildingProposal=true), computes a fair share of remaining budget
183
+ * across remaining blocks scaled by the multiplier. When validating, only caps by per-block limit
184
+ * and remaining checkpoint budget (no redistribution or multiplier).
185
+ */
186
+ protected capLimitsByCheckpointBudgets(
187
+ opts: BlockBuilderOptions,
188
+ ): Pick<PublicProcessorLimits, 'maxBlockGas' | 'maxBlobFields' | 'maxTransactions'> {
189
+ const existingBlocks = this.checkpointBuilder.getBlocks();
190
+
191
+ // Remaining L2 gas (mana)
192
+ // IMPORTANT: This assumes mana is computed solely based on L2 gas used in transactions.
193
+ // This may change in the future.
194
+ const usedMana = sum(existingBlocks.map(b => b.header.totalManaUsed.toNumber()));
195
+ const remainingMana = this.config.rollupManaLimit - usedMana;
196
+
197
+ // Remaining DA gas
198
+ const usedDAGas = sum(existingBlocks.map(b => b.computeDAGasUsed())) ?? 0;
199
+ const remainingDAGas = MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT - usedDAGas;
200
+
201
+ // Remaining blob fields (block blob fields include both tx data and block-end overhead)
202
+ const usedBlobFields = sum(existingBlocks.map(b => b.toBlobFields().length));
203
+ const totalBlobCapacity = BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB - NUM_CHECKPOINT_END_MARKER_FIELDS;
204
+ const isFirstBlock = existingBlocks.length === 0;
205
+ const blockEndOverhead = getNumBlockEndBlobFields(isFirstBlock);
206
+ const maxBlobFieldsForTxs = totalBlobCapacity - usedBlobFields - blockEndOverhead;
207
+
208
+ // Remaining txs
209
+ const usedTxs = sum(existingBlocks.map(b => b.body.txEffects.length));
210
+ const remainingTxs = Math.max(0, (this.config.maxTxsPerCheckpoint ?? Infinity) - usedTxs);
211
+
212
+ // Cap by per-block limit + remaining checkpoint budget
213
+ let cappedL2Gas = Math.min(opts.maxBlockGas?.l2Gas ?? Infinity, remainingMana);
214
+ let cappedDAGas = Math.min(opts.maxBlockGas?.daGas ?? Infinity, remainingDAGas);
215
+ let cappedBlobFields = Math.min(opts.maxBlobFields ?? Infinity, maxBlobFieldsForTxs);
216
+ let cappedMaxTransactions = Math.min(opts.maxTransactions ?? Infinity, remainingTxs);
217
+
218
+ // Proposer mode: further cap by fair share of remaining budget across remaining blocks
219
+ if (opts.isBuildingProposal) {
220
+ const remainingBlocks = Math.max(1, opts.maxBlocksPerCheckpoint - existingBlocks.length);
221
+ const multiplier = opts.perBlockAllocationMultiplier;
222
+
223
+ cappedL2Gas = Math.min(cappedL2Gas, Math.ceil((remainingMana / remainingBlocks) * multiplier));
224
+ cappedDAGas = Math.min(cappedDAGas, Math.ceil((remainingDAGas / remainingBlocks) * multiplier));
225
+ cappedBlobFields = Math.min(cappedBlobFields, Math.ceil((maxBlobFieldsForTxs / remainingBlocks) * multiplier));
226
+ cappedMaxTransactions = Math.min(cappedMaxTransactions, Math.ceil((remainingTxs / remainingBlocks) * multiplier));
227
+ }
228
+
229
+ return {
230
+ maxBlockGas: new Gas(cappedDAGas, cappedL2Gas),
231
+ maxBlobFields: cappedBlobFields,
232
+ maxTransactions: Number.isFinite(cappedMaxTransactions) ? cappedMaxTransactions : undefined,
233
+ };
234
+ }
235
+
150
236
  protected async makeBlockBuilderDeps(globalVariables: GlobalVariables, fork: MerkleTreeWriteOperations) {
151
- const txPublicSetupAllowList = this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
152
- const contractsDB = new PublicContractsDB(this.contractDataSource, this.log.getBindings());
237
+ const txPublicSetupAllowList = [
238
+ ...(await getDefaultAllowedSetupFunctions()),
239
+ ...(this.config.txPublicSetupAllowListExtend ?? []),
240
+ ];
241
+ const contractsDB = this.contractsDB;
153
242
  const guardedFork = new GuardedMerkleTreeOperations(fork);
154
243
 
155
244
  const collectDebugLogs = this.debugLogStore.isEnabled;
package/src/config.ts CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  secretValueConfigHelper,
7
7
  } from '@aztec/foundation/config';
8
8
  import { EthAddress } from '@aztec/foundation/eth-address';
9
- import { validatorHASignerConfigMappings } from '@aztec/stdlib/ha-signing';
9
+ import { localSignerConfigMappings, validatorHASignerConfigMappings } from '@aztec/stdlib/ha-signing';
10
10
  import type { ValidatorClientConfig } from '@aztec/stdlib/interfaces/server';
11
11
 
12
12
  export type { ValidatorClientConfig };
@@ -77,6 +77,27 @@ export const validatorClientConfigMappings: ConfigMappingsType<ValidatorClientCo
77
77
  description: 'Agree to attest to equivocated checkpoint proposals (for testing purposes only)',
78
78
  ...booleanConfigHelper(false),
79
79
  },
80
+ validateMaxL2BlockGas: {
81
+ env: 'VALIDATOR_MAX_L2_BLOCK_GAS',
82
+ description: 'Maximum L2 block gas for validation. Proposals exceeding this limit are rejected.',
83
+ parseEnv: (val: string) => (val ? parseInt(val, 10) : undefined),
84
+ },
85
+ validateMaxDABlockGas: {
86
+ env: 'VALIDATOR_MAX_DA_BLOCK_GAS',
87
+ description: 'Maximum DA block gas for validation. Proposals exceeding this limit are rejected.',
88
+ parseEnv: (val: string) => (val ? parseInt(val, 10) : undefined),
89
+ },
90
+ validateMaxTxsPerBlock: {
91
+ env: 'VALIDATOR_MAX_TX_PER_BLOCK',
92
+ description: 'Maximum transactions per block for validation. Proposals exceeding this limit are rejected.',
93
+ parseEnv: (val: string) => (val ? parseInt(val, 10) : undefined),
94
+ },
95
+ validateMaxTxsPerCheckpoint: {
96
+ env: 'VALIDATOR_MAX_TX_PER_CHECKPOINT',
97
+ description: 'Maximum transactions per checkpoint for validation. Proposals exceeding this limit are rejected.',
98
+ parseEnv: (val: string) => (val ? parseInt(val, 10) : undefined),
99
+ },
100
+ ...localSignerConfigMappings,
80
101
  ...validatorHASignerConfigMappings,
81
102
  };
82
103
 
@@ -150,16 +150,10 @@ export class ValidationService {
150
150
  );
151
151
 
152
152
  // TODO(spy/ha): Use checkpointNumber instead of blockNumber once CheckpointHeader includes it.
153
- // Currently using lastBlock.blockNumber as a proxy for checkpoint identification in HA signing.
153
+ // CheckpointProposalCore doesn't have lastBlock info, so use 0 as a proxy.
154
154
  // blockNumber is NOT used for the primary key so it's safe to use here.
155
155
  // See CheckpointHeader TODO and SigningContext types documentation.
156
- let blockNumber: BlockNumber;
157
- try {
158
- blockNumber = proposal.blockNumber;
159
- } catch {
160
- // Checkpoint proposal may not have lastBlock, use 0 as fallback
161
- blockNumber = BlockNumber(0);
162
- }
156
+ const blockNumber = BlockNumber(0);
163
157
  const context: SigningContext = {
164
158
  slot: proposal.slotNumber,
165
159
  blockNumber,
@@ -183,7 +177,7 @@ export class ValidationService {
183
177
  } else {
184
178
  const error = result.reason;
185
179
  if (error instanceof DutyAlreadySignedError || error instanceof SlashingProtectionError) {
186
- this.log.info(
180
+ this.log.verbose(
187
181
  `Attestation for slot ${proposal.slotNumber} by ${attestors[i]} already signed by another High-Availability node`,
188
182
  );
189
183
  // Continue with remaining attestors