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

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 (522) hide show
  1. package/README.md +130 -3
  2. package/dest/bootstrap/bootstrap.d.ts +1 -1
  3. package/dest/bootstrap/bootstrap.d.ts.map +1 -1
  4. package/dest/bootstrap/bootstrap.js +22 -6
  5. package/dest/client/factory.d.ts +11 -10
  6. package/dest/client/factory.d.ts.map +1 -1
  7. package/dest/client/factory.js +64 -17
  8. package/dest/client/interface.d.ts +86 -40
  9. package/dest/client/interface.d.ts.map +1 -1
  10. package/dest/client/p2p_client.d.ts +67 -64
  11. package/dest/client/p2p_client.d.ts.map +1 -1
  12. package/dest/client/p2p_client.js +251 -251
  13. package/dest/config.d.ts +173 -94
  14. package/dest/config.d.ts.map +1 -1
  15. package/dest/config.js +158 -44
  16. package/dest/errors/p2p-service.error.d.ts +9 -0
  17. package/dest/errors/p2p-service.error.d.ts.map +1 -0
  18. package/dest/errors/p2p-service.error.js +10 -0
  19. package/dest/errors/reqresp.error.d.ts +9 -16
  20. package/dest/errors/reqresp.error.d.ts.map +1 -1
  21. package/dest/errors/reqresp.error.js +10 -17
  22. package/dest/errors/tx-pool.error.d.ts +8 -0
  23. package/dest/errors/tx-pool.error.d.ts.map +1 -0
  24. package/dest/errors/tx-pool.error.js +9 -0
  25. package/dest/index.d.ts +2 -2
  26. package/dest/index.d.ts.map +1 -1
  27. package/dest/index.js +1 -1
  28. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +106 -55
  29. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  30. package/dest/mem_pools/attestation_pool/attestation_pool.js +302 -195
  31. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +1 -1
  32. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  33. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +274 -115
  34. package/dest/mem_pools/attestation_pool/index.d.ts +2 -2
  35. package/dest/mem_pools/attestation_pool/index.d.ts.map +1 -1
  36. package/dest/mem_pools/attestation_pool/index.js +1 -1
  37. package/dest/mem_pools/attestation_pool/mocks.d.ts +2 -2
  38. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  39. package/dest/mem_pools/attestation_pool/mocks.js +7 -5
  40. package/dest/mem_pools/index.d.ts +2 -2
  41. package/dest/mem_pools/index.d.ts.map +1 -1
  42. package/dest/mem_pools/instrumentation.d.ts +12 -5
  43. package/dest/mem_pools/instrumentation.d.ts.map +1 -1
  44. package/dest/mem_pools/instrumentation.js +38 -17
  45. package/dest/mem_pools/interface.d.ts +3 -3
  46. package/dest/mem_pools/interface.d.ts.map +1 -1
  47. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.d.ts +10 -5
  48. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.d.ts.map +1 -1
  49. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.js +29 -20
  50. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts +104 -0
  51. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts.map +1 -0
  52. package/dest/mem_pools/tx_pool_v2/deleted_pool.js +251 -0
  53. package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.d.ts +3 -3
  54. package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.d.ts.map +1 -1
  55. package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.js +18 -9
  56. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts +1 -1
  57. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -1
  58. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.js +8 -4
  59. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts +3 -3
  60. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts.map +1 -1
  61. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.js +15 -5
  62. package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts +3 -2
  63. package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts.map +1 -1
  64. package/dest/mem_pools/tx_pool_v2/eviction/index.js +2 -1
  65. package/dest/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.d.ts +16 -0
  66. package/dest/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.d.ts.map +1 -0
  67. package/dest/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.js +64 -0
  68. package/dest/mem_pools/tx_pool_v2/eviction/interfaces.d.ts +54 -5
  69. package/dest/mem_pools/tx_pool_v2/eviction/interfaces.d.ts.map +1 -1
  70. package/dest/mem_pools/tx_pool_v2/eviction/interfaces.js +8 -0
  71. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.d.ts +1 -1
  72. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.d.ts.map +1 -1
  73. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.js +6 -5
  74. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.d.ts +1 -1
  75. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.d.ts.map +1 -1
  76. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.js +9 -8
  77. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts +2 -2
  78. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts.map +1 -1
  79. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.js +15 -6
  80. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.d.ts +4 -4
  81. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.d.ts.map +1 -1
  82. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.js +16 -4
  83. package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.d.ts +3 -3
  84. package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.d.ts.map +1 -1
  85. package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.js +3 -3
  86. package/dest/mem_pools/tx_pool_v2/index.d.ts +3 -2
  87. package/dest/mem_pools/tx_pool_v2/index.d.ts.map +1 -1
  88. package/dest/mem_pools/tx_pool_v2/index.js +2 -1
  89. package/dest/mem_pools/tx_pool_v2/instrumentation.d.ts +25 -0
  90. package/dest/mem_pools/tx_pool_v2/instrumentation.d.ts.map +1 -0
  91. package/dest/mem_pools/tx_pool_v2/instrumentation.js +67 -0
  92. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +62 -16
  93. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
  94. package/dest/mem_pools/tx_pool_v2/interfaces.js +6 -1
  95. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +81 -15
  96. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
  97. package/dest/mem_pools/tx_pool_v2/tx_metadata.js +147 -19
  98. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts +26 -3
  99. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts.map +1 -1
  100. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.js +79 -43
  101. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +27 -7
  102. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -1
  103. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.js +94 -32
  104. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +62 -9
  105. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
  106. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +537 -246
  107. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +10 -3
  108. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
  109. package/dest/msg_validators/attestation_validator/attestation_validator.js +38 -15
  110. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +7 -3
  111. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -1
  112. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.js +4 -5
  113. package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts +12 -5
  114. package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts.map +1 -1
  115. package/dest/msg_validators/proposal_validator/block_proposal_validator.js +11 -3
  116. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts +12 -5
  117. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts.map +1 -1
  118. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.js +23 -3
  119. package/dest/msg_validators/proposal_validator/proposal_validator.d.ts +36 -9
  120. package/dest/msg_validators/proposal_validator/proposal_validator.d.ts.map +1 -1
  121. package/dest/msg_validators/proposal_validator/proposal_validator.js +130 -48
  122. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +4 -4
  123. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
  124. package/dest/msg_validators/tx_validator/aggregate_tx_validator.js +11 -18
  125. package/dest/msg_validators/tx_validator/allowed_public_setup.d.ts +2 -1
  126. package/dest/msg_validators/tx_validator/allowed_public_setup.d.ts.map +1 -1
  127. package/dest/msg_validators/tx_validator/allowed_public_setup.js +25 -21
  128. package/dest/msg_validators/tx_validator/allowed_setup_helpers.d.ts +17 -0
  129. package/dest/msg_validators/tx_validator/allowed_setup_helpers.d.ts.map +1 -0
  130. package/dest/msg_validators/tx_validator/allowed_setup_helpers.js +24 -0
  131. package/dest/msg_validators/tx_validator/archive_cache.js +1 -1
  132. package/dest/msg_validators/tx_validator/block_header_validator.d.ts +16 -3
  133. package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
  134. package/dest/msg_validators/tx_validator/block_header_validator.js +1 -1
  135. package/dest/msg_validators/tx_validator/cached_tx_validator.d.ts +15 -0
  136. package/dest/msg_validators/tx_validator/cached_tx_validator.d.ts.map +1 -0
  137. package/dest/msg_validators/tx_validator/cached_tx_validator.js +19 -0
  138. package/dest/msg_validators/tx_validator/contract_instance_validator.d.ts +9 -0
  139. package/dest/msg_validators/tx_validator/contract_instance_validator.d.ts.map +1 -0
  140. package/dest/msg_validators/tx_validator/contract_instance_validator.js +48 -0
  141. package/dest/msg_validators/tx_validator/data_validator.d.ts +2 -1
  142. package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
  143. package/dest/msg_validators/tx_validator/data_validator.js +36 -2
  144. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts +13 -3
  145. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts.map +1 -1
  146. package/dest/msg_validators/tx_validator/double_spend_validator.js +4 -4
  147. package/dest/msg_validators/tx_validator/factory.d.ts +133 -7
  148. package/dest/msg_validators/tx_validator/factory.d.ts.map +1 -1
  149. package/dest/msg_validators/tx_validator/factory.js +261 -61
  150. package/dest/msg_validators/tx_validator/fee_payer_balance.d.ts +1 -1
  151. package/dest/msg_validators/tx_validator/fee_payer_balance.d.ts.map +1 -1
  152. package/dest/msg_validators/tx_validator/fee_payer_balance.js +6 -2
  153. package/dest/msg_validators/tx_validator/gas_validator.d.ts +104 -3
  154. package/dest/msg_validators/tx_validator/gas_validator.d.ts.map +1 -1
  155. package/dest/msg_validators/tx_validator/gas_validator.js +137 -53
  156. package/dest/msg_validators/tx_validator/index.d.ts +5 -1
  157. package/dest/msg_validators/tx_validator/index.d.ts.map +1 -1
  158. package/dest/msg_validators/tx_validator/index.js +4 -0
  159. package/dest/msg_validators/tx_validator/metadata_validator.d.ts +1 -1
  160. package/dest/msg_validators/tx_validator/metadata_validator.d.ts.map +1 -1
  161. package/dest/msg_validators/tx_validator/metadata_validator.js +4 -4
  162. package/dest/msg_validators/tx_validator/nullifier_cache.d.ts +14 -0
  163. package/dest/msg_validators/tx_validator/nullifier_cache.d.ts.map +1 -0
  164. package/dest/msg_validators/tx_validator/nullifier_cache.js +24 -0
  165. package/dest/msg_validators/tx_validator/phases_validator.d.ts +22 -2
  166. package/dest/msg_validators/tx_validator/phases_validator.d.ts.map +1 -1
  167. package/dest/msg_validators/tx_validator/phases_validator.js +72 -24
  168. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +20 -4
  169. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -1
  170. package/dest/msg_validators/tx_validator/timestamp_validator.js +6 -6
  171. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts +2 -1
  172. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts.map +1 -1
  173. package/dest/msg_validators/tx_validator/tx_proof_validator.js +2 -0
  174. package/dest/msg_validators/tx_validator/tx_validation_cache.d.ts +48 -0
  175. package/dest/msg_validators/tx_validator/tx_validation_cache.d.ts.map +1 -0
  176. package/dest/msg_validators/tx_validator/tx_validation_cache.js +68 -0
  177. package/dest/services/data_store.d.ts +1 -1
  178. package/dest/services/data_store.d.ts.map +1 -1
  179. package/dest/services/data_store.js +5 -5
  180. package/dest/services/discv5/discV5_service.d.ts +13 -2
  181. package/dest/services/discv5/discV5_service.d.ts.map +1 -1
  182. package/dest/services/discv5/discV5_service.js +119 -23
  183. package/dest/services/discv5/persisted_enr_store.d.ts +38 -0
  184. package/dest/services/discv5/persisted_enr_store.d.ts.map +1 -0
  185. package/dest/services/discv5/persisted_enr_store.js +145 -0
  186. package/dest/services/dummy_service.d.ts +25 -19
  187. package/dest/services/dummy_service.d.ts.map +1 -1
  188. package/dest/services/dummy_service.js +28 -20
  189. package/dest/services/encoding.d.ts +24 -7
  190. package/dest/services/encoding.d.ts.map +1 -1
  191. package/dest/services/encoding.js +43 -26
  192. package/dest/services/gossipsub/topic_score_params.d.ts +23 -17
  193. package/dest/services/gossipsub/topic_score_params.d.ts.map +1 -1
  194. package/dest/services/gossipsub/topic_score_params.js +36 -24
  195. package/dest/services/libp2p/instrumentation.d.ts +6 -1
  196. package/dest/services/libp2p/instrumentation.d.ts.map +1 -1
  197. package/dest/services/libp2p/instrumentation.js +21 -0
  198. package/dest/services/libp2p/libp2p_service.d.ts +71 -60
  199. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  200. package/dest/services/libp2p/libp2p_service.js +602 -407
  201. package/dest/services/peer-manager/metrics.d.ts +3 -1
  202. package/dest/services/peer-manager/metrics.d.ts.map +1 -1
  203. package/dest/services/peer-manager/metrics.js +6 -0
  204. package/dest/services/peer-manager/peer_manager.d.ts +31 -3
  205. package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
  206. package/dest/services/peer-manager/peer_manager.js +127 -26
  207. package/dest/services/peer-manager/peer_scoring.d.ts +34 -2
  208. package/dest/services/peer-manager/peer_scoring.d.ts.map +1 -1
  209. package/dest/services/peer-manager/peer_scoring.js +102 -13
  210. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts +5 -8
  211. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts.map +1 -1
  212. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.js +95 -127
  213. package/dest/services/reqresp/batch-tx-requester/interface.d.ts +11 -11
  214. package/dest/services/reqresp/batch-tx-requester/interface.d.ts.map +1 -1
  215. package/dest/services/reqresp/batch-tx-requester/missing_txs.d.ts +11 -13
  216. package/dest/services/reqresp/batch-tx-requester/missing_txs.d.ts.map +1 -1
  217. package/dest/services/reqresp/batch-tx-requester/missing_txs.js +31 -46
  218. package/dest/services/reqresp/batch-tx-requester/peer_collection.d.ts +19 -11
  219. package/dest/services/reqresp/batch-tx-requester/peer_collection.d.ts.map +1 -1
  220. package/dest/services/reqresp/batch-tx-requester/peer_collection.js +52 -15
  221. package/dest/services/reqresp/batch-tx-requester/tx_validator.d.ts +5 -14
  222. package/dest/services/reqresp/batch-tx-requester/tx_validator.d.ts.map +1 -1
  223. package/dest/services/reqresp/batch-tx-requester/tx_validator.js +6 -20
  224. package/dest/services/reqresp/config.d.ts +15 -3
  225. package/dest/services/reqresp/config.d.ts.map +1 -1
  226. package/dest/services/reqresp/config.js +11 -0
  227. package/dest/services/reqresp/interface.d.ts +25 -18
  228. package/dest/services/reqresp/interface.d.ts.map +1 -1
  229. package/dest/services/reqresp/interface.js +23 -19
  230. package/dest/services/reqresp/metrics.d.ts +1 -1
  231. package/dest/services/reqresp/metrics.d.ts.map +1 -1
  232. package/dest/services/reqresp/metrics.js +0 -1
  233. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts +4 -3
  234. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts.map +1 -1
  235. package/dest/services/reqresp/protocols/block_txs/bitvector.js +3 -2
  236. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +3 -3
  237. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -1
  238. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.js +31 -28
  239. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +18 -7
  240. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
  241. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +57 -24
  242. package/dest/services/reqresp/protocols/index.d.ts +1 -2
  243. package/dest/services/reqresp/protocols/index.d.ts.map +1 -1
  244. package/dest/services/reqresp/protocols/index.js +0 -1
  245. package/dest/services/reqresp/protocols/tx.d.ts +7 -1
  246. package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
  247. package/dest/services/reqresp/protocols/tx.js +21 -3
  248. package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts +5 -4
  249. package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts.map +1 -1
  250. package/dest/services/reqresp/rate-limiter/rate_limiter.js +10 -8
  251. package/dest/services/reqresp/rate-limiter/rate_limits.d.ts +1 -1
  252. package/dest/services/reqresp/rate-limiter/rate_limits.d.ts.map +1 -1
  253. package/dest/services/reqresp/rate-limiter/rate_limits.js +3 -13
  254. package/dest/services/reqresp/reqresp.d.ts +7 -29
  255. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  256. package/dest/services/reqresp/reqresp.js +85 -219
  257. package/dest/services/service.d.ts +63 -19
  258. package/dest/services/service.d.ts.map +1 -1
  259. package/dest/services/tx_collection/config.d.ts +11 -14
  260. package/dest/services/tx_collection/config.d.ts.map +1 -1
  261. package/dest/services/tx_collection/config.js +26 -33
  262. package/dest/services/tx_collection/file_store_tx_collection.d.ts +37 -0
  263. package/dest/services/tx_collection/file_store_tx_collection.d.ts.map +1 -0
  264. package/dest/services/tx_collection/file_store_tx_collection.js +127 -0
  265. package/dest/services/tx_collection/file_store_tx_source.d.ts +38 -0
  266. package/dest/services/tx_collection/file_store_tx_source.d.ts.map +1 -0
  267. package/dest/services/tx_collection/file_store_tx_source.js +100 -0
  268. package/dest/services/tx_collection/index.d.ts +3 -3
  269. package/dest/services/tx_collection/index.d.ts.map +1 -1
  270. package/dest/services/tx_collection/index.js +1 -1
  271. package/dest/services/tx_collection/instrumentation.js +1 -2
  272. package/dest/services/tx_collection/request_tracker.d.ts +53 -0
  273. package/dest/services/tx_collection/request_tracker.d.ts.map +1 -0
  274. package/dest/services/tx_collection/request_tracker.js +84 -0
  275. package/dest/services/tx_collection/tx_collection.d.ts +45 -51
  276. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
  277. package/dest/services/tx_collection/tx_collection.js +296 -68
  278. package/dest/services/tx_collection/tx_collection_sink.d.ts +19 -9
  279. package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -1
  280. package/dest/services/tx_collection/tx_collection_sink.js +28 -31
  281. package/dest/services/tx_collection/tx_source.d.ts +13 -7
  282. package/dest/services/tx_collection/tx_source.d.ts.map +1 -1
  283. package/dest/services/tx_collection/tx_source.js +29 -7
  284. package/dest/services/tx_file_store/config.d.ts +1 -3
  285. package/dest/services/tx_file_store/config.d.ts.map +1 -1
  286. package/dest/services/tx_file_store/config.js +0 -4
  287. package/dest/services/tx_file_store/tx_file_store.d.ts +4 -5
  288. package/dest/services/tx_file_store/tx_file_store.d.ts.map +1 -1
  289. package/dest/services/tx_file_store/tx_file_store.js +13 -20
  290. package/dest/services/tx_provider.d.ts +6 -4
  291. package/dest/services/tx_provider.d.ts.map +1 -1
  292. package/dest/services/tx_provider.js +12 -8
  293. package/dest/test-helpers/make-test-p2p-clients.d.ts +7 -8
  294. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  295. package/dest/test-helpers/make-test-p2p-clients.js +5 -3
  296. package/dest/test-helpers/mock-pubsub.d.ts +56 -6
  297. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  298. package/dest/test-helpers/mock-pubsub.js +128 -14
  299. package/dest/test-helpers/reqresp-nodes.d.ts +5 -7
  300. package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
  301. package/dest/test-helpers/reqresp-nodes.js +17 -19
  302. package/dest/test-helpers/test_tx_provider.d.ts +3 -1
  303. package/dest/test-helpers/test_tx_provider.d.ts.map +1 -1
  304. package/dest/test-helpers/test_tx_provider.js +3 -0
  305. package/dest/test-helpers/testbench-utils.d.ts +49 -37
  306. package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
  307. package/dest/test-helpers/testbench-utils.js +142 -50
  308. package/dest/testbench/p2p_client_testbench_worker.d.ts +3 -5
  309. package/dest/testbench/p2p_client_testbench_worker.d.ts.map +1 -1
  310. package/dest/testbench/p2p_client_testbench_worker.js +112 -49
  311. package/dest/testbench/worker_client_manager.d.ts +12 -6
  312. package/dest/testbench/worker_client_manager.d.ts.map +1 -1
  313. package/dest/testbench/worker_client_manager.js +57 -11
  314. package/dest/util.d.ts +13 -8
  315. package/dest/util.d.ts.map +1 -1
  316. package/dest/util.js +35 -14
  317. package/dest/versioning.d.ts +3 -6
  318. package/dest/versioning.d.ts.map +1 -1
  319. package/dest/versioning.js +3 -24
  320. package/package.json +17 -17
  321. package/src/bootstrap/bootstrap.ts +17 -3
  322. package/src/client/factory.ts +129 -28
  323. package/src/client/interface.ts +88 -48
  324. package/src/client/p2p_client.ts +284 -317
  325. package/src/client/test/{tx_proposal_collector/README.md → p2p_client.batch_tx_requester.bench.README.md} +23 -53
  326. package/src/config.ts +280 -49
  327. package/src/errors/p2p-service.error.ts +11 -0
  328. package/src/errors/reqresp.error.ts +13 -19
  329. package/src/errors/tx-pool.error.ts +12 -0
  330. package/src/index.ts +1 -1
  331. package/src/mem_pools/attestation_pool/attestation_pool.ts +373 -236
  332. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +298 -119
  333. package/src/mem_pools/attestation_pool/index.ts +3 -3
  334. package/src/mem_pools/attestation_pool/mocks.ts +14 -8
  335. package/src/mem_pools/index.ts +1 -1
  336. package/src/mem_pools/instrumentation.ts +27 -17
  337. package/src/mem_pools/interface.ts +2 -2
  338. package/src/mem_pools/tx_pool_v2/README.md +85 -11
  339. package/src/mem_pools/tx_pool_v2/archive/tx_archive.ts +30 -20
  340. package/src/mem_pools/tx_pool_v2/deleted_pool.ts +321 -0
  341. package/src/mem_pools/tx_pool_v2/eviction/eviction_manager.ts +21 -8
  342. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.ts +6 -4
  343. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.ts +21 -6
  344. package/src/mem_pools/tx_pool_v2/eviction/index.ts +5 -0
  345. package/src/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.ts +67 -0
  346. package/src/mem_pools/tx_pool_v2/eviction/interfaces.ts +59 -4
  347. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.ts +4 -6
  348. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.ts +8 -10
  349. package/src/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.ts +14 -10
  350. package/src/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.ts +33 -6
  351. package/src/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.ts +4 -3
  352. package/src/mem_pools/tx_pool_v2/index.ts +2 -1
  353. package/src/mem_pools/tx_pool_v2/instrumentation.ts +96 -0
  354. package/src/mem_pools/tx_pool_v2/interfaces.ts +63 -16
  355. package/src/mem_pools/tx_pool_v2/tx_metadata.ts +215 -27
  356. package/src/mem_pools/tx_pool_v2/tx_pool_indices.ts +91 -43
  357. package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +122 -33
  358. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +602 -242
  359. package/src/msg_validators/attestation_validator/README.md +49 -0
  360. package/src/msg_validators/attestation_validator/attestation_validator.ts +46 -14
  361. package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +15 -7
  362. package/src/msg_validators/proposal_validator/README.md +123 -0
  363. package/src/msg_validators/proposal_validator/block_proposal_validator.ts +26 -4
  364. package/src/msg_validators/proposal_validator/checkpoint_proposal_validator.ts +43 -7
  365. package/src/msg_validators/proposal_validator/proposal_validator.ts +155 -49
  366. package/src/msg_validators/tx_validator/README.md +129 -0
  367. package/src/msg_validators/tx_validator/aggregate_tx_validator.ts +8 -17
  368. package/src/msg_validators/tx_validator/allowed_public_setup.ts +22 -27
  369. package/src/msg_validators/tx_validator/allowed_setup_helpers.ts +31 -0
  370. package/src/msg_validators/tx_validator/archive_cache.ts +1 -1
  371. package/src/msg_validators/tx_validator/block_header_validator.ts +15 -3
  372. package/src/msg_validators/tx_validator/cached_tx_validator.ts +31 -0
  373. package/src/msg_validators/tx_validator/contract_instance_validator.ts +56 -0
  374. package/src/msg_validators/tx_validator/data_validator.ts +44 -1
  375. package/src/msg_validators/tx_validator/double_spend_validator.ts +11 -6
  376. package/src/msg_validators/tx_validator/factory.ts +413 -80
  377. package/src/msg_validators/tx_validator/fee_payer_balance.ts +6 -2
  378. package/src/msg_validators/tx_validator/gas_validator.ts +195 -54
  379. package/src/msg_validators/tx_validator/index.ts +4 -0
  380. package/src/msg_validators/tx_validator/metadata_validator.ts +12 -4
  381. package/src/msg_validators/tx_validator/nullifier_cache.ts +30 -0
  382. package/src/msg_validators/tx_validator/phases_validator.ts +82 -27
  383. package/src/msg_validators/tx_validator/timestamp_validator.ts +23 -18
  384. package/src/msg_validators/tx_validator/tx_proof_validator.ts +2 -0
  385. package/src/msg_validators/tx_validator/tx_validation_cache.ts +101 -0
  386. package/src/services/data_store.ts +5 -13
  387. package/src/services/discv5/discV5_service.ts +126 -14
  388. package/src/services/discv5/persisted_enr_store.ts +158 -0
  389. package/src/services/dummy_service.ts +38 -46
  390. package/src/services/encoding.ts +42 -26
  391. package/src/services/gossipsub/README.md +59 -22
  392. package/src/services/gossipsub/topic_score_params.ts +58 -31
  393. package/src/services/libp2p/instrumentation.ts +22 -0
  394. package/src/services/libp2p/libp2p_service.ts +687 -466
  395. package/src/services/peer-manager/metrics.ts +7 -0
  396. package/src/services/peer-manager/peer_manager.ts +140 -25
  397. package/src/services/peer-manager/peer_scoring.ts +102 -8
  398. package/src/services/reqresp/README.md +194 -0
  399. package/src/services/reqresp/batch-tx-requester/README.md +46 -7
  400. package/src/services/reqresp/batch-tx-requester/batch_tx_requester.ts +92 -138
  401. package/src/services/reqresp/batch-tx-requester/interface.ts +14 -10
  402. package/src/services/reqresp/batch-tx-requester/missing_txs.ts +30 -71
  403. package/src/services/reqresp/batch-tx-requester/peer_collection.ts +68 -24
  404. package/src/services/reqresp/batch-tx-requester/tx_validator.ts +12 -25
  405. package/src/services/reqresp/config.ts +19 -2
  406. package/src/services/reqresp/interface.ts +45 -46
  407. package/src/services/reqresp/metrics.ts +0 -1
  408. package/src/services/reqresp/protocols/block_txs/bitvector.ts +3 -2
  409. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +36 -32
  410. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +64 -20
  411. package/src/services/reqresp/protocols/index.ts +0 -1
  412. package/src/services/reqresp/protocols/tx.ts +23 -3
  413. package/src/services/reqresp/rate-limiter/rate_limiter.ts +13 -9
  414. package/src/services/reqresp/rate-limiter/rate_limits.ts +3 -13
  415. package/src/services/reqresp/reqresp.ts +100 -263
  416. package/src/services/service.ts +80 -35
  417. package/src/services/tx_collection/config.ts +40 -49
  418. package/src/services/tx_collection/file_store_tx_collection.ts +153 -0
  419. package/src/services/tx_collection/file_store_tx_source.ts +129 -0
  420. package/src/services/tx_collection/index.ts +2 -6
  421. package/src/services/tx_collection/instrumentation.ts +1 -1
  422. package/src/services/tx_collection/request_tracker.ts +127 -0
  423. package/src/services/tx_collection/tx_collection.ts +362 -110
  424. package/src/services/tx_collection/tx_collection_sink.ts +32 -36
  425. package/src/services/tx_collection/tx_source.ts +29 -8
  426. package/src/services/tx_file_store/config.ts +0 -6
  427. package/src/services/tx_file_store/tx_file_store.ts +15 -25
  428. package/src/services/tx_provider.ts +15 -9
  429. package/src/test-helpers/make-test-p2p-clients.ts +7 -6
  430. package/src/test-helpers/mock-pubsub.ts +152 -14
  431. package/src/test-helpers/reqresp-nodes.ts +15 -28
  432. package/src/test-helpers/test_tx_provider.ts +5 -0
  433. package/src/test-helpers/testbench-utils.ts +171 -68
  434. package/src/testbench/p2p_client_testbench_worker.ts +121 -64
  435. package/src/testbench/worker_client_manager.ts +72 -25
  436. package/src/util.ts +40 -19
  437. package/src/versioning.ts +3 -33
  438. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.d.ts +0 -2
  439. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.d.ts.map +0 -1
  440. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.js +0 -305
  441. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.d.ts +0 -73
  442. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.d.ts.map +0 -1
  443. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.js +0 -8
  444. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts +0 -125
  445. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts.map +0 -1
  446. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.js +0 -596
  447. package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts +0 -32
  448. package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts.map +0 -1
  449. package/dest/mem_pools/tx_pool/eviction/eviction_manager.js +0 -112
  450. package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts +0 -157
  451. package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts.map +0 -1
  452. package/dest/mem_pools/tx_pool/eviction/eviction_strategy.js +0 -52
  453. package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts +0 -16
  454. package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts.map +0 -1
  455. package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.js +0 -122
  456. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.d.ts +0 -17
  457. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.d.ts.map +0 -1
  458. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.js +0 -84
  459. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.d.ts +0 -19
  460. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.d.ts.map +0 -1
  461. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.js +0 -78
  462. package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.d.ts +0 -26
  463. package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.d.ts.map +0 -1
  464. package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.js +0 -84
  465. package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.d.ts +0 -25
  466. package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.d.ts.map +0 -1
  467. package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.js +0 -57
  468. package/dest/mem_pools/tx_pool/index.d.ts +0 -3
  469. package/dest/mem_pools/tx_pool/index.d.ts.map +0 -1
  470. package/dest/mem_pools/tx_pool/index.js +0 -2
  471. package/dest/mem_pools/tx_pool/priority.d.ts +0 -12
  472. package/dest/mem_pools/tx_pool/priority.d.ts.map +0 -1
  473. package/dest/mem_pools/tx_pool/priority.js +0 -15
  474. package/dest/mem_pools/tx_pool/tx_pool.d.ts +0 -127
  475. package/dest/mem_pools/tx_pool/tx_pool.d.ts.map +0 -1
  476. package/dest/mem_pools/tx_pool/tx_pool.js +0 -3
  477. package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts +0 -7
  478. package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts.map +0 -1
  479. package/dest/mem_pools/tx_pool/tx_pool_test_suite.js +0 -400
  480. package/dest/msg_validators/clock_tolerance.d.ts +0 -21
  481. package/dest/msg_validators/clock_tolerance.d.ts.map +0 -1
  482. package/dest/msg_validators/clock_tolerance.js +0 -37
  483. package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts +0 -23
  484. package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts.map +0 -1
  485. package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.js +0 -212
  486. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts +0 -64
  487. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts.map +0 -1
  488. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.js +0 -151
  489. package/dest/services/reqresp/protocols/block.d.ts +0 -9
  490. package/dest/services/reqresp/protocols/block.d.ts.map +0 -1
  491. package/dest/services/reqresp/protocols/block.js +0 -32
  492. package/dest/services/tx_collection/fast_tx_collection.d.ts +0 -52
  493. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +0 -1
  494. package/dest/services/tx_collection/fast_tx_collection.js +0 -307
  495. package/dest/services/tx_collection/proposal_tx_collector.d.ts +0 -48
  496. package/dest/services/tx_collection/proposal_tx_collector.d.ts.map +0 -1
  497. package/dest/services/tx_collection/proposal_tx_collector.js +0 -49
  498. package/dest/services/tx_collection/slow_tx_collection.d.ts +0 -53
  499. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +0 -1
  500. package/dest/services/tx_collection/slow_tx_collection.js +0 -177
  501. package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker.ts +0 -336
  502. package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.ts +0 -43
  503. package/src/mem_pools/tx_pool/README.md +0 -270
  504. package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +0 -746
  505. package/src/mem_pools/tx_pool/eviction/eviction_manager.ts +0 -132
  506. package/src/mem_pools/tx_pool/eviction/eviction_strategy.ts +0 -208
  507. package/src/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.ts +0 -162
  508. package/src/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.ts +0 -104
  509. package/src/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.ts +0 -93
  510. package/src/mem_pools/tx_pool/eviction/low_priority_eviction_rule.ts +0 -106
  511. package/src/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.ts +0 -75
  512. package/src/mem_pools/tx_pool/index.ts +0 -2
  513. package/src/mem_pools/tx_pool/priority.ts +0 -20
  514. package/src/mem_pools/tx_pool/tx_pool.ts +0 -141
  515. package/src/mem_pools/tx_pool/tx_pool_test_suite.ts +0 -319
  516. package/src/msg_validators/clock_tolerance.ts +0 -51
  517. package/src/msg_validators/proposal_validator/proposal_validator_test_suite.ts +0 -230
  518. package/src/services/reqresp/connection-sampler/batch_connection_sampler.ts +0 -161
  519. package/src/services/reqresp/protocols/block.ts +0 -37
  520. package/src/services/tx_collection/fast_tx_collection.ts +0 -354
  521. package/src/services/tx_collection/proposal_tx_collector.ts +0 -112
  522. package/src/services/tx_collection/slow_tx_collection.ts +0 -233
@@ -1,5 +1,7 @@
1
- import { SlotNumber } from '@aztec/foundation/branded-types';
1
+ import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
2
+ import { FifoSet } from '@aztec/foundation/fifo-set';
2
3
  import type { Logger } from '@aztec/foundation/log';
4
+ import type { DateProvider } from '@aztec/foundation/timer';
3
5
  import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
4
6
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
5
7
  import { computeFeePayerBalanceStorageSlot } from '@aztec/protocol-contracts/fee-juice';
@@ -8,20 +10,27 @@ import type { L2Block, L2BlockId, L2BlockSource } from '@aztec/stdlib/block';
8
10
  import type { WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
9
11
  import { DatabasePublicStateSource } from '@aztec/stdlib/trees';
10
12
  import { BlockHeader, Tx, TxHash, type TxValidator } from '@aztec/stdlib/tx';
13
+ import type { TelemetryClient } from '@aztec/telemetry-client';
11
14
 
12
15
  import { TxArchive } from './archive/index.js';
16
+ import { DeletedPool } from './deleted_pool.js';
13
17
  import {
14
18
  EvictionManager,
15
19
  FeePayerBalanceEvictionRule,
16
20
  FeePayerBalancePreAddRule,
21
+ InsufficientFeePerGasEvictionRule,
17
22
  InvalidTxsAfterMiningRule,
18
23
  InvalidTxsAfterReorgRule,
19
24
  LowPriorityEvictionRule,
20
25
  LowPriorityPreAddRule,
21
26
  NullifierConflictRule,
22
27
  type PoolOperations,
28
+ type PreAddContext,
23
29
  type PreAddPoolAccess,
30
+ TxPoolRejectionCode,
31
+ type TxPoolRejectionError,
24
32
  } from './eviction/index.js';
33
+ import { TxPoolV2Instrumentation } from './instrumentation.js';
25
34
  import {
26
35
  type AddTxsResult,
27
36
  DEFAULT_TX_POOL_V2_CONFIG,
@@ -32,12 +41,29 @@ import {
32
41
  import { type TxMetaData, type TxState, buildTxMetaData, checkNullifierConflict } from './tx_metadata.js';
33
42
  import { TxPoolIndices } from './tx_pool_indices.js';
34
43
 
44
+ /**
45
+ * Maximum number of finalized txs to archive or hard-delete per serial-queue item. Bounds both
46
+ * peak memory (~23k txs/epoch at 10 TPS would otherwise OOM the node) and, more importantly, how
47
+ * long finalization occupies the pool's serial queue per item: gossip tx validation waits on that
48
+ * queue, so each item must stay well under the gossipsub mcache eviction window.
49
+ */
50
+ export const FINALIZE_BLOCK_CHUNK_SIZE = 16;
51
+
35
52
  /**
36
53
  * Callbacks for the implementation to notify the outer class about events and metrics.
37
54
  */
55
+ /** A tx that has just transitioned to mined, with the time it spent in the pool. */
56
+ export interface MinedTxInfo {
57
+ txHash: string;
58
+ /** Wall-clock ms from receipt into the pool to being marked mined. Undefined when the
59
+ * receive time is unknown (e.g. a tx hydrated from the DB on restart, receivedAt === 0). */
60
+ minedDelayMs?: number;
61
+ }
62
+
38
63
  export interface TxPoolV2Callbacks {
39
64
  onTxsAdded: (txs: Tx[], opts: { source?: string }) => void;
40
65
  onTxsRemoved: (txHashes: string[] | bigint[]) => void;
66
+ onTxsMined: (minedTxs: MinedTxInfo[]) => void;
41
67
  }
42
68
 
43
69
  /**
@@ -48,12 +74,16 @@ export interface TxPoolV2Callbacks {
48
74
  export class TxPoolV2Impl {
49
75
  // === Persistence ===
50
76
  #store: AztecAsyncKVStore;
77
+ /** Tx data without its proof. Proofs live in #proofsDB so a tx can be read without loading its proof. */
51
78
  #txsDB: AztecAsyncMap<string, Buffer>;
79
+ /** Tx proofs, keyed by tx hash. Written and deleted in lockstep with #txsDB. */
80
+ #proofsDB: AztecAsyncMap<string, Buffer>;
52
81
 
53
82
  // === Dependencies ===
54
83
  #l2BlockSource: L2BlockSource;
55
84
  #worldStateSynchronizer: WorldStateSynchronizer;
56
- #pendingTxValidator: TxValidator<Tx>;
85
+ #createTxValidator: TxPoolV2Dependencies['createTxValidator'];
86
+ #checkAllowedSetupCalls: TxPoolV2Dependencies['checkAllowedSetupCalls'];
57
87
 
58
88
  // === In-Memory Indices ===
59
89
  #indices: TxPoolIndices = new TxPoolIndices();
@@ -61,7 +91,11 @@ export class TxPoolV2Impl {
61
91
  // === Config & Services ===
62
92
  #config: TxPoolV2Config;
63
93
  #archive: TxArchive;
94
+ #deletedPool: DeletedPool;
64
95
  #evictionManager: EvictionManager;
96
+ #dateProvider: DateProvider;
97
+ #instrumentation: TxPoolV2Instrumentation;
98
+ #evictedTxHashes: FifoSet<string>;
65
99
  #log: Logger;
66
100
  #callbacks: TxPoolV2Callbacks;
67
101
 
@@ -70,18 +104,26 @@ export class TxPoolV2Impl {
70
104
  archiveStore: AztecAsyncKVStore,
71
105
  deps: TxPoolV2Dependencies,
72
106
  callbacks: TxPoolV2Callbacks,
107
+ telemetry: TelemetryClient,
73
108
  config: Partial<TxPoolV2Config> = {},
109
+ dateProvider: DateProvider,
74
110
  log: Logger,
75
111
  ) {
76
112
  this.#store = store;
77
113
  this.#txsDB = store.openMap('txs');
114
+ this.#proofsDB = store.openMap('tx_proofs');
78
115
 
79
116
  this.#l2BlockSource = deps.l2BlockSource;
80
117
  this.#worldStateSynchronizer = deps.worldStateSynchronizer;
81
- this.#pendingTxValidator = deps.pendingTxValidator;
118
+ this.#createTxValidator = deps.createTxValidator;
119
+ this.#checkAllowedSetupCalls = deps.checkAllowedSetupCalls;
82
120
 
83
121
  this.#config = { ...DEFAULT_TX_POOL_V2_CONFIG, ...config };
122
+ this.#evictedTxHashes = FifoSet.withLimit<string>(this.#config.evictedTxCacheSize);
84
123
  this.#archive = new TxArchive(archiveStore, this.#config.archivedTxLimit, log);
124
+ this.#deletedPool = new DeletedPool(store, txHashStr => this.#deleteTxData(txHashStr), log);
125
+ this.#dateProvider = dateProvider;
126
+ this.#instrumentation = new TxPoolV2Instrumentation(telemetry, () => this.#indices.getTotalMetadataBytes());
85
127
  this.#log = log;
86
128
  this.#callbacks = callbacks;
87
129
 
@@ -97,6 +139,7 @@ export class TxPoolV2Impl {
97
139
 
98
140
  // Post-event eviction rules (run after events to check ALL pending txs)
99
141
  this.#evictionManager.registerRule(new InvalidTxsAfterMiningRule());
142
+ this.#evictionManager.registerRule(new InsufficientFeePerGasEvictionRule(deps.blockMinFeesProvider));
100
143
  this.#evictionManager.registerRule(new InvalidTxsAfterReorgRule(deps.worldStateSynchronizer));
101
144
  this.#evictionManager.registerRule(new FeePayerBalanceEvictionRule(deps.worldStateSynchronizer));
102
145
  // LowPriorityEvictionRule handles cases where txs become pending via prepareForSlot (unprotect)
@@ -116,55 +159,118 @@ export class TxPoolV2Impl {
116
159
  * by running pre-add rules to resolve nullifier conflicts, balance checks, and pool size limits.
117
160
  */
118
161
  async hydrateFromDatabase(): Promise<void> {
119
- // Step 1: Load all transactions from DB
120
- const { loaded, errors: deserializationErrors } = await this.#loadAllTxsFromDb();
121
-
122
- // Step 2: Check mined status for each tx
123
- await this.#markMinedStatusBatch(loaded.map(l => l.meta));
124
-
125
- // Step 3: Partition by mined status
126
- const mined: TxMetaData[] = [];
127
- const nonMined: { tx: Tx; meta: TxMetaData }[] = [];
128
- for (const entry of loaded) {
129
- if (entry.meta.minedL2BlockId !== undefined) {
130
- mined.push(entry.meta);
162
+ // Step 0: Hydrate deleted pool state
163
+ await this.#deletedPool.hydrateFromDatabase();
164
+
165
+ // Step 1: Stream txs from DB, building metadata and mined status one at a time.
166
+ const minedMetas: TxMetaData[] = [];
167
+ const pendingMetas: TxMetaData[] = [];
168
+ const deserializationErrors: string[] = [];
169
+
170
+ for await (const [txHashStr, buffer] of this.#txsDB.entriesAsync()) {
171
+ // Skip soft-deleted transactions - they stay in DB but not in indices
172
+ if (this.#deletedPool.isSoftDeleted(txHashStr)) {
173
+ continue;
174
+ }
175
+
176
+ let meta: TxMetaData;
177
+ let txEffect: Awaited<ReturnType<L2BlockSource['getTxEffect']>>;
178
+ try {
179
+ const tx = Tx.fromBuffer(buffer);
180
+ // Resolve allowed-setup-calls and the tx effect concurrently
181
+ // getTxEffect failures are non-fatal (we just treat the tx as not-yet-mined), so
182
+ // its rejection is swallowed before the Promise.all so it can't fail-fast the batch.
183
+ const txEffectPromise = this.#l2BlockSource.getTxEffect(tx.getTxHash()).catch(err => {
184
+ this.#log.warn(`Failed to check mined status for tx ${txHashStr}`, { err });
185
+ return undefined;
186
+ });
187
+ const [allowedSetupCalls, fetchedTxEffect] = await Promise.all([
188
+ this.#checkAllowedSetupCalls(tx),
189
+ txEffectPromise,
190
+ ]);
191
+ meta = await buildTxMetaData(tx, allowedSetupCalls);
192
+ txEffect = fetchedTxEffect;
193
+ } catch (err) {
194
+ this.#log.warn(`Failed to deserialize tx ${txHashStr}, deleting`, { err });
195
+ deserializationErrors.push(txHashStr);
196
+ continue;
197
+ }
198
+
199
+ if (txEffect) {
200
+ meta.minedL2BlockId = {
201
+ number: txEffect.l2BlockNumber,
202
+ hash: txEffect.l2BlockHash.toString(),
203
+ };
204
+ }
205
+
206
+ if (meta.minedL2BlockId !== undefined) {
207
+ minedMetas.push(meta);
131
208
  } else {
132
- nonMined.push(entry);
209
+ pendingMetas.push(meta);
133
210
  }
134
211
  }
135
212
 
136
- // Step 4: Validate non-mined transactions
137
- const { valid, invalid } = await this.#validateTxBatch(nonMined, 'on startup');
213
+ // Step 2: Validate non-mined transactions
214
+ const { valid, invalid } = await this.#revalidateMetadata(pendingMetas, 'on startup');
138
215
 
139
- // Step 5: Populate mined indices (these don't need conflict resolution)
140
- for (const meta of mined) {
216
+ // Step 3: Populate mined indices (these don't need conflict resolution)
217
+ for (const meta of minedMetas) {
141
218
  this.#indices.addMined(meta);
142
219
  }
143
220
 
144
- // Step 6: Rebuild pending pool by running pre-add rules for each tx
221
+ // Step 4: Rebuild pending pool by running pre-add rules for each tx
145
222
  // This resolves nullifier conflicts, fee payer balance issues, and pool size limits
146
223
  const { rejected } = await this.#rebuildPendingPool(valid);
147
224
 
148
- // Step 7: Delete invalid and rejected txs from DB only (indices were never populated for these)
225
+ // Step 5: Delete invalid and rejected txs from DB only (indices were never populated for these)
149
226
  const toDelete = [...deserializationErrors, ...invalid, ...rejected];
150
227
  if (toDelete.length === 0) {
151
228
  return;
152
229
  }
153
230
  await this.#store.transactionAsync(async () => {
154
231
  for (const txHashStr of toDelete) {
155
- await this.#txsDB.delete(txHashStr);
232
+ await this.#deleteTxData(txHashStr);
156
233
  }
157
234
  });
158
- this.#log.info(`Deleted ${toDelete.length} invalid/rejected transactions on startup`);
235
+ this.#log.info(`Deleted ${toDelete.length} invalid/rejected transactions on startup`, { txHashes: toDelete });
159
236
  }
160
237
 
161
- async addPendingTxs(txs: Tx[], opts: { source?: string }): Promise<AddTxsResult> {
238
+ async addPendingTxs(txs: Tx[], opts: { source?: string; feeComparisonOnly?: boolean }): Promise<AddTxsResult> {
162
239
  const accepted: TxHash[] = [];
163
240
  const ignored: TxHash[] = [];
164
241
  const rejected: TxHash[] = [];
242
+ const errors = new Map<string, TxPoolRejectionError>();
165
243
  const acceptedPending = new Set<string>();
166
244
 
245
+ // Phase 1: Pre-compute all throwable I/O outside the transaction.
246
+ // If any pre-computation throws, the entire call fails before mutations happen.
247
+ const precomputed = new Map<string, { meta: TxMetaData; minedBlockId: L2BlockId | undefined; isValid: boolean }>();
248
+
249
+ const validator = await this.#createTxValidator();
250
+
251
+ for (const tx of txs) {
252
+ const txHash = tx.getTxHash();
253
+ const txHashStr = txHash.toString();
254
+
255
+ const meta = await buildTxMetaData(tx);
256
+ const minedBlockId = await this.#getMinedBlockId(txHash);
257
+
258
+ // Validate non-mined txs (mined and pre-protected txs bypass validation inside the transaction)
259
+ let isValid = true;
260
+ if (!minedBlockId) {
261
+ isValid = await this.#validateMeta(meta, validator);
262
+ }
263
+
264
+ precomputed.set(txHashStr, { meta, minedBlockId, isValid });
265
+ }
266
+
267
+ // Phase 2: Apply mutations inside the transaction using only pre-computed results,
268
+ // in-memory reads, and buffered DB writes. Nothing here can throw an unhandled exception.
167
269
  const poolAccess = this.#createPreAddPoolAccess();
270
+ const preAddContext: PreAddContext | undefined =
271
+ opts.feeComparisonOnly !== undefined
272
+ ? { feeComparisonOnly: opts.feeComparisonOnly, priceBumpPercentage: this.#config.priceBumpPercentage }
273
+ : undefined;
168
274
 
169
275
  await this.#store.transactionAsync(async () => {
170
276
  for (const tx of txs) {
@@ -177,30 +283,46 @@ export class TxPoolV2Impl {
177
283
  continue;
178
284
  }
179
285
 
180
- // Check mined status first (applies to all paths)
181
- const minedBlockId = await this.#getMinedBlockId(txHash);
286
+ const { meta, minedBlockId, isValid } = precomputed.get(txHashStr)!;
182
287
  const preProtectedSlot = this.#indices.getProtectionSlot(txHashStr);
183
288
 
184
289
  if (minedBlockId) {
185
290
  // Already mined - add directly (protection already set if pre-protected)
186
- await this.#addTx(tx, { mined: minedBlockId }, opts);
291
+ await this.#addTx(tx, { mined: minedBlockId }, opts, meta);
187
292
  accepted.push(txHash);
188
293
  } else if (preProtectedSlot !== undefined) {
189
294
  // Pre-protected and not mined - add as protected (bypass validation)
190
- await this.#addTx(tx, { protected: preProtectedSlot }, opts);
295
+ await this.#addTx(tx, { protected: preProtectedSlot }, opts, meta);
191
296
  accepted.push(txHash);
297
+ } else if (!isValid) {
298
+ // Failed pre-computed validation
299
+ rejected.push(txHash);
192
300
  } else {
193
- // Regular pending tx - validate and run pre-add rules
194
- const result = await this.#tryAddRegularPendingTx(tx, opts, poolAccess, acceptedPending, ignored);
301
+ // Regular pending tx - run pre-add rules using pre-computed metadata
302
+ const result = await this.#tryAddRegularPendingTx(
303
+ tx,
304
+ meta,
305
+ opts,
306
+ poolAccess,
307
+ acceptedPending,
308
+ ignored,
309
+ errors,
310
+ preAddContext,
311
+ );
195
312
  if (result.status === 'accepted') {
196
313
  acceptedPending.add(txHashStr);
197
- } else if (result.status === 'rejected') {
198
- rejected.push(txHash);
199
314
  } else {
200
315
  ignored.push(txHash);
201
316
  }
202
317
  }
203
318
  }
319
+
320
+ // Run post-add eviction rules for pending txs (inside transaction for atomicity)
321
+ if (acceptedPending.size > 0) {
322
+ const feePayers = Array.from(acceptedPending).map(txHash => this.#indices.getMetadata(txHash)!.feePayer);
323
+ const uniqueFeePayers = new Set<string>(feePayers);
324
+ await this.#evictionManager.evictAfterNewTxs(Array.from(acceptedPending), [...uniqueFeePayers]);
325
+ }
204
326
  });
205
327
 
206
328
  // Build final accepted list for pending txs (excludes intra-batch evictions)
@@ -208,96 +330,124 @@ export class TxPoolV2Impl {
208
330
  accepted.push(TxHash.fromString(txHashStr));
209
331
  }
210
332
 
211
- // Run post-add eviction rules for pending txs
212
- if (acceptedPending.size > 0) {
213
- const feePayers = Array.from(acceptedPending).map(txHash => this.#indices.getMetadata(txHash)!.feePayer);
214
- const uniqueFeePayers = new Set<string>(feePayers);
215
- await this.#evictionManager.evictAfterNewTxs(Array.from(acceptedPending), [...uniqueFeePayers]);
333
+ // Record metrics
334
+ if (ignored.length > 0) {
335
+ this.#instrumentation.recordIgnored(ignored.length);
336
+ }
337
+ if (rejected.length > 0) {
338
+ this.#instrumentation.recordRejected(rejected.length);
216
339
  }
217
340
 
218
- return { accepted, ignored, rejected };
341
+ return { accepted, ignored, rejected, ...(errors.size > 0 ? { errors } : {}) };
219
342
  }
220
343
 
221
- /** Validates and adds a regular pending tx. Returns status. */
344
+ /** Adds a validated pending tx, running pre-add rules and evicting conflicts. */
222
345
  async #tryAddRegularPendingTx(
223
346
  tx: Tx,
347
+ precomputedMeta: TxMetaData,
224
348
  opts: { source?: string },
225
349
  poolAccess: PreAddPoolAccess,
226
350
  acceptedPending: Set<string>,
227
351
  ignored: TxHash[],
228
- ): Promise<{ status: 'accepted' | 'ignored' | 'rejected' }> {
229
- const txHash = tx.getTxHash();
230
- const txHashStr = txHash.toString();
352
+ errors: Map<string, TxPoolRejectionError>,
353
+ preAddContext?: PreAddContext,
354
+ ): Promise<{ status: 'accepted' | 'ignored' }> {
355
+ const txHashStr = tx.getTxHash().toString();
231
356
 
232
- // Validate transaction
233
- if (!(await this.#validateTx(tx))) {
234
- return { status: 'rejected' };
235
- }
236
-
237
- // Build metadata and run pre-add rules
238
- const meta = await buildTxMetaData(tx);
239
- const preAddResult = await this.#evictionManager.runPreAddRules(meta, poolAccess);
357
+ // Run pre-add rules
358
+ const preAddResult = await this.#evictionManager.runPreAddRules(precomputedMeta, poolAccess, preAddContext);
240
359
 
241
360
  if (preAddResult.shouldIgnore) {
242
- this.#log.debug(`Ignoring tx ${txHashStr}: ${preAddResult.reason}`);
361
+ this.#log.debug(`Ignoring tx ${txHashStr}: ${preAddResult.reason?.message ?? 'unknown reason'}`);
362
+ if (preAddResult.reason && preAddResult.reason.code !== TxPoolRejectionCode.INTERNAL_ERROR) {
363
+ errors.set(txHashStr, preAddResult.reason);
364
+ }
243
365
  return { status: 'ignored' };
244
366
  }
245
367
 
246
- // Evict conflicts
247
- for (const evictHashStr of preAddResult.txHashesToEvict) {
248
- await this.#deleteTx(evictHashStr);
249
- this.#log.debug(`Evicted tx ${evictHashStr} due to higher-fee tx ${txHashStr}`);
250
- if (acceptedPending.has(evictHashStr)) {
251
- // Evicted tx was from this batch - mark as ignored in result
252
- acceptedPending.delete(evictHashStr);
253
- ignored.push(TxHash.fromString(evictHashStr));
368
+ // Evict conflicts, grouped by rule name for metrics
369
+ if (preAddResult.evictions && preAddResult.evictions.length > 0) {
370
+ const byReason = new Map<string, string[]>();
371
+ for (const { txHash: evictHash, reason } of preAddResult.evictions) {
372
+ const group = byReason.get(reason);
373
+ if (group) {
374
+ group.push(evictHash);
375
+ } else {
376
+ byReason.set(reason, [evictHash]);
377
+ }
254
378
  }
379
+ for (const [reason, hashes] of byReason) {
380
+ await this.#evictTxs(hashes, reason);
381
+ }
382
+ for (const evictHashStr of preAddResult.txHashesToEvict) {
383
+ this.#log.debug(`Evicted tx ${evictHashStr} due to higher-fee tx ${txHashStr}`, {
384
+ evictedTxHash: evictHashStr,
385
+ replacementTxHash: txHashStr,
386
+ });
387
+ if (acceptedPending.has(evictHashStr)) {
388
+ // Evicted tx was from this batch - mark as ignored in result
389
+ acceptedPending.delete(evictHashStr);
390
+ ignored.push(TxHash.fromString(evictHashStr));
391
+ }
392
+ }
393
+ }
394
+
395
+ // Randomly drop the transaction for testing purposes (report as accepted so it propagates)
396
+ if (this.#config.dropTransactionsProbability > 0 && Math.random() < this.#config.dropTransactionsProbability) {
397
+ this.#log.debug(`Dropping tx ${txHashStr} (simulated drop for testing)`);
398
+ return { status: 'accepted' };
255
399
  }
256
400
 
257
401
  // Add the transaction
258
- await this.#addTx(tx, 'pending', opts);
402
+ await this.#addTx(tx, 'pending', opts, precomputedMeta);
259
403
  return { status: 'accepted' };
260
404
  }
261
405
 
262
- async canAddPendingTx(tx: Tx): Promise<'accepted' | 'ignored' | 'rejected'> {
406
+ async canAddPendingTx(tx: Tx): Promise<'accepted' | 'ignored'> {
263
407
  const txHashStr = tx.getTxHash().toString();
264
408
 
265
409
  // Check if already in pool
266
410
  if (this.#indices.has(txHashStr)) {
411
+ this.#log.verbose(`canAddPendingTx: tx ${txHashStr} already in pool`);
267
412
  return 'ignored';
268
413
  }
269
414
 
270
- // Validate transaction (no logging for dry-run check)
271
- const validationResult = await this.#pendingTxValidator.validateTx(tx);
272
- if (validationResult.result !== 'valid') {
273
- return 'rejected';
274
- }
275
-
276
- // Build metadata and use pre-add rules
415
+ // Build metadata and check pre-add rules
277
416
  const meta = await buildTxMetaData(tx);
278
417
  const poolAccess = this.#createPreAddPoolAccess();
279
418
  const preAddResult = await this.#evictionManager.runPreAddRules(meta, poolAccess);
280
419
 
281
- return preAddResult.shouldIgnore ? 'ignored' : 'accepted';
420
+ if (preAddResult.shouldIgnore) {
421
+ this.#log.verbose(`canAddPendingTx: tx ${txHashStr} ignored by pre-add rule`, {
422
+ reason: preAddResult.reason?.message ?? 'no reason provided',
423
+ });
424
+ return 'ignored';
425
+ }
426
+ return 'accepted';
282
427
  }
283
428
 
284
429
  async addProtectedTxs(txs: Tx[], block: BlockHeader, opts: { source?: string }): Promise<void> {
285
430
  const slotNumber = block.globalVariables.slotNumber;
286
431
 
432
+ // Precompute setup-call allow-list flags outside the store transaction
433
+ const allowedFlags = await Promise.all(txs.map(tx => this.#checkAllowedSetupCalls(tx)));
434
+
287
435
  await this.#store.transactionAsync(async () => {
288
- for (const tx of txs) {
436
+ for (let i = 0; i < txs.length; i++) {
437
+ const tx = txs[i];
289
438
  const txHash = tx.getTxHash();
290
439
  const txHashStr = txHash.toString();
291
440
  const isNew = !this.#indices.has(txHashStr);
292
441
  const minedBlockId = await this.#getMinedBlockId(txHash);
293
442
 
294
443
  if (isNew) {
444
+ const meta = await buildTxMetaData(tx, allowedFlags[i]);
295
445
  // New tx - add as mined or protected (callback emitted by #addTx)
296
446
  if (minedBlockId) {
297
- await this.#addTx(tx, { mined: minedBlockId }, opts);
447
+ await this.#addTx(tx, { mined: minedBlockId }, opts, meta);
298
448
  this.#indices.setProtection(txHashStr, slotNumber);
299
449
  } else {
300
- await this.#addTx(tx, { protected: slotNumber }, opts);
450
+ await this.#addTx(tx, { protected: slotNumber }, opts, meta);
301
451
  }
302
452
  } else {
303
453
  // Existing tx - update protection and mined status
@@ -311,23 +461,59 @@ export class TxPoolV2Impl {
311
461
  });
312
462
  }
313
463
 
314
- protectTxs(txHashes: TxHash[], block: BlockHeader): TxHash[] {
464
+ async protectTxs(txHashes: TxHash[], block: BlockHeader): Promise<TxHash[]> {
315
465
  const slotNumber = block.globalVariables.slotNumber;
316
466
  const missing: TxHash[] = [];
467
+ let softDeletedHits = 0;
468
+ let missingPreviouslyEvicted = 0;
317
469
 
318
- for (const txHash of txHashes) {
319
- const txHashStr = txHash.toString();
470
+ await this.#store.transactionAsync(async () => {
471
+ for (const txHash of txHashes) {
472
+ const txHashStr = txHash.toString();
320
473
 
321
- if (this.#indices.has(txHashStr)) {
322
- // Update protection for existing tx
323
- this.#indices.updateProtection(txHashStr, slotNumber);
324
- } else {
325
- // Pre-record protection for tx we don't have yet
326
- this.#indices.setProtection(txHashStr, slotNumber);
327
- missing.push(txHash);
474
+ if (this.#indices.has(txHashStr)) {
475
+ // Update protection for existing tx
476
+ this.#indices.updateProtection(txHashStr, slotNumber);
477
+ } else if (this.#deletedPool.isSoftDeleted(txHashStr)) {
478
+ // Resurrect soft-deleted tx as protected. Load the proof too: #addTx rewrites both entries, so re-adding
479
+ // a proofless tx would wipe the stored proof.
480
+ const buffer = await this.#txsDB.getAsync(txHashStr);
481
+ if (buffer) {
482
+ const tx = await this.#loadTxWithProof(txHashStr, buffer);
483
+ await this.#addTx(tx, { protected: slotNumber });
484
+ softDeletedHits++;
485
+ } else {
486
+ // Data missing despite soft-delete flag — treat as truly missing
487
+ this.#indices.setProtection(txHashStr, slotNumber);
488
+ missing.push(txHash);
489
+ }
490
+ } else {
491
+ // Truly missing — pre-record protection for tx we don't have yet
492
+ this.#indices.setProtection(txHashStr, slotNumber);
493
+ missing.push(txHash);
494
+ if (this.#evictedTxHashes.has(txHashStr)) {
495
+ missingPreviouslyEvicted++;
496
+ }
497
+ }
328
498
  }
499
+ });
500
+
501
+ // Record metrics
502
+ if (softDeletedHits > 0) {
503
+ this.#instrumentation.recordSoftDeletedHits(softDeletedHits);
504
+ }
505
+ if (missing.length > 0) {
506
+ this.#log.debug(`protectTxs missing tx hashes: ${missing.map(h => h.toString()).join(', ')}`);
507
+ this.#instrumentation.recordMissingOnProtect(missing.length);
508
+ }
509
+ if (missingPreviouslyEvicted > 0) {
510
+ this.#instrumentation.recordMissingPreviouslyEvicted(missingPreviouslyEvicted);
329
511
  }
330
512
 
513
+ this.#log.info(
514
+ `Protected ${txHashes.length} txs, missing: ${missing.length}, soft-deleted hits: ${softDeletedHits}`,
515
+ );
516
+
331
517
  return missing;
332
518
  }
333
519
 
@@ -347,6 +533,7 @@ export class TxPoolV2Impl {
347
533
  // Add new mined tx (callback emitted by #addTx)
348
534
  await this.#addTx(tx, { mined: blockId }, opts);
349
535
  }
536
+ await this.#deletedPool.clearIfMinedHigher(txHashStr, blockId.number);
350
537
  }
351
538
  });
352
539
  }
@@ -370,42 +557,97 @@ export class TxPoolV2Impl {
370
557
  }
371
558
  }
372
559
 
373
- // Step 4: Mark txs as mined (only those we have in the pool)
374
- for (const meta of found) {
375
- this.#indices.markAsMined(meta, blockId);
376
- }
560
+ await this.#store.transactionAsync(async () => {
561
+ // Step 4: Mark txs as mined (only those we have in the pool)
562
+ for (const meta of found) {
563
+ this.#indices.markAsMined(meta, blockId);
564
+ await this.#deletedPool.clearIfMinedHigher(meta.txHash, blockId.number);
565
+ }
377
566
 
378
- // Step 5: Run eviction rules (remove pending txs with conflicting nullifiers/expired timestamps)
379
- await this.#evictionManager.evictAfterNewBlock(block.header, nullifiers, feePayers);
567
+ // Step 5: Run post-event eviction rules (inside transaction for atomicity)
568
+ await this.#evictionManager.evictAfterNewBlock(block.header, nullifiers, feePayers);
569
+ });
570
+
571
+ if (found.length > 0) {
572
+ // receivedAt is 0 for txs hydrated from the DB on restart (true receive time lost) — leave
573
+ // their delay undefined so the metric isn't polluted by epoch-sized values.
574
+ const now = this.#dateProvider.now();
575
+ this.#callbacks.onTxsMined(
576
+ found.map(m => ({
577
+ txHash: m.txHash,
578
+ minedDelayMs: m.receivedAt > 0 ? now - m.receivedAt : undefined,
579
+ })),
580
+ );
581
+ }
380
582
 
381
583
  this.#log.info(`Marked ${found.length} txs as mined in block ${blockId.number}`);
382
584
  }
383
585
 
384
586
  async prepareForSlot(slotNumber: SlotNumber): Promise<void> {
385
- // Step 1: Find expired protected txs
386
- const expiredProtected = this.#indices.findExpiredProtectedTxs(slotNumber);
587
+ await this.#store.transactionAsync(async () => {
588
+ // Step 0: Clean up slot-deleted txs from previous slots
589
+ await this.#deletedPool.cleanupSlotDeleted(slotNumber);
387
590
 
388
- // Step 2: Clear protection for all expired entries (including those without metadata)
389
- this.#indices.clearProtection(expiredProtected);
591
+ // Step 1: Find expired protected txs
592
+ const expiredProtected = this.#indices.findExpiredProtectedTxs(slotNumber);
390
593
 
391
- // Step 3: Filter to only txs that have metadata and are not mined
392
- const txsToRestore = this.#indices.filterRestorable(expiredProtected);
393
- if (txsToRestore.length === 0) {
394
- return;
395
- }
594
+ // Step 2: Clear protection for all expired entries (including those without metadata)
595
+ this.#indices.clearProtection(expiredProtected);
596
+
597
+ // Step 3: Filter to only txs that have metadata and are not mined
598
+ const txsToRestore = this.#indices.filterRestorable(expiredProtected);
599
+ if (txsToRestore.length === 0) {
600
+ this.#log.debug(`Preparing for slot ${slotNumber}, no txs to unprotect`);
601
+ return;
602
+ }
603
+
604
+ this.#log.info(`Preparing for slot ${slotNumber}: unprotecting ${txsToRestore.length} txs`);
605
+ await this.#restoreUnprotectedToPending(txsToRestore, 'during prepareForSlot');
606
+ });
607
+ }
608
+
609
+ async unprotectTxs(txHashes: TxHash[], slotNumber: SlotNumber): Promise<void> {
610
+ const hashStrs = txHashes.map(h => h.toString());
611
+
612
+ await this.#store.transactionAsync(async () => {
613
+ // Only release entries still recorded at this exact slot. A tx that another, still-live proposal
614
+ // raised to a higher slot via updateProtection must keep that protection. Mined txs have no
615
+ // protection entry and so are never matched here.
616
+ const matching = this.#indices.findProtectedTxsAtSlot(hashStrs, slotNumber);
617
+ if (matching.length === 0) {
618
+ this.#log.debug(`Unprotecting txs for slot ${slotNumber}: no matching protections to release`);
619
+ return;
620
+ }
396
621
 
397
- this.#log.info(`Preparing for slot ${slotNumber}: unprotecting ${txsToRestore.length} txs`);
622
+ this.#indices.clearProtection(matching);
398
623
 
399
- // Step 4: Validate for pending pool
400
- const { valid, invalid } = await this.#loadAndValidateTxs(txsToRestore, 'during prepareForSlot');
624
+ const txsToRestore = this.#indices.filterRestorable(matching);
625
+ if (txsToRestore.length === 0) {
626
+ return;
627
+ }
401
628
 
402
- // Step 5: Resolve nullifier conflicts and add winners to pending indices
629
+ this.#log.info(`Unprotecting ${txsToRestore.length} txs from failed proposal at slot ${slotNumber}`);
630
+ await this.#restoreUnprotectedToPending(txsToRestore, 'during unprotectTxs');
631
+ });
632
+ }
633
+
634
+ /**
635
+ * Returns just-unprotected txs to the pending pool: revalidates them, resolves nullifier
636
+ * conflicts, deletes losers, then enforces the pool size limit. Must run inside a store
637
+ * transaction; callers clear the protection entries before invoking.
638
+ */
639
+ async #restoreUnprotectedToPending(txsToRestore: TxMetaData[], context: string): Promise<void> {
640
+ // Step 1: Validate for pending pool
641
+ const { valid, invalid } = await this.#revalidateMetadata(txsToRestore, context);
642
+
643
+ // Step 2: Resolve nullifier conflicts and add winners to pending indices
403
644
  const { added, toEvict } = this.#applyNullifierConflictResolution(valid);
404
645
 
405
- // Step 6: Delete invalid and evicted txs
406
- await this.#deleteTxsBatch([...invalid, ...toEvict]);
646
+ // Step 3: Delete invalid txs and evict conflict losers
647
+ await this.#deleteTxsBatch(invalid);
648
+ await this.#evictTxs(toEvict, 'NullifierConflict');
407
649
 
408
- // Step 7: Run eviction rules (enforce pool size limit)
650
+ // Step 4: Run eviction rules (enforce pool size limit)
409
651
  if (added.length > 0) {
410
652
  const feePayers = added.map(meta => meta.feePayer);
411
653
  const uniqueFeePayers = new Set<string>(feePayers);
@@ -416,7 +658,7 @@ export class TxPoolV2Impl {
416
658
  }
417
659
  }
418
660
 
419
- async handlePrunedBlocks(latestBlock: L2BlockId): Promise<void> {
661
+ async handlePrunedBlocks(latestBlock: L2BlockId, options?: { deleteAllTxs?: boolean }): Promise<void> {
420
662
  // Step 1: Find transactions mined after the prune point
421
663
  const txsToUnmine = this.#indices.findTxsMinedAfter(latestBlock.number);
422
664
  if (txsToUnmine.length === 0) {
@@ -426,102 +668,240 @@ export class TxPoolV2Impl {
426
668
 
427
669
  this.#log.info(`Handling prune to block ${latestBlock.number}: un-mining ${txsToUnmine.length} txs`);
428
670
 
429
- // Step 2: Unmine - clear mined status from metadata
430
- for (const meta of txsToUnmine) {
431
- this.#indices.markAsUnmined(meta);
432
- }
671
+ await this.#store.transactionAsync(async () => {
672
+ // Step 2: Mark ALL un-mined txs with their original mined block number
673
+ // This ensures they get soft-deleted if removed later, and only hard-deleted
674
+ // when their original mined block is finalized
675
+ await this.#deletedPool.markFromPrunedBlock(
676
+ txsToUnmine.map(m => ({
677
+ txHash: m.txHash,
678
+ minedAtBlock: BlockNumber(m.minedL2BlockId!.number),
679
+ })),
680
+ );
433
681
 
434
- // Step 3: Filter out protected txs (they'll be handled by prepareForSlot)
435
- const unprotectedTxs = this.#indices.filterUnprotected(txsToUnmine);
682
+ // Step 3: Unmine - clear mined status from metadata
683
+ for (const meta of txsToUnmine) {
684
+ this.#indices.markAsUnmined(meta);
685
+ }
436
686
 
437
- // Step 4: Validate for pending pool
438
- const { valid, invalid } = await this.#loadAndValidateTxs(unprotectedTxs, 'during handlePrunedBlocks');
687
+ // If deleteAllTxs is set (epoch prune), delete all un-mined txs and return early
688
+ if (options?.deleteAllTxs) {
689
+ const allTxHashes = txsToUnmine.map(m => m.txHash);
690
+ await this.#deleteTxsBatch(allTxHashes);
691
+ this.#log.info(
692
+ `Handled prune to block ${latestBlock.number} with deleteAllTxs: deleted ${allTxHashes.length} txs`,
693
+ );
694
+ return;
695
+ }
696
+
697
+ // Step 4: Filter out protected txs (they'll be handled by prepareForSlot)
698
+ const unprotectedTxs = this.#indices.filterUnprotected(txsToUnmine);
699
+
700
+ // Step 5: Validate for pending pool
701
+ const { valid, invalid } = await this.#revalidateMetadata(unprotectedTxs, 'during handlePrunedBlocks');
439
702
 
440
- // Step 5: Resolve nullifier conflicts and add winners to pending indices
441
- const { toEvict } = this.#applyNullifierConflictResolution(valid);
703
+ // Step 6: Resolve nullifier conflicts and add winners to pending indices
704
+ const { toEvict } = this.#applyNullifierConflictResolution(valid);
442
705
 
443
- // Step 6: Delete invalid and evicted txs
444
- await this.#deleteTxsBatch([...invalid, ...toEvict]);
706
+ // Step 7: Delete invalid txs and evict conflict losers
707
+ await this.#deleteTxsBatch(invalid);
708
+ await this.#evictTxs(toEvict, 'NullifierConflict');
445
709
 
446
- // Step 7: Run eviction rules for ALL pending txs (not just restored ones)
447
- // This handles cases like existing pending txs with invalid fee payer balances
448
- await this.#evictionManager.evictAfterChainPrune(latestBlock.number);
710
+ this.#log.info(
711
+ `Handled prune to block ${latestBlock.number}: ${valid.length} txs restored to pending, ${invalid.length} invalid, ${toEvict.length} evicted due to nullifier conflicts`,
712
+ { txHashesRestored: valid.map(m => m.txHash), txHashesInvalid: invalid, txHashesEvicted: toEvict },
713
+ );
714
+
715
+ // Step 8: Run eviction rules for ALL pending txs (not just restored ones)
716
+ // This handles cases like existing pending txs with invalid fee payer balances
717
+ await this.#evictionManager.evictAfterChainPrune(latestBlock.number);
718
+ });
449
719
  }
450
720
 
451
721
  async handleFailedExecution(txHashes: TxHash[]): Promise<void> {
452
- // Delete failed txs
453
- await this.#deleteTxsBatch(txHashes.map(h => h.toString()));
722
+ await this.#store.transactionAsync(async () => {
723
+ await this.#deleteTxsBatch(txHashes.map(h => h.toString()));
724
+ });
454
725
 
455
- this.#log.info(`Deleted ${txHashes.length} failed txs`);
726
+ this.#log.info(`Deleted ${txHashes.length} failed txs`, { txHashes: txHashes.map(h => h.toString()) });
456
727
  }
457
728
 
458
- async handleFinalizedBlock(block: BlockHeader): Promise<void> {
459
- const blockNumber = block.globalVariables.blockNumber;
729
+ /**
730
+ * Resolves what a finalized block event should process: the cutoff block and the mined txs at or
731
+ * before it. The wrapper feeds the result to archiveFinalizedTxs / finalizeTxs in chunks, each as
732
+ * its own serial-queue item, so gossip-driven pool operations can interleave with finalization
733
+ * instead of stalling behind an entire epoch's worth of tx processing.
734
+ */
735
+ async prepareFinalization(block: BlockHeader): Promise<{ cutoffBlock: BlockNumber; txHashes: string[] }> {
736
+ // Hold finalized txs for a configurable margin behind the finalized tip so a prover still
737
+ // proving an epoch with already-finalized blocks isn't starved of its txs. 0 deletes at the finalized tip.
738
+ const cutoffBlock = await this.#finalizationCutoffBlock(block);
739
+ return { cutoffBlock, txHashes: this.#indices.findTxsMinedAtOrBefore(cutoffBlock) };
740
+ }
460
741
 
461
- // Step 1: Find txs mined at or before finalized block
462
- const txsToFinalize = this.#indices.findTxsMinedAtOrBefore(blockNumber);
463
- if (txsToFinalize.length === 0) {
742
+ /**
743
+ * Copies the given finalized txs into the archive. The pool stores txs proof-stripped, which is
744
+ * exactly the archive format, so buffers are copied as-is without deserialization.
745
+ */
746
+ async archiveFinalizedTxs(txHashes: string[]): Promise<void> {
747
+ if (!this.#archive.isEnabled()) {
464
748
  return;
465
749
  }
466
-
467
- // Step 2: Collect txs for archiving (before deletion)
468
- const txsToArchive: Tx[] = [];
469
- if (this.#archive.isEnabled()) {
470
- for (const txHashStr of txsToFinalize) {
471
- const buffer = await this.#txsDB.getAsync(txHashStr);
472
- if (buffer) {
473
- txsToArchive.push(Tx.fromBuffer(buffer));
474
- }
750
+ const entries: { txHash: string; buffer: Buffer }[] = [];
751
+ for (const txHash of txHashes) {
752
+ const buffer = await this.#txsDB.getAsync(txHash);
753
+ if (buffer) {
754
+ entries.push({ txHash, buffer });
475
755
  }
476
756
  }
757
+ await this.#archive.archiveTxBuffers(entries);
758
+ }
477
759
 
478
- // Step 3: Delete from active pool
479
- await this.#deleteTxsBatch(txsToFinalize);
760
+ /**
761
+ * Deletes a batch of finalized mined txs from the active pool. Callers may invoke this in
762
+ * chunks: a crash between chunks leaves some finalized txs mined, and the next finalized-block
763
+ * event lists and deletes them again (the operation is idempotent), so per-chunk atomicity is
764
+ * sufficient. Since other pool operations may interleave between the finalization plan being
765
+ * computed and this call, each tx is re-checked to still be mined at or before the cutoff.
766
+ */
767
+ async deleteFinalizedTxs(txHashes: string[], cutoffBlock: BlockNumber): Promise<void> {
768
+ const stillFinalized = txHashes.filter(txHash => {
769
+ const minedBlock = this.#indices.getMetadata(txHash)?.minedL2BlockId?.number;
770
+ return minedBlock !== undefined && minedBlock <= cutoffBlock;
771
+ });
772
+ if (stillFinalized.length === 0) {
773
+ return;
774
+ }
775
+ await this.#store.transactionAsync(async () => {
776
+ await this.#deleteTxsBatch(stillFinalized);
777
+ });
778
+ }
480
779
 
481
- // Step 4: Archive
482
- if (txsToArchive.length > 0) {
483
- await this.#archive.archiveTxs(txsToArchive);
780
+ /** Finalizes soft-deleted txs up to the cutoff and logs the completed finalization. */
781
+ async completeFinalization(
782
+ txHashes: string[],
783
+ cutoffBlock: BlockNumber,
784
+ finalizedBlockNumber: BlockNumber,
785
+ ): Promise<void> {
786
+ await this.#store.transactionAsync(async () => {
787
+ await this.#deletedPool.finalizeBlock(cutoffBlock);
788
+ });
789
+
790
+ if (txHashes.length > 0) {
791
+ this.#log.info(`Finalized ${txHashes.length} mined txs from blocks up to ${cutoffBlock}`, {
792
+ txHashes,
793
+ finalizedBlockNumber,
794
+ cutoffBlock,
795
+ });
796
+ }
797
+ }
798
+
799
+ /**
800
+ * Resolves the block up to which finalized txs may be deleted, given `keepFinalizedTxsForSlots`.
801
+ * With a margin of 0 this is just the finalized block. Otherwise we keep txs whose block is within
802
+ * the margin (in slots) of the finalized tip: take the target slot `finalizedSlot - margin`, find the
803
+ * latest checkpoint at or before it with a single reverse range query (`limit: 1`), and use that
804
+ * checkpoint's last block as the cutoff. This rounds to a checkpoint boundary, so it can retain slightly
805
+ * more than the configured margin but never less, and never deletes past the finalized block.
806
+ */
807
+ async #finalizationCutoffBlock(block: BlockHeader): Promise<BlockNumber> {
808
+ const keepSlots = this.#config.keepFinalizedTxsForSlots;
809
+ if (keepSlots <= 0) {
810
+ return block.globalVariables.blockNumber;
484
811
  }
485
812
 
486
- this.#log.info(`Finalized ${txsToFinalize.length} txs from blocks up to ${blockNumber}`);
813
+ // The goal here really is to ensure we keep transactions long enough for them to be proven.
814
+ // So we could be smart and calculate the number of slots until the end of the proof submission window.
815
+ // Our approach here is a lot simpler however and we just keep transactions for the configured number
816
+ // of slots past L1 finalisation. This means we may keep transactions in the mined pool for longer
817
+ // than strictly necessary.
818
+ const targetSlot = block.getSlot() - keepSlots;
819
+ if (targetSlot < 0) {
820
+ // The margin reaches past genesis: keep everything.
821
+ return BlockNumber(0);
822
+ }
823
+
824
+ const [checkpoint] = await this.#l2BlockSource.getCheckpointsData({
825
+ fromSlot: SlotNumber(targetSlot),
826
+ limit: 1,
827
+ reverse: true,
828
+ });
829
+ // No checkpoint at or before the target slot (e.g. early chain): keep everything.
830
+ return checkpoint ? BlockNumber(checkpoint.startBlock + checkpoint.blockCount - 1) : BlockNumber(0);
487
831
  }
488
832
 
489
833
  // === Query Methods ===
490
834
 
491
- async getTxByHash(txHash: TxHash): Promise<Tx | undefined> {
492
- const buffer = await this.#txsDB.getAsync(txHash.toString());
493
- return buffer ? Tx.fromBuffer(buffer) : undefined;
835
+ async getTxByHash(txHash: TxHash, opts: { includeProof?: boolean } = {}): Promise<Tx | undefined> {
836
+ const txHashStr = txHash.toString();
837
+ const buffer = await this.#txsDB.getAsync(txHashStr);
838
+ if (!buffer) {
839
+ return undefined;
840
+ }
841
+ return opts.includeProof === false ? Tx.fromBuffer(buffer) : this.#loadTxWithProof(txHashStr, buffer);
494
842
  }
495
843
 
496
- async getTxsByHash(txHashes: TxHash[]): Promise<(Tx | undefined)[]> {
844
+ async getTxsByHash(txHashes: TxHash[], opts: { includeProof?: boolean } = {}): Promise<(Tx | undefined)[]> {
497
845
  const results: (Tx | undefined)[] = [];
498
846
  for (const h of txHashes) {
499
- const buffer = await this.#txsDB.getAsync(h.toString());
500
- results.push(buffer ? Tx.fromBuffer(buffer) : undefined);
847
+ results.push(await this.getTxByHash(h, opts));
501
848
  }
502
849
  return results;
503
850
  }
504
851
 
852
+ /** Deserializes a tx blob loaded from #txsDB together with its separately-stored proof. */
853
+ async #loadTxWithProof(txHashStr: string, buffer: Buffer): Promise<Tx> {
854
+ const proofBuffer = await this.#proofsDB.getAsync(txHashStr);
855
+ if (!proofBuffer) {
856
+ // #addTx always writes a proof entry (an empty proof serializes to a 4-byte sentinel), so a missing entry
857
+ // means the blob/proof invariant is broken. Return the proofless tx but flag it, since a consumer that needs
858
+ // the proof (eg serving peers) would otherwise fail far away from the root cause.
859
+ this.#log.warn(`Missing stored proof for tx ${txHashStr}`, { txHash: txHashStr });
860
+ return Tx.fromBuffer(buffer);
861
+ }
862
+ return Tx.fromBuffers(buffer, proofBuffer);
863
+ }
864
+
505
865
  hasTxs(txHashes: TxHash[]): boolean[] {
506
- return txHashes.map(h => this.#indices.has(h.toString()));
866
+ return txHashes.map(h => {
867
+ const hashStr = h.toString();
868
+ return this.#indices.has(hashStr) || this.#deletedPool.isSoftDeleted(hashStr);
869
+ });
507
870
  }
508
871
 
509
872
  getTxStatus(txHash: TxHash): TxState | undefined {
510
- const meta = this.#indices.getMetadata(txHash.toString());
511
- if (!meta) {
512
- return undefined;
873
+ const txHashStr = txHash.toString();
874
+ const meta = this.#indices.getMetadata(txHashStr);
875
+ if (meta) {
876
+ return this.#indices.getTxState(meta);
877
+ }
878
+ // Check if soft-deleted
879
+ if (this.#deletedPool.isSoftDeleted(txHashStr)) {
880
+ return 'deleted';
513
881
  }
514
- return this.#indices.getTxState(meta);
882
+ return undefined;
515
883
  }
516
884
 
517
885
  getPendingTxHashes(): TxHash[] {
518
886
  return [...this.#indices.iteratePendingByPriority('desc')].map(hash => TxHash.fromString(hash));
519
887
  }
520
888
 
889
+ getEligiblePendingTxHashes(): TxHash[] {
890
+ const maxReceivedAt = this.#dateProvider.now() - this.#config.minTxPoolAgeMs;
891
+ return [...this.#indices.iterateEligiblePendingByPriority('desc', maxReceivedAt)].map(hash =>
892
+ TxHash.fromString(hash),
893
+ );
894
+ }
895
+
521
896
  getPendingTxCount(): number {
522
897
  return this.#indices.getPendingTxCount();
523
898
  }
524
899
 
900
+ hasEligiblePendingTxs(minCount: number): boolean {
901
+ const maxReceivedAt = this.#dateProvider.now() - this.#config.minTxPoolAgeMs;
902
+ return this.#indices.hasEligiblePendingTxs(maxReceivedAt, minCount);
903
+ }
904
+
525
905
  getMinedTxHashes(): [TxHash, L2BlockId][] {
526
906
  return this.#indices.getMinedTxs().map(([hash, blockId]) => [TxHash.fromString(hash), blockId]);
527
907
  }
@@ -562,6 +942,12 @@ export class TxPoolV2Impl {
562
942
  this.#config.archivedTxLimit = config.archivedTxLimit;
563
943
  this.#archive.updateLimit(config.archivedTxLimit);
564
944
  }
945
+ if (config.minTxPoolAgeMs !== undefined) {
946
+ this.#config.minTxPoolAgeMs = config.minTxPoolAgeMs;
947
+ }
948
+ if (config.keepFinalizedTxsForSlots !== undefined) {
949
+ this.#config.keepFinalizedTxsForSlots = config.keepFinalizedTxsForSlots;
950
+ }
565
951
  // Update eviction rules with new config
566
952
  this.#evictionManager.updateConfig(config);
567
953
  }
@@ -579,8 +965,17 @@ export class TxPoolV2Impl {
579
965
 
580
966
  // === Metrics ===
581
967
 
582
- countTxs(): { pending: number; protected: number; mined: number } {
583
- return this.#indices.countTxs();
968
+ countTxs(): {
969
+ pending: number;
970
+ protected: number;
971
+ mined: number;
972
+ softDeleted: number;
973
+ totalMetadataBytes: number;
974
+ } {
975
+ return {
976
+ ...this.#indices.countTxs(),
977
+ softDeleted: this.#deletedPool.getSoftDeletedCount(),
978
+ };
584
979
  }
585
980
 
586
981
  // ============================================================================
@@ -595,11 +990,15 @@ export class TxPoolV2Impl {
595
990
  tx: Tx,
596
991
  state: 'pending' | { protected: SlotNumber } | { mined: L2BlockId },
597
992
  opts: { source?: string } = {},
993
+ precomputedMeta?: TxMetaData,
598
994
  ): Promise<TxMetaData> {
599
995
  const txHashStr = tx.getTxHash().toString();
600
- const meta = await buildTxMetaData(tx);
996
+ const meta = precomputedMeta ?? (await buildTxMetaData(tx));
997
+ meta.receivedAt = this.#dateProvider.now();
601
998
 
602
- await this.#txsDB.set(txHashStr, tx.toBuffer());
999
+ await this.#txsDB.set(txHashStr, tx.withoutProof().toBuffer());
1000
+ await this.#proofsDB.set(txHashStr, tx.chonkProof.toBuffer());
1001
+ await this.#deletedPool.clearSoftDeleted(txHashStr);
603
1002
  this.#callbacks.onTxsAdded([tx], opts);
604
1003
 
605
1004
  if (state === 'pending') {
@@ -612,9 +1011,11 @@ export class TxPoolV2Impl {
612
1011
  }
613
1012
 
614
1013
  const stateStr = typeof state === 'string' ? state : Object.keys(state)[0];
615
- this.#log.verbose(`Added ${stateStr} tx ${txHashStr}`, {
1014
+ this.#log.debug(`Added tx ${txHashStr} as ${stateStr}`, {
616
1015
  eventName: 'tx-added-to-pool',
1016
+ txHash: txHashStr,
617
1017
  state: stateStr,
1018
+ source: opts.source,
618
1019
  });
619
1020
 
620
1021
  return meta;
@@ -624,10 +1025,21 @@ export class TxPoolV2Impl {
624
1025
  * Deletes a transaction from both indices and DB.
625
1026
  * Emits onTxsRemoved callback immediately after DB delete.
626
1027
  */
1028
+ /**
1029
+ * Deletes a transaction from the pool.
1030
+ * Delegates to DeletedPool which decides soft vs hard delete based on whether
1031
+ * the tx is from a pruned block.
1032
+ */
627
1033
  async #deleteTx(txHashStr: string): Promise<void> {
628
1034
  this.#indices.remove(txHashStr);
629
- await this.#txsDB.delete(txHashStr);
630
1035
  this.#callbacks.onTxsRemoved([txHashStr]);
1036
+ await this.#deletedPool.deleteTx(txHashStr);
1037
+ }
1038
+
1039
+ /** Hard-deletes a tx's stored data (tx blob and proof) from the DB. */
1040
+ async #deleteTxData(txHashStr: string): Promise<void> {
1041
+ await this.#txsDB.delete(txHashStr);
1042
+ await this.#proofsDB.delete(txHashStr);
631
1043
  }
632
1044
 
633
1045
  /** Deletes a batch of transactions, emitting callbacks individually for each. */
@@ -637,68 +1049,53 @@ export class TxPoolV2Impl {
637
1049
  }
638
1050
  }
639
1051
 
1052
+ /** Evicts transactions: records eviction metric with reason, caches hashes, then deletes. */
1053
+ async #evictTxs(txHashes: string[], reason: string): Promise<void> {
1054
+ if (txHashes.length === 0) {
1055
+ return;
1056
+ }
1057
+ this.#instrumentation.recordEvictions(txHashes.length, reason);
1058
+ for (const txHashStr of txHashes) {
1059
+ this.#log.debug(`Evicting tx ${txHashStr}`, { txHash: txHashStr, reason });
1060
+ this.#evictedTxHashes.add(txHashStr);
1061
+ }
1062
+ await this.#deleteTxsBatch(txHashes);
1063
+ }
1064
+
640
1065
  // ============================================================================
641
1066
  // PRIVATE HELPERS - Validation & Conflict Resolution
642
1067
  // ============================================================================
643
1068
 
644
- /** Validates a single transaction, returning true if valid */
645
- async #validateTx(tx: Tx, context?: string): Promise<boolean> {
646
- const result = await this.#pendingTxValidator.validateTx(tx);
1069
+ /** Validates transaction metadata, returning true if valid */
1070
+ async #validateMeta(meta: TxMetaData, validator?: TxValidator<TxMetaData>, context?: string): Promise<boolean> {
1071
+ const txValidator = validator ?? (await this.#createTxValidator());
1072
+ const result = await txValidator.validateTx(meta);
647
1073
  if (result.result !== 'valid') {
648
1074
  const contextStr = context ? ` ${context}` : '';
649
- this.#log.info(`Tx ${tx.getTxHash()}${contextStr} failed validation: ${result.reason?.join(', ')}`);
1075
+ this.#log.info(`Tx ${meta.txHash}${contextStr} failed validation: ${result.reason?.join(', ')}`);
650
1076
  return false;
651
1077
  }
652
1078
  return true;
653
1079
  }
654
1080
 
655
- /** Loads transactions from DB, returning loaded txs and missing hashes */
656
- async #loadTxsFromDb(metas: TxMetaData[]): Promise<{ loaded: { tx: Tx; meta: TxMetaData }[]; missing: string[] }> {
657
- const loaded: { tx: Tx; meta: TxMetaData }[] = [];
658
- const missing: string[] = [];
659
-
660
- for (const meta of metas) {
661
- const buffer = await this.#txsDB.getAsync(meta.txHash);
662
- if (!buffer) {
663
- this.#log.warn(`Tx ${meta.txHash} not found in DB`);
664
- missing.push(meta.txHash);
665
- continue;
666
- }
667
- loaded.push({ tx: Tx.fromBuffer(buffer), meta });
668
- }
669
-
670
- return { loaded, missing };
671
- }
672
-
673
- /** Validates a batch of transactions, returning valid and invalid groups */
674
- async #validateTxBatch(
675
- txs: { tx: Tx; meta: TxMetaData }[],
1081
+ /** Validates metadata directly */
1082
+ async #revalidateMetadata(
1083
+ metas: TxMetaData[],
676
1084
  context?: string,
677
1085
  ): Promise<{ valid: TxMetaData[]; invalid: string[] }> {
678
1086
  const valid: TxMetaData[] = [];
679
1087
  const invalid: string[] = [];
680
-
681
- for (const { tx, meta } of txs) {
682
- if (await this.#validateTx(tx, context)) {
1088
+ const validator = await this.#createTxValidator();
1089
+ for (const meta of metas) {
1090
+ if (await this.#validateMeta(meta, validator, context)) {
683
1091
  valid.push(meta);
684
1092
  } else {
685
1093
  invalid.push(meta.txHash);
686
1094
  }
687
1095
  }
688
-
689
1096
  return { valid, invalid };
690
1097
  }
691
1098
 
692
- /** Loads transactions from DB and validates them */
693
- async #loadAndValidateTxs(
694
- metas: TxMetaData[],
695
- context?: string,
696
- ): Promise<{ valid: TxMetaData[]; invalid: string[] }> {
697
- const { loaded, missing } = await this.#loadTxsFromDb(metas);
698
- const { valid, invalid } = await this.#validateTxBatch(loaded, context);
699
- return { valid, invalid: [...missing, ...invalid] };
700
- }
701
-
702
1099
  /**
703
1100
  * Resolves nullifier conflicts between incoming txs and existing pending txs.
704
1101
  * Modifies the pending indices during iteration to maintain consistent state
@@ -759,45 +1156,6 @@ export class TxPoolV2Impl {
759
1156
  };
760
1157
  }
761
1158
 
762
- /** Loads all transactions from the database, returning loaded txs and deserialization errors */
763
- async #loadAllTxsFromDb(): Promise<{
764
- loaded: { tx: Tx; meta: TxMetaData }[];
765
- errors: string[];
766
- }> {
767
- const loaded: { tx: Tx; meta: TxMetaData }[] = [];
768
- const errors: string[] = [];
769
-
770
- for await (const [txHashStr, buffer] of this.#txsDB.entriesAsync()) {
771
- try {
772
- const tx = Tx.fromBuffer(buffer);
773
- const meta = await buildTxMetaData(tx);
774
- loaded.push({ tx, meta });
775
- } catch (err) {
776
- this.#log.warn(`Failed to deserialize tx ${txHashStr}, deleting`, { err });
777
- errors.push(txHashStr);
778
- }
779
- }
780
-
781
- return { loaded, errors };
782
- }
783
-
784
- /** Queries block source and marks mined status on transaction metadata */
785
- async #markMinedStatusBatch(metas: TxMetaData[]): Promise<void> {
786
- for (const meta of metas) {
787
- try {
788
- const txEffect = await this.#l2BlockSource.getTxEffect(TxHash.fromString(meta.txHash));
789
- if (txEffect) {
790
- meta.minedL2BlockId = {
791
- number: txEffect.l2BlockNumber,
792
- hash: txEffect.l2BlockHash.toString(),
793
- };
794
- }
795
- } catch (err) {
796
- this.#log.warn(`Failed to check mined status for tx ${meta.txHash}`, { err });
797
- }
798
- }
799
- }
800
-
801
1159
  /**
802
1160
  * Rebuilds the pending pool by processing each tx through pre-add rules.
803
1161
  * Starts with an empty pending pool and adds txs one by one, resolving conflicts.
@@ -815,7 +1173,9 @@ export class TxPoolV2Impl {
815
1173
  if (preAddResult.shouldIgnore) {
816
1174
  // Transaction rejected - mark for deletion from DB
817
1175
  rejected.push(meta.txHash);
818
- this.#log.debug(`Rejected tx ${meta.txHash} during rebuild: ${preAddResult.reason}`);
1176
+ this.#log.debug(
1177
+ `Rejected tx ${meta.txHash} during rebuild: ${preAddResult.reason?.message ?? 'unknown reason'}`,
1178
+ );
819
1179
  continue;
820
1180
  }
821
1181
 
@@ -851,7 +1211,7 @@ export class TxPoolV2Impl {
851
1211
  getFeePayerPendingTxs: (feePayer: string) => this.#indices.getFeePayerPendingTxs(feePayer),
852
1212
  getPendingTxCount: () => this.#indices.getPendingTxCount(),
853
1213
  getLowestPriorityPending: (limit: number) => this.#indices.getLowestPriorityPending(limit),
854
- deleteTxs: (txHashes: string[]) => this.#deleteTxsBatch(txHashes),
1214
+ deleteTxs: (txHashes: string[], reason?: string) => this.#evictTxs(txHashes, reason ?? 'unknown'),
855
1215
  };
856
1216
  }
857
1217
 
@@ -870,7 +1230,7 @@ export class TxPoolV2Impl {
870
1230
  const publicStateSource = new DatabasePublicStateSource(db);
871
1231
  const balance = await publicStateSource.storageRead(
872
1232
  ProtocolContractAddress.FeeJuice,
873
- await computeFeePayerBalanceStorageSlot(AztecAddress.fromString(feePayer)),
1233
+ await computeFeePayerBalanceStorageSlot(AztecAddress.fromStringUnsafe(feePayer)),
874
1234
  );
875
1235
  return balance.toBigInt();
876
1236
  },