@aztec/p2p 0.0.0-test.1 → 0.0.1-commit.03f7ef2

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 (387) 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 +22 -9
  4. package/dest/client/factory.d.ts +15 -5
  5. package/dest/client/factory.d.ts.map +1 -1
  6. package/dest/client/factory.js +60 -25
  7. package/dest/client/index.d.ts +2 -1
  8. package/dest/client/index.d.ts.map +1 -1
  9. package/dest/client/index.js +1 -0
  10. package/dest/client/interface.d.ts +157 -0
  11. package/dest/client/interface.d.ts.map +1 -0
  12. package/dest/client/interface.js +9 -0
  13. package/dest/client/p2p_client.d.ts +75 -190
  14. package/dest/client/p2p_client.d.ts.map +1 -1
  15. package/dest/client/p2p_client.js +381 -183
  16. package/dest/config.d.ts +151 -125
  17. package/dest/config.d.ts.map +1 -1
  18. package/dest/config.js +183 -34
  19. package/dest/enr/generate-enr.d.ts +11 -3
  20. package/dest/enr/generate-enr.d.ts.map +1 -1
  21. package/dest/enr/generate-enr.js +27 -5
  22. package/dest/enr/index.d.ts +1 -1
  23. package/dest/errors/attestation-pool.error.d.ts +7 -0
  24. package/dest/errors/attestation-pool.error.d.ts.map +1 -0
  25. package/dest/errors/attestation-pool.error.js +12 -0
  26. package/dest/errors/reqresp.error.d.ts +1 -1
  27. package/dest/errors/reqresp.error.d.ts.map +1 -1
  28. package/dest/index.d.ts +4 -1
  29. package/dest/index.d.ts.map +1 -1
  30. package/dest/index.js +2 -0
  31. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +68 -8
  32. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  33. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +1 -1
  34. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  35. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +216 -65
  36. package/dest/mem_pools/attestation_pool/index.d.ts +1 -1
  37. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +21 -6
  38. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +1 -1
  39. package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +127 -26
  40. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +19 -6
  41. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +1 -1
  42. package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +111 -21
  43. package/dest/mem_pools/attestation_pool/mocks.d.ts +227 -7
  44. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  45. package/dest/mem_pools/attestation_pool/mocks.js +10 -16
  46. package/dest/mem_pools/index.d.ts +1 -1
  47. package/dest/mem_pools/instrumentation.d.ts +16 -12
  48. package/dest/mem_pools/instrumentation.d.ts.map +1 -1
  49. package/dest/mem_pools/instrumentation.js +57 -35
  50. package/dest/mem_pools/interface.d.ts +3 -4
  51. package/dest/mem_pools/interface.d.ts.map +1 -1
  52. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts +64 -14
  53. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts.map +1 -1
  54. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.js +472 -97
  55. package/dest/mem_pools/tx_pool/index.d.ts +1 -1
  56. package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts +36 -11
  57. package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts.map +1 -1
  58. package/dest/mem_pools/tx_pool/memory_tx_pool.js +137 -36
  59. package/dest/mem_pools/tx_pool/priority.d.ts +1 -1
  60. package/dest/mem_pools/tx_pool/priority.js +1 -1
  61. package/dest/mem_pools/tx_pool/tx_pool.d.ts +67 -10
  62. package/dest/mem_pools/tx_pool/tx_pool.d.ts.map +1 -1
  63. package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts +1 -1
  64. package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts.map +1 -1
  65. package/dest/mem_pools/tx_pool/tx_pool_test_suite.js +273 -42
  66. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +4 -2
  67. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
  68. package/dest/msg_validators/attestation_validator/attestation_validator.js +45 -9
  69. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +20 -0
  70. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -0
  71. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.js +67 -0
  72. package/dest/msg_validators/attestation_validator/index.d.ts +2 -1
  73. package/dest/msg_validators/attestation_validator/index.d.ts.map +1 -1
  74. package/dest/msg_validators/attestation_validator/index.js +1 -0
  75. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts +6 -2
  76. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts.map +1 -1
  77. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.js +73 -12
  78. package/dest/msg_validators/block_proposal_validator/index.d.ts +1 -1
  79. package/dest/msg_validators/index.d.ts +1 -1
  80. package/dest/msg_validators/msg_seen_validator/msg_seen_validator.d.ts +10 -0
  81. package/dest/msg_validators/msg_seen_validator/msg_seen_validator.d.ts.map +1 -0
  82. package/dest/msg_validators/msg_seen_validator/msg_seen_validator.js +36 -0
  83. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +1 -1
  84. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
  85. package/dest/msg_validators/tx_validator/allowed_public_setup.d.ts +3 -0
  86. package/dest/msg_validators/tx_validator/allowed_public_setup.d.ts.map +1 -0
  87. package/dest/msg_validators/tx_validator/allowed_public_setup.js +27 -0
  88. package/dest/msg_validators/tx_validator/archive_cache.d.ts +14 -0
  89. package/dest/msg_validators/tx_validator/archive_cache.d.ts.map +1 -0
  90. package/dest/msg_validators/tx_validator/archive_cache.js +22 -0
  91. package/dest/msg_validators/tx_validator/block_header_validator.d.ts +2 -2
  92. package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
  93. package/dest/msg_validators/tx_validator/block_header_validator.js +4 -4
  94. package/dest/msg_validators/tx_validator/data_validator.d.ts +1 -1
  95. package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
  96. package/dest/msg_validators/tx_validator/data_validator.js +56 -86
  97. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts +1 -3
  98. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts.map +1 -1
  99. package/dest/msg_validators/tx_validator/double_spend_validator.js +21 -27
  100. package/dest/msg_validators/tx_validator/factory.d.ts +16 -0
  101. package/dest/msg_validators/tx_validator/factory.d.ts.map +1 -0
  102. package/dest/msg_validators/tx_validator/factory.js +74 -0
  103. package/dest/msg_validators/tx_validator/gas_validator.d.ts +11 -0
  104. package/dest/msg_validators/tx_validator/gas_validator.d.ts.map +1 -0
  105. package/dest/msg_validators/tx_validator/gas_validator.js +115 -0
  106. package/dest/msg_validators/tx_validator/index.d.ts +8 -1
  107. package/dest/msg_validators/tx_validator/index.d.ts.map +1 -1
  108. package/dest/msg_validators/tx_validator/index.js +7 -0
  109. package/dest/msg_validators/tx_validator/metadata_validator.d.ts +9 -5
  110. package/dest/msg_validators/tx_validator/metadata_validator.d.ts.map +1 -1
  111. package/dest/msg_validators/tx_validator/metadata_validator.js +39 -20
  112. package/dest/msg_validators/tx_validator/phases_validator.d.ts +14 -0
  113. package/dest/msg_validators/tx_validator/phases_validator.d.ts.map +1 -0
  114. package/dest/msg_validators/tx_validator/phases_validator.js +93 -0
  115. package/dest/msg_validators/tx_validator/test_utils.d.ts +17 -0
  116. package/dest/msg_validators/tx_validator/test_utils.d.ts.map +1 -0
  117. package/dest/msg_validators/tx_validator/test_utils.js +22 -0
  118. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +13 -0
  119. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -0
  120. package/dest/msg_validators/tx_validator/timestamp_validator.js +32 -0
  121. package/dest/msg_validators/tx_validator/tx_permitted_validator.d.ts +8 -0
  122. package/dest/msg_validators/tx_validator/tx_permitted_validator.d.ts.map +1 -0
  123. package/dest/msg_validators/tx_validator/tx_permitted_validator.js +24 -0
  124. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts +1 -1
  125. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts.map +1 -1
  126. package/dest/msg_validators/tx_validator/tx_proof_validator.js +6 -5
  127. package/dest/services/data_store.d.ts +1 -1
  128. package/dest/services/data_store.d.ts.map +1 -1
  129. package/dest/services/discv5/discV5_service.d.ts +10 -9
  130. package/dest/services/discv5/discV5_service.d.ts.map +1 -1
  131. package/dest/services/discv5/discV5_service.js +63 -36
  132. package/dest/services/dummy_service.d.ts +50 -11
  133. package/dest/services/dummy_service.d.ts.map +1 -1
  134. package/dest/services/dummy_service.js +88 -5
  135. package/dest/services/encoding.d.ts +26 -7
  136. package/dest/services/encoding.d.ts.map +1 -1
  137. package/dest/services/encoding.js +74 -6
  138. package/dest/services/gossipsub/scoring.d.ts +1 -1
  139. package/dest/services/index.d.ts +5 -1
  140. package/dest/services/index.d.ts.map +1 -1
  141. package/dest/services/index.js +4 -0
  142. package/dest/services/libp2p/instrumentation.d.ts +20 -0
  143. package/dest/services/libp2p/instrumentation.d.ts.map +1 -0
  144. package/dest/services/libp2p/instrumentation.js +164 -0
  145. package/dest/services/libp2p/libp2p_service.d.ts +78 -89
  146. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  147. package/dest/services/libp2p/libp2p_service.js +695 -248
  148. package/dest/services/peer-manager/interface.d.ts +23 -0
  149. package/dest/services/peer-manager/interface.d.ts.map +1 -0
  150. package/dest/services/peer-manager/interface.js +1 -0
  151. package/dest/services/peer-manager/metrics.d.ts +6 -2
  152. package/dest/services/peer-manager/metrics.d.ts.map +1 -1
  153. package/dest/services/peer-manager/metrics.js +22 -2
  154. package/dest/services/peer-manager/peer_manager.d.ts +102 -22
  155. package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
  156. package/dest/services/peer-manager/peer_manager.js +549 -72
  157. package/dest/services/peer-manager/peer_scoring.d.ts +7 -2
  158. package/dest/services/peer-manager/peer_scoring.d.ts.map +1 -1
  159. package/dest/services/peer-manager/peer_scoring.js +40 -2
  160. package/dest/services/reqresp/config.d.ts +11 -9
  161. package/dest/services/reqresp/config.d.ts.map +1 -1
  162. package/dest/services/reqresp/config.js +18 -4
  163. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts +2 -2
  164. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts.map +1 -1
  165. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.js +10 -6
  166. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts +31 -17
  167. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts.map +1 -1
  168. package/dest/services/reqresp/connection-sampler/connection_sampler.js +142 -84
  169. package/dest/services/reqresp/index.d.ts +3 -2
  170. package/dest/services/reqresp/index.d.ts.map +1 -1
  171. package/dest/services/reqresp/index.js +2 -1
  172. package/dest/services/reqresp/interface.d.ts +73 -24
  173. package/dest/services/reqresp/interface.d.ts.map +1 -1
  174. package/dest/services/reqresp/interface.js +46 -27
  175. package/dest/services/reqresp/metrics.d.ts +1 -1
  176. package/dest/services/reqresp/metrics.d.ts.map +1 -1
  177. package/dest/services/reqresp/protocols/auth.d.ts +43 -0
  178. package/dest/services/reqresp/protocols/auth.d.ts.map +1 -0
  179. package/dest/services/reqresp/protocols/auth.js +71 -0
  180. package/dest/services/reqresp/protocols/block.d.ts +6 -1
  181. package/dest/services/reqresp/protocols/block.d.ts.map +1 -1
  182. package/dest/services/reqresp/protocols/block.js +30 -6
  183. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts +30 -0
  184. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts.map +1 -0
  185. package/dest/services/reqresp/protocols/block_txs/bitvector.js +75 -0
  186. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +11 -0
  187. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -0
  188. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.js +39 -0
  189. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +47 -0
  190. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -0
  191. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +75 -0
  192. package/dest/services/reqresp/protocols/block_txs/index.d.ts +4 -0
  193. package/dest/services/reqresp/protocols/block_txs/index.d.ts.map +1 -0
  194. package/dest/services/reqresp/protocols/block_txs/index.js +3 -0
  195. package/dest/services/reqresp/protocols/goodbye.d.ts +3 -5
  196. package/dest/services/reqresp/protocols/goodbye.d.ts.map +1 -1
  197. package/dest/services/reqresp/protocols/goodbye.js +7 -7
  198. package/dest/services/reqresp/protocols/index.d.ts +3 -1
  199. package/dest/services/reqresp/protocols/index.d.ts.map +1 -1
  200. package/dest/services/reqresp/protocols/index.js +2 -0
  201. package/dest/services/reqresp/protocols/ping.d.ts +1 -3
  202. package/dest/services/reqresp/protocols/ping.d.ts.map +1 -1
  203. package/dest/services/reqresp/protocols/status.d.ts +40 -7
  204. package/dest/services/reqresp/protocols/status.d.ts.map +1 -1
  205. package/dest/services/reqresp/protocols/status.js +73 -5
  206. package/dest/services/reqresp/protocols/tx.d.ts +14 -4
  207. package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
  208. package/dest/services/reqresp/protocols/tx.js +34 -6
  209. package/dest/services/reqresp/rate-limiter/index.d.ts +1 -1
  210. package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts +6 -4
  211. package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts.map +1 -1
  212. package/dest/services/reqresp/rate-limiter/rate_limiter.js +10 -2
  213. package/dest/services/reqresp/rate-limiter/rate_limits.d.ts +1 -1
  214. package/dest/services/reqresp/rate-limiter/rate_limits.d.ts.map +1 -1
  215. package/dest/services/reqresp/rate-limiter/rate_limits.js +21 -1
  216. package/dest/services/reqresp/reqresp.d.ts +24 -66
  217. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  218. package/dest/services/reqresp/reqresp.js +298 -207
  219. package/dest/services/reqresp/status.d.ts +10 -4
  220. package/dest/services/reqresp/status.d.ts.map +1 -1
  221. package/dest/services/reqresp/status.js +9 -2
  222. package/dest/services/service.d.ts +23 -19
  223. package/dest/services/service.d.ts.map +1 -1
  224. package/dest/services/tx_collection/config.d.ts +25 -0
  225. package/dest/services/tx_collection/config.d.ts.map +1 -0
  226. package/dest/services/tx_collection/config.js +58 -0
  227. package/dest/services/tx_collection/fast_tx_collection.d.ts +51 -0
  228. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -0
  229. package/dest/services/tx_collection/fast_tx_collection.js +300 -0
  230. package/dest/services/tx_collection/index.d.ts +3 -0
  231. package/dest/services/tx_collection/index.d.ts.map +1 -0
  232. package/dest/services/tx_collection/index.js +2 -0
  233. package/dest/services/tx_collection/instrumentation.d.ts +10 -0
  234. package/dest/services/tx_collection/instrumentation.d.ts.map +1 -0
  235. package/dest/services/tx_collection/instrumentation.js +34 -0
  236. package/dest/services/tx_collection/slow_tx_collection.d.ts +53 -0
  237. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -0
  238. package/dest/services/tx_collection/slow_tx_collection.js +177 -0
  239. package/dest/services/tx_collection/tx_collection.d.ts +110 -0
  240. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -0
  241. package/dest/services/tx_collection/tx_collection.js +128 -0
  242. package/dest/services/tx_collection/tx_collection_sink.d.ts +30 -0
  243. package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -0
  244. package/dest/services/tx_collection/tx_collection_sink.js +111 -0
  245. package/dest/services/tx_collection/tx_source.d.ts +18 -0
  246. package/dest/services/tx_collection/tx_source.d.ts.map +1 -0
  247. package/dest/services/tx_collection/tx_source.js +31 -0
  248. package/dest/services/tx_provider.d.ts +51 -0
  249. package/dest/services/tx_provider.d.ts.map +1 -0
  250. package/dest/services/tx_provider.js +217 -0
  251. package/dest/services/tx_provider_instrumentation.d.ts +16 -0
  252. package/dest/services/tx_provider_instrumentation.d.ts.map +1 -0
  253. package/dest/services/tx_provider_instrumentation.js +47 -0
  254. package/dest/test-helpers/generate-peer-id-private-keys.d.ts +1 -1
  255. package/dest/test-helpers/get-ports.d.ts +1 -1
  256. package/dest/test-helpers/get-ports.d.ts.map +1 -1
  257. package/dest/test-helpers/index.d.ts +2 -1
  258. package/dest/test-helpers/index.d.ts.map +1 -1
  259. package/dest/test-helpers/index.js +1 -0
  260. package/dest/test-helpers/make-enrs.d.ts +1 -1
  261. package/dest/test-helpers/make-enrs.d.ts.map +1 -1
  262. package/dest/test-helpers/make-enrs.js +4 -5
  263. package/dest/test-helpers/make-test-p2p-clients.d.ts +33 -5
  264. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  265. package/dest/test-helpers/make-test-p2p-clients.js +86 -16
  266. package/dest/test-helpers/mock-pubsub.d.ts +59 -0
  267. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -0
  268. package/dest/test-helpers/mock-pubsub.js +130 -0
  269. package/dest/test-helpers/mock-tx-helpers.d.ts +12 -0
  270. package/dest/test-helpers/mock-tx-helpers.d.ts.map +1 -0
  271. package/dest/test-helpers/mock-tx-helpers.js +19 -0
  272. package/dest/test-helpers/reqresp-nodes.d.ts +15 -11
  273. package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
  274. package/dest/test-helpers/reqresp-nodes.js +62 -28
  275. package/dest/testbench/p2p_client_testbench_worker.d.ts +1 -1
  276. package/dest/testbench/p2p_client_testbench_worker.js +103 -29
  277. package/dest/testbench/parse_log_file.d.ts +1 -1
  278. package/dest/testbench/parse_log_file.js +4 -4
  279. package/dest/testbench/testbench.d.ts +1 -1
  280. package/dest/testbench/testbench.js +4 -4
  281. package/dest/testbench/worker_client_manager.d.ts +1 -6
  282. package/dest/testbench/worker_client_manager.d.ts.map +1 -1
  283. package/dest/testbench/worker_client_manager.js +11 -19
  284. package/dest/types/index.d.ts +4 -2
  285. package/dest/types/index.d.ts.map +1 -1
  286. package/dest/types/index.js +2 -0
  287. package/dest/util.d.ts +24 -16
  288. package/dest/util.d.ts.map +1 -1
  289. package/dest/util.js +75 -69
  290. package/dest/versioning.d.ts +4 -4
  291. package/dest/versioning.d.ts.map +1 -1
  292. package/dest/versioning.js +8 -3
  293. package/package.json +32 -27
  294. package/src/bootstrap/bootstrap.ts +27 -11
  295. package/src/client/factory.ts +139 -53
  296. package/src/client/index.ts +1 -0
  297. package/src/client/interface.ts +198 -0
  298. package/src/client/p2p_client.ts +484 -348
  299. package/src/config.ts +305 -134
  300. package/src/enr/generate-enr.ts +39 -6
  301. package/src/errors/attestation-pool.error.ts +13 -0
  302. package/src/index.ts +4 -0
  303. package/src/mem_pools/attestation_pool/attestation_pool.ts +75 -7
  304. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +266 -67
  305. package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +174 -35
  306. package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +156 -30
  307. package/src/mem_pools/attestation_pool/mocks.ts +13 -12
  308. package/src/mem_pools/instrumentation.ts +70 -40
  309. package/src/mem_pools/interface.ts +2 -4
  310. package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +555 -110
  311. package/src/mem_pools/tx_pool/memory_tx_pool.ts +160 -46
  312. package/src/mem_pools/tx_pool/priority.ts +1 -1
  313. package/src/mem_pools/tx_pool/tx_pool.ts +69 -9
  314. package/src/mem_pools/tx_pool/tx_pool_test_suite.ts +224 -35
  315. package/src/msg_validators/attestation_validator/attestation_validator.ts +54 -11
  316. package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +91 -0
  317. package/src/msg_validators/attestation_validator/index.ts +1 -0
  318. package/src/msg_validators/block_proposal_validator/block_proposal_validator.ts +82 -14
  319. package/src/msg_validators/msg_seen_validator/msg_seen_validator.ts +36 -0
  320. package/src/msg_validators/tx_validator/allowed_public_setup.ts +35 -0
  321. package/src/msg_validators/tx_validator/archive_cache.ts +28 -0
  322. package/src/msg_validators/tx_validator/block_header_validator.ts +5 -5
  323. package/src/msg_validators/tx_validator/data_validator.ts +81 -69
  324. package/src/msg_validators/tx_validator/double_spend_validator.ts +19 -17
  325. package/src/msg_validators/tx_validator/factory.ts +110 -0
  326. package/src/msg_validators/tx_validator/gas_validator.ts +134 -0
  327. package/src/msg_validators/tx_validator/index.ts +7 -0
  328. package/src/msg_validators/tx_validator/metadata_validator.ts +59 -22
  329. package/src/msg_validators/tx_validator/phases_validator.ts +116 -0
  330. package/src/msg_validators/tx_validator/test_utils.ts +43 -0
  331. package/src/msg_validators/tx_validator/timestamp_validator.ts +47 -0
  332. package/src/msg_validators/tx_validator/tx_permitted_validator.ts +17 -0
  333. package/src/msg_validators/tx_validator/tx_proof_validator.ts +6 -5
  334. package/src/services/discv5/discV5_service.ts +84 -38
  335. package/src/services/dummy_service.ts +147 -9
  336. package/src/services/encoding.ts +81 -6
  337. package/src/services/index.ts +4 -0
  338. package/src/services/libp2p/instrumentation.ts +167 -0
  339. package/src/services/libp2p/libp2p_service.ts +865 -298
  340. package/src/services/peer-manager/interface.ts +29 -0
  341. package/src/services/peer-manager/metrics.ts +26 -1
  342. package/src/services/peer-manager/peer_manager.ts +654 -78
  343. package/src/services/peer-manager/peer_scoring.ts +46 -3
  344. package/src/services/reqresp/config.ts +26 -9
  345. package/src/services/reqresp/connection-sampler/batch_connection_sampler.ts +12 -6
  346. package/src/services/reqresp/connection-sampler/connection_sampler.ts +148 -95
  347. package/src/services/reqresp/index.ts +2 -0
  348. package/src/services/reqresp/interface.ts +92 -37
  349. package/src/services/reqresp/metrics.ts +4 -1
  350. package/src/services/reqresp/protocols/auth.ts +83 -0
  351. package/src/services/reqresp/protocols/block.ts +26 -4
  352. package/src/services/reqresp/protocols/block_txs/bitvector.ts +90 -0
  353. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +53 -0
  354. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +79 -0
  355. package/src/services/reqresp/protocols/block_txs/index.ts +3 -0
  356. package/src/services/reqresp/protocols/goodbye.ts +9 -7
  357. package/src/services/reqresp/protocols/index.ts +2 -0
  358. package/src/services/reqresp/protocols/status.ts +118 -5
  359. package/src/services/reqresp/protocols/tx.ts +36 -8
  360. package/src/services/reqresp/rate-limiter/rate_limiter.ts +12 -3
  361. package/src/services/reqresp/rate-limiter/rate_limits.ts +21 -1
  362. package/src/services/reqresp/reqresp.ts +387 -256
  363. package/src/services/reqresp/status.ts +12 -3
  364. package/src/services/service.ts +45 -21
  365. package/src/services/tx_collection/config.ts +84 -0
  366. package/src/services/tx_collection/fast_tx_collection.ts +341 -0
  367. package/src/services/tx_collection/index.ts +2 -0
  368. package/src/services/tx_collection/instrumentation.ts +43 -0
  369. package/src/services/tx_collection/slow_tx_collection.ts +233 -0
  370. package/src/services/tx_collection/tx_collection.ts +216 -0
  371. package/src/services/tx_collection/tx_collection_sink.ts +129 -0
  372. package/src/services/tx_collection/tx_source.ts +37 -0
  373. package/src/services/tx_provider.ts +229 -0
  374. package/src/services/tx_provider_instrumentation.ts +61 -0
  375. package/src/test-helpers/index.ts +1 -0
  376. package/src/test-helpers/make-enrs.ts +4 -5
  377. package/src/test-helpers/make-test-p2p-clients.ts +111 -21
  378. package/src/test-helpers/mock-pubsub.ts +188 -0
  379. package/src/test-helpers/mock-tx-helpers.ts +24 -0
  380. package/src/test-helpers/reqresp-nodes.ts +87 -36
  381. package/src/testbench/p2p_client_testbench_worker.ts +151 -25
  382. package/src/testbench/parse_log_file.ts +4 -4
  383. package/src/testbench/testbench.ts +4 -4
  384. package/src/testbench/worker_client_manager.ts +17 -23
  385. package/src/types/index.ts +2 -0
  386. package/src/util.ts +105 -91
  387. package/src/versioning.ts +11 -4
@@ -1,30 +1,69 @@
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
1
3
  import { toArray } from '@aztec/foundation/iterable';
2
4
  import { type Logger, createLogger } from '@aztec/foundation/log';
3
- import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncMultiMap } from '@aztec/kv-store';
4
- import { ClientIvcProof } from '@aztec/stdlib/proofs';
5
+ import type { TypedEventEmitter } from '@aztec/foundation/types';
6
+ import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncMultiMap, AztecAsyncSingleton } from '@aztec/kv-store';
7
+ import { ProtocolContractAddress } from '@aztec/protocol-contracts';
8
+ import { GasFees } from '@aztec/stdlib/gas';
9
+ import type { MerkleTreeReadOperations, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
10
+ import { ChonkProof } from '@aztec/stdlib/proofs';
5
11
  import type { TxAddedToPoolStats } from '@aztec/stdlib/stats';
6
- import { Tx, TxHash } from '@aztec/stdlib/tx';
12
+ import { DatabasePublicStateSource } from '@aztec/stdlib/trees';
13
+ import { BlockHeader, Tx, TxHash } from '@aztec/stdlib/tx';
7
14
  import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
8
15
 
9
- import { PoolInstrumentation, PoolName } from '../instrumentation.js';
16
+ import assert from 'assert';
17
+ import EventEmitter from 'node:events';
18
+
19
+ import { ArchiveCache } from '../../msg_validators/tx_validator/archive_cache.js';
20
+ import { GasTxValidator } from '../../msg_validators/tx_validator/gas_validator.js';
21
+ import { PoolInstrumentation, PoolName, type PoolStatsCallback } from '../instrumentation.js';
10
22
  import { getPendingTxPriority } from './priority.js';
11
- import type { TxPool } from './tx_pool.js';
23
+ import type { TxPool, TxPoolEvents, TxPoolOptions } from './tx_pool.js';
12
24
 
13
25
  /**
14
26
  * KV implementation of the Transaction Pool.
15
27
  */
16
- export class AztecKVTxPool implements TxPool {
28
+ export class AztecKVTxPool extends (EventEmitter as new () => TypedEventEmitter<TxPoolEvents>) implements TxPool {
17
29
  #store: AztecAsyncKVStore;
18
30
 
19
31
  /** Our tx pool, stored as a Map, with K: tx hash and V: the transaction. */
20
32
  #txs: AztecAsyncMap<string, Buffer>;
21
33
 
34
+ /** The maximum cumulative tx size that the pending txs in the pool take up. */
35
+ #maxTxPoolSize: number = 0;
36
+
37
+ /** The tx evicion logic will kick after pool size is greater than maxTxPoolSize * txPoolOverflowFactor */
38
+ txPoolOverflowFactor: number = 1;
39
+
22
40
  /** Index from tx hash to the block number in which they were mined, filtered by mined txs. */
23
- #minedTxHashToBlock: AztecAsyncMap<string, number>;
41
+ #minedTxHashToBlock: AztecAsyncMap<string, BlockNumber>;
24
42
 
25
43
  /** Index from tx priority (stored as hex) to its tx hash, filtered by pending txs. */
26
44
  #pendingTxPriorityToHash: AztecAsyncMultiMap<string, string>;
27
45
 
46
+ /** Index from tx hash to its tx size (in bytes), filtered by pending txs. */
47
+ #pendingTxHashToSize: AztecAsyncMap<string, number>;
48
+
49
+ /** Index from tx hash to its header hash, filtered by pending txs. */
50
+ #pendingTxHashToHeaderHash: AztecAsyncMap<string, string>;
51
+
52
+ /** Map from tx hash to the block number it was originally mined in (for soft-deleted txs). */
53
+ #deletedMinedTxHashes: AztecAsyncMap<string, BlockNumber>;
54
+
55
+ /** MultiMap from block number to deleted mined tx hashes for efficient cleanup. */
56
+ #blockToDeletedMinedTxHash: AztecAsyncMultiMap<BlockNumber, string>;
57
+
58
+ /** The cumulative tx size in bytes that the pending txs in the pool take up. */
59
+ #pendingTxSize: AztecAsyncSingleton<number>;
60
+
61
+ /** In-memory mapping of pending tx hashes to the hydrated pending tx in the pool. */
62
+ #pendingTxs: Map<string, Tx>;
63
+
64
+ /** In-memory set of txs that should not be evicted from the pool. */
65
+ #nonEvictableTxs: Set<string>;
66
+
28
67
  /** KV store for archived txs. */
29
68
  #archive: AztecAsyncKVStore;
30
69
 
@@ -35,7 +74,10 @@ export class AztecKVTxPool implements TxPool {
35
74
  #archivedTxIndices: AztecAsyncMap<number, string>;
36
75
 
37
76
  /** Number of txs to archive. */
38
- #archivedTxLimit: number;
77
+ #archivedTxLimit: number = 0;
78
+
79
+ /** The world state synchronizer used in the node. */
80
+ #worldStateSynchronizer: WorldStateSynchronizer;
39
81
 
40
82
  #log: Logger;
41
83
 
@@ -52,85 +94,160 @@ export class AztecKVTxPool implements TxPool {
52
94
  constructor(
53
95
  store: AztecAsyncKVStore,
54
96
  archive: AztecAsyncKVStore,
97
+ worldStateSynchronizer: WorldStateSynchronizer,
55
98
  telemetry: TelemetryClient = getTelemetryClient(),
56
- archivedTxLimit: number = 0,
99
+ config: TxPoolOptions = {},
57
100
  log = createLogger('p2p:tx_pool'),
58
101
  ) {
102
+ super();
103
+
104
+ this.#log = log;
105
+ this.updateConfig(config);
106
+
59
107
  this.#txs = store.openMap('txs');
60
108
  this.#minedTxHashToBlock = store.openMap('txHashToBlockMined');
61
109
  this.#pendingTxPriorityToHash = store.openMultiMap('pendingTxFeeToHash');
110
+ this.#pendingTxHashToSize = store.openMap('pendingTxHashToSize');
111
+ this.#pendingTxHashToHeaderHash = store.openMap('pendingTxHashToHeaderHash');
112
+ this.#pendingTxSize = store.openSingleton('pendingTxSize');
113
+ this.#deletedMinedTxHashes = store.openMap('deletedMinedTxHashes');
114
+ this.#blockToDeletedMinedTxHash = store.openMultiMap('blockToDeletedMinedTxHash');
115
+
116
+ this.#pendingTxs = new Map<string, Tx>();
117
+ this.#nonEvictableTxs = new Set<string>();
62
118
 
63
119
  this.#archivedTxs = archive.openMap('archivedTxs');
64
120
  this.#archivedTxIndices = archive.openMap('archivedTxIndices');
65
- this.#archivedTxLimit = archivedTxLimit;
66
121
 
67
122
  this.#store = store;
68
123
  this.#archive = archive;
69
- this.#log = log;
70
- this.#metrics = new PoolInstrumentation(telemetry, PoolName.TX_POOL, () => store.estimateSize());
124
+ this.#worldStateSynchronizer = worldStateSynchronizer;
125
+ this.#metrics = new PoolInstrumentation(telemetry, PoolName.TX_POOL, this.countTxs, () => store.estimateSize());
71
126
  }
72
127
 
73
- public markAsMined(txHashes: TxHash[], blockNumber: number): Promise<void> {
128
+ private countTxs: PoolStatsCallback = async () => {
129
+ const [pending = 0, mined = 0] = await Promise.all([this.getPendingTxCount(), this.getMinedTxCount()]);
130
+
131
+ return Promise.resolve({
132
+ itemCount: {
133
+ pending,
134
+ mined,
135
+ },
136
+ });
137
+ };
138
+
139
+ public async isEmpty(): Promise<boolean> {
140
+ for await (const _ of this.#txs.entriesAsync()) {
141
+ return false;
142
+ }
143
+ return true;
144
+ }
145
+ /**
146
+ * Marks transactions as mined in a block and updates the pool state accordingly.
147
+ * Removes the transactions from the pending set and adds them to the mined set.
148
+ * Also evicts any transactions that become invalid after the block is mined.
149
+ * @param txHashes - Array of transaction hashes that were mined
150
+ * @param blockHeader - The header of the block the transactions were mined in
151
+ */
152
+ public async markAsMined(txHashes: TxHash[], blockHeader: BlockHeader): Promise<void> {
74
153
  if (txHashes.length === 0) {
75
154
  return Promise.resolve();
76
155
  }
77
156
 
78
- let deletedPending = 0;
79
- return this.#store.transactionAsync(async () => {
157
+ const minedNullifiers = new Set<string>();
158
+ const minedFeePayers = new Set<string>();
159
+
160
+ await this.#store.transactionAsync(async () => {
161
+ let pendingTxSize = (await this.#pendingTxSize.getAsync()) ?? 0;
80
162
  for (const hash of txHashes) {
81
163
  const key = hash.toString();
82
- await this.#minedTxHashToBlock.set(key, blockNumber);
83
164
 
84
- const tx = await this.getTxByHash(hash);
165
+ // If this tx was previously soft-deleted, remove it from the deleted sets
166
+ if (await this.#deletedMinedTxHashes.hasAsync(key)) {
167
+ const originalBlock = await this.#deletedMinedTxHashes.getAsync(key);
168
+ await this.#deletedMinedTxHashes.delete(key);
169
+ // Remove from block-to-hash mapping
170
+ if (originalBlock !== undefined) {
171
+ await this.#blockToDeletedMinedTxHash.deleteValue(originalBlock, key);
172
+ }
173
+ }
174
+
175
+ await this.#minedTxHashToBlock.set(key, blockHeader.globalVariables.blockNumber);
176
+
177
+ const tx = await this.getPendingTxByHash(hash);
85
178
  if (tx) {
86
- deletedPending++;
87
- const fee = getPendingTxPriority(tx);
88
- await this.#pendingTxPriorityToHash.deleteValue(fee, key);
179
+ const nullifiers = tx.data.getNonEmptyNullifiers();
180
+ nullifiers.forEach(nullifier => minedNullifiers.add(nullifier.toString()));
181
+ minedFeePayers.add(tx.data.feePayer.toString());
182
+ pendingTxSize -= tx.getSize();
183
+ await this.removePendingTxIndices(tx, key);
89
184
  }
90
185
  }
91
- this.#metrics.recordAddedObjects(txHashes.length, 'mined');
92
- this.#metrics.recordRemovedObjects(deletedPending, 'pending');
186
+ await this.#pendingTxSize.set(pendingTxSize);
187
+
188
+ await this.evictInvalidTxsAfterMining(txHashes, blockHeader, minedNullifiers, minedFeePayers);
93
189
  });
190
+ this.#metrics.transactionsRemoved(txHashes);
191
+ // We update this after the transaction above. This ensures that the non-evictable transactions are not evicted
192
+ // until any that have been mined are marked as such.
193
+ // The non-evictable set is not considered when evicting transactions that are invalid after a block is mined.
194
+ this.#nonEvictableTxs.clear();
94
195
  }
95
196
 
96
- public markMinedAsPending(txHashes: TxHash[]): Promise<void> {
197
+ public async markMinedAsPending(txHashes: TxHash[]): Promise<void> {
97
198
  if (txHashes.length === 0) {
98
199
  return Promise.resolve();
99
200
  }
100
-
101
- let markedAsPending = 0;
102
- return this.#store.transactionAsync(async () => {
201
+ await this.#store.transactionAsync(async () => {
202
+ let pendingTxSize = (await this.#pendingTxSize.getAsync()) ?? 0;
103
203
  for (const hash of txHashes) {
104
204
  const key = hash.toString();
105
205
  await this.#minedTxHashToBlock.delete(key);
106
206
 
107
- const tx = await this.getTxByHash(hash);
207
+ // Rehydrate the tx in the in-memory pending txs mapping
208
+ const tx = await this.getPendingTxByHash(hash);
108
209
  if (tx) {
109
- await this.#pendingTxPriorityToHash.set(getPendingTxPriority(tx), key);
110
- markedAsPending++;
210
+ await this.addPendingTxIndices(tx, key);
211
+ pendingTxSize += tx.getSize();
111
212
  }
112
213
  }
113
214
 
114
- this.#metrics.recordAddedObjects(markedAsPending, 'pending');
115
- this.#metrics.recordRemovedObjects(markedAsPending, 'mined');
215
+ await this.#pendingTxSize.set(pendingTxSize);
116
216
  });
217
+
218
+ await this.evictInvalidTxsAfterReorg(txHashes);
219
+ await this.evictLowPriorityTxs(txHashes);
117
220
  }
118
221
 
119
222
  public async getPendingTxHashes(): Promise<TxHash[]> {
120
223
  const vals = await toArray(this.#pendingTxPriorityToHash.valuesAsync({ reverse: true }));
121
- return vals.map(x => TxHash.fromString(x));
224
+ return vals.map(TxHash.fromString);
122
225
  }
123
226
 
124
- public async getMinedTxHashes(): Promise<[TxHash, number][]> {
227
+ public async getMinedTxHashes(): Promise<[TxHash, BlockNumber][]> {
125
228
  const vals = await toArray(this.#minedTxHashToBlock.entriesAsync());
126
229
  return vals.map(([txHash, blockNumber]) => [TxHash.fromString(txHash), blockNumber]);
127
230
  }
128
231
 
129
- public async getTxStatus(txHash: TxHash): Promise<'pending' | 'mined' | undefined> {
130
- const key = txHash.toString();
131
- const [isMined, isKnown] = await Promise.all([this.#minedTxHashToBlock.hasAsync(key), this.#txs.hasAsync(key)]);
232
+ public async getPendingTxCount(): Promise<number> {
233
+ return (await this.#pendingTxHashToHeaderHash.sizeAsync()) ?? 0;
234
+ }
132
235
 
133
- if (isMined) {
236
+ public async getMinedTxCount(): Promise<number> {
237
+ return (await this.#minedTxHashToBlock.sizeAsync()) ?? 0;
238
+ }
239
+
240
+ public async getTxStatus(txHash: TxHash): Promise<'pending' | 'mined' | 'deleted' | undefined> {
241
+ const key = txHash.toString();
242
+ const [isMined, isKnown, isDeleted] = await Promise.all([
243
+ this.#minedTxHashToBlock.hasAsync(key),
244
+ this.#txs.hasAsync(key),
245
+ this.#deletedMinedTxHashes.hasAsync(key),
246
+ ]);
247
+
248
+ if (isDeleted) {
249
+ return 'deleted';
250
+ } else if (isMined) {
134
251
  return 'mined';
135
252
  } else if (isKnown) {
136
253
  return 'pending';
@@ -146,12 +263,21 @@ export class AztecKVTxPool implements TxPool {
146
263
  */
147
264
  public async getTxByHash(txHash: TxHash): Promise<Tx | undefined> {
148
265
  const buffer = await this.#txs.getAsync(txHash.toString());
149
- if (buffer) {
150
- const tx = Tx.fromBuffer(buffer);
151
- tx.setTxHash(txHash);
152
- return tx;
153
- }
154
- return undefined;
266
+ return buffer ? Tx.fromBuffer(buffer) : undefined;
267
+ }
268
+
269
+ async getTxsByHash(txHashes: TxHash[]): Promise<(Tx | undefined)[]> {
270
+ const txs = await Promise.all(txHashes.map(txHash => this.#txs.getAsync(txHash.toString())));
271
+ return txs.map(buffer => (buffer ? Tx.fromBuffer(buffer) : undefined));
272
+ }
273
+
274
+ async hasTxs(txHashes: TxHash[]): Promise<boolean[]> {
275
+ return await Promise.all(txHashes.map(txHash => this.#txs.hasAsync(txHash.toString())));
276
+ }
277
+
278
+ async hasTx(txHash: TxHash): Promise<boolean> {
279
+ const result = await this.hasTxs([txHash]);
280
+ return result[0];
155
281
  }
156
282
 
157
283
  /**
@@ -161,12 +287,7 @@ export class AztecKVTxPool implements TxPool {
161
287
  */
162
288
  public async getArchivedTxByHash(txHash: TxHash): Promise<Tx | undefined> {
163
289
  const buffer = await this.#archivedTxs.getAsync(txHash.toString());
164
- if (buffer) {
165
- const tx = Tx.fromBuffer(buffer);
166
- tx.setTxHash(txHash);
167
- return tx;
168
- }
169
- return undefined;
290
+ return buffer ? Tx.fromBuffer(buffer) : undefined;
170
291
  }
171
292
 
172
293
  /**
@@ -174,75 +295,99 @@ export class AztecKVTxPool implements TxPool {
174
295
  * @param txs - An array of txs to be added to the pool.
175
296
  * @returns Empty promise.
176
297
  */
177
- public async addTxs(txs: Tx[]): Promise<void> {
178
- const hashesAndStats = await Promise.all(
179
- txs.map(async tx => ({ txHash: await tx.getTxHash(), txStats: await tx.getStats() })),
180
- );
298
+ public async addTxs(txs: Tx[], opts: { source?: string } = {}): Promise<number> {
299
+ const addedTxs: Tx[] = [];
300
+ const hashesAndStats = txs.map(tx => ({ txHash: tx.getTxHash(), txStats: tx.getStats() }));
181
301
  await this.#store.transactionAsync(async () => {
182
- let pendingCount = 0;
302
+ let pendingTxSize = (await this.#pendingTxSize.getAsync()) ?? 0;
183
303
  await Promise.all(
184
304
  txs.map(async (tx, i) => {
185
305
  const { txHash, txStats } = hashesAndStats[i];
306
+ const key = txHash.toString();
307
+ if (await this.#txs.hasAsync(key)) {
308
+ this.#log.debug(`Tx ${txHash.toString()} already exists in the pool`);
309
+ return;
310
+ }
311
+
186
312
  this.#log.verbose(`Adding tx ${txHash.toString()} to pool`, {
187
313
  eventName: 'tx-added-to-pool',
188
314
  ...txStats,
189
315
  } satisfies TxAddedToPoolStats);
190
316
 
191
- const key = txHash.toString();
192
317
  await this.#txs.set(key, tx.toBuffer());
318
+ addedTxs.push(tx as Tx);
193
319
 
194
320
  if (!(await this.#minedTxHashToBlock.hasAsync(key))) {
195
- pendingCount++;
196
- // REFACTOR: Use an lmdb conditional write to avoid race conditions with this write tx
197
- await this.#pendingTxPriorityToHash.set(getPendingTxPriority(tx), key);
321
+ pendingTxSize += tx.getSize();
322
+ await this.addPendingTxIndices(tx, key);
198
323
  this.#metrics.recordSize(tx);
199
324
  }
200
325
  }),
201
326
  );
202
327
 
203
- this.#metrics.recordAddedObjects(pendingCount, 'pending');
328
+ await this.#pendingTxSize.set(pendingTxSize);
329
+ await this.evictLowPriorityTxs(hashesAndStats.map(({ txHash }) => txHash));
204
330
  });
331
+
332
+ if (addedTxs.length > 0) {
333
+ this.#metrics.transactionsAdded(addedTxs);
334
+ this.emit('txs-added', { ...opts, txs: addedTxs });
335
+ }
336
+ return addedTxs.length;
205
337
  }
206
338
 
207
339
  /**
208
340
  * Deletes transactions from the pool. Tx hashes that are not present are ignored.
209
- * @param txHashes - An array of tx hashes to be removed from the tx pool.
341
+ * Mined transactions are soft-deleted with a timestamp, pending transactions are permanently deleted.
342
+ * @param txHashes - An array of tx hashes to be deleted from the tx pool.
210
343
  * @returns Empty promise.
211
344
  */
212
- public deleteTxs(txHashes: TxHash[]): Promise<void> {
213
- let pendingDeleted = 0;
214
- let minedDeleted = 0;
215
-
345
+ public deleteTxs(txHashes: TxHash[], opts: { eviction?: boolean; permanently?: boolean } = {}): Promise<void> {
346
+ if (txHashes.length === 0) {
347
+ return Promise.resolve();
348
+ }
216
349
  const deletedTxs: Tx[] = [];
217
350
  const poolDbTx = this.#store.transactionAsync(async () => {
351
+ let pendingTxSize = (await this.#pendingTxSize.getAsync()) ?? 0;
218
352
  for (const hash of txHashes) {
219
353
  const key = hash.toString();
220
354
  const tx = await this.getTxByHash(hash);
221
355
 
222
356
  if (tx) {
223
- const fee = getPendingTxPriority(tx);
224
- await this.#pendingTxPriorityToHash.deleteValue(fee, key);
225
-
226
- const isMined = await this.#minedTxHashToBlock.hasAsync(key);
227
- if (isMined) {
228
- minedDeleted++;
357
+ const minedBlockNumber = await this.#minedTxHashToBlock.getAsync(key);
358
+
359
+ if (minedBlockNumber !== undefined) {
360
+ await this.#minedTxHashToBlock.delete(key);
361
+ if (opts.permanently) {
362
+ // Permanently delete mined transactions if specified
363
+ this.#log.trace(`Deleting mined tx ${key} from pool`);
364
+ await this.#txs.delete(key);
365
+ } else {
366
+ // Soft-delete mined transactions: remove from mined set but keep in storage
367
+ this.#log.trace(`Soft-deleting mined tx ${key} from pool`);
368
+ await this.#deletedMinedTxHashes.set(key, minedBlockNumber);
369
+ await this.#blockToDeletedMinedTxHash.set(minedBlockNumber, key);
370
+ }
229
371
  } else {
230
- pendingDeleted++;
372
+ // Permanently delete pending transactions
373
+ this.#log.trace(`Deleting pending tx ${key} from pool`);
374
+ pendingTxSize -= tx.getSize();
375
+ await this.removePendingTxIndices(tx, key);
376
+ await this.#txs.delete(key);
231
377
  }
232
378
 
233
- if (this.#archivedTxLimit) {
379
+ if (!opts.eviction && this.#archivedTxLimit) {
234
380
  deletedTxs.push(tx);
235
381
  }
236
-
237
- await this.#txs.delete(key);
238
- await this.#minedTxHashToBlock.delete(key);
382
+ } else {
383
+ this.#log.trace(`Skipping deletion of missing tx ${key} from pool`);
239
384
  }
240
385
  }
241
386
 
242
- this.#metrics.recordRemovedObjects(pendingDeleted, 'pending');
243
- this.#metrics.recordRemovedObjects(minedDeleted, 'mined');
387
+ await this.#pendingTxSize.set(pendingTxSize);
244
388
  });
245
-
389
+ this.#metrics.transactionsRemoved(txHashes);
390
+ this.#log.debug(`Deleted ${txHashes.length} txs from pool`, { txHashes });
246
391
  return this.#archivedTxLimit ? poolDbTx.then(() => this.archiveTxs(deletedTxs)) : poolDbTx;
247
392
  }
248
393
 
@@ -251,12 +396,8 @@ export class AztecKVTxPool implements TxPool {
251
396
  * @returns Array of tx objects in the order they were added to the pool.
252
397
  */
253
398
  public async getAllTxs(): Promise<Tx[]> {
254
- const vals = await toArray(this.#txs.entriesAsync());
255
- return vals.map(([hash, buffer]) => {
256
- const tx = Tx.fromBuffer(buffer);
257
- tx.setTxHash(TxHash.fromString(hash));
258
- return tx;
259
- });
399
+ const vals = await toArray(this.#txs.valuesAsync());
400
+ return vals.map(buffer => Tx.fromBuffer(buffer));
260
401
  }
261
402
 
262
403
  /**
@@ -268,42 +409,346 @@ export class AztecKVTxPool implements TxPool {
268
409
  return vals.map(x => TxHash.fromString(x));
269
410
  }
270
411
 
412
+ public updateConfig({ maxTxPoolSize, txPoolOverflowFactor, archivedTxLimit }: TxPoolOptions): void {
413
+ if (typeof maxTxPoolSize === 'number') {
414
+ assert(maxTxPoolSize >= 0, 'maxTxPoolSize must be greater or equal to 0');
415
+ this.#maxTxPoolSize = maxTxPoolSize;
416
+
417
+ if (maxTxPoolSize === 0) {
418
+ this.#log.info(`Disabling maximum tx mempool size. Tx eviction stopped`);
419
+ } else {
420
+ this.#log.info(`Setting maximum tx mempool size`, { maxTxPoolSize });
421
+ }
422
+ }
423
+
424
+ if (typeof txPoolOverflowFactor === 'number') {
425
+ assert(txPoolOverflowFactor >= 1, 'txPoolOveflowFactor must be greater or equal to 1');
426
+ this.txPoolOverflowFactor = txPoolOverflowFactor;
427
+ this.#log.info(`Allowing tx pool size to grow above limit`, { maxTxPoolSize, txPoolOverflowFactor });
428
+ }
429
+
430
+ if (typeof archivedTxLimit === 'number') {
431
+ assert(archivedTxLimit >= 0, 'archivedTxLimit must be greater or equal to 0');
432
+ this.#archivedTxLimit = archivedTxLimit;
433
+ }
434
+
435
+ // deletedMinedCleanupThresholdMs is no longer used in block-based cleanup
436
+ }
437
+
438
+ public markTxsAsNonEvictable(txHashes: TxHash[]): Promise<void> {
439
+ txHashes.forEach(txHash => this.#nonEvictableTxs.add(txHash.toString()));
440
+ return Promise.resolve();
441
+ }
442
+
443
+ /**
444
+ * Permanently deletes deleted mined transactions from blocks up to and including the specified block number.
445
+ * @param blockNumber - Block number threshold. Deleted mined txs from this block or earlier will be permanently deleted.
446
+ * @returns The number of transactions permanently deleted.
447
+ */
448
+ public async cleanupDeletedMinedTxs(blockNumber: BlockNumber): Promise<number> {
449
+ let deletedCount = 0;
450
+ const txHashesToDelete: string[] = [];
451
+ const blocksToDelete: BlockNumber[] = [];
452
+
453
+ await this.#store.transactionAsync(async () => {
454
+ // Iterate through all entries and check block numbers
455
+ for await (const [block, txHash] of this.#blockToDeletedMinedTxHash.entriesAsync()) {
456
+ if (block <= blockNumber) {
457
+ // Permanently delete the transaction
458
+ await this.#txs.delete(txHash);
459
+ await this.#deletedMinedTxHashes.delete(txHash);
460
+ txHashesToDelete.push(txHash);
461
+ if (!blocksToDelete.includes(block)) {
462
+ blocksToDelete.push(block);
463
+ }
464
+ deletedCount++;
465
+ }
466
+ }
467
+
468
+ // Clean up block-to-hash mapping - delete all values for each block
469
+ for (const block of blocksToDelete) {
470
+ const txHashesForBlock = await toArray(this.#blockToDeletedMinedTxHash.getValuesAsync(block));
471
+ for (const txHash of txHashesForBlock) {
472
+ await this.#blockToDeletedMinedTxHash.deleteValue(block, txHash);
473
+ }
474
+ }
475
+ });
476
+
477
+ if (deletedCount > 0) {
478
+ this.#log.debug(`Permanently deleted ${deletedCount} deleted mined txs from blocks up to ${blockNumber}`);
479
+ }
480
+ return deletedCount;
481
+ }
482
+
483
+ /**
484
+ * Creates a GasTxValidator instance.
485
+ * @param db - DB for the validator to use
486
+ * @returns A GasTxValidator instance
487
+ */
488
+ protected createGasTxValidator(db: MerkleTreeReadOperations): GasTxValidator {
489
+ return new GasTxValidator(new DatabasePublicStateSource(db), ProtocolContractAddress.FeeJuice, GasFees.empty());
490
+ }
491
+
492
+ /**
493
+ * Creates an ArchiveCache instance.
494
+ * @param db - DB for the cache to use
495
+ * @returns An ArchiveCache instance
496
+ */
497
+ protected createArchiveCache(db: MerkleTreeReadOperations): ArchiveCache {
498
+ return new ArchiveCache(db);
499
+ }
500
+
501
+ /**
502
+ * Checks if a cached transaction exists in the in-memory pending tx pool and returns it.
503
+ * Otherwise, it checks the tx pool, updates the pending tx pool, and returns the tx.
504
+ * @param txHash - The generated tx hash.
505
+ * @returns The transaction, if found, 'undefined' otherwise.
506
+ */
507
+ private async getPendingTxByHash(txHash: TxHash | string): Promise<Tx | undefined> {
508
+ let key;
509
+ if (typeof txHash === 'string') {
510
+ key = txHash;
511
+ txHash = TxHash.fromString(txHash);
512
+ } else {
513
+ key = txHash.toString();
514
+ }
515
+
516
+ if (this.#pendingTxs.has(key)) {
517
+ return this.#pendingTxs.get(key);
518
+ }
519
+ const tx = await this.getTxByHash(txHash);
520
+ if (tx) {
521
+ this.#pendingTxs.set(key, tx);
522
+ return tx;
523
+ }
524
+ return undefined;
525
+ }
526
+
271
527
  /**
272
528
  * Archives a list of txs for future reference. The number of archived txs is limited by the specified archivedTxLimit.
273
529
  * @param txs - The list of transactions to archive.
274
530
  * @returns Empty promise.
275
531
  */
276
532
  private async archiveTxs(txs: Tx[]): Promise<void> {
277
- const txHashes = await Promise.all(txs.map(tx => tx.getTxHash()));
278
- await this.#archive.transactionAsync(async () => {
279
- // calcualte the head and tail indices of the archived txs by insertion order.
280
- let headIdx =
281
- ((await this.#archivedTxIndices.entriesAsync({ limit: 1, reverse: true }).next()).value?.[0] ?? -1) + 1;
282
- let tailIdx = (await this.#archivedTxIndices.entriesAsync({ limit: 1 }).next()).value?.[0] ?? 0;
283
-
284
- for (let i = 0; i < txs.length; i++) {
285
- const tx = txs[i];
286
- while (headIdx - tailIdx >= this.#archivedTxLimit) {
287
- const txHash = await this.#archivedTxIndices.getAsync(tailIdx);
288
- if (txHash) {
289
- await this.#archivedTxs.delete(txHash);
290
- await this.#archivedTxIndices.delete(tailIdx);
533
+ if (txs.length === 0) {
534
+ return;
535
+ }
536
+ try {
537
+ const txHashes = await Promise.all(txs.map(tx => tx.getTxHash()));
538
+ await this.#archive.transactionAsync(async () => {
539
+ // calculate the head and tail indices of the archived txs by insertion order.
540
+ let headIdx =
541
+ ((await this.#archivedTxIndices.entriesAsync({ limit: 1, reverse: true }).next()).value?.[0] ?? -1) + 1;
542
+ let tailIdx = (await this.#archivedTxIndices.entriesAsync({ limit: 1 }).next()).value?.[0] ?? 0;
543
+
544
+ for (let i = 0; i < txs.length; i++) {
545
+ const tx = txs[i];
546
+ while (headIdx - tailIdx >= this.#archivedTxLimit) {
547
+ const txHash = await this.#archivedTxIndices.getAsync(tailIdx);
548
+ if (txHash) {
549
+ await this.#archivedTxs.delete(txHash);
550
+ await this.#archivedTxIndices.delete(tailIdx);
551
+ }
552
+ tailIdx++;
291
553
  }
292
- tailIdx++;
554
+
555
+ const archivedTx: Tx = new Tx(
556
+ tx.txHash,
557
+ tx.data,
558
+ ChonkProof.empty(),
559
+ tx.contractClassLogFields,
560
+ tx.publicFunctionCalldata,
561
+ );
562
+ const txHash = txHashes[i].toString();
563
+ await this.#archivedTxs.set(txHash, archivedTx.toBuffer());
564
+ await this.#archivedTxIndices.set(headIdx, txHash);
565
+ headIdx++;
566
+ }
567
+ this.#log.debug(`Archived ${txs.length} txs`, { txHashes });
568
+ this.#log.debug(`Total archived txs: ${headIdx - tailIdx}`);
569
+ });
570
+ } catch (error) {
571
+ this.#log.error(`Error archiving txs`, { error });
572
+ }
573
+ }
574
+
575
+ /**
576
+ * Evicts pending txs with the lowest priority fees from the pool to accomodate the max tx count and cumulative max tx size
577
+ * after new txs are added.
578
+ *
579
+ * @param newTxHashes - The tx hashes of the new txs added to the pool.
580
+ * @returns The total number of txs evicted from the pool and the number of new txs that were evicted.
581
+ */
582
+ private async evictLowPriorityTxs(
583
+ newTxHashes: TxHash[],
584
+ ): Promise<{ numLowPriorityTxsEvicted: number; numNewTxsEvicted: number }> {
585
+ if (this.#maxTxPoolSize === undefined || this.#maxTxPoolSize === 0) {
586
+ return { numLowPriorityTxsEvicted: 0, numNewTxsEvicted: 0 };
587
+ }
588
+
589
+ let numNewTxsEvicted = 0;
590
+ const txsToEvict: TxHash[] = [];
591
+
592
+ let pendingTxsSize = (await this.#pendingTxSize.getAsync()) ?? 0;
593
+ if (pendingTxsSize > this.#maxTxPoolSize * this.txPoolOverflowFactor) {
594
+ for await (const txHash of this.#pendingTxPriorityToHash.valuesAsync()) {
595
+ if (this.#nonEvictableTxs.has(txHash.toString())) {
596
+ continue;
293
597
  }
598
+ const txSize =
599
+ (await this.#pendingTxHashToSize.getAsync(txHash.toString())) ??
600
+ (await this.getPendingTxByHash(txHash))?.getSize();
294
601
 
295
- const archivedTx: Tx = new Tx(
296
- tx.data,
297
- ClientIvcProof.empty(),
298
- tx.contractClassLogs,
299
- tx.enqueuedPublicFunctionCalls,
300
- tx.publicTeardownFunctionCall,
602
+ this.#log.verbose(`Evicting tx ${txHash} from pool due to low priority to satisfy max tx size limit`, {
603
+ txHash,
604
+ txSize,
605
+ });
606
+
607
+ txsToEvict.push(TxHash.fromString(txHash));
608
+
609
+ if (txSize) {
610
+ pendingTxsSize -= txSize;
611
+ if (pendingTxsSize <= this.#maxTxPoolSize) {
612
+ break;
613
+ }
614
+ }
615
+ }
616
+ numNewTxsEvicted += newTxHashes.filter(txHash => txsToEvict.includes(txHash)).length;
617
+ }
618
+
619
+ if (txsToEvict.length > 0) {
620
+ await this.deleteTxs(txsToEvict, { eviction: true });
621
+ }
622
+ return {
623
+ numLowPriorityTxsEvicted: txsToEvict.length,
624
+ numNewTxsEvicted,
625
+ };
626
+ }
627
+
628
+ /**
629
+ * Evicts invalid pending txs from the pool after a txs from a block are mined.
630
+ * Eviction criteria includes:
631
+ * - txs with nullifiers that are already included in the mined block
632
+ * - txs with an insufficient fee payer balance
633
+ * - txs with an expiration timestamp lower than that of the mined block
634
+ *
635
+ * @param minedTxHashes - The tx hashes of the txs mined in the block.
636
+ * @param blockHeader - The header of the mined block.
637
+ * @returns The total number of txs evicted from the pool.
638
+ */
639
+ private async evictInvalidTxsAfterMining(
640
+ minedTxHashes: TxHash[],
641
+ blockHeader: BlockHeader,
642
+ minedNullifiers: Set<string>,
643
+ minedFeePayers: Set<string>,
644
+ ): Promise<number> {
645
+ if (minedTxHashes.length === 0) {
646
+ return 0;
647
+ }
648
+
649
+ const { blockNumber, timestamp } = blockHeader.globalVariables;
650
+
651
+ // Wait for world state to be synced to at least the mined block number
652
+ await this.#worldStateSynchronizer.syncImmediate(blockNumber);
653
+
654
+ const db = this.#worldStateSynchronizer.getCommitted();
655
+ const gasTxValidator = this.createGasTxValidator(db);
656
+
657
+ const txsToEvict: TxHash[] = [];
658
+ for await (const txHash of this.#pendingTxPriorityToHash.valuesAsync()) {
659
+ const tx = await this.getPendingTxByHash(txHash);
660
+ if (!tx) {
661
+ continue;
662
+ }
663
+
664
+ // Evict pending txs that share nullifiers with mined txs
665
+ const txNullifiers = tx.data.getNonEmptyNullifiers();
666
+ if (txNullifiers.some(nullifier => minedNullifiers.has(nullifier.toString()))) {
667
+ this.#log.verbose(`Evicting tx ${txHash} from pool due to a duplicate nullifier with a mined tx`);
668
+ txsToEvict.push(TxHash.fromString(txHash));
669
+ continue;
670
+ }
671
+
672
+ // Evict pending txs with an insufficient fee payer balance
673
+ if (
674
+ minedFeePayers.has(tx.data.feePayer.toString()) &&
675
+ (await gasTxValidator.validateTxFee(tx)).result === 'invalid'
676
+ ) {
677
+ this.#log.verbose(`Evicting tx ${txHash} from pool due to an insufficient fee payer balance`);
678
+ txsToEvict.push(TxHash.fromString(txHash));
679
+ continue;
680
+ }
681
+
682
+ // Evict pending txs with an expiration timestamp less than or equal to the mined block timestamp
683
+ const includeByTimestamp = tx.data.includeByTimestamp;
684
+ if (includeByTimestamp <= timestamp) {
685
+ this.#log.verbose(
686
+ `Evicting tx ${txHash} from pool due to the tx being expired (includeByTimestamp: ${includeByTimestamp}, mined block timestamp: ${timestamp})`,
301
687
  );
302
- const txHash = txHashes[i].toString();
303
- await this.#archivedTxs.set(txHash, archivedTx.toBuffer());
304
- await this.#archivedTxIndices.set(headIdx, txHash);
305
- headIdx++;
688
+ txsToEvict.push(TxHash.fromString(txHash));
689
+ continue;
306
690
  }
307
- });
691
+ }
692
+
693
+ if (txsToEvict.length > 0) {
694
+ await this.deleteTxs(txsToEvict, { eviction: true });
695
+ }
696
+ return txsToEvict.length;
697
+ }
698
+
699
+ /**
700
+ * Evicts pending txs that no longer have valid archive roots or fee payer balances from the pool after a reorg.
701
+ *
702
+ * @param txHashes - The tx hashes of the txs that were moved from mined to pending.
703
+ * @returns The total number of txs evicted from the pool.
704
+ */
705
+ private async evictInvalidTxsAfterReorg(txHashes: TxHash[]): Promise<number> {
706
+ if (txHashes.length === 0) {
707
+ return 0;
708
+ }
709
+
710
+ await this.#worldStateSynchronizer.syncImmediate();
711
+ const db = this.#worldStateSynchronizer.getCommitted();
712
+ const archiveCache = this.createArchiveCache(db);
713
+ const gasTxValidator = this.createGasTxValidator(db);
714
+
715
+ const txsToEvict: TxHash[] = [];
716
+
717
+ for await (const [txHash, headerHash] of this.#pendingTxHashToHeaderHash.entriesAsync()) {
718
+ const tx = await this.getPendingTxByHash(txHash);
719
+ if (!tx) {
720
+ continue;
721
+ }
722
+
723
+ const [index] = await archiveCache.getArchiveIndices([Fr.fromString(headerHash)]);
724
+ if (index === undefined) {
725
+ this.#log.verbose(`Evicting tx ${txHash} from pool due to an invalid archive root`);
726
+ txsToEvict.push(TxHash.fromString(txHash));
727
+ continue;
728
+ }
729
+
730
+ if ((await gasTxValidator.validateTxFee(tx)).result === 'invalid') {
731
+ this.#log.verbose(`Evicting tx ${txHash} from pool due to an insufficient fee payer balance`);
732
+ txsToEvict.push(TxHash.fromString(txHash));
733
+ }
734
+ }
735
+
736
+ if (txsToEvict.length > 0) {
737
+ await this.deleteTxs(txsToEvict, { eviction: true });
738
+ }
739
+ return txsToEvict.length;
740
+ }
741
+
742
+ private async addPendingTxIndices(tx: Tx, txHash: string): Promise<void> {
743
+ await this.#pendingTxPriorityToHash.set(getPendingTxPriority(tx), txHash);
744
+ await this.#pendingTxHashToSize.set(txHash, tx.getSize());
745
+ await this.#pendingTxHashToHeaderHash.set(txHash, (await tx.data.constants.anchorBlockHeader.hash()).toString());
746
+ }
747
+
748
+ private async removePendingTxIndices(tx: Tx, txHash: string): Promise<void> {
749
+ await this.#pendingTxPriorityToHash.deleteValue(getPendingTxPriority(tx), txHash);
750
+ await this.#pendingTxHashToSize.delete(txHash);
751
+ await this.#pendingTxHashToHeaderHash.delete(txHash);
752
+ this.#pendingTxs.delete(txHash);
308
753
  }
309
754
  }