@aztec/p2p 0.0.0-test.1 → 0.0.1-commit.1142ef1

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