@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,12 +1,14 @@
1
1
  // @attribution: lodestar impl for inspiration
2
- import { type Logger, createLogger } from '@aztec/foundation/log';
2
+ import { compactArray } from '@aztec/foundation/collection';
3
+ import { AbortError, TimeoutError } from '@aztec/foundation/error';
4
+ import { createLogger } from '@aztec/foundation/log';
3
5
  import { executeTimeout } from '@aztec/foundation/timer';
4
6
  import { PeerErrorSeverity } from '@aztec/stdlib/p2p';
5
7
  import { Attributes, type TelemetryClient, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
6
8
 
7
9
  import type { IncomingStreamData, PeerId, Stream } from '@libp2p/interface';
8
- import { pipe } from 'it-pipe';
9
10
  import type { Libp2p } from 'libp2p';
11
+ import { pipeline } from 'node:stream/promises';
10
12
  import type { Uint8ArrayList } from 'uint8arraylist';
11
13
 
12
14
  import {
@@ -16,18 +18,24 @@ import {
16
18
  } from '../../errors/reqresp.error.js';
17
19
  import { SnappyTransform } from '../encoding.js';
18
20
  import type { PeerScoring } from '../peer-manager/peer_scoring.js';
19
- import type { P2PReqRespConfig } from './config.js';
21
+ import {
22
+ DEFAULT_INDIVIDUAL_REQUEST_TIMEOUT_MS,
23
+ DEFAULT_REQRESP_DIAL_TIMEOUT_MS,
24
+ type P2PReqRespConfig,
25
+ } from './config.js';
20
26
  import { BatchConnectionSampler } from './connection-sampler/batch_connection_sampler.js';
21
- import { ConnectionSampler } from './connection-sampler/connection_sampler.js';
27
+ import { ConnectionSampler, RandomSampler } from './connection-sampler/connection_sampler.js';
22
28
  import {
23
- DEFAULT_SUB_PROTOCOL_HANDLERS,
24
29
  DEFAULT_SUB_PROTOCOL_VALIDATORS,
30
+ type ReqRespInterface,
25
31
  type ReqRespResponse,
26
32
  ReqRespSubProtocol,
33
+ type ReqRespSubProtocolHandler,
27
34
  type ReqRespSubProtocolHandlers,
35
+ type ReqRespSubProtocolRateLimits,
28
36
  type ReqRespSubProtocolValidators,
29
37
  type SubProtocolMap,
30
- subProtocolMap,
38
+ responseFromBuffer,
31
39
  } from './interface.js';
32
40
  import { ReqRespMetrics } from './metrics.js';
33
41
  import {
@@ -51,15 +59,12 @@ import { ReqRespStatus, ReqRespStatusError, parseStatusChunk, prettyPrintReqResp
51
59
  *
52
60
  * see: https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/p2p-interface.md#the-reqresp-domain
53
61
  */
54
- export class ReqResp {
55
- protected readonly logger: Logger;
56
-
57
- private overallRequestTimeoutMs: number;
58
- private individualRequestTimeoutMs: number;
62
+ export class ReqResp implements ReqRespInterface {
63
+ private individualRequestTimeoutMs: number = DEFAULT_INDIVIDUAL_REQUEST_TIMEOUT_MS;
64
+ private dialTimeoutMs: number = DEFAULT_REQRESP_DIAL_TIMEOUT_MS;
59
65
 
60
- // Warning, if the `start` function is not called as the parent class constructor, then the default sub protocol handlers will be used ( not good )
61
- private subProtocolHandlers: ReqRespSubProtocolHandlers = DEFAULT_SUB_PROTOCOL_HANDLERS;
62
- private subProtocolValidators: ReqRespSubProtocolValidators = DEFAULT_SUB_PROTOCOL_VALIDATORS;
66
+ private subProtocolHandlers: Partial<ReqRespSubProtocolHandlers> = {};
67
+ private subProtocolValidators: Partial<ReqRespSubProtocolValidators> = {};
63
68
 
64
69
  private connectionSampler: ConnectionSampler;
65
70
  private rateLimiter: RequestResponseRateLimiter;
@@ -72,22 +77,36 @@ export class ReqResp {
72
77
  config: P2PReqRespConfig,
73
78
  private libp2p: Libp2p,
74
79
  private peerScoring: PeerScoring,
80
+ private logger = createLogger('p2p:reqresp'),
81
+ rateLimits: Partial<ReqRespSubProtocolRateLimits> = {},
75
82
  telemetryClient: TelemetryClient = getTelemetryClient(),
76
83
  ) {
77
- this.logger = createLogger('p2p:reqresp');
84
+ this.updateConfig(config);
78
85
 
79
- this.overallRequestTimeoutMs = config.overallRequestTimeoutMs;
80
- this.individualRequestTimeoutMs = config.individualRequestTimeoutMs;
81
-
82
- this.rateLimiter = new RequestResponseRateLimiter(peerScoring);
86
+ this.rateLimiter = new RequestResponseRateLimiter(peerScoring, rateLimits);
83
87
 
84
88
  // Connection sampler is used to sample our connected peers
85
- this.connectionSampler = new ConnectionSampler(libp2p);
89
+ this.connectionSampler = new ConnectionSampler(
90
+ libp2p,
91
+ new RandomSampler(),
92
+ createLogger(`${logger.module}:connection-sampler`),
93
+ config,
94
+ );
86
95
 
87
96
  this.snappyTransform = new SnappyTransform();
88
97
  this.metrics = new ReqRespMetrics(telemetryClient);
89
98
  }
90
99
 
100
+ public updateConfig(config: Partial<P2PReqRespConfig>): void {
101
+ if (typeof config.individualRequestTimeoutMs === 'number') {
102
+ this.individualRequestTimeoutMs = config.individualRequestTimeoutMs;
103
+ }
104
+
105
+ if (typeof config.dialTimeoutMs === 'number') {
106
+ this.dialTimeoutMs = config.dialTimeoutMs;
107
+ }
108
+ }
109
+
91
110
  get tracer() {
92
111
  return this.metrics.tracer;
93
112
  }
@@ -96,11 +115,12 @@ export class ReqResp {
96
115
  * Start the reqresp service
97
116
  */
98
117
  async start(subProtocolHandlers: ReqRespSubProtocolHandlers, subProtocolValidators: ReqRespSubProtocolValidators) {
99
- this.subProtocolHandlers = subProtocolHandlers;
100
- this.subProtocolValidators = subProtocolValidators;
118
+ Object.assign(this.subProtocolHandlers, subProtocolHandlers);
119
+ Object.assign(this.subProtocolValidators, subProtocolValidators);
101
120
 
102
121
  // Register all protocol handlers
103
- for (const subProtocol of Object.keys(this.subProtocolHandlers)) {
122
+ for (const subProtocol of Object.keys(subProtocolHandlers)) {
123
+ this.logger.debug(`Registering handler for sub protocol ${subProtocol}`);
104
124
  await this.libp2p.handle(
105
125
  subProtocol,
106
126
  (data: IncomingStreamData) =>
@@ -112,6 +132,23 @@ export class ReqResp {
112
132
  this.rateLimiter.start();
113
133
  }
114
134
 
135
+ async addSubProtocol(
136
+ subProtocol: ReqRespSubProtocol,
137
+ handler: ReqRespSubProtocolHandler,
138
+ validator: ReqRespSubProtocolValidators[ReqRespSubProtocol] = DEFAULT_SUB_PROTOCOL_VALIDATORS[subProtocol],
139
+ ): Promise<void> {
140
+ this.subProtocolHandlers[subProtocol] = handler;
141
+ this.subProtocolValidators[subProtocol] = validator;
142
+ this.logger.debug(`Registering handler for sub protocol ${subProtocol}`);
143
+ await this.libp2p.handle(
144
+ subProtocol,
145
+ (data: IncomingStreamData) =>
146
+ void this.streamHandler(subProtocol as ReqRespSubProtocol, data).catch(err =>
147
+ this.logger.error(`Error on libp2p subprotocol ${subProtocol} handler`, err),
148
+ ),
149
+ );
150
+ }
151
+
115
152
  /**
116
153
  * Stop the reqresp service
117
154
  */
@@ -135,100 +172,6 @@ export class ReqResp {
135
172
  // NOTE: We assume libp2p instance is managed by the caller
136
173
  }
137
174
 
138
- /**
139
- * Send a request to peers, returns the first response
140
- *
141
- * @param subProtocol - The protocol being requested
142
- * @param request - The request to send
143
- * @returns - The response from the peer, otherwise undefined
144
- *
145
- * @description
146
- * This method attempts to send a request to all active peers using the specified sub-protocol.
147
- * It opens a stream with each peer, sends the request, and awaits a response.
148
- * If a valid response is received, it returns the response; otherwise, it continues to the next peer.
149
- * If no response is received from any peer, it returns undefined.
150
- *
151
- * The method performs the following steps:
152
- * - Sample a peer to send the request to.
153
- * - Opens a stream with the peer using the specified sub-protocol.
154
- *
155
- * When a response is received, it is validated using the given sub protocols response validator.
156
- * To see the interface for the response validator - see `interface.ts`
157
- *
158
- * Failing a response validation requests in a severe peer penalty, and will
159
- * prompt the node to continue to search to the next peer.
160
- * For example, a transaction request validator will check that the payload returned does in fact
161
- * match the txHash that was requested. A peer that fails this check an only be an extremely naughty peer.
162
- *
163
- * This entire operation is wrapped in an overall timeout, that is independent of the
164
- * peer it is requesting data from.
165
- *
166
- */
167
- async sendRequest<SubProtocol extends ReqRespSubProtocol>(
168
- subProtocol: SubProtocol,
169
- request: InstanceType<SubProtocolMap[SubProtocol]['request']>,
170
- ): Promise<InstanceType<SubProtocolMap[SubProtocol]['response']> | undefined> {
171
- const responseValidator = this.subProtocolValidators[subProtocol];
172
- const requestBuffer = request.toBuffer();
173
-
174
- const requestFunction = async () => {
175
- // Attempt to ask all of our peers, but sampled in a random order
176
- // This function is wrapped in a timeout, so we will exit the loop if we have not received a response
177
- const numberOfPeers = this.libp2p.getPeers().length;
178
-
179
- if (numberOfPeers === 0) {
180
- this.logger.debug('No active peers to send requests to');
181
- return undefined;
182
- }
183
-
184
- const attemptedPeers: Map<string, boolean> = new Map();
185
- for (let i = 0; i < numberOfPeers; i++) {
186
- // Sample a peer to make a request to
187
- const peer = this.connectionSampler.getPeer(attemptedPeers);
188
- this.logger.trace(`Attempting to send request to peer: ${peer?.toString()}`);
189
- if (!peer) {
190
- this.logger.debug('No peers available to send requests to');
191
- return undefined;
192
- }
193
-
194
- attemptedPeers.set(peer.toString(), true);
195
-
196
- this.logger.trace(`Sending request to peer: ${peer.toString()}`);
197
- const response = await this.sendRequestToPeer(peer, subProtocol, requestBuffer);
198
-
199
- if (response && response.status !== ReqRespStatus.SUCCESS) {
200
- this.logger.debug(
201
- `Request to peer ${peer.toString()} failed with status ${prettyPrintReqRespStatus(response.status)}`,
202
- );
203
- continue;
204
- }
205
-
206
- // If we get a response, return it, otherwise we iterate onto the next peer
207
- // We do not consider it a success if we have an empty buffer
208
- if (response && response.data.length > 0) {
209
- const object = subProtocolMap[subProtocol].response.fromBuffer(response.data);
210
- // The response validator handles peer punishment within
211
- const isValid = await responseValidator(request, object, peer);
212
- if (!isValid) {
213
- throw new InvalidResponseError();
214
- }
215
- return object;
216
- }
217
- }
218
- };
219
-
220
- try {
221
- return await executeTimeout<InstanceType<SubProtocolMap[SubProtocol]['response']> | undefined>(
222
- requestFunction,
223
- this.overallRequestTimeoutMs,
224
- () => new CollectiveReqRespTimeoutError(),
225
- );
226
- } catch (e: any) {
227
- this.logger.debug(`${e.message} | subProtocol: ${subProtocol}`);
228
- return undefined;
229
- }
230
- }
231
-
232
175
  /**
233
176
  * Request multiple messages over the same sub protocol, balancing the requests across peers.
234
177
  *
@@ -261,23 +204,30 @@ export class ReqResp {
261
204
  async sendBatchRequest<SubProtocol extends ReqRespSubProtocol>(
262
205
  subProtocol: SubProtocol,
263
206
  requests: InstanceType<SubProtocolMap[SubProtocol]['request']>[],
207
+ pinnedPeer: PeerId | undefined,
264
208
  timeoutMs = 10000,
265
- maxPeers = Math.min(10, requests.length),
209
+ maxPeers = Math.max(10, Math.ceil(requests.length / 3)),
266
210
  maxRetryAttempts = 3,
267
211
  ): Promise<InstanceType<SubProtocolMap[SubProtocol]['response']>[]> {
268
- const responseValidator = this.subProtocolValidators[subProtocol];
212
+ const responseValidator = this.subProtocolValidators[subProtocol] ?? DEFAULT_SUB_PROTOCOL_VALIDATORS[subProtocol];
269
213
  const responses: InstanceType<SubProtocolMap[SubProtocol]['response']>[] = new Array(requests.length);
270
214
  const requestBuffers = requests.map(req => req.toBuffer());
271
215
 
272
- const requestFunction = async () => {
216
+ const requestFunction = async (signal: AbortSignal) => {
273
217
  // Track which requests still need to be processed
274
218
  const pendingRequestIndices = new Set(requestBuffers.map((_, i) => i));
275
219
 
276
220
  // Create batch sampler with the total number of requests and max peers
277
- const batchSampler = new BatchConnectionSampler(this.connectionSampler, requests.length, maxPeers);
221
+ const batchSampler = new BatchConnectionSampler(
222
+ this.connectionSampler,
223
+ requests.length,
224
+ maxPeers,
225
+ compactArray([pinnedPeer]), // Exclude pinned peer from sampling, we will forcefully send all requests to it
226
+ createLogger(`${this.logger.module}:batch-connection-sampler`),
227
+ );
278
228
 
279
- if (batchSampler.activePeerCount === 0) {
280
- this.logger.debug('No active peers to send requests to');
229
+ if (batchSampler.activePeerCount === 0 && !pinnedPeer) {
230
+ this.logger.warn('No active peers to send requests to');
281
231
  return [];
282
232
  }
283
233
 
@@ -291,8 +241,12 @@ export class ReqResp {
291
241
 
292
242
  let retryAttempts = 0;
293
243
  while (pendingRequestIndices.size > 0 && batchSampler.activePeerCount > 0 && retryAttempts < maxRetryAttempts) {
244
+ if (signal.aborted) {
245
+ throw new AbortError('Batch request aborted');
246
+ }
294
247
  // Process requests in parallel for each available peer
295
- const requestBatches = new Map<PeerId, number[]>();
248
+ type BatchEntry = { peerId: PeerId; indices: number[] };
249
+ const requestBatches = new Map<string, BatchEntry>();
296
250
 
297
251
  // Group requests by peer
298
252
  for (const requestIndex of pendingRequestIndices) {
@@ -300,11 +254,21 @@ export class ReqResp {
300
254
  if (!peer) {
301
255
  break;
302
256
  }
303
-
304
- if (!requestBatches.has(peer)) {
305
- requestBatches.set(peer, []);
257
+ const peerAsString = peer.toString();
258
+ if (!requestBatches.has(peerAsString)) {
259
+ requestBatches.set(peerAsString, { peerId: peer, indices: [] });
306
260
  }
307
- requestBatches.get(peer)!.push(requestIndex);
261
+ requestBatches.get(peerAsString)!.indices.push(requestIndex);
262
+ }
263
+
264
+ // If there is a pinned peer, we will always send every request to that peer
265
+ // We use the default limits for the subprotocol to avoid hitting the rate limiter
266
+ if (pinnedPeer) {
267
+ const limit = this.rateLimiter.getRateLimits(subProtocol).peerLimit.quotaCount;
268
+ requestBatches.set(pinnedPeer.toString(), {
269
+ peerId: pinnedPeer,
270
+ indices: Array.from(pendingRequestIndices.values()).slice(0, limit),
271
+ });
308
272
  }
309
273
 
310
274
  // Make parallel requests for each peer's batch
@@ -316,20 +280,19 @@ export class ReqResp {
316
280
  // while simultaneously Peer Id 1 will send requests 4, 5, 6, 7 in serial
317
281
 
318
282
  const batchResults = await Promise.all(
319
- Array.from(requestBatches.entries()).map(async ([peer, indices]) => {
283
+ Array.from(requestBatches.entries()).map(async ([peerAsString, { peerId: peer, indices }]) => {
320
284
  try {
321
285
  // Requests all going to the same peer are sent synchronously
322
286
  const peerResults: { index: number; response: InstanceType<SubProtocolMap[SubProtocol]['response']> }[] =
323
287
  [];
324
288
  for (const index of indices) {
289
+ this.logger.trace(`Sending request ${index} to peer ${peerAsString}`);
325
290
  const response = await this.sendRequestToPeer(peer, subProtocol, requestBuffers[index]);
326
291
 
327
292
  // Check the status of the response buffer
328
- if (response && response.status !== ReqRespStatus.SUCCESS) {
293
+ if (response.status !== ReqRespStatus.SUCCESS) {
329
294
  this.logger.debug(
330
- `Request to peer ${peer.toString()} failed with status ${prettyPrintReqRespStatus(
331
- response.status,
332
- )}`,
295
+ `Request to peer ${peerAsString} failed with status ${prettyPrintReqRespStatus(response.status)}`,
333
296
  );
334
297
 
335
298
  // If we hit a rate limit or some failure, we remove the peer and return the results,
@@ -339,7 +302,7 @@ export class ReqResp {
339
302
  }
340
303
 
341
304
  if (response && response.data.length > 0) {
342
- const object = subProtocolMap[subProtocol].response.fromBuffer(response.data);
305
+ const object = responseFromBuffer(subProtocol, response.data);
343
306
  const isValid = await responseValidator(requests[index], object, peer);
344
307
 
345
308
  if (isValid) {
@@ -350,7 +313,7 @@ export class ReqResp {
350
313
 
351
314
  return { peer, results: peerResults };
352
315
  } catch (error) {
353
- this.logger.debug(`Failed batch request to peer ${peer.toString()}:`, error);
316
+ this.logger.debug(`Failed batch request to peer ${peerAsString}:`, error);
354
317
  batchSampler.removePeerAndReplace(peer);
355
318
  return { peer, results: [] };
356
319
  }
@@ -399,6 +362,7 @@ export class ReqResp {
399
362
  * @param peerId - The peer to send the request to
400
363
  * @param subProtocol - The protocol to use to request
401
364
  * @param payload - The payload to send
365
+ * @param dialTimeout - If establishing a stream takes longer than this an error will be thrown
402
366
  * @returns If the request is successful, the response is returned, otherwise undefined
403
367
  *
404
368
  * @description
@@ -421,29 +385,52 @@ export class ReqResp {
421
385
  peerId: PeerId,
422
386
  subProtocol: ReqRespSubProtocol,
423
387
  payload: Buffer,
424
- ): Promise<ReqRespResponse | undefined> {
388
+ dialTimeout: number = this.dialTimeoutMs,
389
+ ): Promise<ReqRespResponse> {
425
390
  let stream: Stream | undefined;
426
391
  try {
427
392
  this.metrics.recordRequestSent(subProtocol);
428
393
 
429
- stream = await this.connectionSampler.dialProtocol(peerId, subProtocol);
394
+ this.logger.trace(`Sending request to peer ${peerId.toString()} on sub protocol ${subProtocol}`);
395
+ stream = await this.connectionSampler.dialProtocol(peerId, subProtocol, dialTimeout);
396
+ this.logger.trace(
397
+ `Opened stream ${stream.id} for sending request to peer ${peerId.toString()} on sub protocol ${subProtocol}`,
398
+ );
430
399
 
431
- // Open the stream with a timeout
432
- const result = await executeTimeout<ReqRespResponse>(
433
- (): Promise<ReqRespResponse> => pipe([payload], stream!, this.readMessage.bind(this)),
400
+ const timeoutErr = new IndividualReqRespTimeoutError();
401
+ const [_, resp] = await executeTimeout(
402
+ signal =>
403
+ Promise.all([
404
+ pipeline([payload], stream!.sink, { signal }),
405
+ pipeline(stream!.source, this.readMessage.bind(this), { signal }),
406
+ ]),
434
407
  this.individualRequestTimeoutMs,
435
- () => new IndividualReqRespTimeoutError(),
408
+ () => timeoutErr,
436
409
  );
437
-
438
- return result;
410
+ return resp;
439
411
  } catch (e: any) {
412
+ // On error we immediately abort the stream, this is preferred way,
413
+ // because it signals to the sender that error happened, whereas
414
+ // closing the stream only closes our side and is much slower
415
+ if (stream) {
416
+ stream!.abort(e);
417
+ }
418
+
440
419
  this.metrics.recordRequestError(subProtocol);
441
420
  this.handleResponseError(e, peerId, subProtocol);
421
+
422
+ // If there is an exception, we return an unknown response
423
+ this.logger.debug(`Error sending request to peer ${peerId.toString()} on sub protocol ${subProtocol}: ${e}`);
424
+ return { status: ReqRespStatus.FAILURE };
442
425
  } finally {
443
426
  // Only close the stream if we created it
427
+ // Note even if we aborted the stream, calling close on it is ok, it's just a no-op
444
428
  if (stream) {
445
429
  try {
446
- await this.connectionSampler.close(stream.id);
430
+ this.logger.trace(
431
+ `Closing stream ${stream.id} for request to peer ${peerId.toString()} on sub protocol ${subProtocol}`,
432
+ );
433
+ await this.connectionSampler.close(stream);
447
434
  } catch (closeError) {
448
435
  this.logger.error(
449
436
  `Error closing stream: ${closeError instanceof Error ? closeError.message : 'Unknown error'}`,
@@ -464,67 +451,12 @@ export class ReqResp {
464
451
  * @returns If the error is non pubishable, then undefined is returned, otherwise the peer is penalized
465
452
  */
466
453
  private handleResponseError(e: any, peerId: PeerId, subProtocol: ReqRespSubProtocol): void {
467
- const severity = this.categorizeError(e, peerId, subProtocol);
454
+ const severity = this.categorizeResponseError(e, peerId, subProtocol);
468
455
  if (severity) {
469
456
  this.peerScoring.penalizePeer(peerId, severity);
470
457
  }
471
458
  }
472
459
 
473
- /**
474
- * Categorize the error and log it.
475
- */
476
- private categorizeError(e: any, peerId: PeerId, subProtocol: ReqRespSubProtocol): PeerErrorSeverity | undefined {
477
- // Non punishable errors - we do not expect a response for goodbye messages
478
- if (subProtocol === ReqRespSubProtocol.GOODBYE) {
479
- this.logger.debug('Error encountered on goodbye sub protocol, no penalty', {
480
- peerId: peerId.toString(),
481
- subProtocol,
482
- });
483
- return undefined;
484
- }
485
-
486
- // We do not punish a collective timeout, as the node triggers this interupt, independent of the peer's behaviour
487
- const logTags = {
488
- peerId: peerId.toString(),
489
- subProtocol,
490
- };
491
- if (e instanceof CollectiveReqRespTimeoutError || e instanceof InvalidResponseError) {
492
- this.logger.debug(
493
- `Non-punishable error: ${e.message} | peerId: ${peerId.toString()} | subProtocol: ${subProtocol}`,
494
- logTags,
495
- );
496
- return undefined;
497
- }
498
-
499
- // Pubishable errors
500
- // Connection reset errors in the networking stack are punished with high severity
501
- // it just signals an unreliable peer
502
- // We assume that the requesting node has a functioning networking stack.
503
- if (e?.code === 'ECONNRESET' || e?.code === 'EPIPE') {
504
- this.logger.debug(`Connection reset: ${peerId.toString()}`, logTags);
505
- return PeerErrorSeverity.HighToleranceError;
506
- }
507
-
508
- if (e?.code === 'ECONNREFUSED') {
509
- this.logger.debug(`Connection refused: ${peerId.toString()}`, logTags);
510
- return PeerErrorSeverity.HighToleranceError;
511
- }
512
-
513
- // Timeout errors are punished with high tolerance, they can be due to a geogrpahically far away peer or an
514
- // overloaded peer
515
- if (e instanceof IndividualReqRespTimeoutError) {
516
- this.logger.debug(
517
- `Timeout error: ${e.message} | peerId: ${peerId.toString()} | subProtocol: ${subProtocol}`,
518
- logTags,
519
- );
520
- return PeerErrorSeverity.HighToleranceError;
521
- }
522
-
523
- // Catch all error
524
- this.logger.error(`Unexpected error sending request to peer`, e, logTags);
525
- return PeerErrorSeverity.HighToleranceError;
526
- }
527
-
528
460
  /**
529
461
  * Read a message returned from a stream into a single buffer
530
462
  *
@@ -533,24 +465,34 @@ export class ReqResp {
533
465
  * - The second chunk should contain the response data
534
466
  */
535
467
  private async readMessage(source: AsyncIterable<Uint8ArrayList>): Promise<ReqRespResponse> {
536
- let statusBuffer: ReqRespStatus | undefined;
468
+ let status: ReqRespStatus | undefined;
537
469
  const chunks: Uint8Array[] = [];
538
470
 
539
471
  try {
540
472
  for await (const chunk of source) {
541
- if (statusBuffer === undefined) {
542
- const firstChunkBuffer = chunk.subarray();
543
- statusBuffer = parseStatusChunk(firstChunkBuffer);
544
- } else {
473
+ const statusParsed = status !== undefined;
474
+ if (statusParsed) {
545
475
  chunks.push(chunk.subarray());
476
+ continue;
477
+ }
478
+
479
+ const firstChunkBuffer = chunk.subarray();
480
+ status = parseStatusChunk(firstChunkBuffer);
481
+
482
+ // In case status is not SUCCESS, we do not expect any data in the response
483
+ // we can return early
484
+ if (status !== ReqRespStatus.SUCCESS) {
485
+ return {
486
+ status,
487
+ };
546
488
  }
547
489
  }
548
490
 
549
491
  const messageData = Buffer.concat(chunks);
550
- const message: Buffer = this.snappyTransform.inboundTransformNoTopic(messageData);
492
+ const message: Buffer = this.snappyTransform.inboundTransformData(messageData);
551
493
 
552
494
  return {
553
- status: statusBuffer ?? ReqRespStatus.UNKNOWN,
495
+ status: status ?? ReqRespStatus.UNKNOWN,
554
496
  data: message,
555
497
  };
556
498
  } catch (e: any) {
@@ -563,7 +505,6 @@ export class ReqResp {
563
505
 
564
506
  return {
565
507
  status,
566
- data: Buffer.from([]),
567
508
  };
568
509
  }
569
510
  }
@@ -572,7 +513,8 @@ export class ReqResp {
572
513
  * Stream Handler
573
514
  * Reads the incoming stream, determines the protocol, then triggers the appropriate handler
574
515
  *
575
- * @param param0 - The incoming stream data
516
+ * @param protocol - The sub protocol to handle
517
+ * @param incomingStream - The incoming stream data containing the stream and connection
576
518
  *
577
519
  * @description
578
520
  * An individual stream handler will be bound to each sub protocol, and handles returning data back
@@ -588,14 +530,13 @@ export class ReqResp {
588
530
  [Attributes.P2P_REQ_RESP_PROTOCOL]: protocol,
589
531
  [Attributes.P2P_ID]: connection.remotePeer.toString(),
590
532
  }))
591
- private async streamHandler(protocol: ReqRespSubProtocol, { stream, connection }: IncomingStreamData) {
592
- this.metrics.recordRequestReceived(protocol);
593
-
533
+ private async streamHandler(protocol: ReqRespSubProtocol, incomingStream: IncomingStreamData) {
534
+ const { stream, connection } = incomingStream;
594
535
  try {
595
- // Store a reference to from this for the async generator
536
+ this.metrics.recordRequestReceived(protocol);
596
537
  const rateLimitStatus = this.rateLimiter.allow(protocol, connection.remotePeer);
597
- if (rateLimitStatus != RateLimitStatus.Allowed) {
598
- this.logger.warn(
538
+ if (rateLimitStatus !== RateLimitStatus.Allowed) {
539
+ this.logger.verbose(
599
540
  `Rate limit exceeded ${prettyPrintRateLimitStatus(rateLimitStatus)} for ${protocol} from ${
600
541
  connection.remotePeer
601
542
  }`,
@@ -604,58 +545,248 @@ export class ReqResp {
604
545
  throw new ReqRespStatusError(ReqRespStatus.RATE_LIMIT_EXCEEDED);
605
546
  }
606
547
 
607
- const handler = this.subProtocolHandlers[protocol];
608
- const transform = this.snappyTransform;
548
+ await this.processStream(protocol, incomingStream);
549
+ } catch (err: any) {
550
+ this.metrics.recordResponseError(protocol);
551
+ this.handleRequestError(err, connection.remotePeer, protocol);
552
+
553
+ if (err instanceof ReqRespStatusError) {
554
+ const errorSent = await this.trySendError(stream, connection.remotePeer, protocol, err.status);
555
+ const logMessage = errorSent
556
+ ? 'Protocol error sent successfully.'
557
+ : 'Stream already closed or poisoned, not sending error response.';
558
+
559
+ const isRateLimit = err.status === ReqRespStatus.RATE_LIMIT_EXCEEDED;
560
+
561
+ const level = isRateLimit ? 'debug' : 'warn';
562
+ this.logger[level](logMessage + ` Status: ${ReqRespStatus[err.status]}`, {
563
+ protocol,
564
+ err,
565
+ errorStatus: err.status,
566
+ cause: err.cause ?? 'Cause unknown',
567
+ });
568
+ } else {
569
+ // In erroneous case we abort the stream, this will signal the peer that something went wrong
570
+ // and that this stream should be dropped
571
+ const isMessageToNotWarn =
572
+ err instanceof Error &&
573
+ ['stream reset', 'Cannot push value onto an ended pushable'].some(msg => err.message.includes(msg));
574
+ const level = isMessageToNotWarn ? 'debug' : 'warn';
575
+ this.logger[level]('Unknown stream error while handling the stream, aborting', {
576
+ protocol,
577
+ err,
578
+ });
579
+
580
+ stream.abort(err);
581
+ }
582
+ } finally {
583
+ //NOTE: This is idempotent action, so it's ok to call it even if stream was aborted
584
+ await stream.close();
585
+ }
586
+ }
609
587
 
610
- await pipe(
611
- stream,
612
- async function* (source: any) {
613
- for await (const chunkList of source) {
614
- const msg = Buffer.from(chunkList.subarray());
615
- const response = await handler(connection.remotePeer, msg);
588
+ /**
589
+ * Reads incoming data from the stream, processes it according to the sub protocol,
590
+ * and puts response back into the stream.
591
+ *
592
+ * @param protocol - The sub protocol to use for processing the stream
593
+ * @param incomingStream - The incoming stream data containing the stream and connection
594
+ *
595
+ * */
596
+ private async processStream(protocol: ReqRespSubProtocol, { stream, connection }: IncomingStreamData): Promise<void> {
597
+ const handler = this.subProtocolHandlers[protocol];
598
+ if (!handler) {
599
+ throw new Error(`No handler defined for reqresp subprotocol ${protocol}`);
600
+ }
616
601
 
617
- if (protocol === ReqRespSubProtocol.GOODBYE) {
618
- // Don't respond
619
- await stream.close();
620
- return;
621
- }
602
+ const snappy = this.snappyTransform;
603
+ const SUCCESS = Uint8Array.of(ReqRespStatus.SUCCESS);
622
604
 
623
- // Send success code first, then the response
624
- const successChunk = Buffer.from([ReqRespStatus.SUCCESS]);
625
- yield new Uint8Array(successChunk);
605
+ await pipeline(
606
+ stream.source,
607
+ async function* (source: any) {
608
+ for await (const chunk of source) {
609
+ const response = await handler(connection.remotePeer, chunk.subarray());
626
610
 
627
- yield new Uint8Array(transform.outboundTransformNoTopic(response));
611
+ if (protocol === ReqRespSubProtocol.GOODBYE) {
612
+ // NOTE: The stream was already closed by Goodbye handler
613
+ // peerManager.goodbyeReceived(peerId, reason); will call libp2p.hangUp closing all active streams and connections
614
+ // Don't try to respond
615
+ return;
628
616
  }
629
- },
630
- stream,
631
- );
617
+
618
+ stream.metadata.written = true; // Mark the stream as written to;
619
+
620
+ yield SUCCESS;
621
+ yield snappy.outboundTransformData(response);
622
+ }
623
+ },
624
+ stream.sink,
625
+ );
626
+ }
627
+
628
+ /**
629
+ * Try to send error status to the peer. We say try, because the stream,
630
+ * might already be closed
631
+ * @param stream - The stream opened between us and the peer
632
+ * @param status - The error status to send back to the peer
633
+ * @returns true if error was sent successfully, otherwise false
634
+ *
635
+ */
636
+ private async trySendError(
637
+ stream: Stream,
638
+ peerId: PeerId,
639
+ protocol: ReqRespSubProtocol,
640
+ status: ReqRespStatus,
641
+ ): Promise<boolean> {
642
+ const canWriteToStream =
643
+ // 'writing' is a bit weird naming, but it actually means that the stream is ready to write
644
+ // 'ready' means that stream ready to be opened for writing
645
+ stream.status === 'open' && (stream.writeStatus === 'writing' || stream.writeStatus === 'ready');
646
+
647
+ // Stream was already written to, we consider it poisoned, in a sense,
648
+ // that even if we write an error response, it will not be interpreted correctly by the peer
649
+ const streamPoisoned = stream.metadata.written === true;
650
+ const shouldWriteToStream = canWriteToStream && !streamPoisoned;
651
+
652
+ if (!shouldWriteToStream) {
653
+ return false;
654
+ }
655
+
656
+ try {
657
+ await pipeline(function* () {
658
+ yield Uint8Array.of(status);
659
+ }, stream.sink);
660
+
661
+ return true;
632
662
  } catch (e: any) {
633
- this.logger.warn('Reqresp Response error: ', e);
634
- this.metrics.recordResponseError(protocol);
663
+ this.logger.warn('Error while sending error response', e);
664
+ stream.abort(e);
635
665
 
636
- // If we receive a known error, we use the error status in the response chunk, otherwise we categorize as unknown
637
- let errorStatus = ReqRespStatus.UNKNOWN;
638
- if (e instanceof ReqRespStatusError) {
639
- errorStatus = e.status;
640
- }
666
+ this.handleRequestError(e, peerId, protocol);
667
+ return false;
668
+ }
669
+ }
670
+
671
+ private handleRequestError(e: any, peerId: PeerId, subProtocol: ReqRespSubProtocol): void {
672
+ const severity = this.categorizeRequestError(e, peerId, subProtocol);
673
+ if (severity) {
674
+ this.peerScoring.penalizePeer(peerId, severity);
675
+ }
676
+ }
641
677
 
642
- const sendErrorChunk = this.sendErrorChunk(errorStatus);
678
+ /**
679
+ * Categorize the request error and log it.
680
+ *
681
+ * @returns Severity of the error, or undefined if the error is not punishable.
682
+ */
683
+ private categorizeRequestError(
684
+ e: any,
685
+ peerId: PeerId,
686
+ subProtocol: ReqRespSubProtocol,
687
+ ): PeerErrorSeverity | undefined {
688
+ const logTags = { peerId: peerId.toString(), subProtocol };
643
689
 
644
- // Return and yield the response chunk
645
- await pipe(
646
- stream,
647
- async function* (_source: any) {
648
- yield* sendErrorChunk;
649
- },
650
- stream,
651
- );
652
- } finally {
653
- await stream.close();
690
+ //Punishable error - peer should never send badly formed request
691
+ if (e instanceof ReqRespStatusError && e.status === ReqRespStatus.BADLY_FORMED_REQUEST) {
692
+ this.logger.debug(`Punishable error in ${subProtocol}: ${e.cause}`, logTags);
693
+ return PeerErrorSeverity.LowToleranceError;
654
694
  }
695
+
696
+ //TODO: (mralj): think if we should penalize peer here based on connection errors
697
+ return undefined;
655
698
  }
656
699
 
657
- private async *sendErrorChunk(error: ReqRespStatus): AsyncIterable<Uint8Array> {
658
- const errorChunk = Buffer.from([error]);
659
- yield new Uint8Array(errorChunk);
700
+ /**
701
+ * Categorize the response error and log it.
702
+ *
703
+ * @returns Severity of the error, or undefined if the error is not punishable.
704
+ */
705
+ private categorizeResponseError(
706
+ e: any,
707
+ peerId: PeerId,
708
+ subProtocol: ReqRespSubProtocol,
709
+ ): PeerErrorSeverity | undefined {
710
+ const logTags = { peerId: peerId.toString(), subProtocol };
711
+
712
+ // Non punishable errors - we do not expect a response for goodbye messages
713
+ if (subProtocol === ReqRespSubProtocol.GOODBYE) {
714
+ this.logger.debug('Error encountered on goodbye sub protocol, no penalty', logTags);
715
+ return undefined;
716
+ }
717
+
718
+ // We do not punish a collective timeout, as the node triggers this interupt, independent of the peer's behaviour
719
+ if (e instanceof CollectiveReqRespTimeoutError || e instanceof InvalidResponseError) {
720
+ this.logger.debug(`Non-punishable error in ${subProtocol}: ${e.message}`, logTags);
721
+ return undefined;
722
+ }
723
+
724
+ return this.categorizeConnectionErrors(e, peerId, subProtocol);
725
+ }
726
+
727
+ /*
728
+ * Errors specific to connection handling
729
+ * These can happen both when sending request and response*/
730
+ private categorizeConnectionErrors(
731
+ e: any,
732
+ peerId: PeerId,
733
+ subProtocol: ReqRespSubProtocol,
734
+ ): PeerErrorSeverity | undefined {
735
+ const logTags = { peerId: peerId.toString(), subProtocol };
736
+ // Do not punish if we are stopping the service
737
+ if (e instanceof AbortError || e?.code == 'ABORT_ERR') {
738
+ this.logger.debug(`Request aborted: ${e.message}`, logTags);
739
+ return undefined;
740
+ }
741
+
742
+ // Do not punish if we are the ones closing the connection
743
+ if (
744
+ e?.code === 'ERR_CONNECTION_BEING_CLOSED' ||
745
+ e?.code === 'ERR_CONNECTION_CLOSED' ||
746
+ e?.code === 'ERR_TRANSIENT_CONNECTION' ||
747
+ e?.message?.includes('Muxer already closed') ||
748
+ e?.message?.includes('muxer closed') ||
749
+ e?.message?.includes('ended pushable')
750
+ ) {
751
+ this.logger.debug(
752
+ `Connection closed to peer from our side: ${peerId.toString()} (${e?.message ?? 'missing error message'})`,
753
+ logTags,
754
+ );
755
+ return undefined;
756
+ }
757
+
758
+ // Pubishable errors
759
+ // Connection reset errors in the networking stack are punished with high severity
760
+ // it just signals an unreliable peer
761
+ // We assume that the requesting node has a functioning networking stack.
762
+ if (e?.code === 'ECONNRESET' || e?.code === 'EPIPE') {
763
+ this.logger.debug(`Connection reset: ${peerId.toString()}`, logTags);
764
+ return PeerErrorSeverity.HighToleranceError;
765
+ }
766
+
767
+ if (e?.code === 'ECONNREFUSED') {
768
+ this.logger.debug(`Connection refused: ${peerId.toString()}`, logTags);
769
+ return PeerErrorSeverity.HighToleranceError;
770
+ }
771
+
772
+ if (e?.code === 'ERR_UNEXPECTED_EOF') {
773
+ this.logger.debug(`Connection unexpected EOF: ${peerId.toString()}`, logTags);
774
+ return PeerErrorSeverity.HighToleranceError;
775
+ }
776
+
777
+ if (e?.code === 'ERR_UNSUPPORTED_PROTOCOL') {
778
+ this.logger.debug(`Sub protocol not supported by peer: ${peerId.toString()}`, logTags);
779
+ return PeerErrorSeverity.HighToleranceError;
780
+ }
781
+
782
+ // Timeout errors are punished with high tolerance, they can be due to a geographically far away or overloaded peer
783
+ if (e instanceof IndividualReqRespTimeoutError || e instanceof TimeoutError) {
784
+ this.logger.debug(`Timeout error in ${subProtocol}: ${e.message}`, logTags);
785
+ return PeerErrorSeverity.HighToleranceError;
786
+ }
787
+
788
+ // Catch all error
789
+ this.logger.error(`Unexpected error in ReqResp protocol`, e, logTags);
790
+ return PeerErrorSeverity.HighToleranceError;
660
791
  }
661
792
  }