@aztec/p2p 0.0.0-test.1 → 0.0.1-commit.001888fc

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