@aztec/p2p 0.0.1-commit.43c09e3f → 0.0.1-commit.4d79d1f2d

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 (216) hide show
  1. package/dest/client/factory.d.ts +5 -5
  2. package/dest/client/factory.d.ts.map +1 -1
  3. package/dest/client/factory.js +40 -9
  4. package/dest/client/interface.d.ts +33 -15
  5. package/dest/client/interface.d.ts.map +1 -1
  6. package/dest/client/p2p_client.d.ts +33 -36
  7. package/dest/client/p2p_client.d.ts.map +1 -1
  8. package/dest/client/p2p_client.js +104 -139
  9. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.js +1 -1
  10. package/dest/config.d.ts +14 -5
  11. package/dest/config.d.ts.map +1 -1
  12. package/dest/config.js +11 -1
  13. package/dest/index.d.ts +2 -1
  14. package/dest/index.d.ts.map +1 -1
  15. package/dest/index.js +1 -0
  16. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +102 -88
  17. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  18. package/dest/mem_pools/attestation_pool/attestation_pool.js +436 -3
  19. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +2 -2
  20. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  21. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +353 -87
  22. package/dest/mem_pools/attestation_pool/index.d.ts +2 -3
  23. package/dest/mem_pools/attestation_pool/index.d.ts.map +1 -1
  24. package/dest/mem_pools/attestation_pool/index.js +1 -2
  25. package/dest/mem_pools/index.d.ts +3 -2
  26. package/dest/mem_pools/index.d.ts.map +1 -1
  27. package/dest/mem_pools/index.js +1 -1
  28. package/dest/mem_pools/interface.d.ts +5 -5
  29. package/dest/mem_pools/interface.d.ts.map +1 -1
  30. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts +87 -0
  31. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts.map +1 -0
  32. package/dest/mem_pools/tx_pool_v2/deleted_pool.js +180 -0
  33. package/dest/mem_pools/tx_pool_v2/index.d.ts +2 -1
  34. package/dest/mem_pools/tx_pool_v2/index.d.ts.map +1 -1
  35. package/dest/mem_pools/tx_pool_v2/index.js +1 -0
  36. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +7 -3
  37. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
  38. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +25 -3
  39. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
  40. package/dest/mem_pools/tx_pool_v2/tx_metadata.js +38 -5
  41. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts +99 -0
  42. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts.map +1 -0
  43. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.js +332 -0
  44. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +1 -1
  45. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -1
  46. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.js +6 -0
  47. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +1 -1
  48. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
  49. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +226 -516
  50. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +3 -3
  51. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -1
  52. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +3 -3
  53. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
  54. package/dest/msg_validators/tx_validator/block_header_validator.d.ts +16 -3
  55. package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
  56. package/dest/msg_validators/tx_validator/block_header_validator.js +1 -1
  57. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts +13 -3
  58. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts.map +1 -1
  59. package/dest/msg_validators/tx_validator/double_spend_validator.js +4 -4
  60. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +20 -4
  61. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -1
  62. package/dest/msg_validators/tx_validator/timestamp_validator.js +2 -2
  63. package/dest/services/dummy_service.d.ts +10 -2
  64. package/dest/services/dummy_service.d.ts.map +1 -1
  65. package/dest/services/dummy_service.js +6 -0
  66. package/dest/services/encoding.d.ts +2 -2
  67. package/dest/services/encoding.d.ts.map +1 -1
  68. package/dest/services/encoding.js +2 -2
  69. package/dest/services/gossipsub/index.d.ts +3 -0
  70. package/dest/services/gossipsub/index.d.ts.map +1 -0
  71. package/dest/services/gossipsub/index.js +2 -0
  72. package/dest/services/gossipsub/scoring.d.ts +21 -3
  73. package/dest/services/gossipsub/scoring.d.ts.map +1 -1
  74. package/dest/services/gossipsub/scoring.js +24 -7
  75. package/dest/services/gossipsub/topic_score_params.d.ts +161 -0
  76. package/dest/services/gossipsub/topic_score_params.d.ts.map +1 -0
  77. package/dest/services/gossipsub/topic_score_params.js +324 -0
  78. package/dest/services/libp2p/libp2p_service.d.ts +84 -35
  79. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  80. package/dest/services/libp2p/libp2p_service.js +368 -273
  81. package/dest/services/peer-manager/peer_scoring.d.ts +1 -1
  82. package/dest/services/peer-manager/peer_scoring.d.ts.map +1 -1
  83. package/dest/services/peer-manager/peer_scoring.js +25 -2
  84. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts +4 -4
  85. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts.map +1 -1
  86. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.js +8 -8
  87. package/dest/services/reqresp/interface.d.ts +10 -1
  88. package/dest/services/reqresp/interface.d.ts.map +1 -1
  89. package/dest/services/reqresp/interface.js +15 -1
  90. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +7 -5
  91. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -1
  92. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.js +16 -11
  93. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +21 -10
  94. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
  95. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +27 -11
  96. package/dest/services/reqresp/protocols/tx.d.ts +7 -1
  97. package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
  98. package/dest/services/reqresp/protocols/tx.js +20 -0
  99. package/dest/services/reqresp/reqresp.d.ts +1 -1
  100. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  101. package/dest/services/reqresp/reqresp.js +11 -4
  102. package/dest/services/service.d.ts +35 -1
  103. package/dest/services/service.d.ts.map +1 -1
  104. package/dest/services/tx_collection/config.d.ts +10 -4
  105. package/dest/services/tx_collection/config.d.ts.map +1 -1
  106. package/dest/services/tx_collection/config.js +19 -3
  107. package/dest/services/tx_collection/fast_tx_collection.d.ts +6 -5
  108. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
  109. package/dest/services/tx_collection/fast_tx_collection.js +27 -17
  110. package/dest/services/tx_collection/file_store_tx_collection.d.ts +44 -0
  111. package/dest/services/tx_collection/file_store_tx_collection.d.ts.map +1 -0
  112. package/dest/services/tx_collection/file_store_tx_collection.js +118 -0
  113. package/dest/services/tx_collection/file_store_tx_source.d.ts +27 -0
  114. package/dest/services/tx_collection/file_store_tx_source.d.ts.map +1 -0
  115. package/dest/services/tx_collection/file_store_tx_source.js +57 -0
  116. package/dest/services/tx_collection/index.d.ts +3 -2
  117. package/dest/services/tx_collection/index.d.ts.map +1 -1
  118. package/dest/services/tx_collection/index.js +1 -0
  119. package/dest/services/tx_collection/proposal_tx_collector.d.ts +12 -12
  120. package/dest/services/tx_collection/proposal_tx_collector.d.ts.map +1 -1
  121. package/dest/services/tx_collection/proposal_tx_collector.js +4 -5
  122. package/dest/services/tx_collection/slow_tx_collection.d.ts +3 -1
  123. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -1
  124. package/dest/services/tx_collection/slow_tx_collection.js +48 -19
  125. package/dest/services/tx_collection/tx_collection.d.ts +17 -7
  126. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
  127. package/dest/services/tx_collection/tx_collection.js +58 -2
  128. package/dest/services/tx_collection/tx_collection_sink.d.ts +15 -6
  129. package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -1
  130. package/dest/services/tx_collection/tx_collection_sink.js +13 -7
  131. package/dest/services/tx_file_store/config.d.ts +1 -3
  132. package/dest/services/tx_file_store/config.d.ts.map +1 -1
  133. package/dest/services/tx_file_store/config.js +0 -4
  134. package/dest/services/tx_file_store/tx_file_store.d.ts +3 -3
  135. package/dest/services/tx_file_store/tx_file_store.d.ts.map +1 -1
  136. package/dest/services/tx_provider.d.ts +3 -3
  137. package/dest/services/tx_provider.d.ts.map +1 -1
  138. package/dest/services/tx_provider.js +5 -4
  139. package/dest/test-helpers/make-test-p2p-clients.d.ts +3 -3
  140. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  141. package/dest/test-helpers/mock-pubsub.d.ts +27 -1
  142. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  143. package/dest/test-helpers/mock-pubsub.js +97 -2
  144. package/dest/test-helpers/reqresp-nodes.d.ts +1 -1
  145. package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
  146. package/dest/test-helpers/reqresp-nodes.js +2 -1
  147. package/dest/test-helpers/testbench-utils.d.ts +38 -38
  148. package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
  149. package/dest/test-helpers/testbench-utils.js +124 -61
  150. package/dest/testbench/p2p_client_testbench_worker.js +2 -2
  151. package/package.json +14 -14
  152. package/src/client/factory.ts +68 -12
  153. package/src/client/interface.ts +39 -14
  154. package/src/client/p2p_client.ts +139 -162
  155. package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker.ts +1 -1
  156. package/src/config.ts +26 -2
  157. package/src/index.ts +1 -0
  158. package/src/mem_pools/attestation_pool/attestation_pool.ts +488 -91
  159. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +442 -102
  160. package/src/mem_pools/attestation_pool/index.ts +9 -2
  161. package/src/mem_pools/index.ts +4 -1
  162. package/src/mem_pools/interface.ts +4 -4
  163. package/src/mem_pools/tx_pool_v2/README.md +87 -16
  164. package/src/mem_pools/tx_pool_v2/deleted_pool.ts +234 -0
  165. package/src/mem_pools/tx_pool_v2/index.ts +1 -0
  166. package/src/mem_pools/tx_pool_v2/interfaces.ts +4 -2
  167. package/src/mem_pools/tx_pool_v2/tx_metadata.ts +53 -6
  168. package/src/mem_pools/tx_pool_v2/tx_pool_indices.ts +417 -0
  169. package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +3 -0
  170. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +234 -604
  171. package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +2 -2
  172. package/src/msg_validators/tx_validator/aggregate_tx_validator.ts +2 -2
  173. package/src/msg_validators/tx_validator/block_header_validator.ts +15 -3
  174. package/src/msg_validators/tx_validator/double_spend_validator.ts +11 -6
  175. package/src/msg_validators/tx_validator/timestamp_validator.ts +19 -14
  176. package/src/services/dummy_service.ts +12 -0
  177. package/src/services/encoding.ts +2 -2
  178. package/src/services/gossipsub/README.md +626 -0
  179. package/src/services/gossipsub/index.ts +2 -0
  180. package/src/services/gossipsub/scoring.ts +29 -5
  181. package/src/services/gossipsub/topic_score_params.ts +451 -0
  182. package/src/services/libp2p/libp2p_service.ts +370 -275
  183. package/src/services/peer-manager/peer_scoring.ts +25 -0
  184. package/src/services/reqresp/batch-tx-requester/README.md +7 -7
  185. package/src/services/reqresp/batch-tx-requester/batch_tx_requester.ts +11 -11
  186. package/src/services/reqresp/interface.ts +26 -1
  187. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +23 -14
  188. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +38 -15
  189. package/src/services/reqresp/protocols/tx.ts +22 -0
  190. package/src/services/reqresp/reqresp.ts +13 -3
  191. package/src/services/service.ts +40 -0
  192. package/src/services/tx_collection/config.ts +32 -6
  193. package/src/services/tx_collection/fast_tx_collection.ts +28 -26
  194. package/src/services/tx_collection/file_store_tx_collection.ts +152 -0
  195. package/src/services/tx_collection/file_store_tx_source.ts +70 -0
  196. package/src/services/tx_collection/index.ts +2 -1
  197. package/src/services/tx_collection/proposal_tx_collector.ts +12 -14
  198. package/src/services/tx_collection/slow_tx_collection.ts +55 -26
  199. package/src/services/tx_collection/tx_collection.ts +78 -12
  200. package/src/services/tx_collection/tx_collection_sink.ts +17 -7
  201. package/src/services/tx_file_store/config.ts +0 -6
  202. package/src/services/tx_file_store/tx_file_store.ts +4 -4
  203. package/src/services/tx_provider.ts +8 -7
  204. package/src/test-helpers/make-test-p2p-clients.ts +3 -3
  205. package/src/test-helpers/mock-pubsub.ts +133 -3
  206. package/src/test-helpers/reqresp-nodes.ts +2 -1
  207. package/src/test-helpers/testbench-utils.ts +122 -74
  208. package/src/testbench/p2p_client_testbench_worker.ts +2 -2
  209. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +0 -40
  210. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +0 -1
  211. package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +0 -218
  212. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +0 -31
  213. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +0 -1
  214. package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +0 -180
  215. package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +0 -320
  216. package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +0 -264
@@ -1,2 +1,9 @@
1
- export * from './attestation_pool.js';
2
- export * from './memory_attestation_pool.js';
1
+ export {
2
+ AttestationPool,
3
+ type AttestationPoolApi,
4
+ type TryAddResult,
5
+ createTestAttestationPool,
6
+ MAX_CHECKPOINT_PROPOSALS_PER_SLOT,
7
+ MAX_BLOCK_PROPOSALS_PER_POSITION,
8
+ MAX_ATTESTATIONS_PER_SLOT_AND_SIGNER,
9
+ } from './attestation_pool.js';
@@ -1,3 +1,6 @@
1
- export { type AttestationPool } from './attestation_pool/attestation_pool.js';
1
+ export { AttestationPool, type AttestationPoolApi } from './attestation_pool/attestation_pool.js';
2
2
  export { type MemPools } from './interface.js';
3
+ // Old TxPool exports - kept temporarily for external consumers
3
4
  export { type TxPool } from './tx_pool/tx_pool.js';
5
+ // New TxPoolV2 exports
6
+ export { type TxPoolV2, type TxPoolV2Config, type TxPoolV2Events, type AddTxsResult } from './tx_pool_v2/index.js';
@@ -1,10 +1,10 @@
1
- import type { AttestationPool } from './attestation_pool/attestation_pool.js';
2
- import type { TxPool } from './tx_pool/tx_pool.js';
1
+ import type { AttestationPoolApi } from './attestation_pool/attestation_pool.js';
2
+ import type { TxPoolV2 } from './tx_pool_v2/interfaces.js';
3
3
 
4
4
  /**
5
5
  * A interface the combines all mempools
6
6
  */
7
7
  export type MemPools = {
8
- txPool: TxPool;
9
- attestationPool: AttestationPool;
8
+ txPool: TxPoolV2;
9
+ attestationPool: AttestationPoolApi;
10
10
  };
@@ -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,17 +60,77 @@ 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:
58
76
  - `txHash`: Transaction identifier
59
- - `state`: Current state (pending, protected, mined)
60
- - `priorityFee`: For priority ordering
77
+ - `anchorBlockHeaderHash`: Hash of the anchor block header
78
+ - `priorityFee`: For priority ordering and challenges
61
79
  - `feePayer`: For balance-based eviction
80
+ - `claimAmount`: Fee payer's claim from bridging
81
+ - `feeLimit`: Maximum fee the tx can pay
62
82
  - `nullifiers`: For conflict detection
63
- - `estimatedTxFee`: For balance calculations
64
- - `blockId`: Block info when mined
65
- - `protectedSlot`: Slot number when protected
83
+ - `includeByTimestamp`: Expiration timestamp
84
+ - `minedL2BlockId`: Set when mined (undefined otherwise)
85
+
86
+ State is derived by TxPoolIndices:
87
+ - `mined` if `minedL2BlockId` is set
88
+ - `protected` if in protection map
89
+ - `deleted` if soft-deleted (from a pruned block, evicted but kept in DB)
90
+ - `pending` otherwise
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
+
120
+ ## Architecture: Pre-add vs Post-event Rules
121
+
122
+ **Pre-add rules** (run during `addPendingTxs`):
123
+ - Used for external transactions entering the pool
124
+ - Can reject the incoming tx entirely
125
+ - Can evict lower-priority existing txs to make room
126
+ - Rules: NullifierConflictRule, FeePayerBalancePreAddRule, LowPriorityPreAddRule
127
+
128
+ **Post-event rules** (run after state transitions):
129
+ - Used for internal state changes (block mined, reorg, slot change)
130
+ - Only evict txs already in the pool
131
+ - Rules: InvalidTxsAfterMiningRule, InvalidTxsAfterReorgRule, FeePayerBalanceEvictionRule, LowPriorityEvictionRule
132
+
133
+ This design choice means restored txs (from protected/mined states) use post-event rules only, because they were already validated on initial submission.
66
134
 
67
135
  ## Eviction Rules
68
136
 
@@ -85,8 +153,8 @@ Run after events to clean up the pool:
85
153
  | Rule | Trigger | Purpose |
86
154
  |------|---------|---------|
87
155
  | `LowPriorityEvictionRule` | `txs_added` | Evicts lowest priority txs when pool exceeds limit. |
88
- | `FeePayerBalanceEvictionRule` | `block_mined` | Evicts txs when fee payer balance decreases. |
89
- | `InvalidTxsAfterMiningRule` | `block_mined` | Evicts pending txs with nullifiers that were just mined. |
156
+ | `FeePayerBalanceEvictionRule` | `txs_added`, `block_mined`, `chain_pruned` | Evicts txs when fee payer has insufficient balance. |
157
+ | `InvalidTxsAfterMiningRule` | `block_mined` | Evicts pending txs with: (1) nullifiers in mined block, (2) expired timestamp. |
90
158
  | `InvalidTxsAfterReorgRule` | `chain_pruned` | Evicts txs with invalid anchor blocks after reorg. |
91
159
 
92
160
  ## Usage
@@ -99,7 +167,7 @@ import { AztecKVTxPoolV2 } from './tx_pool_v2.js';
99
167
  const pool = new AztecKVTxPoolV2(txStore, archiveStore, {
100
168
  l2BlockSource: archiver,
101
169
  worldStateSynchronizer: worldState,
102
- pendingTxValidator: validator,
170
+ createTxValidator: () => validator,
103
171
  });
104
172
 
105
173
  await pool.start();
@@ -174,9 +242,12 @@ The archive uses FIFO eviction when `archivedTxLimit` is reached.
174
242
  ## Testing
175
243
 
176
244
  ```bash
177
- # Unit tests (131 tests)
245
+ # Unit tests (177 tests)
178
246
  yarn test src/mem_pools/tx_pool_v2/tx_pool_v2.test.ts
179
247
 
248
+ # Deleted pool tests (17 tests)
249
+ yarn test src/mem_pools/tx_pool_v2/deleted_pool.test.ts
250
+
180
251
  # Compatibility tests (25 tests)
181
252
  yarn test src/mem_pools/tx_pool_v2/tx_pool_v2.compat.test.ts
182
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';
@@ -25,6 +25,8 @@ export type AddTxsResult = {
25
25
  export type TxPoolV2Events = {
26
26
  /** Emitted when transactions are successfully added to the pool */
27
27
  'txs-added': (args: { txs: Tx[]; source?: string }) => void;
28
+ /** Emitted when transactions are removed from the pool */
29
+ 'txs-removed': (args: { txHashes: TxHash[] }) => void;
28
30
  };
29
31
 
30
32
  /**
@@ -53,8 +55,8 @@ export type TxPoolV2Dependencies = {
53
55
  l2BlockSource: L2BlockSource;
54
56
  /** World state synchronizer for validating transactions after chain prunes */
55
57
  worldStateSynchronizer: WorldStateSynchronizer;
56
- /** Validator for transactions entering the pending pool */
57
- pendingTxValidator: TxValidator<Tx>;
58
+ /** Factory that creates a validator for re-validating pool transactions using metadata */
59
+ createTxValidator: () => Promise<TxValidator<TxMetaData>>;
58
60
  };
59
61
 
60
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
 
@@ -145,7 +177,8 @@ export function checkNullifierConflict(
145
177
 
146
178
  // If incoming tx has strictly higher priority, mark for eviction
147
179
  // Otherwise, ignore incoming tx (ties go to existing tx)
148
- if (incomingMeta.priorityFee > conflictingMeta.priorityFee) {
180
+ // Use comparePriority for deterministic ordering (includes txHash as tiebreaker)
181
+ if (comparePriority(incomingMeta, conflictingMeta) > 0) {
149
182
  txHashesToEvict.push(conflictingHashStr);
150
183
  } else {
151
184
  return {
@@ -158,3 +191,17 @@ export function checkNullifierConflict(
158
191
 
159
192
  return { shouldIgnore: false, txHashesToEvict };
160
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
+ }