@aztec/validator-client 0.0.1-commit.ef17749e1 → 0.0.1-commit.f1b29a41e

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.
@@ -20,13 +20,14 @@ import type { ContractDataSource } from '@aztec/stdlib/contract';
20
20
  import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
21
21
  import { Gas } from '@aztec/stdlib/gas';
22
22
  import {
23
+ type BlockBuilderOptions,
23
24
  type BuildBlockInCheckpointResult,
24
25
  type FullNodeBlockBuilderConfig,
25
26
  FullNodeBlockBuilderConfigKeys,
26
27
  type ICheckpointBlockBuilder,
27
28
  type ICheckpointsBuilder,
29
+ InsufficientValidTxsError,
28
30
  type MerkleTreeWriteOperations,
29
- NoValidTxsError,
30
31
  type PublicProcessorLimits,
31
32
  type WorldStateSynchronizer,
32
33
  } from '@aztec/stdlib/interfaces/server';
@@ -34,6 +35,7 @@ import { type DebugLogStore, NullDebugLogStore } from '@aztec/stdlib/logs';
34
35
  import { MerkleTreeId } from '@aztec/stdlib/trees';
35
36
  import { type CheckpointGlobalVariables, GlobalVariables, StateReference, Tx } from '@aztec/stdlib/tx';
36
37
  import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
38
+ import { ForkCheckpoint } from '@aztec/world-state';
37
39
 
38
40
  // Re-export for backward compatibility
39
41
  export type { BuildBlockInCheckpointResult } from '@aztec/stdlib/interfaces/server';
@@ -45,6 +47,9 @@ export type { BuildBlockInCheckpointResult } from '@aztec/stdlib/interfaces/serv
45
47
  export class CheckpointBuilder implements ICheckpointBlockBuilder {
46
48
  private log: Logger;
47
49
 
50
+ /** Persistent contracts DB shared across all blocks in this checkpoint. */
51
+ protected contractsDB: PublicContractsDB;
52
+
48
53
  constructor(
49
54
  private checkpointBuilder: LightweightCheckpointBuilder,
50
55
  private fork: MerkleTreeWriteOperations,
@@ -59,6 +64,7 @@ export class CheckpointBuilder implements ICheckpointBlockBuilder {
59
64
  ...bindings,
60
65
  instanceId: `checkpoint-${checkpointBuilder.checkpointNumber}`,
61
66
  });
67
+ this.contractsDB = new PublicContractsDB(this.contractDataSource, this.log.getBindings());
62
68
  }
63
69
 
64
70
  getConstantData(): CheckpointGlobalVariables {
@@ -73,7 +79,7 @@ export class CheckpointBuilder implements ICheckpointBlockBuilder {
73
79
  pendingTxs: Iterable<Tx> | AsyncIterable<Tx>,
74
80
  blockNumber: BlockNumber,
75
81
  timestamp: bigint,
76
- opts: PublicProcessorLimits & { expectedEndState?: StateReference } = {},
82
+ opts: BlockBuilderOptions & { expectedEndState?: StateReference },
77
83
  ): Promise<BuildBlockInCheckpointResult> {
78
84
  const slot = this.checkpointBuilder.constants.slotNumber;
79
85
 
@@ -103,34 +109,54 @@ export class CheckpointBuilder implements ICheckpointBlockBuilder {
103
109
  ...this.capLimitsByCheckpointBudgets(opts),
104
110
  };
105
111
 
106
- const [publicProcessorDuration, [processedTxs, failedTxs, usedTxs]] = await elapsed(() =>
107
- processor.process(pendingTxs, cappedOpts, validator),
108
- );
109
-
110
- // Throw if we didn't collect a single valid tx and we're not allowed to build empty blocks
111
- // (only the first block in a checkpoint can be empty)
112
- if (processedTxs.length === 0 && this.checkpointBuilder.getBlockCount() > 0) {
113
- throw new NoValidTxsError(failedTxs);
114
- }
115
-
116
- // Add block to checkpoint
117
- const { block } = await this.checkpointBuilder.addBlock(globalVariables, processedTxs, {
118
- expectedEndState: opts.expectedEndState,
119
- });
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);
120
117
 
121
- this.log.debug('Built block within checkpoint', {
122
- header: block.header.toInspect(),
123
- processedTxs: processedTxs.map(tx => tx.hash.toString()),
124
- failedTxs: failedTxs.map(tx => tx.tx.txHash.toString()),
125
- });
118
+ try {
119
+ const [publicProcessorDuration, [processedTxs, failedTxs, usedTxs]] = await elapsed(() =>
120
+ processor.process(pendingTxs, cappedOpts, validator),
121
+ );
126
122
 
127
- return {
128
- block,
129
- publicProcessorDuration,
130
- numTxs: processedTxs.length,
131
- failedTxs,
132
- usedTxs,
133
- };
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
+ }
134
160
  }
135
161
 
136
162
  /** Completes the checkpoint and returns it. */
@@ -153,11 +179,12 @@ export class CheckpointBuilder implements ICheckpointBlockBuilder {
153
179
 
154
180
  /**
155
181
  * Caps per-block gas and blob field limits by remaining checkpoint-level budgets.
156
- * Computes remaining L2 gas (mana), DA gas, and blob fields from blocks already added to the checkpoint,
157
- * then returns opts with maxBlockGas and maxBlobFields capped accordingly.
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).
158
185
  */
159
186
  protected capLimitsByCheckpointBudgets(
160
- opts: PublicProcessorLimits,
187
+ opts: BlockBuilderOptions,
161
188
  ): Pick<PublicProcessorLimits, 'maxBlockGas' | 'maxBlobFields' | 'maxTransactions'> {
162
189
  const existingBlocks = this.checkpointBuilder.getBlocks();
163
190
 
@@ -178,31 +205,31 @@ export class CheckpointBuilder implements ICheckpointBlockBuilder {
178
205
  const blockEndOverhead = getNumBlockEndBlobFields(isFirstBlock);
179
206
  const maxBlobFieldsForTxs = totalBlobCapacity - usedBlobFields - blockEndOverhead;
180
207
 
181
- // Cap L2 gas by remaining checkpoint mana
182
- const cappedL2Gas = Math.min(opts.maxBlockGas?.l2Gas ?? remainingMana, remainingMana);
183
-
184
- // Cap DA gas by remaining checkpoint DA gas budget
185
- const cappedDAGas = Math.min(opts.maxBlockGas?.daGas ?? remainingDAGas, remainingDAGas);
186
-
187
- // Cap blob fields by remaining checkpoint blob capacity
188
- const cappedBlobFields =
189
- opts.maxBlobFields !== undefined ? Math.min(opts.maxBlobFields, maxBlobFieldsForTxs) : maxBlobFieldsForTxs;
190
-
191
- // Cap transaction count by remaining checkpoint tx budget
192
- let cappedMaxTransactions: number | undefined;
193
- if (this.config.maxTxsPerCheckpoint !== undefined) {
194
- const usedTxs = sum(existingBlocks.map(b => b.body.txEffects.length));
195
- const remainingTxs = Math.max(0, this.config.maxTxsPerCheckpoint - usedTxs);
196
- cappedMaxTransactions =
197
- opts.maxTransactions !== undefined ? Math.min(opts.maxTransactions, remainingTxs) : remainingTxs;
198
- } else {
199
- cappedMaxTransactions = opts.maxTransactions;
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));
200
227
  }
201
228
 
202
229
  return {
203
230
  maxBlockGas: new Gas(cappedDAGas, cappedL2Gas),
204
231
  maxBlobFields: cappedBlobFields,
205
- maxTransactions: cappedMaxTransactions,
232
+ maxTransactions: Number.isFinite(cappedMaxTransactions) ? cappedMaxTransactions : undefined,
206
233
  };
207
234
  }
208
235
 
@@ -211,7 +238,7 @@ export class CheckpointBuilder implements ICheckpointBlockBuilder {
211
238
  ...(await getDefaultAllowedSetupFunctions()),
212
239
  ...(this.config.txPublicSetupAllowListExtend ?? []),
213
240
  ];
214
- const contractsDB = new PublicContractsDB(this.contractDataSource, this.log.getBindings());
241
+ const contractsDB = this.contractsDB;
215
242
  const guardedFork = new GuardedMerkleTreeOperations(fork);
216
243
 
217
244
  const collectDebugLogs = this.debugLogStore.isEnabled;
package/src/config.ts CHANGED
@@ -49,11 +49,6 @@ export const validatorClientConfigMappings: ConfigMappingsType<ValidatorClientCo
49
49
  description: 'Interval between polling for new attestations',
50
50
  ...numberConfigHelper(200),
51
51
  },
52
- validatorReexecute: {
53
- env: 'VALIDATOR_REEXECUTE',
54
- description: 'Re-execute transactions before attesting',
55
- ...booleanConfigHelper(true),
56
- },
57
52
  alwaysReexecuteBlockProposals: {
58
53
  description:
59
54
  'Whether to always reexecute block proposals, even for non-validator nodes (useful for monitoring network status).',
@@ -177,7 +177,7 @@ export class ValidationService {
177
177
  } else {
178
178
  const error = result.reason;
179
179
  if (error instanceof DutyAlreadySignedError || error instanceof SlashingProtectionError) {
180
- this.log.info(
180
+ this.log.verbose(
181
181
  `Attestation for slot ${proposal.slotNumber} by ${attestors[i]} already signed by another High-Availability node`,
182
182
  );
183
183
  // Continue with remaining attestors
package/src/factory.ts CHANGED
@@ -7,13 +7,14 @@ import type { L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
7
7
  import type { ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
8
8
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
9
9
  import type { TelemetryClient } from '@aztec/telemetry-client';
10
+ import type { SlashingProtectionDatabase } from '@aztec/validator-ha-signer/types';
10
11
 
11
- import { BlockProposalHandler } from './block_proposal_handler.js';
12
12
  import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
13
13
  import { ValidatorMetrics } from './metrics.js';
14
+ import { ProposalHandler } from './proposal_handler.js';
14
15
  import { ValidatorClient } from './validator.js';
15
16
 
16
- export function createBlockProposalHandler(
17
+ export function createProposalHandler(
17
18
  config: ValidatorClientFullConfig,
18
19
  deps: {
19
20
  checkpointsBuilder: FullNodeCheckpointsBuilder;
@@ -22,6 +23,7 @@ export function createBlockProposalHandler(
22
23
  l1ToL2MessageSource: L1ToL2MessageSource;
23
24
  p2pClient: P2PClient;
24
25
  epochCache: EpochCache;
26
+ blobClient: BlobClientInterface;
25
27
  dateProvider: DateProvider;
26
28
  telemetry: TelemetryClient;
27
29
  },
@@ -29,9 +31,9 @@ export function createBlockProposalHandler(
29
31
  const metrics = new ValidatorMetrics(deps.telemetry);
30
32
  const blockProposalValidator = new BlockProposalValidator(deps.epochCache, {
31
33
  txsPermitted: !config.disableTransactions,
32
- maxTxsPerBlock: config.validateMaxTxsPerBlock,
34
+ maxTxsPerBlock: config.validateMaxTxsPerBlock ?? config.validateMaxTxsPerCheckpoint,
33
35
  });
34
- return new BlockProposalHandler(
36
+ return new ProposalHandler(
35
37
  deps.checkpointsBuilder,
36
38
  deps.worldState,
37
39
  deps.blockSource,
@@ -40,6 +42,7 @@ export function createBlockProposalHandler(
40
42
  blockProposalValidator,
41
43
  deps.epochCache,
42
44
  config,
45
+ deps.blobClient,
43
46
  metrics,
44
47
  deps.dateProvider,
45
48
  deps.telemetry,
@@ -59,6 +62,7 @@ export function createValidatorClient(
59
62
  epochCache: EpochCache;
60
63
  keyStoreManager: KeystoreManager | undefined;
61
64
  blobClient: BlobClientInterface;
65
+ slashingProtectionDb?: SlashingProtectionDatabase;
62
66
  },
63
67
  ) {
64
68
  if (config.disableValidator || !deps.keyStoreManager) {
@@ -79,5 +83,6 @@ export function createValidatorClient(
79
83
  deps.blobClient,
80
84
  deps.dateProvider,
81
85
  deps.telemetry,
86
+ deps.slashingProtectionDb,
82
87
  );
83
88
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from './block_proposal_handler.js';
1
+ export * from './proposal_handler.js';
2
2
  export * from './checkpoint_builder.js';
3
3
  export * from './config.js';
4
4
  export * from './factory.js';
@@ -240,7 +240,7 @@ export class HAKeyStore implements ExtendedValidatorKeyStore {
240
240
  }
241
241
 
242
242
  if (error instanceof SlashingProtectionError) {
243
- this.log.warn(`Duty already signed by another node with different payload`, {
243
+ this.log.info(`Duty already signed by another node with different payload`, {
244
244
  dutyType: context.dutyType,
245
245
  slot: context.slot,
246
246
  existingMessageHash: error.existingMessageHash,
package/src/metrics.ts CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  createUpDownCounterWithDefault,
12
12
  } from '@aztec/telemetry-client';
13
13
 
14
- import type { BlockProposalValidationFailureReason } from './block_proposal_handler.js';
14
+ import type { BlockProposalValidationFailureReason } from './proposal_handler.js';
15
15
 
16
16
  export class ValidatorMetrics {
17
17
  private failedReexecutionCounter: UpDownCounter;