@aztec/p2p 0.0.1-commit.87a0206 → 0.0.1-commit.8f9871590

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 (213) 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 +42 -8
  4. package/dest/client/interface.d.ts +30 -15
  5. package/dest/client/interface.d.ts.map +1 -1
  6. package/dest/client/p2p_client.d.ts +33 -35
  7. package/dest/client/p2p_client.d.ts.map +1 -1
  8. package/dest/client/p2p_client.js +107 -137
  9. package/dest/config.d.ts +22 -4
  10. package/dest/config.d.ts.map +1 -1
  11. package/dest/config.js +16 -1
  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 +21 -12
  16. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  17. package/dest/mem_pools/attestation_pool/attestation_pool.js +67 -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/attestation_pool/mocks.d.ts +2 -2
  25. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  26. package/dest/mem_pools/attestation_pool/mocks.js +2 -2
  27. package/dest/mem_pools/index.d.ts +2 -1
  28. package/dest/mem_pools/index.d.ts.map +1 -1
  29. package/dest/mem_pools/interface.d.ts +3 -3
  30. package/dest/mem_pools/interface.d.ts.map +1 -1
  31. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.js +3 -3
  32. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts +102 -0
  33. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts.map +1 -0
  34. package/dest/mem_pools/tx_pool_v2/deleted_pool.js +242 -0
  35. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts +1 -1
  36. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -1
  37. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.js +3 -0
  38. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.js +5 -3
  39. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.js +3 -1
  40. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts +1 -1
  41. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts.map +1 -1
  42. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.js +3 -1
  43. package/dest/mem_pools/tx_pool_v2/index.d.ts +2 -1
  44. package/dest/mem_pools/tx_pool_v2/index.d.ts.map +1 -1
  45. package/dest/mem_pools/tx_pool_v2/index.js +1 -0
  46. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +7 -3
  47. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
  48. package/dest/mem_pools/tx_pool_v2/interfaces.js +2 -1
  49. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +28 -4
  50. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
  51. package/dest/mem_pools/tx_pool_v2/tx_metadata.js +37 -4
  52. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts +8 -2
  53. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts.map +1 -1
  54. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.js +15 -2
  55. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +4 -2
  56. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -1
  57. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.js +6 -2
  58. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +4 -2
  59. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
  60. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +110 -78
  61. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +3 -3
  62. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
  63. package/dest/msg_validators/tx_validator/block_header_validator.d.ts +16 -3
  64. package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
  65. package/dest/msg_validators/tx_validator/block_header_validator.js +1 -1
  66. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts +13 -3
  67. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts.map +1 -1
  68. package/dest/msg_validators/tx_validator/double_spend_validator.js +4 -4
  69. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +20 -4
  70. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -1
  71. package/dest/msg_validators/tx_validator/timestamp_validator.js +6 -6
  72. package/dest/services/dummy_service.d.ts +6 -2
  73. package/dest/services/dummy_service.d.ts.map +1 -1
  74. package/dest/services/dummy_service.js +3 -0
  75. package/dest/services/encoding.d.ts +2 -2
  76. package/dest/services/encoding.d.ts.map +1 -1
  77. package/dest/services/encoding.js +2 -2
  78. package/dest/services/gossipsub/index.d.ts +3 -0
  79. package/dest/services/gossipsub/index.d.ts.map +1 -0
  80. package/dest/services/gossipsub/index.js +2 -0
  81. package/dest/services/gossipsub/scoring.d.ts +21 -3
  82. package/dest/services/gossipsub/scoring.d.ts.map +1 -1
  83. package/dest/services/gossipsub/scoring.js +24 -7
  84. package/dest/services/gossipsub/topic_score_params.d.ts +161 -0
  85. package/dest/services/gossipsub/topic_score_params.d.ts.map +1 -0
  86. package/dest/services/gossipsub/topic_score_params.js +324 -0
  87. package/dest/services/libp2p/libp2p_service.d.ts +11 -3
  88. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  89. package/dest/services/libp2p/libp2p_service.js +91 -67
  90. package/dest/services/peer-manager/peer_scoring.d.ts +1 -1
  91. package/dest/services/peer-manager/peer_scoring.d.ts.map +1 -1
  92. package/dest/services/peer-manager/peer_scoring.js +25 -2
  93. package/dest/services/reqresp/interface.d.ts +10 -1
  94. package/dest/services/reqresp/interface.d.ts.map +1 -1
  95. package/dest/services/reqresp/interface.js +15 -1
  96. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +3 -3
  97. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -1
  98. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +7 -1
  99. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
  100. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +15 -0
  101. package/dest/services/reqresp/protocols/tx.d.ts +7 -1
  102. package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
  103. package/dest/services/reqresp/protocols/tx.js +20 -0
  104. package/dest/services/reqresp/reqresp.d.ts +1 -1
  105. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  106. package/dest/services/reqresp/reqresp.js +11 -4
  107. package/dest/services/service.d.ts +18 -1
  108. package/dest/services/service.d.ts.map +1 -1
  109. package/dest/services/tx_collection/config.d.ts +19 -1
  110. package/dest/services/tx_collection/config.d.ts.map +1 -1
  111. package/dest/services/tx_collection/config.js +46 -0
  112. package/dest/services/tx_collection/fast_tx_collection.d.ts +3 -1
  113. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
  114. package/dest/services/tx_collection/fast_tx_collection.js +17 -3
  115. package/dest/services/tx_collection/file_store_tx_collection.d.ts +53 -0
  116. package/dest/services/tx_collection/file_store_tx_collection.d.ts.map +1 -0
  117. package/dest/services/tx_collection/file_store_tx_collection.js +165 -0
  118. package/dest/services/tx_collection/file_store_tx_source.d.ts +28 -0
  119. package/dest/services/tx_collection/file_store_tx_source.d.ts.map +1 -0
  120. package/dest/services/tx_collection/file_store_tx_source.js +59 -0
  121. package/dest/services/tx_collection/index.d.ts +2 -1
  122. package/dest/services/tx_collection/index.d.ts.map +1 -1
  123. package/dest/services/tx_collection/index.js +1 -0
  124. package/dest/services/tx_collection/slow_tx_collection.d.ts +6 -2
  125. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -1
  126. package/dest/services/tx_collection/slow_tx_collection.js +55 -23
  127. package/dest/services/tx_collection/tx_collection.d.ts +19 -7
  128. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
  129. package/dest/services/tx_collection/tx_collection.js +75 -3
  130. package/dest/services/tx_collection/tx_collection_sink.d.ts +15 -6
  131. package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -1
  132. package/dest/services/tx_collection/tx_collection_sink.js +13 -7
  133. package/dest/services/tx_file_store/config.d.ts +1 -3
  134. package/dest/services/tx_file_store/config.d.ts.map +1 -1
  135. package/dest/services/tx_file_store/config.js +0 -4
  136. package/dest/services/tx_file_store/tx_file_store.d.ts +4 -3
  137. package/dest/services/tx_file_store/tx_file_store.d.ts.map +1 -1
  138. package/dest/services/tx_file_store/tx_file_store.js +8 -5
  139. package/dest/services/tx_provider.d.ts +3 -3
  140. package/dest/services/tx_provider.d.ts.map +1 -1
  141. package/dest/services/tx_provider.js +5 -4
  142. package/dest/test-helpers/make-test-p2p-clients.d.ts +3 -3
  143. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  144. package/dest/test-helpers/mock-pubsub.d.ts +27 -1
  145. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  146. package/dest/test-helpers/mock-pubsub.js +97 -2
  147. package/dest/test-helpers/reqresp-nodes.d.ts +1 -1
  148. package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
  149. package/dest/test-helpers/reqresp-nodes.js +2 -1
  150. package/dest/test-helpers/testbench-utils.d.ts +32 -24
  151. package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
  152. package/dest/test-helpers/testbench-utils.js +103 -36
  153. package/dest/testbench/p2p_client_testbench_worker.js +3 -3
  154. package/package.json +14 -14
  155. package/src/client/factory.ts +78 -9
  156. package/src/client/interface.ts +32 -13
  157. package/src/client/p2p_client.ts +135 -157
  158. package/src/config.ts +34 -2
  159. package/src/index.ts +1 -0
  160. package/src/mem_pools/attestation_pool/attestation_pool.ts +95 -44
  161. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +57 -53
  162. package/src/mem_pools/attestation_pool/index.ts +3 -3
  163. package/src/mem_pools/attestation_pool/mocks.ts +2 -1
  164. package/src/mem_pools/index.ts +3 -0
  165. package/src/mem_pools/interface.ts +2 -2
  166. package/src/mem_pools/tx_pool/README.md +1 -1
  167. package/src/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.ts +3 -3
  168. package/src/mem_pools/tx_pool_v2/README.md +76 -10
  169. package/src/mem_pools/tx_pool_v2/deleted_pool.ts +310 -0
  170. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.ts +3 -0
  171. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.ts +3 -3
  172. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.ts +1 -1
  173. package/src/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.ts +3 -1
  174. package/src/mem_pools/tx_pool_v2/index.ts +1 -0
  175. package/src/mem_pools/tx_pool_v2/interfaces.ts +8 -2
  176. package/src/mem_pools/tx_pool_v2/tx_metadata.ts +57 -7
  177. package/src/mem_pools/tx_pool_v2/tx_pool_indices.ts +18 -2
  178. package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +7 -1
  179. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +122 -80
  180. package/src/msg_validators/tx_validator/aggregate_tx_validator.ts +2 -2
  181. package/src/msg_validators/tx_validator/block_header_validator.ts +15 -3
  182. package/src/msg_validators/tx_validator/double_spend_validator.ts +11 -6
  183. package/src/msg_validators/tx_validator/timestamp_validator.ts +23 -18
  184. package/src/services/dummy_service.ts +6 -0
  185. package/src/services/encoding.ts +2 -2
  186. package/src/services/gossipsub/README.md +626 -0
  187. package/src/services/gossipsub/index.ts +2 -0
  188. package/src/services/gossipsub/scoring.ts +29 -5
  189. package/src/services/gossipsub/topic_score_params.ts +451 -0
  190. package/src/services/libp2p/libp2p_service.ts +91 -70
  191. package/src/services/peer-manager/peer_scoring.ts +25 -0
  192. package/src/services/reqresp/interface.ts +26 -1
  193. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +2 -2
  194. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +17 -0
  195. package/src/services/reqresp/protocols/tx.ts +22 -0
  196. package/src/services/reqresp/reqresp.ts +13 -3
  197. package/src/services/service.ts +20 -0
  198. package/src/services/tx_collection/config.ts +68 -0
  199. package/src/services/tx_collection/fast_tx_collection.ts +14 -2
  200. package/src/services/tx_collection/file_store_tx_collection.ts +198 -0
  201. package/src/services/tx_collection/file_store_tx_source.ts +73 -0
  202. package/src/services/tx_collection/index.ts +1 -0
  203. package/src/services/tx_collection/slow_tx_collection.ts +64 -30
  204. package/src/services/tx_collection/tx_collection.ts +109 -13
  205. package/src/services/tx_collection/tx_collection_sink.ts +17 -7
  206. package/src/services/tx_file_store/config.ts +0 -6
  207. package/src/services/tx_file_store/tx_file_store.ts +9 -7
  208. package/src/services/tx_provider.ts +8 -7
  209. package/src/test-helpers/make-test-p2p-clients.ts +3 -3
  210. package/src/test-helpers/mock-pubsub.ts +133 -3
  211. package/src/test-helpers/reqresp-nodes.ts +2 -1
  212. package/src/test-helpers/testbench-utils.ts +117 -40
  213. package/src/testbench/p2p_client_testbench_worker.ts +3 -3
@@ -0,0 +1,310 @@
1
+ import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
2
+ import type { Logger } from '@aztec/foundation/log';
3
+ import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSet } 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 "prune-soft-deleted" - removed from indices but kept
43
+ * in the database for later re-execution until their mined block is finalized
44
+ * - Transactions NOT from pruned blocks are "slot-soft-deleted" - kept in the database
45
+ * until the next slot, so other nodes can still fetch them via reqresp
46
+ *
47
+ * When a block is finalized, prune-soft-deleted transactions that were originally mined at or
48
+ * before that block number are permanently (hard) deleted. Slot-soft-deleted transactions
49
+ * are hard-deleted when `prepareForSlot` advances to a new slot.
50
+ */
51
+ export class DeletedPool {
52
+ /** Persisted map: txHash -> DeletedTxState (serialized) - for prune-based soft deletions */
53
+ #deletedTxsDB: AztecAsyncMap<string, Buffer>;
54
+
55
+ /** Reference to the main txs database for hard deletion */
56
+ #txsDB: AztecAsyncMap<string, Buffer>;
57
+
58
+ /** In-memory state for transactions from pruned blocks */
59
+ #state: Map<string, DeletedTxState> = new Map();
60
+
61
+ /** In-memory tracking: txHash -> slot at which the tx was deleted */
62
+ #slotDeletedTxs: Map<string, SlotNumber> = new Map();
63
+
64
+ /** Persisted set tracking which txs are slot-deleted, for hydration cleanup. */
65
+ #slotDeletedDB: AztecAsyncSet<string>;
66
+
67
+ /** Current slot number, updated by cleanupSlotDeleted */
68
+ #currentSlot: SlotNumber = SlotNumber(0);
69
+
70
+ #log: Logger;
71
+
72
+ constructor(store: AztecAsyncKVStore, txsDB: AztecAsyncMap<string, Buffer>, log: Logger) {
73
+ this.#deletedTxsDB = store.openMap('deleted_txs');
74
+ this.#slotDeletedDB = store.openSet('slot_deleted_txs');
75
+ this.#txsDB = txsDB;
76
+ this.#log = log;
77
+ }
78
+
79
+ /**
80
+ * Loads state from the database on startup.
81
+ * Slot-deleted txs are stale after restart and are immediately hard-deleted.
82
+ */
83
+ async hydrateFromDatabase(): Promise<void> {
84
+ let prunedCount = 0;
85
+ let softDeletedCount = 0;
86
+
87
+ for await (const [txHash, buffer] of this.#deletedTxsDB.entriesAsync()) {
88
+ const state = deserializeState(buffer);
89
+ this.#state.set(txHash, state);
90
+ prunedCount++;
91
+ if (state.softDeleted) {
92
+ softDeletedCount++;
93
+ }
94
+ }
95
+
96
+ if (prunedCount > 0 || softDeletedCount > 0) {
97
+ this.#log.info(`Loaded ${prunedCount} txs from pruned blocks, ${softDeletedCount} soft-deleted`);
98
+ }
99
+
100
+ // Slot-deleted txs are stale after restart - hard-delete them all
101
+ let slotDeletedCount = 0;
102
+ for await (const txHash of this.#slotDeletedDB.entriesAsync()) {
103
+ await this.#txsDB.delete(txHash);
104
+ await this.#slotDeletedDB.delete(txHash);
105
+ slotDeletedCount++;
106
+ }
107
+
108
+ if (slotDeletedCount > 0) {
109
+ this.#log.info(`Hard-deleted ${slotDeletedCount} stale slot-deleted txs on startup`);
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Marks transactions as being from a pruned block.
115
+ * Called during handlePrunedBlocks for ALL transactions that were un-mined.
116
+ *
117
+ * If a tx was previously tracked (e.g., mined at block 4, pruned, re-mined at block 5,
118
+ * pruned again), updates to the higher block number. This ensures the tx is kept until
119
+ * its most recent mined block is finalized.
120
+ *
121
+ * @param txs - Array of {txHash, minedAtBlock} pairs, where minedAtBlock is the block
122
+ * number in which the tx was mined before being un-mined
123
+ */
124
+ async markFromPrunedBlock(txs: { txHash: string; minedAtBlock: BlockNumber }[]): Promise<void> {
125
+ if (txs.length === 0) {
126
+ return;
127
+ }
128
+
129
+ let count = 0;
130
+ for (const { txHash, minedAtBlock } of txs) {
131
+ const existing = this.#state.get(txHash);
132
+ // Update if not tracked, or if this is a higher mined block (tx was re-mined then pruned again)
133
+ if (existing === undefined || minedAtBlock > existing.minedAtBlock) {
134
+ const state: DeletedTxState = {
135
+ minedAtBlock,
136
+ softDeleted: existing?.softDeleted ?? false,
137
+ };
138
+ this.#state.set(txHash, state);
139
+ await this.#deletedTxsDB.set(txHash, serializeState(state));
140
+ count++;
141
+ }
142
+ }
143
+
144
+ if (count > 0) {
145
+ this.#log.info(`Marked ${count} transactions from pruned blocks`);
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Deletes a transaction. This is the single entry point for ALL deletions.
151
+ * The tx is always soft-deleted (kept in DB):
152
+ *
153
+ * - If the tx is from a pruned block: prune-soft-delete (kept until finalized)
154
+ * - If the tx is NOT from a pruned block: slot-soft-delete (kept until next slot)
155
+ */
156
+ async deleteTx(txHash: string): Promise<void> {
157
+ const existing = this.#state.get(txHash);
158
+ if (existing !== undefined) {
159
+ // Prune-soft-delete - keep in DB until finalized
160
+ const state: DeletedTxState = {
161
+ minedAtBlock: existing.minedAtBlock,
162
+ softDeleted: true,
163
+ };
164
+ this.#state.set(txHash, state);
165
+ await this.#deletedTxsDB.set(txHash, serializeState(state));
166
+ } else {
167
+ // Slot-soft-delete - keep in DB until next slot
168
+ this.#slotDeletedTxs.set(txHash, this.#currentSlot);
169
+ await this.#slotDeletedDB.add(txHash);
170
+ }
171
+ }
172
+
173
+ /**
174
+ * Clears tracking for a transaction if it re-mines at a block number >= the tracked minedAtBlock.
175
+ *
176
+ * When a tx re-mines at a higher (or equal) block, the old high-water mark is no longer needed:
177
+ * any future prune would re-add the tx with an even higher block number. Clearing keeps
178
+ * DeletedPool consistent — only txs that are actually un-mined should be tracked here.
179
+ *
180
+ * When a tx re-mines at a lower block, we must preserve the existing entry to retain
181
+ * the high-water mark for re-execution purposes.
182
+ */
183
+ async clearIfMinedHigher(txHash: string, minedAtBlock: BlockNumber): Promise<void> {
184
+ const existing = this.#state.get(txHash);
185
+ if (existing !== undefined && minedAtBlock >= existing.minedAtBlock) {
186
+ this.#state.delete(txHash);
187
+ await this.#deletedTxsDB.delete(txHash);
188
+ this.#log.debug(
189
+ `Cleared tracking for tx ${txHash}: re-mined at block ${minedAtBlock} (was ${existing.minedAtBlock})`,
190
+ );
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Checks if a transaction is from a pruned block.
196
+ */
197
+ isFromPrunedBlock(txHash: string): boolean {
198
+ return this.#state.has(txHash);
199
+ }
200
+
201
+ /**
202
+ * Checks if a transaction is soft-deleted (either prune-based or slot-based).
203
+ */
204
+ isSoftDeleted(txHash: string): boolean {
205
+ return (this.#state.get(txHash)?.softDeleted ?? false) || this.#slotDeletedTxs.has(txHash);
206
+ }
207
+
208
+ /**
209
+ * Gets the block number in which a transaction was originally mined.
210
+ */
211
+ getMinedAtBlock(txHash: string): BlockNumber | undefined {
212
+ return this.#state.get(txHash)?.minedAtBlock;
213
+ }
214
+
215
+ /**
216
+ * Finalizes transactions when a block is finalized.
217
+ * Hard-deletes transactions that were originally mined at or before the finalized block.
218
+ *
219
+ * @returns The hashes of transactions that were hard-deleted
220
+ */
221
+ async finalizeBlock(finalizedBlockNumber: BlockNumber): Promise<string[]> {
222
+ const toHardDelete: string[] = [];
223
+ for (const [txHash, state] of this.#state) {
224
+ if (state.softDeleted && state.minedAtBlock <= finalizedBlockNumber) {
225
+ toHardDelete.push(txHash);
226
+ }
227
+ }
228
+
229
+ if (toHardDelete.length === 0) {
230
+ return [];
231
+ }
232
+
233
+ // Hard-delete from all stores
234
+ for (const txHash of toHardDelete) {
235
+ this.#state.delete(txHash);
236
+ await this.#deletedTxsDB.delete(txHash);
237
+ await this.#txsDB.delete(txHash);
238
+ }
239
+
240
+ this.#log.debug(`Finalized ${toHardDelete.length} txs from pruned blocks at block ${finalizedBlockNumber}`, {
241
+ txHashes: toHardDelete,
242
+ });
243
+ return toHardDelete;
244
+ }
245
+
246
+ /**
247
+ * Cleans up slot-deleted transactions from previous slots.
248
+ * Called at the start of prepareForSlot. Updates #currentSlot and hard-deletes
249
+ * any txs that were deleted in an earlier slot.
250
+ */
251
+ async cleanupSlotDeleted(currentSlot: SlotNumber): Promise<void> {
252
+ const previousSlot = this.#currentSlot;
253
+ this.#currentSlot = currentSlot;
254
+
255
+ const toHardDelete: string[] = [];
256
+ for (const [txHash, deletedAtSlot] of this.#slotDeletedTxs) {
257
+ if (deletedAtSlot < currentSlot) {
258
+ toHardDelete.push(txHash);
259
+ }
260
+ }
261
+
262
+ if (toHardDelete.length === 0) {
263
+ return;
264
+ }
265
+
266
+ for (const txHash of toHardDelete) {
267
+ this.#slotDeletedTxs.delete(txHash);
268
+ await this.#slotDeletedDB.delete(txHash);
269
+ await this.#txsDB.delete(txHash);
270
+ }
271
+
272
+ this.#log.debug(
273
+ `Cleaned up ${toHardDelete.length} slot-deleted txs from slot ${previousSlot} (now slot ${currentSlot})`,
274
+ { txHashes: toHardDelete },
275
+ );
276
+ }
277
+
278
+ /**
279
+ * Clears soft-deletion status for a transaction being re-added to the pool.
280
+ * Removes slot-deleted tracking entirely, and resets the prune-soft-deleted flag
281
+ * while preserving the prune tracking itself (so a subsequent delete still uses
282
+ * the prune path).
283
+ */
284
+ async clearSoftDeleted(txHash: string): Promise<void> {
285
+ if (this.#slotDeletedTxs.has(txHash)) {
286
+ this.#slotDeletedTxs.delete(txHash);
287
+ await this.#slotDeletedDB.delete(txHash);
288
+ }
289
+ const existing = this.#state.get(txHash);
290
+ if (existing?.softDeleted) {
291
+ const state: DeletedTxState = { minedAtBlock: existing.minedAtBlock, softDeleted: false };
292
+ this.#state.set(txHash, state);
293
+ await this.#deletedTxsDB.set(txHash, serializeState(state));
294
+ }
295
+ }
296
+
297
+ /**
298
+ * Gets the count of transactions from pruned blocks.
299
+ */
300
+ getCount(): number {
301
+ return this.#state.size;
302
+ }
303
+
304
+ /**
305
+ * Gets all transaction hashes from pruned blocks.
306
+ */
307
+ getPrunedTxHashes(): string[] {
308
+ return Array.from(this.#state.keys());
309
+ }
310
+ }
@@ -68,6 +68,9 @@ export class FeePayerBalanceEvictionRule implements EvictionRule {
68
68
 
69
69
  if (txsToEvict.length > 0) {
70
70
  await pool.deleteTxs(txsToEvict);
71
+ this.log.verbose(`Evicted ${txsToEvict.length} txs due to insufficient fee payer balance`, {
72
+ txHashes: txsToEvict,
73
+ });
71
74
  }
72
75
 
73
76
  return {
@@ -41,9 +41,9 @@ export class InvalidTxsAfterMiningRule implements EvictionRule {
41
41
  }
42
42
 
43
43
  // Evict pending txs with an expiration timestamp less than or equal to the mined block timestamp
44
- if (meta.includeByTimestamp <= timestamp) {
44
+ if (meta.expirationTimestamp <= timestamp) {
45
45
  this.log.verbose(
46
- `Evicting tx ${meta.txHash} from pool due to the tx being expired (includeByTimestamp: ${meta.includeByTimestamp}, mined block timestamp: ${timestamp})`,
46
+ `Evicting tx ${meta.txHash} from pool due to the tx being expired (expirationTimestamp: ${meta.expirationTimestamp}, mined block timestamp: ${timestamp})`,
47
47
  );
48
48
  txsToEvict.push(meta.txHash);
49
49
  continue;
@@ -54,7 +54,7 @@ export class InvalidTxsAfterMiningRule implements EvictionRule {
54
54
  await pool.deleteTxs(txsToEvict);
55
55
  }
56
56
 
57
- this.log.debug(`Evicted ${txsToEvict.length} invalid txs after block mined`);
57
+ this.log.debug(`Evicted ${txsToEvict.length} invalid txs after block mined`, { txHashes: txsToEvict });
58
58
 
59
59
  return {
60
60
  reason: 'block_mined_invalid_txs',
@@ -81,7 +81,7 @@ export class InvalidTxsAfterReorgRule implements EvictionRule {
81
81
  this.log.verbose(`Kept ${keptCount} txs that did not reference pruned blocks`);
82
82
  }
83
83
 
84
- this.log.debug(`Evicted ${txsToEvict.length} invalid txs after reorg`);
84
+ this.log.info(`Evicted ${txsToEvict.length} invalid txs after reorg`, { txHashes: txsToEvict });
85
85
 
86
86
  return {
87
87
  reason: 'reorg_invalid_txs',
@@ -60,7 +60,9 @@ export class LowPriorityEvictionRule implements EvictionRule {
60
60
 
61
61
  const numNewTxsEvicted = context.newTxHashes.filter(newTxHash => txsToEvict.includes(newTxHash)).length;
62
62
 
63
- this.log.verbose(`Evicted ${txsToEvict.length} low priority txs, including ${numNewTxsEvicted} newly added txs`);
63
+ this.log.verbose(`Evicted ${txsToEvict.length} low priority txs, including ${numNewTxsEvicted} newly added txs`, {
64
+ txHashes: txsToEvict,
65
+ });
64
66
 
65
67
  return {
66
68
  reason: 'low_priority',
@@ -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';
@@ -37,6 +37,8 @@ export type TxPoolV2Config = {
37
37
  maxPendingTxCount: number;
38
38
  /** Maximum number of archived transactions to retain (0 = disabled) */
39
39
  archivedTxLimit: number;
40
+ /** Minimum age (ms) a transaction must have been in the pool before it's eligible for block building */
41
+ minTxPoolAgeMs: number;
40
42
  };
41
43
 
42
44
  /**
@@ -45,6 +47,7 @@ export type TxPoolV2Config = {
45
47
  export const DEFAULT_TX_POOL_V2_CONFIG: TxPoolV2Config = {
46
48
  maxPendingTxCount: 0, // 0 = disabled
47
49
  archivedTxLimit: 0, // 0 = disabled
50
+ minTxPoolAgeMs: 2_000,
48
51
  };
49
52
 
50
53
  /**
@@ -55,8 +58,8 @@ export type TxPoolV2Dependencies = {
55
58
  l2BlockSource: L2BlockSource;
56
59
  /** World state synchronizer for validating transactions after chain prunes */
57
60
  worldStateSynchronizer: WorldStateSynchronizer;
58
- /** Validator for transactions entering the pending pool */
59
- pendingTxValidator: TxValidator<Tx>;
61
+ /** Factory that creates a validator for re-validating pool transactions using metadata */
62
+ createTxValidator: () => Promise<TxValidator<TxMetaData>>;
60
63
  };
61
64
 
62
65
  /**
@@ -187,6 +190,9 @@ export interface TxPoolV2 extends TypedEventEmitter<TxPoolV2Events> {
187
190
  /** Gets pending transaction hashes sorted by priority (highest first) */
188
191
  getPendingTxHashes(): Promise<TxHash[]>;
189
192
 
193
+ /** Gets pending transaction hashes that have been in the pool long enough per minTxPoolAgeMs, sorted by priority (highest first) */
194
+ getEligiblePendingTxHashes(): Promise<TxHash[]>;
195
+
190
196
  /** Gets the count of pending transactions */
191
197
  getPendingTxCount(): Promise<number>;
192
198
 
@@ -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
+ expirationTimestamp: 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
@@ -41,23 +56,33 @@ export type TxMetaData = {
41
56
  readonly nullifiers: readonly string[];
42
57
 
43
58
  /** Timestamp by which the transaction must be included (for expiration checks) */
44
- readonly includeByTimestamp: bigint;
59
+ readonly expirationTimestamp: bigint;
60
+
61
+ /** Validator-compatible data, providing the same access patterns as Tx.data */
62
+ readonly data: TxMetaValidationData;
63
+
64
+ /** Timestamp (ms) when the tx was received into the pool. 0 for hydrated txs (always eligible). */
65
+ receivedAt: number;
45
66
  };
46
67
 
47
68
  /** Transaction state derived from TxMetaData fields and pool protection status */
48
- export type TxState = 'pending' | 'protected' | 'mined';
69
+ export type TxState = 'pending' | 'protected' | 'mined' | 'deleted';
49
70
 
50
71
  /**
51
72
  * Builds TxMetaData from a full Tx object.
52
73
  * Extracts all relevant fields for efficient in-memory storage and querying.
74
+ * Fr values are captured in closures for zero-cost re-validation.
53
75
  */
54
76
  export async function buildTxMetaData(tx: Tx): Promise<TxMetaData> {
55
77
  const txHash = tx.getTxHash().toString();
56
- const anchorBlockHeaderHash = (await tx.data.constants.anchorBlockHeader.hash()).toString();
78
+ const nullifierFrs = tx.data.getNonEmptyNullifiers();
79
+ const nullifiers = nullifierFrs.map(n => n.toString());
80
+ const anchorBlockHeaderHashFr = await tx.data.constants.anchorBlockHeader.hash();
81
+ const anchorBlockHeaderHash = anchorBlockHeaderHashFr.toString();
82
+ const expirationTimestamp = tx.data.expirationTimestamp;
83
+ const anchorBlockNumber = tx.data.constants.anchorBlockHeader.globalVariables.blockNumber;
57
84
  const priorityFee = getTxPriorityFee(tx);
58
85
  const feePayer = tx.data.feePayer.toString();
59
- const nullifiers = tx.data.getNonEmptyNullifiers().map(n => n.toString());
60
- const includeByTimestamp = tx.data.includeByTimestamp;
61
86
 
62
87
  const { feeLimit, claimAmount } = await getFeePayerBalanceDelta(tx, ProtocolContractAddress.FeeJuice);
63
88
 
@@ -69,7 +94,18 @@ export async function buildTxMetaData(tx: Tx): Promise<TxMetaData> {
69
94
  claimAmount,
70
95
  feeLimit,
71
96
  nullifiers,
72
- includeByTimestamp,
97
+ expirationTimestamp,
98
+ receivedAt: 0,
99
+ data: {
100
+ getNonEmptyNullifiers: () => nullifierFrs,
101
+ expirationTimestamp,
102
+ constants: {
103
+ anchorBlockHeader: {
104
+ hash: () => Promise.resolve(anchorBlockHeaderHashFr),
105
+ globalVariables: { blockNumber: anchorBlockNumber },
106
+ },
107
+ },
108
+ },
73
109
  };
74
110
  }
75
111
 
@@ -159,3 +195,17 @@ export function checkNullifierConflict(
159
195
 
160
196
  return { shouldIgnore: false, txHashesToEvict };
161
197
  }
198
+
199
+ /** Creates a stub TxMetaValidationData for tests that don't exercise validators. */
200
+ export function stubTxMetaValidationData(overrides: { expirationTimestamp?: bigint } = {}): TxMetaValidationData {
201
+ return {
202
+ getNonEmptyNullifiers: () => [],
203
+ expirationTimestamp: overrides.expirationTimestamp ?? 0n,
204
+ constants: {
205
+ anchorBlockHeader: {
206
+ hash: () => Promise.resolve(new BlockHash(Fr.ZERO)),
207
+ globalVariables: { blockNumber: BlockNumber(0) },
208
+ },
209
+ },
210
+ };
211
+ }
@@ -72,7 +72,7 @@ export class TxPoolIndices {
72
72
  * Iterates pending transaction hashes in priority order.
73
73
  * @param order - 'desc' for highest priority first, 'asc' for lowest priority first
74
74
  */
75
- *iteratePendingByPriority(order: 'asc' | 'desc'): Generator<string> {
75
+ *iteratePendingByPriority(order: 'asc' | 'desc', filter?: (hash: string) => boolean): Generator<string> {
76
76
  // Use compareFee from tx_metadata, swap args for descending order
77
77
  const feeCompareFn = order === 'desc' ? (a: bigint, b: bigint) => compareFee(b, a) : compareFee;
78
78
  const hashCompareFn = order === 'desc' ? (a: string, b: string) => compareTxHash(b, a) : compareTxHash;
@@ -84,11 +84,27 @@ export class TxPoolIndices {
84
84
  // Use compareTxHash from tx_metadata, swap args for descending order
85
85
  const sortedHashes = [...hashesAtFee].sort(hashCompareFn);
86
86
  for (const hash of sortedHashes) {
87
- yield hash;
87
+ if (filter === undefined || filter(hash)) {
88
+ yield hash;
89
+ }
88
90
  }
89
91
  }
90
92
  }
91
93
 
94
+ /**
95
+ * Iterates pending transaction hashes in priority order, skipping txs received after maxReceivedAt.
96
+ * @param order - 'desc' for highest priority first, 'asc' for lowest priority first
97
+ * @param maxReceivedAt - Only yield txs with receivedAt <= this value
98
+ */
99
+ *iterateEligiblePendingByPriority(order: 'asc' | 'desc', maxReceivedAt: number): Generator<string> {
100
+ const filter = (hash: string) => {
101
+ const meta = this.#metadata.get(hash);
102
+ return meta !== undefined && meta.receivedAt <= maxReceivedAt;
103
+ };
104
+
105
+ yield* this.iteratePendingByPriority(order, filter);
106
+ }
107
+
92
108
  /** Iterates all metadata entries */
93
109
  *iterateMetadata(): Generator<[string, TxMetaData]> {
94
110
  yield* this.#metadata;
@@ -1,6 +1,7 @@
1
1
  import { SlotNumber } from '@aztec/foundation/branded-types';
2
2
  import { type Logger, createLogger } from '@aztec/foundation/log';
3
3
  import { SerialQueue } from '@aztec/foundation/queue';
4
+ import { DateProvider } from '@aztec/foundation/timer';
4
5
  import type { TypedEventEmitter } from '@aztec/foundation/types';
5
6
  import type { AztecAsyncKVStore } from '@aztec/kv-store';
6
7
  import type { L2Block, L2BlockId } from '@aztec/stdlib/block';
@@ -35,6 +36,7 @@ export class AztecKVTxPoolV2 extends (EventEmitter as new () => TypedEventEmitte
35
36
  deps: TxPoolV2Dependencies,
36
37
  telemetry: TelemetryClient = getTelemetryClient(),
37
38
  config: Partial<TxPoolV2Config> = {},
39
+ dateProvider: DateProvider = new DateProvider(),
38
40
  log = createLogger('p2p:tx_pool_v2'),
39
41
  ) {
40
42
  super();
@@ -59,7 +61,7 @@ export class AztecKVTxPoolV2 extends (EventEmitter as new () => TypedEventEmitte
59
61
  };
60
62
 
61
63
  // Create the implementation
62
- this.#impl = new TxPoolV2Impl(store, archiveStore, deps, callbacks, config, log);
64
+ this.#impl = new TxPoolV2Impl(store, archiveStore, deps, callbacks, config, dateProvider, log);
63
65
  }
64
66
 
65
67
  // ============================================================================
@@ -132,6 +134,10 @@ export class AztecKVTxPoolV2 extends (EventEmitter as new () => TypedEventEmitte
132
134
  return this.#queue.put(() => Promise.resolve(this.#impl.getPendingTxHashes()));
133
135
  }
134
136
 
137
+ getEligiblePendingTxHashes(): Promise<TxHash[]> {
138
+ return this.#queue.put(() => Promise.resolve(this.#impl.getEligiblePendingTxHashes()));
139
+ }
140
+
135
141
  getPendingTxCount(): Promise<number> {
136
142
  return this.#queue.put(() => Promise.resolve(this.#impl.getPendingTxCount()));
137
143
  }