@aztec/p2p 0.0.1-commit.f2ce05ee → 0.0.1-commit.f8ca9b2f3

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 (163) hide show
  1. package/dest/client/factory.d.ts +3 -3
  2. package/dest/client/factory.d.ts.map +1 -1
  3. package/dest/client/factory.js +38 -7
  4. package/dest/client/interface.d.ts +26 -15
  5. package/dest/client/interface.d.ts.map +1 -1
  6. package/dest/client/p2p_client.d.ts +31 -35
  7. package/dest/client/p2p_client.d.ts.map +1 -1
  8. package/dest/client/p2p_client.js +97 -138
  9. package/dest/config.d.ts +10 -2
  10. package/dest/config.d.ts.map +1 -1
  11. package/dest/config.js +9 -0
  12. package/dest/index.d.ts +2 -1
  13. package/dest/index.d.ts.map +1 -1
  14. package/dest/index.js +1 -0
  15. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +18 -11
  16. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  17. package/dest/mem_pools/attestation_pool/attestation_pool.js +62 -37
  18. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +1 -1
  19. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  20. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +53 -53
  21. package/dest/mem_pools/attestation_pool/index.d.ts +2 -2
  22. package/dest/mem_pools/attestation_pool/index.d.ts.map +1 -1
  23. package/dest/mem_pools/attestation_pool/index.js +1 -1
  24. package/dest/mem_pools/index.d.ts +2 -1
  25. package/dest/mem_pools/index.d.ts.map +1 -1
  26. package/dest/mem_pools/interface.d.ts +3 -3
  27. package/dest/mem_pools/interface.d.ts.map +1 -1
  28. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts +87 -0
  29. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts.map +1 -0
  30. package/dest/mem_pools/tx_pool_v2/deleted_pool.js +180 -0
  31. package/dest/mem_pools/tx_pool_v2/index.d.ts +2 -1
  32. package/dest/mem_pools/tx_pool_v2/index.d.ts.map +1 -1
  33. package/dest/mem_pools/tx_pool_v2/index.js +1 -0
  34. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +3 -3
  35. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
  36. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +25 -3
  37. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
  38. package/dest/mem_pools/tx_pool_v2/tx_metadata.js +36 -4
  39. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +1 -1
  40. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
  41. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +77 -74
  42. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +3 -3
  43. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
  44. package/dest/msg_validators/tx_validator/block_header_validator.d.ts +16 -3
  45. package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
  46. package/dest/msg_validators/tx_validator/block_header_validator.js +1 -1
  47. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts +13 -3
  48. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts.map +1 -1
  49. package/dest/msg_validators/tx_validator/double_spend_validator.js +4 -4
  50. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +20 -4
  51. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -1
  52. package/dest/msg_validators/tx_validator/timestamp_validator.js +2 -2
  53. package/dest/services/dummy_service.d.ts +6 -2
  54. package/dest/services/dummy_service.d.ts.map +1 -1
  55. package/dest/services/dummy_service.js +3 -0
  56. package/dest/services/encoding.d.ts +2 -2
  57. package/dest/services/encoding.d.ts.map +1 -1
  58. package/dest/services/encoding.js +2 -2
  59. package/dest/services/libp2p/libp2p_service.d.ts +11 -3
  60. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  61. package/dest/services/libp2p/libp2p_service.js +71 -35
  62. package/dest/services/reqresp/interface.d.ts +10 -1
  63. package/dest/services/reqresp/interface.d.ts.map +1 -1
  64. package/dest/services/reqresp/interface.js +15 -1
  65. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +3 -3
  66. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -1
  67. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +7 -1
  68. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
  69. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +15 -0
  70. package/dest/services/reqresp/protocols/tx.d.ts +7 -1
  71. package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
  72. package/dest/services/reqresp/protocols/tx.js +20 -0
  73. package/dest/services/reqresp/reqresp.d.ts +1 -1
  74. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  75. package/dest/services/reqresp/reqresp.js +11 -4
  76. package/dest/services/service.d.ts +18 -1
  77. package/dest/services/service.d.ts.map +1 -1
  78. package/dest/services/tx_collection/config.d.ts +7 -1
  79. package/dest/services/tx_collection/config.d.ts.map +1 -1
  80. package/dest/services/tx_collection/config.js +16 -0
  81. package/dest/services/tx_collection/fast_tx_collection.d.ts +3 -1
  82. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
  83. package/dest/services/tx_collection/fast_tx_collection.js +17 -3
  84. package/dest/services/tx_collection/file_store_tx_collection.d.ts +44 -0
  85. package/dest/services/tx_collection/file_store_tx_collection.d.ts.map +1 -0
  86. package/dest/services/tx_collection/file_store_tx_collection.js +118 -0
  87. package/dest/services/tx_collection/file_store_tx_source.d.ts +27 -0
  88. package/dest/services/tx_collection/file_store_tx_source.d.ts.map +1 -0
  89. package/dest/services/tx_collection/file_store_tx_source.js +57 -0
  90. package/dest/services/tx_collection/index.d.ts +2 -1
  91. package/dest/services/tx_collection/index.d.ts.map +1 -1
  92. package/dest/services/tx_collection/index.js +1 -0
  93. package/dest/services/tx_collection/slow_tx_collection.d.ts +3 -1
  94. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -1
  95. package/dest/services/tx_collection/slow_tx_collection.js +48 -19
  96. package/dest/services/tx_collection/tx_collection.d.ts +17 -7
  97. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
  98. package/dest/services/tx_collection/tx_collection.js +58 -2
  99. package/dest/services/tx_collection/tx_collection_sink.d.ts +15 -6
  100. package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -1
  101. package/dest/services/tx_collection/tx_collection_sink.js +13 -7
  102. package/dest/services/tx_file_store/config.d.ts +1 -3
  103. package/dest/services/tx_file_store/config.d.ts.map +1 -1
  104. package/dest/services/tx_file_store/config.js +0 -4
  105. package/dest/services/tx_file_store/tx_file_store.d.ts +3 -3
  106. package/dest/services/tx_file_store/tx_file_store.d.ts.map +1 -1
  107. package/dest/services/tx_provider.d.ts +3 -3
  108. package/dest/services/tx_provider.d.ts.map +1 -1
  109. package/dest/services/tx_provider.js +5 -4
  110. package/dest/test-helpers/make-test-p2p-clients.d.ts +3 -3
  111. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  112. package/dest/test-helpers/mock-pubsub.d.ts +27 -1
  113. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  114. package/dest/test-helpers/mock-pubsub.js +97 -2
  115. package/dest/test-helpers/testbench-utils.d.ts +30 -24
  116. package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
  117. package/dest/test-helpers/testbench-utils.js +87 -35
  118. package/dest/testbench/p2p_client_testbench_worker.js +1 -1
  119. package/package.json +14 -14
  120. package/src/client/factory.ts +65 -8
  121. package/src/client/interface.ts +26 -13
  122. package/src/client/p2p_client.ts +123 -158
  123. package/src/config.ts +16 -0
  124. package/src/index.ts +1 -0
  125. package/src/mem_pools/attestation_pool/attestation_pool.ts +87 -44
  126. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +57 -53
  127. package/src/mem_pools/attestation_pool/index.ts +3 -3
  128. package/src/mem_pools/index.ts +3 -0
  129. package/src/mem_pools/interface.ts +2 -2
  130. package/src/mem_pools/tx_pool_v2/README.md +59 -9
  131. package/src/mem_pools/tx_pool_v2/deleted_pool.ts +234 -0
  132. package/src/mem_pools/tx_pool_v2/index.ts +1 -0
  133. package/src/mem_pools/tx_pool_v2/interfaces.ts +2 -2
  134. package/src/mem_pools/tx_pool_v2/tx_metadata.ts +51 -5
  135. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +90 -77
  136. package/src/msg_validators/tx_validator/aggregate_tx_validator.ts +2 -2
  137. package/src/msg_validators/tx_validator/block_header_validator.ts +15 -3
  138. package/src/msg_validators/tx_validator/double_spend_validator.ts +11 -6
  139. package/src/msg_validators/tx_validator/timestamp_validator.ts +19 -14
  140. package/src/services/dummy_service.ts +6 -0
  141. package/src/services/encoding.ts +2 -2
  142. package/src/services/libp2p/libp2p_service.ts +70 -37
  143. package/src/services/reqresp/interface.ts +26 -1
  144. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +2 -2
  145. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +17 -0
  146. package/src/services/reqresp/protocols/tx.ts +22 -0
  147. package/src/services/reqresp/reqresp.ts +13 -3
  148. package/src/services/service.ts +20 -0
  149. package/src/services/tx_collection/config.ts +26 -0
  150. package/src/services/tx_collection/fast_tx_collection.ts +14 -2
  151. package/src/services/tx_collection/file_store_tx_collection.ts +152 -0
  152. package/src/services/tx_collection/file_store_tx_source.ts +70 -0
  153. package/src/services/tx_collection/index.ts +1 -0
  154. package/src/services/tx_collection/slow_tx_collection.ts +55 -26
  155. package/src/services/tx_collection/tx_collection.ts +78 -12
  156. package/src/services/tx_collection/tx_collection_sink.ts +17 -7
  157. package/src/services/tx_file_store/config.ts +0 -6
  158. package/src/services/tx_file_store/tx_file_store.ts +4 -4
  159. package/src/services/tx_provider.ts +8 -7
  160. package/src/test-helpers/make-test-p2p-clients.ts +3 -3
  161. package/src/test-helpers/mock-pubsub.ts +133 -3
  162. package/src/test-helpers/testbench-utils.ts +100 -40
  163. package/src/testbench/p2p_client_testbench_worker.ts +1 -1
@@ -28,13 +28,21 @@ TxPoolV2 manages transactions through a state machine with clear transitions:
28
28
  │ MINED │───────────────────┘
29
29
  │ (included in a block) │ handlePrunedBlocks()
30
30
  └─────────────────────────────────────┘ (reorg)
31
-
32
- │ handleFinalizedBlock()
33
-
34
- ┌─────────────────────────────────────┐
35
- │ DELETED │
36
- │ (optionally archived)
37
- └─────────────────────────────────────┘
31
+
32
+ │ handleFinalizedBlock() │ eviction after reorg
33
+
34
+ ┌─────────────────────────────────────┐ ┌─────────────────────────────────────┐
35
+ │ DELETED │ │ SOFT-DELETED │
36
+ │ (hard-deleted or archived) │ (kept in DB for debugging) │
37
+ └─────────────────────────────────────┘ └─────────────────────────────────────┘
38
+
39
+ │ handleFinalizedBlock()
40
+ │ (mined block finalized)
41
+
42
+ ┌─────────────────────────────────────┐
43
+ │ HARD-DELETED │
44
+ │ (permanently removed from DB) │
45
+ └─────────────────────────────────────┘
38
46
  ```
39
47
 
40
48
  ## Key Components
@@ -52,6 +60,16 @@ Core implementation containing:
52
60
  - Pre-add rule execution
53
61
  - Post-event eviction rule execution
54
62
 
63
+ ### DeletedPool (`deleted_pool.ts`)
64
+
65
+ Manages soft deletion of transactions from pruned blocks:
66
+ - When a reorg (chain prune) occurs, transactions from pruned blocks are tracked with their original mined block number
67
+ - When these transactions are later evicted (e.g., failed validation, nullifier conflict), they are "soft-deleted" instead of removed
68
+ - Soft-deleted transactions remain in the database for debugging and potential resubmission
69
+ - When the original mined block is finalized on the new chain, soft-deleted transactions are permanently hard-deleted
70
+
71
+ This ensures transactions from reorged blocks are kept around until we're certain they won't be needed.
72
+
55
73
  ### TxMetaData (`tx_metadata.ts`)
56
74
 
57
75
  Lightweight metadata stored alongside each transaction:
@@ -68,8 +86,37 @@ Lightweight metadata stored alongside each transaction:
68
86
  State is derived by TxPoolIndices:
69
87
  - `mined` if `minedL2BlockId` is set
70
88
  - `protected` if in protection map
89
+ - `deleted` if soft-deleted (from a pruned block, evicted but kept in DB)
71
90
  - `pending` otherwise
72
91
 
92
+ ## Soft Deletion
93
+
94
+ When a chain reorganization occurs, transactions that were mined in pruned blocks are handled specially:
95
+
96
+ 1. **Tracking**: When `handlePrunedBlocks` is called, all un-mined transactions are tracked by their original mined block number
97
+ 2. **Soft Delete**: If these transactions are later evicted (failed validation, nullifier conflict, etc.), they are "soft-deleted" - removed from indices but kept in the database
98
+ 3. **Retrieval**: Soft-deleted transactions can still be retrieved via `getTxByHash` and `hasTxs`, and return status `'deleted'` from `getTxStatus`
99
+ 4. **Hard Delete**: When `handleFinalizedBlock` is called and the finalized block number reaches or exceeds the transaction's original mined block, the transaction is permanently removed
100
+
101
+ This design allows:
102
+ - Debugging reorg scenarios by keeping transaction data available
103
+ - Potential resubmission of transactions that failed validation after a reorg
104
+ - Clean eventual cleanup once we're certain the transaction won't be needed
105
+
106
+ **Example scenario:**
107
+ 1. Tx mined at block 10
108
+ 2. Chain prunes to block 5 (tx becomes un-mined, tracked as minedAtBlock=10)
109
+ 3. Tx fails validation and is soft-deleted
110
+ 4. Block 9 finalized → tx still in DB (minedAtBlock=10 > finalized=9)
111
+ 5. Block 10 finalized → tx hard-deleted (minedAtBlock=10 ≤ finalized=10)
112
+
113
+ If the tx is re-mined at a higher block before being soft-deleted:
114
+ 1. Tx mined at block 10, pruned (tracked as minedAtBlock=10)
115
+ 2. Tx re-mined at block 15, pruned again (updated to minedAtBlock=15)
116
+ 3. Tx soft-deleted
117
+ 4. Block 10 finalized → tx still in DB
118
+ 5. Block 15 finalized → tx hard-deleted
119
+
73
120
  ## Architecture: Pre-add vs Post-event Rules
74
121
 
75
122
  **Pre-add rules** (run during `addPendingTxs`):
@@ -120,7 +167,7 @@ import { AztecKVTxPoolV2 } from './tx_pool_v2.js';
120
167
  const pool = new AztecKVTxPoolV2(txStore, archiveStore, {
121
168
  l2BlockSource: archiver,
122
169
  worldStateSynchronizer: worldState,
123
- pendingTxValidator: validator,
170
+ createTxValidator: () => validator,
124
171
  });
125
172
 
126
173
  await pool.start();
@@ -195,9 +242,12 @@ The archive uses FIFO eviction when `archivedTxLimit` is reached.
195
242
  ## Testing
196
243
 
197
244
  ```bash
198
- # Unit tests (131 tests)
245
+ # Unit tests (177 tests)
199
246
  yarn test src/mem_pools/tx_pool_v2/tx_pool_v2.test.ts
200
247
 
248
+ # Deleted pool tests (17 tests)
249
+ yarn test src/mem_pools/tx_pool_v2/deleted_pool.test.ts
250
+
201
251
  # Compatibility tests (25 tests)
202
252
  yarn test src/mem_pools/tx_pool_v2/tx_pool_v2.compat.test.ts
203
253
 
@@ -0,0 +1,234 @@
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import type { Logger } from '@aztec/foundation/log';
3
+ import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
4
+
5
+ /**
6
+ * State stored for each transaction from a pruned block.
7
+ */
8
+ type DeletedTxState = {
9
+ /** Block number in which the transaction was originally mined (before being un-mined by a prune) */
10
+ minedAtBlock: BlockNumber;
11
+ /** Whether the transaction has been soft-deleted (removed from indices) */
12
+ softDeleted: boolean;
13
+ };
14
+
15
+ /**
16
+ * Serializes DeletedTxState to a Buffer.
17
+ * Format: 4 bytes for blockNumber (uint32) + 1 byte for softDeleted (0 or 1)
18
+ */
19
+ function serializeState(state: DeletedTxState): Buffer {
20
+ const buffer = Buffer.alloc(5);
21
+ buffer.writeUInt32BE(Number(state.minedAtBlock), 0);
22
+ buffer.writeUInt8(state.softDeleted ? 1 : 0, 4);
23
+ return buffer;
24
+ }
25
+
26
+ /**
27
+ * Deserializes a Buffer to DeletedTxState.
28
+ */
29
+ function deserializeState(buffer: Buffer): DeletedTxState {
30
+ return {
31
+ minedAtBlock: BlockNumber(buffer.readUInt32BE(0)),
32
+ softDeleted: buffer.readUInt8(4) === 1,
33
+ };
34
+ }
35
+
36
+ /**
37
+ * Manages all transaction deletions in the pool.
38
+ *
39
+ * When a chain prune (reorg) happens, transactions from pruned blocks are tracked here.
40
+ * This class is responsible for ALL deletion decisions:
41
+ *
42
+ * - Transactions from pruned blocks are "soft deleted" - removed from indices but kept
43
+ * in the database for later re-execution
44
+ * - Transactions NOT from pruned blocks are "hard deleted" - completely removed from DB
45
+ *
46
+ * When a block is finalized, soft-deleted transactions that were originally mined at or
47
+ * before that block number are permanently (hard) deleted.
48
+ */
49
+ export class DeletedPool {
50
+ /** Persisted map: txHash -> DeletedTxState (serialized) */
51
+ #deletedTxsDB: AztecAsyncMap<string, Buffer>;
52
+
53
+ /** Reference to the main txs database for hard deletion */
54
+ #txsDB: AztecAsyncMap<string, Buffer>;
55
+
56
+ /** In-memory state for transactions from pruned blocks */
57
+ #state: Map<string, DeletedTxState> = new Map();
58
+
59
+ #log: Logger;
60
+
61
+ constructor(store: AztecAsyncKVStore, txsDB: AztecAsyncMap<string, Buffer>, log: Logger) {
62
+ this.#deletedTxsDB = store.openMap('deleted_txs');
63
+ this.#txsDB = txsDB;
64
+ this.#log = log;
65
+ }
66
+
67
+ /**
68
+ * Loads state from the database on startup.
69
+ */
70
+ async hydrateFromDatabase(): Promise<void> {
71
+ let prunedCount = 0;
72
+ let softDeletedCount = 0;
73
+
74
+ for await (const [txHash, buffer] of this.#deletedTxsDB.entriesAsync()) {
75
+ const state = deserializeState(buffer);
76
+ this.#state.set(txHash, state);
77
+ prunedCount++;
78
+ if (state.softDeleted) {
79
+ softDeletedCount++;
80
+ }
81
+ }
82
+
83
+ if (prunedCount > 0 || softDeletedCount > 0) {
84
+ this.#log.info(`Loaded ${prunedCount} txs from pruned blocks, ${softDeletedCount} soft-deleted`);
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Marks transactions as being from a pruned block.
90
+ * Called during handlePrunedBlocks for ALL transactions that were un-mined.
91
+ *
92
+ * If a tx was previously tracked (e.g., mined at block 4, pruned, re-mined at block 5,
93
+ * pruned again), updates to the higher block number. This ensures the tx is kept until
94
+ * its most recent mined block is finalized.
95
+ *
96
+ * @param txs - Array of {txHash, minedAtBlock} pairs, where minedAtBlock is the block
97
+ * number in which the tx was mined before being un-mined
98
+ */
99
+ async markFromPrunedBlock(txs: { txHash: string; minedAtBlock: BlockNumber }[]): Promise<void> {
100
+ if (txs.length === 0) {
101
+ return;
102
+ }
103
+
104
+ let count = 0;
105
+ for (const { txHash, minedAtBlock } of txs) {
106
+ const existing = this.#state.get(txHash);
107
+ // Update if not tracked, or if this is a higher mined block (tx was re-mined then pruned again)
108
+ if (existing === undefined || minedAtBlock > existing.minedAtBlock) {
109
+ const state: DeletedTxState = {
110
+ minedAtBlock,
111
+ softDeleted: existing?.softDeleted ?? false,
112
+ };
113
+ this.#state.set(txHash, state);
114
+ await this.#deletedTxsDB.set(txHash, serializeState(state));
115
+ count++;
116
+ }
117
+ }
118
+
119
+ if (count > 0) {
120
+ this.#log.debug(`Marked ${count} transactions from pruned blocks`);
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Deletes a transaction. This is the single entry point for ALL deletions.
126
+ *
127
+ * - If the tx is from a pruned block: soft-delete (keep in DB, mark as deleted)
128
+ * - If the tx is NOT from a pruned block: hard-delete (remove from DB)
129
+ *
130
+ * @returns 'soft' if soft-deleted, 'hard' if hard-deleted
131
+ */
132
+ async deleteTx(txHash: string): Promise<'soft' | 'hard'> {
133
+ const existing = this.#state.get(txHash);
134
+ if (existing !== undefined) {
135
+ // Soft delete - keep in DB
136
+ const state: DeletedTxState = {
137
+ minedAtBlock: existing.minedAtBlock,
138
+ softDeleted: true,
139
+ };
140
+ this.#state.set(txHash, state);
141
+ await this.#deletedTxsDB.set(txHash, serializeState(state));
142
+ return 'soft';
143
+ } else {
144
+ // Hard delete - remove from DB
145
+ await this.#txsDB.delete(txHash);
146
+ return 'hard';
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Clears tracking for a transaction if it re-mines at a block number >= the tracked minedAtBlock.
152
+ *
153
+ * When a tx re-mines at a higher (or equal) block, the old high-water mark is no longer needed:
154
+ * any future prune would re-add the tx with an even higher block number. Clearing keeps
155
+ * DeletedPool consistent — only txs that are actually un-mined should be tracked here.
156
+ *
157
+ * When a tx re-mines at a lower block, we must preserve the existing entry to retain
158
+ * the high-water mark for re-execution purposes.
159
+ */
160
+ async clearIfMinedHigher(txHash: string, minedAtBlock: BlockNumber): Promise<void> {
161
+ const existing = this.#state.get(txHash);
162
+ if (existing !== undefined && minedAtBlock >= existing.minedAtBlock) {
163
+ this.#state.delete(txHash);
164
+ await this.#deletedTxsDB.delete(txHash);
165
+ this.#log.debug(
166
+ `Cleared tracking for tx ${txHash}: re-mined at block ${minedAtBlock} (was ${existing.minedAtBlock})`,
167
+ );
168
+ }
169
+ }
170
+
171
+ /**
172
+ * Checks if a transaction is from a pruned block.
173
+ */
174
+ isFromPrunedBlock(txHash: string): boolean {
175
+ return this.#state.has(txHash);
176
+ }
177
+
178
+ /**
179
+ * Checks if a transaction is soft-deleted.
180
+ */
181
+ isSoftDeleted(txHash: string): boolean {
182
+ return this.#state.get(txHash)?.softDeleted ?? false;
183
+ }
184
+
185
+ /**
186
+ * Gets the block number in which a transaction was originally mined.
187
+ */
188
+ getMinedAtBlock(txHash: string): BlockNumber | undefined {
189
+ return this.#state.get(txHash)?.minedAtBlock;
190
+ }
191
+
192
+ /**
193
+ * Finalizes transactions when a block is finalized.
194
+ * Hard-deletes transactions that were originally mined at or before the finalized block.
195
+ *
196
+ * @returns The hashes of transactions that were hard-deleted
197
+ */
198
+ async finalizeBlock(finalizedBlockNumber: BlockNumber): Promise<string[]> {
199
+ const toHardDelete: string[] = [];
200
+ for (const [txHash, state] of this.#state) {
201
+ if (state.softDeleted && state.minedAtBlock <= finalizedBlockNumber) {
202
+ toHardDelete.push(txHash);
203
+ }
204
+ }
205
+
206
+ if (toHardDelete.length === 0) {
207
+ return [];
208
+ }
209
+
210
+ // Hard-delete from all stores
211
+ for (const txHash of toHardDelete) {
212
+ this.#state.delete(txHash);
213
+ await this.#deletedTxsDB.delete(txHash);
214
+ await this.#txsDB.delete(txHash);
215
+ }
216
+
217
+ this.#log.debug(`Finalized ${toHardDelete.length} txs from pruned blocks at block ${finalizedBlockNumber}`);
218
+ return toHardDelete;
219
+ }
220
+
221
+ /**
222
+ * Gets the count of transactions from pruned blocks.
223
+ */
224
+ getCount(): number {
225
+ return this.#state.size;
226
+ }
227
+
228
+ /**
229
+ * Gets all transaction hashes from pruned blocks.
230
+ */
231
+ getPrunedTxHashes(): string[] {
232
+ return Array.from(this.#state.keys());
233
+ }
234
+ }
@@ -9,3 +9,4 @@ export {
9
9
  } from './interfaces.js';
10
10
  export { type TxMetaData, type TxState, buildTxMetaData, comparePriority } from './tx_metadata.js';
11
11
  export { TxArchive } from './archive/index.js';
12
+ export { DeletedPool } from './deleted_pool.js';
@@ -55,8 +55,8 @@ export type TxPoolV2Dependencies = {
55
55
  l2BlockSource: L2BlockSource;
56
56
  /** World state synchronizer for validating transactions after chain prunes */
57
57
  worldStateSynchronizer: WorldStateSynchronizer;
58
- /** Validator for transactions entering the pending pool */
59
- pendingTxValidator: TxValidator<Tx>;
58
+ /** Factory that creates a validator for re-validating pool transactions using metadata */
59
+ createTxValidator: () => Promise<TxValidator<TxMetaData>>;
60
60
  };
61
61
 
62
62
  /**
@@ -1,12 +1,27 @@
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
1
2
  import { Fr } from '@aztec/foundation/curves/bn254';
2
3
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
3
- import type { L2BlockId } from '@aztec/stdlib/block';
4
+ import { BlockHash, type L2BlockId } from '@aztec/stdlib/block';
4
5
  import type { Tx } from '@aztec/stdlib/tx';
5
6
 
6
7
  import { getFeePayerBalanceDelta } from '../../msg_validators/tx_validator/fee_payer_balance.js';
7
8
  import { getTxPriorityFee } from '../tx_pool/priority.js';
8
9
  import type { PreAddResult } from './eviction/interfaces.js';
9
10
 
11
+ /** Validator-compatible data interface, mirroring the subset of PrivateKernelTailCircuitPublicInputs used by validators. */
12
+ export type TxMetaValidationData = {
13
+ getNonEmptyNullifiers(): Fr[];
14
+ includeByTimestamp: bigint;
15
+ constants: {
16
+ anchorBlockHeader: {
17
+ hash(): Promise<BlockHash>;
18
+ globalVariables: {
19
+ blockNumber: BlockNumber;
20
+ };
21
+ };
22
+ };
23
+ };
24
+
10
25
  /**
11
26
  * Lightweight in-memory representation of a transaction.
12
27
  * Stored for every tx in the pool to enable efficient queries and challenges
@@ -42,22 +57,29 @@ export type TxMetaData = {
42
57
 
43
58
  /** Timestamp by which the transaction must be included (for expiration checks) */
44
59
  readonly includeByTimestamp: bigint;
60
+
61
+ /** Validator-compatible data, providing the same access patterns as Tx.data */
62
+ readonly data: TxMetaValidationData;
45
63
  };
46
64
 
47
65
  /** Transaction state derived from TxMetaData fields and pool protection status */
48
- export type TxState = 'pending' | 'protected' | 'mined';
66
+ export type TxState = 'pending' | 'protected' | 'mined' | 'deleted';
49
67
 
50
68
  /**
51
69
  * Builds TxMetaData from a full Tx object.
52
70
  * Extracts all relevant fields for efficient in-memory storage and querying.
71
+ * Fr values are captured in closures for zero-cost re-validation.
53
72
  */
54
73
  export async function buildTxMetaData(tx: Tx): Promise<TxMetaData> {
55
74
  const txHash = tx.getTxHash().toString();
56
- const anchorBlockHeaderHash = (await tx.data.constants.anchorBlockHeader.hash()).toString();
75
+ const nullifierFrs = tx.data.getNonEmptyNullifiers();
76
+ const nullifiers = nullifierFrs.map(n => n.toString());
77
+ const anchorBlockHeaderHashFr = await tx.data.constants.anchorBlockHeader.hash();
78
+ const anchorBlockHeaderHash = anchorBlockHeaderHashFr.toString();
79
+ const includeByTimestamp = tx.data.includeByTimestamp;
80
+ const anchorBlockNumber = tx.data.constants.anchorBlockHeader.globalVariables.blockNumber;
57
81
  const priorityFee = getTxPriorityFee(tx);
58
82
  const feePayer = tx.data.feePayer.toString();
59
- const nullifiers = tx.data.getNonEmptyNullifiers().map(n => n.toString());
60
- const includeByTimestamp = tx.data.includeByTimestamp;
61
83
 
62
84
  const { feeLimit, claimAmount } = await getFeePayerBalanceDelta(tx, ProtocolContractAddress.FeeJuice);
63
85
 
@@ -70,6 +92,16 @@ export async function buildTxMetaData(tx: Tx): Promise<TxMetaData> {
70
92
  feeLimit,
71
93
  nullifiers,
72
94
  includeByTimestamp,
95
+ data: {
96
+ getNonEmptyNullifiers: () => nullifierFrs,
97
+ includeByTimestamp,
98
+ constants: {
99
+ anchorBlockHeader: {
100
+ hash: () => Promise.resolve(anchorBlockHeaderHashFr),
101
+ globalVariables: { blockNumber: anchorBlockNumber },
102
+ },
103
+ },
104
+ },
73
105
  };
74
106
  }
75
107
 
@@ -159,3 +191,17 @@ export function checkNullifierConflict(
159
191
 
160
192
  return { shouldIgnore: false, txHashesToEvict };
161
193
  }
194
+
195
+ /** Creates a stub TxMetaValidationData for tests that don't exercise validators. */
196
+ export function stubTxMetaValidationData(overrides: { includeByTimestamp?: bigint } = {}): TxMetaValidationData {
197
+ return {
198
+ getNonEmptyNullifiers: () => [],
199
+ includeByTimestamp: overrides.includeByTimestamp ?? 0n,
200
+ constants: {
201
+ anchorBlockHeader: {
202
+ hash: () => Promise.resolve(new BlockHash(Fr.ZERO)),
203
+ globalVariables: { blockNumber: BlockNumber(0) },
204
+ },
205
+ },
206
+ };
207
+ }