@aztec/p2p 0.0.1-commit.c31f2472 → 0.0.1-commit.c52d6e7

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