@aztec/p2p 4.0.0-nightly.20250907 → 4.0.0-nightly.20260107

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 (334) hide show
  1. package/dest/bootstrap/bootstrap.d.ts +1 -1
  2. package/dest/bootstrap/bootstrap.d.ts.map +1 -1
  3. package/dest/bootstrap/bootstrap.js +14 -4
  4. package/dest/client/factory.d.ts +3 -2
  5. package/dest/client/factory.d.ts.map +1 -1
  6. package/dest/client/factory.js +9 -5
  7. package/dest/client/index.d.ts +1 -1
  8. package/dest/client/interface.d.ts +8 -6
  9. package/dest/client/interface.d.ts.map +1 -1
  10. package/dest/client/p2p_client.d.ts +12 -35
  11. package/dest/client/p2p_client.d.ts.map +1 -1
  12. package/dest/client/p2p_client.js +457 -61
  13. package/dest/config.d.ts +67 -61
  14. package/dest/config.d.ts.map +1 -1
  15. package/dest/config.js +28 -15
  16. package/dest/enr/generate-enr.d.ts +2 -2
  17. package/dest/enr/generate-enr.d.ts.map +1 -1
  18. package/dest/enr/generate-enr.js +1 -1
  19. package/dest/enr/index.d.ts +1 -1
  20. package/dest/errors/attestation-pool.error.d.ts +7 -0
  21. package/dest/errors/attestation-pool.error.d.ts.map +1 -0
  22. package/dest/errors/attestation-pool.error.js +12 -0
  23. package/dest/errors/reqresp.error.d.ts +1 -1
  24. package/dest/errors/reqresp.error.d.ts.map +1 -1
  25. package/dest/index.d.ts +1 -1
  26. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +43 -6
  27. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  28. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +1 -1
  29. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  30. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +72 -46
  31. package/dest/mem_pools/attestation_pool/index.d.ts +1 -1
  32. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +15 -6
  33. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +1 -1
  34. package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +73 -18
  35. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +13 -6
  36. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +1 -1
  37. package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +69 -11
  38. package/dest/mem_pools/attestation_pool/mocks.d.ts +226 -5
  39. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  40. package/dest/mem_pools/attestation_pool/mocks.js +9 -7
  41. package/dest/mem_pools/index.d.ts +1 -1
  42. package/dest/mem_pools/instrumentation.d.ts +9 -1
  43. package/dest/mem_pools/instrumentation.d.ts.map +1 -1
  44. package/dest/mem_pools/instrumentation.js +38 -2
  45. package/dest/mem_pools/interface.d.ts +3 -4
  46. package/dest/mem_pools/interface.d.ts.map +1 -1
  47. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts +39 -58
  48. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts.map +1 -1
  49. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.js +314 -317
  50. package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts +18 -0
  51. package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts.map +1 -0
  52. package/dest/mem_pools/tx_pool/eviction/eviction_manager.js +56 -0
  53. package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts +83 -0
  54. package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts.map +1 -0
  55. package/dest/mem_pools/tx_pool/eviction/eviction_strategy.js +5 -0
  56. package/dest/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.d.ts +15 -0
  57. package/dest/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.d.ts.map +1 -0
  58. package/dest/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.js +88 -0
  59. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.d.ts +17 -0
  60. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.d.ts.map +1 -0
  61. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.js +84 -0
  62. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.d.ts +19 -0
  63. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.d.ts.map +1 -0
  64. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.js +76 -0
  65. package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.d.ts +26 -0
  66. package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.d.ts.map +1 -0
  67. package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.js +84 -0
  68. package/dest/mem_pools/tx_pool/index.d.ts +1 -2
  69. package/dest/mem_pools/tx_pool/index.d.ts.map +1 -1
  70. package/dest/mem_pools/tx_pool/index.js +0 -1
  71. package/dest/mem_pools/tx_pool/priority.d.ts +5 -1
  72. package/dest/mem_pools/tx_pool/priority.d.ts.map +1 -1
  73. package/dest/mem_pools/tx_pool/priority.js +6 -1
  74. package/dest/mem_pools/tx_pool/tx_pool.d.ts +28 -9
  75. package/dest/mem_pools/tx_pool/tx_pool.d.ts.map +1 -1
  76. package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts +1 -1
  77. package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts.map +1 -1
  78. package/dest/mem_pools/tx_pool/tx_pool_test_suite.js +155 -25
  79. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +4 -2
  80. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
  81. package/dest/msg_validators/attestation_validator/attestation_validator.js +32 -5
  82. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +20 -0
  83. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -0
  84. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.js +67 -0
  85. package/dest/msg_validators/attestation_validator/index.d.ts +2 -1
  86. package/dest/msg_validators/attestation_validator/index.d.ts.map +1 -1
  87. package/dest/msg_validators/attestation_validator/index.js +1 -0
  88. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts +5 -2
  89. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts.map +1 -1
  90. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.js +44 -12
  91. package/dest/msg_validators/block_proposal_validator/index.d.ts +1 -1
  92. package/dest/msg_validators/index.d.ts +1 -1
  93. package/dest/msg_validators/msg_seen_validator/msg_seen_validator.d.ts +1 -1
  94. package/dest/msg_validators/msg_seen_validator/msg_seen_validator.d.ts.map +1 -1
  95. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +1 -1
  96. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
  97. package/dest/msg_validators/tx_validator/allowed_public_setup.d.ts +1 -1
  98. package/dest/msg_validators/tx_validator/archive_cache.d.ts +2 -2
  99. package/dest/msg_validators/tx_validator/archive_cache.d.ts.map +1 -1
  100. package/dest/msg_validators/tx_validator/block_header_validator.d.ts +2 -2
  101. package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
  102. package/dest/msg_validators/tx_validator/block_header_validator.js +1 -1
  103. package/dest/msg_validators/tx_validator/data_validator.d.ts +1 -1
  104. package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
  105. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts +1 -1
  106. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts.map +1 -1
  107. package/dest/msg_validators/tx_validator/double_spend_validator.js +1 -1
  108. package/dest/msg_validators/tx_validator/factory.d.ts +4 -3
  109. package/dest/msg_validators/tx_validator/factory.d.ts.map +1 -1
  110. package/dest/msg_validators/tx_validator/factory.js +11 -5
  111. package/dest/msg_validators/tx_validator/gas_validator.d.ts +1 -1
  112. package/dest/msg_validators/tx_validator/gas_validator.d.ts.map +1 -1
  113. package/dest/msg_validators/tx_validator/index.d.ts +2 -1
  114. package/dest/msg_validators/tx_validator/index.d.ts.map +1 -1
  115. package/dest/msg_validators/tx_validator/index.js +1 -0
  116. package/dest/msg_validators/tx_validator/metadata_validator.d.ts +3 -6
  117. package/dest/msg_validators/tx_validator/metadata_validator.d.ts.map +1 -1
  118. package/dest/msg_validators/tx_validator/metadata_validator.js +6 -24
  119. package/dest/msg_validators/tx_validator/phases_validator.d.ts +1 -1
  120. package/dest/msg_validators/tx_validator/phases_validator.d.ts.map +1 -1
  121. package/dest/msg_validators/tx_validator/phases_validator.js +3 -1
  122. package/dest/msg_validators/tx_validator/test_utils.d.ts +2 -2
  123. package/dest/msg_validators/tx_validator/test_utils.d.ts.map +1 -1
  124. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +13 -0
  125. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -0
  126. package/dest/msg_validators/tx_validator/timestamp_validator.js +32 -0
  127. package/dest/msg_validators/tx_validator/tx_permitted_validator.d.ts +1 -1
  128. package/dest/msg_validators/tx_validator/tx_permitted_validator.d.ts.map +1 -1
  129. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts +1 -1
  130. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts.map +1 -1
  131. package/dest/services/data_store.d.ts +1 -1
  132. package/dest/services/data_store.d.ts.map +1 -1
  133. package/dest/services/discv5/discV5_service.d.ts +3 -3
  134. package/dest/services/discv5/discV5_service.d.ts.map +1 -1
  135. package/dest/services/discv5/discV5_service.js +2 -2
  136. package/dest/services/dummy_service.d.ts +2 -2
  137. package/dest/services/dummy_service.d.ts.map +1 -1
  138. package/dest/services/encoding.d.ts +25 -4
  139. package/dest/services/encoding.d.ts.map +1 -1
  140. package/dest/services/encoding.js +74 -6
  141. package/dest/services/gossipsub/scoring.d.ts +1 -1
  142. package/dest/services/index.d.ts +1 -1
  143. package/dest/services/libp2p/instrumentation.d.ts +3 -1
  144. package/dest/services/libp2p/instrumentation.d.ts.map +1 -1
  145. package/dest/services/libp2p/instrumentation.js +9 -2
  146. package/dest/services/libp2p/libp2p_service.d.ts +32 -72
  147. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  148. package/dest/services/libp2p/libp2p_service.js +796 -171
  149. package/dest/services/peer-manager/interface.d.ts +1 -1
  150. package/dest/services/peer-manager/metrics.d.ts +8 -1
  151. package/dest/services/peer-manager/metrics.d.ts.map +1 -1
  152. package/dest/services/peer-manager/metrics.js +28 -0
  153. package/dest/services/peer-manager/peer_manager.d.ts +1 -32
  154. package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
  155. package/dest/services/peer-manager/peer_manager.js +412 -21
  156. package/dest/services/peer-manager/peer_scoring.d.ts +7 -2
  157. package/dest/services/peer-manager/peer_scoring.d.ts.map +1 -1
  158. package/dest/services/peer-manager/peer_scoring.js +40 -2
  159. package/dest/services/reqresp/config.d.ts +1 -1
  160. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts +1 -1
  161. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts.map +1 -1
  162. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts +1 -4
  163. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts.map +1 -1
  164. package/dest/services/reqresp/index.d.ts +1 -1
  165. package/dest/services/reqresp/interface.d.ts +2 -11
  166. package/dest/services/reqresp/interface.d.ts.map +1 -1
  167. package/dest/services/reqresp/interface.js +1 -18
  168. package/dest/services/reqresp/metrics.d.ts +1 -1
  169. package/dest/services/reqresp/metrics.d.ts.map +1 -1
  170. package/dest/services/reqresp/protocols/auth.d.ts +2 -2
  171. package/dest/services/reqresp/protocols/auth.d.ts.map +1 -1
  172. package/dest/services/reqresp/protocols/auth.js +2 -2
  173. package/dest/services/reqresp/protocols/block.d.ts +1 -1
  174. package/dest/services/reqresp/protocols/block.d.ts.map +1 -1
  175. package/dest/services/reqresp/protocols/block.js +3 -2
  176. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts +1 -1
  177. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts.map +1 -1
  178. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +1 -1
  179. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +4 -6
  180. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
  181. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +1 -1
  182. package/dest/services/reqresp/protocols/block_txs/index.d.ts +1 -1
  183. package/dest/services/reqresp/protocols/goodbye.d.ts +1 -1
  184. package/dest/services/reqresp/protocols/goodbye.d.ts.map +1 -1
  185. package/dest/services/reqresp/protocols/index.d.ts +1 -1
  186. package/dest/services/reqresp/protocols/ping.d.ts +1 -1
  187. package/dest/services/reqresp/protocols/status.d.ts +6 -5
  188. package/dest/services/reqresp/protocols/status.d.ts.map +1 -1
  189. package/dest/services/reqresp/protocols/status.js +4 -3
  190. package/dest/services/reqresp/protocols/tx.d.ts +2 -3
  191. package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
  192. package/dest/services/reqresp/rate-limiter/index.d.ts +1 -1
  193. package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts +2 -2
  194. package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts.map +1 -1
  195. package/dest/services/reqresp/rate-limiter/rate_limits.d.ts +1 -1
  196. package/dest/services/reqresp/reqresp.d.ts +1 -41
  197. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  198. package/dest/services/reqresp/reqresp.js +416 -34
  199. package/dest/services/reqresp/status.d.ts +2 -2
  200. package/dest/services/reqresp/status.d.ts.map +1 -1
  201. package/dest/services/service.d.ts +2 -2
  202. package/dest/services/service.d.ts.map +1 -1
  203. package/dest/services/tx_collection/config.d.ts +1 -1
  204. package/dest/services/tx_collection/config.js +1 -1
  205. package/dest/services/tx_collection/fast_tx_collection.d.ts +4 -9
  206. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
  207. package/dest/services/tx_collection/fast_tx_collection.js +6 -1
  208. package/dest/services/tx_collection/index.d.ts +1 -1
  209. package/dest/services/tx_collection/instrumentation.d.ts +1 -1
  210. package/dest/services/tx_collection/instrumentation.d.ts.map +1 -1
  211. package/dest/services/tx_collection/slow_tx_collection.d.ts +6 -7
  212. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -1
  213. package/dest/services/tx_collection/slow_tx_collection.js +2 -1
  214. package/dest/services/tx_collection/tx_collection.d.ts +12 -11
  215. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
  216. package/dest/services/tx_collection/tx_collection.js +3 -2
  217. package/dest/services/tx_collection/tx_collection_sink.d.ts +3 -3
  218. package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -1
  219. package/dest/services/tx_collection/tx_collection_sink.js +34 -4
  220. package/dest/services/tx_collection/tx_source.d.ts +1 -1
  221. package/dest/services/tx_collection/tx_source.d.ts.map +1 -1
  222. package/dest/services/tx_collection/tx_source.js +2 -2
  223. package/dest/services/tx_provider.d.ts +6 -4
  224. package/dest/services/tx_provider.d.ts.map +1 -1
  225. package/dest/services/tx_provider.js +19 -6
  226. package/dest/services/tx_provider_instrumentation.d.ts +5 -2
  227. package/dest/services/tx_provider_instrumentation.d.ts.map +1 -1
  228. package/dest/services/tx_provider_instrumentation.js +14 -1
  229. package/dest/test-helpers/generate-peer-id-private-keys.d.ts +1 -1
  230. package/dest/test-helpers/get-ports.d.ts +1 -1
  231. package/dest/test-helpers/get-ports.d.ts.map +1 -1
  232. package/dest/test-helpers/index.d.ts +1 -1
  233. package/dest/test-helpers/make-enrs.d.ts +1 -1
  234. package/dest/test-helpers/make-enrs.js +1 -1
  235. package/dest/test-helpers/make-test-p2p-clients.d.ts +2 -2
  236. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  237. package/dest/test-helpers/mock-pubsub.d.ts +4 -4
  238. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  239. package/dest/test-helpers/mock-tx-helpers.d.ts +12 -0
  240. package/dest/test-helpers/mock-tx-helpers.d.ts.map +1 -0
  241. package/dest/test-helpers/mock-tx-helpers.js +19 -0
  242. package/dest/test-helpers/reqresp-nodes.d.ts +3 -3
  243. package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
  244. package/dest/test-helpers/reqresp-nodes.js +4 -3
  245. package/dest/testbench/p2p_client_testbench_worker.d.ts +1 -1
  246. package/dest/testbench/p2p_client_testbench_worker.js +17 -9
  247. package/dest/testbench/parse_log_file.d.ts +1 -1
  248. package/dest/testbench/testbench.d.ts +1 -1
  249. package/dest/testbench/testbench.js +2 -2
  250. package/dest/testbench/worker_client_manager.d.ts +1 -1
  251. package/dest/testbench/worker_client_manager.d.ts.map +1 -1
  252. package/dest/types/index.d.ts +1 -1
  253. package/dest/util.d.ts +2 -1
  254. package/dest/util.d.ts.map +1 -1
  255. package/dest/util.js +11 -2
  256. package/dest/versioning.d.ts +2 -2
  257. package/dest/versioning.d.ts.map +1 -1
  258. package/dest/versioning.js +2 -2
  259. package/package.json +21 -21
  260. package/src/bootstrap/bootstrap.ts +15 -4
  261. package/src/client/factory.ts +21 -12
  262. package/src/client/interface.ts +8 -5
  263. package/src/client/p2p_client.ts +97 -88
  264. package/src/config.ts +42 -21
  265. package/src/enr/generate-enr.ts +1 -1
  266. package/src/errors/attestation-pool.error.ts +13 -0
  267. package/src/mem_pools/attestation_pool/attestation_pool.ts +46 -5
  268. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +89 -48
  269. package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +107 -24
  270. package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +98 -19
  271. package/src/mem_pools/attestation_pool/mocks.ts +11 -8
  272. package/src/mem_pools/instrumentation.ts +46 -0
  273. package/src/mem_pools/interface.ts +2 -4
  274. package/src/mem_pools/tx_pool/README.md +255 -0
  275. package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +368 -360
  276. package/src/mem_pools/tx_pool/eviction/eviction_manager.ts +71 -0
  277. package/src/mem_pools/tx_pool/eviction/eviction_strategy.ts +93 -0
  278. package/src/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.ts +108 -0
  279. package/src/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.ts +104 -0
  280. package/src/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.ts +91 -0
  281. package/src/mem_pools/tx_pool/eviction/low_priority_eviction_rule.ts +106 -0
  282. package/src/mem_pools/tx_pool/index.ts +0 -1
  283. package/src/mem_pools/tx_pool/priority.ts +8 -1
  284. package/src/mem_pools/tx_pool/tx_pool.ts +28 -8
  285. package/src/mem_pools/tx_pool/tx_pool_test_suite.ts +131 -18
  286. package/src/msg_validators/attestation_validator/attestation_validator.ts +41 -6
  287. package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +91 -0
  288. package/src/msg_validators/attestation_validator/index.ts +1 -0
  289. package/src/msg_validators/block_proposal_validator/block_proposal_validator.ts +53 -12
  290. package/src/msg_validators/tx_validator/archive_cache.ts +1 -1
  291. package/src/msg_validators/tx_validator/block_header_validator.ts +2 -2
  292. package/src/msg_validators/tx_validator/double_spend_validator.ts +1 -1
  293. package/src/msg_validators/tx_validator/factory.ts +13 -6
  294. package/src/msg_validators/tx_validator/index.ts +1 -0
  295. package/src/msg_validators/tx_validator/metadata_validator.ts +8 -42
  296. package/src/msg_validators/tx_validator/phases_validator.ts +3 -1
  297. package/src/msg_validators/tx_validator/test_utils.ts +1 -1
  298. package/src/msg_validators/tx_validator/timestamp_validator.ts +47 -0
  299. package/src/services/discv5/discV5_service.ts +2 -2
  300. package/src/services/dummy_service.ts +1 -1
  301. package/src/services/encoding.ts +81 -6
  302. package/src/services/libp2p/instrumentation.ts +10 -1
  303. package/src/services/libp2p/libp2p_service.ts +430 -165
  304. package/src/services/peer-manager/metrics.ts +32 -0
  305. package/src/services/peer-manager/peer_manager.ts +24 -14
  306. package/src/services/peer-manager/peer_scoring.ts +46 -3
  307. package/src/services/reqresp/interface.ts +1 -22
  308. package/src/services/reqresp/protocols/auth.ts +2 -2
  309. package/src/services/reqresp/protocols/block.ts +3 -2
  310. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +1 -1
  311. package/src/services/reqresp/protocols/status.ts +9 -8
  312. package/src/services/reqresp/protocols/tx.ts +1 -2
  313. package/src/services/reqresp/reqresp.ts +15 -11
  314. package/src/services/service.ts +1 -1
  315. package/src/services/tx_collection/config.ts +1 -1
  316. package/src/services/tx_collection/fast_tx_collection.ts +8 -5
  317. package/src/services/tx_collection/slow_tx_collection.ts +7 -6
  318. package/src/services/tx_collection/tx_collection.ts +12 -10
  319. package/src/services/tx_collection/tx_collection_sink.ts +34 -3
  320. package/src/services/tx_collection/tx_source.ts +2 -2
  321. package/src/services/tx_provider.ts +26 -9
  322. package/src/services/tx_provider_instrumentation.ts +19 -2
  323. package/src/test-helpers/make-enrs.ts +1 -1
  324. package/src/test-helpers/mock-pubsub.ts +1 -1
  325. package/src/test-helpers/mock-tx-helpers.ts +24 -0
  326. package/src/test-helpers/reqresp-nodes.ts +4 -3
  327. package/src/testbench/p2p_client_testbench_worker.ts +14 -6
  328. package/src/testbench/testbench.ts +2 -2
  329. package/src/util.ts +12 -2
  330. package/src/versioning.ts +3 -3
  331. package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts +0 -68
  332. package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts.map +0 -1
  333. package/dest/mem_pools/tx_pool/memory_tx_pool.js +0 -160
  334. package/src/mem_pools/tx_pool/memory_tx_pool.ts +0 -199
@@ -1,37 +1,38 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { insertIntoSortedArray } from '@aztec/foundation/array';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
3
  import { toArray } from '@aztec/foundation/iterable';
3
4
  import { createLogger } from '@aztec/foundation/log';
4
- import { ProtocolContractAddress } from '@aztec/protocol-contracts';
5
- import { GasFees } from '@aztec/stdlib/gas';
6
- import { ClientIvcProof } from '@aztec/stdlib/proofs';
7
- import { DatabasePublicStateSource } from '@aztec/stdlib/trees';
5
+ import { ChonkProof } from '@aztec/stdlib/proofs';
8
6
  import { Tx, TxHash } from '@aztec/stdlib/tx';
9
7
  import { getTelemetryClient } from '@aztec/telemetry-client';
10
8
  import assert from 'assert';
11
9
  import EventEmitter from 'node:events';
12
10
  import { ArchiveCache } from '../../msg_validators/tx_validator/archive_cache.js';
13
- import { GasTxValidator } from '../../msg_validators/tx_validator/gas_validator.js';
14
11
  import { PoolInstrumentation, PoolName } from '../instrumentation.js';
12
+ import { EvictionManager } from './eviction/eviction_manager.js';
13
+ import { InsufficientFeePayerBalanceRule } from './eviction/insufficient_fee_payer_balance_rule.js';
14
+ import { InvalidTxsAfterMiningRule } from './eviction/invalid_txs_after_mining_rule.js';
15
+ import { InvalidTxsAfterReorgRule } from './eviction/invalid_txs_after_reorg_rule.js';
16
+ import { LowPriorityEvictionRule } from './eviction/low_priority_eviction_rule.js';
15
17
  import { getPendingTxPriority } from './priority.js';
16
18
  /**
17
19
  * KV implementation of the Transaction Pool.
18
20
  */ export class AztecKVTxPool extends EventEmitter {
19
21
  #store;
20
22
  /** Our tx pool, stored as a Map, with K: tx hash and V: the transaction. */ #txs;
21
- /** The maximum cumulative tx size that the pending txs in the pool take up. */ #maxTxPoolSize = 0;
22
- /** The tx evicion logic will kick after pool size is greater than maxTxPoolSize * txPoolOverflowFactor */ txPoolOverflowFactor = 1;
23
+ /** Holds the historical block for each tx */ #pendingTxHashToHistoricalBlockHeaderHash;
23
24
  /** Index from tx hash to the block number in which they were mined, filtered by mined txs. */ #minedTxHashToBlock;
24
25
  /** Index from tx priority (stored as hex) to its tx hash, filtered by pending txs. */ #pendingTxPriorityToHash;
25
- /** Index from tx hash to its tx size (in bytes), filtered by pending txs. */ #pendingTxHashToSize;
26
- /** Index from tx hash to its header hash, filtered by pending txs. */ #pendingTxHashToHeaderHash;
27
- /** The cumulative tx size in bytes that the pending txs in the pool take up. */ #pendingTxSize;
28
- /** In-memory mapping of pending tx hashes to the hydrated pending tx in the pool. */ #pendingTxs;
26
+ /** Map from tx hash to the block number it was originally mined in (for soft-deleted txs). */ #deletedMinedTxHashes;
27
+ /** MultiMap from block number to deleted mined tx hashes for efficient cleanup. */ #blockToDeletedMinedTxHash;
28
+ #historicalHeaderToTxHash;
29
+ #feePayerToTxHash;
29
30
  /** In-memory set of txs that should not be evicted from the pool. */ #nonEvictableTxs;
30
31
  /** KV store for archived txs. */ #archive;
31
32
  /** Archived txs map for future lookup. */ #archivedTxs;
32
33
  /** Indexes of the archived txs by insertion order. */ #archivedTxIndices;
33
34
  /** Number of txs to archive. */ #archivedTxLimit = 0;
34
- /** The world state synchronizer used in the node. */ #worldStateSynchronizer;
35
+ #evictionManager;
35
36
  #log;
36
37
  #metrics;
37
38
  /**
@@ -41,23 +42,31 @@ import { getPendingTxPriority } from './priority.js';
41
42
  * @param telemetry - A telemetry client.
42
43
  * @param archivedTxLimit - The number of txs to archive.
43
44
  * @param log - A logger.
44
- */ constructor(store, archive, worldStateSynchronizer, telemetry = getTelemetryClient(), config = {}, log = createLogger('p2p:tx_pool')){
45
+ */ constructor(store, archive, worldState, telemetry = getTelemetryClient(), config = {}, log = createLogger('p2p:tx_pool')){
45
46
  super();
46
47
  this.#log = log;
48
+ this.#evictionManager = new EvictionManager(this);
49
+ this.#evictionManager.registerRule(new InvalidTxsAfterMiningRule());
50
+ this.#evictionManager.registerRule(new InvalidTxsAfterReorgRule(worldState));
51
+ this.#evictionManager.registerRule(new InsufficientFeePayerBalanceRule(worldState));
52
+ this.#evictionManager.registerRule(new LowPriorityEvictionRule({
53
+ //NOTE: 0 effectively disables low priority eviction
54
+ maxPoolSize: config.maxPendingTxCount ?? 0
55
+ }));
47
56
  this.updateConfig(config);
48
57
  this.#txs = store.openMap('txs');
49
58
  this.#minedTxHashToBlock = store.openMap('txHashToBlockMined');
50
59
  this.#pendingTxPriorityToHash = store.openMultiMap('pendingTxFeeToHash');
51
- this.#pendingTxHashToSize = store.openMap('pendingTxHashToSize');
52
- this.#pendingTxHashToHeaderHash = store.openMap('pendingTxHashToHeaderHash');
53
- this.#pendingTxSize = store.openSingleton('pendingTxSize');
54
- this.#pendingTxs = new Map();
60
+ this.#deletedMinedTxHashes = store.openMap('deletedMinedTxHashes');
61
+ this.#blockToDeletedMinedTxHash = store.openMultiMap('blockToDeletedMinedTxHash');
62
+ this.#pendingTxHashToHistoricalBlockHeaderHash = store.openMap('txHistoricalBlock');
63
+ this.#historicalHeaderToTxHash = store.openMultiMap('historicalHeaderToPendingTxHash');
64
+ this.#feePayerToTxHash = store.openMultiMap('feePayerToPendingTxHash');
55
65
  this.#nonEvictableTxs = new Set();
56
66
  this.#archivedTxs = archive.openMap('archivedTxs');
57
67
  this.#archivedTxIndices = archive.openMap('archivedTxIndices');
58
68
  this.#store = store;
59
69
  this.#archive = archive;
60
- this.#worldStateSynchronizer = worldStateSynchronizer;
61
70
  this.#metrics = new PoolInstrumentation(telemetry, PoolName.TX_POOL, this.countTxs, ()=>store.estimateSize());
62
71
  }
63
72
  countTxs = async ()=>{
@@ -88,83 +97,67 @@ import { getPendingTxPriority } from './priority.js';
88
97
  if (txHashes.length === 0) {
89
98
  return Promise.resolve();
90
99
  }
91
- const minedNullifiers = new Set();
92
- const minedFeePayers = new Set();
93
- await this.#store.transactionAsync(async ()=>{
94
- let pendingTxSize = await this.#pendingTxSize.getAsync() ?? 0;
95
- for (const hash of txHashes){
96
- const key = hash.toString();
97
- await this.#minedTxHashToBlock.set(key, blockHeader.globalVariables.blockNumber);
98
- const tx = await this.getPendingTxByHash(hash);
99
- if (tx) {
100
- const nullifiers = tx.data.getNonEmptyNullifiers();
101
- nullifiers.forEach((nullifier)=>minedNullifiers.add(nullifier.toString()));
102
- minedFeePayers.add(tx.data.feePayer.toString());
103
- pendingTxSize -= tx.getSize();
104
- await this.removePendingTxIndices(tx, key);
100
+ const uniqueMinedNullifiers = [];
101
+ const uniqueMinedFeePayers = [];
102
+ try {
103
+ await this.#store.transactionAsync(async ()=>{
104
+ for (const hash of txHashes){
105
+ const key = hash.toString();
106
+ await this.#minedTxHashToBlock.set(key, blockHeader.globalVariables.blockNumber);
107
+ const tx = await this.getPendingTxByHash(hash);
108
+ if (tx) {
109
+ const nullifiers = tx.data.getNonEmptyNullifiers();
110
+ nullifiers.forEach((nullifier)=>insertIntoSortedArray(uniqueMinedNullifiers, nullifier, Fr.cmp, false));
111
+ insertIntoSortedArray(uniqueMinedFeePayers, tx.data.feePayer, (a, b)=>a.toField().cmp(b.toField()), false);
112
+ await this.removePendingTxIndicesInDbTx(tx, key);
113
+ }
114
+ // If this tx was previously soft-deleted, remove it from the deleted sets
115
+ if (await this.#deletedMinedTxHashes.hasAsync(key)) {
116
+ const originalBlock = await this.#deletedMinedTxHashes.getAsync(key);
117
+ await this.#deletedMinedTxHashes.delete(key);
118
+ // Remove from block-to-hash mapping
119
+ if (originalBlock !== undefined) {
120
+ await this.#blockToDeletedMinedTxHash.deleteValue(originalBlock, key);
121
+ }
122
+ }
105
123
  }
106
- }
107
- await this.#pendingTxSize.set(pendingTxSize);
108
- await this.evictInvalidTxsAfterMining(txHashes, blockHeader, minedNullifiers, minedFeePayers);
109
- });
110
- // We update this after the transaction above. This ensures that the non-evictable transactions are not evicted
111
- // until any that have been mined are marked as such.
112
- // The non-evictable set is not considered when evicting transactions that are invalid after a block is mined.
113
- this.#nonEvictableTxs.clear();
124
+ });
125
+ await this.#evictionManager.evictAfterNewBlock(blockHeader, uniqueMinedNullifiers, uniqueMinedFeePayers);
126
+ this.#metrics.transactionsRemoved(txHashes.map((hash)=>hash.toBigInt()));
127
+ } catch (err) {
128
+ this.#log.warn('Unexpected error when marking txs as mined', {
129
+ err
130
+ });
131
+ }
114
132
  }
115
- async markMinedAsPending(txHashes) {
133
+ async markMinedAsPending(txHashes, latestBlock) {
116
134
  if (txHashes.length === 0) {
117
135
  return Promise.resolve();
118
136
  }
119
- await this.#store.transactionAsync(async ()=>{
120
- let pendingTxSize = await this.#pendingTxSize.getAsync() ?? 0;
121
- for (const hash of txHashes){
122
- const key = hash.toString();
123
- await this.#minedTxHashToBlock.delete(key);
124
- // Rehydrate the tx in the in-memory pending txs mapping
125
- const tx = await this.getPendingTxByHash(hash);
126
- if (tx) {
127
- await this.addPendingTxIndices(tx, key);
128
- pendingTxSize += tx.getSize();
137
+ try {
138
+ await this.#store.transactionAsync(async ()=>{
139
+ for (const hash of txHashes){
140
+ const key = hash.toString();
141
+ await this.#minedTxHashToBlock.delete(key);
142
+ // Rehydrate the tx in the in-memory pending txs mapping
143
+ const tx = await this.getPendingTxByHash(hash);
144
+ if (tx) {
145
+ await this.addPendingTxIndicesInDbTx(tx, key);
146
+ }
129
147
  }
130
- }
131
- await this.#pendingTxSize.set(pendingTxSize);
132
- });
133
- await this.evictInvalidTxsAfterReorg(txHashes);
134
- await this.evictLowPriorityTxs(txHashes);
148
+ });
149
+ await this.#evictionManager.evictAfterChainPrune(latestBlock);
150
+ } catch (err) {
151
+ this.#log.warn('Unexpected error when marking mined txs as pending', {
152
+ err
153
+ });
154
+ }
135
155
  }
136
156
  async getPendingTxHashes() {
137
157
  const vals = await toArray(this.#pendingTxPriorityToHash.valuesAsync({
138
158
  reverse: true
139
159
  }));
140
- return vals.map((x)=>TxHash.fromString(x));
141
- }
142
- async getMinedTxHashes() {
143
- const vals = await toArray(this.#minedTxHashToBlock.entriesAsync());
144
- return vals.map(([txHash, blockNumber])=>[
145
- TxHash.fromString(txHash),
146
- blockNumber
147
- ]);
148
- }
149
- async getPendingTxCount() {
150
- return await this.#pendingTxHashToHeaderHash.sizeAsync() ?? 0;
151
- }
152
- async getMinedTxCount() {
153
- return await this.#minedTxHashToBlock.sizeAsync() ?? 0;
154
- }
155
- async getTxStatus(txHash) {
156
- const key = txHash.toString();
157
- const [isMined, isKnown] = await Promise.all([
158
- this.#minedTxHashToBlock.hasAsync(key),
159
- this.#txs.hasAsync(key)
160
- ]);
161
- if (isMined) {
162
- return 'mined';
163
- } else if (isKnown) {
164
- return 'pending';
165
- } else {
166
- return undefined;
167
- }
160
+ return vals.map(TxHash.fromString);
168
161
  }
169
162
  /**
170
163
  * Checks if a transaction exists in the pool and returns it.
@@ -172,75 +165,72 @@ import { getPendingTxPriority } from './priority.js';
172
165
  * @returns The transaction, if found, 'undefined' otherwise.
173
166
  */ async getTxByHash(txHash) {
174
167
  const buffer = await this.#txs.getAsync(txHash.toString());
175
- if (buffer) {
176
- const tx = Tx.fromBuffer(buffer);
177
- tx.setTxHash(txHash);
178
- return tx;
179
- }
180
- return undefined;
168
+ return buffer ? Tx.fromBuffer(buffer) : undefined;
181
169
  }
182
170
  async getTxsByHash(txHashes) {
183
171
  const txs = await Promise.all(txHashes.map((txHash)=>this.#txs.getAsync(txHash.toString())));
184
- return txs.map((buffer, index)=>{
185
- if (buffer) {
186
- const tx = Tx.fromBuffer(buffer);
187
- tx.setTxHash(txHashes[index]);
188
- return tx;
189
- }
190
- return undefined;
191
- });
172
+ return txs.map((buffer)=>buffer ? Tx.fromBuffer(buffer) : undefined);
192
173
  }
193
174
  async hasTxs(txHashes) {
194
175
  return await Promise.all(txHashes.map((txHash)=>this.#txs.hasAsync(txHash.toString())));
195
176
  }
177
+ async hasTx(txHash) {
178
+ const result = await this.hasTxs([
179
+ txHash
180
+ ]);
181
+ return result[0];
182
+ }
196
183
  /**
197
184
  * Checks if an archived tx exists and returns it.
198
185
  * @param txHash - The tx hash.
199
186
  * @returns The transaction metadata, if found, 'undefined' otherwise.
200
187
  */ async getArchivedTxByHash(txHash) {
201
188
  const buffer = await this.#archivedTxs.getAsync(txHash.toString());
202
- if (buffer) {
203
- const tx = Tx.fromBuffer(buffer);
204
- tx.setTxHash(txHash);
205
- return tx;
206
- }
207
- return undefined;
189
+ return buffer ? Tx.fromBuffer(buffer) : undefined;
208
190
  }
209
191
  /**
210
192
  * Adds a list of transactions to the pool. Duplicates are ignored.
211
193
  * @param txs - An array of txs to be added to the pool.
212
- * @returns Empty promise.
194
+ * @returns count of added transactions
213
195
  */ async addTxs(txs, opts = {}) {
196
+ if (txs.length === 0) {
197
+ return Promise.resolve(0);
198
+ }
214
199
  const addedTxs = [];
215
200
  const hashesAndStats = txs.map((tx)=>({
216
201
  txHash: tx.getTxHash(),
217
202
  txStats: tx.getStats()
218
203
  }));
219
- await this.#store.transactionAsync(async ()=>{
220
- let pendingTxSize = await this.#pendingTxSize.getAsync() ?? 0;
221
- await Promise.all(txs.map(async (tx, i)=>{
222
- const { txHash, txStats } = hashesAndStats[i];
223
- const key = txHash.toString();
224
- if (await this.#txs.hasAsync(key)) {
225
- this.#log.debug(`Tx ${txHash.toString()} already exists in the pool`);
226
- return;
227
- }
228
- this.#log.verbose(`Adding tx ${txHash.toString()} to pool`, {
229
- eventName: 'tx-added-to-pool',
230
- ...txStats
231
- });
232
- await this.#txs.set(key, tx.toBuffer());
233
- addedTxs.push(tx);
234
- if (!await this.#minedTxHashToBlock.hasAsync(key)) {
235
- pendingTxSize += tx.getSize();
236
- await this.addPendingTxIndices(tx, key);
237
- this.#metrics.recordSize(tx);
238
- }
239
- }));
240
- await this.#pendingTxSize.set(pendingTxSize);
241
- await this.evictLowPriorityTxs(hashesAndStats.map(({ txHash })=>txHash));
242
- });
204
+ try {
205
+ await this.#store.transactionAsync(async ()=>{
206
+ await Promise.all(txs.map(async (tx, i)=>{
207
+ const { txHash, txStats } = hashesAndStats[i];
208
+ const key = txHash.toString();
209
+ if (await this.#txs.hasAsync(key)) {
210
+ this.#log.debug(`Tx ${txHash.toString()} already exists in the pool`);
211
+ return;
212
+ }
213
+ this.#log.verbose(`Adding tx ${txHash.toString()} to pool`, {
214
+ eventName: 'tx-added-to-pool',
215
+ ...txStats
216
+ });
217
+ await this.#txs.set(key, tx.toBuffer());
218
+ addedTxs.push(tx);
219
+ await this.#pendingTxHashToHistoricalBlockHeaderHash.set(key, (await tx.data.constants.anchorBlockHeader.hash()).toString());
220
+ if (!await this.#minedTxHashToBlock.hasAsync(key)) {
221
+ await this.addPendingTxIndicesInDbTx(tx, key);
222
+ this.#metrics.recordSize(tx);
223
+ }
224
+ }));
225
+ });
226
+ await this.#evictionManager.evictAfterNewTxs(addedTxs.map(({ txHash })=>txHash));
227
+ } catch (err) {
228
+ this.#log.warn('Unexpected error when adding txs', {
229
+ err
230
+ });
231
+ }
243
232
  if (addedTxs.length > 0) {
233
+ this.#metrics.transactionsAdded(addedTxs);
244
234
  this.emit('txs-added', {
245
235
  ...opts,
246
236
  txs: addedTxs
@@ -250,48 +240,66 @@ import { getPendingTxPriority } from './priority.js';
250
240
  }
251
241
  /**
252
242
  * Deletes transactions from the pool. Tx hashes that are not present are ignored.
253
- * @param txHashes - An array of tx hashes to be removed from the tx pool.
243
+ * Mined transactions are soft-deleted with a timestamp, pending transactions are permanently deleted.
244
+ * @param txHashes - An array of tx hashes to be deleted from the tx pool.
254
245
  * @returns Empty promise.
255
- */ deleteTxs(txHashes, eviction = false) {
246
+ */ deleteTxs(txHashes, opts) {
256
247
  if (txHashes.length === 0) {
257
248
  return Promise.resolve();
258
249
  }
259
250
  const deletedTxs = [];
260
251
  const poolDbTx = this.#store.transactionAsync(async ()=>{
261
- let pendingTxSize = await this.#pendingTxSize.getAsync() ?? 0;
262
252
  for (const hash of txHashes){
263
253
  const key = hash.toString();
264
254
  const tx = await this.getTxByHash(hash);
265
- if (tx) {
266
- const isMined = await this.#minedTxHashToBlock.hasAsync(key);
267
- if (!isMined) {
268
- pendingTxSize -= tx.getSize();
269
- await this.removePendingTxIndices(tx, key);
270
- }
271
- if (!eviction && this.#archivedTxLimit) {
255
+ if (!tx) {
256
+ this.#log.trace(`Skipping deletion of missing tx ${key} from pool`);
257
+ continue;
258
+ }
259
+ const minedBlockNumber = await this.#minedTxHashToBlock.getAsync(key);
260
+ const txIsPending = minedBlockNumber === undefined;
261
+ if (txIsPending) {
262
+ await this.deletePendingTx(tx, key);
263
+ } else {
264
+ await this.deleteMinedTx(key, minedBlockNumber, opts?.permanently ?? false);
265
+ const shouldArchiveTx = this.#archivedTxLimit && !opts?.permanently;
266
+ if (shouldArchiveTx) {
272
267
  deletedTxs.push(tx);
273
268
  }
274
- await this.#txs.delete(key);
275
- await this.#minedTxHashToBlock.delete(key);
276
269
  }
277
270
  }
278
- await this.#pendingTxSize.set(pendingTxSize);
279
271
  });
272
+ this.#metrics.transactionsRemoved(txHashes.map((hash)=>hash.toBigInt()));
280
273
  this.#log.debug(`Deleted ${txHashes.length} txs from pool`, {
281
274
  txHashes
282
275
  });
283
276
  return this.#archivedTxLimit ? poolDbTx.then(()=>this.archiveTxs(deletedTxs)) : poolDbTx;
284
277
  }
278
+ async deleteMinedTx(txHash, minedBlockNumber, permanently) {
279
+ await this.#minedTxHashToBlock.delete(txHash);
280
+ if (permanently) {
281
+ this.#log.trace(`Deleting mined tx ${txHash} from pool`);
282
+ await this.#txs.delete(txHash);
283
+ return;
284
+ }
285
+ // Soft-delete mined transactions: remove from mined set but keep in storage
286
+ this.#log.trace(`Soft-deleting mined tx ${txHash} from pool`);
287
+ await this.#deletedMinedTxHashes.set(txHash, minedBlockNumber);
288
+ await this.#blockToDeletedMinedTxHash.set(minedBlockNumber, txHash);
289
+ }
290
+ async deletePendingTx(tx, txHash) {
291
+ // We always permanently delete pending transactions
292
+ this.#log.trace(`Deleting pending tx ${txHash} from pool`);
293
+ await this.removePendingTxIndices(tx, txHash);
294
+ await this.#txs.delete(txHash);
295
+ await this.#pendingTxHashToHistoricalBlockHeaderHash.delete(txHash);
296
+ }
285
297
  /**
286
298
  * Gets all the transactions stored in the pool.
287
299
  * @returns Array of tx objects in the order they were added to the pool.
288
300
  */ async getAllTxs() {
289
- const vals = await toArray(this.#txs.entriesAsync());
290
- return vals.map(([hash, buffer])=>{
291
- const tx = Tx.fromBuffer(buffer);
292
- tx.setTxHash(TxHash.fromString(hash));
293
- return tx;
294
- });
301
+ const vals = await toArray(this.#txs.valuesAsync());
302
+ return vals.map((buffer)=>Tx.fromBuffer(buffer));
295
303
  }
296
304
  /**
297
305
  * Gets the hashes of all transactions currently in the tx pool.
@@ -300,41 +308,136 @@ import { getPendingTxPriority } from './priority.js';
300
308
  const vals = await toArray(this.#txs.keysAsync());
301
309
  return vals.map((x)=>TxHash.fromString(x));
302
310
  }
303
- updateConfig({ maxTxPoolSize, txPoolOverflowFactor, archivedTxLimit }) {
304
- if (typeof maxTxPoolSize === 'number') {
305
- assert(maxTxPoolSize >= 0, 'maxTxPoolSize must be greater or equal to 0');
306
- this.#maxTxPoolSize = maxTxPoolSize;
307
- if (maxTxPoolSize === 0) {
308
- this.#log.info(`Disabling maximum tx mempool size. Tx eviction stopped`);
309
- } else {
310
- this.#log.info(`Setting maximum tx mempool size`, {
311
- maxTxPoolSize
312
- });
311
+ async getPendingTxInfos() {
312
+ const vals = await toArray(this.#pendingTxPriorityToHash.valuesAsync());
313
+ const results = await Promise.all(vals.map((val)=>this.getPendingTxInfo(TxHash.fromString(val))));
314
+ return results.filter((info)=>info !== undefined);
315
+ }
316
+ async getPendingTxInfo(txHash) {
317
+ let historicalBlockHash = await this.#pendingTxHashToHistoricalBlockHeaderHash.getAsync(txHash.toString());
318
+ // Not all tx might have this index created.
319
+ if (!historicalBlockHash) {
320
+ const tx = await this.getPendingTxByHash(txHash);
321
+ if (!tx) {
322
+ this.#log.warn(`PendingTxInfo:tx ${txHash} not found`);
323
+ return undefined;
313
324
  }
325
+ historicalBlockHash = (await tx.data.constants.anchorBlockHeader.hash()).toString();
326
+ await this.#pendingTxHashToHistoricalBlockHeaderHash.set(txHash.toString(), historicalBlockHash);
314
327
  }
315
- if (typeof txPoolOverflowFactor === 'number') {
316
- assert(txPoolOverflowFactor >= 1, 'txPoolOveflowFactor must be greater or equal to 1');
317
- this.txPoolOverflowFactor = txPoolOverflowFactor;
318
- this.#log.info(`Allowing tx pool size to grow above limit`, {
319
- maxTxPoolSize,
320
- txPoolOverflowFactor
321
- });
328
+ return {
329
+ txHash,
330
+ blockHash: Fr.fromString(historicalBlockHash),
331
+ isEvictable: !this.#nonEvictableTxs.has(txHash.toString())
332
+ };
333
+ }
334
+ async getPendingTxsReferencingBlocks(blockHashes) {
335
+ const result = [];
336
+ for (const blockHash of blockHashes){
337
+ const chunk = await toArray(this.#historicalHeaderToTxHash.getValuesAsync(blockHash.toString()));
338
+ result.push(...chunk.map((txHash)=>({
339
+ txHash: TxHash.fromString(txHash),
340
+ blockHash,
341
+ isEvictable: !this.#nonEvictableTxs.has(txHash)
342
+ })));
322
343
  }
323
- if (typeof archivedTxLimit === 'number') {
324
- assert(archivedTxLimit >= 0, 'archivedTxLimit must be greater or equal to 0');
325
- this.#archivedTxLimit = archivedTxLimit;
344
+ return result;
345
+ }
346
+ async getPendingTxsWithFeePayer(feePayers) {
347
+ const result = [];
348
+ for (const feePayer of feePayers){
349
+ const chunk = await toArray(this.#feePayerToTxHash.getValuesAsync(feePayer.toString()));
350
+ const infos = await Promise.all(chunk.map((txHash)=>this.getPendingTxInfo(TxHash.fromString(txHash))));
351
+ result.push(...infos.filter((info)=>info !== undefined));
352
+ }
353
+ return result;
354
+ }
355
+ async getMinedTxHashes() {
356
+ const vals = await toArray(this.#minedTxHashToBlock.entriesAsync());
357
+ return vals.map(([txHash, blockNumber])=>[
358
+ TxHash.fromString(txHash),
359
+ blockNumber
360
+ ]);
361
+ }
362
+ async getPendingTxCount() {
363
+ return await this.#pendingTxPriorityToHash.sizeAsync() ?? 0;
364
+ }
365
+ async getMinedTxCount() {
366
+ return await this.#minedTxHashToBlock.sizeAsync() ?? 0;
367
+ }
368
+ async getTxStatus(txHash) {
369
+ const key = txHash.toString();
370
+ const [isMined, isKnown, isDeleted] = await Promise.all([
371
+ this.#minedTxHashToBlock.hasAsync(key),
372
+ this.#txs.hasAsync(key),
373
+ this.#deletedMinedTxHashes.hasAsync(key)
374
+ ]);
375
+ if (isDeleted) {
376
+ return 'deleted';
377
+ } else if (isMined) {
378
+ return 'mined';
379
+ } else if (isKnown) {
380
+ return 'pending';
381
+ } else {
382
+ return undefined;
383
+ }
384
+ }
385
+ updateConfig(cfg) {
386
+ if (typeof cfg.archivedTxLimit === 'number') {
387
+ assert(cfg.archivedTxLimit >= 0, 'archivedTxLimit must be greater or equal to 0');
388
+ this.#archivedTxLimit = cfg.archivedTxLimit;
389
+ }
390
+ if (this.#evictionManager) {
391
+ this.#evictionManager.updateConfig(cfg);
326
392
  }
327
393
  }
328
394
  markTxsAsNonEvictable(txHashes) {
329
395
  txHashes.forEach((txHash)=>this.#nonEvictableTxs.add(txHash.toString()));
330
396
  return Promise.resolve();
331
397
  }
398
+ clearNonEvictableTxs() {
399
+ // Clear the non-evictable set after completing the DB updates above.
400
+ // This ensures pinned (non-evictable) txs are protected while we mark mined txs,
401
+ // but they won't remain pinned indefinitely across blocks. Note that eviction rules
402
+ // (including post-mining invalidation) respect the non-evictable flag while it is set.
403
+ this.#nonEvictableTxs.clear();
404
+ return Promise.resolve();
405
+ }
332
406
  /**
333
- * Creates a GasTxValidator instance.
334
- * @param db - DB for the validator to use
335
- * @returns A GasTxValidator instance
336
- */ createGasTxValidator(db) {
337
- return new GasTxValidator(new DatabasePublicStateSource(db), ProtocolContractAddress.FeeJuice, GasFees.empty());
407
+ * Permanently deletes deleted mined transactions from blocks up to and including the specified block number.
408
+ * @param blockNumber - Block number threshold. Deleted mined txs from this block or earlier will be permanently deleted.
409
+ * @returns The number of transactions permanently deleted.
410
+ */ async cleanupDeletedMinedTxs(blockNumber) {
411
+ let deletedCount = 0;
412
+ await this.#store.transactionAsync(async ()=>{
413
+ const txHashesToDelete = [];
414
+ const blocksToDelete = [];
415
+ // Iterate through all entries and check block numbers
416
+ for await (const [block, txHash] of this.#blockToDeletedMinedTxHash.entriesAsync()){
417
+ if (block <= blockNumber) {
418
+ // Permanently delete the transaction
419
+ await this.#txs.delete(txHash);
420
+ await this.#deletedMinedTxHashes.delete(txHash);
421
+ txHashesToDelete.push(txHash);
422
+ if (!blocksToDelete.includes(block)) {
423
+ blocksToDelete.push(block);
424
+ }
425
+ deletedCount++;
426
+ }
427
+ }
428
+ this.#metrics.transactionsRemoved(txHashesToDelete);
429
+ // Clean up block-to-hash mapping - delete all values for each block
430
+ for (const block of blocksToDelete){
431
+ const txHashesForBlock = await toArray(this.#blockToDeletedMinedTxHash.getValuesAsync(block));
432
+ for (const txHash of txHashesForBlock){
433
+ await this.#blockToDeletedMinedTxHash.deleteValue(block, txHash);
434
+ }
435
+ }
436
+ });
437
+ if (deletedCount > 0) {
438
+ this.#log.debug(`Permanently deleted ${deletedCount} deleted mined txs from blocks up to ${blockNumber}`);
439
+ }
440
+ return deletedCount;
338
441
  }
339
442
  /**
340
443
  * Creates an ArchiveCache instance.
@@ -349,31 +452,27 @@ import { getPendingTxPriority } from './priority.js';
349
452
  * @param txHash - The generated tx hash.
350
453
  * @returns The transaction, if found, 'undefined' otherwise.
351
454
  */ async getPendingTxByHash(txHash) {
352
- let key;
353
455
  if (typeof txHash === 'string') {
354
- key = txHash;
355
456
  txHash = TxHash.fromString(txHash);
356
- } else {
357
- key = txHash.toString();
358
- }
359
- if (this.#pendingTxs.has(key)) {
360
- return this.#pendingTxs.get(key);
361
457
  }
362
458
  const tx = await this.getTxByHash(txHash);
363
459
  if (tx) {
364
- this.#pendingTxs.set(key, tx);
365
460
  return tx;
366
461
  }
367
462
  return undefined;
368
463
  }
369
464
  /**
370
465
  * Archives a list of txs for future reference. The number of archived txs is limited by the specified archivedTxLimit.
466
+ * Note: Pending txs should not be archived, only finalized txs
371
467
  * @param txs - The list of transactions to archive.
372
468
  * @returns Empty promise.
373
469
  */ async archiveTxs(txs) {
374
470
  if (txs.length === 0) {
375
471
  return;
376
472
  }
473
+ if (this.#archivedTxLimit === 0) {
474
+ return;
475
+ }
377
476
  try {
378
477
  const txHashes = await Promise.all(txs.map((tx)=>tx.getTxHash()));
379
478
  await this.#archive.transactionAsync(async ()=>{
@@ -395,7 +494,7 @@ import { getPendingTxPriority } from './priority.js';
395
494
  }
396
495
  tailIdx++;
397
496
  }
398
- const archivedTx = new Tx(tx.txHash, tx.data, ClientIvcProof.empty(), tx.contractClassLogFields, tx.publicFunctionCalldata);
497
+ const archivedTx = new Tx(tx.txHash, tx.data, ChonkProof.empty(), tx.contractClassLogFields, tx.publicFunctionCalldata);
399
498
  const txHash = txHashes[i].toString();
400
499
  await this.#archivedTxs.set(txHash, archivedTx.toBuffer());
401
500
  await this.#archivedTxIndices.set(headIdx, txHash);
@@ -412,147 +511,45 @@ import { getPendingTxPriority } from './priority.js';
412
511
  });
413
512
  }
414
513
  }
415
- /**
416
- * Evicts pending txs with the lowest priority fees from the pool to accomodate the max tx count and cumulative max tx size
417
- * after new txs are added.
418
- *
419
- * @param newTxHashes - The tx hashes of the new txs added to the pool.
420
- * @returns The total number of txs evicted from the pool and the number of new txs that were evicted.
421
- */ async evictLowPriorityTxs(newTxHashes) {
422
- if (this.#maxTxPoolSize === undefined || this.#maxTxPoolSize === 0) {
423
- return {
424
- numLowPriorityTxsEvicted: 0,
425
- numNewTxsEvicted: 0
426
- };
427
- }
428
- let numNewTxsEvicted = 0;
429
- const txsToEvict = [];
430
- let pendingTxsSize = await this.#pendingTxSize.getAsync() ?? 0;
431
- if (pendingTxsSize > this.#maxTxPoolSize * this.txPoolOverflowFactor) {
432
- for await (const txHash of this.#pendingTxPriorityToHash.valuesAsync()){
433
- if (this.#nonEvictableTxs.has(txHash.toString())) {
434
- continue;
435
- }
436
- const txSize = await this.#pendingTxHashToSize.getAsync(txHash.toString()) ?? (await this.getPendingTxByHash(txHash))?.getSize();
437
- this.#log.verbose(`Evicting tx ${txHash} from pool due to low priority to satisfy max tx size limit`, {
438
- txHash,
439
- txSize
440
- });
441
- txsToEvict.push(TxHash.fromString(txHash));
442
- if (txSize) {
443
- pendingTxsSize -= txSize;
444
- if (pendingTxsSize <= this.#maxTxPoolSize) {
445
- break;
446
- }
447
- }
448
- }
449
- numNewTxsEvicted += newTxHashes.filter((txHash)=>txsToEvict.includes(txHash)).length;
450
- }
451
- if (txsToEvict.length > 0) {
452
- await this.deleteTxs(txsToEvict, true);
453
- }
454
- return {
455
- numLowPriorityTxsEvicted: txsToEvict.length,
456
- numNewTxsEvicted
457
- };
514
+ // Assumes being called within a DB transaction
515
+ async addPendingTxIndicesInDbTx(tx, txHash) {
516
+ await this.#pendingTxPriorityToHash.set(getPendingTxPriority(tx), txHash);
517
+ await this.#historicalHeaderToTxHash.set((await tx.data.constants.anchorBlockHeader.hash()).toString(), txHash);
518
+ await this.#feePayerToTxHash.set(tx.data.feePayer.toString(), txHash);
458
519
  }
459
- /**
460
- * Evicts invalid pending txs from the pool after a txs from a block are mined.
461
- * Eviction criteria includes:
462
- * - txs with nullifiers that are already included in the mined block
463
- * - txs with an insufficient fee payer balance
464
- * - txs with an expiration timestamp lower than that of the mined block
465
- *
466
- * @param minedTxHashes - The tx hashes of the txs mined in the block.
467
- * @param blockHeader - The header of the mined block.
468
- * @returns The total number of txs evicted from the pool.
469
- */ async evictInvalidTxsAfterMining(minedTxHashes, blockHeader, minedNullifiers, minedFeePayers) {
470
- if (minedTxHashes.length === 0) {
471
- return 0;
472
- }
473
- const { blockNumber, timestamp } = blockHeader.globalVariables;
474
- // Wait for world state to be synced to at least the mined block number
475
- await this.#worldStateSynchronizer.syncImmediate(blockNumber);
476
- const db = this.#worldStateSynchronizer.getCommitted();
477
- const gasTxValidator = this.createGasTxValidator(db);
478
- const txsToEvict = [];
479
- for await (const txHash of this.#pendingTxPriorityToHash.valuesAsync()){
480
- const tx = await this.getPendingTxByHash(txHash);
481
- if (!tx) {
482
- continue;
483
- }
484
- // Evict pending txs that share nullifiers with mined txs
485
- const txNullifiers = tx.data.getNonEmptyNullifiers();
486
- if (txNullifiers.some((nullifier)=>minedNullifiers.has(nullifier.toString()))) {
487
- this.#log.verbose(`Evicting tx ${txHash} from pool due to a duplicate nullifier with a mined tx`);
488
- txsToEvict.push(TxHash.fromString(txHash));
489
- continue;
490
- }
491
- // Evict pending txs with an insufficient fee payer balance
492
- if (minedFeePayers.has(tx.data.feePayer.toString()) && (await gasTxValidator.validateTxFee(tx)).result === 'invalid') {
493
- this.#log.verbose(`Evicting tx ${txHash} from pool due to an insufficient fee payer balance`);
494
- txsToEvict.push(TxHash.fromString(txHash));
495
- continue;
496
- }
497
- // Evict pending txs with an expiration timestamp less than or equal to the mined block timestamp
498
- const includeByTimestamp = tx.data.includeByTimestamp;
499
- if (includeByTimestamp <= timestamp) {
500
- this.#log.verbose(`Evicting tx ${txHash} from pool due to the tx being expired (includeByTimestamp: ${includeByTimestamp}, mined block timestamp: ${timestamp})`);
501
- txsToEvict.push(TxHash.fromString(txHash));
502
- continue;
503
- }
504
- }
505
- if (txsToEvict.length > 0) {
506
- await this.deleteTxs(txsToEvict, true);
507
- }
508
- return txsToEvict.length;
520
+ async addPendingTxIndices(tx, txHash) {
521
+ return await this.#store.transactionAsync(async ()=>{
522
+ await this.addPendingTxIndicesInDbTx(tx, txHash);
523
+ });
524
+ }
525
+ // Assumes being called within a DB transaction
526
+ async removePendingTxIndicesInDbTx(tx, txHash) {
527
+ await this.#pendingTxPriorityToHash.deleteValue(getPendingTxPriority(tx), txHash);
528
+ await this.#historicalHeaderToTxHash.deleteValue((await tx.data.constants.anchorBlockHeader.hash()).toString(), txHash);
529
+ await this.#feePayerToTxHash.deleteValue(tx.data.feePayer.toString(), txHash);
530
+ }
531
+ async removePendingTxIndices(tx, txHash) {
532
+ return await this.#store.transactionAsync(async ()=>{
533
+ await this.removePendingTxIndicesInDbTx(tx, txHash);
534
+ });
509
535
  }
510
536
  /**
511
- * Evicts pending txs that no longer have valid archive roots or fee payer balances from the pool after a reorg.
512
- *
513
- * @param txHashes - The tx hashes of the txs that were moved from mined to pending.
514
- * @returns The total number of txs evicted from the pool.
515
- */ async evictInvalidTxsAfterReorg(txHashes) {
516
- if (txHashes.length === 0) {
517
- return 0;
518
- }
519
- await this.#worldStateSynchronizer.syncImmediate();
520
- const db = this.#worldStateSynchronizer.getCommitted();
521
- const archiveCache = this.createArchiveCache(db);
522
- const gasTxValidator = this.createGasTxValidator(db);
537
+ * Returns up to `limit` lowest-priority evictable pending tx hashes without hydrating transactions.
538
+ * Iterates the priority index in ascending order and skips non-evictable txs.
539
+ */ async getLowestPriorityEvictable(limit) {
523
540
  const txsToEvict = [];
524
- for await (const [txHash, headerHash] of this.#pendingTxHashToHeaderHash.entriesAsync()){
525
- const tx = await this.getPendingTxByHash(txHash);
526
- if (!tx) {
527
- continue;
528
- }
529
- const [index] = await archiveCache.getArchiveIndices([
530
- Fr.fromString(headerHash)
531
- ]);
532
- if (index === undefined) {
533
- this.#log.verbose(`Evicting tx ${txHash} from pool due to an invalid archive root`);
534
- txsToEvict.push(TxHash.fromString(txHash));
541
+ if (limit <= 0) {
542
+ return txsToEvict;
543
+ }
544
+ for await (const txHashStr of this.#pendingTxPriorityToHash.valuesAsync()){
545
+ if (this.#nonEvictableTxs.has(txHashStr)) {
535
546
  continue;
536
547
  }
537
- if ((await gasTxValidator.validateTxFee(tx)).result === 'invalid') {
538
- this.#log.verbose(`Evicting tx ${txHash} from pool due to an insufficient fee payer balance`);
539
- txsToEvict.push(TxHash.fromString(txHash));
548
+ txsToEvict.push(TxHash.fromString(txHashStr));
549
+ if (txsToEvict.length >= limit) {
550
+ break;
540
551
  }
541
552
  }
542
- if (txsToEvict.length > 0) {
543
- await this.deleteTxs(txsToEvict, true);
544
- }
545
- return txsToEvict.length;
546
- }
547
- async addPendingTxIndices(tx, txHash) {
548
- await this.#pendingTxPriorityToHash.set(getPendingTxPriority(tx), txHash);
549
- await this.#pendingTxHashToSize.set(txHash, tx.getSize());
550
- await this.#pendingTxHashToHeaderHash.set(txHash, (await tx.data.constants.historicalHeader.hash()).toString());
551
- }
552
- async removePendingTxIndices(tx, txHash) {
553
- await this.#pendingTxPriorityToHash.deleteValue(getPendingTxPriority(tx), txHash);
554
- await this.#pendingTxHashToSize.delete(txHash);
555
- await this.#pendingTxHashToHeaderHash.delete(txHash);
556
- this.#pendingTxs.delete(txHash);
553
+ return txsToEvict;
557
554
  }
558
555
  }