@aztec/p2p 0.0.0-test.1 → 0.0.1-commit.21caa21

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