@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,26 +1,44 @@
1
1
  import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
- import { Fr } from '@aztec/foundation/fields';
3
- import { createLibp2pComponentLogger, createLogger } from '@aztec/foundation/log';
4
- import { SerialQueue } from '@aztec/foundation/queue';
2
+ import { BlockNumber } from '@aztec/foundation/branded-types';
3
+ import { randomInt } from '@aztec/foundation/crypto/random';
4
+ import { Fr } from '@aztec/foundation/curves/bn254';
5
+ import { type Logger, createLibp2pComponentLogger, createLogger } from '@aztec/foundation/log';
5
6
  import { RunningPromise } from '@aztec/foundation/running-promise';
7
+ import { Timer } from '@aztec/foundation/timer';
6
8
  import type { AztecAsyncKVStore } from '@aztec/kv-store';
7
- import type { L2BlockSource } from '@aztec/stdlib/block';
9
+ import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
10
+ import { protocolContractsHash } from '@aztec/protocol-contracts';
11
+ import type { EthAddress, L2Block, L2BlockSource } from '@aztec/stdlib/block';
12
+ import type { ContractDataSource } from '@aztec/stdlib/contract';
13
+ import { GasFees } from '@aztec/stdlib/gas';
8
14
  import type { ClientProtocolCircuitVerifier, PeerInfo, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
9
15
  import {
10
- BlockAttestation,
11
16
  BlockProposal,
17
+ CheckpointAttestation,
18
+ CheckpointProposal,
19
+ type CheckpointProposalCore,
12
20
  type Gossipable,
13
21
  P2PClientType,
22
+ P2PMessage,
14
23
  PeerErrorSeverity,
15
- TopicTypeMap,
16
- getTopicTypeForClientType,
24
+ TopicType,
25
+ createTopicString,
26
+ getTopicsForClientAndConfig,
17
27
  metricsTopicStrToLabels,
18
28
  } from '@aztec/stdlib/p2p';
19
29
  import { MerkleTreeId } from '@aztec/stdlib/trees';
20
- import { Tx, type TxHash, type TxValidationResult } from '@aztec/stdlib/tx';
21
- import { Attributes, OtelMetricsAdapter, type TelemetryClient, WithTracer, trackSpan } from '@aztec/telemetry-client';
30
+ import { Tx, type TxHash, type TxValidationResult, type TxValidator } from '@aztec/stdlib/tx';
31
+ import type { UInt64 } from '@aztec/stdlib/types';
32
+ import { compressComponentVersions } from '@aztec/stdlib/versioning';
33
+ import {
34
+ Attributes,
35
+ OtelMetricsAdapter,
36
+ SpanStatusCode,
37
+ type TelemetryClient,
38
+ WithTracer,
39
+ trackSpan,
40
+ } from '@aztec/telemetry-client';
22
41
 
23
- import type { ENR } from '@chainsafe/enr';
24
42
  import {
25
43
  type GossipSub,
26
44
  type GossipSubComponents,
@@ -33,17 +51,27 @@ import { noise } from '@chainsafe/libp2p-noise';
33
51
  import { yamux } from '@chainsafe/libp2p-yamux';
34
52
  import { bootstrap } from '@libp2p/bootstrap';
35
53
  import { identify } from '@libp2p/identify';
36
- import { type Message, type PeerId, TopicValidatorResult } from '@libp2p/interface';
54
+ import { type Message, type MultiaddrConnection, type PeerId, TopicValidatorResult } from '@libp2p/interface';
37
55
  import type { ConnectionManager } from '@libp2p/interface-internal';
38
- import '@libp2p/kad-dht';
39
56
  import { mplex } from '@libp2p/mplex';
40
57
  import { tcp } from '@libp2p/tcp';
58
+ import { ENR } from '@nethermindeth/enr';
41
59
  import { createLibp2p } from 'libp2p';
42
60
 
43
61
  import type { P2PConfig } from '../../config.js';
62
+ import { ProposalSlotCapExceededError } from '../../errors/attestation-pool.error.js';
44
63
  import type { MemPools } from '../../mem_pools/interface.js';
45
- import { AttestationValidator, BlockProposalValidator } from '../../msg_validators/index.js';
46
64
  import {
65
+ BlockProposalValidator,
66
+ CheckpointAttestationValidator,
67
+ CheckpointProposalValidator,
68
+ FishermanAttestationValidator,
69
+ } from '../../msg_validators/index.js';
70
+ import { MessageSeenValidator } from '../../msg_validators/msg_seen_validator/msg_seen_validator.js';
71
+ import { getDefaultAllowedSetupFunctions } from '../../msg_validators/tx_validator/allowed_public_setup.js';
72
+ import { type MessageValidator, createTxMessageValidators } from '../../msg_validators/tx_validator/factory.js';
73
+ import {
74
+ AggregateTxValidator,
47
75
  DataTxValidator,
48
76
  DoubleSpendTxValidator,
49
77
  MetadataTxValidator,
@@ -51,23 +79,45 @@ import {
51
79
  } from '../../msg_validators/tx_validator/index.js';
52
80
  import { GossipSubEvent } from '../../types/index.js';
53
81
  import { type PubSubLibp2p, convertToMultiaddr } from '../../util.js';
82
+ import { getVersions } from '../../versioning.js';
54
83
  import { AztecDatastore } from '../data_store.js';
84
+ import { DiscV5Service } from '../discv5/discV5_service.js';
55
85
  import { SnappyTransform, fastMsgIdFn, getMsgIdFn, msgIdToStrFn } from '../encoding.js';
56
86
  import { gossipScoreThresholds } from '../gossipsub/scoring.js';
87
+ import type { PeerManagerInterface } from '../peer-manager/interface.js';
57
88
  import { PeerManager } from '../peer-manager/peer_manager.js';
58
89
  import { PeerScoring } from '../peer-manager/peer_scoring.js';
59
- import { DEFAULT_SUB_PROTOCOL_VALIDATORS, ReqRespSubProtocol, type SubProtocolMap } from '../reqresp/interface.js';
90
+ import type { P2PReqRespConfig } from '../reqresp/config.js';
91
+ import {
92
+ DEFAULT_SUB_PROTOCOL_VALIDATORS,
93
+ type ReqRespInterface,
94
+ ReqRespSubProtocol,
95
+ type ReqRespSubProtocolHandler,
96
+ type ReqRespSubProtocolHandlers,
97
+ type ReqRespSubProtocolValidators,
98
+ type SubProtocolMap,
99
+ ValidationError,
100
+ } from '../reqresp/interface.js';
101
+ import { reqRespBlockTxsHandler } from '../reqresp/protocols/block_txs/block_txs_handler.js';
60
102
  import { reqGoodbyeHandler } from '../reqresp/protocols/goodbye.js';
61
- import { pingHandler, reqRespBlockHandler, reqRespTxHandler, statusHandler } from '../reqresp/protocols/index.js';
103
+ import {
104
+ AuthRequest,
105
+ BlockTxsRequest,
106
+ BlockTxsResponse,
107
+ StatusMessage,
108
+ pingHandler,
109
+ reqRespBlockHandler,
110
+ reqRespStatusHandler,
111
+ reqRespTxHandler,
112
+ } from '../reqresp/protocols/index.js';
62
113
  import { ReqResp } from '../reqresp/reqresp.js';
63
- import type { P2PService, PeerDiscoveryService } from '../service.js';
64
-
65
- interface MessageValidator {
66
- validator: {
67
- validateTx(tx: Tx): Promise<TxValidationResult>;
68
- };
69
- severity: PeerErrorSeverity;
70
- }
114
+ import type {
115
+ P2PBlockReceivedCallback,
116
+ P2PCheckpointReceivedCallback,
117
+ P2PService,
118
+ PeerDiscoveryService,
119
+ } from '../service.js';
120
+ import { P2PInstrumentation } from './instrumentation.js';
71
121
 
72
122
  interface ValidationResult {
73
123
  name: string;
@@ -77,74 +127,125 @@ interface ValidationResult {
77
127
 
78
128
  type ValidationOutcome = { allPassed: true } | { allPassed: false; failure: ValidationResult };
79
129
 
130
+ // REFACTOR: Unify with the type above
131
+ type ReceivedMessageValidationResult<T> =
132
+ | { obj: T; result: Exclude<TopicValidatorResult, TopicValidatorResult.Reject> }
133
+ | { obj?: undefined; result: TopicValidatorResult.Reject };
134
+
80
135
  /**
81
136
  * Lib P2P implementation of the P2PService interface.
82
137
  */
83
- export class LibP2PService<T extends P2PClientType> extends WithTracer implements P2PService {
84
- private jobQueue: SerialQueue = new SerialQueue();
85
- private peerManager: PeerManager;
138
+ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends WithTracer implements P2PService {
86
139
  private discoveryRunningPromise?: RunningPromise;
140
+ private msgIdSeenValidators: Record<TopicType, MessageSeenValidator> = {} as Record<TopicType, MessageSeenValidator>;
87
141
 
88
142
  // Message validators
89
- private attestationValidator: AttestationValidator;
90
143
  private blockProposalValidator: BlockProposalValidator;
144
+ private checkpointProposalValidator: CheckpointProposalValidator;
145
+ private checkpointAttestationValidator: CheckpointAttestationValidator;
146
+
147
+ private protocolVersion = '';
148
+ private topicStrings: Record<TopicType, string> = {} as Record<TopicType, string>;
91
149
 
92
- // Request and response sub service
93
- public reqresp: ReqResp;
150
+ private feesCache: { blockNumber: BlockNumber; gasFees: GasFees } | undefined;
94
151
 
95
152
  /**
96
153
  * Callback for when a block is received from a peer.
97
154
  * @param block - The block received from the peer.
98
155
  * @returns The attestation for the block, if any.
99
156
  */
100
- private blockReceivedCallback: (block: BlockProposal) => Promise<BlockAttestation | undefined>;
157
+ private blockReceivedCallback: P2PBlockReceivedCallback;
158
+
159
+ /**
160
+ * Callback for when a checkpoint proposal is received from a peer.
161
+ * @param checkpoint - The checkpoint proposal received from the peer.
162
+ * @returns The attestations for the checkpoint, if any.
163
+ */
164
+ private checkpointReceivedCallback: P2PCheckpointReceivedCallback;
165
+
166
+ private gossipSubEventHandler: (e: CustomEvent<GossipsubMessage>) => void;
167
+
168
+ private instrumentation: P2PInstrumentation;
169
+
170
+ private telemetry: TelemetryClient;
171
+
172
+ protected logger: Logger;
101
173
 
102
174
  constructor(
103
175
  private clientType: T,
104
176
  private config: P2PConfig,
105
- private node: PubSubLibp2p,
177
+ protected node: PubSubLibp2p,
106
178
  private peerDiscoveryService: PeerDiscoveryService,
107
- private mempools: MemPools<T>,
108
- private l2BlockSource: L2BlockSource,
109
- epochCache: EpochCacheInterface,
179
+ private reqresp: ReqRespInterface,
180
+ private peerManager: PeerManagerInterface,
181
+ protected mempools: MemPools,
182
+ private archiver: L2BlockSource & ContractDataSource,
183
+ private epochCache: EpochCacheInterface,
110
184
  private proofVerifier: ClientProtocolCircuitVerifier,
111
185
  private worldStateSynchronizer: WorldStateSynchronizer,
112
186
  telemetry: TelemetryClient,
113
- private logger = createLogger('p2p:libp2p_service'),
187
+ logger: Logger = createLogger('p2p:libp2p_service'),
114
188
  ) {
115
189
  super(telemetry, 'LibP2PService');
190
+ this.telemetry = telemetry;
116
191
 
117
- const peerScoring = new PeerScoring(config);
118
- this.reqresp = new ReqResp(config, node, peerScoring);
192
+ // Create child logger with fisherman prefix if in fisherman mode
193
+ this.logger = config.fishermanMode ? logger.createChild('[FISHERMAN]') : logger;
119
194
 
120
- this.peerManager = new PeerManager(
121
- node,
122
- peerDiscoveryService,
123
- config,
124
- telemetry,
125
- createLogger(`${logger.module}:peer_manager`),
126
- peerScoring,
127
- this.reqresp,
195
+ this.instrumentation = new P2PInstrumentation(telemetry, 'LibP2PService');
196
+
197
+ this.msgIdSeenValidators[TopicType.tx] = new MessageSeenValidator(config.seenMessageCacheSize);
198
+ this.msgIdSeenValidators[TopicType.block_proposal] = new MessageSeenValidator(config.seenMessageCacheSize);
199
+ this.msgIdSeenValidators[TopicType.checkpoint_proposal] = new MessageSeenValidator(config.seenMessageCacheSize);
200
+ this.msgIdSeenValidators[TopicType.checkpoint_attestation] = new MessageSeenValidator(config.seenMessageCacheSize);
201
+
202
+ const versions = getVersions(config);
203
+ this.protocolVersion = compressComponentVersions(versions);
204
+ logger.info(`Started libp2p service with protocol version ${this.protocolVersion}`);
205
+
206
+ this.topicStrings[TopicType.tx] = createTopicString(TopicType.tx, this.protocolVersion);
207
+ this.topicStrings[TopicType.block_proposal] = createTopicString(TopicType.block_proposal, this.protocolVersion);
208
+ this.topicStrings[TopicType.checkpoint_proposal] = createTopicString(
209
+ TopicType.checkpoint_proposal,
210
+ this.protocolVersion,
211
+ );
212
+ this.topicStrings[TopicType.checkpoint_attestation] = createTopicString(
213
+ TopicType.checkpoint_attestation,
214
+ this.protocolVersion,
128
215
  );
129
216
 
130
- // Update gossipsub score params
131
- this.node.services.pubsub.score.params.appSpecificScore = (peerId: string) => {
132
- return this.peerManager.getPeerScore(peerId);
133
- };
134
- this.node.services.pubsub.score.params.appSpecificWeight = 10;
217
+ this.blockProposalValidator = new BlockProposalValidator(epochCache, { txsPermitted: !config.disableTransactions });
218
+ this.checkpointProposalValidator = new CheckpointProposalValidator(epochCache, {
219
+ txsPermitted: !config.disableTransactions,
220
+ });
221
+ this.checkpointAttestationValidator = config.fishermanMode
222
+ ? new FishermanAttestationValidator(epochCache, mempools.attestationPool, telemetry)
223
+ : new CheckpointAttestationValidator(epochCache);
135
224
 
136
- this.attestationValidator = new AttestationValidator(epochCache);
137
- this.blockProposalValidator = new BlockProposalValidator(epochCache);
225
+ this.gossipSubEventHandler = this.handleGossipSubEvent.bind(this);
138
226
 
139
- this.blockReceivedCallback = async (block: BlockProposal): Promise<BlockAttestation | undefined> => {
227
+ this.blockReceivedCallback = async (block: BlockProposal): Promise<boolean> => {
140
228
  this.logger.debug(
141
- `Handler not yet registered: Block received callback not set. Received block for slot ${block.slotNumber.toNumber()} from peer.`,
142
- { p2pMessageIdentifier: await block.p2pMessageIdentifier() },
229
+ `Handler not yet registered: Block received callback not set. Received block for slot ${block.slotNumber} from peer.`,
230
+ { p2pMessageIdentifier: await block.p2pMessageLoggingIdentifier() },
143
231
  );
144
- return undefined;
232
+ return false;
233
+ };
234
+
235
+ this.checkpointReceivedCallback = (
236
+ checkpoint: CheckpointProposalCore,
237
+ ): Promise<CheckpointAttestation[] | undefined> => {
238
+ this.logger.debug(
239
+ `Handler not yet registered: Checkpoint received callback not set. Received checkpoint for slot ${checkpoint.slotNumber} from peer.`,
240
+ );
241
+ return Promise.resolve(undefined);
145
242
  };
146
243
  }
147
244
 
245
+ public updateConfig(config: Partial<P2PReqRespConfig>) {
246
+ this.reqresp.updateConfig(config);
247
+ }
248
+
148
249
  /**
149
250
  * Creates an instance of the LibP2P service.
150
251
  * @param config - The configuration to use when creating the service.
@@ -154,69 +255,152 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
154
255
  public static async new<T extends P2PClientType>(
155
256
  clientType: T,
156
257
  config: P2PConfig,
157
- peerDiscoveryService: PeerDiscoveryService,
158
258
  peerId: PeerId,
159
- mempools: MemPools<T>,
160
- l2BlockSource: L2BlockSource,
161
- epochCache: EpochCacheInterface,
162
- proofVerifier: ClientProtocolCircuitVerifier,
163
- worldStateSynchronizer: WorldStateSynchronizer,
164
- store: AztecAsyncKVStore,
165
- telemetry: TelemetryClient,
166
- logger = createLogger('p2p:libp2p_service'),
259
+ deps: {
260
+ mempools: MemPools;
261
+ l2BlockSource: L2BlockSource & ContractDataSource;
262
+ epochCache: EpochCacheInterface;
263
+ proofVerifier: ClientProtocolCircuitVerifier;
264
+ worldStateSynchronizer: WorldStateSynchronizer;
265
+ peerStore: AztecAsyncKVStore;
266
+ telemetry: TelemetryClient;
267
+ logger: Logger;
268
+ packageVersion: string;
269
+ },
167
270
  ) {
168
- const { tcpListenAddress, tcpAnnounceAddress, maxPeerCount } = config;
169
- const bindAddrTcp = convertToMultiaddr(tcpListenAddress, 'tcp');
170
- // We know tcpAnnounceAddress cannot be null here because we set it or throw when setting up the service.
171
- const announceAddrTcp = convertToMultiaddr(tcpAnnounceAddress!, 'tcp');
271
+ const {
272
+ worldStateSynchronizer,
273
+ epochCache,
274
+ l2BlockSource,
275
+ mempools,
276
+ proofVerifier,
277
+ peerStore,
278
+ telemetry,
279
+ logger,
280
+ packageVersion,
281
+ } = deps;
282
+ const { p2pPort, maxPeerCount, listenAddress } = config;
283
+ const bindAddrTcp = convertToMultiaddr(listenAddress, p2pPort, 'tcp');
172
284
 
173
- const datastore = new AztecDatastore(store);
285
+ const datastore = new AztecDatastore(peerStore);
174
286
 
175
287
  const otelMetricsAdapter = new OtelMetricsAdapter(telemetry);
176
288
 
177
- // If bootstrap nodes are provided, also provide them to the p2p service
289
+ const peerDiscoveryService = new DiscV5Service(
290
+ peerId,
291
+ config,
292
+ packageVersion,
293
+ telemetry,
294
+ createLogger(`${logger.module}:discv5_service`),
295
+ );
296
+
297
+ // Seed libp2p's bootstrap discovery with private and trusted peers
298
+ const bootstrapNodes = [...config.privatePeers, ...config.trustedPeers];
299
+
178
300
  const peerDiscovery = [];
179
- if (peerDiscoveryService.bootstrapNodes.length > 0) {
180
- peerDiscovery.push(bootstrap({ list: peerDiscoveryService.bootstrapNodes }));
301
+ if (bootstrapNodes.length > 0) {
302
+ peerDiscovery.push(bootstrap({ list: bootstrapNodes }));
181
303
  }
182
304
 
305
+ const versions = getVersions(config);
306
+ const protocolVersion = compressComponentVersions(versions);
307
+
308
+ const txTopic = createTopicString(TopicType.tx, protocolVersion);
309
+ const blockProposalTopic = createTopicString(TopicType.block_proposal, protocolVersion);
310
+ const checkpointProposalTopic = createTopicString(TopicType.checkpoint_proposal, protocolVersion);
311
+ const checkpointAttestationTopic = createTopicString(TopicType.checkpoint_attestation, protocolVersion);
312
+
313
+ const preferredPeersEnrs: ENR[] = config.preferredPeers.map(enr => ENR.decodeTxt(enr));
314
+ const directPeers = (
315
+ await Promise.all(
316
+ preferredPeersEnrs.map(async enr => {
317
+ const peerId = await enr.peerId();
318
+ const address = enr.getLocationMultiaddr('tcp');
319
+ if (address === undefined) {
320
+ throw new Error(`Direct peer ${peerId.toString()} has no TCP address, ENR: ${enr.encodeTxt()}`);
321
+ }
322
+ return {
323
+ id: peerId,
324
+ addrs: [address],
325
+ };
326
+ }),
327
+ )
328
+ ).filter(peer => peer !== undefined);
329
+
330
+ const announceTcpMultiaddr = config.p2pIp ? [convertToMultiaddr(config.p2pIp, p2pPort, 'tcp')] : [];
331
+
183
332
  const node = await createLibp2p({
184
333
  start: false,
185
334
  peerId,
186
335
  addresses: {
187
336
  listen: [bindAddrTcp],
188
- announce: [announceAddrTcp],
337
+ announce: announceTcpMultiaddr,
189
338
  },
190
339
  transports: [
191
340
  tcp({
192
- maxConnections: config.maxPeerCount,
341
+ // It's better to have this number a bit higher than our maxPeerCount because it's sets the limit on transport (TCP) layer
342
+ // The connection attempts to the node on TCP layer are not necessarily valid Aztec peers so we want to have a bit of leeway here
343
+ // If we hit the limit, the connection will be temporarily accepted and immediately dropped.
344
+ // Docs: https://nodejs.org/api/net.html#servermaxconnections
345
+ maxConnections: maxPeerCount * 2,
193
346
  // socket option: the maximum length of the queue of pending connections
194
- // https://nodejs.org/dist/latest-v18.x/docs/api/net.html#serverlisten
347
+ // https://nodejs.org/dist/latest-v22.x/docs/api/net.html#serverlisten
195
348
  // it's not safe if we increase this number
196
349
  backlog: 5,
197
350
  closeServerOnMaxConnections: {
198
- closeAbove: maxPeerCount ?? Infinity,
199
- listenBelow: maxPeerCount ?? Infinity,
351
+ // The property `maxConnections` will protect us against the most DDOS attack
352
+ // This property protects us in case of burst of new connections where server is not able to close them quickly enough
353
+ // In case closeAbove is reached, the server stops listening altogether
354
+ // It's important that there is enough difference between closeAbove and listenAbove,
355
+ // otherwise the server.listener will flap between being closed and open potentially degrading perf even more
356
+ closeAbove: maxPeerCount * 3,
357
+ listenBelow: Math.floor(maxPeerCount * 0.9),
200
358
  },
201
359
  }),
202
360
  ],
203
361
  datastore,
204
362
  peerDiscovery,
205
- streamMuxers: [mplex(), yamux()],
363
+ streamMuxers: [yamux(), mplex()],
206
364
  connectionEncryption: [noise()],
207
365
  connectionManager: {
208
- minConnections: 0,
209
-
366
+ minConnections: 0, // Disable libp2p peer dialing, we do it manually
367
+ // We set maxConnections above maxPeerCount because if we hit limit of maxPeerCount
368
+ // libp2p will start aggressively rejecting all new connections, preventing network discovery and crawling.
369
+ maxConnections: maxPeerCount * 2,
210
370
  maxParallelDials: 100,
211
371
  dialTimeout: 30_000,
212
372
  maxPeerAddrsToDial: 5,
213
373
  maxIncomingPendingConnections: 5,
214
374
  },
375
+ connectionGater: {
376
+ denyInboundConnection: (maConn: MultiaddrConnection) => {
377
+ const allowed = peerManager.isNodeAllowedToConnect(maConn.remoteAddr.nodeAddress().address);
378
+ if (allowed) {
379
+ return false;
380
+ }
381
+
382
+ logger.debug(`Connection gater: Denying inbound connection from ${maConn.remoteAddr.toString()}`);
383
+ return true;
384
+ },
385
+ denyInboundEncryptedConnection: (peerId: PeerId, _maConn: MultiaddrConnection) => {
386
+ //NOTE: it is not necessary to check address here because this was already done by
387
+ // denyInboundConnection
388
+ const allowed = peerManager.isNodeAllowedToConnect(peerId);
389
+ if (allowed) {
390
+ return false;
391
+ }
392
+
393
+ logger.debug(`Connection gater: Denying inbound encrypted connection from ${peerId.toString()}`);
394
+ return true;
395
+ },
396
+ },
215
397
  services: {
216
398
  identify: identify({
217
399
  protocolPrefix: 'aztec',
400
+ runOnConnectionOpen: true,
218
401
  }),
219
402
  pubsub: gossipsub({
403
+ directPeers,
220
404
  debugName: 'gossipsub',
221
405
  globalSignaturePolicy: SignaturePolicy.StrictNoSign,
222
406
  allowPublishToZeroTopicPeers: true,
@@ -228,29 +412,35 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
228
412
  heartbeatInterval: config.gossipsubInterval,
229
413
  mcacheLength: config.gossipsubMcacheLength,
230
414
  mcacheGossip: config.gossipsubMcacheGossip,
415
+ seenTTL: config.gossipsubSeenTTL,
231
416
  msgIdFn: getMsgIdFn,
232
417
  msgIdToStrFn: msgIdToStrFn,
233
418
  fastMsgIdFn: fastMsgIdFn,
234
419
  dataTransform: new SnappyTransform(),
235
420
  metricsRegister: otelMetricsAdapter,
236
- metricsTopicStrToLabel: metricsTopicStrToLabels(),
421
+ metricsTopicStrToLabel: metricsTopicStrToLabels(protocolVersion),
237
422
  asyncValidation: true,
238
423
  scoreThresholds: gossipScoreThresholds,
239
424
  scoreParams: createPeerScoreParams({
240
425
  // IPColocation factor can be disabled for local testing - default to -5
241
426
  IPColocationFactorWeight: config.debugDisableColocationPenalty ? 0 : -5.0,
242
427
  topics: {
243
- [Tx.p2pTopic]: createTopicScoreParams({
428
+ [txTopic]: createTopicScoreParams({
244
429
  topicWeight: 1,
245
430
  invalidMessageDeliveriesWeight: -20,
246
431
  invalidMessageDeliveriesDecay: 0.5,
247
432
  }),
248
- [BlockAttestation.p2pTopic]: createTopicScoreParams({
433
+ [blockProposalTopic]: createTopicScoreParams({
249
434
  topicWeight: 1,
250
435
  invalidMessageDeliveriesWeight: -20,
251
436
  invalidMessageDeliveriesDecay: 0.5,
252
437
  }),
253
- [BlockProposal.p2pTopic]: createTopicScoreParams({
438
+ [checkpointProposalTopic]: createTopicScoreParams({
439
+ topicWeight: 1,
440
+ invalidMessageDeliveriesWeight: -20,
441
+ invalidMessageDeliveriesDecay: 0.5,
442
+ }),
443
+ [checkpointAttestationTopic]: createTopicScoreParams({
254
444
  topicWeight: 1,
255
445
  invalidMessageDeliveriesWeight: -20,
256
446
  invalidMessageDeliveriesDecay: 0.5,
@@ -265,11 +455,34 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
265
455
  logger: createLibp2pComponentLogger(logger.module),
266
456
  });
267
457
 
458
+ const peerScoring = new PeerScoring(config, telemetry);
459
+ const reqresp = new ReqResp(config, node, peerScoring, createLogger(`${logger.module}:reqresp`));
460
+
461
+ const peerManager = new PeerManager(
462
+ node,
463
+ peerDiscoveryService,
464
+ config,
465
+ telemetry,
466
+ createLogger(`${logger.module}:peer_manager`),
467
+ peerScoring,
468
+ reqresp,
469
+ worldStateSynchronizer,
470
+ protocolVersion,
471
+ epochCache,
472
+ );
473
+
474
+ // Update gossipsub score params
475
+ node.services.pubsub.score.params.appSpecificWeight = 10;
476
+ node.services.pubsub.score.params.appSpecificScore = (peerId: string) =>
477
+ peerManager.shouldDisableP2PGossip(peerId) ? -Infinity : peerManager.getPeerScore(peerId);
478
+
268
479
  return new LibP2PService(
269
480
  clientType,
270
481
  config,
271
482
  node,
272
483
  peerDiscoveryService,
484
+ reqresp,
485
+ peerManager,
273
486
  mempools,
274
487
  l2BlockSource,
275
488
  epochCache,
@@ -291,55 +504,72 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
291
504
  }
292
505
 
293
506
  // Get listen & announce addresses for logging
294
- const { tcpListenAddress, tcpAnnounceAddress } = this.config;
295
- if (!tcpAnnounceAddress) {
507
+ const { p2pIp, p2pPort } = this.config;
508
+ if (!p2pIp) {
296
509
  throw new Error('Announce address not provided.');
297
510
  }
298
- const announceTcpMultiaddr = convertToMultiaddr(tcpAnnounceAddress, 'tcp');
299
-
300
- // Start job queue, peer discovery service and libp2p node
301
- this.jobQueue.start();
302
- await this.peerDiscoveryService.start();
303
- await this.node.start();
304
-
305
- // Subscribe to standard GossipSub topics by default
306
- for (const topic of getTopicTypeForClientType(this.clientType)) {
307
- this.subscribeToTopic(TopicTypeMap[topic].p2pTopic);
308
- }
511
+ const announceTcpMultiaddr = convertToMultiaddr(p2pIp, p2pPort, 'tcp');
309
512
 
310
513
  // Create request response protocol handlers
311
514
  const txHandler = reqRespTxHandler(this.mempools);
312
515
  const goodbyeHandler = reqGoodbyeHandler(this.peerManager);
313
- const blockHandler = reqRespBlockHandler(this.l2BlockSource);
516
+ const blockHandler = reqRespBlockHandler(this.archiver);
517
+ const statusHandler = reqRespStatusHandler(this.protocolVersion, this.worldStateSynchronizer, this.logger);
314
518
 
315
- const requestResponseHandlers = {
519
+ const requestResponseHandlers: Partial<ReqRespSubProtocolHandlers> = {
316
520
  [ReqRespSubProtocol.PING]: pingHandler,
317
- [ReqRespSubProtocol.STATUS]: statusHandler,
318
- [ReqRespSubProtocol.TX]: txHandler.bind(this),
521
+ [ReqRespSubProtocol.STATUS]: statusHandler.bind(this),
319
522
  [ReqRespSubProtocol.GOODBYE]: goodbyeHandler.bind(this),
320
523
  [ReqRespSubProtocol.BLOCK]: blockHandler.bind(this),
321
524
  };
322
525
 
526
+ if (!this.config.disableTransactions) {
527
+ const blockTxsHandler = reqRespBlockTxsHandler(this.mempools.attestationPool, this.mempools.txPool);
528
+ requestResponseHandlers[ReqRespSubProtocol.BLOCK_TXS] = blockTxsHandler.bind(this);
529
+ }
530
+
531
+ if (!this.config.disableTransactions) {
532
+ requestResponseHandlers[ReqRespSubProtocol.TX] = txHandler.bind(this);
533
+ }
534
+
535
+ // Define the sub protocol validators - This is done within this start() method to gain a callback to the existing validateTx function
536
+ const reqrespSubProtocolValidators = {
537
+ ...DEFAULT_SUB_PROTOCOL_VALIDATORS,
538
+ [ReqRespSubProtocol.TX]: this.validateRequestedTxs.bind(this),
539
+ [ReqRespSubProtocol.BLOCK_TXS]: this.validateRequestedBlockTxs.bind(this),
540
+ [ReqRespSubProtocol.BLOCK]: this.validateRequestedBlock.bind(this),
541
+ };
542
+
543
+ await this.peerManager.initializePeers();
544
+
545
+ await this.reqresp.start(requestResponseHandlers, reqrespSubProtocolValidators);
546
+
547
+ await this.node.start();
548
+
549
+ // Subscribe to standard GossipSub topics by default
550
+ for (const topic of getTopicsForClientAndConfig(this.clientType, this.config.disableTransactions)) {
551
+ this.subscribeToTopic(this.topicStrings[topic]);
552
+ }
553
+
323
554
  // add GossipSub listener
324
- this.node.services.pubsub.addEventListener(GossipSubEvent.MESSAGE, this.handleGossipSubEvent.bind(this));
555
+ this.node.services.pubsub.addEventListener(GossipSubEvent.MESSAGE, this.gossipSubEventHandler);
325
556
 
326
- // Start running promise for peer discovery
557
+ // Start running promise for peer discovery and metrics collection
558
+ if (!this.config.p2pDiscoveryDisabled) {
559
+ await this.peerDiscoveryService.start();
560
+ }
327
561
  this.discoveryRunningPromise = new RunningPromise(
328
- () => this.peerManager.heartbeat(),
562
+ async () => {
563
+ await this.peerManager.heartbeat();
564
+ },
329
565
  this.logger,
330
566
  this.config.peerCheckIntervalMS,
331
567
  );
332
568
  this.discoveryRunningPromise.start();
333
569
 
334
- // Define the sub protocol validators - This is done within this start() method to gain a callback to the existing validateTx function
335
- const reqrespSubProtocolValidators = {
336
- ...DEFAULT_SUB_PROTOCOL_VALIDATORS,
337
- // TODO(#11336): A request validator for blocks
338
- [ReqRespSubProtocol.TX]: this.validateRequestedTx.bind(this),
339
- };
340
- await this.reqresp.start(requestResponseHandlers, reqrespSubProtocolValidators);
341
570
  this.logger.info(`Started P2P service`, {
342
- listen: tcpListenAddress,
571
+ listen: this.config.listenAddress,
572
+ port: this.config.p2pPort,
343
573
  announce: announceTcpMultiaddr,
344
574
  peerId: this.node.peerId.toString(),
345
575
  });
@@ -351,14 +581,11 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
351
581
  */
352
582
  public async stop() {
353
583
  // Remove gossip sub listener
354
- this.node.services.pubsub.removeEventListener(GossipSubEvent.MESSAGE, this.handleGossipSubEvent.bind(this));
584
+ this.node.services.pubsub.removeEventListener(GossipSubEvent.MESSAGE, this.gossipSubEventHandler);
355
585
 
356
586
  // Stop peer manager
357
587
  this.logger.debug('Stopping peer manager...');
358
588
  await this.peerManager.stop();
359
-
360
- this.logger.debug('Stopping job queue...');
361
- await this.jobQueue.end();
362
589
  this.logger.debug('Stopping running promise...');
363
590
  await this.discoveryRunningPromise?.stop();
364
591
  this.logger.debug('Stopping peer discovery service...');
@@ -370,6 +597,18 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
370
597
  this.logger.info('LibP2P service stopped');
371
598
  }
372
599
 
600
+ addReqRespSubProtocol(
601
+ subProtocol: ReqRespSubProtocol,
602
+ handler: ReqRespSubProtocolHandler,
603
+ validator?: ReqRespSubProtocolValidators[ReqRespSubProtocol],
604
+ ): Promise<void> {
605
+ return this.reqresp.addSubProtocol(subProtocol, handler, validator);
606
+ }
607
+
608
+ public registerThisValidatorAddresses(address: EthAddress[]): void {
609
+ this.peerManager.registerThisValidatorAddresses(address);
610
+ }
611
+
373
612
  public getPeers(includePending?: boolean): PeerInfo[] {
374
613
  return this.peerManager.getPeers(includePending);
375
614
  }
@@ -387,23 +626,6 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
387
626
  setImmediate(() => void safeJob());
388
627
  }
389
628
 
390
- /**
391
- * Send Request via the ReqResp service
392
- * The subprotocol defined will determine the request and response types
393
- *
394
- * See the subProtocolMap for the mapping of subprotocols to request/response types in `interface.ts`
395
- *
396
- * @param protocol The request response protocol to use
397
- * @param request The request type to send
398
- * @returns
399
- */
400
- sendRequest<SubProtocol extends ReqRespSubProtocol>(
401
- protocol: SubProtocol,
402
- request: InstanceType<SubProtocolMap[SubProtocol]['request']>,
403
- ): Promise<InstanceType<SubProtocolMap[SubProtocol]['response']> | undefined> {
404
- return this.reqresp.sendRequest(protocol, request);
405
- }
406
-
407
629
  /**
408
630
  * Send a batch of requests to peers, and return the responses
409
631
  * @param protocol - The request response protocol to use
@@ -413,8 +635,9 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
413
635
  sendBatchRequest<SubProtocol extends ReqRespSubProtocol>(
414
636
  protocol: SubProtocol,
415
637
  requests: InstanceType<SubProtocolMap[SubProtocol]['request']>[],
416
- ): Promise<InstanceType<SubProtocolMap[SubProtocol]['response']>[] | undefined> {
417
- return this.reqresp.sendBatchRequest(protocol, requests);
638
+ pinnedPeerId: PeerId | undefined,
639
+ ): Promise<InstanceType<SubProtocolMap[SubProtocol]['response']>[]> {
640
+ return this.reqresp.sendBatchRequest(protocol, requests, pinnedPeerId);
418
641
  }
419
642
 
420
643
  /**
@@ -425,9 +648,12 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
425
648
  return this.peerDiscoveryService.getEnr();
426
649
  }
427
650
 
428
- public registerBlockReceivedCallback(callback: (block: BlockProposal) => Promise<BlockAttestation | undefined>) {
651
+ public registerBlockReceivedCallback(callback: P2PBlockReceivedCallback) {
429
652
  this.blockReceivedCallback = callback;
430
- this.logger.verbose('Block received callback registered');
653
+ }
654
+
655
+ public registerCheckpointReceivedCallback(callback: P2PCheckpointReceivedCallback) {
656
+ this.checkpointReceivedCallback = callback;
431
657
  }
432
658
 
433
659
  /**
@@ -444,175 +670,516 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
444
670
  /**
445
671
  * Publishes data to a topic.
446
672
  * @param topic - The topic to publish to.
447
- * @param data - The data to publish.
673
+ * @param data - The message to publish.
448
674
  * @returns The number of recipients the data was sent to.
449
675
  */
450
- private async publishToTopic(topic: string, data: Uint8Array) {
676
+ private async publishToTopic(topic: string, message: Gossipable) {
451
677
  if (!this.node.services.pubsub) {
452
678
  throw new Error('Pubsub service not available.');
453
679
  }
454
- const result = await this.node.services.pubsub.publish(topic, data);
455
-
680
+ const isBlockProposal = topic === this.topicStrings[TopicType.block_proposal];
681
+ const traceContext =
682
+ this.config.debugP2PInstrumentMessages && isBlockProposal ? this.telemetry.getTraceContext() : undefined;
683
+ const p2pMessage = P2PMessage.fromGossipable(message, this.config.debugP2PInstrumentMessages, traceContext);
684
+ const result = await this.node.services.pubsub.publish(topic, p2pMessage.toMessageData());
456
685
  return result.recipients.length;
457
686
  }
458
687
 
688
+ /**
689
+ * Checks if this message has already been seen, based on its msgId computed from hashing the message data.
690
+ * Note that we do not rely on the seenCache from gossipsub since we want to keep a longer history of seen
691
+ * messages to avoid tx echoes across the network.
692
+ */
693
+ protected preValidateReceivedMessage(
694
+ msg: Message,
695
+ msgId: string,
696
+ source: PeerId,
697
+ ): { result: boolean; topicType?: TopicType } {
698
+ let topicType: TopicType | undefined;
699
+
700
+ switch (msg.topic) {
701
+ case this.topicStrings[TopicType.tx]:
702
+ topicType = TopicType.tx;
703
+ break;
704
+ case this.topicStrings[TopicType.block_proposal]:
705
+ topicType = TopicType.block_proposal;
706
+ break;
707
+ case this.topicStrings[TopicType.checkpoint_proposal]:
708
+ topicType = TopicType.checkpoint_proposal;
709
+ break;
710
+ case this.topicStrings[TopicType.checkpoint_attestation]:
711
+ topicType = TopicType.checkpoint_attestation;
712
+ break;
713
+ default:
714
+ this.logger.error(`Received message on unknown topic: ${msg.topic}`);
715
+ break;
716
+ }
717
+
718
+ const validator = topicType ? this.msgIdSeenValidators[topicType] : undefined;
719
+
720
+ if (!validator || !validator.addMessage(msgId)) {
721
+ this.instrumentation.incMessagePrevalidationStatus(false, topicType);
722
+ this.node.services.pubsub.reportMessageValidationResult(msgId, source.toString(), TopicValidatorResult.Ignore);
723
+ return { result: false, topicType };
724
+ }
725
+
726
+ this.instrumentation.incMessagePrevalidationStatus(true, topicType);
727
+
728
+ return { result: true, topicType };
729
+ }
730
+
731
+ /**
732
+ * Safely deserializes a P2PMessage from raw message data.
733
+ * @param msgId - The message ID.
734
+ * @param source - The peer ID of the message source.
735
+ * @param data - The raw message data.
736
+ * @returns The deserialized P2PMessage or undefined if deserialization fails.
737
+ */
738
+ private safelyDeserializeP2PMessage(msgId: string, source: PeerId, data: Uint8Array): P2PMessage | undefined {
739
+ try {
740
+ return P2PMessage.fromMessageData(Buffer.from(data), this.config.debugP2PInstrumentMessages);
741
+ } catch (err) {
742
+ this.logger.error(`Error deserializing P2PMessage`, err, {
743
+ msgId,
744
+ source: source.toString(),
745
+ });
746
+ this.node.services.pubsub.reportMessageValidationResult(msgId, source.toString(), TopicValidatorResult.Reject);
747
+ this.peerManager.penalizePeer(source, PeerErrorSeverity.LowToleranceError);
748
+ return undefined;
749
+ }
750
+ }
751
+
459
752
  /**
460
753
  * Handles a new gossip message that was received by the client.
461
754
  * @param topic - The message's topic.
462
755
  * @param data - The message data
463
756
  */
464
- private async handleNewGossipMessage(msg: Message, msgId: string, source: PeerId) {
465
- if (msg.topic === Tx.p2pTopic) {
466
- await this.handleGossipedTx(msg, msgId, source);
757
+ protected async handleNewGossipMessage(msg: Message, msgId: string, source: PeerId) {
758
+ const msgReceivedTime = Date.now();
759
+ let topicType: TopicType | undefined;
760
+ const p2pMessage = this.safelyDeserializeP2PMessage(msgId, source, msg.data);
761
+ if (!p2pMessage) {
762
+ return;
467
763
  }
468
- if (msg.topic === BlockAttestation.p2pTopic && this.clientType === P2PClientType.Full) {
469
- await this.processAttestationFromPeer(msg, msgId, source);
764
+
765
+ const preValidationResult = this.preValidateReceivedMessage(msg, msgId, source);
766
+
767
+ if (!preValidationResult.result) {
768
+ return;
470
769
  }
471
- if (msg.topic == BlockProposal.p2pTopic) {
472
- await this.processBlockFromPeer(msg, msgId, source);
770
+
771
+ // Determine topic type for attributes
772
+ if (msg.topic === this.topicStrings[TopicType.tx]) {
773
+ topicType = TopicType.tx;
774
+ } else if (msg.topic === this.topicStrings[TopicType.checkpoint_attestation]) {
775
+ topicType = TopicType.checkpoint_attestation;
776
+ } else if (msg.topic === this.topicStrings[TopicType.block_proposal]) {
777
+ topicType = TopicType.block_proposal;
778
+ } else if (msg.topic === this.topicStrings[TopicType.checkpoint_proposal]) {
779
+ topicType = TopicType.checkpoint_proposal;
780
+ }
781
+
782
+ // Process the message, optionally within a linked span for trace propagation
783
+ const processMessage = async () => {
784
+ if (msg.topic === this.topicStrings[TopicType.tx]) {
785
+ await this.handleGossipedTx(p2pMessage.payload, msgId, source);
786
+ } else if (msg.topic === this.topicStrings[TopicType.checkpoint_attestation]) {
787
+ if (this.clientType === P2PClientType.Full) {
788
+ await this.processCheckpointAttestationFromPeer(p2pMessage.payload, msgId, source);
789
+ }
790
+ } else if (msg.topic === this.topicStrings[TopicType.block_proposal]) {
791
+ await this.processBlockFromPeer(p2pMessage.payload, msgId, source);
792
+ } else if (msg.topic === this.topicStrings[TopicType.checkpoint_proposal]) {
793
+ await this.handleGossipedCheckpointProposal(p2pMessage.payload, msgId, source);
794
+ } else {
795
+ this.logger.error(`Received message on unknown topic: ${msg.topic}`);
796
+ }
797
+ };
798
+
799
+ const latency = p2pMessage.timestamp !== undefined ? msgReceivedTime - p2pMessage.timestamp.getTime() : undefined;
800
+ const propagatedContext = p2pMessage.traceContext
801
+ ? this.telemetry.extractPropagatedContext(p2pMessage.traceContext)
802
+ : undefined;
803
+
804
+ if (propagatedContext) {
805
+ await this.tracer.startActiveSpan(
806
+ 'LibP2PService.processMessage',
807
+ {
808
+ attributes: {
809
+ [Attributes.TOPIC_NAME]: topicType!,
810
+ [Attributes.PEER_ID]: source.toString(),
811
+ },
812
+ },
813
+ propagatedContext,
814
+ async span => {
815
+ try {
816
+ await processMessage();
817
+ span.setStatus({
818
+ code: SpanStatusCode.OK,
819
+ });
820
+ } catch (err) {
821
+ span.setStatus({
822
+ code: SpanStatusCode.ERROR,
823
+ message: String(err),
824
+ });
825
+ if (typeof err === 'string' || (err && err instanceof Error)) {
826
+ span.recordException(err);
827
+ }
828
+ throw err;
829
+ } finally {
830
+ span.end();
831
+ }
832
+ },
833
+ );
834
+ } else {
835
+ await processMessage();
836
+ }
837
+
838
+ if (latency !== undefined && topicType !== undefined) {
839
+ this.instrumentation.recordMessageLatency(topicType, latency);
473
840
  }
474
841
 
475
842
  return;
476
843
  }
477
844
 
478
- private async validateReceivedMessage<T>(
479
- validationFunc: () => Promise<{ result: boolean; obj: T }>,
845
+ protected async validateReceivedMessage<T>(
846
+ validationFunc: () => Promise<ReceivedMessageValidationResult<T>>,
480
847
  msgId: string,
481
848
  source: PeerId,
482
- ): Promise<{ result: boolean; obj: T | undefined }> {
483
- let resultAndObj: { result: boolean; obj: T | undefined } = { result: false, obj: undefined };
849
+ topicType: TopicType,
850
+ ): Promise<ReceivedMessageValidationResult<T>> {
851
+ let resultAndObj: ReceivedMessageValidationResult<T> = { result: TopicValidatorResult.Reject };
852
+ const timer = new Timer();
484
853
  try {
485
854
  resultAndObj = await validationFunc();
486
855
  } catch (err) {
487
- this.logger.error(`Error deserialising and validating message `, err);
856
+ this.peerManager.penalizePeer(source, PeerErrorSeverity.LowToleranceError);
857
+ this.logger.error(`Error deserializing and validating gossipsub message`, err, {
858
+ msgId,
859
+ source: source.toString(),
860
+ topicType,
861
+ });
488
862
  }
489
863
 
490
- this.node.services.pubsub.reportMessageValidationResult(
491
- msgId,
492
- source.toString(),
493
- resultAndObj.result && resultAndObj.obj ? TopicValidatorResult.Accept : TopicValidatorResult.Reject,
494
- );
864
+ if (resultAndObj.result === TopicValidatorResult.Accept) {
865
+ this.instrumentation.recordMessageValidation(topicType, timer);
866
+ }
867
+
868
+ this.node.services.pubsub.reportMessageValidationResult(msgId, source.toString(), resultAndObj.result);
495
869
  return resultAndObj;
496
870
  }
497
871
 
498
- private async handleGossipedTx(msg: Message, msgId: string, source: PeerId) {
499
- const validationFunc = async () => {
500
- const tx = Tx.fromBuffer(Buffer.from(msg.data));
501
- const result = await this.validatePropagatedTx(tx, source);
502
- return { result, obj: tx };
872
+ protected async handleGossipedTx(payloadData: Buffer, msgId: string, source: PeerId) {
873
+ const validationFunc: () => Promise<ReceivedMessageValidationResult<Tx>> = async () => {
874
+ const tx = Tx.fromBuffer(payloadData);
875
+ const isValid = await this.validatePropagatedTx(tx, source);
876
+ const exists = isValid && (await this.mempools.txPool.hasTx(tx.getTxHash()));
877
+
878
+ this.logger.trace(`Validate propagated tx`, {
879
+ isValid,
880
+ exists,
881
+ [Attributes.P2P_ID]: source.toString(),
882
+ });
883
+
884
+ if (!isValid) {
885
+ return { result: TopicValidatorResult.Reject };
886
+ } else if (exists) {
887
+ return { result: TopicValidatorResult.Ignore, obj: tx };
888
+ } else {
889
+ return { result: TopicValidatorResult.Accept, obj: tx };
890
+ }
503
891
  };
504
892
 
505
- const { result, obj: tx } = await this.validateReceivedMessage<Tx>(validationFunc, msgId, source);
506
- if (!result || !tx) {
893
+ const { result, obj: tx } = await this.validateReceivedMessage<Tx>(validationFunc, msgId, source, TopicType.tx);
894
+ if (result !== TopicValidatorResult.Accept || !tx) {
507
895
  return;
508
896
  }
509
- const txHash = await tx.getTxHash();
897
+
898
+ const txHash = tx.getTxHash();
510
899
  const txHashString = txHash.toString();
511
- this.logger.verbose(`Received tx ${txHashString} from external peer ${source.toString()}.`);
900
+ this.logger.verbose(`Received tx ${txHashString} from external peer ${source.toString()} via gossip`, {
901
+ source: source.toString(),
902
+ txHash: txHashString,
903
+ });
904
+
905
+ if (this.config.dropTransactions && randomInt(1000) < this.config.dropTransactionsProbability * 1000) {
906
+ this.logger.warn(`Intentionally dropping tx ${txHashString} (probability rule)`);
907
+ return;
908
+ }
909
+
910
+ this.instrumentation.incrementTxReceived(1);
512
911
  await this.mempools.txPool.addTxs([tx]);
513
912
  }
514
913
 
515
- /**Process Attestation From Peer
516
- * When a proposal is received from a peer, we add it to the attestation pool, so it can be accessed by other services.
517
- *
518
- * @param attestation - The attestation to process.
914
+ /**
915
+ * Process a checkpoint attestation from a peer.
916
+ * Validates the attestation and adds it to the pool.
519
917
  */
520
- private async processAttestationFromPeer(msg: Message, msgId: string, source: PeerId): Promise<void> {
521
- const validationFunc = async () => {
522
- const attestation = BlockAttestation.fromBuffer(Buffer.from(msg.data));
523
- const result = await this.validateAttestation(source, attestation);
524
- this.logger.trace(`validatePropagatedAttestation: ${result}`, {
525
- [Attributes.SLOT_NUMBER]: attestation.payload.header.globalVariables.slotNumber.toString(),
918
+ private async processCheckpointAttestationFromPeer(
919
+ payloadData: Buffer,
920
+ msgId: string,
921
+ source: PeerId,
922
+ ): Promise<void> {
923
+ const validationFunc: () => Promise<ReceivedMessageValidationResult<CheckpointAttestation>> = async () => {
924
+ const attestation = CheckpointAttestation.fromBuffer(payloadData);
925
+ const pool = this.mempools.attestationPool;
926
+ const isValid = await this.validateCheckpointAttestation(source, attestation);
927
+ const exists = isValid && (await pool.hasCheckpointAttestation(attestation));
928
+
929
+ let canAdd = true;
930
+ if (isValid && !exists) {
931
+ const slot = attestation.payload.header.slotNumber;
932
+ const { committee } = await this.epochCache.getCommittee(slot);
933
+ const committeeSize = committee?.length ?? 0;
934
+ canAdd = await pool.canAddCheckpointAttestation(attestation, committeeSize);
935
+ }
936
+
937
+ this.logger.trace(`Validate propagated checkpoint attestation`, {
938
+ isValid,
939
+ exists,
940
+ canAdd,
941
+ [Attributes.SLOT_NUMBER]: attestation.payload.header.slotNumber.toString(),
526
942
  [Attributes.P2P_ID]: source.toString(),
527
943
  });
528
- return { result, obj: attestation };
944
+
945
+ if (!isValid) {
946
+ return { result: TopicValidatorResult.Reject };
947
+ } else if (exists) {
948
+ return { result: TopicValidatorResult.Ignore, obj: attestation };
949
+ } else if (!canAdd) {
950
+ this.logger.warn(`Dropping checkpoint attestation due to per-(slot, proposalId) attestation cap`, {
951
+ slot: attestation.payload.header.slotNumber.toString(),
952
+ archive: attestation.archive.toString(),
953
+ source: source.toString(),
954
+ });
955
+ return { result: TopicValidatorResult.Ignore, obj: attestation };
956
+ } else {
957
+ return { result: TopicValidatorResult.Accept, obj: attestation };
958
+ }
529
959
  };
530
960
 
531
- const { result, obj: attestation } = await this.validateReceivedMessage<BlockAttestation>(
961
+ const { result, obj: attestation } = await this.validateReceivedMessage<CheckpointAttestation>(
532
962
  validationFunc,
533
963
  msgId,
534
964
  source,
965
+ TopicType.checkpoint_attestation,
535
966
  );
536
- if (!result || !attestation) {
967
+
968
+ if (result !== TopicValidatorResult.Accept || !attestation) {
537
969
  return;
538
970
  }
971
+
539
972
  this.logger.debug(
540
- `Received attestation for block ${attestation.blockNumber.toNumber()} slot ${attestation.slotNumber.toNumber()} from external peer.`,
973
+ `Received checkpoint attestation for slot ${attestation.slotNumber} from external peer ${source.toString()}`,
541
974
  {
542
- p2pMessageIdentifier: await attestation.p2pMessageIdentifier(),
543
- slot: attestation.slotNumber.toNumber(),
975
+ p2pMessageIdentifier: await attestation.p2pMessageLoggingIdentifier(),
976
+ slot: attestation.slotNumber,
544
977
  archive: attestation.archive.toString(),
545
- block: attestation.blockNumber.toNumber(),
978
+ source: source.toString(),
546
979
  },
547
980
  );
548
- await this.mempools.attestationPool!.addAttestations([attestation]);
981
+
982
+ await this.mempools.attestationPool.addCheckpointAttestations([attestation]);
549
983
  }
550
984
 
551
- private async processBlockFromPeer(msg: Message, msgId: string, source: PeerId): Promise<void> {
552
- const validationFunc = async () => {
553
- const block = BlockProposal.fromBuffer(Buffer.from(msg.data));
554
- const result = await this.validateBlockProposal(source, block);
555
- this.logger.trace(`validatePropagatedBlock: ${result}`, {
556
- [Attributes.SLOT_NUMBER]: block.payload.header.globalVariables.slotNumber.toString(),
985
+ private async processBlockFromPeer(payloadData: Buffer, msgId: string, source: PeerId): Promise<void> {
986
+ const validationFunc: () => Promise<ReceivedMessageValidationResult<BlockProposal>> = async () => {
987
+ const block = BlockProposal.fromBuffer(payloadData);
988
+ const isValid = await this.validateBlockProposal(source, block);
989
+ const pool = this.mempools.attestationPool;
990
+
991
+ const exists = isValid && (await pool.hasBlockProposal(block));
992
+ const canAdd = isValid && (await pool.canAddProposal(block));
993
+
994
+ this.logger.trace(`Validate propagated block proposal`, {
995
+ isValid,
996
+ exists,
997
+ canAdd,
998
+ [Attributes.SLOT_NUMBER]: block.slotNumber.toString(),
557
999
  [Attributes.P2P_ID]: source.toString(),
558
1000
  });
559
- return { result, obj: block };
1001
+
1002
+ if (!isValid) {
1003
+ return { result: TopicValidatorResult.Reject };
1004
+ } else if (exists) {
1005
+ return { result: TopicValidatorResult.Ignore, obj: block };
1006
+ } else if (!canAdd) {
1007
+ this.peerManager.penalizePeer(source, PeerErrorSeverity.MidToleranceError);
1008
+ this.logger.warn(`Penalizing peer for block proposal exceeding per-slot cap`, {
1009
+ slot: block.slotNumber.toString(),
1010
+ archive: block.archive.toString(),
1011
+ source: source.toString(),
1012
+ });
1013
+ return { result: TopicValidatorResult.Reject };
1014
+ } else {
1015
+ return { result: TopicValidatorResult.Accept, obj: block };
1016
+ }
560
1017
  };
561
1018
 
562
- const { result, obj: block } = await this.validateReceivedMessage<BlockProposal>(validationFunc, msgId, source);
1019
+ const { result, obj: block } = await this.validateReceivedMessage<BlockProposal>(
1020
+ validationFunc,
1021
+ msgId,
1022
+ source,
1023
+ TopicType.block_proposal,
1024
+ );
1025
+
563
1026
  if (!result || !block) {
564
1027
  return;
565
1028
  }
566
- await this.processValidBlockProposal(block);
1029
+
1030
+ await this.processValidBlockProposal(block, source);
567
1031
  }
568
1032
 
569
1033
  // REVIEW: callback pattern https://github.com/AztecProtocol/aztec-packages/issues/7963
1034
+ // REFACTOR(palla): This method should be moved to the p2p_client or to a separate component,
1035
+ // should not be here as it does not deal with p2p networking.
570
1036
  @trackSpan('Libp2pService.processValidBlockProposal', async block => ({
571
- [Attributes.BLOCK_NUMBER]: block.blockNumber.toNumber(),
572
- [Attributes.SLOT_NUMBER]: block.slotNumber.toNumber(),
1037
+ [Attributes.SLOT_NUMBER]: block.slotNumber,
573
1038
  [Attributes.BLOCK_ARCHIVE]: block.archive.toString(),
574
- [Attributes.P2P_ID]: await block.p2pMessageIdentifier().then(i => i.toString()),
1039
+ [Attributes.P2P_ID]: await block.p2pMessageLoggingIdentifier().then(i => i.toString()),
575
1040
  }))
576
- private async processValidBlockProposal(block: BlockProposal) {
577
- this.logger.verbose(
578
- `Received block ${block.blockNumber.toNumber()} for slot ${block.slotNumber.toNumber()} from external peer.`,
579
- {
580
- p2pMessageIdentifier: await block.p2pMessageIdentifier(),
581
- slot: block.slotNumber.toNumber(),
582
- archive: block.archive.toString(),
583
- block: block.blockNumber.toNumber(),
584
- },
1041
+ private async processValidBlockProposal(block: BlockProposal, sender: PeerId) {
1042
+ const slot = block.slotNumber;
1043
+ this.logger.verbose(`Received block proposal for slot ${slot} from external peer ${sender.toString()}.`, {
1044
+ p2pMessageIdentifier: await block.p2pMessageLoggingIdentifier(),
1045
+ source: sender.toString(),
1046
+ ...block.toBlockInfo(),
1047
+ });
1048
+
1049
+ // Attempt to add proposal
1050
+ try {
1051
+ await this.mempools.attestationPool.addBlockProposal(block);
1052
+ } catch (err: unknown) {
1053
+ // Drop proposals if we hit per-slot cap in the attestation pool; rethrow unknown errors
1054
+ if (err instanceof ProposalSlotCapExceededError) {
1055
+ this.logger.warn(`Dropping block proposal due to per-slot proposal cap`, {
1056
+ slot: String(slot),
1057
+ archive: block.archive.toString(),
1058
+ error: (err as Error).message,
1059
+ });
1060
+ return;
1061
+ }
1062
+ throw err;
1063
+ }
1064
+
1065
+ // Mark the txs in this proposal as non-evictable
1066
+ await this.mempools.txPool.markTxsAsNonEvictable(block.txHashes);
1067
+
1068
+ // Call the block received callback to validate the proposal.
1069
+ // Note: Validators do NOT attest to individual blocks, only to checkpoint proposals.
1070
+ const isValid = await this.blockReceivedCallback(block, sender);
1071
+ if (!isValid) {
1072
+ this.logger.warn(`Block proposal validation failed for block ${block.blockNumber}`, block.toBlockInfo());
1073
+ }
1074
+ }
1075
+
1076
+ /**
1077
+ * Handle a gossiped checkpoint proposal.
1078
+ * Validates and processes the checkpoint proposal, then triggers the callback for attestation.
1079
+ */
1080
+ private async handleGossipedCheckpointProposal(payloadData: Buffer, msgId: string, source: PeerId): Promise<void> {
1081
+ // TODO(palla/mbps): This pattern is repeated across multiple message handlers, consider abstracting it.
1082
+ const validationFunc: () => Promise<ReceivedMessageValidationResult<CheckpointProposal>> = async () => {
1083
+ const checkpoint = CheckpointProposal.fromBuffer(payloadData);
1084
+ const isValid = await this.validateCheckpointProposal(source, checkpoint);
1085
+ const pool = this.mempools.attestationPool;
1086
+
1087
+ const exists = isValid && (await pool.hasCheckpointProposal(checkpoint));
1088
+ const canAdd = isValid && (await pool.canAddCheckpointProposal(checkpoint));
1089
+
1090
+ this.logger.trace(`Validate propagated checkpoint proposal`, {
1091
+ isValid,
1092
+ exists,
1093
+ canAdd,
1094
+ [Attributes.SLOT_NUMBER]: checkpoint.slotNumber.toString(),
1095
+ [Attributes.P2P_ID]: source.toString(),
1096
+ });
1097
+
1098
+ if (!isValid) {
1099
+ return { result: TopicValidatorResult.Reject };
1100
+ } else if (exists) {
1101
+ return { result: TopicValidatorResult.Ignore, obj: checkpoint };
1102
+ } else if (!canAdd) {
1103
+ this.peerManager.penalizePeer(source, PeerErrorSeverity.MidToleranceError);
1104
+ this.logger.warn(`Penalizing peer for checkpoint proposal exceeding per-slot cap`, {
1105
+ slot: checkpoint.slotNumber.toString(),
1106
+ archive: checkpoint.archive.toString(),
1107
+ source: source.toString(),
1108
+ });
1109
+ return { result: TopicValidatorResult.Reject };
1110
+ } else {
1111
+ return { result: TopicValidatorResult.Accept, obj: checkpoint };
1112
+ }
1113
+ };
1114
+
1115
+ const { result, obj: checkpoint } = await this.validateReceivedMessage<CheckpointProposal>(
1116
+ validationFunc,
1117
+ msgId,
1118
+ source,
1119
+ TopicType.checkpoint_proposal,
585
1120
  );
586
- const attestation = await this.blockReceivedCallback(block);
587
1121
 
588
- // TODO: fix up this pattern - the abstraction is not nice
589
- // The attestation can be undefined if no handler is registered / the validator deems the block invalid
590
- if (attestation != undefined) {
591
- this.logger.verbose(
592
- `Broadcasting attestation for block ${attestation.blockNumber.toNumber()} slot ${attestation.slotNumber.toNumber()}`,
593
- {
594
- p2pMessageIdentifier: await attestation.p2pMessageIdentifier(),
595
- slot: attestation.slotNumber.toNumber(),
596
- archive: attestation.archive.toString(),
597
- block: attestation.blockNumber.toNumber(),
598
- },
599
- );
600
- await this.broadcastAttestation(attestation);
1122
+ if (result !== TopicValidatorResult.Accept || !checkpoint) {
1123
+ return;
601
1124
  }
1125
+
1126
+ await this.processValidCheckpointProposal(checkpoint, source);
602
1127
  }
603
1128
 
604
1129
  /**
605
- * Broadcast an attestation to all peers.
606
- * @param attestation - The attestation to broadcast.
1130
+ * Process a validated checkpoint proposal.
1131
+ * Extracts and processes the last block proposal (if present) first, then processes the checkpoint.
1132
+ * The block callback is invoked before the checkpoint callback.
607
1133
  */
608
- @trackSpan('Libp2pService.broadcastAttestation', async attestation => ({
609
- [Attributes.BLOCK_NUMBER]: attestation.payload.header.globalVariables.blockNumber.toNumber(),
610
- [Attributes.SLOT_NUMBER]: attestation.payload.header.globalVariables.slotNumber.toNumber(),
611
- [Attributes.BLOCK_ARCHIVE]: attestation.archive.toString(),
612
- [Attributes.P2P_ID]: await attestation.p2pMessageIdentifier().then(i => i.toString()),
1134
+ @trackSpan('Libp2pService.processValidCheckpointProposal', async checkpoint => ({
1135
+ [Attributes.SLOT_NUMBER]: checkpoint.slotNumber,
1136
+ [Attributes.BLOCK_ARCHIVE]: checkpoint.archive.toString(),
1137
+ [Attributes.P2P_ID]: await checkpoint.p2pMessageLoggingIdentifier().then(i => i.toString()),
613
1138
  }))
614
- private async broadcastAttestation(attestation: BlockAttestation) {
615
- await this.propagate(attestation);
1139
+ private async processValidCheckpointProposal(checkpoint: CheckpointProposal, sender: PeerId) {
1140
+ const slot = checkpoint.slotNumber;
1141
+ this.logger.verbose(`Received checkpoint proposal for slot ${slot} from external peer ${sender.toString()}.`, {
1142
+ p2pMessageIdentifier: await checkpoint.p2pMessageLoggingIdentifier(),
1143
+ slot: checkpoint.slotNumber,
1144
+ archive: checkpoint.archive.toString(),
1145
+ source: sender.toString(),
1146
+ });
1147
+
1148
+ // Extract block proposal before adding to pool (pool stores them separately)
1149
+ const blockProposal = checkpoint.getBlockProposal();
1150
+
1151
+ // Add proposal to the pool (this extracts and stores block proposal separately)
1152
+ await this.mempools.attestationPool.addCheckpointProposal(checkpoint);
1153
+
1154
+ // Mark txs as non-evictable if present (from the last block)
1155
+ if (checkpoint.txHashes.length > 0) {
1156
+ await this.mempools.txPool.markTxsAsNonEvictable(checkpoint.txHashes);
1157
+ }
1158
+
1159
+ // If there was a last block proposal, invoke the block callback first for validation.
1160
+ // Note: The block proposal is already stored in the pool by addCheckpointProposal.
1161
+ if (blockProposal) {
1162
+ const isValid = await this.blockReceivedCallback(blockProposal, sender);
1163
+ if (!isValid) {
1164
+ this.logger.warn(`Block proposal from checkpoint failed validation`, {
1165
+ slot: slot.toString(),
1166
+ archive: checkpoint.archive.toString(),
1167
+ blockNumber: blockProposal.blockNumber.toString(),
1168
+ });
1169
+ return;
1170
+ }
1171
+ }
1172
+
1173
+ // Call the checkpoint received callback with the core version (without lastBlock)
1174
+ // to validate and potentially generate attestations
1175
+ const attestations = await this.checkpointReceivedCallback(checkpoint.toCore(), sender);
1176
+ if (attestations && attestations.length > 0) {
1177
+ // If the callback returned attestations, add them to the pool and propagate them
1178
+ await this.mempools.attestationPool.addCheckpointAttestations(attestations);
1179
+ for (const attestation of attestations) {
1180
+ await this.propagate(attestation);
1181
+ }
1182
+ }
616
1183
  }
617
1184
 
618
1185
  /**
@@ -620,111 +1187,312 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
620
1187
  * @param message - The message to propagate.
621
1188
  */
622
1189
  public async propagate<T extends Gossipable>(message: T) {
623
- const p2pMessageIdentifier = await message.p2pMessageIdentifier();
1190
+ const p2pMessageIdentifier = await message.p2pMessageLoggingIdentifier();
624
1191
  this.logger.trace(`Message ${p2pMessageIdentifier} queued`, { p2pMessageIdentifier });
625
- void this.jobQueue
626
- .put(async () => {
627
- await this.sendToPeers(message);
628
- })
629
- .catch(error => {
630
- this.logger.error(`Error propagating message ${p2pMessageIdentifier}`, { error });
631
- });
1192
+ void this.sendToPeers(message).catch(error => {
1193
+ this.logger.error(`Error propagating message ${p2pMessageIdentifier}`, { error });
1194
+ });
632
1195
  }
633
1196
 
634
1197
  /**
635
- * Validate a tx that has been requested from a peer.
1198
+ * Validate the requested block transactions. Allow partial returns.
1199
+ * @param request - The block transactions request.
1200
+ * @param response - The block transactions response.
1201
+ * @param peerId - The ID of the peer that made the request.
1202
+ * @returns True if the requested block transactions are valid, false otherwise.
1203
+ */
1204
+ @trackSpan('Libp2pService.validateRequestedBlockTxs', request => ({
1205
+ [Attributes.BLOCK_HASH]: request.blockHash.toString(),
1206
+ }))
1207
+ private async validateRequestedBlockTxs(
1208
+ request: BlockTxsRequest,
1209
+ response: BlockTxsResponse,
1210
+ peerId: PeerId,
1211
+ ): Promise<boolean> {
1212
+ const requestedTxValidator = this.createRequestedTxValidator();
1213
+
1214
+ try {
1215
+ if (!response.blockHash.equals(request.blockHash)) {
1216
+ this.peerManager.penalizePeer(peerId, PeerErrorSeverity.MidToleranceError);
1217
+ throw new ValidationError(
1218
+ `Received block txs for unexpected block: expected ${request.blockHash.toString()}, got ${response.blockHash.toString()}`,
1219
+ );
1220
+ }
1221
+
1222
+ if (response.txIndices.getLength() !== request.txIndices.getLength()) {
1223
+ this.peerManager.penalizePeer(peerId, PeerErrorSeverity.MidToleranceError);
1224
+ throw new ValidationError(
1225
+ `Received block txs with mismatched bitvector length: expected ${request.txIndices.getLength()}, got ${response.txIndices.getLength()}`,
1226
+ );
1227
+ }
1228
+
1229
+ // Check no duplicates and not exceeding returnable count
1230
+ const requestedIndices = new Set(request.txIndices.getTrueIndices());
1231
+ const availableIndices = new Set(response.txIndices.getTrueIndices());
1232
+ const maxReturnable = [...requestedIndices].filter(i => availableIndices.has(i)).length;
1233
+
1234
+ const returnedHashes = await Promise.all(response.txs.map(tx => tx.getTxHash().toString()));
1235
+ const uniqueReturned = new Set(returnedHashes.map(h => h.toString()));
1236
+ if (uniqueReturned.size !== returnedHashes.length) {
1237
+ this.peerManager.penalizePeer(peerId, PeerErrorSeverity.MidToleranceError);
1238
+ throw new ValidationError(`Received duplicate txs in block txs response`);
1239
+ }
1240
+ if (response.txs.length > maxReturnable) {
1241
+ this.peerManager.penalizePeer(peerId, PeerErrorSeverity.MidToleranceError);
1242
+ throw new ValidationError(
1243
+ `Received more txs (${response.txs.length}) than requested-and-available (${maxReturnable})`,
1244
+ );
1245
+ }
1246
+
1247
+ // Given proposal (should have locally), ensure returned txs are valid subset and match request indices
1248
+ const proposal = await this.mempools.attestationPool.getBlockProposal(request.blockHash.toString());
1249
+ if (proposal) {
1250
+ // Build intersected indices
1251
+ const intersectIdx = request.txIndices.getTrueIndices().filter(i => response.txIndices.isSet(i));
1252
+
1253
+ // Enforce subset membership and preserve increasing order by index.
1254
+ const hashToIndexInProposal = new Map<string, number>(
1255
+ proposal.txHashes.map((h, i) => [h.toString(), i] as [string, number]),
1256
+ );
1257
+ const allowedIndexSet = new Set(intersectIdx);
1258
+ const indices = returnedHashes.map(h => hashToIndexInProposal.get(h));
1259
+ const allAllowed = indices.every(idx => idx !== undefined && allowedIndexSet.has(idx));
1260
+ const strictlyIncreasing = indices.every((idx, i) => (i === 0 ? idx !== undefined : idx! > indices[i - 1]!));
1261
+ if (!allAllowed || !strictlyIncreasing) {
1262
+ this.peerManager.penalizePeer(peerId, PeerErrorSeverity.LowToleranceError);
1263
+ throw new ValidationError('Returned txs do not match expected subset/order for requested indices');
1264
+ }
1265
+ } else {
1266
+ // No local proposal, cannot check the membership/order of the returned txs
1267
+ this.logger.warn(
1268
+ `Block proposal not found for block hash ${request.blockHash.toString()}; cannot validate membership/order of returned txs`,
1269
+ );
1270
+ return false;
1271
+ }
1272
+
1273
+ await Promise.all(response.txs.map(tx => this.validateRequestedTx(tx, peerId, requestedTxValidator)));
1274
+ return true;
1275
+ } catch (e: any) {
1276
+ if (e instanceof ValidationError) {
1277
+ this.logger.warn(`Failed validation for requested block txs from peer ${peerId.toString()}`);
1278
+ } else {
1279
+ this.logger.error(`Error during validation of requested block txs`, e);
1280
+ }
1281
+
1282
+ return false;
1283
+ }
1284
+ }
1285
+
1286
+ /**
1287
+ * Validate a collection of txs that has been requested from a peer.
636
1288
  *
637
- * The core component of this validator is that the tx hash MUST match the requested tx hash,
1289
+ * The core component of this validator is that each tx hash MUST match the requested tx hash,
638
1290
  * In order to perform this check, the tx proof must be verified.
639
1291
  *
640
1292
  * Note: This function is called from within `ReqResp.sendRequest` as part of the
641
1293
  * ReqRespSubProtocol.TX subprotocol validation.
642
1294
  *
643
- * @param requestedTxHash - The hash of the tx that was requested.
644
- * @param responseTx - The tx that was received as a response to the request.
1295
+ * @param requestedTxHash - The collection of the txs that was requested.
1296
+ * @param responseTx - The collection of txs that was received as a response to the request.
645
1297
  * @param peerId - The peer ID of the peer that sent the tx.
646
- * @returns True if the tx is valid, false otherwise.
1298
+ * @returns True if the whole collection of txs is valid, false otherwise.
647
1299
  */
648
1300
  @trackSpan('Libp2pService.validateRequestedTx', (requestedTxHash, _responseTx) => ({
649
1301
  [Attributes.TX_HASH]: requestedTxHash.toString(),
650
1302
  }))
651
- private async validateRequestedTx(requestedTxHash: TxHash, responseTx: Tx, peerId: PeerId): Promise<boolean> {
652
- const proofValidator = new TxProofValidator(this.proofVerifier);
653
- const validProof = await proofValidator.validateTx(responseTx);
654
-
655
- // If the node returns the wrong data, we penalize it
656
- if (!requestedTxHash.equals(await responseTx.getTxHash())) {
657
- // Returning the wrong data is a low tolerance error
658
- this.peerManager.penalizePeer(peerId, PeerErrorSeverity.MidToleranceError);
1303
+ private async validateRequestedTxs(requestedTxHash: TxHash[], responseTx: Tx[], peerId: PeerId): Promise<boolean> {
1304
+ const requested = new Set(requestedTxHash.map(h => h.toString()));
1305
+ const requestedTxValidator = this.createRequestedTxValidator();
1306
+
1307
+ //TODO: (mralj) - this is somewhat naive implementation, if single tx is invlid we consider the whole response invalid.
1308
+ // I think we should still extract the valid txs and return them, so that we can still use the response.
1309
+ try {
1310
+ await Promise.all(responseTx.map(tx => this.validateRequestedTx(tx, peerId, requestedTxValidator, requested)));
1311
+ return true;
1312
+ } catch (e: any) {
1313
+ if (e instanceof ValidationError) {
1314
+ this.logger.warn(`Failed to validate requested txs from peer ${peerId.toString()}, reason ${e.message}`);
1315
+ } else {
1316
+ this.logger.error(`Error during validation of requested txs`, e);
1317
+ }
1318
+
659
1319
  return false;
660
1320
  }
1321
+ }
661
1322
 
662
- if (validProof.result === 'invalid') {
663
- // If the proof is invalid, but the txHash is correct, then this is an active attack and we severly punish
664
- this.peerManager.penalizePeer(peerId, PeerErrorSeverity.LowToleranceError);
1323
+ /**
1324
+ * Validates a BLOCK response.
1325
+ *
1326
+ * If a local copy exists, enforces hash equality. If missing, rejects (no penalty) since the hash cannot be verified.
1327
+ * Penalizes on block number mismatch or hash mismatch.
1328
+ *
1329
+ * @param requestedBlockNumber - The requested block number.
1330
+ * @param responseBlock - The block returned by the peer.
1331
+ * @param peerId - The peer that returned the block.
1332
+ * @returns True if the response is valid, false otherwise.
1333
+ */
1334
+ @trackSpan('Libp2pService.validateRequestedBlock', (requestedBlockNumber, _responseBlock) => ({
1335
+ [Attributes.BLOCK_NUMBER]: requestedBlockNumber.toString(),
1336
+ }))
1337
+ private async validateRequestedBlock(
1338
+ requestedBlockNumber: Fr,
1339
+ responseBlock: L2Block,
1340
+ peerId: PeerId,
1341
+ ): Promise<boolean> {
1342
+ try {
1343
+ const reqNum = Number(requestedBlockNumber.toString());
1344
+ if (responseBlock.number !== reqNum) {
1345
+ this.peerManager.penalizePeer(peerId, PeerErrorSeverity.LowToleranceError);
1346
+ return false;
1347
+ }
1348
+
1349
+ const local = await this.archiver.getBlock(BlockNumber(reqNum));
1350
+ if (!local) {
1351
+ // We are missing the local block; we cannot verify the hash yet. Reject without penalizing.
1352
+ // TODO: Consider extending this validator to accept an expected hash or
1353
+ // performing quorum-based checks when using P2P syncing prior to L1 sync.
1354
+ this.logger.warn(`Local block ${reqNum} not found; rejecting BLOCK response without hash verification`);
1355
+ return false;
1356
+ }
1357
+ const [localHash, respHash] = await Promise.all([local.hash(), responseBlock.hash()]);
1358
+ if (!localHash.equals(respHash)) {
1359
+ this.peerManager.penalizePeer(peerId, PeerErrorSeverity.MidToleranceError);
1360
+ return false;
1361
+ }
1362
+
1363
+ return true;
1364
+ } catch (e) {
1365
+ this.logger.warn(`Error validating requested block`, e);
665
1366
  return false;
666
1367
  }
1368
+ }
667
1369
 
668
- return true;
1370
+ private createRequestedTxValidator(): TxValidator {
1371
+ return new AggregateTxValidator(
1372
+ new DataTxValidator(),
1373
+ new MetadataTxValidator({
1374
+ l1ChainId: new Fr(this.config.l1ChainId),
1375
+ rollupVersion: new Fr(this.config.rollupVersion),
1376
+ protocolContractsHash,
1377
+ vkTreeRoot: getVKTreeRoot(),
1378
+ }),
1379
+ new TxProofValidator(this.proofVerifier),
1380
+ );
669
1381
  }
670
1382
 
671
- @trackSpan('Libp2pService.validatePropagatedTx', async tx => ({
672
- [Attributes.TX_HASH]: (await tx.getTxHash()).toString(),
1383
+ private async validateRequestedTx(tx: Tx, peerId: PeerId, txValidator: TxValidator, requested?: Set<`0x${string}`>) {
1384
+ const penalize = (severity: PeerErrorSeverity) => this.peerManager.penalizePeer(peerId, severity);
1385
+
1386
+ if (!(await tx.validateTxHash())) {
1387
+ penalize(PeerErrorSeverity.MidToleranceError);
1388
+ throw new ValidationError(`Received tx with invalid hash ${tx.getTxHash().toString()}.`);
1389
+ }
1390
+
1391
+ if (requested && !requested.has(tx.getTxHash().toString())) {
1392
+ penalize(PeerErrorSeverity.MidToleranceError);
1393
+ throw new ValidationError(`Received tx with hash ${tx.getTxHash().toString()} that was not requested.`);
1394
+ }
1395
+
1396
+ const { result } = await txValidator.validateTx(tx);
1397
+ if (result === 'invalid') {
1398
+ penalize(PeerErrorSeverity.LowToleranceError);
1399
+ throw new ValidationError(`Received tx with hash ${tx.getTxHash().toString()} that is invalid.`);
1400
+ }
1401
+ }
1402
+
1403
+ @trackSpan('Libp2pService.validatePropagatedTx', tx => ({
1404
+ [Attributes.TX_HASH]: tx.getTxHash().toString(),
673
1405
  }))
674
1406
  private async validatePropagatedTx(tx: Tx, peerId: PeerId): Promise<boolean> {
675
- const blockNumber = (await this.l2BlockSource.getBlockNumber()) + 1;
676
- const messageValidators = this.createMessageValidators(blockNumber);
677
- const outcome = await this.runValidations(tx, messageValidators);
1407
+ const currentBlockNumber = await this.archiver.getBlockNumber();
678
1408
 
679
- if (outcome.allPassed) {
680
- return true;
1409
+ // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
1410
+ const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
1411
+ const messageValidators = await this.createMessageValidators(currentBlockNumber, nextSlotTimestamp);
1412
+
1413
+ for (const validator of messageValidators) {
1414
+ const outcome = await this.runValidations(tx, validator);
1415
+
1416
+ if (outcome.allPassed) {
1417
+ continue;
1418
+ }
1419
+ const { name } = outcome.failure;
1420
+ let { severity } = outcome.failure;
1421
+
1422
+ // Double spend validator has a special case handler
1423
+ if (name === 'doubleSpendValidator') {
1424
+ const txBlockNumber = BlockNumber(currentBlockNumber + 1); // tx is expected to be in the next block
1425
+ severity = await this.handleDoubleSpendFailure(tx, txBlockNumber);
1426
+ }
1427
+
1428
+ this.peerManager.penalizePeer(peerId, severity);
1429
+ return false;
681
1430
  }
682
- const { name } = outcome.failure;
683
- let { severity } = outcome.failure;
1431
+ return true;
1432
+ }
684
1433
 
685
- // Double spend validator has a special case handler
686
- if (name === 'doubleSpendValidator') {
687
- severity = await this.handleDoubleSpendFailure(tx, blockNumber);
1434
+ private async getGasFees(blockNumber: BlockNumber): Promise<GasFees> {
1435
+ if (blockNumber === this.feesCache?.blockNumber) {
1436
+ return this.feesCache.gasFees;
688
1437
  }
689
1438
 
690
- this.peerManager.penalizePeer(peerId, severity);
691
- return false;
1439
+ const header = await this.archiver.getBlockHeader(blockNumber);
1440
+ const gasFees = header?.globalVariables.gasFees ?? GasFees.empty();
1441
+ this.feesCache = { blockNumber, gasFees };
1442
+ return gasFees;
1443
+ }
1444
+
1445
+ public async validate(txs: Tx[]): Promise<void> {
1446
+ const currentBlockNumber = await this.archiver.getBlockNumber();
1447
+
1448
+ // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
1449
+ const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
1450
+ const messageValidators = await this.createMessageValidators(currentBlockNumber, nextSlotTimestamp);
1451
+
1452
+ await Promise.all(
1453
+ txs.map(async tx => {
1454
+ for (const validator of messageValidators) {
1455
+ const outcome = await this.runValidations(tx, validator);
1456
+ if (!outcome.allPassed) {
1457
+ throw new Error('Invalid tx detected', { cause: { outcome } });
1458
+ }
1459
+ }
1460
+ }),
1461
+ );
692
1462
  }
693
1463
 
694
1464
  /**
695
- * Create message validators for the given block number.
1465
+ * Create message validators for the given block number and timestamp.
696
1466
  *
697
1467
  * Each validator is a pair of a validator and a severity.
698
1468
  * If a validator fails, the peer is penalized with the severity of the validator.
699
1469
  *
700
- * @param blockNumber - The block number to create validators for.
1470
+ * @param currentBlockNumber - The current synced block number.
1471
+ * @param nextSlotTimestamp - The timestamp of the next slot (used to validate txs are not expired).
701
1472
  * @returns The message validators.
702
1473
  */
703
- private createMessageValidators(blockNumber: number): Record<string, MessageValidator> {
704
- return {
705
- dataValidator: {
706
- validator: new DataTxValidator(),
707
- severity: PeerErrorSeverity.HighToleranceError,
708
- },
709
- metadataValidator: {
710
- validator: new MetadataTxValidator(new Fr(this.config.l1ChainId), new Fr(blockNumber)),
711
- severity: PeerErrorSeverity.HighToleranceError,
712
- },
713
- proofValidator: {
714
- validator: new TxProofValidator(this.proofVerifier),
715
- severity: PeerErrorSeverity.MidToleranceError,
716
- },
717
- doubleSpendValidator: {
718
- validator: new DoubleSpendTxValidator({
719
- nullifiersExist: async (nullifiers: Buffer[]) => {
720
- const merkleTree = this.worldStateSynchronizer.getCommitted();
721
- const indices = await merkleTree.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, nullifiers);
722
- return indices.map(index => index !== undefined);
723
- },
724
- }),
725
- severity: PeerErrorSeverity.HighToleranceError,
726
- },
727
- };
1474
+ private async createMessageValidators(
1475
+ currentBlockNumber: BlockNumber,
1476
+ nextSlotTimestamp: UInt64,
1477
+ ): Promise<Record<string, MessageValidator>[]> {
1478
+ const gasFees = await this.getGasFees(currentBlockNumber);
1479
+ const allowedInSetup = this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
1480
+
1481
+ const blockNumberInWhichTheTxIsConsideredToBeIncluded = BlockNumber(currentBlockNumber + 1);
1482
+
1483
+ return createTxMessageValidators(
1484
+ nextSlotTimestamp,
1485
+ blockNumberInWhichTheTxIsConsideredToBeIncluded,
1486
+ this.worldStateSynchronizer,
1487
+ gasFees,
1488
+ this.config.l1ChainId,
1489
+ this.config.rollupVersion,
1490
+ protocolContractsHash,
1491
+ this.archiver,
1492
+ this.proofVerifier,
1493
+ !this.config.disableTransactions,
1494
+ allowedInSetup,
1495
+ );
728
1496
  }
729
1497
 
730
1498
  /**
@@ -739,28 +1507,26 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
739
1507
  ): Promise<ValidationOutcome> {
740
1508
  const validationPromises = Object.entries(messageValidators).map(async ([name, { validator, severity }]) => {
741
1509
  const { result } = await validator.validateTx(tx);
742
- return { name, isValid: result === 'valid', severity };
1510
+ return { name, isValid: result !== 'invalid', severity };
743
1511
  });
744
1512
 
745
1513
  // A promise that resolves when all validations have been run
746
- const allValidations = Promise.all(validationPromises);
747
-
748
- // A promise that resolves when the first validation fails
749
- const firstFailure = Promise.race(
750
- validationPromises.map(async promise => {
751
- const result = await promise;
752
- return result.isValid ? new Promise(() => {}) : result;
753
- }),
754
- );
755
-
756
- // Wait for the first validation to fail or all validations to pass
757
- const result = await Promise.race([
758
- allValidations.then(() => ({ allPassed: true as const })),
759
- firstFailure.then(failure => ({ allPassed: false as const, failure: failure as ValidationResult })),
760
- ]);
761
-
762
- // If all validations pass, allPassed will be true, if failed, then the failure will be the first validation to fail
763
- return result;
1514
+ const allValidations = await Promise.all(validationPromises);
1515
+ const failed = allValidations.find(x => !x.isValid);
1516
+ if (failed) {
1517
+ return {
1518
+ allPassed: false,
1519
+ failure: {
1520
+ isValid: { result: 'invalid' as const, reason: ['Failed validation'] },
1521
+ name: failed.name,
1522
+ severity: failed.severity,
1523
+ },
1524
+ };
1525
+ } else {
1526
+ return {
1527
+ allPassed: true,
1528
+ };
1529
+ }
764
1530
  }
765
1531
 
766
1532
  /**
@@ -774,7 +1540,7 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
774
1540
  * @param peerId - The peer ID of the peer that sent the tx.
775
1541
  * @returns Severity
776
1542
  */
777
- private async handleDoubleSpendFailure(tx: Tx, blockNumber: number): Promise<PeerErrorSeverity> {
1543
+ private async handleDoubleSpendFailure(tx: Tx, blockNumber: BlockNumber): Promise<PeerErrorSeverity> {
778
1544
  if (blockNumber <= this.config.doubleSpendSeverePeerPenaltyWindow) {
779
1545
  return PeerErrorSeverity.HighToleranceError;
780
1546
  }
@@ -782,7 +1548,7 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
782
1548
  const snapshotValidator = new DoubleSpendTxValidator({
783
1549
  nullifiersExist: async (nullifiers: Buffer[]) => {
784
1550
  const merkleTree = this.worldStateSynchronizer.getSnapshot(
785
- blockNumber - this.config.doubleSpendSeverePeerPenaltyWindow,
1551
+ BlockNumber(blockNumber - this.config.doubleSpendSeverePeerPenaltyWindow),
786
1552
  );
787
1553
  const indices = await merkleTree.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, nullifiers);
788
1554
  return indices.map(index => index !== undefined);
@@ -798,20 +1564,20 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
798
1564
  }
799
1565
 
800
1566
  /**
801
- * Validate an attestation.
1567
+ * Validate a checkpoint attestation.
802
1568
  *
803
- * @param attestation - The attestation to validate.
804
- * @returns True if the attestation is valid, false otherwise.
1569
+ * @param attestation - The checkpoint attestation to validate.
1570
+ * @returns True if the checkpoint attestation is valid, false otherwise.
805
1571
  */
806
- @trackSpan('Libp2pService.validateAttestation', async (_, attestation) => ({
807
- [Attributes.BLOCK_NUMBER]: attestation.payload.header.globalVariables.blockNumber.toNumber(),
808
- [Attributes.SLOT_NUMBER]: attestation.payload.header.globalVariables.slotNumber.toNumber(),
1572
+ @trackSpan('Libp2pService.validateCheckpointAttestation', async (_, attestation) => ({
1573
+ [Attributes.SLOT_NUMBER]: attestation.payload.header.slotNumber,
809
1574
  [Attributes.BLOCK_ARCHIVE]: attestation.archive.toString(),
810
- [Attributes.P2P_ID]: await attestation.p2pMessageIdentifier().then(i => i.toString()),
1575
+ [Attributes.P2P_ID]: await attestation.p2pMessageLoggingIdentifier().then(i => i.toString()),
811
1576
  }))
812
- public async validateAttestation(peerId: PeerId, attestation: BlockAttestation): Promise<boolean> {
813
- const severity = await this.attestationValidator.validate(attestation);
1577
+ public async validateCheckpointAttestation(peerId: PeerId, attestation: CheckpointAttestation): Promise<boolean> {
1578
+ const severity = await this.checkpointAttestationValidator.validate(attestation);
814
1579
  if (severity) {
1580
+ this.logger.debug(`Penalizing peer ${peerId} for checkpoint attestation validation failure`);
815
1581
  this.peerManager.penalizePeer(peerId, severity);
816
1582
  return false;
817
1583
  }
@@ -826,11 +1592,32 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
826
1592
  * @returns True if the block proposal is valid, false otherwise.
827
1593
  */
828
1594
  @trackSpan('Libp2pService.validateBlockProposal', (_peerId, block) => ({
829
- [Attributes.SLOT_NUMBER]: block.payload.header.globalVariables.slotNumber.toString(),
1595
+ [Attributes.SLOT_NUMBER]: block.slotNumber.toString(),
830
1596
  }))
831
1597
  public async validateBlockProposal(peerId: PeerId, block: BlockProposal): Promise<boolean> {
832
1598
  const severity = await this.blockProposalValidator.validate(block);
833
1599
  if (severity) {
1600
+ this.logger.debug(`Penalizing peer ${peerId} for block proposal validation failure`);
1601
+ this.peerManager.penalizePeer(peerId, severity);
1602
+ return false;
1603
+ }
1604
+
1605
+ return true;
1606
+ }
1607
+
1608
+ /**
1609
+ * Validate a checkpoint proposal.
1610
+ *
1611
+ * @param checkpoint - The checkpoint proposal to validate.
1612
+ * @returns True if the checkpoint proposal is valid, false otherwise.
1613
+ */
1614
+ @trackSpan('Libp2pService.validateCheckpointProposal', (_peerId, checkpoint) => ({
1615
+ [Attributes.SLOT_NUMBER]: checkpoint.slotNumber.toString(),
1616
+ }))
1617
+ public async validateCheckpointProposal(peerId: PeerId, checkpoint: CheckpointProposal): Promise<boolean> {
1618
+ const severity = await this.checkpointProposalValidator.validate(checkpoint);
1619
+ if (severity) {
1620
+ this.logger.debug(`Penalizing peer ${peerId} for checkpoint proposal validation failure`);
834
1621
  this.peerManager.penalizePeer(peerId, severity);
835
1622
  return false;
836
1623
  }
@@ -842,13 +1629,17 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement
842
1629
  return this.node.services.pubsub.score.score(peerId.toString());
843
1630
  }
844
1631
 
1632
+ public handleAuthRequestFromPeer(authRequest: AuthRequest, peerId: PeerId): Promise<StatusMessage> {
1633
+ return this.peerManager.handleAuthRequestFromPeer(authRequest, peerId);
1634
+ }
1635
+
845
1636
  private async sendToPeers<T extends Gossipable>(message: T) {
846
1637
  const parent = message.constructor as typeof Gossipable;
847
1638
 
848
- const identifier = await message.p2pMessageIdentifier().then(i => i.toString());
1639
+ const identifier = await message.p2pMessageLoggingIdentifier().then(i => i.toString());
849
1640
  this.logger.trace(`Sending message ${identifier}`, { p2pMessageIdentifier: identifier });
850
1641
 
851
- const recipientsNum = await this.publishToTopic(parent.p2pTopic, message.toBuffer());
1642
+ const recipientsNum = await this.publishToTopic(this.topicStrings[parent.p2pTopic], message);
852
1643
  this.logger.debug(`Sent message ${identifier} to ${recipientsNum} peers`, {
853
1644
  p2pMessageIdentifier: identifier,
854
1645
  sourcePeer: this.node.peerId.toString(),