@aztec/validator-client 4.0.0-nightly.20260111 → 4.0.0-nightly.20260113

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 (42) hide show
  1. package/README.md +256 -0
  2. package/dest/block_proposal_handler.d.ts +20 -10
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +333 -72
  5. package/dest/checkpoint_builder.d.ts +70 -0
  6. package/dest/checkpoint_builder.d.ts.map +1 -0
  7. package/dest/checkpoint_builder.js +155 -0
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +10 -0
  11. package/dest/duties/validation_service.d.ts +26 -10
  12. package/dest/duties/validation_service.d.ts.map +1 -1
  13. package/dest/duties/validation_service.js +51 -21
  14. package/dest/factory.d.ts +10 -7
  15. package/dest/factory.d.ts.map +1 -1
  16. package/dest/factory.js +2 -2
  17. package/dest/index.d.ts +3 -1
  18. package/dest/index.d.ts.map +1 -1
  19. package/dest/index.js +2 -0
  20. package/dest/tx_validator/index.d.ts +3 -0
  21. package/dest/tx_validator/index.d.ts.map +1 -0
  22. package/dest/tx_validator/index.js +2 -0
  23. package/dest/tx_validator/nullifier_cache.d.ts +14 -0
  24. package/dest/tx_validator/nullifier_cache.d.ts.map +1 -0
  25. package/dest/tx_validator/nullifier_cache.js +24 -0
  26. package/dest/tx_validator/tx_validator_factory.d.ts +18 -0
  27. package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -0
  28. package/dest/tx_validator/tx_validator_factory.js +53 -0
  29. package/dest/validator.d.ts +39 -15
  30. package/dest/validator.d.ts.map +1 -1
  31. package/dest/validator.js +297 -449
  32. package/package.json +16 -12
  33. package/src/block_proposal_handler.ts +249 -39
  34. package/src/checkpoint_builder.ts +267 -0
  35. package/src/config.ts +10 -0
  36. package/src/duties/validation_service.ts +79 -25
  37. package/src/factory.ts +13 -8
  38. package/src/index.ts +2 -0
  39. package/src/tx_validator/index.ts +2 -0
  40. package/src/tx_validator/nullifier_cache.ts +30 -0
  41. package/src/tx_validator/tx_validator_factory.ts +133 -0
  42. package/src/validator.ts +400 -94
@@ -0,0 +1,267 @@
1
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
+ import { merge, pick } from '@aztec/foundation/collection';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
4
+ import { createLogger } from '@aztec/foundation/log';
5
+ import { bufferToHex } from '@aztec/foundation/string';
6
+ import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
7
+ import { getDefaultAllowedSetupFunctions } from '@aztec/p2p/msg_validators';
8
+ import { LightweightCheckpointBuilder } from '@aztec/prover-client/light';
9
+ import {
10
+ GuardedMerkleTreeOperations,
11
+ PublicContractsDB,
12
+ PublicProcessor,
13
+ createPublicTxSimulatorForBlockBuilding,
14
+ } from '@aztec/simulator/server';
15
+ import { L2BlockNew } from '@aztec/stdlib/block';
16
+ import { Checkpoint } from '@aztec/stdlib/checkpoint';
17
+ import type { ContractDataSource } from '@aztec/stdlib/contract';
18
+ import { Gas } from '@aztec/stdlib/gas';
19
+ import {
20
+ type FullNodeBlockBuilderConfig,
21
+ FullNodeBlockBuilderConfigKeys,
22
+ type MerkleTreeWriteOperations,
23
+ type PublicProcessorLimits,
24
+ } from '@aztec/stdlib/interfaces/server';
25
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
26
+ import { type CheckpointGlobalVariables, type FailedTx, GlobalVariables, StateReference, Tx } from '@aztec/stdlib/tx';
27
+ import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
28
+
29
+ import { createValidatorForBlockBuilding } from './tx_validator/tx_validator_factory.js';
30
+
31
+ const log = createLogger('checkpoint-builder');
32
+
33
+ export interface BuildBlockInCheckpointResult {
34
+ block: L2BlockNew;
35
+ publicGas: Gas;
36
+ publicProcessorDuration: number;
37
+ numTxs: number;
38
+ failedTxs: FailedTx[];
39
+ blockBuildingTimer: Timer;
40
+ usedTxs: Tx[];
41
+ }
42
+
43
+ /**
44
+ * Builder for a single checkpoint. Handles building blocks within the checkpoint
45
+ * and completing it.
46
+ */
47
+ export class CheckpointBuilder {
48
+ constructor(
49
+ private checkpointBuilder: LightweightCheckpointBuilder,
50
+ private fork: MerkleTreeWriteOperations,
51
+ private config: FullNodeBlockBuilderConfig,
52
+ private contractDataSource: ContractDataSource,
53
+ private dateProvider: DateProvider,
54
+ private telemetryClient: TelemetryClient,
55
+ ) {}
56
+
57
+ getConstantData(): CheckpointGlobalVariables {
58
+ return this.checkpointBuilder.constants;
59
+ }
60
+
61
+ /**
62
+ * Builds a single block within this checkpoint.
63
+ */
64
+ async buildBlock(
65
+ pendingTxs: Iterable<Tx> | AsyncIterable<Tx>,
66
+ blockNumber: BlockNumber,
67
+ timestamp: bigint,
68
+ opts: PublicProcessorLimits & { expectedEndState?: StateReference },
69
+ ): Promise<BuildBlockInCheckpointResult> {
70
+ const blockBuildingTimer = new Timer();
71
+ const slot = this.checkpointBuilder.constants.slotNumber;
72
+
73
+ log.verbose(`Building block ${blockNumber} for slot ${slot} within checkpoint`, { slot, blockNumber, ...opts });
74
+
75
+ const constants = this.checkpointBuilder.constants;
76
+ const globalVariables = GlobalVariables.from({
77
+ chainId: constants.chainId,
78
+ version: constants.version,
79
+ blockNumber,
80
+ slotNumber: constants.slotNumber,
81
+ timestamp,
82
+ coinbase: constants.coinbase,
83
+ feeRecipient: constants.feeRecipient,
84
+ gasFees: constants.gasFees,
85
+ });
86
+ const { processor, validator } = await this.makeBlockBuilderDeps(globalVariables, this.fork);
87
+
88
+ const [publicProcessorDuration, [processedTxs, failedTxs, usedTxs]] = await elapsed(() =>
89
+ processor.process(pendingTxs, opts, validator),
90
+ );
91
+
92
+ // Add block to checkpoint
93
+ const block = await this.checkpointBuilder.addBlock(globalVariables, processedTxs, {
94
+ expectedEndState: opts.expectedEndState,
95
+ });
96
+
97
+ // How much public gas was processed
98
+ const publicGas = processedTxs.reduce((acc, tx) => acc.add(tx.gasUsed.publicGas), Gas.empty());
99
+
100
+ const res = {
101
+ block,
102
+ publicGas,
103
+ publicProcessorDuration,
104
+ numTxs: processedTxs.length,
105
+ failedTxs,
106
+ blockBuildingTimer,
107
+ usedTxs,
108
+ };
109
+ log.debug('Built block within checkpoint', res.block.header);
110
+ return res;
111
+ }
112
+
113
+ /** Completes the checkpoint and returns it. */
114
+ async completeCheckpoint(): Promise<Checkpoint> {
115
+ const checkpoint = await this.checkpointBuilder.completeCheckpoint();
116
+
117
+ log.verbose(`Completed checkpoint ${checkpoint.number}`, {
118
+ checkpointNumber: checkpoint.number,
119
+ numBlocks: checkpoint.blocks.length,
120
+ archiveRoot: checkpoint.archive.root.toString(),
121
+ });
122
+
123
+ return checkpoint;
124
+ }
125
+
126
+ /** Gets the checkpoint currently in progress. */
127
+ getCheckpoint(): Promise<Checkpoint> {
128
+ return this.checkpointBuilder.clone().completeCheckpoint();
129
+ }
130
+
131
+ protected async makeBlockBuilderDeps(globalVariables: GlobalVariables, fork: MerkleTreeWriteOperations) {
132
+ const txPublicSetupAllowList = this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
133
+ const contractsDB = new PublicContractsDB(this.contractDataSource);
134
+ const guardedFork = new GuardedMerkleTreeOperations(fork);
135
+
136
+ const publicTxSimulator = createPublicTxSimulatorForBlockBuilding(
137
+ guardedFork,
138
+ contractsDB,
139
+ globalVariables,
140
+ this.telemetryClient,
141
+ );
142
+
143
+ const processor = new PublicProcessor(
144
+ globalVariables,
145
+ guardedFork,
146
+ contractsDB,
147
+ publicTxSimulator,
148
+ this.dateProvider,
149
+ this.telemetryClient,
150
+ undefined,
151
+ this.config,
152
+ );
153
+
154
+ const validator = createValidatorForBlockBuilding(
155
+ fork,
156
+ this.contractDataSource,
157
+ globalVariables,
158
+ txPublicSetupAllowList,
159
+ );
160
+
161
+ return {
162
+ processor,
163
+ validator,
164
+ };
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Factory for creating checkpoint builders.
170
+ */
171
+ export class FullNodeCheckpointsBuilder {
172
+ constructor(
173
+ private config: FullNodeBlockBuilderConfig,
174
+ private contractDataSource: ContractDataSource,
175
+ private dateProvider: DateProvider,
176
+ private telemetryClient: TelemetryClient = getTelemetryClient(),
177
+ ) {}
178
+
179
+ public getConfig(): FullNodeBlockBuilderConfig {
180
+ return this.config;
181
+ }
182
+
183
+ public updateConfig(config: Partial<FullNodeBlockBuilderConfig>) {
184
+ this.config = merge(this.config, pick(config, ...FullNodeBlockBuilderConfigKeys));
185
+ }
186
+
187
+ /**
188
+ * Starts a new checkpoint and returns a CheckpointBuilder to build blocks within it.
189
+ */
190
+ async startCheckpoint(
191
+ checkpointNumber: CheckpointNumber,
192
+ constants: CheckpointGlobalVariables,
193
+ l1ToL2Messages: Fr[],
194
+ fork: MerkleTreeWriteOperations,
195
+ ): Promise<CheckpointBuilder> {
196
+ const stateReference = await fork.getStateReference();
197
+ const archiveTree = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
198
+
199
+ log.verbose(`Building new checkpoint ${checkpointNumber}`, {
200
+ checkpointNumber,
201
+ msgCount: l1ToL2Messages.length,
202
+ initialStateReference: stateReference.toInspect(),
203
+ initialArchiveRoot: bufferToHex(archiveTree.root),
204
+ constants,
205
+ });
206
+
207
+ const lightweightBuilder = await LightweightCheckpointBuilder.startNewCheckpoint(
208
+ checkpointNumber,
209
+ constants,
210
+ l1ToL2Messages,
211
+ fork,
212
+ );
213
+
214
+ return new CheckpointBuilder(
215
+ lightweightBuilder,
216
+ fork,
217
+ this.config,
218
+ this.contractDataSource,
219
+ this.dateProvider,
220
+ this.telemetryClient,
221
+ );
222
+ }
223
+
224
+ /**
225
+ * Opens a checkpoint, either starting fresh or resuming from existing blocks.
226
+ */
227
+ async openCheckpoint(
228
+ checkpointNumber: CheckpointNumber,
229
+ constants: CheckpointGlobalVariables,
230
+ l1ToL2Messages: Fr[],
231
+ fork: MerkleTreeWriteOperations,
232
+ existingBlocks: L2BlockNew[] = [],
233
+ ): Promise<CheckpointBuilder> {
234
+ const stateReference = await fork.getStateReference();
235
+ const archiveTree = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
236
+
237
+ if (existingBlocks.length === 0) {
238
+ return this.startCheckpoint(checkpointNumber, constants, l1ToL2Messages, fork);
239
+ }
240
+
241
+ log.verbose(`Resuming checkpoint ${checkpointNumber} with ${existingBlocks.length} existing blocks`, {
242
+ checkpointNumber,
243
+ msgCount: l1ToL2Messages.length,
244
+ existingBlockCount: existingBlocks.length,
245
+ initialStateReference: stateReference.toInspect(),
246
+ initialArchiveRoot: bufferToHex(archiveTree.root),
247
+ constants,
248
+ });
249
+
250
+ const lightweightBuilder = await LightweightCheckpointBuilder.resumeCheckpoint(
251
+ checkpointNumber,
252
+ constants,
253
+ l1ToL2Messages,
254
+ fork,
255
+ existingBlocks,
256
+ );
257
+
258
+ return new CheckpointBuilder(
259
+ lightweightBuilder,
260
+ fork,
261
+ this.config,
262
+ this.contractDataSource,
263
+ this.dateProvider,
264
+ this.telemetryClient,
265
+ );
266
+ }
267
+ }
package/src/config.ts CHANGED
@@ -70,6 +70,16 @@ export const validatorClientConfigMappings: ConfigMappingsType<ValidatorClientCo
70
70
  'Whether to run in fisherman mode: validates all proposals and attestations but does not broadcast attestations or participate in consensus.',
71
71
  ...booleanConfigHelper(false),
72
72
  },
73
+ // TODO(palla/mbps): Change default to false once checkpoint validation is stable
74
+ skipCheckpointProposalValidation: {
75
+ description: 'Skip checkpoint proposal validation and always attest (default: true)',
76
+ defaultValue: true,
77
+ },
78
+ // TODO(palla/mbps): Change default to false once block sync is stable
79
+ skipPushProposedBlocksToArchiver: {
80
+ description: 'Skip pushing re-executed blocks to archiver (default: true)',
81
+ defaultValue: true,
82
+ },
73
83
  };
74
84
 
75
85
  /**
@@ -5,15 +5,19 @@ import type { EthAddress } from '@aztec/foundation/eth-address';
5
5
  import type { Signature } from '@aztec/foundation/eth-signature';
6
6
  import { createLogger } from '@aztec/foundation/log';
7
7
  import type { CommitteeAttestationsAndSigners } from '@aztec/stdlib/block';
8
+ import type { CreateCheckpointProposalLastBlockData } from '@aztec/stdlib/interfaces/server';
8
9
  import {
9
- BlockAttestation,
10
10
  BlockProposal,
11
11
  type BlockProposalOptions,
12
+ CheckpointAttestation,
13
+ CheckpointProposal,
14
+ type CheckpointProposalCore,
15
+ type CheckpointProposalOptions,
12
16
  ConsensusPayload,
13
17
  SignatureDomainSeparator,
14
18
  } from '@aztec/stdlib/p2p';
15
19
  import type { CheckpointHeader } from '@aztec/stdlib/rollup';
16
- import type { Tx } from '@aztec/stdlib/tx';
20
+ import type { BlockHeader, Tx } from '@aztec/stdlib/tx';
17
21
 
18
22
  import type { ValidatorKeyStore } from '../key_store/interface.js';
19
23
 
@@ -26,63 +30,113 @@ export class ValidationService {
26
30
  /**
27
31
  * Create a block proposal with the given header, archive, and transactions
28
32
  *
29
- * @param header - The block header
33
+ * @param blockHeader - The block header
34
+ * @param indexWithinCheckpoint - Index of this block within the checkpoint (0-indexed)
35
+ * @param inHash - Hash of L1 to L2 messages for this checkpoint
30
36
  * @param archive - The archive of the current block
31
37
  * @param txs - TxHash[] ordered list of transactions
32
38
  * @param options - Block proposal options (including broadcastInvalidBlockProposal for testing)
33
39
  *
34
- * @returns A block proposal signing the above information (not the current implementation!!!)
40
+ * @returns A block proposal signing the above information
35
41
  */
36
- async createBlockProposal(
37
- header: CheckpointHeader,
42
+ public createBlockProposal(
43
+ blockHeader: BlockHeader,
44
+ indexWithinCheckpoint: number,
45
+ inHash: Fr,
38
46
  archive: Fr,
39
47
  txs: Tx[],
40
48
  proposerAttesterAddress: EthAddress | undefined,
41
49
  options: BlockProposalOptions,
42
50
  ): Promise<BlockProposal> {
43
- let payloadSigner: (payload: Buffer32) => Promise<Signature>;
44
- if (proposerAttesterAddress !== undefined) {
45
- payloadSigner = (payload: Buffer32) => this.keyStore.signMessageWithAddress(proposerAttesterAddress, payload);
46
- } else {
47
- // if there is no proposer attester address, just use the first signer
48
- const signer = this.keyStore.getAddress(0);
49
- payloadSigner = (payload: Buffer32) => this.keyStore.signMessageWithAddress(signer, payload);
50
- }
51
- // TODO: check if this is calculated earlier / can not be recomputed
52
- const txHashes = await Promise.all(txs.map(tx => tx.getTxHash()));
51
+ const payloadSigner = this.getPayloadSigner(proposerAttesterAddress);
53
52
 
54
53
  // For testing: change the new archive to trigger state_mismatch validation failure
55
54
  if (options.broadcastInvalidBlockProposal) {
56
55
  archive = Fr.random();
57
- this.log.warn(`Creating INVALID block proposal for slot ${header.slotNumber}`);
56
+ this.log.warn(`Creating INVALID block proposal for slot ${blockHeader.globalVariables.slotNumber}`);
58
57
  }
59
58
 
60
59
  return BlockProposal.createProposalFromSigner(
61
- new ConsensusPayload(header, archive),
62
- txHashes,
60
+ blockHeader,
61
+ indexWithinCheckpoint,
62
+ inHash,
63
+ archive,
64
+ txs.map(tx => tx.getTxHash()),
63
65
  options.publishFullTxs ? txs : undefined,
64
66
  payloadSigner,
65
67
  );
66
68
  }
67
69
 
68
70
  /**
69
- * Attest with selection of validators to the given block proposal, constructed by the current sequencer
71
+ * Create a checkpoint proposal with the last block header and checkpoint header
72
+ *
73
+ * @param checkpointHeader - The checkpoint header containing aggregated data
74
+ * @param archive - The archive of the checkpoint
75
+ * @param lastBlockInfo - Info about the last block (header, index, txs) or undefined
76
+ * @param proposerAttesterAddress - The address of the proposer
77
+ * @param options - Checkpoint proposal options
78
+ *
79
+ * @returns A checkpoint proposal signing the above information
80
+ */
81
+ public createCheckpointProposal(
82
+ checkpointHeader: CheckpointHeader,
83
+ archive: Fr,
84
+ lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
85
+ proposerAttesterAddress: EthAddress | undefined,
86
+ options: CheckpointProposalOptions,
87
+ ): Promise<CheckpointProposal> {
88
+ const payloadSigner = this.getPayloadSigner(proposerAttesterAddress);
89
+
90
+ // For testing: change the archive to trigger state_mismatch validation failure
91
+ if (options.broadcastInvalidCheckpointProposal) {
92
+ archive = Fr.random();
93
+ this.log.warn(`Creating INVALID checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
94
+ }
95
+
96
+ // Last block to include in the proposal
97
+ const lastBlock = lastBlockInfo && {
98
+ blockHeader: lastBlockInfo.blockHeader,
99
+ indexWithinCheckpoint: lastBlockInfo.indexWithinCheckpoint,
100
+ txHashes: lastBlockInfo.txs.map(tx => tx.getTxHash()),
101
+ txs: options.publishFullTxs ? lastBlockInfo.txs : undefined,
102
+ };
103
+
104
+ return CheckpointProposal.createProposalFromSigner(checkpointHeader, archive, lastBlock, payloadSigner);
105
+ }
106
+
107
+ private getPayloadSigner(proposerAttesterAddress: EthAddress | undefined): (payload: Buffer32) => Promise<Signature> {
108
+ if (proposerAttesterAddress !== undefined) {
109
+ return (payload: Buffer32) => this.keyStore.signMessageWithAddress(proposerAttesterAddress, payload);
110
+ } else {
111
+ // if there is no proposer attester address, just use the first signer
112
+ const signer = this.keyStore.getAddress(0);
113
+ return (payload: Buffer32) => this.keyStore.signMessageWithAddress(signer, payload);
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Attest with selection of validators to the given checkpoint proposal
70
119
  *
71
120
  * NOTE: This is just a blind signing.
72
121
  * We assume that the proposal is valid and DA guarantees have been checked previously.
73
122
  *
74
- * @param proposal - The proposal to attest to
123
+ * @param proposal - The checkpoint proposal (core version without lastBlock) to attest to
75
124
  * @param attestors - The validators to attest with
76
- * @returns attestations
125
+ * @returns checkpoint attestations
77
126
  */
78
- async attestToProposal(proposal: BlockProposal, attestors: EthAddress[]): Promise<BlockAttestation[]> {
127
+ async attestToCheckpointProposal(
128
+ proposal: CheckpointProposalCore,
129
+ attestors: EthAddress[],
130
+ ): Promise<CheckpointAttestation[]> {
131
+ // Create the attestation payload from the checkpoint proposal
132
+ const payload = new ConsensusPayload(proposal.checkpointHeader, proposal.archive);
79
133
  const buf = Buffer32.fromBuffer(
80
- keccak256(proposal.payload.getPayloadToSign(SignatureDomainSeparator.blockAttestation)),
134
+ keccak256(payload.getPayloadToSign(SignatureDomainSeparator.checkpointAttestation)),
81
135
  );
82
136
  const signatures = await Promise.all(
83
137
  attestors.map(attestor => this.keyStore.signMessageWithAddress(attestor, buf)),
84
138
  );
85
- return signatures.map(sig => new BlockAttestation(proposal.payload, sig, proposal.signature));
139
+ return signatures.map(sig => new CheckpointAttestation(payload, sig, proposal.signature));
86
140
  }
87
141
 
88
142
  async signAttestationsAndSigners(
package/src/factory.ts CHANGED
@@ -3,20 +3,22 @@ import type { EpochCache } from '@aztec/epoch-cache';
3
3
  import type { DateProvider } from '@aztec/foundation/timer';
4
4
  import type { KeystoreManager } from '@aztec/node-keystore';
5
5
  import { BlockProposalValidator, type P2PClient } from '@aztec/p2p';
6
- import type { L2BlockSource } from '@aztec/stdlib/block';
7
- import type { IFullNodeBlockBuilder, ValidatorClientFullConfig } from '@aztec/stdlib/interfaces/server';
6
+ import type { L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
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
10
 
11
11
  import { BlockProposalHandler } from './block_proposal_handler.js';
12
+ import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
12
13
  import { ValidatorMetrics } from './metrics.js';
13
14
  import { ValidatorClient } from './validator.js';
14
15
 
15
16
  export function createBlockProposalHandler(
16
17
  config: ValidatorClientFullConfig,
17
18
  deps: {
18
- blockBuilder: IFullNodeBlockBuilder;
19
- blockSource: L2BlockSource;
19
+ checkpointsBuilder: FullNodeCheckpointsBuilder;
20
+ worldState: WorldStateSynchronizer;
21
+ blockSource: L2BlockSource & L2BlockSink;
20
22
  l1ToL2MessageSource: L1ToL2MessageSource;
21
23
  p2pClient: P2PClient;
22
24
  epochCache: EpochCache;
@@ -29,7 +31,8 @@ export function createBlockProposalHandler(
29
31
  txsPermitted: !config.disableTransactions,
30
32
  });
31
33
  return new BlockProposalHandler(
32
- deps.blockBuilder,
34
+ deps.checkpointsBuilder,
35
+ deps.worldState,
33
36
  deps.blockSource,
34
37
  deps.l1ToL2MessageSource,
35
38
  deps.p2pClient.getTxProvider(),
@@ -44,9 +47,10 @@ export function createBlockProposalHandler(
44
47
  export function createValidatorClient(
45
48
  config: ValidatorClientFullConfig,
46
49
  deps: {
47
- blockBuilder: IFullNodeBlockBuilder;
50
+ checkpointsBuilder: FullNodeCheckpointsBuilder;
51
+ worldState: WorldStateSynchronizer;
48
52
  p2pClient: P2PClient;
49
- blockSource: L2BlockSource;
53
+ blockSource: L2BlockSource & L2BlockSink;
50
54
  l1ToL2MessageSource: L1ToL2MessageSource;
51
55
  telemetry: TelemetryClient;
52
56
  dateProvider: DateProvider;
@@ -62,7 +66,8 @@ export function createValidatorClient(
62
66
  const txProvider = deps.p2pClient.getTxProvider();
63
67
  return ValidatorClient.new(
64
68
  config,
65
- deps.blockBuilder,
69
+ deps.checkpointsBuilder,
70
+ deps.worldState,
66
71
  deps.epochCache,
67
72
  deps.p2pClient,
68
73
  deps.blockSource,
package/src/index.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from './block_proposal_handler.js';
2
+ export * from './checkpoint_builder.js';
2
3
  export * from './config.js';
3
4
  export * from './factory.js';
4
5
  export * from './validator.js';
5
6
  export * from './key_store/index.js';
7
+ export * from './tx_validator/index.js';
@@ -0,0 +1,2 @@
1
+ export * from './nullifier_cache.js';
2
+ export * from './tx_validator_factory.js';
@@ -0,0 +1,30 @@
1
+ import type { NullifierSource } from '@aztec/p2p';
2
+ import type { MerkleTreeReadOperations } from '@aztec/stdlib/interfaces/server';
3
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
4
+
5
+ /**
6
+ * Implements a nullifier source by checking a DB and an in-memory collection.
7
+ * Intended for validating transactions as they are added to a block.
8
+ */
9
+ export class NullifierCache implements NullifierSource {
10
+ nullifiers: Set<string>;
11
+
12
+ constructor(private db: MerkleTreeReadOperations) {
13
+ this.nullifiers = new Set();
14
+ }
15
+
16
+ public async nullifiersExist(nullifiers: Buffer[]): Promise<boolean[]> {
17
+ const cacheResults = nullifiers.map(n => this.nullifiers.has(n.toString()));
18
+ const toCheckDb = nullifiers.filter((_n, index) => !cacheResults[index]);
19
+ const dbHits = await this.db.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, toCheckDb);
20
+
21
+ let dbIndex = 0;
22
+ return nullifiers.map((_n, index) => cacheResults[index] || dbHits[dbIndex++] !== undefined);
23
+ }
24
+
25
+ public addNullifiers(nullifiers: Buffer[]) {
26
+ for (const nullifier of nullifiers) {
27
+ this.nullifiers.add(nullifier.toString());
28
+ }
29
+ }
30
+ }
@@ -0,0 +1,133 @@
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
4
+ import {
5
+ AggregateTxValidator,
6
+ ArchiveCache,
7
+ BlockHeaderTxValidator,
8
+ DataTxValidator,
9
+ DoubleSpendTxValidator,
10
+ GasTxValidator,
11
+ MetadataTxValidator,
12
+ PhasesTxValidator,
13
+ TimestampTxValidator,
14
+ TxPermittedValidator,
15
+ TxProofValidator,
16
+ } from '@aztec/p2p';
17
+ import { ProtocolContractAddress, protocolContractsHash } from '@aztec/protocol-contracts';
18
+ import type { ContractDataSource } from '@aztec/stdlib/contract';
19
+ import type { GasFees } from '@aztec/stdlib/gas';
20
+ import type {
21
+ AllowedElement,
22
+ ClientProtocolCircuitVerifier,
23
+ MerkleTreeReadOperations,
24
+ PublicProcessorValidator,
25
+ } from '@aztec/stdlib/interfaces/server';
26
+ import { DatabasePublicStateSource, type PublicStateSource } from '@aztec/stdlib/trees';
27
+ import { GlobalVariables, type Tx, type TxValidator } from '@aztec/stdlib/tx';
28
+ import type { UInt64 } from '@aztec/stdlib/types';
29
+
30
+ import { NullifierCache } from './nullifier_cache.js';
31
+
32
+ export function createValidatorForAcceptingTxs(
33
+ db: MerkleTreeReadOperations,
34
+ contractDataSource: ContractDataSource,
35
+ verifier: ClientProtocolCircuitVerifier | undefined,
36
+ {
37
+ l1ChainId,
38
+ rollupVersion,
39
+ setupAllowList,
40
+ gasFees,
41
+ skipFeeEnforcement,
42
+ timestamp,
43
+ blockNumber,
44
+ txsPermitted,
45
+ }: {
46
+ l1ChainId: number;
47
+ rollupVersion: number;
48
+ setupAllowList: AllowedElement[];
49
+ gasFees: GasFees;
50
+ skipFeeEnforcement?: boolean;
51
+ timestamp: UInt64;
52
+ blockNumber: BlockNumber;
53
+ txsPermitted: boolean;
54
+ },
55
+ ): TxValidator<Tx> {
56
+ const validators: TxValidator<Tx>[] = [
57
+ new TxPermittedValidator(txsPermitted),
58
+ new DataTxValidator(),
59
+ new MetadataTxValidator({
60
+ l1ChainId: new Fr(l1ChainId),
61
+ rollupVersion: new Fr(rollupVersion),
62
+ protocolContractsHash,
63
+ vkTreeRoot: getVKTreeRoot(),
64
+ }),
65
+ new TimestampTxValidator({
66
+ timestamp,
67
+ blockNumber,
68
+ }),
69
+ new DoubleSpendTxValidator(new NullifierCache(db)),
70
+ new PhasesTxValidator(contractDataSource, setupAllowList, timestamp),
71
+ new BlockHeaderTxValidator(new ArchiveCache(db)),
72
+ ];
73
+
74
+ if (!skipFeeEnforcement) {
75
+ validators.push(new GasTxValidator(new DatabasePublicStateSource(db), ProtocolContractAddress.FeeJuice, gasFees));
76
+ }
77
+
78
+ if (verifier) {
79
+ validators.push(new TxProofValidator(verifier));
80
+ }
81
+
82
+ return new AggregateTxValidator(...validators);
83
+ }
84
+
85
+ export function createValidatorForBlockBuilding(
86
+ db: MerkleTreeReadOperations,
87
+ contractDataSource: ContractDataSource,
88
+ globalVariables: GlobalVariables,
89
+ setupAllowList: AllowedElement[],
90
+ ): PublicProcessorValidator {
91
+ const nullifierCache = new NullifierCache(db);
92
+ const archiveCache = new ArchiveCache(db);
93
+ const publicStateSource = new DatabasePublicStateSource(db);
94
+
95
+ return {
96
+ preprocessValidator: preprocessValidator(
97
+ nullifierCache,
98
+ archiveCache,
99
+ publicStateSource,
100
+ contractDataSource,
101
+ globalVariables,
102
+ setupAllowList,
103
+ ),
104
+ nullifierCache,
105
+ };
106
+ }
107
+
108
+ function preprocessValidator(
109
+ nullifierCache: NullifierCache,
110
+ archiveCache: ArchiveCache,
111
+ publicStateSource: PublicStateSource,
112
+ contractDataSource: ContractDataSource,
113
+ globalVariables: GlobalVariables,
114
+ setupAllowList: AllowedElement[],
115
+ ): TxValidator<Tx> {
116
+ // We don't include the TxProofValidator nor the DataTxValidator here because they are already checked by the time we get to block building.
117
+ return new AggregateTxValidator(
118
+ new MetadataTxValidator({
119
+ l1ChainId: globalVariables.chainId,
120
+ rollupVersion: globalVariables.version,
121
+ protocolContractsHash,
122
+ vkTreeRoot: getVKTreeRoot(),
123
+ }),
124
+ new TimestampTxValidator({
125
+ timestamp: globalVariables.timestamp,
126
+ blockNumber: globalVariables.blockNumber,
127
+ }),
128
+ new DoubleSpendTxValidator(nullifierCache),
129
+ new PhasesTxValidator(contractDataSource, setupAllowList, globalVariables.timestamp),
130
+ new GasTxValidator(publicStateSource, ProtocolContractAddress.FeeJuice, globalVariables.gasFees),
131
+ new BlockHeaderTxValidator(archiveCache),
132
+ );
133
+ }