@aztec/p2p 4.0.0-rc.5 → 4.0.0-rc.7

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 (92) hide show
  1. package/dest/client/factory.d.ts +1 -1
  2. package/dest/client/factory.d.ts.map +1 -1
  3. package/dest/client/factory.js +8 -20
  4. package/dest/client/interface.d.ts +6 -11
  5. package/dest/client/interface.d.ts.map +1 -1
  6. package/dest/client/p2p_client.d.ts +4 -11
  7. package/dest/client/p2p_client.d.ts.map +1 -1
  8. package/dest/client/p2p_client.js +5 -57
  9. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.js +1 -2
  10. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts +1 -1
  11. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts.map +1 -1
  12. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.js +2 -0
  13. package/dest/mem_pools/tx_pool_v2/index.d.ts +2 -2
  14. package/dest/mem_pools/tx_pool_v2/index.d.ts.map +1 -1
  15. package/dest/mem_pools/tx_pool_v2/index.js +1 -1
  16. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +5 -5
  17. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
  18. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +26 -4
  19. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
  20. package/dest/mem_pools/tx_pool_v2/tx_metadata.js +48 -7
  21. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts +1 -1
  22. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts.map +1 -1
  23. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.js +9 -10
  24. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +2 -2
  25. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -1
  26. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +2 -2
  27. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
  28. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +37 -28
  29. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +2 -2
  30. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
  31. package/dest/msg_validators/tx_validator/aggregate_tx_validator.js +3 -3
  32. package/dest/msg_validators/tx_validator/factory.d.ts +114 -6
  33. package/dest/msg_validators/tx_validator/factory.d.ts.map +1 -1
  34. package/dest/msg_validators/tx_validator/factory.js +219 -58
  35. package/dest/msg_validators/tx_validator/gas_validator.d.ts +58 -3
  36. package/dest/msg_validators/tx_validator/gas_validator.d.ts.map +1 -1
  37. package/dest/msg_validators/tx_validator/gas_validator.js +72 -35
  38. package/dest/msg_validators/tx_validator/index.d.ts +2 -1
  39. package/dest/msg_validators/tx_validator/index.d.ts.map +1 -1
  40. package/dest/msg_validators/tx_validator/index.js +1 -0
  41. package/dest/msg_validators/tx_validator/nullifier_cache.d.ts +14 -0
  42. package/dest/msg_validators/tx_validator/nullifier_cache.d.ts.map +1 -0
  43. package/dest/msg_validators/tx_validator/nullifier_cache.js +24 -0
  44. package/dest/services/dummy_service.d.ts +4 -4
  45. package/dest/services/dummy_service.d.ts.map +1 -1
  46. package/dest/services/dummy_service.js +4 -4
  47. package/dest/services/encoding.d.ts +2 -2
  48. package/dest/services/encoding.d.ts.map +1 -1
  49. package/dest/services/encoding.js +7 -7
  50. package/dest/services/libp2p/libp2p_service.d.ts +11 -7
  51. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  52. package/dest/services/libp2p/libp2p_service.js +60 -70
  53. package/dest/services/reqresp/batch-tx-requester/tx_validator.js +2 -2
  54. package/dest/services/service.d.ts +5 -3
  55. package/dest/services/service.d.ts.map +1 -1
  56. package/dest/services/tx_provider.d.ts +3 -3
  57. package/dest/services/tx_provider.d.ts.map +1 -1
  58. package/dest/services/tx_provider.js +4 -4
  59. package/dest/test-helpers/mock-pubsub.d.ts +3 -2
  60. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  61. package/dest/test-helpers/mock-pubsub.js +6 -0
  62. package/dest/test-helpers/testbench-utils.d.ts +2 -2
  63. package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
  64. package/dest/util.d.ts +2 -2
  65. package/dest/util.d.ts.map +1 -1
  66. package/package.json +14 -14
  67. package/src/client/factory.ts +13 -33
  68. package/src/client/interface.ts +12 -11
  69. package/src/client/p2p_client.ts +7 -76
  70. package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker.ts +1 -1
  71. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.ts +3 -0
  72. package/src/mem_pools/tx_pool_v2/index.ts +1 -1
  73. package/src/mem_pools/tx_pool_v2/interfaces.ts +4 -4
  74. package/src/mem_pools/tx_pool_v2/tx_metadata.ts +65 -10
  75. package/src/mem_pools/tx_pool_v2/tx_pool_indices.ts +11 -11
  76. package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +1 -1
  77. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +42 -28
  78. package/src/msg_validators/tx_validator/README.md +115 -0
  79. package/src/msg_validators/tx_validator/aggregate_tx_validator.ts +3 -3
  80. package/src/msg_validators/tx_validator/factory.ts +353 -77
  81. package/src/msg_validators/tx_validator/gas_validator.ts +84 -29
  82. package/src/msg_validators/tx_validator/index.ts +1 -0
  83. package/src/msg_validators/tx_validator/nullifier_cache.ts +30 -0
  84. package/src/services/dummy_service.ts +6 -6
  85. package/src/services/encoding.ts +5 -6
  86. package/src/services/libp2p/libp2p_service.ts +74 -79
  87. package/src/services/reqresp/batch-tx-requester/tx_validator.ts +2 -2
  88. package/src/services/service.ts +11 -2
  89. package/src/services/tx_provider.ts +2 -2
  90. package/src/test-helpers/mock-pubsub.ts +10 -0
  91. package/src/test-helpers/testbench-utils.ts +1 -1
  92. package/src/util.ts +7 -1
@@ -187,6 +187,30 @@ export class TxPoolV2Impl {
187
187
  const errors = new Map<string, TxPoolRejectionError>();
188
188
  const acceptedPending = new Set<string>();
189
189
 
190
+ // Phase 1: Pre-compute all throwable I/O outside the transaction.
191
+ // If any pre-computation throws, the entire call fails before mutations happen.
192
+ const precomputed = new Map<string, { meta: TxMetaData; minedBlockId: L2BlockId | undefined; isValid: boolean }>();
193
+
194
+ const validator = await this.#createTxValidator();
195
+
196
+ for (const tx of txs) {
197
+ const txHash = tx.getTxHash();
198
+ const txHashStr = txHash.toString();
199
+
200
+ const meta = await buildTxMetaData(tx);
201
+ const minedBlockId = await this.#getMinedBlockId(txHash);
202
+
203
+ // Validate non-mined txs (mined and pre-protected txs bypass validation inside the transaction)
204
+ let isValid = true;
205
+ if (!minedBlockId) {
206
+ isValid = await this.#validateMeta(meta, validator);
207
+ }
208
+
209
+ precomputed.set(txHashStr, { meta, minedBlockId, isValid });
210
+ }
211
+
212
+ // Phase 2: Apply mutations inside the transaction using only pre-computed results,
213
+ // in-memory reads, and buffered DB writes. Nothing here can throw an unhandled exception.
190
214
  const poolAccess = this.#createPreAddPoolAccess();
191
215
  const preAddContext: PreAddContext | undefined =
192
216
  opts.feeComparisonOnly !== undefined ? { feeComparisonOnly: opts.feeComparisonOnly } : undefined;
@@ -202,22 +226,25 @@ export class TxPoolV2Impl {
202
226
  continue;
203
227
  }
204
228
 
205
- // Check mined status first (applies to all paths)
206
- const minedBlockId = await this.#getMinedBlockId(txHash);
229
+ const { meta, minedBlockId, isValid } = precomputed.get(txHashStr)!;
207
230
  const preProtectedSlot = this.#indices.getProtectionSlot(txHashStr);
208
231
 
209
232
  if (minedBlockId) {
210
233
  // Already mined - add directly (protection already set if pre-protected)
211
- await this.#addTx(tx, { mined: minedBlockId }, opts);
234
+ await this.#addTx(tx, { mined: minedBlockId }, opts, meta);
212
235
  accepted.push(txHash);
213
236
  } else if (preProtectedSlot !== undefined) {
214
237
  // Pre-protected and not mined - add as protected (bypass validation)
215
- await this.#addTx(tx, { protected: preProtectedSlot }, opts);
238
+ await this.#addTx(tx, { protected: preProtectedSlot }, opts, meta);
216
239
  accepted.push(txHash);
240
+ } else if (!isValid) {
241
+ // Failed pre-computed validation
242
+ rejected.push(txHash);
217
243
  } else {
218
- // Regular pending tx - validate and run pre-add rules
244
+ // Regular pending tx - run pre-add rules using pre-computed metadata
219
245
  const result = await this.#tryAddRegularPendingTx(
220
246
  tx,
247
+ meta,
221
248
  opts,
222
249
  poolAccess,
223
250
  acceptedPending,
@@ -227,8 +254,6 @@ export class TxPoolV2Impl {
227
254
  );
228
255
  if (result.status === 'accepted') {
229
256
  acceptedPending.add(txHashStr);
230
- } else if (result.status === 'rejected') {
231
- rejected.push(txHash);
232
257
  } else {
233
258
  ignored.push(txHash);
234
259
  }
@@ -259,27 +284,21 @@ export class TxPoolV2Impl {
259
284
  return { accepted, ignored, rejected, ...(errors.size > 0 ? { errors } : {}) };
260
285
  }
261
286
 
262
- /** Validates and adds a regular pending tx. Returns status. */
287
+ /** Adds a validated pending tx, running pre-add rules and evicting conflicts. */
263
288
  async #tryAddRegularPendingTx(
264
289
  tx: Tx,
290
+ precomputedMeta: TxMetaData,
265
291
  opts: { source?: string },
266
292
  poolAccess: PreAddPoolAccess,
267
293
  acceptedPending: Set<string>,
268
294
  ignored: TxHash[],
269
295
  errors: Map<string, TxPoolRejectionError>,
270
296
  preAddContext?: PreAddContext,
271
- ): Promise<{ status: 'accepted' | 'ignored' | 'rejected' }> {
272
- const txHash = tx.getTxHash();
273
- const txHashStr = txHash.toString();
274
-
275
- // Build metadata and validate using metadata
276
- const meta = await buildTxMetaData(tx);
277
- if (!(await this.#validateMeta(meta))) {
278
- return { status: 'rejected' };
279
- }
297
+ ): Promise<{ status: 'accepted' | 'ignored' }> {
298
+ const txHashStr = tx.getTxHash().toString();
280
299
 
281
300
  // Run pre-add rules
282
- const preAddResult = await this.#evictionManager.runPreAddRules(meta, poolAccess, preAddContext);
301
+ const preAddResult = await this.#evictionManager.runPreAddRules(precomputedMeta, poolAccess, preAddContext);
283
302
 
284
303
  if (preAddResult.shouldIgnore) {
285
304
  this.#log.debug(`Ignoring tx ${txHashStr}: ${preAddResult.reason?.message ?? 'unknown reason'}`);
@@ -317,11 +336,11 @@ export class TxPoolV2Impl {
317
336
  }
318
337
 
319
338
  // Add the transaction
320
- await this.#addTx(tx, 'pending', opts);
339
+ await this.#addTx(tx, 'pending', opts, precomputedMeta);
321
340
  return { status: 'accepted' };
322
341
  }
323
342
 
324
- async canAddPendingTx(tx: Tx): Promise<'accepted' | 'ignored' | 'rejected'> {
343
+ async canAddPendingTx(tx: Tx): Promise<'accepted' | 'ignored'> {
325
344
  const txHashStr = tx.getTxHash().toString();
326
345
 
327
346
  // Check if already in pool
@@ -329,14 +348,8 @@ export class TxPoolV2Impl {
329
348
  return 'ignored';
330
349
  }
331
350
 
332
- // Build metadata and validate using metadata
351
+ // Build metadata and check pre-add rules
333
352
  const meta = await buildTxMetaData(tx);
334
- const validationResult = await this.#validateMeta(meta, undefined, 'can add pending');
335
- if (validationResult !== true) {
336
- return 'rejected';
337
- }
338
-
339
- // Use pre-add rules
340
353
  const poolAccess = this.#createPreAddPoolAccess();
341
354
  const preAddResult = await this.#evictionManager.runPreAddRules(meta, poolAccess);
342
355
 
@@ -765,9 +778,10 @@ export class TxPoolV2Impl {
765
778
  tx: Tx,
766
779
  state: 'pending' | { protected: SlotNumber } | { mined: L2BlockId },
767
780
  opts: { source?: string } = {},
781
+ precomputedMeta?: TxMetaData,
768
782
  ): Promise<TxMetaData> {
769
783
  const txHashStr = tx.getTxHash().toString();
770
- const meta = await buildTxMetaData(tx);
784
+ const meta = precomputedMeta ?? (await buildTxMetaData(tx));
771
785
  meta.receivedAt = this.#dateProvider.now();
772
786
 
773
787
  await this.#txsDB.set(txHashStr, tx.toBuffer());
@@ -0,0 +1,115 @@
1
+ # Transaction Validation
2
+
3
+ This module defines the transaction validators and the factory functions that assemble them for each entry point into the system.
4
+
5
+ ## Validation Strategy
6
+
7
+ Transactions enter the system through different paths. **Unsolicited** transactions (gossip and RPC) are fully validated before acceptance. **Solicited** transactions (req/resp and block proposals) are only checked for well-formedness because we must store them for block re-execution — they may ultimately be invalid, which is caught during block building and reported as part of block validation/attestation.
8
+
9
+ When solicited transactions fail to be mined, they may be migrated to the pending pool. At that point, the pool runs the state-dependent checks that were skipped on initial receipt.
10
+
11
+ ## Entry Points
12
+
13
+ ### 1. Gossip (libp2p pubsub)
14
+
15
+ **Factory**: `createFirstStageTxValidationsForGossipedTransactions` + `createSecondStageTxValidationsForGossipedTransactions`
16
+ **Called from**: `LibP2PService.handleGossipedTx()` in `libp2p_service.ts`
17
+
18
+ Unsolicited transactions from any peer. Fully validated in two stages with a pool pre-check in between to avoid wasting CPU on proof verification for transactions the pool would reject:
19
+
20
+ | Step | What runs | On failure |
21
+ |------|-----------|------------|
22
+ | **Stage 1** (fast) | TxPermitted, Data, Metadata, Timestamp, DoubleSpend, Gas, Phases, BlockHeader | Penalize peer, reject tx |
23
+ | **Pool pre-check** | `canAddPendingTx` — checks for duplicates, pool capacity | Ignore tx (no penalty) |
24
+ | **Stage 2** (slow) | Proof verification | Penalize peer, reject tx |
25
+ | **Pool add** | `addPendingTxs` | Accept, ignore, or reject |
26
+
27
+ Each stage-1 and stage-2 validator is paired with a `PeerErrorSeverity`. If a validator fails, the sending peer is penalized with that severity. The `doubleSpendValidator` has special handling: its severity is determined by how recently the nullifier appeared (recent = high tolerance, old = low tolerance).
28
+
29
+ ### 2. JSON-RPC
30
+
31
+ **Factory**: `createTxValidatorForAcceptingTxsOverRPC`
32
+ **Called from**: `AztecNodeService.isValidTx()` in `aztec-node/server.ts`
33
+
34
+ Unsolicited transactions from a local wallet/PXE. Runs the full set of checks as a single aggregate validator:
35
+
36
+ - TxPermitted, Size, Data, Metadata, Timestamp, DoubleSpend, Phases, BlockHeader
37
+ - Gas (optional — skipped when `skipFeeEnforcement` is set)
38
+ - Proof verification (optional — skipped for simulations when no verifier is provided)
39
+
40
+ ### 3. Req/resp and block proposals
41
+
42
+ **Factories**: `createTxValidatorForReqResponseReceivedTxs`, `createTxValidatorForBlockProposalReceivedTxs`
43
+ **Called from**: `LibP2PService.validateRequestedTx()`, `LibP2PService.validateTxsReceivedInBlockProposal()`, and `BatchRequestTxValidator` in `batch-tx-requester/tx_validator.ts`
44
+
45
+ Solicited transactions — we requested these from peers or received them as part of a block proposal we need to validate. We must accept them for re-execution even if they are invalid against the current state. Only well-formedness is checked:
46
+
47
+ - Metadata, Size, Data, Proof
48
+
49
+ State-dependent checks are deferred to either the block building validator (for txs included in blocks) or the pending pool migration validator (for unmined txs migrating to pending).
50
+
51
+ ### 4. Block building
52
+
53
+ **Factory**: `createTxValidatorForBlockBuilding`
54
+ **Called from**: `CheckpointBuilder.makeBlockBuilderDeps()` in `validator-client/checkpoint_builder.ts`
55
+
56
+ Transactions already in the pool, about to be sequenced into a block. Re-validates against the current state of the block being built. **This is where invalid txs that entered via req/resp or block proposals are caught** — their invalidity is reported as part of block validation/attestation.
57
+
58
+ Runs:
59
+ - Timestamp, DoubleSpend, Phases, Gas, BlockHeader
60
+
61
+ Does **not** run:
62
+ - Proof, Data — already verified on entry (by gossip, RPC, or req/resp validators)
63
+
64
+ ### 5. Pending pool migration
65
+
66
+ **Factory**: `createTxValidatorForTransactionsEnteringPendingTxPool`
67
+ **Called from**: `TxPoolV2Impl` (injected as the `createTxValidator` factory via `TxPoolV2Dependencies`)
68
+
69
+ When transactions that arrived via req/resp or block proposals fail to be mined, they may need to be included in our pending pool. These txs only had well-formedness checks on receipt, so the pool runs the state-dependent checks they missed before accepting them.
70
+
71
+ This validator is invoked on **every** transaction potentially entering the pending pool:
72
+ - `addPendingTxs` — validating each tx before adding
73
+ - `prepareForSlot` — unprotecting txs back to pending after a slot ends
74
+ - `handlePrunedBlocks` — unmining txs from pruned blocks back to pending
75
+ - Startup hydration — revalidating persisted non-mined txs on node restart
76
+
77
+ Runs:
78
+ - DoubleSpend, BlockHeader, GasLimits, Timestamp
79
+
80
+ Operates on `TxMetaData` (pre-built by the pool) rather than full `Tx` objects.
81
+
82
+ ## Individual Validators
83
+
84
+ | Validator | What it checks | Benchmarked verification duration |
85
+ |-----------|---------------|---------------|
86
+ | `TxPermittedValidator` | Whether the system is accepting transactions (controlled by config flag) | 1.56 us |
87
+ | `DataTxValidator` | Transaction data integrity — correct structure, non-empty fields | 4.10–18.18 ms |
88
+ | `SizeTxValidator` | Transaction does not exceed maximum size limits | 2.28 us |
89
+ | `MetadataTxValidator` | Chain ID, rollup version, protocol contracts hash, VK tree root | 4.18 us |
90
+ | `TimestampTxValidator` | Transaction has not expired (expiration timestamp vs next slot) | 1.56 us |
91
+ | `DoubleSpendTxValidator` | Nullifiers do not already exist in the nullifier tree | 106.08 us |
92
+ | `GasTxValidator` | Gas limits are within bounds (delegates to `GasLimitsValidator`), max fee per gas meets current block fees, and fee payer has sufficient FeeJuice balance | 1.02 ms |
93
+ | `GasLimitsValidator` | Gas limits are >= fixed minimums and <= AVM max processable L2 gas. Used standalone in pool migration; also called internally by `GasTxValidator` | 3–10 us |
94
+ | `PhasesTxValidator` | Public function calls in setup phase are on the allow list | 10.12–13.12 us |
95
+ | `BlockHeaderTxValidator` | Transaction's anchor block hash exists in the archive tree | 98.88 us |
96
+ | `TxProofValidator` | Client proof verifies correctly | ~250ms |
97
+
98
+ ## Validator Coverage by Entry Point
99
+
100
+ | Validator | Gossip | RPC | Req/resp | Block building | Pool migration |
101
+ |-----------|--------|-----|----------|----------------|----------------|
102
+ | TxPermitted | Stage 1 | Yes | — | — | — |
103
+ | Data | Stage 1 | Yes | Yes | — | — |
104
+ | Size | — | Yes | Yes | — | — |
105
+ | Metadata | Stage 1 | Yes | Yes | — | — |
106
+ | Timestamp | Stage 1 | Yes | — | Yes | Yes |
107
+ | DoubleSpend | Stage 1 | Yes | — | Yes | Yes |
108
+ | Gas (balance + limits) | Stage 1 | Optional* | — | Yes | — |
109
+ | GasLimits (standalone) | — | — | — | — | Yes |
110
+ | Phases | Stage 1 | Yes | — | Yes | — |
111
+ | BlockHeader | Stage 1 | Yes | — | Yes | Yes |
112
+ | Proof | Stage 2 | Optional** | Yes | — | — |
113
+
114
+ \* Gas balance check is skipped when `skipFeeEnforcement` is set (testing/dev). `GasTxValidator` internally delegates to `GasLimitsValidator` as its first step, so gas limits are checked wherever `GasTxValidator` runs. Pool migration uses `GasLimitsValidator` standalone because it doesn't need the balance or fee-per-gas checks.
115
+ \** Proof verification is skipped for simulations (no verifier provided).
@@ -1,18 +1,18 @@
1
1
  import type { TxValidationResult, TxValidator } from '@aztec/stdlib/tx';
2
2
 
3
3
  export class AggregateTxValidator<T> implements TxValidator<T> {
4
- #validators: TxValidator<T>[];
4
+ readonly validators: TxValidator<T>[];
5
5
  constructor(...validators: TxValidator<T>[]) {
6
6
  if (validators.length === 0) {
7
7
  throw new Error('At least one validator must be provided');
8
8
  }
9
9
 
10
- this.#validators = validators;
10
+ this.validators = validators;
11
11
  }
12
12
 
13
13
  async validateTx(tx: T): Promise<TxValidationResult> {
14
14
  const aggregate: { result: string; reason?: string[] } = { result: 'valid', reason: [] };
15
- for (const validator of this.#validators) {
15
+ for (const validator of this.validators) {
16
16
  const result = await validator.validateTx(tx);
17
17
  if (result.result === 'invalid') {
18
18
  aggregate.result = 'invalid';