@aztec/p2p 0.0.0-test.1 → 0.0.1-commit.5476d83

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,9 +1,22 @@
1
1
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { SlotNumber } from '@aztec/foundation/branded-types';
2
3
  import { createLogger } from '@aztec/foundation/log';
4
+ import { DateProvider } from '@aztec/foundation/timer';
3
5
  import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton } from '@aztec/kv-store';
4
- import type { L2Block, L2BlockId, L2BlockSource, L2BlockStreamEvent, L2Tips } from '@aztec/stdlib/block';
5
- import type { P2PApi, PeerInfo, ProverCoordination } from '@aztec/stdlib/interfaces/server';
6
- import type { BlockAttestation, BlockProposal, P2PClientType } from '@aztec/stdlib/p2p';
6
+ import type {
7
+ EthAddress,
8
+ L2Block,
9
+ L2BlockId,
10
+ L2BlockSource,
11
+ L2BlockStream,
12
+ L2BlockStreamEvent,
13
+ L2Tips,
14
+ PublishedL2Block,
15
+ } from '@aztec/stdlib/block';
16
+ import type { ContractDataSource } from '@aztec/stdlib/contract';
17
+ import { getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
18
+ import { type PeerInfo, tryStop } from '@aztec/stdlib/interfaces/server';
19
+ import { BlockAttestation, type BlockProposal, type P2PClientType } from '@aztec/stdlib/p2p';
7
20
  import type { Tx, TxHash } from '@aztec/stdlib/tx';
8
21
  import {
9
22
  Attributes,
@@ -14,152 +27,24 @@ import {
14
27
  trackSpan,
15
28
  } from '@aztec/telemetry-client';
16
29
 
17
- import type { ENR } from '@chainsafe/enr';
30
+ import type { PeerId } from '@libp2p/interface';
31
+ import type { ENR } from '@nethermindeth/enr';
18
32
 
19
33
  import { type P2PConfig, getP2PDefaultConfig } from '../config.js';
20
34
  import type { AttestationPool } from '../mem_pools/attestation_pool/attestation_pool.js';
21
35
  import type { MemPools } from '../mem_pools/interface.js';
22
36
  import type { TxPool } from '../mem_pools/tx_pool/index.js';
23
- import { ReqRespSubProtocol } from '../services/reqresp/interface.js';
24
- import type { P2PService } from '../services/service.js';
25
-
26
- /**
27
- * Enum defining the possible states of the p2p client.
28
- */
29
- export enum P2PClientState {
30
- IDLE,
31
- SYNCHING,
32
- RUNNING,
33
- STOPPED,
34
- }
35
-
36
- /**
37
- * The synchronization status of the P2P client.
38
- */
39
- export interface P2PSyncState {
40
- /**
41
- * The current state of the p2p client.
42
- */
43
- state: P2PClientState;
44
- /**
45
- * The block number that the p2p client is synced to.
46
- */
47
- syncedToL2Block: L2BlockId;
48
- }
49
-
50
- /**
51
- * Interface of a P2P client.
52
- **/
53
- export type P2P<T extends P2PClientType = P2PClientType.Full> = ProverCoordination &
54
- P2PApi<T> & {
55
- /**
56
- * Broadcasts a block proposal to other peers.
57
- *
58
- * @param proposal - the block proposal
59
- */
60
- broadcastProposal(proposal: BlockProposal): void;
61
-
62
- /**
63
- * Registers a callback from the validator client that determines how to behave when
64
- * foreign block proposals are received
65
- *
66
- * @param handler - A function taking a received block proposal and producing an attestation
67
- */
68
- // REVIEW: https://github.com/AztecProtocol/aztec-packages/issues/7963
69
- // ^ This pattern is not my favorite (md)
70
- registerBlockProposalHandler(handler: (block: BlockProposal) => Promise<BlockAttestation | undefined>): void;
71
-
72
- /**
73
- * Request a list of transactions from another peer by their tx hashes.
74
- * @param txHashes - Hashes of the txs to query.
75
- * @returns A list of transactions or undefined if the transactions are not found.
76
- */
77
- requestTxs(txHashes: TxHash[]): Promise<(Tx | undefined)[]>;
78
-
79
- /**
80
- * Request a transaction from another peer by its tx hash.
81
- * @param txHash - Hash of the tx to query.
82
- */
83
- requestTxByHash(txHash: TxHash): Promise<Tx | undefined>;
84
-
85
- /**
86
- * Verifies the 'tx' and, if valid, adds it to local tx pool and forwards it to other peers.
87
- * @param tx - The transaction.
88
- **/
89
- sendTx(tx: Tx): Promise<void>;
90
-
91
- /**
92
- * Deletes 'txs' from the pool, given hashes.
93
- * NOT used if we use sendTx as reconcileTxPool will handle this.
94
- * @param txHashes - Hashes to check.
95
- **/
96
- deleteTxs(txHashes: TxHash[]): Promise<void>;
97
-
98
- /**
99
- * Returns a transaction in the transaction pool by its hash.
100
- * @param txHash - Hash of tx to return.
101
- * @returns A single tx or undefined.
102
- */
103
- getTxByHashFromPool(txHash: TxHash): Promise<Tx | undefined>;
104
-
105
- /**
106
- * Returns a transaction in the transaction pool by its hash, requesting it from the network if it is not found.
107
- * @param txHash - Hash of tx to return.
108
- * @returns A single tx or undefined.
109
- */
110
- getTxByHash(txHash: TxHash): Promise<Tx | undefined>;
111
-
112
- /**
113
- * Returns an archived transaction from the transaction pool by its hash.
114
- * @param txHash - Hash of tx to return.
115
- * @returns A single tx or undefined.
116
- */
117
- getArchivedTxByHash(txHash: TxHash): Promise<Tx | undefined>;
118
-
119
- /**
120
- * Returns whether the given tx hash is flagged as pending or mined.
121
- * @param txHash - Hash of the tx to query.
122
- * @returns Pending or mined depending on its status, or undefined if not found.
123
- */
124
- getTxStatus(txHash: TxHash): Promise<'pending' | 'mined' | undefined>;
125
-
126
- /** Returns an iterator over pending txs on the mempool. */
127
- iteratePendingTxs(): AsyncIterableIterator<Tx>;
128
-
129
- /** Returns the number of pending txs in the mempool. */
130
- getPendingTxCount(): Promise<number>;
131
-
132
- /**
133
- * Starts the p2p client.
134
- * @returns A promise signalling the completion of the block sync.
135
- */
136
- start(): Promise<void>;
137
-
138
- /**
139
- * Stops the p2p client.
140
- * @returns A promise signalling the completion of the stop process.
141
- */
142
- stop(): Promise<void>;
143
-
144
- /**
145
- * Indicates if the p2p client is ready for transaction submission.
146
- * @returns A boolean flag indicating readiness.
147
- */
148
- isReady(): boolean;
149
-
150
- /**
151
- * Returns the current status of the p2p client.
152
- */
153
- getStatus(): Promise<P2PSyncState>;
154
-
155
- /**
156
- * Returns the ENR of this node, if any.
157
- */
158
- getEnr(): ENR | undefined;
159
-
160
- /** Identifies a p2p client. */
161
- isP2PClient(): true;
162
- };
37
+ import type { AuthRequest, StatusMessage } from '../services/index.js';
38
+ import {
39
+ ReqRespSubProtocol,
40
+ type ReqRespSubProtocolHandler,
41
+ type ReqRespSubProtocolValidators,
42
+ } from '../services/reqresp/interface.js';
43
+ import { chunkTxHashesRequest } from '../services/reqresp/protocols/tx.js';
44
+ import type { P2PBlockReceivedCallback, P2PService } from '../services/service.js';
45
+ import { TxCollection } from '../services/tx_collection/tx_collection.js';
46
+ import { TxProvider } from '../services/tx_provider.js';
47
+ import { type P2P, P2PClientState, type P2PSyncState } from './interface.js';
163
48
 
164
49
  /**
165
50
  * The P2P client implementation.
@@ -176,20 +61,24 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
176
61
  private syncResolve?: () => void = undefined;
177
62
  private latestBlockNumberAtStart = -1;
178
63
  private provenBlockNumberAtStart = -1;
64
+ private finalizedBlockNumberAtStart = -1;
179
65
 
180
66
  private synchedBlockHashes: AztecAsyncMap<number, string>;
181
67
  private synchedLatestBlockNumber: AztecAsyncSingleton<number>;
182
68
  private synchedProvenBlockNumber: AztecAsyncSingleton<number>;
69
+ private synchedFinalizedBlockNumber: AztecAsyncSingleton<number>;
70
+ private synchedLatestSlot: AztecAsyncSingleton<bigint>;
183
71
 
184
72
  private txPool: TxPool;
185
73
  private attestationPool: T extends P2PClientType.Full ? AttestationPool : undefined;
186
74
 
187
- /** How many slots to keep attestations for. */
188
- private keepAttestationsInPoolFor: number;
189
- /** How many slots to keep proven txs for. */
190
- private keepProvenTxsFor: number;
75
+ private config: P2PConfig;
191
76
 
192
- private blockStream;
77
+ private blockStream: L2BlockStream | undefined;
78
+
79
+ private txProvider: TxProvider;
80
+
81
+ private validatorAddresses: EthAddress[] = [];
193
82
 
194
83
  /**
195
84
  * In-memory P2P client constructor.
@@ -197,47 +86,79 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
197
86
  * @param l2BlockSource - P2P client's source for fetching existing blocks.
198
87
  * @param txPool - The client's instance of a transaction pool. Defaults to in-memory implementation.
199
88
  * @param p2pService - The concrete instance of p2p networking to use.
200
- * @param keepProvenTxsFor - How many blocks have to pass after a block is proven before its txs are deleted (zero to delete immediately once proven).
201
89
  * @param log - A logger.
202
90
  */
203
91
  constructor(
204
92
  _clientType: T,
205
- store: AztecAsyncKVStore,
206
- private l2BlockSource: L2BlockSource,
93
+ private store: AztecAsyncKVStore,
94
+ private l2BlockSource: L2BlockSource & ContractDataSource,
207
95
  mempools: MemPools<T>,
208
96
  private p2pService: P2PService,
97
+ private txCollection: TxCollection,
209
98
  config: Partial<P2PConfig> = {},
210
- telemetry: TelemetryClient = getTelemetryClient(),
99
+ private _dateProvider: DateProvider = new DateProvider(),
100
+ private telemetry: TelemetryClient = getTelemetryClient(),
211
101
  private log = createLogger('p2p'),
212
102
  ) {
213
103
  super(telemetry, 'P2PClient');
214
104
 
215
- const { keepProvenTxsInPoolFor, blockCheckIntervalMS, blockRequestBatchSize, keepAttestationsInPoolFor } = {
216
- ...getP2PDefaultConfig(),
217
- ...config,
218
- };
219
- this.keepProvenTxsFor = keepProvenTxsInPoolFor;
220
- this.keepAttestationsInPoolFor = keepAttestationsInPoolFor;
221
-
222
- const tracer = telemetry.getTracer('P2PL2BlockStream');
223
- const logger = createLogger('p2p:l2-block-stream');
224
- this.blockStream = new TraceableL2BlockStream(l2BlockSource, this, this, tracer, 'P2PL2BlockStream', logger, {
225
- batchSize: blockRequestBatchSize,
226
- pollIntervalMS: blockCheckIntervalMS,
105
+ this.config = { ...getP2PDefaultConfig(), ...config };
106
+ this.txPool = mempools.txPool;
107
+ this.attestationPool = mempools.attestationPool!;
108
+
109
+ this.txProvider = new TxProvider(
110
+ this.txCollection,
111
+ this.txPool,
112
+ this,
113
+ this.log.createChild('tx-provider'),
114
+ this.telemetry,
115
+ );
116
+
117
+ // Default to collecting all txs when we see a valid proposal
118
+ // This can be overridden by the validator client to attest, and it will call getTxsForBlockProposal on its own
119
+ // TODO(palla/txs): We should not trigger a request for txs on a proposal before fully validating it. We need to bring
120
+ // validator-client code into here so we can validate a proposal is reasonable.
121
+ this.registerBlockProposalHandler(async (block, sender) => {
122
+ this.log.debug(`Received block proposal from ${sender.toString()}`);
123
+ // TODO(palla/txs): Need to subtract validatorReexecuteDeadlineMs from this deadline (see ValidatorClient.getReexecutionDeadline)
124
+ const constants = this.txCollection.getConstants();
125
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(block.slotNumber + 1), constants));
126
+ const deadline = new Date(nextSlotTimestampSeconds * 1000);
127
+ const parentBlock = await this.l2BlockSource.getBlockHeaderByArchive(block.payload.header.lastArchiveRoot);
128
+ if (!parentBlock) {
129
+ this.log.debug(`Cannot collect txs for proposal as parent block not found`);
130
+ return;
131
+ }
132
+ const blockNumber = parentBlock.getBlockNumber() + 1;
133
+ await this.txProvider.getTxsForBlockProposal(block, blockNumber, { pinnedPeer: sender, deadline });
134
+ return undefined;
227
135
  });
228
136
 
137
+ // REFACTOR: Try replacing these with an L2TipsStore
229
138
  this.synchedBlockHashes = store.openMap('p2p_pool_block_hashes');
230
139
  this.synchedLatestBlockNumber = store.openSingleton('p2p_pool_last_l2_block');
231
140
  this.synchedProvenBlockNumber = store.openSingleton('p2p_pool_last_proven_l2_block');
141
+ this.synchedFinalizedBlockNumber = store.openSingleton('p2p_pool_last_finalized_l2_block');
142
+ this.synchedLatestSlot = store.openSingleton('p2p_pool_last_l2_slot');
143
+ }
232
144
 
233
- this.txPool = mempools.txPool;
234
- this.attestationPool = mempools.attestationPool!;
145
+ public registerThisValidatorAddresses(addresses: EthAddress[]): void {
146
+ this.validatorAddresses = [...addresses];
147
+ this.p2pService.registerThisValidatorAddresses(this.validatorAddresses);
148
+ }
149
+
150
+ public clear(): Promise<void> {
151
+ return this.store.clear();
235
152
  }
236
153
 
237
154
  public isP2PClient(): true {
238
155
  return true;
239
156
  }
240
157
 
158
+ public getTxProvider(): TxProvider {
159
+ return this.txProvider;
160
+ }
161
+
241
162
  public getPeers(includePending?: boolean): Promise<PeerInfo[]> {
242
163
  return Promise.resolve(this.p2pService.getPeers(includePending));
243
164
  }
@@ -246,33 +167,48 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
246
167
  return this.synchedBlockHashes.getAsync(number);
247
168
  }
248
169
 
170
+ public updateP2PConfig(config: Partial<P2PConfig>): Promise<void> {
171
+ this.txPool.updateConfig(config);
172
+ this.p2pService.updateConfig(config);
173
+ return Promise.resolve();
174
+ }
175
+
249
176
  public async getL2Tips(): Promise<L2Tips> {
250
177
  const latestBlockNumber = await this.getSyncedLatestBlockNum();
251
178
  let latestBlockHash: string | undefined;
179
+
252
180
  const provenBlockNumber = await this.getSyncedProvenBlockNum();
253
181
  let provenBlockHash: string | undefined;
254
182
 
183
+ const finalizedBlockNumber = await this.getSyncedFinalizedBlockNum();
184
+ let finalizedBlockHash: string | undefined;
185
+
255
186
  if (latestBlockNumber > 0) {
256
187
  latestBlockHash = await this.synchedBlockHashes.getAsync(latestBlockNumber);
257
188
  if (typeof latestBlockHash === 'undefined') {
258
- this.log.warn(`Block hash for latest block ${latestBlockNumber} not found`);
259
- throw new Error();
189
+ throw new Error(`Block hash for latest block ${latestBlockNumber} not found in p2p client`);
260
190
  }
261
191
  }
262
192
 
263
193
  if (provenBlockNumber > 0) {
264
194
  provenBlockHash = await this.synchedBlockHashes.getAsync(provenBlockNumber);
265
195
  if (typeof provenBlockHash === 'undefined') {
266
- this.log.warn(`Block hash for proven block ${provenBlockNumber} not found`);
267
- throw new Error();
196
+ throw new Error(`Block hash for proven block ${provenBlockNumber} not found in p2p client`);
268
197
  }
269
198
  }
270
199
 
271
- return Promise.resolve({
200
+ if (finalizedBlockNumber > 0) {
201
+ finalizedBlockHash = await this.synchedBlockHashes.getAsync(finalizedBlockNumber);
202
+ if (typeof finalizedBlockHash === 'undefined') {
203
+ throw new Error(`Block hash for finalized block ${finalizedBlockNumber} not found in p2p client`);
204
+ }
205
+ }
206
+
207
+ return {
272
208
  latest: { hash: latestBlockHash!, number: latestBlockNumber },
273
209
  proven: { hash: provenBlockHash!, number: provenBlockNumber },
274
- finalized: { hash: provenBlockHash!, number: provenBlockNumber },
275
- });
210
+ finalized: { hash: finalizedBlockHash!, number: finalizedBlockNumber },
211
+ };
276
212
  }
277
213
 
278
214
  public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
@@ -281,17 +217,26 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
281
217
  case 'blocks-added':
282
218
  await this.handleLatestL2Blocks(event.blocks);
283
219
  break;
284
- case 'chain-finalized':
220
+ case 'chain-finalized': {
285
221
  // TODO (alexg): I think we can prune the block hashes map here
222
+ await this.setBlockHash(event.block);
223
+ const from = (await this.getSyncedFinalizedBlockNum()) + 1;
224
+ const limit = event.block.number - from + 1;
225
+ if (limit > 0) {
226
+ await this.handleFinalizedL2Blocks(await this.l2BlockSource.getBlocks(from, limit));
227
+ }
286
228
  break;
229
+ }
287
230
  case 'chain-proven': {
288
- const from = (await this.getSyncedProvenBlockNum()) + 1;
289
- const limit = event.blockNumber - from + 1;
290
- await this.handleProvenL2Blocks(await this.l2BlockSource.getBlocks(from, limit));
231
+ await this.setBlockHash(event.block);
232
+ this.txCollection.stopCollectingForBlocksUpTo(event.block.number);
233
+ await this.synchedProvenBlockNumber.set(event.block.number);
291
234
  break;
292
235
  }
293
236
  case 'chain-pruned':
294
- await this.handlePruneL2Blocks(event.blockNumber);
237
+ await this.setBlockHash(event.block);
238
+ this.txCollection.stopCollectingForBlocksAfter(event.block.number);
239
+ await this.handlePruneL2Blocks(event.block.number);
295
240
  break;
296
241
  default: {
297
242
  const _: never = event;
@@ -300,6 +245,12 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
300
245
  }
301
246
  }
302
247
 
248
+ private async setBlockHash(block: L2BlockId): Promise<void> {
249
+ if (block.hash !== undefined) {
250
+ await this.synchedBlockHashes.set(block.number, block.hash.toString());
251
+ }
252
+ }
253
+
303
254
  #assertIsReady() {
304
255
  // this.log.info('Checking if p2p client is ready, current state: ', this.currentState);
305
256
  if (!this.isReady()) {
@@ -320,123 +271,179 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
320
271
  }
321
272
 
322
273
  // get the current latest block numbers
323
- this.latestBlockNumberAtStart = await this.l2BlockSource.getBlockNumber();
324
- this.provenBlockNumberAtStart = await this.l2BlockSource.getProvenBlockNumber();
274
+ const latestBlockNumbers = await this.l2BlockSource.getL2Tips();
275
+ this.latestBlockNumberAtStart = latestBlockNumbers.latest.number;
276
+ this.provenBlockNumberAtStart = latestBlockNumbers.proven.number;
277
+ this.finalizedBlockNumberAtStart = latestBlockNumbers.finalized.number;
325
278
 
326
279
  const syncedLatestBlock = (await this.getSyncedLatestBlockNum()) + 1;
327
280
  const syncedProvenBlock = (await this.getSyncedProvenBlockNum()) + 1;
281
+ const syncedFinalizedBlock = (await this.getSyncedFinalizedBlockNum()) + 1;
328
282
 
329
- // if there are blocks to be retrieved, go to a synching state
330
- if (syncedLatestBlock <= this.latestBlockNumberAtStart || syncedProvenBlock <= this.provenBlockNumberAtStart) {
283
+ if (
284
+ (await this.txPool.isEmpty()) &&
285
+ (this.attestationPool === undefined || (await this.attestationPool?.isEmpty()))
286
+ ) {
287
+ // if mempools are empty, we don't care about syncing prior blocks
288
+ this.initBlockStream(this.latestBlockNumberAtStart);
289
+ this.setCurrentState(P2PClientState.RUNNING);
290
+ this.syncPromise = Promise.resolve();
291
+ await this.p2pService.start();
292
+ this.log.info(`Starting p2p client from block ${this.latestBlockNumberAtStart} with empty mempools`);
293
+ } else if (
294
+ syncedLatestBlock <= this.latestBlockNumberAtStart ||
295
+ syncedProvenBlock <= this.provenBlockNumberAtStart ||
296
+ syncedFinalizedBlock <= this.finalizedBlockNumberAtStart
297
+ ) {
298
+ // if there are blocks to be retrieved, go to a synching state
299
+ // this gets resolved on `startServiceIfSynched`
300
+ this.initBlockStream();
331
301
  this.setCurrentState(P2PClientState.SYNCHING);
332
302
  this.syncPromise = new Promise(resolve => {
333
303
  this.syncResolve = resolve;
334
304
  });
335
- this.log.verbose(`Starting sync from ${syncedLatestBlock} (last proven ${syncedProvenBlock})`);
305
+ this.log.info(`Initiating p2p sync from ${syncedLatestBlock}`, {
306
+ syncedLatestBlock,
307
+ syncedProvenBlock,
308
+ syncedFinalizedBlock,
309
+ });
336
310
  } else {
337
311
  // if no blocks to be retrieved, go straight to running
312
+ this.initBlockStream();
338
313
  this.setCurrentState(P2PClientState.RUNNING);
339
314
  this.syncPromise = Promise.resolve();
340
315
  await this.p2pService.start();
341
- this.log.debug(`Block ${syncedLatestBlock} (proven ${syncedProvenBlock}) already beyond current block`);
316
+ this.log.info(`Starting P2P client synced to ${syncedLatestBlock}`, {
317
+ syncedLatestBlock,
318
+ syncedProvenBlock,
319
+ syncedFinalizedBlock,
320
+ });
342
321
  }
343
322
 
344
- this.blockStream.start();
345
- this.log.verbose(`Started block downloader from block ${syncedLatestBlock}`);
346
-
323
+ this.blockStream!.start();
324
+ await this.txCollection.start();
347
325
  return this.syncPromise;
348
326
  }
349
327
 
328
+ addReqRespSubProtocol(
329
+ subProtocol: ReqRespSubProtocol,
330
+ handler: ReqRespSubProtocolHandler,
331
+ validator: ReqRespSubProtocolValidators[ReqRespSubProtocol],
332
+ ): Promise<void> {
333
+ return this.p2pService.addReqRespSubProtocol(subProtocol, handler, validator);
334
+ }
335
+
336
+ private initBlockStream(startingBlock?: number) {
337
+ if (!this.blockStream) {
338
+ const { blockRequestBatchSize: batchSize, blockCheckIntervalMS: pollIntervalMS } = this.config;
339
+ this.blockStream = new TraceableL2BlockStream(
340
+ this.l2BlockSource,
341
+ this,
342
+ this,
343
+ this.telemetry.getTracer('P2PL2BlockStream'),
344
+ 'P2PL2BlockStream',
345
+ createLogger(`${this.log.module}:l2-block-stream`),
346
+ { batchSize, pollIntervalMS, startingBlock },
347
+ );
348
+ }
349
+ }
350
+
350
351
  /**
351
352
  * Allows consumers to stop the instance of the P2P client.
352
353
  * 'ready' will now return 'false' and the running promise that keeps the client synced is interrupted.
353
354
  */
354
355
  public async stop() {
355
356
  this.log.debug('Stopping p2p client...');
357
+ await tryStop(this.txCollection);
358
+ this.log.debug('Stopped tx collection service');
356
359
  await this.p2pService.stop();
357
360
  this.log.debug('Stopped p2p service');
358
- await this.blockStream.stop();
361
+ await this.blockStream?.stop();
359
362
  this.log.debug('Stopped block downloader');
360
363
  await this.runningPromise;
361
364
  this.setCurrentState(P2PClientState.STOPPED);
362
- this.log.info('P2P client stopped.');
365
+ this.log.info('P2P client stopped');
366
+ }
367
+
368
+ /** Triggers a sync to the archiver. Used for testing. */
369
+ public async sync() {
370
+ this.initBlockStream();
371
+ await this.blockStream!.sync();
363
372
  }
364
373
 
365
374
  @trackSpan('p2pClient.broadcastProposal', async proposal => ({
366
- [Attributes.BLOCK_NUMBER]: proposal.blockNumber.toNumber(),
367
- [Attributes.SLOT_NUMBER]: proposal.slotNumber.toNumber(),
375
+ [Attributes.SLOT_NUMBER]: proposal.slotNumber,
368
376
  [Attributes.BLOCK_ARCHIVE]: proposal.archive.toString(),
369
- [Attributes.P2P_ID]: (await proposal.p2pMessageIdentifier()).toString(),
377
+ [Attributes.P2P_ID]: (await proposal.p2pMessageLoggingIdentifier()).toString(),
370
378
  }))
371
- public broadcastProposal(proposal: BlockProposal): void {
372
- this.log.verbose(`Broadcasting proposal for slot ${proposal.slotNumber.toNumber()} to peers`);
379
+ public broadcastProposal(proposal: BlockProposal): Promise<void> {
380
+ this.log.verbose(`Broadcasting proposal for slot ${proposal.slotNumber} to peers`);
373
381
  return this.p2pService.propagate(proposal);
374
382
  }
375
383
 
376
- public async getAttestationsForSlot(slot: bigint, proposalId: string): Promise<BlockAttestation[]> {
377
- return (await this.attestationPool?.getAttestationsForSlot(slot, proposalId)) ?? [];
384
+ public async broadcastAttestations(attestations: BlockAttestation[]): Promise<void> {
385
+ this.log.verbose(`Broadcasting ${attestations.length} attestations to peers`);
386
+ await Promise.all(attestations.map(att => this.p2pService.propagate(att)));
378
387
  }
379
388
 
380
- // REVIEW: https://github.com/AztecProtocol/aztec-packages/issues/7963
381
- // ^ This pattern is not my favorite (md)
382
- public registerBlockProposalHandler(handler: (block: BlockProposal) => Promise<BlockAttestation | undefined>): void {
383
- this.p2pService.registerBlockReceivedCallback(handler);
389
+ public async getAttestationsForSlot(slot: SlotNumber, proposalId?: string): Promise<BlockAttestation[]> {
390
+ return (
391
+ (await (proposalId
392
+ ? this.attestationPool?.getAttestationsForSlotAndProposal(slot, proposalId)
393
+ : this.attestationPool?.getAttestationsForSlot(slot))) ?? []
394
+ );
384
395
  }
385
396
 
386
- /**
387
- * Requests the transactions with the given hashes from the network.
388
- *
389
- * If a transaction can be retrieved, it will be returned, if not an undefined
390
- * will be returned. In place.
391
- *
392
- * @param txHashes - The hashes of the transactions to request.
393
- * @returns A promise that resolves to an array of transactions or undefined.
394
- */
395
- public async requestTxs(txHashes: TxHash[]): Promise<(Tx | undefined)[]> {
396
- const res = await this.p2pService.sendBatchRequest(ReqRespSubProtocol.TX, txHashes);
397
- return Promise.resolve(res ?? []);
397
+ public addAttestations(attestations: BlockAttestation[]): Promise<void> {
398
+ return this.attestationPool?.addAttestations(attestations) ?? Promise.resolve();
398
399
  }
399
400
 
400
- /**
401
- * Uses the Request Response protocol to request a transaction from the network.
402
- *
403
- * If the underlying request response protocol fails, then we return undefined.
404
- * If it succeeds then we add the transaction to our transaction pool and return.
405
- *
406
- * @param txHash - The hash of the transaction to request.
407
- * @returns A promise that resolves to a transaction or undefined.
408
- */
409
- public async requestTxByHash(txHash: TxHash): Promise<Tx | undefined> {
410
- const tx = await this.p2pService.sendRequest(ReqRespSubProtocol.TX, txHash);
411
-
412
- if (tx) {
413
- this.log.debug(`Received tx ${txHash.toString()} from peer`);
414
- await this.txPool.addTxs([tx]);
415
- } else {
416
- this.log.debug(`Failed to receive tx ${txHash.toString()} from peer`);
417
- }
401
+ public deleteAttestation(attestation: BlockAttestation): Promise<void> {
402
+ return this.attestationPool?.deleteAttestations([attestation]) ?? Promise.resolve();
403
+ }
418
404
 
419
- return tx;
405
+ // REVIEW: https://github.com/AztecProtocol/aztec-packages/issues/7963
406
+ // ^ This pattern is not my favorite (md)
407
+ public registerBlockProposalHandler(handler: P2PBlockReceivedCallback): void {
408
+ this.p2pService.registerBlockReceivedCallback(handler);
420
409
  }
421
410
 
422
411
  /**
423
412
  * Uses the batched Request Response protocol to request a set of transactions from the network.
424
413
  */
425
- public async requestTxsByHash(txHashes: TxHash[]): Promise<Tx[]> {
426
- const txs = (await this.p2pService.sendBatchRequest(ReqRespSubProtocol.TX, txHashes)) ?? [];
427
- await this.txPool.addTxs(txs);
414
+ public async requestTxsByHash(txHashes: TxHash[], pinnedPeerId: PeerId | undefined): Promise<Tx[]> {
415
+ const timeoutMs = 8000; // Longer timeout for now
416
+ const maxRetryAttempts = 10; // Keep retrying within the timeout
417
+ const requests = chunkTxHashesRequest(txHashes);
418
+ const maxPeers = Math.min(Math.ceil(requests.length / 3), 10);
419
+
420
+ const txBatches = await this.p2pService.sendBatchRequest(
421
+ ReqRespSubProtocol.TX,
422
+ requests,
423
+ pinnedPeerId,
424
+ timeoutMs,
425
+ maxPeers,
426
+ maxRetryAttempts,
427
+ );
428
+
429
+ const txs = txBatches.flat();
430
+ if (txs.length > 0) {
431
+ await this.txPool.addTxs(txs);
432
+ }
433
+
428
434
  const txHashesStr = txHashes.map(tx => tx.toString()).join(', ');
429
- this.log.debug(`Received batched txs ${txHashesStr} (${txs.length} / ${txHashes.length}}) from peers`);
430
- return txs as Tx[];
435
+ this.log.debug(`Requested txs ${txHashesStr} (${txs.length} / ${txHashes.length}) from peers`);
436
+
437
+ // We return all transactions, even the not found ones to the caller, such they can handle missing items themselves.
438
+ return txs;
431
439
  }
432
440
 
433
- public getPendingTxs(): Promise<Tx[]> {
434
- return Promise.resolve(this.getTxs('pending'));
441
+ public getPendingTxs(limit?: number, after?: TxHash): Promise<Tx[]> {
442
+ return this.getTxs('pending', limit, after);
435
443
  }
436
444
 
437
- public async getPendingTxCount(): Promise<number> {
438
- const pendingTxs = await this.txPool.getPendingTxHashes();
439
- return pendingTxs.length;
445
+ public getPendingTxCount(): Promise<number> {
446
+ return this.txPool.getPendingTxCount();
440
447
  }
441
448
 
442
449
  public async *iteratePendingTxs(): AsyncIterableIterator<Tx> {
@@ -450,23 +457,60 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
450
457
 
451
458
  /**
452
459
  * Returns all transactions in the transaction pool.
460
+ * @param filter - The type of txs to return
461
+ * @param limit - How many txs to return
462
+ * @param after - If paginating, the last known tx hash. Will return txs after this hash
453
463
  * @returns An array of Txs.
454
464
  */
455
- public async getTxs(filter: 'all' | 'pending' | 'mined'): Promise<Tx[]> {
465
+ public async getTxs(filter: 'all' | 'pending' | 'mined', limit?: number, after?: TxHash): Promise<Tx[]> {
466
+ if (limit !== undefined && limit <= 0) {
467
+ throw new TypeError('limit must be greater than 0');
468
+ }
469
+
470
+ let txs: Tx[] | undefined = undefined;
471
+ let txHashes: TxHash[];
472
+
456
473
  if (filter === 'all') {
457
- return this.txPool.getAllTxs();
474
+ txs = await this.txPool.getAllTxs();
475
+ txHashes = await Promise.all(txs.map(tx => tx.getTxHash()));
458
476
  } else if (filter === 'mined') {
459
- const minedHashes = await this.txPool.getMinedTxHashes();
460
- const minedTx = await Promise.all(minedHashes.map(([txHash]) => this.txPool.getTxByHash(txHash)));
461
- return minedTx.filter((tx): tx is Tx => !!tx);
477
+ const minedTxHashes = await this.txPool.getMinedTxHashes();
478
+ txHashes = minedTxHashes.map(([txHash]) => txHash);
462
479
  } else if (filter === 'pending') {
463
- const pendingHashses = await this.txPool.getPendingTxHashes();
464
- const pendingTxs = await Promise.all(pendingHashses.map(txHash => this.txPool.getTxByHash(txHash)));
465
- return pendingTxs.filter((tx): tx is Tx => !!tx);
480
+ txHashes = await this.txPool.getPendingTxHashes();
466
481
  } else {
467
482
  const _: never = filter;
468
483
  throw new Error(`Unknown filter ${filter}`);
469
484
  }
485
+
486
+ let startIndex = 0;
487
+ let endIndex: number | undefined = undefined;
488
+
489
+ if (after) {
490
+ startIndex = txHashes.findIndex(txHash => after.equals(txHash));
491
+
492
+ // if we can't find the last tx in our set then return an empty array as pagination is no longer valid.
493
+ if (startIndex === -1) {
494
+ return [];
495
+ }
496
+
497
+ // increment by one because we don't want to return the same tx again
498
+ startIndex++;
499
+ }
500
+
501
+ if (limit !== undefined) {
502
+ endIndex = startIndex + limit;
503
+ }
504
+
505
+ txHashes = txHashes.slice(startIndex, endIndex);
506
+ if (txs) {
507
+ txs = txs.slice(startIndex, endIndex);
508
+ } else {
509
+ const maybeTxs = await Promise.all(txHashes.map(txHash => this.txPool.getTxByHash(txHash)));
510
+ txs = maybeTxs.filter((tx): tx is Tx => !!tx);
511
+ }
512
+
513
+ return txs;
470
514
  }
471
515
 
472
516
  /**
@@ -479,26 +523,25 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
479
523
  }
480
524
 
481
525
  /**
482
- * Returns a transaction in the transaction pool by its hash.
483
- * If the transaction is not in the pool, it will be requested from the network.
484
- * @param txHash - Hash of the transaction to look for in the pool.
485
- * @returns A single tx or undefined.
526
+ * Returns transactions in the transaction pool by hash.
527
+ * @param txHashes - Hashes of the transactions to look for.
528
+ * @returns The txs found, in the same order as the requested hashes. If a tx is not found, it will be undefined.
486
529
  */
487
- async getTxByHash(txHash: TxHash): Promise<Tx | undefined> {
488
- const tx = await this.txPool.getTxByHash(txHash);
489
- if (tx) {
490
- return tx;
491
- }
492
- return this.requestTxByHash(txHash);
530
+ getTxsByHashFromPool(txHashes: TxHash[]): Promise<(Tx | undefined)[]> {
531
+ return this.txPool.getTxsByHash(txHashes);
532
+ }
533
+
534
+ hasTxsInPool(txHashes: TxHash[]): Promise<boolean[]> {
535
+ return this.txPool.hasTxs(txHashes);
493
536
  }
494
537
 
495
538
  /**
496
539
  * Returns transactions in the transaction pool by hash.
497
540
  * If a transaction is not in the pool, it will be requested from the network.
498
541
  * @param txHashes - Hashes of the transactions to look for.
499
- * @returns The txs found, not necessarily on the same order as the hashes.
542
+ * @returns The txs found, or undefined if not found in the order requested.
500
543
  */
501
- async getTxsByHash(txHashes: TxHash[]): Promise<Tx[]> {
544
+ async getTxsByHash(txHashes: TxHash[], pinnedPeerId: PeerId | undefined): Promise<(Tx | undefined)[]> {
502
545
  const txs = await Promise.all(txHashes.map(txHash => this.txPool.getTxByHash(txHash)));
503
546
  const missingTxHashes = txs
504
547
  .map((tx, index) => [tx, index] as const)
@@ -509,8 +552,30 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
509
552
  return txs as Tx[];
510
553
  }
511
554
 
512
- const missingTxs = await this.requestTxsByHash(missingTxHashes);
513
- return txs.filter((tx): tx is Tx => !!tx).concat(missingTxs);
555
+ const missingTxs = await this.requestTxsByHash(missingTxHashes, pinnedPeerId);
556
+ // TODO: optimize
557
+ // Merge the found txs in order
558
+ const mergingTxs = txHashes.map(txHash => {
559
+ // Is it in the txs list from the mempool?
560
+ for (const tx of txs) {
561
+ if (tx !== undefined && tx.getTxHash().equals(txHash)) {
562
+ return tx;
563
+ }
564
+ }
565
+
566
+ // Is it in the fetched missing txs?
567
+ // Note: this is an O(n^2) operation, but we expect the number of missing txs to be small.
568
+ for (const tx of missingTxs) {
569
+ if (tx.getTxHash().equals(txHash)) {
570
+ return tx;
571
+ }
572
+ }
573
+
574
+ // Otherwise return undefined
575
+ return undefined;
576
+ });
577
+
578
+ return mergingTxs;
514
579
  }
515
580
 
516
581
  /**
@@ -528,9 +593,20 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
528
593
  * @returns Empty promise.
529
594
  **/
530
595
  public async sendTx(tx: Tx): Promise<void> {
596
+ const addedCount = await this.addTxsToPool([tx]);
597
+ const txAddedSuccessfully = addedCount === 1;
598
+ if (txAddedSuccessfully) {
599
+ await this.p2pService.propagate(tx);
600
+ }
601
+ }
602
+
603
+ /**
604
+ * Adds transactions to the pool. Does not send to peers or validate the txs.
605
+ * @param txs - The transactions.
606
+ **/
607
+ public async addTxsToPool(txs: Tx[]): Promise<number> {
531
608
  this.#assertIsReady();
532
- await this.txPool.addTxs([tx]);
533
- this.p2pService.propagate(tx);
609
+ return await this.txPool.addTxs(txs);
534
610
  }
535
611
 
536
612
  /**
@@ -538,7 +614,7 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
538
614
  * @param txHash - Hash of the tx to query.
539
615
  * @returns Pending or mined depending on its status, or undefined if not found.
540
616
  */
541
- public getTxStatus(txHash: TxHash): Promise<'pending' | 'mined' | undefined> {
617
+ public getTxStatus(txHash: TxHash): Promise<'pending' | 'mined' | 'deleted' | undefined> {
542
618
  return this.txPool.getTxStatus(txHash);
543
619
  }
544
620
 
@@ -585,6 +661,15 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
585
661
  return (await this.synchedProvenBlockNumber.getAsync()) ?? INITIAL_L2_BLOCK_NUM - 1;
586
662
  }
587
663
 
664
+ public async getSyncedFinalizedBlockNum(): Promise<number> {
665
+ return (await this.synchedFinalizedBlockNumber.getAsync()) ?? INITIAL_L2_BLOCK_NUM - 1;
666
+ }
667
+
668
+ /** Returns latest L2 slot for which we have seen an L2 block. */
669
+ public async getSyncedLatestSlot(): Promise<bigint> {
670
+ return (await this.synchedLatestSlot.getAsync()) ?? BigInt(0);
671
+ }
672
+
588
673
  /**
589
674
  * Method to check the status the p2p client.
590
675
  * @returns Information about p2p client status: state & syncedToBlockNum.
@@ -613,20 +698,7 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
613
698
  private async markTxsAsMinedFromBlocks(blocks: L2Block[]): Promise<void> {
614
699
  for (const block of blocks) {
615
700
  const txHashes = block.body.txEffects.map(txEffect => txEffect.txHash);
616
- await this.txPool.markAsMined(txHashes, block.number);
617
- }
618
- }
619
-
620
- /**
621
- * Deletes txs from these blocks.
622
- * @param blocks - A list of existing blocks with txs that the P2P client needs to ensure the tx pool is reconciled with.
623
- * @returns Empty promise.
624
- */
625
- private async deleteTxsFromBlocks(blocks: L2Block[]): Promise<void> {
626
- this.log.debug(`Deleting txs from blocks ${blocks[0].number} to ${blocks[blocks.length - 1].number}`);
627
- for (const block of blocks) {
628
- const txHashes = block.body.txEffects.map(txEffect => txEffect.txHash);
629
- await this.txPool.deleteTxs(txHashes);
701
+ await this.txPool.markAsMined(txHashes, block.getBlockHeader());
630
702
  }
631
703
  }
632
704
 
@@ -635,54 +707,83 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
635
707
  * @param blocks - A list of existing blocks with txs that the P2P client needs to ensure the tx pool is reconciled with.
636
708
  * @returns Empty promise.
637
709
  */
638
- private async handleLatestL2Blocks(blocks: L2Block[]): Promise<void> {
710
+ private async handleLatestL2Blocks(blocks: PublishedL2Block[]): Promise<void> {
639
711
  if (!blocks.length) {
640
712
  return Promise.resolve();
641
713
  }
642
714
 
643
- await this.markTxsAsMinedFromBlocks(blocks);
644
- const lastBlockNum = blocks[blocks.length - 1].number;
715
+ await this.markTxsAsMinedFromBlocks(blocks.map(b => b.block));
716
+ await this.startCollectingMissingTxs(blocks.map(b => b.block));
717
+
718
+ const lastBlock = blocks.at(-1)!.block;
719
+
645
720
  await Promise.all(
646
- blocks.map(async block => this.synchedBlockHashes.set(block.number, (await block.hash()).toString())),
721
+ blocks.map(async block =>
722
+ this.setBlockHash({
723
+ number: block.block.number,
724
+ hash: await block.block.hash().then(h => h.toString()),
725
+ }),
726
+ ),
647
727
  );
648
- await this.synchedLatestBlockNumber.set(lastBlockNum);
649
- this.log.verbose(`Synched to latest block ${lastBlockNum}`);
728
+
729
+ await this.synchedLatestBlockNumber.set(lastBlock.number);
730
+ await this.synchedLatestSlot.set(BigInt(lastBlock.header.getSlot()));
731
+ this.log.verbose(`Synched to latest block ${lastBlock.number}`);
650
732
  await this.startServiceIfSynched();
651
733
  }
652
734
 
735
+ /** Request txs for unproven blocks so the prover node has more chances to get them. */
736
+ private async startCollectingMissingTxs(blocks: L2Block[]): Promise<void> {
737
+ try {
738
+ // TODO(#15435): If the archiver has lagged behind L1, the reported proven block number may
739
+ // be much lower than the actual one, and it does not update until the pending chain is
740
+ // fully synced. This could lead to a ton of tx collection requests for blocks that
741
+ // are already proven, but the archiver has not yet updated its state. Until this is properly
742
+ // fixed, it is mitigated by the expiration date of collection requests, which depends on
743
+ // the slot number of the block.
744
+ const provenBlockNumber = await this.l2BlockSource.getProvenBlockNumber();
745
+ const unprovenBlocks = blocks.filter(block => block.number > provenBlockNumber);
746
+ for (const block of unprovenBlocks) {
747
+ const txHashes = block.body.txEffects.map(txEffect => txEffect.txHash);
748
+ const missingTxHashes = await this.txPool
749
+ .hasTxs(txHashes)
750
+ .then(availability => txHashes.filter((_, index) => !availability[index]));
751
+ if (missingTxHashes.length > 0) {
752
+ this.log.verbose(
753
+ `Starting collection of ${missingTxHashes.length} missing txs for unproven mined block ${block.number}`,
754
+ { missingTxHashes, blockNumber: block.number, blockHash: await block.hash().then(h => h.toString()) },
755
+ );
756
+ this.txCollection.startCollecting(block, missingTxHashes);
757
+ }
758
+ }
759
+ } catch (err) {
760
+ this.log.error(`Error while starting collection of missing txs for unproven blocks`, err);
761
+ }
762
+ }
763
+
653
764
  /**
654
- * Handles new proven blocks by deleting the txs in them, or by deleting the txs in blocks `keepProvenTxsFor` ago.
655
- * @param blocks - A list of proven L2 blocks.
765
+ * Handles new finalized blocks by deleting the txs and attestations in them.
766
+ * @param blocks - A list of finalized L2 blocks.
656
767
  * @returns Empty promise.
657
768
  */
658
- private async handleProvenL2Blocks(blocks: L2Block[]): Promise<void> {
769
+ private async handleFinalizedL2Blocks(blocks: L2Block[]): Promise<void> {
659
770
  if (!blocks.length) {
660
771
  return Promise.resolve();
661
772
  }
773
+ this.log.debug(`Handling finalized blocks ${blocks.length} up to ${blocks.at(-1)?.number}`);
662
774
 
663
- const firstBlockNum = blocks[0].number;
664
775
  const lastBlockNum = blocks[blocks.length - 1].number;
665
- const lastBlockSlot = blocks[blocks.length - 1].header.globalVariables.slotNumber.toBigInt();
666
-
667
- // If keepProvenTxsFor is 0, we delete all txs from all proven blocks.
668
- if (this.keepProvenTxsFor === 0) {
669
- await this.deleteTxsFromBlocks(blocks);
670
- } else if (lastBlockNum - this.keepProvenTxsFor >= INITIAL_L2_BLOCK_NUM) {
671
- const fromBlock = Math.max(INITIAL_L2_BLOCK_NUM, firstBlockNum - this.keepProvenTxsFor);
672
- const toBlock = lastBlockNum - this.keepProvenTxsFor;
673
- const limit = toBlock - fromBlock + 1;
674
- const blocksToDeleteTxsFrom = await this.l2BlockSource.getBlocks(fromBlock, limit, true);
675
- await this.deleteTxsFromBlocks(blocksToDeleteTxsFrom);
676
- }
776
+ const lastBlockSlot = blocks[blocks.length - 1].header.getSlot();
677
777
 
678
- // We delete attestations older than the last block slot minus the number of slots we want to keep in the pool.
679
- const lastBlockSlotMinusKeepAttestationsInPoolFor = lastBlockSlot - BigInt(this.keepAttestationsInPoolFor);
680
- if (lastBlockSlotMinusKeepAttestationsInPoolFor >= BigInt(INITIAL_L2_BLOCK_NUM)) {
681
- await this.attestationPool?.deleteAttestationsOlderThan(lastBlockSlotMinusKeepAttestationsInPoolFor);
682
- }
778
+ const txHashes = blocks.flatMap(block => block.body.txEffects.map(txEffect => txEffect.txHash));
779
+ this.log.debug(`Deleting ${txHashes.length} txs from pool from finalized blocks up to ${lastBlockNum}`);
780
+ await this.txPool.deleteTxs(txHashes, { permanently: true });
781
+ await this.txPool.cleanupDeletedMinedTxs(lastBlockNum);
782
+
783
+ await this.attestationPool?.deleteAttestationsOlderThan(lastBlockSlot);
683
784
 
684
- await this.synchedProvenBlockNumber.set(lastBlockNum);
685
- this.log.debug(`Synched to proven block ${lastBlockNum}`);
785
+ await this.synchedFinalizedBlockNumber.set(lastBlockNum);
786
+ this.log.debug(`Synched to finalized block ${lastBlockNum} at slot ${lastBlockSlot}`);
686
787
 
687
788
  await this.startServiceIfSynched();
688
789
  }
@@ -692,48 +793,70 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
692
793
  * @param latestBlock - The block number the chain was pruned to.
693
794
  */
694
795
  private async handlePruneL2Blocks(latestBlock: number): Promise<void> {
695
- const txsToDelete: TxHash[] = [];
796
+ const txsToDelete = new Map<string, TxHash>();
797
+ const minedTxs = await this.txPool.getMinedTxHashes();
798
+
799
+ // Find transactions that reference pruned blocks in their historical header
696
800
  for (const tx of await this.txPool.getAllTxs()) {
697
801
  // every tx that's been generated against a block that has now been pruned is no longer valid
698
- if (tx.data.constants.historicalHeader.globalVariables.blockNumber.toNumber() > latestBlock) {
699
- txsToDelete.push(await tx.getTxHash());
802
+ if (tx.data.constants.anchorBlockHeader.globalVariables.blockNumber > latestBlock) {
803
+ const txHash = tx.getTxHash();
804
+ txsToDelete.set(txHash.toString(), txHash);
700
805
  }
701
806
  }
702
807
 
703
- this.log.info(
704
- `Detected chain prune. Removing invalid txs count=${
705
- txsToDelete.length
706
- } newLatestBlock=${latestBlock} previousLatestBlock=${this.getSyncedLatestBlockNum()}`,
707
- );
808
+ this.log.info(`Detected chain prune. Removing ${txsToDelete.size} txs built against pruned blocks.`, {
809
+ newLatestBlock: latestBlock,
810
+ previousLatestBlock: await this.getSyncedLatestBlockNum(),
811
+ txsToDelete: Array.from(txsToDelete.keys()),
812
+ });
708
813
 
709
814
  // delete invalid txs (both pending and mined)
710
- await this.txPool.deleteTxs(txsToDelete);
815
+ await this.txPool.deleteTxs(Array.from(txsToDelete.values()));
711
816
 
712
817
  // everything left in the mined set was built against a block on the proven chain so its still valid
713
- // move back to pending the txs that were reorged out of the chain
818
+ // move back to pending the txs that were reorged out of the chain, unless txPoolDeleteTxsAfterReorg is set,
819
+ // in which case we clean them up to avoid potential reorg loops
714
820
  // NOTE: we can't move _all_ txs back to pending because the tx pool could keep hold of mined txs for longer
715
821
  // (see this.keepProvenTxsFor)
716
- const txsToMoveToPending: TxHash[] = [];
717
- for (const [txHash, blockNumber] of await this.txPool.getMinedTxHashes()) {
718
- if (blockNumber > latestBlock) {
719
- txsToMoveToPending.push(txHash);
822
+ const minedTxsFromReorg: TxHash[] = [];
823
+ for (const [txHash, blockNumber] of minedTxs) {
824
+ // We keep the txsToDelete out of this list as they have already been deleted above
825
+ if (blockNumber > latestBlock && !txsToDelete.has(txHash.toString())) {
826
+ minedTxsFromReorg.push(txHash);
720
827
  }
721
828
  }
722
829
 
723
- this.log.info(`Moving ${txsToMoveToPending.length} mined txs back to pending`);
724
- await this.txPool.markMinedAsPending(txsToMoveToPending);
830
+ if (this.config.txPoolDeleteTxsAfterReorg) {
831
+ this.log.info(`Deleting ${minedTxsFromReorg.length} mined txs from reorg`);
832
+ await this.txPool.deleteTxs(minedTxsFromReorg);
833
+ } else {
834
+ this.log.info(`Moving ${minedTxsFromReorg.length} mined txs from reorg back to pending`);
835
+ await this.txPool.markMinedAsPending(minedTxsFromReorg);
836
+ }
725
837
 
726
838
  await this.synchedLatestBlockNumber.set(latestBlock);
727
839
  // no need to update block hashes, as they will be updated as new blocks are added
728
840
  }
729
841
 
730
842
  private async startServiceIfSynched() {
843
+ if (this.currentState !== P2PClientState.SYNCHING) {
844
+ return;
845
+ }
846
+ const syncedFinalizedBlock = await this.getSyncedFinalizedBlockNum();
847
+ const syncedProvenBlock = await this.getSyncedProvenBlockNum();
848
+ const syncedLatestBlock = await this.getSyncedLatestBlockNum();
849
+
731
850
  if (
732
- this.currentState === P2PClientState.SYNCHING &&
733
- (await this.getSyncedLatestBlockNum()) >= this.latestBlockNumberAtStart &&
734
- (await this.getSyncedProvenBlockNum()) >= this.provenBlockNumberAtStart
851
+ syncedLatestBlock >= this.latestBlockNumberAtStart &&
852
+ syncedProvenBlock >= this.provenBlockNumberAtStart &&
853
+ syncedFinalizedBlock >= this.finalizedBlockNumberAtStart
735
854
  ) {
736
- this.log.debug(`Synched to blocks at start`);
855
+ this.log.info(`Completed P2P client sync to block ${syncedLatestBlock}. Starting service.`, {
856
+ syncedLatestBlock,
857
+ syncedProvenBlock,
858
+ syncedFinalizedBlock,
859
+ });
737
860
  this.setCurrentState(P2PClientState.RUNNING);
738
861
  if (this.syncResolve !== undefined) {
739
862
  this.syncResolve();
@@ -751,4 +874,20 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
751
874
  this.currentState = newState;
752
875
  this.log.debug(`Moved from state ${P2PClientState[oldState]} to ${P2PClientState[this.currentState]}`);
753
876
  }
877
+
878
+ public validate(txs: Tx[]): Promise<void> {
879
+ return this.p2pService.validate(txs);
880
+ }
881
+
882
+ /**
883
+ * Marks transactions as non-evictable in the pool.
884
+ * @param txHashes - Hashes of the transactions to mark as non-evictable.
885
+ */
886
+ public markTxsAsNonEvictable(txHashes: TxHash[]): Promise<void> {
887
+ return this.txPool.markTxsAsNonEvictable(txHashes);
888
+ }
889
+
890
+ public handleAuthRequestFromPeer(authRequest: AuthRequest, peerId: PeerId): Promise<StatusMessage> {
891
+ return this.p2pService.handleAuthRequestFromPeer(authRequest, peerId);
892
+ }
754
893
  }