@aztec/p2p 0.0.1-commit.9ef841308 → 0.0.1-commit.a5db02d

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 (344) hide show
  1. package/README.md +7 -6
  2. package/dest/bootstrap/bootstrap.d.ts +1 -1
  3. package/dest/bootstrap/bootstrap.d.ts.map +1 -1
  4. package/dest/bootstrap/bootstrap.js +22 -6
  5. package/dest/client/factory.d.ts +4 -3
  6. package/dest/client/factory.d.ts.map +1 -1
  7. package/dest/client/factory.js +33 -24
  8. package/dest/client/interface.d.ts +48 -11
  9. package/dest/client/interface.d.ts.map +1 -1
  10. package/dest/client/p2p_client.d.ts +34 -19
  11. package/dest/client/p2p_client.d.ts.map +1 -1
  12. package/dest/client/p2p_client.js +105 -46
  13. package/dest/config.d.ts +143 -107
  14. package/dest/config.d.ts.map +1 -1
  15. package/dest/config.js +72 -26
  16. package/dest/errors/p2p-service.error.d.ts +9 -0
  17. package/dest/errors/p2p-service.error.d.ts.map +1 -0
  18. package/dest/errors/p2p-service.error.js +10 -0
  19. package/dest/errors/reqresp.error.d.ts +9 -16
  20. package/dest/errors/reqresp.error.d.ts.map +1 -1
  21. package/dest/errors/reqresp.error.js +10 -17
  22. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +98 -56
  23. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  24. package/dest/mem_pools/attestation_pool/attestation_pool.js +269 -196
  25. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +1 -1
  26. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  27. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +218 -59
  28. package/dest/mem_pools/attestation_pool/mocks.d.ts +1 -1
  29. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  30. package/dest/mem_pools/attestation_pool/mocks.js +6 -4
  31. package/dest/mem_pools/instrumentation.d.ts +9 -4
  32. package/dest/mem_pools/instrumentation.d.ts.map +1 -1
  33. package/dest/mem_pools/instrumentation.js +28 -9
  34. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.d.ts +1 -5
  35. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.d.ts.map +1 -1
  36. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.js +1 -7
  37. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts +3 -3
  38. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts.map +1 -1
  39. package/dest/mem_pools/tx_pool_v2/deleted_pool.js +6 -6
  40. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.js +1 -1
  41. package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts +2 -1
  42. package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts.map +1 -1
  43. package/dest/mem_pools/tx_pool_v2/eviction/index.js +1 -0
  44. package/dest/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.d.ts +16 -0
  45. package/dest/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.d.ts.map +1 -0
  46. package/dest/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.js +62 -0
  47. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.js +2 -2
  48. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +33 -5
  49. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
  50. package/dest/mem_pools/tx_pool_v2/interfaces.js +2 -1
  51. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +5 -2
  52. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
  53. package/dest/mem_pools/tx_pool_v2/tx_metadata.js +8 -5
  54. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts +15 -1
  55. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts.map +1 -1
  56. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.js +31 -0
  57. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +9 -3
  58. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -1
  59. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.js +19 -8
  60. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +17 -4
  61. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
  62. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +208 -118
  63. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +10 -3
  64. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
  65. package/dest/msg_validators/attestation_validator/attestation_validator.js +38 -16
  66. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +7 -3
  67. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -1
  68. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.js +4 -5
  69. package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts +8 -3
  70. package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts.map +1 -1
  71. package/dest/msg_validators/proposal_validator/block_proposal_validator.js +2 -2
  72. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts +8 -3
  73. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts.map +1 -1
  74. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.js +8 -2
  75. package/dest/msg_validators/proposal_validator/proposal_validator.d.ts +25 -3
  76. package/dest/msg_validators/proposal_validator/proposal_validator.d.ts.map +1 -1
  77. package/dest/msg_validators/proposal_validator/proposal_validator.js +85 -15
  78. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +1 -1
  79. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
  80. package/dest/msg_validators/tx_validator/aggregate_tx_validator.js +8 -15
  81. package/dest/msg_validators/tx_validator/allowed_public_setup.js +3 -3
  82. package/dest/msg_validators/tx_validator/archive_cache.js +1 -1
  83. package/dest/msg_validators/tx_validator/cached_tx_validator.d.ts +15 -0
  84. package/dest/msg_validators/tx_validator/cached_tx_validator.d.ts.map +1 -0
  85. package/dest/msg_validators/tx_validator/cached_tx_validator.js +19 -0
  86. package/dest/msg_validators/tx_validator/data_validator.d.ts +2 -1
  87. package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
  88. package/dest/msg_validators/tx_validator/data_validator.js +1 -0
  89. package/dest/msg_validators/tx_validator/factory.d.ts +14 -15
  90. package/dest/msg_validators/tx_validator/factory.d.ts.map +1 -1
  91. package/dest/msg_validators/tx_validator/factory.js +29 -16
  92. package/dest/msg_validators/tx_validator/gas_validator.d.ts +47 -10
  93. package/dest/msg_validators/tx_validator/gas_validator.d.ts.map +1 -1
  94. package/dest/msg_validators/tx_validator/gas_validator.js +65 -48
  95. package/dest/msg_validators/tx_validator/index.d.ts +3 -1
  96. package/dest/msg_validators/tx_validator/index.d.ts.map +1 -1
  97. package/dest/msg_validators/tx_validator/index.js +2 -0
  98. package/dest/msg_validators/tx_validator/phases_validator.js +2 -2
  99. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts +2 -1
  100. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts.map +1 -1
  101. package/dest/msg_validators/tx_validator/tx_proof_validator.js +2 -0
  102. package/dest/msg_validators/tx_validator/tx_validation_cache.d.ts +48 -0
  103. package/dest/msg_validators/tx_validator/tx_validation_cache.d.ts.map +1 -0
  104. package/dest/msg_validators/tx_validator/tx_validation_cache.js +68 -0
  105. package/dest/services/data_store.d.ts +1 -1
  106. package/dest/services/data_store.d.ts.map +1 -1
  107. package/dest/services/data_store.js +5 -5
  108. package/dest/services/discv5/discV5_service.d.ts +13 -2
  109. package/dest/services/discv5/discV5_service.d.ts.map +1 -1
  110. package/dest/services/discv5/discV5_service.js +119 -25
  111. package/dest/services/discv5/persisted_enr_store.d.ts +38 -0
  112. package/dest/services/discv5/persisted_enr_store.d.ts.map +1 -0
  113. package/dest/services/discv5/persisted_enr_store.js +145 -0
  114. package/dest/services/dummy_service.d.ts +15 -15
  115. package/dest/services/dummy_service.d.ts.map +1 -1
  116. package/dest/services/dummy_service.js +15 -16
  117. package/dest/services/encoding.d.ts +19 -6
  118. package/dest/services/encoding.d.ts.map +1 -1
  119. package/dest/services/encoding.js +25 -15
  120. package/dest/services/gossipsub/topic_score_params.d.ts +6 -12
  121. package/dest/services/gossipsub/topic_score_params.d.ts.map +1 -1
  122. package/dest/services/gossipsub/topic_score_params.js +4 -14
  123. package/dest/services/libp2p/instrumentation.d.ts +3 -1
  124. package/dest/services/libp2p/instrumentation.d.ts.map +1 -1
  125. package/dest/services/libp2p/instrumentation.js +14 -0
  126. package/dest/services/libp2p/libp2p_service.d.ts +40 -40
  127. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  128. package/dest/services/libp2p/libp2p_service.js +292 -264
  129. package/dest/services/peer-manager/peer_manager.d.ts +15 -2
  130. package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
  131. package/dest/services/peer-manager/peer_manager.js +59 -17
  132. package/dest/services/peer-manager/peer_scoring.d.ts +30 -1
  133. package/dest/services/peer-manager/peer_scoring.d.ts.map +1 -1
  134. package/dest/services/peer-manager/peer_scoring.js +74 -3
  135. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts +1 -8
  136. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts.map +1 -1
  137. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.js +35 -44
  138. package/dest/services/reqresp/batch-tx-requester/interface.d.ts +8 -5
  139. package/dest/services/reqresp/batch-tx-requester/interface.d.ts.map +1 -1
  140. package/dest/services/reqresp/batch-tx-requester/tx_validator.d.ts +5 -14
  141. package/dest/services/reqresp/batch-tx-requester/tx_validator.d.ts.map +1 -1
  142. package/dest/services/reqresp/batch-tx-requester/tx_validator.js +6 -20
  143. package/dest/services/reqresp/config.d.ts +15 -3
  144. package/dest/services/reqresp/config.d.ts.map +1 -1
  145. package/dest/services/reqresp/config.js +11 -0
  146. package/dest/services/reqresp/interface.d.ts +16 -18
  147. package/dest/services/reqresp/interface.d.ts.map +1 -1
  148. package/dest/services/reqresp/interface.js +10 -20
  149. package/dest/services/reqresp/metrics.d.ts +1 -1
  150. package/dest/services/reqresp/metrics.d.ts.map +1 -1
  151. package/dest/services/reqresp/metrics.js +0 -1
  152. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts +4 -3
  153. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts.map +1 -1
  154. package/dest/services/reqresp/protocols/block_txs/bitvector.js +3 -2
  155. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +1 -1
  156. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -1
  157. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.js +31 -28
  158. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +12 -7
  159. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
  160. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +42 -24
  161. package/dest/services/reqresp/protocols/index.d.ts +1 -2
  162. package/dest/services/reqresp/protocols/index.d.ts.map +1 -1
  163. package/dest/services/reqresp/protocols/index.js +0 -1
  164. package/dest/services/reqresp/protocols/tx.d.ts +1 -1
  165. package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
  166. package/dest/services/reqresp/protocols/tx.js +1 -3
  167. package/dest/services/reqresp/rate-limiter/rate_limits.d.ts +1 -1
  168. package/dest/services/reqresp/rate-limiter/rate_limits.d.ts.map +1 -1
  169. package/dest/services/reqresp/rate-limiter/rate_limits.js +3 -13
  170. package/dest/services/reqresp/reqresp.d.ts +7 -29
  171. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  172. package/dest/services/reqresp/reqresp.js +59 -209
  173. package/dest/services/service.d.ts +23 -16
  174. package/dest/services/service.d.ts.map +1 -1
  175. package/dest/services/tx_collection/config.d.ts +2 -23
  176. package/dest/services/tx_collection/config.d.ts.map +1 -1
  177. package/dest/services/tx_collection/config.js +2 -55
  178. package/dest/services/tx_collection/file_store_tx_collection.d.ts +12 -28
  179. package/dest/services/tx_collection/file_store_tx_collection.d.ts.map +1 -1
  180. package/dest/services/tx_collection/file_store_tx_collection.js +43 -83
  181. package/dest/services/tx_collection/index.d.ts +2 -3
  182. package/dest/services/tx_collection/index.d.ts.map +1 -1
  183. package/dest/services/tx_collection/index.js +0 -1
  184. package/dest/services/tx_collection/instrumentation.d.ts +1 -1
  185. package/dest/services/tx_collection/instrumentation.d.ts.map +1 -1
  186. package/dest/services/tx_collection/instrumentation.js +0 -2
  187. package/dest/services/tx_collection/tx_collection.d.ts +35 -51
  188. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
  189. package/dest/services/tx_collection/tx_collection.js +275 -119
  190. package/dest/services/tx_collection/tx_collection_sink.d.ts +1 -1
  191. package/dest/services/tx_collection/tx_collection_sink.js +2 -2
  192. package/dest/services/tx_collection/tx_source.d.ts +1 -1
  193. package/dest/services/tx_collection/tx_source.d.ts.map +1 -1
  194. package/dest/services/tx_collection/tx_source.js +4 -1
  195. package/dest/services/tx_file_store/tx_file_store.d.ts +1 -3
  196. package/dest/services/tx_file_store/tx_file_store.d.ts.map +1 -1
  197. package/dest/services/tx_file_store/tx_file_store.js +4 -14
  198. package/dest/services/tx_provider.d.ts +3 -1
  199. package/dest/services/tx_provider.d.ts.map +1 -1
  200. package/dest/services/tx_provider.js +3 -0
  201. package/dest/test-helpers/make-test-p2p-clients.d.ts +1 -1
  202. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  203. package/dest/test-helpers/make-test-p2p-clients.js +5 -2
  204. package/dest/test-helpers/mock-pubsub.d.ts +21 -12
  205. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  206. package/dest/test-helpers/mock-pubsub.js +39 -47
  207. package/dest/test-helpers/reqresp-nodes.d.ts +4 -5
  208. package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
  209. package/dest/test-helpers/reqresp-nodes.js +16 -18
  210. package/dest/test-helpers/test_tx_provider.d.ts +3 -1
  211. package/dest/test-helpers/test_tx_provider.d.ts.map +1 -1
  212. package/dest/test-helpers/test_tx_provider.js +3 -0
  213. package/dest/test-helpers/testbench-utils.d.ts +15 -14
  214. package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
  215. package/dest/test-helpers/testbench-utils.js +28 -13
  216. package/dest/testbench/p2p_client_testbench_worker.d.ts +3 -5
  217. package/dest/testbench/p2p_client_testbench_worker.d.ts.map +1 -1
  218. package/dest/testbench/p2p_client_testbench_worker.js +60 -27
  219. package/dest/testbench/worker_client_manager.d.ts +10 -6
  220. package/dest/testbench/worker_client_manager.d.ts.map +1 -1
  221. package/dest/testbench/worker_client_manager.js +51 -8
  222. package/dest/util.d.ts +8 -8
  223. package/dest/util.d.ts.map +1 -1
  224. package/dest/util.js +38 -10
  225. package/dest/versioning.d.ts +3 -6
  226. package/dest/versioning.d.ts.map +1 -1
  227. package/dest/versioning.js +3 -24
  228. package/package.json +16 -16
  229. package/src/bootstrap/bootstrap.ts +17 -3
  230. package/src/client/factory.ts +49 -25
  231. package/src/client/interface.ts +44 -17
  232. package/src/client/p2p_client.ts +125 -79
  233. package/src/client/test/{tx_proposal_collector/README.md → p2p_client.batch_tx_requester.bench.README.md} +23 -53
  234. package/src/config.ts +146 -34
  235. package/src/errors/p2p-service.error.ts +11 -0
  236. package/src/errors/reqresp.error.ts +13 -19
  237. package/src/mem_pools/attestation_pool/attestation_pool.ts +324 -239
  238. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +238 -63
  239. package/src/mem_pools/attestation_pool/mocks.ts +13 -8
  240. package/src/mem_pools/instrumentation.ts +18 -12
  241. package/src/mem_pools/tx_pool_v2/archive/tx_archive.ts +1 -9
  242. package/src/mem_pools/tx_pool_v2/deleted_pool.ts +7 -7
  243. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.ts +1 -1
  244. package/src/mem_pools/tx_pool_v2/eviction/index.ts +1 -0
  245. package/src/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.ts +65 -0
  246. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.ts +3 -3
  247. package/src/mem_pools/tx_pool_v2/interfaces.ts +31 -4
  248. package/src/mem_pools/tx_pool_v2/tx_metadata.ts +13 -7
  249. package/src/mem_pools/tx_pool_v2/tx_pool_indices.ts +35 -0
  250. package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +22 -8
  251. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +233 -123
  252. package/src/msg_validators/attestation_validator/attestation_validator.ts +46 -15
  253. package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +15 -7
  254. package/src/msg_validators/proposal_validator/block_proposal_validator.ts +15 -3
  255. package/src/msg_validators/proposal_validator/checkpoint_proposal_validator.ts +26 -3
  256. package/src/msg_validators/proposal_validator/proposal_validator.ts +94 -12
  257. package/src/msg_validators/tx_validator/README.md +13 -3
  258. package/src/msg_validators/tx_validator/aggregate_tx_validator.ts +3 -12
  259. package/src/msg_validators/tx_validator/allowed_public_setup.ts +3 -3
  260. package/src/msg_validators/tx_validator/archive_cache.ts +1 -1
  261. package/src/msg_validators/tx_validator/cached_tx_validator.ts +31 -0
  262. package/src/msg_validators/tx_validator/data_validator.ts +2 -0
  263. package/src/msg_validators/tx_validator/factory.ts +37 -20
  264. package/src/msg_validators/tx_validator/gas_validator.ts +99 -54
  265. package/src/msg_validators/tx_validator/index.ts +2 -0
  266. package/src/msg_validators/tx_validator/phases_validator.ts +2 -2
  267. package/src/msg_validators/tx_validator/tx_proof_validator.ts +2 -0
  268. package/src/msg_validators/tx_validator/tx_validation_cache.ts +101 -0
  269. package/src/services/data_store.ts +5 -13
  270. package/src/services/discv5/discV5_service.ts +126 -16
  271. package/src/services/discv5/persisted_enr_store.ts +158 -0
  272. package/src/services/dummy_service.ts +20 -39
  273. package/src/services/encoding.ts +28 -20
  274. package/src/services/gossipsub/README.md +33 -11
  275. package/src/services/gossipsub/topic_score_params.ts +9 -18
  276. package/src/services/libp2p/instrumentation.ts +14 -0
  277. package/src/services/libp2p/libp2p_service.ts +342 -290
  278. package/src/services/peer-manager/peer_manager.ts +63 -16
  279. package/src/services/peer-manager/peer_scoring.ts +81 -3
  280. package/src/services/reqresp/README.md +2 -37
  281. package/src/services/reqresp/batch-tx-requester/batch_tx_requester.ts +39 -52
  282. package/src/services/reqresp/batch-tx-requester/interface.ts +11 -4
  283. package/src/services/reqresp/batch-tx-requester/tx_validator.ts +12 -25
  284. package/src/services/reqresp/config.ts +19 -2
  285. package/src/services/reqresp/interface.ts +21 -47
  286. package/src/services/reqresp/metrics.ts +0 -1
  287. package/src/services/reqresp/protocols/block_txs/bitvector.ts +3 -2
  288. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +34 -30
  289. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +47 -20
  290. package/src/services/reqresp/protocols/index.ts +0 -1
  291. package/src/services/reqresp/protocols/tx.ts +1 -3
  292. package/src/services/reqresp/rate-limiter/rate_limits.ts +3 -13
  293. package/src/services/reqresp/reqresp.ts +69 -252
  294. package/src/services/service.ts +28 -32
  295. package/src/services/tx_collection/config.ts +3 -80
  296. package/src/services/tx_collection/file_store_tx_collection.ts +54 -103
  297. package/src/services/tx_collection/index.ts +1 -6
  298. package/src/services/tx_collection/instrumentation.ts +1 -7
  299. package/src/services/tx_collection/tx_collection.ts +330 -173
  300. package/src/services/tx_collection/tx_collection_sink.ts +2 -2
  301. package/src/services/tx_collection/tx_source.ts +2 -1
  302. package/src/services/tx_file_store/tx_file_store.ts +5 -17
  303. package/src/services/tx_provider.ts +5 -0
  304. package/src/test-helpers/make-test-p2p-clients.ts +3 -0
  305. package/src/test-helpers/mock-pubsub.ts +41 -64
  306. package/src/test-helpers/reqresp-nodes.ts +12 -22
  307. package/src/test-helpers/test_tx_provider.ts +5 -0
  308. package/src/test-helpers/testbench-utils.ts +35 -26
  309. package/src/testbench/p2p_client_testbench_worker.ts +68 -44
  310. package/src/testbench/worker_client_manager.ts +59 -19
  311. package/src/util.ts +33 -14
  312. package/src/versioning.ts +3 -33
  313. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.d.ts +0 -2
  314. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.d.ts.map +0 -1
  315. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.js +0 -315
  316. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.d.ts +0 -73
  317. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.d.ts.map +0 -1
  318. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.js +0 -8
  319. package/dest/msg_validators/clock_tolerance.d.ts +0 -21
  320. package/dest/msg_validators/clock_tolerance.d.ts.map +0 -1
  321. package/dest/msg_validators/clock_tolerance.js +0 -38
  322. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts +0 -64
  323. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts.map +0 -1
  324. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.js +0 -151
  325. package/dest/services/reqresp/protocols/block.d.ts +0 -9
  326. package/dest/services/reqresp/protocols/block.d.ts.map +0 -1
  327. package/dest/services/reqresp/protocols/block.js +0 -32
  328. package/dest/services/tx_collection/fast_tx_collection.d.ts +0 -51
  329. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +0 -1
  330. package/dest/services/tx_collection/fast_tx_collection.js +0 -311
  331. package/dest/services/tx_collection/proposal_tx_collector.d.ts +0 -48
  332. package/dest/services/tx_collection/proposal_tx_collector.d.ts.map +0 -1
  333. package/dest/services/tx_collection/proposal_tx_collector.js +0 -50
  334. package/dest/services/tx_collection/slow_tx_collection.d.ts +0 -57
  335. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +0 -1
  336. package/dest/services/tx_collection/slow_tx_collection.js +0 -211
  337. package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker.ts +0 -355
  338. package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.ts +0 -43
  339. package/src/msg_validators/clock_tolerance.ts +0 -52
  340. package/src/services/reqresp/connection-sampler/batch_connection_sampler.ts +0 -161
  341. package/src/services/reqresp/protocols/block.ts +0 -37
  342. package/src/services/tx_collection/fast_tx_collection.ts +0 -361
  343. package/src/services/tx_collection/proposal_tx_collector.ts +0 -108
  344. package/src/services/tx_collection/slow_tx_collection.ts +0 -266
@@ -1,43 +0,0 @@
1
- import { BlockProposal } from '@aztec/stdlib/p2p';
2
- import { Tx, TxHash } from '@aztec/stdlib/tx';
3
-
4
- import type { P2PConfig } from '../../../config.js';
5
-
6
- export type SerializedP2PConfig = Omit<P2PConfig, 'peerIdPrivateKey'> & { peerIdPrivateKey?: string };
7
-
8
- export type CollectorType = 'batch-requester' | 'send-batch-request';
9
-
10
- export type WorkerCommand =
11
- | { type: 'START'; requestId: string; clientIndex: number; config: SerializedP2PConfig }
12
- | { type: 'SET_TXS'; requestId: string; txs: string[]; mode?: 'replace' | 'append' }
13
- | { type: 'SET_BLOCK_PROPOSAL'; requestId: string; blockProposal: string }
14
- | {
15
- type: 'RUN_COLLECTOR';
16
- requestId: string;
17
- collectorType: CollectorType;
18
- txHashes: string[];
19
- blockProposal: string;
20
- pinnedPeerId?: string;
21
- peerIds: string[];
22
- timeoutMs: number;
23
- }
24
- | { type: 'GET_PEER_COUNT'; requestId: string }
25
- | { type: 'STOP'; requestId: string };
26
-
27
- export type WorkerResponse =
28
- | { type: 'READY'; requestId: string; peerId: string }
29
- | { type: 'TXS_SET'; requestId: string; count: number }
30
- | { type: 'BLOCK_PROPOSAL_SET'; requestId: string; archiveRoot: string }
31
- | { type: 'COLLECTOR_RESULT'; requestId: string; durationMs: number; fetchedCount: number }
32
- | { type: 'PEER_COUNT'; requestId: string; count: number }
33
- | { type: 'STOPPED'; requestId: string }
34
- | { type: 'ERROR'; requestId: string; error: string };
35
-
36
- export const serializeTx = (tx: Tx) => tx.toBuffer().toString('hex');
37
- export const deserializeTx = (hex: string) => Tx.fromBuffer(Buffer.from(hex, 'hex'));
38
-
39
- export const serializeTxHash = (txHash: TxHash) => txHash.toString();
40
- export const deserializeTxHash = (hex: string) => TxHash.fromString(hex);
41
-
42
- export const serializeBlockProposal = (proposal: BlockProposal) => proposal.toBuffer().toString('hex');
43
- export const deserializeBlockProposal = (hex: string) => BlockProposal.fromBuffer(Buffer.from(hex, 'hex'));
@@ -1,52 +0,0 @@
1
- import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
- import { SlotNumber } from '@aztec/foundation/branded-types';
3
-
4
- /**
5
- * Maximum clock disparity tolerance for P2P message validation (in milliseconds).
6
- * Messages for the previous slot are accepted if we're within this many milliseconds
7
- * of the current slot start. This prevents penalizing peers for messages that
8
- * were valid when sent but arrived slightly late due to network latency.
9
- *
10
- * This follows Ethereum's MAXIMUM_GOSSIP_CLOCK_DISPARITY approach.
11
- */
12
- export const MAXIMUM_GOSSIP_CLOCK_DISPARITY_MS = 500;
13
-
14
- /**
15
- * Checks if a message for the previous slot should be accepted due to clock tolerance.
16
- *
17
- * @param messageSlot - The slot number from the received message
18
- * @param currentSlot - The current slot number
19
- * @param epochCache - EpochCache to get timing information
20
- * @returns true if the message is for the previous slot AND we're within the clock tolerance window
21
- */
22
- export function isWithinClockTolerance(
23
- messageSlot: SlotNumber,
24
- currentSlot: SlotNumber,
25
- epochCache: EpochCacheInterface,
26
- ): boolean {
27
- // Guard against slot 0 edge case (genesis)
28
- if (currentSlot === SlotNumber.ZERO) {
29
- return false;
30
- }
31
-
32
- // Only apply tolerance to messages for the previous slot
33
- const previousSlot = SlotNumber(currentSlot - 1);
34
- if (messageSlot !== previousSlot) {
35
- return false;
36
- }
37
-
38
- // Check how far we are into the current slot (in milliseconds)
39
- const { ts: slotStartTs, nowMs } = epochCache.getEpochAndSlotNow();
40
- const targetSlot = epochCache.getTargetSlot();
41
-
42
- // Sanity check: ensure the epoch cache's target slot matches the expected current slot
43
- if (targetSlot !== currentSlot) {
44
- return false;
45
- }
46
-
47
- // ts is in seconds, convert to ms; nowMs is already in milliseconds
48
- const slotStartMs = slotStartTs * 1000n;
49
- const elapsedMs = Number(nowMs - slotStartMs);
50
-
51
- return elapsedMs < MAXIMUM_GOSSIP_CLOCK_DISPARITY_MS;
52
- }
@@ -1,161 +0,0 @@
1
- import { createLogger } from '@aztec/foundation/log';
2
-
3
- import type { PeerId } from '@libp2p/interface';
4
-
5
- import type { ConnectionSampler } from './connection_sampler.js';
6
-
7
- /**
8
- * Manages batches of peers for parallel request processing.
9
- * Tracks active peers and provides deterministic peer assignment for requests.
10
- *
11
- * Example with 3 peers and 10 requests:
12
- *
13
- * Peers: [P1] [P2] [P3]
14
- * ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
15
- * Requests: 0,1,2,9 | 3,4,5 | 6,7,8
16
- *
17
- * Each peer handles a bucket of consecutive requests.
18
- * If a peer fails, it is replaced while maintaining the same bucket.
19
- */
20
- export class BatchConnectionSampler {
21
- private readonly batch: PeerId[] = [];
22
- private readonly requestsPerPeer: number;
23
- /** Tracks peer-index combinations that returned empty/invalid responses */
24
- private readonly failedPeerIndices: Map<string, Set<number>> = new Map();
25
-
26
- constructor(
27
- private readonly connectionSampler: ConnectionSampler,
28
- batchSize: number,
29
- maxPeers: number,
30
- exclude?: PeerId[],
31
- private readonly logger = createLogger('p2p:reqresp:batch-connection-sampler'),
32
- ) {
33
- if (maxPeers <= 0) {
34
- throw new Error('Max peers cannot be 0');
35
- }
36
- if (batchSize <= 0) {
37
- throw new Error('Batch size cannot be 0');
38
- }
39
-
40
- // Calculate how many requests each peer should handle, cannot be 0
41
- this.requestsPerPeer = Math.max(1, Math.floor(batchSize / maxPeers));
42
-
43
- // Sample initial peers
44
- const excluding = exclude && new Map(exclude.map(peerId => [peerId.toString(), true] as const));
45
- this.batch = this.connectionSampler.samplePeersBatch(maxPeers, excluding);
46
- }
47
-
48
- /**
49
- * Gets the peer responsible for handling a specific request index.
50
- * If the primary peer has previously failed for this index, tries other peers.
51
- * If all batch peers have failed, attempts to sample a new peer.
52
- *
53
- * @param index - The request index
54
- * @returns The peer assigned to handle this request, or undefined if no peer available
55
- */
56
- getPeerForRequest(index: number): PeerId | undefined {
57
- if (this.batch.length === 0) {
58
- return undefined;
59
- }
60
-
61
- // Calculate which peer bucket this index belongs to
62
- const primaryPeerIndex = Math.floor(index / this.requestsPerPeer) % this.batch.length;
63
-
64
- // Try peers starting from primary, wrapping around
65
- for (let offset = 0; offset < this.batch.length; offset++) {
66
- const peerIndex = (primaryPeerIndex + offset) % this.batch.length;
67
- const peer = this.batch[peerIndex];
68
- const peerKey = peer.toString();
69
-
70
- const failedIndices = this.failedPeerIndices.get(peerKey);
71
- if (!failedIndices || !failedIndices.has(index)) {
72
- return peer;
73
- }
74
- }
75
-
76
- // All batch peers have failed for this index - try to sample a new peer
77
- const newPeer = this.sampleNewPeer();
78
- if (newPeer) {
79
- return newPeer;
80
- }
81
-
82
- return undefined;
83
- }
84
-
85
- /**
86
- * Attempts to sample a new peer that isn't already in the batch.
87
- * If successful, adds the peer to the batch.
88
- *
89
- * @returns The new peer if one was sampled, undefined otherwise
90
- */
91
- private sampleNewPeer(): PeerId | undefined {
92
- // Exclude all current batch peers
93
- const excluding = new Map(this.batch.map(p => [p.toString(), true] as const));
94
- const newPeer = this.connectionSampler.getPeer(excluding);
95
-
96
- if (newPeer) {
97
- this.batch.push(newPeer);
98
- this.logger.trace('Sampled new peer for exhausted index', { newPeer: newPeer.toString() });
99
- return newPeer;
100
- }
101
-
102
- return undefined;
103
- }
104
-
105
- /**
106
- * Marks that a peer returned an empty/invalid response for a specific request index.
107
- * The peer will not be assigned this index again.
108
- *
109
- * @param peerId - The peer that failed
110
- * @param index - The request index that failed
111
- */
112
- markPeerFailedForIndex(peerId: PeerId, index: number): void {
113
- const peerKey = peerId.toString();
114
- let failedIndices = this.failedPeerIndices.get(peerKey);
115
- if (!failedIndices) {
116
- failedIndices = new Set();
117
- this.failedPeerIndices.set(peerKey, failedIndices);
118
- }
119
- failedIndices.add(index);
120
- this.logger.trace('Marked peer failed for index', { peerId: peerKey, index });
121
- }
122
-
123
- /**
124
- * Removes a peer and replaces it with a new one, maintaining the same position
125
- * in the batch array to keep request distribution consistent
126
- *
127
- * @param peerId - The peer to remove and replace
128
- */
129
- removePeerAndReplace(peerId: PeerId): void {
130
- const index = this.batch.findIndex(p => p === peerId);
131
- if (index === -1) {
132
- return;
133
- }
134
-
135
- const excluding = new Map([[peerId.toString(), true]]);
136
- const newPeer = this.connectionSampler.getPeer(excluding); // Q: Shouldn't we accumulate all excluded peers? Otherwise the sampler could return us a previously excluded peer?
137
-
138
- if (newPeer) {
139
- this.batch[index] = newPeer;
140
- this.logger.trace('Replaced peer', { peerId, newPeer });
141
- } else {
142
- // If we couldn't get a replacement, remove the peer and compact the array
143
- this.batch.splice(index, 1);
144
- this.logger.trace('Removed peer', { peerId });
145
- }
146
- }
147
-
148
- /**
149
- * Gets the number of active peers
150
- */
151
- get activePeerCount(): number {
152
- return this.batch.length;
153
- }
154
-
155
- /**
156
- * Gets the number of requests each peer is assigned to handle
157
- */
158
- get requestsPerBucket(): number {
159
- return this.requestsPerPeer;
160
- }
161
- }
@@ -1,37 +0,0 @@
1
- import { BlockNumber } from '@aztec/foundation/branded-types';
2
- import { Fr } from '@aztec/foundation/curves/bn254';
3
- import type { L2BlockSource } from '@aztec/stdlib/block';
4
-
5
- import type { PeerId } from '@libp2p/interface';
6
-
7
- import type { ReqRespSubProtocolHandler } from '../interface.js';
8
- import { ReqRespStatus, ReqRespStatusError } from '../status.js';
9
-
10
- /**
11
- * Handler for L2 Block requests
12
- * @param l2BlockSource - source for L2 blocks
13
- * @returns the Block request handler
14
- * */
15
- export function reqRespBlockHandler(l2BlockSource: L2BlockSource): ReqRespSubProtocolHandler {
16
- /**
17
- * @param peerId - the peer ID of the requester
18
- * @param msg - the block request message, which is expected to contain valid block number as a Buffer
19
- * @returns a Buffer containing the requested block data, or an empty Buffer if the block is not found
20
- * @throws ReqRespStatusError if the input msg is not a valid block number
21
- * */
22
- return async (_peerId: PeerId, msg: Buffer) => {
23
- let blockNumber: Fr;
24
- try {
25
- blockNumber = Fr.fromBuffer(msg);
26
- } catch (err: any) {
27
- throw new ReqRespStatusError(ReqRespStatus.BADLY_FORMED_REQUEST, { cause: err });
28
- }
29
-
30
- try {
31
- const foundBlock = await l2BlockSource.getBlock(BlockNumber(Number(blockNumber)));
32
- return foundBlock ? foundBlock.toBuffer() : Buffer.alloc(0);
33
- } catch (err: any) {
34
- throw new ReqRespStatusError(ReqRespStatus.INTERNAL_ERROR, { cause: err });
35
- }
36
- };
37
- }
@@ -1,361 +0,0 @@
1
- import { BlockNumber } from '@aztec/foundation/branded-types';
2
- import { times } from '@aztec/foundation/collection';
3
- import { type Logger, createLogger } from '@aztec/foundation/log';
4
- import { sleep } from '@aztec/foundation/sleep';
5
- import { DateProvider, elapsed } from '@aztec/foundation/timer';
6
- import type { L2BlockInfo } from '@aztec/stdlib/block';
7
- import { type Tx, TxHash } from '@aztec/stdlib/tx';
8
-
9
- import type { PeerId } from '@libp2p/interface';
10
-
11
- import type { BatchTxRequesterConfig } from '../reqresp/batch-tx-requester/config.js';
12
- import type { BatchTxRequesterLibP2PService } from '../reqresp/batch-tx-requester/interface.js';
13
- import type { TxCollectionConfig } from './config.js';
14
- import {
15
- BatchTxRequesterCollector,
16
- type MissingTxsCollector,
17
- SendBatchRequestCollector,
18
- } from './proposal_tx_collector.js';
19
- import { RequestTracker } from './request_tracker.js';
20
- import type { FastCollectionRequest, FastCollectionRequestInput } from './tx_collection.js';
21
- import type { TxAddContext, TxCollectionSink } from './tx_collection_sink.js';
22
- import type { TxSource } from './tx_source.js';
23
-
24
- export class FastTxCollection {
25
- // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
26
- protected requests: Set<FastCollectionRequest> = new Set();
27
- private missingTxsCollector: MissingTxsCollector;
28
-
29
- constructor(
30
- p2pService: BatchTxRequesterLibP2PService,
31
- private nodes: TxSource[],
32
- private txCollectionSink: TxCollectionSink,
33
- private config: TxCollectionConfig,
34
- private dateProvider: DateProvider = new DateProvider(),
35
- private log: Logger = createLogger('p2p:tx_collection_service'),
36
- missingTxsCollector?: MissingTxsCollector,
37
- ) {
38
- const batchTxRequesterConfig = this.config as Partial<BatchTxRequesterConfig>;
39
- const missingTxsCollectorType = this.config.txCollectionMissingTxsCollectorType;
40
- this.missingTxsCollector =
41
- missingTxsCollector ??
42
- (missingTxsCollectorType === 'old'
43
- ? new SendBatchRequestCollector(p2pService)
44
- : new BatchTxRequesterCollector(p2pService, log, dateProvider, undefined, batchTxRequesterConfig));
45
- }
46
-
47
- public async stop() {
48
- this.requests.forEach(request => {
49
- request.requestTracker.cancel();
50
- });
51
- await Promise.resolve();
52
- }
53
-
54
- public getFastCollectionRequests() {
55
- return this.requests;
56
- }
57
-
58
- public async collectFastFor(
59
- input: FastCollectionRequestInput,
60
- txHashes: TxHash[] | string[],
61
- opts: { deadline: Date; pinnedPeer?: PeerId },
62
- ) {
63
- const timeout = opts.deadline.getTime() - this.dateProvider.now();
64
- if (timeout <= 0) {
65
- this.log.warn(`Deadline for fast tx collection is in the past (${timeout}ms)`, {
66
- deadline: opts.deadline.getTime(),
67
- now: this.dateProvider.now(),
68
- });
69
- return [];
70
- }
71
-
72
- const blockInfo: L2BlockInfo =
73
- input.type === 'proposal'
74
- ? { ...input.blockProposal.toBlockInfo(), blockNumber: input.blockNumber }
75
- : { ...input.block.toBlockInfo() };
76
-
77
- const request: FastCollectionRequest = {
78
- ...input,
79
- blockInfo,
80
- requestTracker: RequestTracker.create(txHashes, opts.deadline, this.dateProvider),
81
- };
82
-
83
- const [duration] = await elapsed(() => this.collectFast(request, { ...opts }));
84
-
85
- this.log.verbose(
86
- `Collected ${request.requestTracker.collectedTxs.length} txs out of ${txHashes.length} for ${input.type} at slot ${blockInfo.slotNumber}`,
87
- {
88
- ...blockInfo,
89
- duration,
90
- requestType: input.type,
91
- missingTxs: [...request.requestTracker.missingTxHashes],
92
- },
93
- );
94
- return request.requestTracker.collectedTxs;
95
- }
96
-
97
- protected async collectFast(request: FastCollectionRequest, opts: { pinnedPeer?: PeerId }) {
98
- this.requests.add(request);
99
- const { blockInfo } = request;
100
-
101
- this.log.debug(
102
- `Starting fast collection of ${request.requestTracker.numberOfMissingTxs} txs for ${request.type} at slot ${blockInfo.slotNumber}`,
103
- { ...blockInfo, requestType: request.type, deadline: request.requestTracker.deadline },
104
- );
105
-
106
- try {
107
- // Start blasting all nodes for the txs. We give them a little time to respond before we start reqresp.
108
- // We race against the cancellation token to exit as soon as all txs are collected, the deadline expires,
109
- // or the request is externally cancelled.
110
- const nodeCollectionPromise = this.collectFastFromNodes(request);
111
- const waitBeforeReqResp = sleep(this.config.txCollectionFastNodesTimeoutBeforeReqRespMs);
112
- await Promise.race([request.requestTracker.cancellationToken, waitBeforeReqResp]);
113
-
114
- // If we have collected all txs or the request was cancelled, we can stop here.
115
- // Wait for node collection to settle so inner tasks finish before we return.
116
- if (request.requestTracker.checkCancelled()) {
117
- if (request.requestTracker.allFetched()) {
118
- this.log.debug(`All txs collected for slot ${blockInfo.slotNumber} without reqresp`, blockInfo);
119
- }
120
- await nodeCollectionPromise;
121
- return;
122
- }
123
-
124
- // Start blasting reqresp for the remaining txs. Note that node collection keeps running in parallel.
125
- // We stop when we have collected all txs, timed out, or both node collection and reqresp have given up.
126
- // Inner tasks observe requestTracker.checkCancelled() and stop themselves, so this settles shortly after cancellation.
127
- await Promise.allSettled([this.collectFastViaReqResp(request, opts), nodeCollectionPromise]);
128
- } catch (err) {
129
- this.log.error(`Error collecting txs for ${request.type} for slot ${blockInfo.slotNumber}`, err, {
130
- ...blockInfo,
131
- missingTxs: request.requestTracker.missingTxHashes.values().map(txHash => txHash.toString()),
132
- });
133
- } finally {
134
- // Ensure no unresolved promises and remove the request from the set
135
- request.requestTracker.cancel();
136
- this.requests.delete(request);
137
- }
138
- }
139
-
140
- /**
141
- * Starts collecting txs from all configured nodes. We send `txCollectionFastMaxParallelRequestsPerNode` requests
142
- * in parallel to each node. We keep track of the number of attempts made to collect each tx, so we can prioritize
143
- * the txs that have been requested less often whenever we need to send a new batch of requests. We ensure that no
144
- * tx is requested more than once at the same time to the same node.
145
- */
146
- private async collectFastFromNodes(request: FastCollectionRequest): Promise<void> {
147
- if (this.nodes.length === 0) {
148
- return;
149
- }
150
-
151
- // Keep a shared priority queue of all txs pending to be requested, sorted by the number of attempts made to collect them.
152
- const attemptsPerTx = [...request.requestTracker.missingTxHashes].map(txHash => ({
153
- txHash,
154
- attempts: 0,
155
- found: false,
156
- }));
157
-
158
- // Returns once we have finished all node loops. Each loop finishes when the deadline is hit, or all txs have been collected.
159
- await Promise.allSettled(this.nodes.map(node => this.collectFastFromNode(request, node, attemptsPerTx)));
160
- }
161
-
162
- private async collectFastFromNode(
163
- request: FastCollectionRequest,
164
- node: TxSource,
165
- attemptsPerTx: { txHash: string; attempts: number; found: boolean }[],
166
- ) {
167
- const notFinished = () => !request.requestTracker.checkCancelled();
168
-
169
- const maxParallelRequests = this.config.txCollectionFastMaxParallelRequestsPerNode;
170
- const maxBatchSize = this.config.txCollectionNodeRpcMaxBatchSize;
171
- const activeRequestsToThisNode = new Set<string>(); // Track the txs being actively requested to this node
172
-
173
- const processBatch = async () => {
174
- while (notFinished()) {
175
- // Pull tx hashes from the attemptsPerTx array, which is sorted by attempts,
176
- // so we prioritize txs that have been requested less often.
177
- const batch = [];
178
- let index = 0;
179
- while (batch.length < maxBatchSize) {
180
- const txToRequest = attemptsPerTx[index++];
181
- if (!txToRequest) {
182
- // No more txs to process
183
- break;
184
- } else if (!request.requestTracker.isMissing(txToRequest.txHash)) {
185
- // Mark as found if it was found somewhere else, we'll then remove it from the array.
186
- // We don't delete it now since 'array.splice' is pretty expensive, so we do it after sorting.
187
- txToRequest.found = true;
188
- } else if (!activeRequestsToThisNode.has(txToRequest.txHash)) {
189
- // If the tx is not alredy being requested to this node, add it to the current batch and increase attempts.
190
- // Note that we increase the attempts *before* making the request, so the next `collectFastFromNode` that
191
- // needs to grab txs to send, will pick txs that have been requested less often, instead of all requesting
192
- // the same txs at the same time.
193
- batch.push(txToRequest);
194
- activeRequestsToThisNode.add(txToRequest.txHash);
195
- txToRequest.attempts++;
196
- }
197
- }
198
-
199
- // After modifying the array by removing txs or updating attempts, re-sort it and trim the found txs from the end.
200
- attemptsPerTx.sort((a, b) =>
201
- a.found === b.found ? a.attempts - b.attempts : Number(a.found) - Number(b.found),
202
- );
203
- const firstFoundTxIndex = attemptsPerTx.findIndex(tx => tx.found);
204
- if (firstFoundTxIndex !== -1) {
205
- attemptsPerTx.length = firstFoundTxIndex;
206
- }
207
-
208
- // If we see no more txs to request, we can stop this "process" loop
209
- if (batch.length === 0) {
210
- return;
211
- }
212
-
213
- const txHashes = batch.map(({ txHash }) => txHash);
214
- // Collect this batch from the node
215
- await this.txCollectionSink.collect(
216
- async () => {
217
- const result = await node.getTxsByHash(txHashes.map(TxHash.fromString));
218
- for (const tx of result.validTxs) {
219
- request.requestTracker.markFetched(tx);
220
- }
221
- return result;
222
- },
223
- txHashes,
224
- {
225
- description: `fast ${node.getInfo()}`,
226
- node: node.getInfo(),
227
- method: 'fast-node-rpc',
228
- ...request.blockInfo,
229
- },
230
- this.getAddContext(request),
231
- );
232
-
233
- // Clear from the active requests the txs we just requested
234
- for (const requestedTx of batch) {
235
- activeRequestsToThisNode.delete(requestedTx.txHash);
236
- }
237
-
238
- // Sleep a bit until hitting the node again, but wake up immediately on cancellation
239
- if (notFinished()) {
240
- await Promise.race([
241
- sleep(this.config.txCollectionFastNodeIntervalMs),
242
- request.requestTracker.cancellationToken,
243
- ]);
244
- }
245
- }
246
- };
247
-
248
- // Kick off N parallel requests to the node, up to the maxParallelRequests limit
249
- await Promise.all(times(maxParallelRequests, processBatch));
250
- }
251
-
252
- private async collectFastViaReqResp(request: FastCollectionRequest, opts: { pinnedPeer?: PeerId }) {
253
- const pinnedPeer = opts.pinnedPeer;
254
- const blockInfo = request.blockInfo;
255
- const slotNumber = blockInfo.slotNumber;
256
- if (request.requestTracker.timeoutMs < 100) {
257
- this.log.warn(
258
- `Not initiating fast reqresp for txs for ${request.type} at slot ${blockInfo.slotNumber} due to timeout`,
259
- { timeoutMs: request.requestTracker.timeoutMs, ...blockInfo },
260
- );
261
- return;
262
- }
263
-
264
- this.log.debug(
265
- `Starting fast reqresp for ${request.requestTracker.numberOfMissingTxs} txs for ${request.type} at slot ${blockInfo.slotNumber}`,
266
- { ...blockInfo, timeoutMs: request.requestTracker.timeoutMs, pinnedPeer },
267
- );
268
-
269
- try {
270
- await this.txCollectionSink.collect(
271
- async () => {
272
- let result: Tx[];
273
- if (request.type === 'proposal') {
274
- result = await this.missingTxsCollector.collectTxs(
275
- request.requestTracker,
276
- request.blockProposal,
277
- pinnedPeer,
278
- );
279
- } else if (request.type === 'block') {
280
- const blockTxsSource = {
281
- txHashes: request.block.body.txEffects.map(e => e.txHash),
282
- archive: request.block.archive.root,
283
- };
284
- result = await this.missingTxsCollector.collectTxs(request.requestTracker, blockTxsSource, pinnedPeer);
285
- } else {
286
- throw new Error(`Unknown request type: ${(request as any).type}`);
287
- }
288
- return { validTxs: result, invalidTxHashes: [] };
289
- },
290
- Array.from(request.requestTracker.missingTxHashes),
291
- { description: `reqresp for slot ${slotNumber}`, method: 'fast-req-resp', ...opts, ...request.blockInfo },
292
- this.getAddContext(request),
293
- );
294
- } catch (err) {
295
- this.log.error(`Error sending fast reqresp request for txs`, err, {
296
- txs: [...request.requestTracker.missingTxHashes],
297
- ...blockInfo,
298
- });
299
- }
300
- }
301
-
302
- /** Returns the TxAddContext for the given request, used by the sink to add txs to the pool correctly. */
303
- private getAddContext(request: FastCollectionRequest): TxAddContext {
304
- if (request.type === 'proposal') {
305
- return { type: 'proposal', blockHeader: request.blockProposal.blockHeader };
306
- } else {
307
- return { type: 'mined', block: request.block };
308
- }
309
- }
310
-
311
- /**
312
- * Handle txs by marking them as found for the requests that are waiting for them, and resolves the request if all its txs have been found.
313
- * Called internally and from the main tx collection manager whenever the tx pool emits a tx-added event.
314
- */
315
- public foundTxs(txs: Tx[]) {
316
- for (const request of this.requests) {
317
- for (const tx of txs) {
318
- const txHash = tx.txHash.toString();
319
- // Remove the tx hash from the missing set, and add it to the found set.
320
- if (request.requestTracker.markFetched(tx)) {
321
- this.log.trace(`Found tx ${txHash} for fast collection request`, {
322
- ...request.blockInfo,
323
- txHash: tx.txHash.toString(),
324
- type: request.type,
325
- });
326
- if (request.requestTracker.allFetched()) {
327
- this.log.trace(`All txs found for fast collection request`, {
328
- ...request.blockInfo,
329
- type: request.type,
330
- });
331
- break;
332
- }
333
- }
334
- }
335
- }
336
- }
337
-
338
- /**
339
- * Stop collecting all txs for blocks less than or requal to the block number specified.
340
- * To be called when we no longer care about gathering txs up to a certain block, eg when they become proven or finalized.
341
- */
342
- public stopCollectingForBlocksUpTo(blockNumber: BlockNumber): void {
343
- for (const request of this.requests) {
344
- if (request.blockInfo.blockNumber <= blockNumber) {
345
- request.requestTracker.cancel();
346
- }
347
- }
348
- }
349
-
350
- /**
351
- * Stop collecting all txs for blocks greater than the block number specified.
352
- * To be called when there is a chain prune and previously mined txs are no longer relevant.
353
- */
354
- public stopCollectingForBlocksAfter(blockNumber: BlockNumber): void {
355
- for (const request of this.requests) {
356
- if (request.blockInfo.blockNumber > blockNumber) {
357
- request.requestTracker.cancel();
358
- }
359
- }
360
- }
361
- }