@aztec/p2p 0.0.0-test.0 → 0.0.1-commit.24de95ac

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 (339) hide show
  1. package/dest/bootstrap/bootstrap.d.ts.map +1 -1
  2. package/dest/bootstrap/bootstrap.js +22 -9
  3. package/dest/client/factory.d.ts +13 -3
  4. package/dest/client/factory.d.ts.map +1 -1
  5. package/dest/client/factory.js +60 -24
  6. package/dest/client/index.d.ts +1 -0
  7. package/dest/client/index.d.ts.map +1 -1
  8. package/dest/client/index.js +1 -0
  9. package/dest/client/interface.d.ts +155 -0
  10. package/dest/client/interface.d.ts.map +1 -0
  11. package/dest/client/interface.js +9 -0
  12. package/dest/client/p2p_client.d.ts +72 -169
  13. package/dest/client/p2p_client.d.ts.map +1 -1
  14. package/dest/client/p2p_client.js +365 -174
  15. package/dest/config.d.ts +123 -103
  16. package/dest/config.d.ts.map +1 -1
  17. package/dest/config.js +173 -34
  18. package/dest/enr/generate-enr.d.ts +10 -2
  19. package/dest/enr/generate-enr.d.ts.map +1 -1
  20. package/dest/enr/generate-enr.js +27 -5
  21. package/dest/index.d.ts +3 -0
  22. package/dest/index.d.ts.map +1 -1
  23. package/dest/index.js +2 -0
  24. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +42 -4
  25. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +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 +204 -54
  28. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +10 -2
  29. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +1 -1
  30. package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +93 -15
  31. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +10 -2
  32. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +1 -1
  33. package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +86 -18
  34. package/dest/mem_pools/attestation_pool/mocks.d.ts +1 -2
  35. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  36. package/dest/mem_pools/attestation_pool/mocks.js +9 -15
  37. package/dest/mem_pools/instrumentation.d.ts +7 -11
  38. package/dest/mem_pools/instrumentation.d.ts.map +1 -1
  39. package/dest/mem_pools/instrumentation.js +25 -37
  40. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts +93 -9
  41. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts.map +1 -1
  42. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.js +469 -97
  43. package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts +33 -9
  44. package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts.map +1 -1
  45. package/dest/mem_pools/tx_pool/memory_tx_pool.js +133 -36
  46. package/dest/mem_pools/tx_pool/priority.js +1 -1
  47. package/dest/mem_pools/tx_pool/tx_pool.d.ts +64 -8
  48. package/dest/mem_pools/tx_pool/tx_pool.d.ts.map +1 -1
  49. package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts.map +1 -1
  50. package/dest/mem_pools/tx_pool/tx_pool_test_suite.js +264 -39
  51. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +1 -0
  52. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
  53. package/dest/msg_validators/attestation_validator/attestation_validator.js +45 -9
  54. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts +5 -1
  55. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts.map +1 -1
  56. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.js +61 -12
  57. package/dest/msg_validators/msg_seen_validator/msg_seen_validator.d.ts +10 -0
  58. package/dest/msg_validators/msg_seen_validator/msg_seen_validator.d.ts.map +1 -0
  59. package/dest/msg_validators/msg_seen_validator/msg_seen_validator.js +36 -0
  60. package/dest/msg_validators/tx_validator/allowed_public_setup.d.ts +3 -0
  61. package/dest/msg_validators/tx_validator/allowed_public_setup.d.ts.map +1 -0
  62. package/dest/msg_validators/tx_validator/allowed_public_setup.js +27 -0
  63. package/dest/msg_validators/tx_validator/archive_cache.d.ts +14 -0
  64. package/dest/msg_validators/tx_validator/archive_cache.d.ts.map +1 -0
  65. package/dest/msg_validators/tx_validator/archive_cache.js +22 -0
  66. package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
  67. package/dest/msg_validators/tx_validator/block_header_validator.js +4 -4
  68. package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
  69. package/dest/msg_validators/tx_validator/data_validator.js +56 -86
  70. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts +0 -2
  71. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts.map +1 -1
  72. package/dest/msg_validators/tx_validator/double_spend_validator.js +21 -27
  73. package/dest/msg_validators/tx_validator/factory.d.ts +15 -0
  74. package/dest/msg_validators/tx_validator/factory.d.ts.map +1 -0
  75. package/dest/msg_validators/tx_validator/factory.js +74 -0
  76. package/dest/msg_validators/tx_validator/gas_validator.d.ts +11 -0
  77. package/dest/msg_validators/tx_validator/gas_validator.d.ts.map +1 -0
  78. package/dest/msg_validators/tx_validator/gas_validator.js +115 -0
  79. package/dest/msg_validators/tx_validator/index.d.ts +7 -0
  80. package/dest/msg_validators/tx_validator/index.d.ts.map +1 -1
  81. package/dest/msg_validators/tx_validator/index.js +7 -0
  82. package/dest/msg_validators/tx_validator/metadata_validator.d.ts +7 -3
  83. package/dest/msg_validators/tx_validator/metadata_validator.d.ts.map +1 -1
  84. package/dest/msg_validators/tx_validator/metadata_validator.js +39 -20
  85. package/dest/msg_validators/tx_validator/phases_validator.d.ts +14 -0
  86. package/dest/msg_validators/tx_validator/phases_validator.d.ts.map +1 -0
  87. package/dest/msg_validators/tx_validator/phases_validator.js +91 -0
  88. package/dest/msg_validators/tx_validator/test_utils.d.ts +17 -0
  89. package/dest/msg_validators/tx_validator/test_utils.d.ts.map +1 -0
  90. package/dest/msg_validators/tx_validator/test_utils.js +22 -0
  91. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +12 -0
  92. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -0
  93. package/dest/msg_validators/tx_validator/timestamp_validator.js +32 -0
  94. package/dest/msg_validators/tx_validator/tx_permitted_validator.d.ts +8 -0
  95. package/dest/msg_validators/tx_validator/tx_permitted_validator.d.ts.map +1 -0
  96. package/dest/msg_validators/tx_validator/tx_permitted_validator.js +24 -0
  97. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts.map +1 -1
  98. package/dest/msg_validators/tx_validator/tx_proof_validator.js +6 -5
  99. package/dest/services/discv5/discV5_service.d.ts +9 -8
  100. package/dest/services/discv5/discV5_service.d.ts.map +1 -1
  101. package/dest/services/discv5/discV5_service.js +63 -36
  102. package/dest/services/dummy_service.d.ts +49 -10
  103. package/dest/services/dummy_service.d.ts.map +1 -1
  104. package/dest/services/dummy_service.js +88 -5
  105. package/dest/services/encoding.d.ts +25 -6
  106. package/dest/services/encoding.d.ts.map +1 -1
  107. package/dest/services/encoding.js +73 -5
  108. package/dest/services/index.d.ts +4 -0
  109. package/dest/services/index.d.ts.map +1 -1
  110. package/dest/services/index.js +4 -0
  111. package/dest/services/libp2p/instrumentation.d.ts +18 -0
  112. package/dest/services/libp2p/instrumentation.d.ts.map +1 -0
  113. package/dest/services/libp2p/instrumentation.js +157 -0
  114. package/dest/services/libp2p/libp2p_service.d.ts +87 -42
  115. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  116. package/dest/services/libp2p/libp2p_service.js +500 -218
  117. package/dest/services/peer-manager/interface.d.ts +23 -0
  118. package/dest/services/peer-manager/interface.d.ts.map +1 -0
  119. package/dest/services/peer-manager/interface.js +1 -0
  120. package/dest/services/peer-manager/metrics.d.ts +3 -1
  121. package/dest/services/peer-manager/metrics.d.ts.map +1 -1
  122. package/dest/services/peer-manager/metrics.js +11 -2
  123. package/dest/services/peer-manager/peer_manager.d.ts +126 -15
  124. package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
  125. package/dest/services/peer-manager/peer_manager.js +547 -72
  126. package/dest/services/reqresp/config.d.ts +10 -8
  127. package/dest/services/reqresp/config.d.ts.map +1 -1
  128. package/dest/services/reqresp/config.js +18 -4
  129. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts +1 -1
  130. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts.map +1 -1
  131. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.js +10 -6
  132. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts +30 -13
  133. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts.map +1 -1
  134. package/dest/services/reqresp/connection-sampler/connection_sampler.js +142 -84
  135. package/dest/services/reqresp/index.d.ts +2 -1
  136. package/dest/services/reqresp/index.d.ts.map +1 -1
  137. package/dest/services/reqresp/index.js +2 -1
  138. package/dest/services/reqresp/interface.d.ts +72 -23
  139. package/dest/services/reqresp/interface.d.ts.map +1 -1
  140. package/dest/services/reqresp/interface.js +45 -26
  141. package/dest/services/reqresp/metrics.d.ts.map +1 -1
  142. package/dest/services/reqresp/protocols/auth.d.ts +43 -0
  143. package/dest/services/reqresp/protocols/auth.d.ts.map +1 -0
  144. package/dest/services/reqresp/protocols/auth.js +71 -0
  145. package/dest/services/reqresp/protocols/block.d.ts +5 -0
  146. package/dest/services/reqresp/protocols/block.d.ts.map +1 -1
  147. package/dest/services/reqresp/protocols/block.js +28 -5
  148. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts +30 -0
  149. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts.map +1 -0
  150. package/dest/services/reqresp/protocols/block_txs/bitvector.js +75 -0
  151. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +11 -0
  152. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -0
  153. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.js +39 -0
  154. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +49 -0
  155. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -0
  156. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +75 -0
  157. package/dest/services/reqresp/protocols/block_txs/index.d.ts +4 -0
  158. package/dest/services/reqresp/protocols/block_txs/index.d.ts.map +1 -0
  159. package/dest/services/reqresp/protocols/block_txs/index.js +3 -0
  160. package/dest/services/reqresp/protocols/goodbye.d.ts +2 -4
  161. package/dest/services/reqresp/protocols/goodbye.d.ts.map +1 -1
  162. package/dest/services/reqresp/protocols/goodbye.js +7 -7
  163. package/dest/services/reqresp/protocols/index.d.ts +2 -0
  164. package/dest/services/reqresp/protocols/index.d.ts.map +1 -1
  165. package/dest/services/reqresp/protocols/index.js +2 -0
  166. package/dest/services/reqresp/protocols/ping.d.ts +0 -2
  167. package/dest/services/reqresp/protocols/ping.d.ts.map +1 -1
  168. package/dest/services/reqresp/protocols/status.d.ts +38 -6
  169. package/dest/services/reqresp/protocols/status.d.ts.map +1 -1
  170. package/dest/services/reqresp/protocols/status.js +72 -5
  171. package/dest/services/reqresp/protocols/tx.d.ts +12 -1
  172. package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
  173. package/dest/services/reqresp/protocols/tx.js +34 -6
  174. package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts +4 -2
  175. package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts.map +1 -1
  176. package/dest/services/reqresp/rate-limiter/rate_limiter.js +10 -2
  177. package/dest/services/reqresp/rate-limiter/rate_limits.d.ts.map +1 -1
  178. package/dest/services/reqresp/rate-limiter/rate_limits.js +21 -1
  179. package/dest/services/reqresp/reqresp.d.ts +45 -47
  180. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  181. package/dest/services/reqresp/reqresp.js +298 -207
  182. package/dest/services/reqresp/status.d.ts +9 -3
  183. package/dest/services/reqresp/status.d.ts.map +1 -1
  184. package/dest/services/reqresp/status.js +9 -2
  185. package/dest/services/service.d.ts +22 -18
  186. package/dest/services/service.d.ts.map +1 -1
  187. package/dest/services/tx_collection/config.d.ts +25 -0
  188. package/dest/services/tx_collection/config.d.ts.map +1 -0
  189. package/dest/services/tx_collection/config.js +58 -0
  190. package/dest/services/tx_collection/fast_tx_collection.d.ts +56 -0
  191. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -0
  192. package/dest/services/tx_collection/fast_tx_collection.js +300 -0
  193. package/dest/services/tx_collection/index.d.ts +3 -0
  194. package/dest/services/tx_collection/index.d.ts.map +1 -0
  195. package/dest/services/tx_collection/index.js +2 -0
  196. package/dest/services/tx_collection/instrumentation.d.ts +10 -0
  197. package/dest/services/tx_collection/instrumentation.d.ts.map +1 -0
  198. package/dest/services/tx_collection/instrumentation.js +34 -0
  199. package/dest/services/tx_collection/slow_tx_collection.d.ts +54 -0
  200. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -0
  201. package/dest/services/tx_collection/slow_tx_collection.js +176 -0
  202. package/dest/services/tx_collection/tx_collection.d.ts +110 -0
  203. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -0
  204. package/dest/services/tx_collection/tx_collection.js +128 -0
  205. package/dest/services/tx_collection/tx_collection_sink.d.ts +30 -0
  206. package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -0
  207. package/dest/services/tx_collection/tx_collection_sink.js +111 -0
  208. package/dest/services/tx_collection/tx_source.d.ts +18 -0
  209. package/dest/services/tx_collection/tx_source.d.ts.map +1 -0
  210. package/dest/services/tx_collection/tx_source.js +31 -0
  211. package/dest/services/tx_provider.d.ts +49 -0
  212. package/dest/services/tx_provider.d.ts.map +1 -0
  213. package/dest/services/tx_provider.js +210 -0
  214. package/dest/services/tx_provider_instrumentation.d.ts +13 -0
  215. package/dest/services/tx_provider_instrumentation.d.ts.map +1 -0
  216. package/dest/services/tx_provider_instrumentation.js +34 -0
  217. package/dest/test-helpers/get-ports.d.ts.map +1 -1
  218. package/dest/test-helpers/index.d.ts +1 -0
  219. package/dest/test-helpers/index.d.ts.map +1 -1
  220. package/dest/test-helpers/index.js +1 -0
  221. package/dest/test-helpers/make-enrs.d.ts.map +1 -1
  222. package/dest/test-helpers/make-enrs.js +4 -5
  223. package/dest/test-helpers/make-test-p2p-clients.d.ts +32 -4
  224. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  225. package/dest/test-helpers/make-test-p2p-clients.js +86 -16
  226. package/dest/test-helpers/mock-pubsub.d.ts +59 -0
  227. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -0
  228. package/dest/test-helpers/mock-pubsub.js +130 -0
  229. package/dest/test-helpers/mock-tx-helpers.d.ts +12 -0
  230. package/dest/test-helpers/mock-tx-helpers.d.ts.map +1 -0
  231. package/dest/test-helpers/mock-tx-helpers.js +19 -0
  232. package/dest/test-helpers/reqresp-nodes.d.ts +14 -10
  233. package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
  234. package/dest/test-helpers/reqresp-nodes.js +62 -28
  235. package/dest/testbench/p2p_client_testbench_worker.js +96 -25
  236. package/dest/testbench/parse_log_file.js +4 -4
  237. package/dest/testbench/testbench.js +4 -4
  238. package/dest/testbench/worker_client_manager.d.ts +0 -5
  239. package/dest/testbench/worker_client_manager.d.ts.map +1 -1
  240. package/dest/testbench/worker_client_manager.js +11 -19
  241. package/dest/types/index.d.ts +3 -1
  242. package/dest/types/index.d.ts.map +1 -1
  243. package/dest/types/index.js +2 -0
  244. package/dest/util.d.ts +22 -15
  245. package/dest/util.d.ts.map +1 -1
  246. package/dest/util.js +64 -67
  247. package/dest/versioning.d.ts +3 -3
  248. package/dest/versioning.d.ts.map +1 -1
  249. package/dest/versioning.js +8 -3
  250. package/package.json +28 -24
  251. package/src/bootstrap/bootstrap.ts +27 -11
  252. package/src/client/factory.ts +136 -45
  253. package/src/client/index.ts +1 -0
  254. package/src/client/interface.ts +195 -0
  255. package/src/client/p2p_client.ts +460 -327
  256. package/src/config.ts +288 -134
  257. package/src/enr/generate-enr.ts +39 -6
  258. package/src/index.ts +4 -0
  259. package/src/mem_pools/attestation_pool/attestation_pool.ts +48 -4
  260. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +241 -55
  261. package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +117 -20
  262. package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +114 -22
  263. package/src/mem_pools/attestation_pool/mocks.ts +11 -10
  264. package/src/mem_pools/instrumentation.ts +32 -46
  265. package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +549 -108
  266. package/src/mem_pools/tx_pool/memory_tx_pool.ts +153 -44
  267. package/src/mem_pools/tx_pool/priority.ts +1 -1
  268. package/src/mem_pools/tx_pool/tx_pool.ts +67 -8
  269. package/src/mem_pools/tx_pool/tx_pool_test_suite.ts +217 -34
  270. package/src/msg_validators/attestation_validator/attestation_validator.ts +55 -10
  271. package/src/msg_validators/block_proposal_validator/block_proposal_validator.ts +66 -14
  272. package/src/msg_validators/msg_seen_validator/msg_seen_validator.ts +36 -0
  273. package/src/msg_validators/tx_validator/allowed_public_setup.ts +35 -0
  274. package/src/msg_validators/tx_validator/archive_cache.ts +28 -0
  275. package/src/msg_validators/tx_validator/block_header_validator.ts +4 -4
  276. package/src/msg_validators/tx_validator/data_validator.ts +81 -69
  277. package/src/msg_validators/tx_validator/double_spend_validator.ts +19 -17
  278. package/src/msg_validators/tx_validator/factory.ts +109 -0
  279. package/src/msg_validators/tx_validator/gas_validator.ts +134 -0
  280. package/src/msg_validators/tx_validator/index.ts +7 -0
  281. package/src/msg_validators/tx_validator/metadata_validator.ts +58 -21
  282. package/src/msg_validators/tx_validator/phases_validator.ts +114 -0
  283. package/src/msg_validators/tx_validator/test_utils.ts +43 -0
  284. package/src/msg_validators/tx_validator/timestamp_validator.ts +46 -0
  285. package/src/msg_validators/tx_validator/tx_permitted_validator.ts +17 -0
  286. package/src/msg_validators/tx_validator/tx_proof_validator.ts +6 -5
  287. package/src/services/discv5/discV5_service.ts +84 -38
  288. package/src/services/dummy_service.ts +147 -9
  289. package/src/services/encoding.ts +80 -5
  290. package/src/services/index.ts +4 -0
  291. package/src/services/libp2p/instrumentation.ts +158 -0
  292. package/src/services/libp2p/libp2p_service.ts +646 -263
  293. package/src/services/peer-manager/interface.ts +29 -0
  294. package/src/services/peer-manager/metrics.ts +16 -1
  295. package/src/services/peer-manager/peer_manager.ts +652 -78
  296. package/src/services/reqresp/config.ts +26 -9
  297. package/src/services/reqresp/connection-sampler/batch_connection_sampler.ts +12 -6
  298. package/src/services/reqresp/connection-sampler/connection_sampler.ts +148 -95
  299. package/src/services/reqresp/index.ts +2 -0
  300. package/src/services/reqresp/interface.ts +91 -36
  301. package/src/services/reqresp/metrics.ts +4 -1
  302. package/src/services/reqresp/protocols/auth.ts +83 -0
  303. package/src/services/reqresp/protocols/block.ts +24 -3
  304. package/src/services/reqresp/protocols/block_txs/bitvector.ts +90 -0
  305. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +53 -0
  306. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +79 -0
  307. package/src/services/reqresp/protocols/block_txs/index.ts +3 -0
  308. package/src/services/reqresp/protocols/goodbye.ts +9 -7
  309. package/src/services/reqresp/protocols/index.ts +2 -0
  310. package/src/services/reqresp/protocols/status.ts +117 -5
  311. package/src/services/reqresp/protocols/tx.ts +35 -6
  312. package/src/services/reqresp/rate-limiter/rate_limiter.ts +12 -3
  313. package/src/services/reqresp/rate-limiter/rate_limits.ts +21 -1
  314. package/src/services/reqresp/reqresp.ts +387 -256
  315. package/src/services/reqresp/status.ts +12 -3
  316. package/src/services/service.ts +45 -21
  317. package/src/services/tx_collection/config.ts +84 -0
  318. package/src/services/tx_collection/fast_tx_collection.ts +340 -0
  319. package/src/services/tx_collection/index.ts +2 -0
  320. package/src/services/tx_collection/instrumentation.ts +43 -0
  321. package/src/services/tx_collection/slow_tx_collection.ts +232 -0
  322. package/src/services/tx_collection/tx_collection.ts +215 -0
  323. package/src/services/tx_collection/tx_collection_sink.ts +129 -0
  324. package/src/services/tx_collection/tx_source.ts +37 -0
  325. package/src/services/tx_provider.ts +216 -0
  326. package/src/services/tx_provider_instrumentation.ts +44 -0
  327. package/src/test-helpers/index.ts +1 -0
  328. package/src/test-helpers/make-enrs.ts +4 -5
  329. package/src/test-helpers/make-test-p2p-clients.ts +111 -21
  330. package/src/test-helpers/mock-pubsub.ts +188 -0
  331. package/src/test-helpers/mock-tx-helpers.ts +24 -0
  332. package/src/test-helpers/reqresp-nodes.ts +86 -35
  333. package/src/testbench/p2p_client_testbench_worker.ts +145 -22
  334. package/src/testbench/parse_log_file.ts +4 -4
  335. package/src/testbench/testbench.ts +4 -4
  336. package/src/testbench/worker_client_manager.ts +17 -23
  337. package/src/types/index.ts +2 -0
  338. package/src/util.ts +93 -89
  339. package/src/versioning.ts +11 -4
@@ -0,0 +1,216 @@
1
+ import { compactArray } from '@aztec/foundation/collection';
2
+ import { type Logger, createLogger } from '@aztec/foundation/log';
3
+ import { elapsed } from '@aztec/foundation/timer';
4
+ import type { L2Block, L2BlockInfo } from '@aztec/stdlib/block';
5
+ import type { ITxProvider } from '@aztec/stdlib/interfaces/server';
6
+ import type { BlockProposal } from '@aztec/stdlib/p2p';
7
+ import { Tx, TxHash } from '@aztec/stdlib/tx';
8
+ import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
9
+
10
+ import type { PeerId } from '@libp2p/interface';
11
+
12
+ import type { P2PClient } from '../client/p2p_client.js';
13
+ import type { TxPool } from '../mem_pools/index.js';
14
+ import type { FastCollectionRequestInput, TxCollection } from './tx_collection/tx_collection.js';
15
+ import { TxProviderInstrumentation } from './tx_provider_instrumentation.js';
16
+
17
+ /**
18
+ * Gathers and returns txs given a block proposal, block, or their hashes.
19
+ * Loads available txs from the tx pool, and relies on a TxCollection service to collect txs from the network and other nodes.
20
+ */
21
+ export class TxProvider implements ITxProvider {
22
+ protected instrumentation: TxProviderInstrumentation;
23
+
24
+ constructor(
25
+ private txCollection: TxCollection,
26
+ private txPool: TxPool,
27
+ private txValidator: Pick<P2PClient, 'validate'>,
28
+ private log: Logger = createLogger('p2p:tx-collector'),
29
+ client: TelemetryClient = getTelemetryClient(),
30
+ ) {
31
+ this.instrumentation = new TxProviderInstrumentation(client, 'TxProvider');
32
+ }
33
+
34
+ /** Returns txs from the tx pool given their hashes.*/
35
+ public async getAvailableTxs(txHashes: TxHash[]): Promise<{ txs: Tx[]; missingTxs: TxHash[] }> {
36
+ const response = await this.txPool.getTxsByHash(txHashes);
37
+ if (response.length !== txHashes.length) {
38
+ throw new Error(`Unexpected response size from tx pool: expected ${txHashes.length} but got ${response.length}`);
39
+ }
40
+ const txs: Tx[] = [];
41
+ const missingTxs: TxHash[] = [];
42
+
43
+ for (let i = 0; i < txHashes.length; i++) {
44
+ const tx = response[i];
45
+ if (tx === undefined) {
46
+ missingTxs.push(txHashes[i]);
47
+ } else {
48
+ txs.push(tx);
49
+ }
50
+ }
51
+
52
+ return { txs, missingTxs };
53
+ }
54
+
55
+ /** Gathers txs from the tx pool, proposal body, remote rpc nodes, and reqresp. */
56
+ public getTxsForBlockProposal(
57
+ blockProposal: BlockProposal,
58
+ blockNumber: number,
59
+ opts: { pinnedPeer: PeerId | undefined; deadline: Date },
60
+ ): Promise<{ txs: Tx[]; missingTxs: TxHash[] }> {
61
+ return this.getOrderedTxsFromAllSources(
62
+ { type: 'proposal', blockProposal, blockNumber },
63
+ { ...blockProposal.toBlockInfo(), blockNumber },
64
+ blockProposal.txHashes,
65
+ { ...opts, pinnedPeer: opts.pinnedPeer },
66
+ );
67
+ }
68
+
69
+ /** Gathers txs from the tx pool, remote rpc nodes, and reqresp. */
70
+ public getTxsForBlock(block: L2Block, opts: { deadline: Date }): Promise<{ txs: Tx[]; missingTxs: TxHash[] }> {
71
+ return this.getOrderedTxsFromAllSources(
72
+ { type: 'block', block },
73
+ block.toBlockInfo(),
74
+ block.body.txEffects.map(tx => tx.txHash),
75
+ { ...opts, pinnedPeer: undefined },
76
+ );
77
+ }
78
+
79
+ private async getOrderedTxsFromAllSources(
80
+ request: FastCollectionRequestInput,
81
+ blockInfo: L2BlockInfo,
82
+ txHashes: TxHash[],
83
+ opts: { pinnedPeer: PeerId | undefined; deadline: Date },
84
+ ) {
85
+ const [durationMs, result] = await elapsed(() => this.getTxsFromAllSources(request, blockInfo, txHashes, opts));
86
+ const { missingTxHashes, txsFromMempool, txsFromNetwork, txsFromProposal } = result;
87
+
88
+ const txs = [...(txsFromMempool ?? []), ...(txsFromProposal ?? []), ...(txsFromNetwork ?? [])];
89
+ const missingTxs = missingTxHashes?.length ?? 0;
90
+
91
+ const level = missingTxs === 0 ? 'verbose' : 'warn';
92
+ this.log[level](`Retrieved ${txs.length} out of ${blockInfo.txCount} txs for ${request.type}`, {
93
+ ...blockInfo,
94
+ txsFromProposal: txsFromProposal?.length,
95
+ txsFromMempool: txsFromMempool?.length,
96
+ txsFromNetwork: txsFromNetwork?.length,
97
+ missingTxs,
98
+ durationMs,
99
+ });
100
+
101
+ const orderedTxs = this.orderTxs(txs, txHashes);
102
+ if (orderedTxs.length + missingTxs !== txHashes.length) {
103
+ throw new Error(
104
+ `Error collecting txs for ${request.type} with ${txHashes.length} txs: found ${orderedTxs.length} and flagged ${missingTxs} as missing`,
105
+ );
106
+ }
107
+
108
+ return { txs: orderedTxs, missingTxs: (missingTxHashes ?? []).map(TxHash.fromString), durationMs };
109
+ }
110
+
111
+ private orderTxs(txs: Tx[], order: TxHash[]): Tx[] {
112
+ const txsMap = new Map(txs.map(tx => [tx.txHash.toString(), tx]));
113
+ return order.map(hash => txsMap.get(hash.toString())!).filter(tx => tx !== undefined);
114
+ }
115
+
116
+ private async getTxsFromAllSources(
117
+ request: FastCollectionRequestInput,
118
+ blockInfo: L2BlockInfo,
119
+ txHashes: TxHash[],
120
+ opts: { pinnedPeer: PeerId | undefined; deadline: Date },
121
+ ) {
122
+ const missingTxHashes = new Set(txHashes.map(txHash => txHash.toString()));
123
+ if (missingTxHashes.size === 0) {
124
+ this.log.debug(`Received request with no transactions`, blockInfo);
125
+ return {};
126
+ }
127
+
128
+ // First go to our tx pool and fetch whatever txs we have there
129
+ // We go to the mempool first since those txs are already validated
130
+ const txsFromMempool = compactArray(await this.txPool.getTxsByHash(txHashes));
131
+ txsFromMempool.forEach(tx => missingTxHashes.delete(tx.getTxHash().toString()));
132
+ this.instrumentation.incTxsFromMempool(txsFromMempool.length);
133
+ this.log.debug(
134
+ `Retrieved ${txsFromMempool.length} txs from mempool for block proposal (${missingTxHashes.size} pending)`,
135
+ { ...blockInfo, missingTxHashes: [...missingTxHashes] },
136
+ );
137
+
138
+ if (missingTxHashes.size === 0) {
139
+ return { txsFromMempool };
140
+ }
141
+
142
+ // Take txs from the proposal body if there are any
143
+ // Note that we still have to validate these txs, but we do it in parallel with tx collection
144
+ const proposal = request.type === 'proposal' ? request.blockProposal : undefined;
145
+ const txsFromProposal = this.extractFromProposal(proposal, [...missingTxHashes]);
146
+ if (txsFromProposal.length > 0) {
147
+ this.instrumentation.incTxsFromProposals(txsFromProposal.length);
148
+ txsFromProposal.forEach(tx => missingTxHashes.delete(tx.txHash.toString()));
149
+ this.log.debug(`Retrieved ${txsFromProposal.length} txs from proposal body (${missingTxHashes.size} pending)`, {
150
+ ...blockInfo,
151
+ missingTxHashes: [...missingTxHashes],
152
+ });
153
+ }
154
+
155
+ if (missingTxHashes.size === 0) {
156
+ await this.processProposalTxs(txsFromProposal);
157
+ return { txsFromMempool, txsFromProposal };
158
+ }
159
+
160
+ // Start tx collection from the network if needed, while we validate the txs taken from the proposal in parallel
161
+ const [txsFromNetwork] = await Promise.all([
162
+ this.txCollection.collectFastFor(request, [...missingTxHashes], opts),
163
+ this.processProposalTxs(txsFromProposal),
164
+ ] as const);
165
+
166
+ if (txsFromNetwork.length > 0) {
167
+ txsFromNetwork.forEach(tx => missingTxHashes.delete(tx.txHash.toString()));
168
+ this.instrumentation.incTxsFromP2P(txsFromNetwork.length);
169
+ this.log.debug(
170
+ `Retrieved ${txsFromNetwork.length} txs from network for block proposal (${missingTxHashes.size} pending)`,
171
+ { ...blockInfo, missingTxHashes: [...missingTxHashes] },
172
+ );
173
+ }
174
+
175
+ if (missingTxHashes.size === 0) {
176
+ return { txsFromNetwork, txsFromMempool, txsFromProposal };
177
+ }
178
+
179
+ // We are still missing txs, make one last attempt to collect them from our pool, in case they showed up somehow else
180
+ const moreTxsFromPool = compactArray(await this.txPool.getTxsByHash([...missingTxHashes].map(TxHash.fromString)));
181
+
182
+ if (moreTxsFromPool.length > 0) {
183
+ this.instrumentation.incTxsFromMempool(moreTxsFromPool.length);
184
+ this.log.debug(
185
+ `Retrieved ${moreTxsFromPool.length} txs from pool retry for block proposal (${missingTxHashes.size} pending)`,
186
+ { ...blockInfo, missingTxHashes: [...missingTxHashes] },
187
+ );
188
+ }
189
+
190
+ if (missingTxHashes.size > 0) {
191
+ this.instrumentation.incMissingTxs(missingTxHashes.size);
192
+ }
193
+
194
+ return {
195
+ txsFromNetwork,
196
+ txsFromMempool: [...txsFromMempool, ...moreTxsFromPool],
197
+ txsFromProposal,
198
+ missingTxHashes: [...missingTxHashes],
199
+ };
200
+ }
201
+
202
+ private extractFromProposal(proposal: BlockProposal | undefined, missingTxHashes: string[]): Tx[] {
203
+ if (!proposal) {
204
+ return [];
205
+ }
206
+ return compactArray(proposal.txs ?? []).filter(tx => missingTxHashes.includes(tx.getTxHash().toString()));
207
+ }
208
+
209
+ private async processProposalTxs(txs: Tx[]): Promise<void> {
210
+ if (txs.length === 0) {
211
+ return;
212
+ }
213
+ await this.txValidator.validate(txs);
214
+ await this.txPool.addTxs(txs);
215
+ }
216
+ }
@@ -0,0 +1,44 @@
1
+ import { Metrics, type TelemetryClient, type UpDownCounter } from '@aztec/telemetry-client';
2
+
3
+ export class TxProviderInstrumentation {
4
+ private txFromProposalCount: UpDownCounter;
5
+ private txFromMempoolCount: UpDownCounter;
6
+ private txFromP2PCount: UpDownCounter;
7
+ private missingTxsCount: UpDownCounter;
8
+
9
+ constructor(client: TelemetryClient, name: string) {
10
+ const meter = client.getMeter(name);
11
+
12
+ this.txFromProposalCount = meter.createUpDownCounter(Metrics.TX_PROVIDER_TXS_FROM_PROPOSALS_COUNT, {
13
+ description: 'The number of txs taken from block proposals',
14
+ });
15
+
16
+ this.txFromMempoolCount = meter.createUpDownCounter(Metrics.TX_PROVIDER_TXS_FROM_MEMPOOL_COUNT, {
17
+ description: 'The number of txs taken from the local mempool',
18
+ });
19
+
20
+ this.txFromP2PCount = meter.createUpDownCounter(Metrics.TX_PROVIDER_TXS_FROM_P2P_COUNT, {
21
+ description: 'The number of txs taken from the p2p network',
22
+ });
23
+
24
+ this.missingTxsCount = meter.createUpDownCounter(Metrics.TX_PROVIDER_MISSING_TXS_COUNT, {
25
+ description: 'The number of txs not found anywhere',
26
+ });
27
+ }
28
+
29
+ incTxsFromProposals(count: number) {
30
+ this.txFromProposalCount.add(count);
31
+ }
32
+
33
+ incTxsFromMempool(count: number) {
34
+ this.txFromMempoolCount.add(count);
35
+ }
36
+
37
+ incTxsFromP2P(count: number) {
38
+ this.txFromP2PCount.add(count);
39
+ }
40
+
41
+ incMissingTxs(count: number) {
42
+ this.missingTxsCount.add(count);
43
+ }
44
+ }
@@ -3,3 +3,4 @@ export * from './get-ports.js';
3
3
  export * from './make-enrs.js';
4
4
  export * from './make-test-p2p-clients.js';
5
5
  export * from './reqresp-nodes.js';
6
+ export * from './mock-pubsub.js';
@@ -1,7 +1,7 @@
1
1
  import type { ChainConfig } from '@aztec/stdlib/config';
2
2
 
3
- import { SignableENR } from '@chainsafe/enr';
4
3
  import { multiaddr } from '@multiformats/multiaddr';
4
+ import { SignableENR } from '@nethermindeth/enr';
5
5
 
6
6
  import { convertToMultiaddr, createLibP2PPeerIdFromPrivateKey } from '../util.js';
7
7
  import { setAztecEnrKey } from '../versioning.js';
@@ -30,10 +30,9 @@ export async function makeEnr(p2pPrivateKey: string, port: number, config: Chain
30
30
  const peerId = await createLibP2PPeerIdFromPrivateKey(p2pPrivateKey);
31
31
  const enr = SignableENR.createFromPeerId(peerId);
32
32
 
33
- const udpAnnounceAddress = `127.0.0.1:${port}`;
34
- const tcpAnnounceAddress = `127.0.0.1:${port}`;
35
- const udpPublicAddr = multiaddr(convertToMultiaddr(udpAnnounceAddress, 'udp'));
36
- const tcpPublicAddr = multiaddr(convertToMultiaddr(tcpAnnounceAddress, 'tcp'));
33
+ const p2pIp = `127.0.0.1`;
34
+ const udpPublicAddr = multiaddr(convertToMultiaddr(p2pIp, port, 'udp'));
35
+ const tcpPublicAddr = multiaddr(convertToMultiaddr(p2pIp, port, 'tcp'));
37
36
 
38
37
  // ENRS must include the network and a discoverable address (udp for discv5)
39
38
  setAztecEnrKey(enr, config);
@@ -1,6 +1,9 @@
1
1
  import { MockL2BlockSource } from '@aztec/archiver/test';
2
2
  import type { EpochCache } from '@aztec/epoch-cache';
3
+ import { SecretValue } from '@aztec/foundation/config';
3
4
  import { type Logger, createLogger } from '@aztec/foundation/log';
5
+ import { retryUntil } from '@aztec/foundation/retry';
6
+ import { sleep } from '@aztec/foundation/sleep';
4
7
  import type { DataStoreConfig } from '@aztec/kv-store/config';
5
8
  import { openTmpStore } from '@aztec/kv-store/lmdb-v2';
6
9
  import type { WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
@@ -14,9 +17,10 @@ import type { TxPool } from '../mem_pools/tx_pool/index.js';
14
17
  import { generatePeerIdPrivateKeys } from '../test-helpers/generate-peer-id-private-keys.js';
15
18
  import { getPorts } from './get-ports.js';
16
19
  import { makeEnrs } from './make-enrs.js';
20
+ import { type MockGossipSubNetwork, getMockPubSubP2PServiceFactory } from './mock-pubsub.js';
17
21
  import { AlwaysFalseCircuitVerifier, AlwaysTrueCircuitVerifier } from './reqresp-nodes.js';
18
22
 
19
- interface MakeTestP2PClientOptions {
23
+ export interface MakeTestP2PClientOptions {
20
24
  mockAttestationPool: AttestationPool;
21
25
  mockTxPool: TxPool;
22
26
  mockEpochCache: EpochCache;
@@ -25,6 +29,27 @@ interface MakeTestP2PClientOptions {
25
29
  p2pBaseConfig: P2PConfig;
26
30
  p2pConfigOverrides?: Partial<P2PConfig>;
27
31
  logger?: Logger;
32
+ mockGossipSubNetwork?: MockGossipSubNetwork;
33
+ }
34
+
35
+ /**
36
+ * Creates a single P2P client and immediately starts it for testing purposes.
37
+ * @param peerIdPrivateKey - The private key of the peer.
38
+ * @param port - The port to run the client on.
39
+ * @param peers - The peers to connect to.
40
+ * @param options - The options for the client.
41
+ * @returns The created and already started client.
42
+ */
43
+ export async function makeAndStartTestP2PClient(
44
+ peerIdPrivateKey: string,
45
+ port: number,
46
+ peers: string[],
47
+ options: MakeTestP2PClientOptions,
48
+ ) {
49
+ const client = await makeTestP2PClient(peerIdPrivateKey, port, peers, options);
50
+
51
+ await client.start();
52
+ return client;
28
53
  }
29
54
 
30
55
  /**
@@ -47,22 +72,18 @@ export async function makeTestP2PClient(
47
72
  mockTxPool,
48
73
  mockEpochCache,
49
74
  mockWorldState,
75
+ mockGossipSubNetwork,
50
76
  logger = createLogger('p2p-test-client'),
51
77
  }: MakeTestP2PClientOptions,
52
78
  ) {
53
- const addr = `127.0.0.1:${port}`;
54
- const listenAddr = `0.0.0.0:${port}`;
55
-
56
79
  // Filter nodes so that we only dial active peers
57
-
58
80
  const config: P2PConfig & DataStoreConfig = {
59
81
  ...p2pBaseConfig,
60
82
  p2pEnabled: true,
61
- peerIdPrivateKey,
62
- tcpListenAddress: listenAddr, // run on port 0
63
- udpListenAddress: listenAddr,
64
- tcpAnnounceAddress: addr,
65
- udpAnnounceAddress: addr,
83
+ peerIdPrivateKey: new SecretValue(peerIdPrivateKey),
84
+ p2pIp: `127.0.0.1`,
85
+ listenAddress: `127.0.0.1`,
86
+ p2pPort: port,
66
87
  bootstrapNodes: peers,
67
88
  peerCheckIntervalMS: 1000,
68
89
  maxPeerCount: 10,
@@ -75,12 +96,7 @@ export async function makeTestP2PClient(
75
96
 
76
97
  const proofVerifier = alwaysTrueVerifier ? new AlwaysTrueCircuitVerifier() : new AlwaysFalseCircuitVerifier();
77
98
  const kvStore = await openTmpStore('test');
78
- const deps = {
79
- txPool: mockTxPool as unknown as TxPool,
80
- attestationPool: mockAttestationPool as unknown as AttestationPool,
81
- store: kvStore,
82
- logger,
83
- };
99
+
84
100
  const client = await createP2PClient(
85
101
  P2PClientType.Full,
86
102
  config,
@@ -88,14 +104,68 @@ export async function makeTestP2PClient(
88
104
  proofVerifier,
89
105
  mockWorldState,
90
106
  mockEpochCache,
107
+ 'test-p2p-client',
91
108
  undefined,
92
- deps,
109
+ undefined,
110
+ {
111
+ txPool: mockTxPool as unknown as TxPool,
112
+ attestationPool: mockAttestationPool as unknown as AttestationPool,
113
+ store: kvStore,
114
+ logger,
115
+ p2pServiceFactory: mockGossipSubNetwork && getMockPubSubP2PServiceFactory(mockGossipSubNetwork),
116
+ },
93
117
  );
94
- await client.start();
95
118
 
96
119
  return client;
97
120
  }
98
121
 
122
+ /**
123
+ * Creates a number of P2P clients and immediately starts them for testing purposes.
124
+ * @param numberOfPeers - The number of clients to create.
125
+ * @param options - The options for the clients.
126
+ * @returns The created and started clients.
127
+ */
128
+ export async function makeAndStartTestP2PClients(numberOfPeers: number, testConfig: MakeTestP2PClientOptions) {
129
+ const clients: P2PClient[] = [];
130
+ const peerIdPrivateKeys = generatePeerIdPrivateKeys(numberOfPeers);
131
+
132
+ let ports = [];
133
+ while (true) {
134
+ try {
135
+ ports = await getPorts(numberOfPeers);
136
+ break;
137
+ } catch {
138
+ await sleep(1000);
139
+ }
140
+ }
141
+
142
+ const peerEnrs = await makeEnrs(peerIdPrivateKeys, ports, testConfig.p2pBaseConfig);
143
+
144
+ for (let i = 0; i < numberOfPeers; i++) {
145
+ const client = await makeAndStartTestP2PClient(peerIdPrivateKeys[i], ports[i], peerEnrs, {
146
+ ...testConfig,
147
+ logger: createLogger(`p2p:${i}`),
148
+ });
149
+ clients.push(client);
150
+ }
151
+
152
+ await retryUntil(() => clients.every(c => c.isReady()), 'p2p clients started', 10, 0.5);
153
+ testConfig.logger?.info(`Created and started ${clients.length} P2P clients at ports ${ports.join(',')}`, {
154
+ ports,
155
+ peerEnrs,
156
+ peerIdPrivateKeys,
157
+ });
158
+
159
+ return clients.map((client, index) => {
160
+ return {
161
+ client,
162
+ peerPrivateKey: peerIdPrivateKeys[index],
163
+ port: ports[index],
164
+ enr: peerEnrs[index],
165
+ };
166
+ });
167
+ }
168
+
99
169
  /**
100
170
  * Creates a number of P2P clients for testing purposes.
101
171
  * @param numberOfPeers - The number of clients to create.
@@ -106,7 +176,16 @@ export async function makeTestP2PClients(numberOfPeers: number, testConfig: Make
106
176
  const clients: P2PClient[] = [];
107
177
  const peerIdPrivateKeys = generatePeerIdPrivateKeys(numberOfPeers);
108
178
 
109
- const ports = await getPorts(numberOfPeers);
179
+ let ports = [];
180
+ while (true) {
181
+ try {
182
+ ports = await getPorts(numberOfPeers);
183
+ break;
184
+ } catch {
185
+ await sleep(1000);
186
+ }
187
+ }
188
+
110
189
  const peerEnrs = await makeEnrs(peerIdPrivateKeys, ports, testConfig.p2pBaseConfig);
111
190
 
112
191
  for (let i = 0; i < numberOfPeers; i++) {
@@ -117,6 +196,17 @@ export async function makeTestP2PClients(numberOfPeers: number, testConfig: Make
117
196
  clients.push(client);
118
197
  }
119
198
 
120
- await Promise.all(clients.map(client => client.isReady()));
121
- return clients;
199
+ return clients.map((client, index) => {
200
+ return {
201
+ client,
202
+ peerPrivateKey: peerIdPrivateKeys[index],
203
+ port: ports[index],
204
+ enr: peerEnrs[index],
205
+ };
206
+ });
207
+ }
208
+
209
+ export async function startTestP2PClients(clients: P2PClient[]) {
210
+ await Promise.all(clients.map(c => c.start()));
211
+ await retryUntil(() => clients.every(c => c.isReady()), 'p2p clients started', 10, 0.5);
122
212
  }
@@ -0,0 +1,188 @@
1
+ import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
+ import { type Logger, createLogger } from '@aztec/foundation/log';
3
+ import type { AztecAsyncKVStore } from '@aztec/kv-store';
4
+ import type { L2BlockSource } from '@aztec/stdlib/block';
5
+ import type { ContractDataSource } from '@aztec/stdlib/contract';
6
+ import type { ClientProtocolCircuitVerifier, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
7
+ import { P2PClientType } from '@aztec/stdlib/p2p';
8
+ import type { TelemetryClient } from '@aztec/telemetry-client';
9
+
10
+ import type { GossipsubEvents, GossipsubMessage } from '@chainsafe/libp2p-gossipsub';
11
+ import type { MsgIdStr, PeerIdStr, PublishOpts, TopicStr } from '@chainsafe/libp2p-gossipsub/types';
12
+ import {
13
+ type Libp2pStatus,
14
+ type PeerId,
15
+ type PublishResult,
16
+ type TopicValidatorResult,
17
+ TypedEventEmitter,
18
+ } from '@libp2p/interface';
19
+
20
+ import type { P2PConfig } from '../config.js';
21
+ import type { MemPools } from '../mem_pools/interface.js';
22
+ import { DummyPeerDiscoveryService, DummyPeerManager, DummyReqResp, LibP2PService } from '../services/index.js';
23
+ import type { ReqRespInterface } from '../services/reqresp/interface.js';
24
+ import { GossipSubEvent } from '../types/index.js';
25
+ import type { PubSubLibp2p } from '../util.js';
26
+
27
+ type GossipSubService = PubSubLibp2p['services']['pubsub'];
28
+
29
+ /**
30
+ * Given a mock gossip sub network, returns a factory function that creates an instance LibP2PService connected to it.
31
+ * Designed to be used in tests in P2PClientDeps.p2pServiceFactory.
32
+ */
33
+ export function getMockPubSubP2PServiceFactory<T extends P2PClientType>(
34
+ network: MockGossipSubNetwork,
35
+ ): (...args: Parameters<(typeof LibP2PService<T>)['new']>) => Promise<LibP2PService<T>> {
36
+ return (
37
+ clientType: P2PClientType,
38
+ config: P2PConfig,
39
+ peerId: PeerId,
40
+ deps: {
41
+ packageVersion: string;
42
+ mempools: MemPools<T>;
43
+ l2BlockSource: L2BlockSource & ContractDataSource;
44
+ epochCache: EpochCacheInterface;
45
+ proofVerifier: ClientProtocolCircuitVerifier;
46
+ worldStateSynchronizer: WorldStateSynchronizer;
47
+ peerStore: AztecAsyncKVStore;
48
+ telemetry: TelemetryClient;
49
+ logger: Logger;
50
+ },
51
+ ) => {
52
+ deps.logger.verbose('Creating mock PubSub service');
53
+ const libp2p = new MockPubSub(peerId, network);
54
+ const peerManager = new DummyPeerManager(peerId, network);
55
+ const reqresp: ReqRespInterface = new DummyReqResp();
56
+ const peerDiscoveryService = new DummyPeerDiscoveryService();
57
+ const service = new LibP2PService<T>(
58
+ clientType as T,
59
+ config,
60
+ libp2p,
61
+ peerDiscoveryService,
62
+ reqresp,
63
+ peerManager,
64
+ deps.mempools,
65
+ deps.l2BlockSource,
66
+ deps.epochCache,
67
+ deps.proofVerifier,
68
+ deps.worldStateSynchronizer,
69
+ deps.telemetry,
70
+ deps.logger,
71
+ );
72
+
73
+ return Promise.resolve(service);
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Implementation of PubSub services that relies on a mock gossip sub network.
79
+ * This is used in tests to simulate a gossip sub network without needing a real P2P network.
80
+ * All messages are sent to the mock network which then distributes them to subscribed peers.
81
+ */
82
+ export class MockPubSub implements PubSubLibp2p {
83
+ public status: Libp2pStatus = 'stopped';
84
+
85
+ private gossipSub: GossipSubService;
86
+
87
+ constructor(
88
+ public peerId: PeerId,
89
+ network: MockGossipSubNetwork,
90
+ ) {
91
+ this.gossipSub = new MockGossipSubService(peerId, network);
92
+ }
93
+
94
+ get services() {
95
+ return {
96
+ pubsub: this.gossipSub,
97
+ };
98
+ }
99
+
100
+ start(): void | Promise<void> {
101
+ this.status = 'started';
102
+ return Promise.resolve();
103
+ }
104
+ stop(): void | Promise<void> {
105
+ this.status = 'stopped';
106
+ return Promise.resolve();
107
+ }
108
+ }
109
+
110
+ class MockGossipSubService extends TypedEventEmitter<GossipsubEvents> implements GossipSubService {
111
+ private logger = createLogger('p2p:test:mock-gossipsub');
112
+ public subscribedTopics: Set<TopicStr> = new Set();
113
+ public readonly direct = new Set<string>();
114
+
115
+ constructor(
116
+ public peerId: PeerId,
117
+ private network: MockGossipSubNetwork,
118
+ ) {
119
+ super();
120
+ network.registerPeer(this);
121
+ }
122
+
123
+ score = {
124
+ score: (_peerId: PeerIdStr) => 0,
125
+ };
126
+
127
+ publish(topic: TopicStr, data: Uint8Array, _opts?: PublishOpts): Promise<PublishResult> {
128
+ this.logger.debug(`Publishing message on topic ${topic}`, { topic, sender: this.peerId.toString() });
129
+ this.network.publishToPeers(topic, data, this.peerId);
130
+ return Promise.resolve({ recipients: this.network.getPeers().filter(peer => !this.peerId.equals(peer)) });
131
+ }
132
+
133
+ receive(msg: GossipsubMessage) {
134
+ if (msg.propagationSource.equals(this.peerId)) {
135
+ return; // Ignore messages from self
136
+ }
137
+ this.logger.debug(`Received message on topic ${msg.msg.topic}`, { ...msg });
138
+ this.safeDispatchEvent<GossipsubMessage>(GossipSubEvent.MESSAGE, { detail: msg });
139
+ }
140
+
141
+ subscribe(topic: TopicStr): void {
142
+ this.logger.debug(`Subscribed to topic ${topic}`, { topic });
143
+ this.subscribedTopics.add(topic);
144
+ }
145
+
146
+ reportMessageValidationResult(msgId: MsgIdStr, propagationSource: PeerIdStr, acceptance: TopicValidatorResult): void {
147
+ this.logger.debug(
148
+ `Reported message validation result ${acceptance} for msgId ${msgId} from source ${propagationSource}`,
149
+ { msgId, propagationSource, acceptance },
150
+ );
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Mock gossip sub network used for testing.
156
+ * All instances of MockGossipSubService connected to the same network will instantly receive the same messages.
157
+ */
158
+ export class MockGossipSubNetwork {
159
+ private peers: MockGossipSubService[] = [];
160
+ private nextMsgId = 0;
161
+
162
+ private logger = createLogger('p2p:test:mock-gossipsub-network');
163
+
164
+ public getPeers(): PeerId[] {
165
+ return this.peers.map(peer => peer.peerId);
166
+ }
167
+
168
+ public registerPeer(peer: MockGossipSubService): void {
169
+ this.peers.push(peer);
170
+ }
171
+
172
+ public publishToPeers(topic: TopicStr, data: Uint8Array, sender: PeerId): void {
173
+ const msgId = (this.nextMsgId++).toString();
174
+ this.logger.debug(`Network is distributing message on topic ${topic}`, {
175
+ topic,
176
+ size: data.length,
177
+ sender: sender.toString(),
178
+ msgId,
179
+ });
180
+
181
+ const gossipSubMsg: GossipsubMessage = { msgId, msg: { type: 'unsigned', topic, data }, propagationSource: sender };
182
+ for (const peer of this.peers) {
183
+ if (peer.subscribedTopics.has(topic)) {
184
+ peer.receive(gossipSubMsg);
185
+ }
186
+ }
187
+ }
188
+ }