@aztec/p2p 0.0.1-commit.d1f2d6c → 0.0.1-commit.d20b825a7

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 (562) 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 +4 -4
  5. package/dest/client/factory.d.ts +12 -11
  6. package/dest/client/factory.d.ts.map +1 -1
  7. package/dest/client/factory.js +61 -19
  8. package/dest/client/interface.d.ts +54 -34
  9. package/dest/client/interface.d.ts.map +1 -1
  10. package/dest/client/p2p_client.d.ts +43 -52
  11. package/dest/client/p2p_client.d.ts.map +1 -1
  12. package/dest/client/p2p_client.js +190 -226
  13. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.d.ts +2 -0
  14. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.d.ts.map +1 -0
  15. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.js +318 -0
  16. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.d.ts +73 -0
  17. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.d.ts.map +1 -0
  18. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.js +8 -0
  19. package/dest/config.d.ts +135 -82
  20. package/dest/config.d.ts.map +1 -1
  21. package/dest/config.js +114 -41
  22. package/dest/errors/p2p-service.error.d.ts +9 -0
  23. package/dest/errors/p2p-service.error.d.ts.map +1 -0
  24. package/dest/errors/p2p-service.error.js +10 -0
  25. package/dest/errors/tx-pool.error.d.ts +8 -0
  26. package/dest/errors/tx-pool.error.d.ts.map +1 -0
  27. package/dest/errors/tx-pool.error.js +9 -0
  28. package/dest/index.d.ts +2 -2
  29. package/dest/index.d.ts.map +1 -1
  30. package/dest/index.js +1 -1
  31. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +106 -88
  32. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  33. package/dest/mem_pools/attestation_pool/attestation_pool.js +448 -3
  34. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +2 -2
  35. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  36. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +353 -87
  37. package/dest/mem_pools/attestation_pool/index.d.ts +2 -3
  38. package/dest/mem_pools/attestation_pool/index.d.ts.map +1 -1
  39. package/dest/mem_pools/attestation_pool/index.js +1 -2
  40. package/dest/mem_pools/attestation_pool/mocks.d.ts +2 -2
  41. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  42. package/dest/mem_pools/attestation_pool/mocks.js +2 -2
  43. package/dest/mem_pools/index.d.ts +3 -3
  44. package/dest/mem_pools/index.d.ts.map +1 -1
  45. package/dest/mem_pools/index.js +1 -1
  46. package/dest/mem_pools/instrumentation.d.ts +4 -2
  47. package/dest/mem_pools/instrumentation.d.ts.map +1 -1
  48. package/dest/mem_pools/instrumentation.js +18 -16
  49. package/dest/mem_pools/interface.d.ts +5 -5
  50. package/dest/mem_pools/interface.d.ts.map +1 -1
  51. package/dest/mem_pools/tx_pool_v2/archive/index.d.ts +2 -0
  52. package/dest/mem_pools/tx_pool_v2/archive/index.d.ts.map +1 -0
  53. package/dest/mem_pools/tx_pool_v2/archive/index.js +1 -0
  54. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.d.ts +43 -0
  55. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.d.ts.map +1 -0
  56. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.js +103 -0
  57. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts +104 -0
  58. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts.map +1 -0
  59. package/dest/mem_pools/tx_pool_v2/deleted_pool.js +251 -0
  60. package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.d.ts +47 -0
  61. package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.d.ts.map +1 -0
  62. package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.js +128 -0
  63. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts +17 -0
  64. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -0
  65. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.js +94 -0
  66. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts +19 -0
  67. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts.map +1 -0
  68. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.js +97 -0
  69. package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts +11 -0
  70. package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts.map +1 -0
  71. package/dest/mem_pools/tx_pool_v2/eviction/index.js +12 -0
  72. package/dest/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.d.ts +16 -0
  73. package/dest/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.d.ts.map +1 -0
  74. package/dest/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.js +62 -0
  75. package/dest/mem_pools/tx_pool_v2/eviction/interfaces.d.ts +180 -0
  76. package/dest/mem_pools/tx_pool_v2/eviction/interfaces.d.ts.map +1 -0
  77. package/dest/mem_pools/tx_pool_v2/eviction/interfaces.js +25 -0
  78. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.d.ts +15 -0
  79. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.d.ts.map +1 -0
  80. package/dest/mem_pools/{tx_pool → tx_pool_v2}/eviction/invalid_txs_after_mining_rule.js +16 -35
  81. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.d.ts +17 -0
  82. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.d.ts.map +1 -0
  83. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.js +93 -0
  84. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts +16 -0
  85. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts.map +1 -0
  86. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.js +78 -0
  87. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.d.ts +20 -0
  88. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.d.ts.map +1 -0
  89. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.js +75 -0
  90. package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.d.ts +15 -0
  91. package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.d.ts.map +1 -0
  92. package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.js +19 -0
  93. package/dest/mem_pools/tx_pool_v2/index.d.ts +6 -0
  94. package/dest/mem_pools/tx_pool_v2/index.d.ts.map +1 -0
  95. package/dest/mem_pools/tx_pool_v2/index.js +5 -0
  96. package/dest/mem_pools/tx_pool_v2/instrumentation.d.ts +15 -0
  97. package/dest/mem_pools/tx_pool_v2/instrumentation.d.ts.map +1 -0
  98. package/dest/mem_pools/tx_pool_v2/instrumentation.js +43 -0
  99. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +218 -0
  100. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -0
  101. package/dest/mem_pools/tx_pool_v2/interfaces.js +10 -0
  102. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +137 -0
  103. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -0
  104. package/dest/mem_pools/tx_pool_v2/tx_metadata.js +223 -0
  105. package/dest/mem_pools/tx_pool_v2/tx_pool_bench_metrics.d.ts +26 -0
  106. package/dest/mem_pools/tx_pool_v2/tx_pool_bench_metrics.d.ts.map +1 -0
  107. package/dest/mem_pools/tx_pool_v2/tx_pool_bench_metrics.js +70 -0
  108. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts +108 -0
  109. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts.map +1 -0
  110. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.js +337 -0
  111. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +62 -0
  112. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -0
  113. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.js +167 -0
  114. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +78 -0
  115. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -0
  116. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +924 -0
  117. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +5 -2
  118. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
  119. package/dest/msg_validators/attestation_validator/attestation_validator.js +20 -11
  120. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +5 -3
  121. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -1
  122. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.js +9 -4
  123. package/dest/msg_validators/clock_tolerance.d.ts +12 -1
  124. package/dest/msg_validators/clock_tolerance.d.ts.map +1 -1
  125. package/dest/msg_validators/clock_tolerance.js +54 -3
  126. package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts +7 -4
  127. package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts.map +1 -1
  128. package/dest/msg_validators/proposal_validator/block_proposal_validator.js +10 -2
  129. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts +7 -4
  130. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts.map +1 -1
  131. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.js +16 -2
  132. package/dest/msg_validators/proposal_validator/proposal_validator.d.ts +15 -8
  133. package/dest/msg_validators/proposal_validator/proposal_validator.d.ts.map +1 -1
  134. package/dest/msg_validators/proposal_validator/proposal_validator.js +70 -50
  135. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +4 -4
  136. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
  137. package/dest/msg_validators/tx_validator/aggregate_tx_validator.js +3 -3
  138. package/dest/msg_validators/tx_validator/allowed_public_setup.d.ts +2 -1
  139. package/dest/msg_validators/tx_validator/allowed_public_setup.d.ts.map +1 -1
  140. package/dest/msg_validators/tx_validator/allowed_public_setup.js +24 -20
  141. package/dest/msg_validators/tx_validator/allowed_setup_helpers.d.ts +17 -0
  142. package/dest/msg_validators/tx_validator/allowed_setup_helpers.d.ts.map +1 -0
  143. package/dest/msg_validators/tx_validator/allowed_setup_helpers.js +24 -0
  144. package/dest/msg_validators/tx_validator/archive_cache.d.ts +3 -3
  145. package/dest/msg_validators/tx_validator/archive_cache.d.ts.map +1 -1
  146. package/dest/msg_validators/tx_validator/block_header_validator.d.ts +20 -6
  147. package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
  148. package/dest/msg_validators/tx_validator/block_header_validator.js +4 -3
  149. package/dest/msg_validators/tx_validator/contract_instance_validator.d.ts +9 -0
  150. package/dest/msg_validators/tx_validator/contract_instance_validator.d.ts.map +1 -0
  151. package/dest/msg_validators/tx_validator/contract_instance_validator.js +48 -0
  152. package/dest/msg_validators/tx_validator/data_validator.d.ts +3 -1
  153. package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
  154. package/dest/msg_validators/tx_validator/data_validator.js +39 -3
  155. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts +15 -4
  156. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts.map +1 -1
  157. package/dest/msg_validators/tx_validator/double_spend_validator.js +7 -6
  158. package/dest/msg_validators/tx_validator/factory.d.ts +137 -5
  159. package/dest/msg_validators/tx_validator/factory.d.ts.map +1 -1
  160. package/dest/msg_validators/tx_validator/factory.js +255 -58
  161. package/dest/msg_validators/tx_validator/fee_payer_balance.d.ts +1 -1
  162. package/dest/msg_validators/tx_validator/fee_payer_balance.d.ts.map +1 -1
  163. package/dest/msg_validators/tx_validator/fee_payer_balance.js +6 -2
  164. package/dest/msg_validators/tx_validator/gas_validator.d.ts +100 -3
  165. package/dest/msg_validators/tx_validator/gas_validator.d.ts.map +1 -1
  166. package/dest/msg_validators/tx_validator/gas_validator.js +138 -53
  167. package/dest/msg_validators/tx_validator/index.d.ts +3 -1
  168. package/dest/msg_validators/tx_validator/index.d.ts.map +1 -1
  169. package/dest/msg_validators/tx_validator/index.js +2 -0
  170. package/dest/msg_validators/tx_validator/metadata_validator.d.ts +3 -2
  171. package/dest/msg_validators/tx_validator/metadata_validator.d.ts.map +1 -1
  172. package/dest/msg_validators/tx_validator/metadata_validator.js +6 -6
  173. package/dest/msg_validators/tx_validator/nullifier_cache.d.ts +14 -0
  174. package/dest/msg_validators/tx_validator/nullifier_cache.d.ts.map +1 -0
  175. package/dest/msg_validators/tx_validator/nullifier_cache.js +24 -0
  176. package/dest/msg_validators/tx_validator/phases_validator.d.ts +24 -3
  177. package/dest/msg_validators/tx_validator/phases_validator.d.ts.map +1 -1
  178. package/dest/msg_validators/tx_validator/phases_validator.js +75 -27
  179. package/dest/msg_validators/tx_validator/size_validator.d.ts +3 -1
  180. package/dest/msg_validators/tx_validator/size_validator.d.ts.map +1 -1
  181. package/dest/msg_validators/tx_validator/size_validator.js +4 -1
  182. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +22 -5
  183. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -1
  184. package/dest/msg_validators/tx_validator/timestamp_validator.js +8 -8
  185. package/dest/msg_validators/tx_validator/tx_permitted_validator.d.ts +3 -2
  186. package/dest/msg_validators/tx_validator/tx_permitted_validator.d.ts.map +1 -1
  187. package/dest/msg_validators/tx_validator/tx_permitted_validator.js +2 -2
  188. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts +3 -2
  189. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts.map +1 -1
  190. package/dest/msg_validators/tx_validator/tx_proof_validator.js +2 -2
  191. package/dest/services/data_store.d.ts +1 -1
  192. package/dest/services/data_store.d.ts.map +1 -1
  193. package/dest/services/data_store.js +14 -10
  194. package/dest/services/discv5/discV5_service.js +1 -1
  195. package/dest/services/dummy_service.d.ts +28 -5
  196. package/dest/services/dummy_service.d.ts.map +1 -1
  197. package/dest/services/dummy_service.js +52 -2
  198. package/dest/services/encoding.d.ts +7 -3
  199. package/dest/services/encoding.d.ts.map +1 -1
  200. package/dest/services/encoding.js +18 -11
  201. package/dest/services/gossipsub/index.d.ts +3 -0
  202. package/dest/services/gossipsub/index.d.ts.map +1 -0
  203. package/dest/services/gossipsub/index.js +2 -0
  204. package/dest/services/gossipsub/scoring.d.ts +21 -3
  205. package/dest/services/gossipsub/scoring.d.ts.map +1 -1
  206. package/dest/services/gossipsub/scoring.js +24 -7
  207. package/dest/services/gossipsub/topic_score_params.d.ts +184 -0
  208. package/dest/services/gossipsub/topic_score_params.d.ts.map +1 -0
  209. package/dest/services/gossipsub/topic_score_params.js +363 -0
  210. package/dest/services/index.d.ts +2 -1
  211. package/dest/services/index.d.ts.map +1 -1
  212. package/dest/services/index.js +1 -0
  213. package/dest/services/libp2p/instrumentation.d.ts +3 -1
  214. package/dest/services/libp2p/instrumentation.d.ts.map +1 -1
  215. package/dest/services/libp2p/instrumentation.js +28 -3
  216. package/dest/services/libp2p/libp2p_service.d.ts +101 -51
  217. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  218. package/dest/services/libp2p/libp2p_service.js +596 -463
  219. package/dest/services/peer-manager/metrics.d.ts +4 -2
  220. package/dest/services/peer-manager/metrics.d.ts.map +1 -1
  221. package/dest/services/peer-manager/metrics.js +26 -5
  222. package/dest/services/peer-manager/peer_manager.d.ts +6 -2
  223. package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
  224. package/dest/services/peer-manager/peer_manager.js +39 -11
  225. package/dest/services/peer-manager/peer_scoring.d.ts +7 -2
  226. package/dest/services/peer-manager/peer_scoring.d.ts.map +1 -1
  227. package/dest/services/peer-manager/peer_scoring.js +65 -14
  228. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts +51 -0
  229. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts.map +1 -0
  230. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.js +543 -0
  231. package/dest/services/reqresp/batch-tx-requester/config.d.ts +17 -0
  232. package/dest/services/reqresp/batch-tx-requester/config.d.ts.map +1 -0
  233. package/dest/services/reqresp/batch-tx-requester/config.js +27 -0
  234. package/dest/services/reqresp/batch-tx-requester/interface.d.ts +47 -0
  235. package/dest/services/reqresp/batch-tx-requester/interface.d.ts.map +1 -0
  236. package/dest/services/reqresp/batch-tx-requester/interface.js +1 -0
  237. package/dest/services/reqresp/batch-tx-requester/missing_txs.d.ts +35 -0
  238. package/dest/services/reqresp/batch-tx-requester/missing_txs.d.ts.map +1 -0
  239. package/dest/services/reqresp/batch-tx-requester/missing_txs.js +136 -0
  240. package/dest/services/reqresp/batch-tx-requester/peer_collection.d.ts +62 -0
  241. package/dest/services/reqresp/batch-tx-requester/peer_collection.d.ts.map +1 -0
  242. package/dest/services/reqresp/batch-tx-requester/peer_collection.js +176 -0
  243. package/dest/services/reqresp/batch-tx-requester/tx_validator.d.ts +20 -0
  244. package/dest/services/reqresp/batch-tx-requester/tx_validator.d.ts.map +1 -0
  245. package/dest/services/reqresp/batch-tx-requester/tx_validator.js +21 -0
  246. package/dest/services/reqresp/config.d.ts +3 -3
  247. package/dest/services/reqresp/config.d.ts.map +1 -1
  248. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts +22 -3
  249. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts.map +1 -1
  250. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.js +63 -4
  251. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts +2 -1
  252. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts.map +1 -1
  253. package/dest/services/reqresp/connection-sampler/connection_sampler.js +12 -0
  254. package/dest/services/reqresp/interface.d.ts +25 -9
  255. package/dest/services/reqresp/interface.d.ts.map +1 -1
  256. package/dest/services/reqresp/interface.js +23 -10
  257. package/dest/services/reqresp/metrics.d.ts +6 -5
  258. package/dest/services/reqresp/metrics.d.ts.map +1 -1
  259. package/dest/services/reqresp/metrics.js +16 -5
  260. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts +5 -1
  261. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts.map +1 -1
  262. package/dest/services/reqresp/protocols/block_txs/bitvector.js +5 -0
  263. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +7 -5
  264. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -1
  265. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.js +27 -9
  266. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +29 -6
  267. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
  268. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +59 -13
  269. package/dest/services/reqresp/protocols/index.d.ts +1 -2
  270. package/dest/services/reqresp/protocols/index.d.ts.map +1 -1
  271. package/dest/services/reqresp/protocols/index.js +0 -1
  272. package/dest/services/reqresp/protocols/tx.d.ts +7 -1
  273. package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
  274. package/dest/services/reqresp/protocols/tx.js +21 -3
  275. package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts +5 -4
  276. package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts.map +1 -1
  277. package/dest/services/reqresp/rate-limiter/rate_limiter.js +10 -8
  278. package/dest/services/reqresp/rate-limiter/rate_limits.d.ts +1 -1
  279. package/dest/services/reqresp/rate-limiter/rate_limits.d.ts.map +1 -1
  280. package/dest/services/reqresp/rate-limiter/rate_limits.js +0 -10
  281. package/dest/services/reqresp/reqresp.d.ts +9 -2
  282. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  283. package/dest/services/reqresp/reqresp.js +95 -34
  284. package/dest/services/service.d.ts +46 -4
  285. package/dest/services/service.d.ts.map +1 -1
  286. package/dest/services/tx_collection/config.d.ts +22 -1
  287. package/dest/services/tx_collection/config.d.ts.map +1 -1
  288. package/dest/services/tx_collection/config.js +55 -1
  289. package/dest/services/tx_collection/fast_tx_collection.d.ts +7 -7
  290. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
  291. package/dest/services/tx_collection/fast_tx_collection.js +95 -84
  292. package/dest/services/tx_collection/file_store_tx_collection.d.ts +53 -0
  293. package/dest/services/tx_collection/file_store_tx_collection.d.ts.map +1 -0
  294. package/dest/services/tx_collection/file_store_tx_collection.js +167 -0
  295. package/dest/services/tx_collection/file_store_tx_source.d.ts +38 -0
  296. package/dest/services/tx_collection/file_store_tx_source.d.ts.map +1 -0
  297. package/dest/services/tx_collection/file_store_tx_source.js +100 -0
  298. package/dest/services/tx_collection/index.d.ts +3 -1
  299. package/dest/services/tx_collection/index.d.ts.map +1 -1
  300. package/dest/services/tx_collection/index.js +2 -0
  301. package/dest/services/tx_collection/instrumentation.d.ts +1 -1
  302. package/dest/services/tx_collection/instrumentation.d.ts.map +1 -1
  303. package/dest/services/tx_collection/instrumentation.js +10 -2
  304. package/dest/services/tx_collection/proposal_tx_collector.d.ts +48 -0
  305. package/dest/services/tx_collection/proposal_tx_collector.d.ts.map +1 -0
  306. package/dest/services/tx_collection/proposal_tx_collector.js +50 -0
  307. package/dest/services/tx_collection/request_tracker.d.ts +53 -0
  308. package/dest/services/tx_collection/request_tracker.d.ts.map +1 -0
  309. package/dest/services/tx_collection/request_tracker.js +84 -0
  310. package/dest/services/tx_collection/slow_tx_collection.d.ts +7 -3
  311. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -1
  312. package/dest/services/tx_collection/slow_tx_collection.js +60 -26
  313. package/dest/services/tx_collection/tx_collection.d.ts +25 -15
  314. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
  315. package/dest/services/tx_collection/tx_collection.js +79 -7
  316. package/dest/services/tx_collection/tx_collection_sink.d.ts +18 -8
  317. package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -1
  318. package/dest/services/tx_collection/tx_collection_sink.js +26 -29
  319. package/dest/services/tx_collection/tx_source.d.ts +13 -7
  320. package/dest/services/tx_collection/tx_source.d.ts.map +1 -1
  321. package/dest/services/tx_collection/tx_source.js +26 -7
  322. package/dest/services/tx_file_store/config.d.ts +16 -0
  323. package/dest/services/tx_file_store/config.d.ts.map +1 -0
  324. package/dest/services/tx_file_store/config.js +22 -0
  325. package/dest/services/tx_file_store/index.d.ts +4 -0
  326. package/dest/services/tx_file_store/index.d.ts.map +1 -0
  327. package/dest/services/tx_file_store/index.js +3 -0
  328. package/dest/services/tx_file_store/instrumentation.d.ts +15 -0
  329. package/dest/services/tx_file_store/instrumentation.d.ts.map +1 -0
  330. package/dest/services/tx_file_store/instrumentation.js +29 -0
  331. package/dest/services/tx_file_store/tx_file_store.d.ts +48 -0
  332. package/dest/services/tx_file_store/tx_file_store.d.ts.map +1 -0
  333. package/dest/services/tx_file_store/tx_file_store.js +152 -0
  334. package/dest/services/tx_provider.d.ts +4 -4
  335. package/dest/services/tx_provider.d.ts.map +1 -1
  336. package/dest/services/tx_provider.js +9 -8
  337. package/dest/services/tx_provider_instrumentation.d.ts +1 -1
  338. package/dest/services/tx_provider_instrumentation.d.ts.map +1 -1
  339. package/dest/services/tx_provider_instrumentation.js +5 -5
  340. package/dest/test-helpers/index.d.ts +3 -1
  341. package/dest/test-helpers/index.d.ts.map +1 -1
  342. package/dest/test-helpers/index.js +2 -0
  343. package/dest/test-helpers/make-test-p2p-clients.d.ts +7 -8
  344. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  345. package/dest/test-helpers/make-test-p2p-clients.js +4 -2
  346. package/dest/test-helpers/mock-pubsub.d.ts +40 -6
  347. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  348. package/dest/test-helpers/mock-pubsub.js +139 -13
  349. package/dest/test-helpers/reqresp-nodes.d.ts +2 -3
  350. package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
  351. package/dest/test-helpers/reqresp-nodes.js +8 -5
  352. package/dest/test-helpers/test_tx_provider.d.ts +40 -0
  353. package/dest/test-helpers/test_tx_provider.d.ts.map +1 -0
  354. package/dest/test-helpers/test_tx_provider.js +41 -0
  355. package/dest/test-helpers/testbench-utils.d.ts +163 -0
  356. package/dest/test-helpers/testbench-utils.d.ts.map +1 -0
  357. package/dest/test-helpers/testbench-utils.js +386 -0
  358. package/dest/testbench/p2p_client_testbench_worker.d.ts +28 -2
  359. package/dest/testbench/p2p_client_testbench_worker.d.ts.map +1 -1
  360. package/dest/testbench/p2p_client_testbench_worker.js +281 -143
  361. package/dest/testbench/worker_client_manager.d.ts +60 -6
  362. package/dest/testbench/worker_client_manager.d.ts.map +1 -1
  363. package/dest/testbench/worker_client_manager.js +280 -46
  364. package/dest/util.d.ts +3 -3
  365. package/dest/util.d.ts.map +1 -1
  366. package/package.json +14 -14
  367. package/src/bootstrap/bootstrap.ts +7 -4
  368. package/src/client/factory.ts +119 -38
  369. package/src/client/interface.ts +65 -35
  370. package/src/client/p2p_client.ts +228 -272
  371. package/src/client/test/tx_proposal_collector/README.md +227 -0
  372. package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker.ts +357 -0
  373. package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.ts +43 -0
  374. package/src/config.ts +184 -46
  375. package/src/errors/p2p-service.error.ts +11 -0
  376. package/src/errors/tx-pool.error.ts +12 -0
  377. package/src/index.ts +1 -1
  378. package/src/mem_pools/attestation_pool/attestation_pool.ts +501 -91
  379. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +442 -102
  380. package/src/mem_pools/attestation_pool/index.ts +9 -2
  381. package/src/mem_pools/attestation_pool/mocks.ts +2 -1
  382. package/src/mem_pools/index.ts +2 -2
  383. package/src/mem_pools/instrumentation.ts +19 -14
  384. package/src/mem_pools/interface.ts +4 -4
  385. package/src/mem_pools/tx_pool_v2/README.md +283 -0
  386. package/src/mem_pools/tx_pool_v2/archive/index.ts +1 -0
  387. package/src/mem_pools/tx_pool_v2/archive/tx_archive.ts +120 -0
  388. package/src/mem_pools/tx_pool_v2/deleted_pool.ts +321 -0
  389. package/src/mem_pools/tx_pool_v2/eviction/eviction_manager.ts +160 -0
  390. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.ts +122 -0
  391. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.ts +125 -0
  392. package/src/mem_pools/tx_pool_v2/eviction/index.ts +28 -0
  393. package/src/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.ts +65 -0
  394. package/src/mem_pools/tx_pool_v2/eviction/interfaces.ts +219 -0
  395. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.ts +74 -0
  396. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.ts +101 -0
  397. package/src/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.ts +91 -0
  398. package/src/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.ts +99 -0
  399. package/src/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.ts +32 -0
  400. package/src/mem_pools/tx_pool_v2/index.ts +12 -0
  401. package/src/mem_pools/tx_pool_v2/instrumentation.ts +69 -0
  402. package/src/mem_pools/tx_pool_v2/interfaces.ts +250 -0
  403. package/src/mem_pools/tx_pool_v2/tx_metadata.ts +349 -0
  404. package/src/mem_pools/tx_pool_v2/tx_pool_bench_metrics.ts +77 -0
  405. package/src/mem_pools/tx_pool_v2/tx_pool_indices.ts +430 -0
  406. package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +238 -0
  407. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +1107 -0
  408. package/src/msg_validators/attestation_validator/README.md +49 -0
  409. package/src/msg_validators/attestation_validator/attestation_validator.ts +21 -9
  410. package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +14 -5
  411. package/src/msg_validators/clock_tolerance.ts +72 -3
  412. package/src/msg_validators/proposal_validator/README.md +123 -0
  413. package/src/msg_validators/proposal_validator/block_proposal_validator.ts +17 -4
  414. package/src/msg_validators/proposal_validator/checkpoint_proposal_validator.ts +23 -7
  415. package/src/msg_validators/proposal_validator/proposal_validator.ts +81 -51
  416. package/src/msg_validators/tx_validator/README.md +127 -0
  417. package/src/msg_validators/tx_validator/aggregate_tx_validator.ts +5 -5
  418. package/src/msg_validators/tx_validator/allowed_public_setup.ts +22 -27
  419. package/src/msg_validators/tx_validator/allowed_setup_helpers.ts +31 -0
  420. package/src/msg_validators/tx_validator/archive_cache.ts +2 -2
  421. package/src/msg_validators/tx_validator/block_header_validator.ts +21 -8
  422. package/src/msg_validators/tx_validator/contract_instance_validator.ts +56 -0
  423. package/src/msg_validators/tx_validator/data_validator.ts +48 -3
  424. package/src/msg_validators/tx_validator/double_spend_validator.ts +15 -9
  425. package/src/msg_validators/tx_validator/factory.ts +415 -56
  426. package/src/msg_validators/tx_validator/fee_payer_balance.ts +6 -2
  427. package/src/msg_validators/tx_validator/gas_validator.ts +203 -52
  428. package/src/msg_validators/tx_validator/index.ts +2 -0
  429. package/src/msg_validators/tx_validator/metadata_validator.ts +18 -7
  430. package/src/msg_validators/tx_validator/nullifier_cache.ts +30 -0
  431. package/src/msg_validators/tx_validator/phases_validator.ts +87 -30
  432. package/src/msg_validators/tx_validator/size_validator.ts +6 -2
  433. package/src/msg_validators/tx_validator/timestamp_validator.ts +29 -21
  434. package/src/msg_validators/tx_validator/tx_permitted_validator.ts +8 -3
  435. package/src/msg_validators/tx_validator/tx_proof_validator.ts +8 -3
  436. package/src/services/data_store.ts +14 -19
  437. package/src/services/discv5/discV5_service.ts +1 -1
  438. package/src/services/dummy_service.ts +66 -3
  439. package/src/services/encoding.ts +18 -10
  440. package/src/services/gossipsub/README.md +641 -0
  441. package/src/services/gossipsub/index.ts +2 -0
  442. package/src/services/gossipsub/scoring.ts +29 -5
  443. package/src/services/gossipsub/topic_score_params.ts +519 -0
  444. package/src/services/index.ts +1 -0
  445. package/src/services/libp2p/instrumentation.ts +29 -2
  446. package/src/services/libp2p/libp2p_service.ts +644 -514
  447. package/src/services/peer-manager/metrics.ts +28 -4
  448. package/src/services/peer-manager/peer_manager.ts +45 -11
  449. package/src/services/peer-manager/peer_scoring.ts +56 -6
  450. package/src/services/reqresp/README.md +229 -0
  451. package/src/services/reqresp/batch-tx-requester/README.md +344 -0
  452. package/src/services/reqresp/batch-tx-requester/batch_tx_requester.ts +673 -0
  453. package/src/services/reqresp/batch-tx-requester/config.ts +40 -0
  454. package/src/services/reqresp/batch-tx-requester/interface.ts +54 -0
  455. package/src/services/reqresp/batch-tx-requester/missing_txs.ts +168 -0
  456. package/src/services/reqresp/batch-tx-requester/peer_collection.ts +249 -0
  457. package/src/services/reqresp/batch-tx-requester/tx_validator.ts +37 -0
  458. package/src/services/reqresp/config.ts +2 -2
  459. package/src/services/reqresp/connection-sampler/batch_connection_sampler.ts +65 -4
  460. package/src/services/reqresp/connection-sampler/connection_sampler.ts +16 -0
  461. package/src/services/reqresp/interface.ts +48 -10
  462. package/src/services/reqresp/metrics.ts +33 -9
  463. package/src/services/reqresp/protocols/block_txs/bitvector.ts +7 -0
  464. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +35 -12
  465. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +74 -9
  466. package/src/services/reqresp/protocols/index.ts +0 -1
  467. package/src/services/reqresp/protocols/tx.ts +23 -3
  468. package/src/services/reqresp/rate-limiter/rate_limiter.ts +13 -9
  469. package/src/services/reqresp/rate-limiter/rate_limits.ts +0 -10
  470. package/src/services/reqresp/reqresp.ts +116 -32
  471. package/src/services/service.ts +61 -3
  472. package/src/services/tx_collection/config.ts +83 -1
  473. package/src/services/tx_collection/fast_tx_collection.ts +111 -91
  474. package/src/services/tx_collection/file_store_tx_collection.ts +202 -0
  475. package/src/services/tx_collection/file_store_tx_source.ts +129 -0
  476. package/src/services/tx_collection/index.ts +6 -0
  477. package/src/services/tx_collection/instrumentation.ts +17 -2
  478. package/src/services/tx_collection/proposal_tx_collector.ts +108 -0
  479. package/src/services/tx_collection/request_tracker.ts +127 -0
  480. package/src/services/tx_collection/slow_tx_collection.ts +66 -33
  481. package/src/services/tx_collection/tx_collection.ts +118 -23
  482. package/src/services/tx_collection/tx_collection_sink.ts +30 -34
  483. package/src/services/tx_collection/tx_source.ts +28 -8
  484. package/src/services/tx_file_store/config.ts +37 -0
  485. package/src/services/tx_file_store/index.ts +3 -0
  486. package/src/services/tx_file_store/instrumentation.ts +36 -0
  487. package/src/services/tx_file_store/tx_file_store.ts +175 -0
  488. package/src/services/tx_provider.ts +10 -9
  489. package/src/services/tx_provider_instrumentation.ts +11 -5
  490. package/src/test-helpers/index.ts +2 -0
  491. package/src/test-helpers/make-test-p2p-clients.ts +6 -6
  492. package/src/test-helpers/mock-pubsub.ts +180 -14
  493. package/src/test-helpers/reqresp-nodes.ts +9 -9
  494. package/src/test-helpers/test_tx_provider.ts +64 -0
  495. package/src/test-helpers/testbench-utils.ts +457 -0
  496. package/src/testbench/p2p_client_testbench_worker.ts +396 -134
  497. package/src/testbench/worker_client_manager.ts +367 -48
  498. package/src/util.ts +8 -2
  499. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +0 -40
  500. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +0 -1
  501. package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +0 -218
  502. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +0 -31
  503. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +0 -1
  504. package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +0 -180
  505. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts +0 -125
  506. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts.map +0 -1
  507. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.js +0 -596
  508. package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts +0 -32
  509. package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts.map +0 -1
  510. package/dest/mem_pools/tx_pool/eviction/eviction_manager.js +0 -112
  511. package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts +0 -157
  512. package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts.map +0 -1
  513. package/dest/mem_pools/tx_pool/eviction/eviction_strategy.js +0 -52
  514. package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts +0 -16
  515. package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts.map +0 -1
  516. package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.js +0 -122
  517. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.d.ts +0 -17
  518. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.d.ts.map +0 -1
  519. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.d.ts +0 -19
  520. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.d.ts.map +0 -1
  521. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.js +0 -78
  522. package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.d.ts +0 -26
  523. package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.d.ts.map +0 -1
  524. package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.js +0 -84
  525. package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.d.ts +0 -25
  526. package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.d.ts.map +0 -1
  527. package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.js +0 -57
  528. package/dest/mem_pools/tx_pool/index.d.ts +0 -3
  529. package/dest/mem_pools/tx_pool/index.d.ts.map +0 -1
  530. package/dest/mem_pools/tx_pool/index.js +0 -2
  531. package/dest/mem_pools/tx_pool/priority.d.ts +0 -12
  532. package/dest/mem_pools/tx_pool/priority.d.ts.map +0 -1
  533. package/dest/mem_pools/tx_pool/priority.js +0 -15
  534. package/dest/mem_pools/tx_pool/tx_pool.d.ts +0 -127
  535. package/dest/mem_pools/tx_pool/tx_pool.d.ts.map +0 -1
  536. package/dest/mem_pools/tx_pool/tx_pool.js +0 -3
  537. package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts +0 -7
  538. package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts.map +0 -1
  539. package/dest/mem_pools/tx_pool/tx_pool_test_suite.js +0 -400
  540. package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts +0 -23
  541. package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts.map +0 -1
  542. package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.js +0 -212
  543. package/dest/services/reqresp/protocols/block.d.ts +0 -9
  544. package/dest/services/reqresp/protocols/block.d.ts.map +0 -1
  545. package/dest/services/reqresp/protocols/block.js +0 -32
  546. package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +0 -320
  547. package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +0 -264
  548. package/src/mem_pools/tx_pool/README.md +0 -270
  549. package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +0 -746
  550. package/src/mem_pools/tx_pool/eviction/eviction_manager.ts +0 -132
  551. package/src/mem_pools/tx_pool/eviction/eviction_strategy.ts +0 -208
  552. package/src/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.ts +0 -162
  553. package/src/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.ts +0 -104
  554. package/src/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.ts +0 -93
  555. package/src/mem_pools/tx_pool/eviction/low_priority_eviction_rule.ts +0 -106
  556. package/src/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.ts +0 -75
  557. package/src/mem_pools/tx_pool/index.ts +0 -2
  558. package/src/mem_pools/tx_pool/priority.ts +0 -20
  559. package/src/mem_pools/tx_pool/tx_pool.ts +0 -141
  560. package/src/mem_pools/tx_pool/tx_pool_test_suite.ts +0 -319
  561. package/src/msg_validators/proposal_validator/proposal_validator_test_suite.ts +0 -230
  562. package/src/services/reqresp/protocols/block.ts +0 -37
@@ -1,16 +1,14 @@
1
1
  import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
- import { BlockNumber } from '@aztec/foundation/branded-types';
3
- import { randomInt } from '@aztec/foundation/crypto/random';
4
- import { Fr } from '@aztec/foundation/curves/bn254';
2
+ import { BlockNumber, type SlotNumber } from '@aztec/foundation/branded-types';
3
+ import { maxBy } from '@aztec/foundation/collection';
5
4
  import { type Logger, createLibp2pComponentLogger, createLogger } from '@aztec/foundation/log';
6
5
  import { RunningPromise } from '@aztec/foundation/running-promise';
7
6
  import { Timer } from '@aztec/foundation/timer';
8
7
  import type { AztecAsyncKVStore } from '@aztec/kv-store';
9
- import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
10
8
  import { protocolContractsHash } from '@aztec/protocol-contracts';
11
- import type { EthAddress, L2Block, L2BlockSource } from '@aztec/stdlib/block';
9
+ import type { EthAddress, L2BlockSource } from '@aztec/stdlib/block';
12
10
  import type { ContractDataSource } from '@aztec/stdlib/contract';
13
- import { GasFees } from '@aztec/stdlib/gas';
11
+ import { type BlockMinFeesProvider, GasFees } from '@aztec/stdlib/gas';
14
12
  import type { ClientProtocolCircuitVerifier, PeerInfo, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
15
13
  import {
16
14
  BlockProposal,
@@ -18,13 +16,12 @@ import {
18
16
  CheckpointProposal,
19
17
  type CheckpointProposalCore,
20
18
  type Gossipable,
21
- P2PClientType,
22
19
  P2PMessage,
23
- type ValidationResult as P2PValidationResult,
24
20
  PeerErrorSeverity,
21
+ PeerErrorSeverityByHarshness,
25
22
  TopicType,
26
23
  createTopicString,
27
- getTopicsForClientAndConfig,
24
+ getTopicsForConfig,
28
25
  metricsTopicStrToLabels,
29
26
  } from '@aztec/stdlib/p2p';
30
27
  import { MerkleTreeId } from '@aztec/stdlib/trees';
@@ -46,7 +43,7 @@ import {
46
43
  type GossipsubMessage,
47
44
  gossipsub,
48
45
  } from '@chainsafe/libp2p-gossipsub';
49
- import { createPeerScoreParams, createTopicScoreParams } from '@chainsafe/libp2p-gossipsub/score';
46
+ import { createPeerScoreParams } from '@chainsafe/libp2p-gossipsub/score';
50
47
  import { SignaturePolicy } from '@chainsafe/libp2p-gossipsub/types';
51
48
  import { noise } from '@chainsafe/libp2p-noise';
52
49
  import { yamux } from '@chainsafe/libp2p-yamux';
@@ -60,62 +57,62 @@ import { ENR } from '@nethermindeth/enr';
60
57
  import { createLibp2p } from 'libp2p';
61
58
 
62
59
  import type { P2PConfig } from '../../config.js';
63
- import { ProposalSlotCapExceededError } from '../../errors/attestation-pool.error.js';
60
+ import { CheckpointProposalReceivedCallbackNotRegisteredError } from '../../errors/p2p-service.error.js';
64
61
  import type { MemPools } from '../../mem_pools/interface.js';
65
62
  import {
66
63
  BlockProposalValidator,
67
64
  CheckpointAttestationValidator,
68
65
  CheckpointProposalValidator,
66
+ DoubleSpendTxValidator,
69
67
  FishermanAttestationValidator,
70
- SizeTxValidator,
68
+ getDefaultAllowedSetupFunctions,
71
69
  } from '../../msg_validators/index.js';
72
70
  import { MessageSeenValidator } from '../../msg_validators/msg_seen_validator/msg_seen_validator.js';
73
- import { getDefaultAllowedSetupFunctions } from '../../msg_validators/tx_validator/allowed_public_setup.js';
74
- import { type MessageValidator, createTxMessageValidators } from '../../msg_validators/tx_validator/factory.js';
75
71
  import {
76
- AggregateTxValidator,
77
- DataTxValidator,
78
- DoubleSpendTxValidator,
79
- MetadataTxValidator,
80
- TxProofValidator,
81
- } from '../../msg_validators/tx_validator/index.js';
72
+ type TransactionValidator,
73
+ createFirstStageTxValidationsForGossipedTransactions,
74
+ createSecondStageTxValidationsForGossipedTransactions,
75
+ createTxValidatorForBlockProposalReceivedTxs,
76
+ createTxValidatorForReqResponseReceivedTxs,
77
+ } from '../../msg_validators/tx_validator/factory.js';
82
78
  import { GossipSubEvent } from '../../types/index.js';
83
79
  import { type PubSubLibp2p, convertToMultiaddr } from '../../util.js';
84
80
  import { getVersions } from '../../versioning.js';
85
81
  import { AztecDatastore } from '../data_store.js';
86
82
  import { DiscV5Service } from '../discv5/discV5_service.js';
87
83
  import { SnappyTransform, fastMsgIdFn, getMsgIdFn, msgIdToStrFn } from '../encoding.js';
88
- import { gossipScoreThresholds } from '../gossipsub/scoring.js';
84
+ import { APP_SPECIFIC_WEIGHT, gossipScoreThresholds } from '../gossipsub/scoring.js';
85
+ import { createAllTopicScoreParams } from '../gossipsub/topic_score_params.js';
89
86
  import type { PeerManagerInterface } from '../peer-manager/interface.js';
90
87
  import { PeerManager } from '../peer-manager/peer_manager.js';
91
88
  import { PeerScoring } from '../peer-manager/peer_scoring.js';
89
+ import type { BatchTxRequesterLibP2PService } from '../reqresp/batch-tx-requester/interface.js';
92
90
  import type { P2PReqRespConfig } from '../reqresp/config.js';
93
91
  import {
92
+ AuthRequest,
93
+ BlockTxsRequest,
94
+ BlockTxsResponse,
94
95
  DEFAULT_SUB_PROTOCOL_VALIDATORS,
95
96
  type ReqRespInterface,
97
+ type ReqRespResponse,
96
98
  ReqRespSubProtocol,
97
99
  type ReqRespSubProtocolHandler,
98
100
  type ReqRespSubProtocolHandlers,
99
101
  type ReqRespSubProtocolValidators,
102
+ StatusMessage,
100
103
  type SubProtocolMap,
101
104
  ValidationError,
102
- } from '../reqresp/interface.js';
103
- import { reqRespBlockTxsHandler } from '../reqresp/protocols/block_txs/block_txs_handler.js';
104
- import { reqGoodbyeHandler } from '../reqresp/protocols/goodbye.js';
105
- import {
106
- AuthRequest,
107
- BlockTxsRequest,
108
- BlockTxsResponse,
109
- StatusMessage,
110
105
  pingHandler,
111
- reqRespBlockHandler,
106
+ reqGoodbyeHandler,
107
+ reqRespBlockTxsHandler,
112
108
  reqRespStatusHandler,
113
109
  reqRespTxHandler,
114
- } from '../reqresp/protocols/index.js';
110
+ } from '../reqresp/index.js';
115
111
  import { ReqResp } from '../reqresp/reqresp.js';
116
112
  import type {
117
113
  P2PBlockReceivedCallback,
118
114
  P2PCheckpointReceivedCallback,
115
+ P2PDuplicateAttestationCallback,
119
116
  P2PService,
120
117
  PeerDiscoveryService,
121
118
  } from '../service.js';
@@ -130,14 +127,14 @@ interface ValidationResult {
130
127
  type ValidationOutcome = { allPassed: true } | { allPassed: false; failure: ValidationResult };
131
128
 
132
129
  // REFACTOR: Unify with the type above
133
- type ReceivedMessageValidationResult<T> =
134
- | { obj: T; result: Exclude<TopicValidatorResult, TopicValidatorResult.Reject> }
135
- | { obj?: undefined; result: TopicValidatorResult.Reject };
130
+ type ReceivedMessageValidationResult<T, M = undefined> =
131
+ | { obj: T; result: Exclude<TopicValidatorResult, TopicValidatorResult.Reject>; metadata?: M }
132
+ | { obj?: T; result: TopicValidatorResult.Reject; metadata?: M; severity: PeerErrorSeverity };
136
133
 
137
134
  /**
138
135
  * Lib P2P implementation of the P2PService interface.
139
136
  */
140
- export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends WithTracer implements P2PService {
137
+ export class LibP2PService extends WithTracer implements P2PService {
141
138
  private discoveryRunningPromise?: RunningPromise;
142
139
  private msgIdSeenValidators: Record<TopicType, MessageSeenValidator> = {} as Record<TopicType, MessageSeenValidator>;
143
140
 
@@ -149,7 +146,15 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
149
146
  private protocolVersion = '';
150
147
  private topicStrings: Record<TopicType, string> = {} as Record<TopicType, string>;
151
148
 
152
- private feesCache: { blockNumber: BlockNumber; gasFees: GasFees } | undefined;
149
+ /** Callback invoked when a duplicate proposal is detected (triggers slashing). */
150
+ private duplicateProposalCallback?: (info: {
151
+ slot: SlotNumber;
152
+ proposer: EthAddress;
153
+ type: 'checkpoint' | 'block';
154
+ }) => void;
155
+
156
+ /** Callback invoked when a duplicate attestation is detected (triggers slashing). */
157
+ private duplicateAttestationCallback?: P2PDuplicateAttestationCallback;
153
158
 
154
159
  /**
155
160
  * Callback for when a block is received from a peer.
@@ -163,7 +168,13 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
163
168
  * @param checkpoint - The checkpoint proposal received from the peer.
164
169
  * @returns The attestations for the checkpoint, if any.
165
170
  */
166
- private checkpointReceivedCallback: P2PCheckpointReceivedCallback;
171
+ private allNodesCheckpointReceivedCallback: P2PCheckpointReceivedCallback;
172
+ /**
173
+ * Callback for when a checkpoint proposal is received - specifically for validators - from a peer.
174
+ * @param checkpoint - The checkpoint proposal received from the peer.
175
+ * @returns The attestations for the checkpoint, if any.
176
+ */
177
+ private validatorCheckpointReceivedCallback: P2PCheckpointReceivedCallback;
167
178
 
168
179
  private gossipSubEventHandler: (e: CustomEvent<GossipsubMessage>) => void;
169
180
 
@@ -174,17 +185,17 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
174
185
  protected logger: Logger;
175
186
 
176
187
  constructor(
177
- private clientType: T,
178
188
  private config: P2PConfig,
179
189
  protected node: PubSubLibp2p,
180
190
  private peerDiscoveryService: PeerDiscoveryService,
181
191
  private reqresp: ReqRespInterface,
182
- private peerManager: PeerManagerInterface,
192
+ protected peerManager: PeerManagerInterface,
183
193
  protected mempools: MemPools,
184
- private archiver: L2BlockSource & ContractDataSource,
194
+ protected archiver: L2BlockSource & ContractDataSource,
185
195
  private epochCache: EpochCacheInterface,
186
196
  private proofVerifier: ClientProtocolCircuitVerifier,
187
197
  private worldStateSynchronizer: WorldStateSynchronizer,
198
+ private blockMinFeesProvider: BlockMinFeesProvider,
188
199
  telemetry: TelemetryClient,
189
200
  logger: Logger = createLogger('p2p:libp2p_service'),
190
201
  ) {
@@ -216,30 +227,39 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
216
227
  this.protocolVersion,
217
228
  );
218
229
 
219
- this.blockProposalValidator = new BlockProposalValidator(epochCache, { txsPermitted: !config.disableTransactions });
220
- this.checkpointProposalValidator = new CheckpointProposalValidator(epochCache, {
230
+ const p2pPropagationTime = config.attestationPropagationTime;
231
+ const proposalValidatorOpts = {
221
232
  txsPermitted: !config.disableTransactions,
222
- });
233
+ maxTxsPerBlock: config.validateMaxTxsPerBlock ?? config.validateMaxTxsPerCheckpoint,
234
+ p2pPropagationTime,
235
+ };
236
+ this.blockProposalValidator = new BlockProposalValidator(epochCache, proposalValidatorOpts);
237
+ this.checkpointProposalValidator = new CheckpointProposalValidator(epochCache, proposalValidatorOpts);
223
238
  this.checkpointAttestationValidator = config.fishermanMode
224
- ? new FishermanAttestationValidator(epochCache, mempools.attestationPool, telemetry)
225
- : new CheckpointAttestationValidator(epochCache);
239
+ ? new FishermanAttestationValidator(epochCache, mempools.attestationPool, telemetry, {
240
+ l1PublishingTime: config.l1PublishingTime,
241
+ })
242
+ : new CheckpointAttestationValidator(epochCache, { l1PublishingTime: config.l1PublishingTime });
226
243
 
227
244
  this.gossipSubEventHandler = this.handleGossipSubEvent.bind(this);
228
245
 
229
246
  this.blockReceivedCallback = async (block: BlockProposal): Promise<boolean> => {
230
- this.logger.debug(
231
- `Handler not yet registered: Block received callback not set. Received block for slot ${block.slotNumber} from peer.`,
247
+ this.logger.warn(
248
+ `Handler for block received not yet registered on P2P service. Received block ${block.blockNumber} for slot ${block.slotNumber} from peer.`,
232
249
  { p2pMessageIdentifier: await block.p2pMessageLoggingIdentifier() },
233
250
  );
234
- return false;
251
+ return true;
235
252
  };
236
253
 
237
- this.checkpointReceivedCallback = (
238
- checkpoint: CheckpointProposalCore,
254
+ this.allNodesCheckpointReceivedCallback = (
255
+ _checkpoint: CheckpointProposalCore,
256
+ ): Promise<CheckpointAttestation[] | undefined> => {
257
+ throw new CheckpointProposalReceivedCallbackNotRegisteredError();
258
+ };
259
+
260
+ this.validatorCheckpointReceivedCallback = (
261
+ _checkpoint: CheckpointProposalCore,
239
262
  ): Promise<CheckpointAttestation[] | undefined> => {
240
- this.logger.debug(
241
- `Handler not yet registered: Checkpoint received callback not set. Received checkpoint for slot ${checkpoint.slotNumber} from peer.`,
242
- );
243
263
  return Promise.resolve(undefined);
244
264
  };
245
265
  }
@@ -254,8 +274,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
254
274
  * @param txPool - The transaction pool to be accessed by the service.
255
275
  * @returns The new service.
256
276
  */
257
- public static async new<T extends P2PClientType>(
258
- clientType: T,
277
+ public static async new(
259
278
  config: P2PConfig,
260
279
  peerId: PeerId,
261
280
  deps: {
@@ -265,6 +284,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
265
284
  proofVerifier: ClientProtocolCircuitVerifier;
266
285
  worldStateSynchronizer: WorldStateSynchronizer;
267
286
  peerStore: AztecAsyncKVStore;
287
+ blockMinFeesProvider: BlockMinFeesProvider;
268
288
  telemetry: TelemetryClient;
269
289
  logger: Logger;
270
290
  packageVersion: string;
@@ -277,6 +297,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
277
297
  mempools,
278
298
  proofVerifier,
279
299
  peerStore,
300
+ blockMinFeesProvider,
280
301
  telemetry,
281
302
  logger,
282
303
  packageVersion,
@@ -286,14 +307,14 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
286
307
 
287
308
  const datastore = new AztecDatastore(peerStore);
288
309
 
289
- const otelMetricsAdapter = new OtelMetricsAdapter(telemetry);
310
+ const otelMetricsAdapter = new OtelMetricsAdapter(telemetry, logger.getBindings());
290
311
 
291
312
  const peerDiscoveryService = new DiscV5Service(
292
313
  peerId,
293
314
  config,
294
315
  packageVersion,
295
316
  telemetry,
296
- createLogger(`${logger.module}:discv5_service`),
317
+ createLogger(`${logger.module}:discv5_service`, logger.getBindings()),
297
318
  );
298
319
 
299
320
  // Seed libp2p's bootstrap discovery with private and trusted peers
@@ -307,11 +328,6 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
307
328
  const versions = getVersions(config);
308
329
  const protocolVersion = compressComponentVersions(versions);
309
330
 
310
- const txTopic = createTopicString(TopicType.tx, protocolVersion);
311
- const blockProposalTopic = createTopicString(TopicType.block_proposal, protocolVersion);
312
- const checkpointProposalTopic = createTopicString(TopicType.checkpoint_proposal, protocolVersion);
313
- const checkpointAttestationTopic = createTopicString(TopicType.checkpoint_attestation, protocolVersion);
314
-
315
331
  const preferredPeersEnrs: ENR[] = config.preferredPeers.map(enr => ENR.decodeTxt(enr));
316
332
  const directPeers = (
317
333
  await Promise.all(
@@ -331,6 +347,19 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
331
347
 
332
348
  const announceTcpMultiaddr = config.p2pIp ? [convertToMultiaddr(config.p2pIp, p2pPort, 'tcp')] : [];
333
349
 
350
+ // Create dynamic topic score params based on network configuration
351
+ const l1Constants = epochCache.getL1Constants();
352
+ const topicScoreParams = createAllTopicScoreParams(protocolVersion, {
353
+ slotDurationMs: l1Constants.slotDuration * 1000,
354
+ ethereumSlotDuration: l1Constants.ethereumSlotDuration,
355
+ heartbeatIntervalMs: config.gossipsubInterval,
356
+ targetCommitteeSize: l1Constants.targetCommitteeSize,
357
+ blockDurationMs: config.blockDurationMs,
358
+ l1PublishingTime: config.l1PublishingTime,
359
+ p2pPropagationTime: config.attestationPropagationTime,
360
+ expectedBlockProposalsPerSlot: config.expectedBlockProposalsPerSlot,
361
+ });
362
+
334
363
  const node = await createLibp2p({
335
364
  start: false,
336
365
  peerId,
@@ -426,35 +455,14 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
426
455
  scoreParams: createPeerScoreParams({
427
456
  // IPColocation factor can be disabled for local testing - default to -5
428
457
  IPColocationFactorWeight: config.debugDisableColocationPenalty ? 0 : -5.0,
429
- topics: {
430
- [txTopic]: createTopicScoreParams({
431
- topicWeight: 1,
432
- invalidMessageDeliveriesWeight: -20,
433
- invalidMessageDeliveriesDecay: 0.5,
434
- }),
435
- [blockProposalTopic]: createTopicScoreParams({
436
- topicWeight: 1,
437
- invalidMessageDeliveriesWeight: -20,
438
- invalidMessageDeliveriesDecay: 0.5,
439
- }),
440
- [checkpointProposalTopic]: createTopicScoreParams({
441
- topicWeight: 1,
442
- invalidMessageDeliveriesWeight: -20,
443
- invalidMessageDeliveriesDecay: 0.5,
444
- }),
445
- [checkpointAttestationTopic]: createTopicScoreParams({
446
- topicWeight: 1,
447
- invalidMessageDeliveriesWeight: -20,
448
- invalidMessageDeliveriesDecay: 0.5,
449
- }),
450
- },
458
+ topics: topicScoreParams,
451
459
  }),
452
460
  }) as (components: GossipSubComponents) => GossipSub,
453
461
  components: (components: { connectionManager: ConnectionManager }) => ({
454
462
  connectionManager: components.connectionManager,
455
463
  }),
456
464
  },
457
- logger: createLibp2pComponentLogger(logger.module),
465
+ logger: createLibp2pComponentLogger(logger.module, logger.getBindings()),
458
466
  });
459
467
 
460
468
  const peerScoring = new PeerScoring(config, telemetry);
@@ -473,13 +481,19 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
473
481
  epochCache,
474
482
  );
475
483
 
476
- // Update gossipsub score params
477
- node.services.pubsub.score.params.appSpecificWeight = 10;
484
+ // Gate req/resp data protocols for unauthenticated peers when p2pAllowOnlyValidators is enabled
485
+ reqresp.setShouldRejectPeer(peerId => peerManager.shouldDisableP2PGossip(peerId));
486
+
487
+ // Configure application-specific scoring for gossipsub.
488
+ // The weight scales app score to align with gossipsub thresholds:
489
+ // - Disconnect (-50) × 10 = -500 = gossipThreshold (stops receiving gossip)
490
+ // - Ban (-100) × 10 = -1000 = publishThreshold (cannot publish)
491
+ // Note: positive topic scores can offset penalties, so alignment is best-effort.
492
+ node.services.pubsub.score.params.appSpecificWeight = APP_SPECIFIC_WEIGHT;
478
493
  node.services.pubsub.score.params.appSpecificScore = (peerId: string) =>
479
494
  peerManager.shouldDisableP2PGossip(peerId) ? -Infinity : peerManager.getPeerScore(peerId);
480
495
 
481
496
  return new LibP2PService(
482
- clientType,
483
497
  config,
484
498
  node,
485
499
  peerDiscoveryService,
@@ -490,6 +504,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
490
504
  epochCache,
491
505
  proofVerifier,
492
506
  worldStateSynchronizer,
507
+ blockMinFeesProvider,
493
508
  telemetry,
494
509
  logger,
495
510
  );
@@ -515,18 +530,20 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
515
530
  // Create request response protocol handlers
516
531
  const txHandler = reqRespTxHandler(this.mempools);
517
532
  const goodbyeHandler = reqGoodbyeHandler(this.peerManager);
518
- const blockHandler = reqRespBlockHandler(this.archiver);
519
533
  const statusHandler = reqRespStatusHandler(this.protocolVersion, this.worldStateSynchronizer, this.logger);
520
534
 
521
535
  const requestResponseHandlers: Partial<ReqRespSubProtocolHandlers> = {
522
536
  [ReqRespSubProtocol.PING]: pingHandler,
523
537
  [ReqRespSubProtocol.STATUS]: statusHandler.bind(this),
524
538
  [ReqRespSubProtocol.GOODBYE]: goodbyeHandler.bind(this),
525
- [ReqRespSubProtocol.BLOCK]: blockHandler.bind(this),
526
539
  };
527
540
 
528
541
  if (!this.config.disableTransactions) {
529
- const blockTxsHandler = reqRespBlockTxsHandler(this.mempools.attestationPool, this.mempools.txPool);
542
+ const blockTxsHandler = reqRespBlockTxsHandler(
543
+ this.mempools.attestationPool,
544
+ this.archiver,
545
+ this.mempools.txPool,
546
+ );
530
547
  requestResponseHandlers[ReqRespSubProtocol.BLOCK_TXS] = blockTxsHandler.bind(this);
531
548
  }
532
549
 
@@ -539,7 +556,6 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
539
556
  ...DEFAULT_SUB_PROTOCOL_VALIDATORS,
540
557
  [ReqRespSubProtocol.TX]: this.validateRequestedTxs.bind(this),
541
558
  [ReqRespSubProtocol.BLOCK_TXS]: this.validateRequestedBlockTxs.bind(this),
542
- [ReqRespSubProtocol.BLOCK]: this.validateRequestedBlock.bind(this),
543
559
  };
544
560
 
545
561
  await this.peerManager.initializePeers();
@@ -549,7 +565,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
549
565
  await this.node.start();
550
566
 
551
567
  // Subscribe to standard GossipSub topics by default
552
- for (const topic of getTopicsForClientAndConfig(this.clientType, this.config.disableTransactions)) {
568
+ for (const topic of getTopicsForConfig(this.config.disableTransactions)) {
553
569
  this.subscribeToTopic(this.topicStrings[topic]);
554
570
  }
555
571
 
@@ -615,6 +631,10 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
615
631
  return this.peerManager.getPeers(includePending);
616
632
  }
617
633
 
634
+ public getGossipMeshPeerCount(topicType: TopicType): number {
635
+ return this.node.services.pubsub.getMeshPeers(this.topicStrings[topicType]).length;
636
+ }
637
+
618
638
  private handleGossipSubEvent(e: CustomEvent<GossipsubMessage>) {
619
639
  this.logger.trace(`Received PUBSUB message.`);
620
640
 
@@ -642,6 +662,15 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
642
662
  return this.reqresp.sendBatchRequest(protocol, requests, pinnedPeerId);
643
663
  }
644
664
 
665
+ public sendRequestToPeer(
666
+ peerId: PeerId,
667
+ subProtocol: ReqRespSubProtocol,
668
+ payload: Buffer,
669
+ dialTimeout?: number,
670
+ ): Promise<ReqRespResponse> {
671
+ return this.reqresp.sendRequestToPeer(peerId, subProtocol, payload, dialTimeout);
672
+ }
673
+
645
674
  /**
646
675
  * Get the ENR of the node
647
676
  * @returns The ENR of the node
@@ -654,8 +683,35 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
654
683
  this.blockReceivedCallback = callback;
655
684
  }
656
685
 
657
- public registerCheckpointReceivedCallback(callback: P2PCheckpointReceivedCallback) {
658
- this.checkpointReceivedCallback = callback;
686
+ public registerValidatorCheckpointReceivedCallback(callback: P2PCheckpointReceivedCallback) {
687
+ this.validatorCheckpointReceivedCallback = callback;
688
+ }
689
+
690
+ public registerAllNodesCheckpointReceivedCallback(callback: P2PCheckpointReceivedCallback) {
691
+ this.allNodesCheckpointReceivedCallback = callback;
692
+ }
693
+
694
+ public async notifyOwnCheckpointProposal(checkpoint: CheckpointProposalCore): Promise<void> {
695
+ await this.allNodesCheckpointReceivedCallback(checkpoint, this.node.peerId);
696
+ }
697
+
698
+ /**
699
+ * Registers a callback to be invoked when a duplicate proposal is detected.
700
+ * This callback is triggered on the first duplicate (when count goes from 1 to 2).
701
+ */
702
+ public registerDuplicateProposalCallback(
703
+ callback: (info: { slot: SlotNumber; proposer: EthAddress; type: 'checkpoint' | 'block' }) => void,
704
+ ): void {
705
+ this.duplicateProposalCallback = callback;
706
+ }
707
+
708
+ /**
709
+ * Registers a callback to be invoked when a duplicate attestation is detected.
710
+ * A validator signing attestations for different proposals at the same slot.
711
+ * This callback is triggered on the first duplicate (when count goes from 1 to 2).
712
+ */
713
+ public registerDuplicateAttestationCallback(callback: P2PDuplicateAttestationCallback): void {
714
+ this.duplicateAttestationCallback = callback;
659
715
  }
660
716
 
661
717
  /**
@@ -722,6 +778,9 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
722
778
  if (!validator || !validator.addMessage(msgId)) {
723
779
  this.instrumentation.incMessagePrevalidationStatus(false, topicType);
724
780
  this.node.services.pubsub.reportMessageValidationResult(msgId, source.toString(), TopicValidatorResult.Ignore);
781
+ if (topicType === TopicType.tx) {
782
+ this.logger.verbose(`Ignoring already-seen tx gossip message`, { msgId, source: source.toString() });
783
+ }
725
784
  return { result: false, topicType };
726
785
  }
727
786
 
@@ -786,9 +845,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
786
845
  if (msg.topic === this.topicStrings[TopicType.tx]) {
787
846
  await this.handleGossipedTx(p2pMessage.payload, msgId, source);
788
847
  } else if (msg.topic === this.topicStrings[TopicType.checkpoint_attestation]) {
789
- if (this.clientType === P2PClientType.Full) {
790
- await this.processCheckpointAttestationFromPeer(p2pMessage.payload, msgId, source);
791
- }
848
+ await this.processCheckpointAttestationFromPeer(p2pMessage.payload, msgId, source);
792
849
  } else if (msg.topic === this.topicStrings[TopicType.block_proposal]) {
793
850
  await this.processBlockFromPeer(p2pMessage.payload, msgId, source);
794
851
  } else if (msg.topic === this.topicStrings[TopicType.checkpoint_proposal]) {
@@ -844,51 +901,145 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
844
901
  return;
845
902
  }
846
903
 
847
- protected async validateReceivedMessage<T>(
848
- validationFunc: () => Promise<ReceivedMessageValidationResult<T>>,
904
+ protected async validateReceivedMessage<T, M = undefined>(
905
+ validationFunc: () => Promise<ReceivedMessageValidationResult<T, M>>,
849
906
  msgId: string,
850
907
  source: PeerId,
851
908
  topicType: TopicType,
852
- ): Promise<ReceivedMessageValidationResult<T>> {
853
- let resultAndObj: ReceivedMessageValidationResult<T> = { result: TopicValidatorResult.Reject };
909
+ ): Promise<ReceivedMessageValidationResult<T, M>> {
910
+ // Default to reject result with a penalty if validation function throws an error
911
+ let resultAndObj: ReceivedMessageValidationResult<T, M> = {
912
+ result: TopicValidatorResult.Reject,
913
+ severity: PeerErrorSeverity.MidToleranceError,
914
+ };
854
915
  const timer = new Timer();
855
916
  try {
856
917
  resultAndObj = await validationFunc();
857
918
  } catch (err) {
858
- this.peerManager.penalizePeer(source, PeerErrorSeverity.LowToleranceError);
859
- this.logger.error(`Error deserializing and validating gossipsub message`, err, {
860
- msgId,
861
- source: source.toString(),
862
- topicType,
863
- });
919
+ this.logger.error(`Error validating gossipsub message`, err, { msgId, source: source.toString(), topicType });
920
+ }
921
+
922
+ const validationTimeMs = timer.ms();
923
+ const mcacheWindowMs = this.config.gossipsubMcacheLength * this.config.gossipsubInterval;
924
+ if (validationTimeMs > mcacheWindowMs * 0.75) {
925
+ this.instrumentation.incSlowValidation(topicType);
926
+ this.logger.warn(
927
+ `Gossip validation for ${topicType} took ${validationTimeMs}ms, approaching mcache eviction window of ${mcacheWindowMs}ms. ` +
928
+ `Message forwarding may be skipped if validation exceeds the window.`,
929
+ { msgId, source: source.toString(), topicType, validationTimeMs, mcacheWindowMs },
930
+ );
864
931
  }
865
932
 
866
933
  if (resultAndObj.result === TopicValidatorResult.Accept) {
934
+ this.logger.debug(`Message ${topicType} accepted by validator`, { msgId, source: source.toString(), topicType });
867
935
  this.instrumentation.recordMessageValidation(topicType, timer);
936
+ } else if (resultAndObj.result === TopicValidatorResult.Reject) {
937
+ this.logger.warn(`Message ${topicType} rejected by validator with severity ${resultAndObj.severity}`, {
938
+ msgId,
939
+ source: source.toString(),
940
+ topicType,
941
+ severity: resultAndObj.severity,
942
+ });
943
+ this.peerManager.penalizePeer(source, resultAndObj.severity);
944
+ } else {
945
+ this.logger.trace(`Message ${topicType} ignored by validator`, { msgId, source: source.toString(), topicType });
868
946
  }
869
947
 
870
948
  this.node.services.pubsub.reportMessageValidationResult(msgId, source.toString(), resultAndObj.result);
871
949
  return resultAndObj;
872
950
  }
873
951
 
952
+ private tryDeserialize<T>(deserializeFunc: () => T, msgId: string, source: PeerId): T | undefined {
953
+ try {
954
+ return deserializeFunc();
955
+ } catch (err) {
956
+ this.logger.warn(`Failed to deserialize gossipsub message from buffer`, {
957
+ err,
958
+ msgId,
959
+ source: source.toString(),
960
+ });
961
+ return undefined;
962
+ }
963
+ }
964
+
874
965
  protected async handleGossipedTx(payloadData: Buffer, msgId: string, source: PeerId) {
875
966
  const validationFunc: () => Promise<ReceivedMessageValidationResult<Tx>> = async () => {
876
- const tx = Tx.fromBuffer(payloadData);
877
- const isValid = await this.validatePropagatedTx(tx, source);
878
- const exists = isValid && (await this.mempools.txPool.hasTx(tx.getTxHash()));
967
+ const tx = this.tryDeserialize(() => Tx.fromBuffer(payloadData), msgId, source);
968
+ if (!tx) {
969
+ return { result: TopicValidatorResult.Reject, severity: PeerErrorSeverity.LowToleranceError };
970
+ }
971
+
972
+ const currentBlockNumber = await this.archiver.getBlockNumber();
973
+ const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
974
+
975
+ // Stage 1: fast validators (metadata, data, timestamps, double-spend, gas, phases, block header)
976
+ const firstStageValidators = await this.createFirstStageMessageValidators(currentBlockNumber, nextSlotTimestamp);
977
+ const firstStageOutcome = await this.runValidations(tx, firstStageValidators);
978
+ if (!firstStageOutcome.allPassed) {
979
+ const { name } = firstStageOutcome.failure;
980
+ let { severity } = firstStageOutcome.failure;
981
+
982
+ // Double spend validator has a special case handler. We perform more detailed examination
983
+ // as to how recently the nullifier was entered into the tree and if the transaction should
984
+ // have 'known' the nullifier existed. This determines the severity of the penalty applied to the peer.
985
+ if (name === 'doubleSpendValidator') {
986
+ const txBlockNumber = BlockNumber(currentBlockNumber + 1);
987
+ severity = await this.handleDoubleSpendFailure(tx, txBlockNumber);
988
+ }
989
+
990
+ this.logger.verbose(`Rejecting gossiped tx ${tx.getTxHash().toString()}: stage 1 validation failed`, {
991
+ validator: name,
992
+ severity,
993
+ source: source.toString(),
994
+ });
995
+ return { result: TopicValidatorResult.Reject, severity };
996
+ }
879
997
 
880
- this.logger.trace(`Validate propagated tx`, {
881
- isValid,
882
- exists,
998
+ // Pool pre-check: see if the pool would accept this tx before doing expensive proof verification
999
+ const canAdd = await this.mempools.txPool.canAddPendingTx(tx);
1000
+ if (canAdd === 'ignored') {
1001
+ this.logger.verbose(`Ignoring gossiped tx ${tx.getTxHash().toString()}: pool pre-check returned ignored`, {
1002
+ source: source.toString(),
1003
+ });
1004
+ return { result: TopicValidatorResult.Ignore, obj: tx };
1005
+ }
1006
+
1007
+ // Stage 2: expensive proof verification
1008
+ const secondStageValidators = this.createSecondStageMessageValidators();
1009
+ const secondStageOutcome = await this.runValidations(tx, secondStageValidators);
1010
+ if (!secondStageOutcome.allPassed) {
1011
+ const { severity, name } = secondStageOutcome.failure;
1012
+ this.logger.verbose(`Rejecting gossiped tx ${tx.getTxHash().toString()}: stage 2 validation failed`, {
1013
+ validator: name,
1014
+ severity,
1015
+ source: source.toString(),
1016
+ });
1017
+ return { result: TopicValidatorResult.Reject, severity };
1018
+ }
1019
+
1020
+ // Pool add: persist the tx
1021
+ const txHash = tx.getTxHash();
1022
+ const addResult = await this.mempools.txPool.addPendingTxs([tx], { source: 'gossip' });
1023
+
1024
+ const wasAccepted = addResult.accepted.some(h => h.equals(txHash));
1025
+ const wasIgnored = addResult.ignored.some(h => h.equals(txHash));
1026
+
1027
+ this.logger.verbose(`Validate propagated tx ${txHash.toString()}`, {
1028
+ wasAccepted,
1029
+ wasIgnored,
883
1030
  [Attributes.P2P_ID]: source.toString(),
884
1031
  });
885
1032
 
886
- if (!isValid) {
887
- return { result: TopicValidatorResult.Reject };
888
- } else if (exists) {
1033
+ if (wasAccepted) {
1034
+ return { result: TopicValidatorResult.Accept, obj: tx };
1035
+ } else if (wasIgnored) {
889
1036
  return { result: TopicValidatorResult.Ignore, obj: tx };
890
1037
  } else {
891
- return { result: TopicValidatorResult.Accept, obj: tx };
1038
+ this.logger.warn(`Gossiped tx ${txHash.toString()} unexpectedly rejected by pool`, {
1039
+ source: source.toString(),
1040
+ txHash: txHash.toString(),
1041
+ });
1042
+ return { result: TopicValidatorResult.Reject, severity: PeerErrorSeverity.HighToleranceError };
892
1043
  }
893
1044
  };
894
1045
 
@@ -897,6 +1048,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
897
1048
  return;
898
1049
  }
899
1050
 
1051
+ // Tx was accepted into pool and will be propagated - just log and record metrics
900
1052
  const txHash = tx.getTxHash();
901
1053
  const txHashString = txHash.toString();
902
1054
  this.logger.verbose(`Received tx ${txHashString} from external peer ${source.toString()} via gossip`, {
@@ -904,13 +1056,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
904
1056
  txHash: txHashString,
905
1057
  });
906
1058
 
907
- if (this.config.dropTransactions && randomInt(1000) < this.config.dropTransactionsProbability * 1000) {
908
- this.logger.warn(`Intentionally dropping tx ${txHashString} (probability rule)`);
909
- return;
910
- }
911
-
912
1059
  this.instrumentation.incrementTxReceived(1);
913
- await this.mempools.txPool.addTxs([tx]);
914
1060
  }
915
1061
 
916
1062
  /**
@@ -922,47 +1068,17 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
922
1068
  msgId: string,
923
1069
  source: PeerId,
924
1070
  ): Promise<void> {
925
- const validationFunc: () => Promise<ReceivedMessageValidationResult<CheckpointAttestation>> = async () => {
926
- const attestation = CheckpointAttestation.fromBuffer(payloadData);
927
- const pool = this.mempools.attestationPool;
928
- const validationResult = await this.validateCheckpointAttestation(source, attestation);
929
- const isValid = validationResult.result === 'accept';
930
- const exists = isValid && (await pool.hasCheckpointAttestation(attestation));
931
-
932
- let canAdd = true;
933
- if (isValid && !exists) {
934
- const slot = attestation.payload.header.slotNumber;
935
- const { committee } = await this.epochCache.getCommittee(slot);
936
- const committeeSize = committee?.length ?? 0;
937
- canAdd = await pool.canAddCheckpointAttestation(attestation, committeeSize);
938
- }
939
-
940
- this.logger.trace(`Validate propagated checkpoint attestation`, {
941
- isValid,
942
- exists,
943
- canAdd,
944
- [Attributes.SLOT_NUMBER]: attestation.payload.header.slotNumber.toString(),
945
- [Attributes.P2P_ID]: source.toString(),
946
- });
947
-
948
- if (validationResult.result === 'reject') {
949
- return { result: TopicValidatorResult.Reject };
950
- } else if (validationResult.result === 'ignore' || exists) {
951
- return { result: TopicValidatorResult.Ignore, obj: attestation };
952
- } else if (!canAdd) {
953
- this.logger.warn(`Dropping checkpoint attestation due to per-(slot, proposalId) attestation cap`, {
954
- slot: attestation.payload.header.slotNumber.toString(),
955
- archive: attestation.archive.toString(),
956
- source: source.toString(),
957
- });
958
- return { result: TopicValidatorResult.Ignore, obj: attestation };
959
- } else {
960
- return { result: TopicValidatorResult.Accept, obj: attestation };
961
- }
962
- };
963
-
964
1071
  const { result, obj: attestation } = await this.validateReceivedMessage<CheckpointAttestation>(
965
- validationFunc,
1072
+ () => {
1073
+ const attestation = this.tryDeserialize(() => CheckpointAttestation.fromBuffer(payloadData), msgId, source);
1074
+ if (!attestation) {
1075
+ return Promise.resolve({
1076
+ result: TopicValidatorResult.Reject,
1077
+ severity: PeerErrorSeverity.LowToleranceError,
1078
+ });
1079
+ }
1080
+ return this.validateAndStoreCheckpointAttestation(source, attestation);
1081
+ },
966
1082
  msgId,
967
1083
  source,
968
1084
  TopicType.checkpoint_attestation,
@@ -972,8 +1088,8 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
972
1088
  return;
973
1089
  }
974
1090
 
975
- this.logger.debug(
976
- `Received checkpoint attestation for slot ${attestation.slotNumber} from external peer ${source.toString()}`,
1091
+ this.logger.verbose(
1092
+ `Received valid checkpoint attestation for slot ${attestation.slotNumber} from external peer ${source.toString()}`,
977
1093
  {
978
1094
  p2pMessageIdentifier: await attestation.p2pMessageLoggingIdentifier(),
979
1095
  slot: attestation.slotNumber,
@@ -981,60 +1097,168 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
981
1097
  source: source.toString(),
982
1098
  },
983
1099
  );
984
-
985
- await this.mempools.attestationPool.addCheckpointAttestations([attestation]);
986
1100
  }
987
1101
 
988
- private async processBlockFromPeer(payloadData: Buffer, msgId: string, source: PeerId): Promise<void> {
989
- const validationFunc: () => Promise<ReceivedMessageValidationResult<BlockProposal>> = async () => {
990
- const block = BlockProposal.fromBuffer(payloadData);
991
- const validationResult = await this.validateBlockProposal(source, block);
992
- const isValid = validationResult.result === 'accept';
993
- const pool = this.mempools.attestationPool;
994
-
995
- const exists = isValid && (await pool.hasBlockProposal(block));
996
- const canAdd = isValid && (await pool.canAddProposal(block));
997
-
998
- this.logger.trace(`Validate propagated block proposal`, {
999
- isValid,
1000
- exists,
1001
- canAdd,
1002
- [Attributes.SLOT_NUMBER]: block.slotNumber.toString(),
1003
- [Attributes.P2P_ID]: source.toString(),
1102
+ /** Validates a checkpoint attestation and adds it to the pool. Penalizes the peer if validation fails. */
1103
+ @trackSpan('Libp2pService.validateAndStoreCheckpointAttestation', (_peerId, attestation) => ({
1104
+ [Attributes.SLOT_NUMBER]: attestation.payload.header.slotNumber.toString(),
1105
+ }))
1106
+ protected async validateAndStoreCheckpointAttestation(
1107
+ peerId: PeerId,
1108
+ attestation: CheckpointAttestation,
1109
+ ): Promise<ReceivedMessageValidationResult<CheckpointAttestation>> {
1110
+ const validationResult = await this.checkpointAttestationValidator.validate(attestation);
1111
+
1112
+ if (validationResult.result === 'reject') {
1113
+ this.logger.warn(`Penalizing peer ${peerId} for checkpoint attestation validation failure`);
1114
+ return { result: TopicValidatorResult.Reject, severity: validationResult.severity };
1115
+ }
1116
+
1117
+ if (validationResult.result === 'ignore') {
1118
+ return { result: TopicValidatorResult.Ignore, obj: attestation };
1119
+ }
1120
+
1121
+ // Try to add the attestation: this handles existence check, cap check, and adding in one call
1122
+ // count is the number of attestations by this signer for this slot (for duplicate detection)
1123
+ const slot = attestation.payload.header.slotNumber;
1124
+ const { added, alreadyExists, count } =
1125
+ await this.mempools.attestationPool.tryAddCheckpointAttestation(attestation);
1126
+
1127
+ this.logger.trace(`Validate propagated checkpoint attestation`, {
1128
+ added,
1129
+ alreadyExists,
1130
+ count,
1131
+ [Attributes.SLOT_NUMBER]: slot.toString(),
1132
+ [Attributes.P2P_ID]: peerId.toString(),
1133
+ });
1134
+
1135
+ // Exact same attestation received, no need to re-broadcast
1136
+ if (alreadyExists) {
1137
+ return { result: TopicValidatorResult.Ignore, obj: attestation };
1138
+ }
1139
+
1140
+ // Could not add (cap reached for signer), penalize and do not re-broadcast
1141
+ if (!added) {
1142
+ this.logger.warn(`Rejecting checkpoint attestation due to cap`, {
1143
+ slot: slot.toString(),
1144
+ archive: attestation.archive.toString(),
1145
+ source: peerId.toString(),
1146
+ attester: attestation.getSender()?.toString(),
1147
+ count,
1004
1148
  });
1149
+ return { result: TopicValidatorResult.Reject, severity: PeerErrorSeverity.HighToleranceError };
1150
+ }
1005
1151
 
1006
- if (validationResult.result === 'reject') {
1007
- return { result: TopicValidatorResult.Reject };
1008
- } else if (validationResult.result === 'ignore' || exists) {
1009
- return { result: TopicValidatorResult.Ignore, obj: block };
1010
- } else if (!canAdd) {
1011
- this.peerManager.penalizePeer(source, PeerErrorSeverity.MidToleranceError);
1012
- this.logger.warn(`Penalizing peer for block proposal exceeding per-slot cap`, {
1013
- slot: block.slotNumber.toString(),
1014
- archive: block.archive.toString(),
1015
- source: source.toString(),
1152
+ // Check if this is a duplicate attestation (signer attested to a different proposal at the same slot)
1153
+ // count is the number of attestations by this signer for this slot
1154
+ if (count === 2) {
1155
+ const attester = attestation.getSender();
1156
+ if (attester) {
1157
+ this.logger.warn(`Detected duplicate attestation (equivocation) at slot ${slot}`, {
1158
+ slot: slot.toString(),
1159
+ archive: attestation.archive.toString(),
1160
+ source: peerId.toString(),
1161
+ attester: attester.toString(),
1016
1162
  });
1017
- return { result: TopicValidatorResult.Reject };
1018
- } else {
1019
- return { result: TopicValidatorResult.Accept, obj: block };
1163
+ this.duplicateAttestationCallback?.({ slot, attester });
1020
1164
  }
1021
- };
1165
+ }
1022
1166
 
1023
- const { result, obj: block } = await this.validateReceivedMessage<BlockProposal>(
1024
- validationFunc,
1167
+ // Attestation was added successfully - accept it so other nodes can also detect the equivocation
1168
+ return { result: TopicValidatorResult.Accept, obj: attestation };
1169
+ }
1170
+
1171
+ protected async processBlockFromPeer(payloadData: Buffer, msgId: string, source: PeerId): Promise<void> {
1172
+ const {
1173
+ result,
1174
+ obj: block,
1175
+ metadata: { isEquivocated } = {},
1176
+ } = await this.validateReceivedMessage<BlockProposal, { isEquivocated: boolean }>(
1177
+ () => this.validateAndStoreBlockProposal(source, BlockProposal.fromBuffer(payloadData)),
1025
1178
  msgId,
1026
1179
  source,
1027
1180
  TopicType.block_proposal,
1028
1181
  );
1029
1182
 
1030
- if (!result || !block) {
1183
+ // If not accepted or equivocated, return
1184
+ if (result !== TopicValidatorResult.Accept || !block || isEquivocated) {
1031
1185
  return;
1032
1186
  }
1033
1187
 
1034
1188
  await this.processValidBlockProposal(block, source);
1035
1189
  }
1036
1190
 
1037
- // REVIEW: callback pattern https://github.com/AztecProtocol/aztec-packages/issues/7963
1191
+ /** Validates a block proposal. Triggers a penalization to the peer that sent it if invalid. Adds to the mempool if valid. */
1192
+ @trackSpan('Libp2pService.validateAndStoreBlockProposal', (_peerId, block) => ({
1193
+ [Attributes.BLOCK_NUMBER]: block.blockNumber.toString(),
1194
+ [Attributes.SLOT_NUMBER]: block.slotNumber.toString(),
1195
+ }))
1196
+ protected async validateAndStoreBlockProposal(
1197
+ peerId: PeerId,
1198
+ block: BlockProposal,
1199
+ ): Promise<ReceivedMessageValidationResult<BlockProposal, { isEquivocated: boolean }>> {
1200
+ const validationResult = await this.blockProposalValidator.validate(block);
1201
+
1202
+ if (validationResult.result === 'reject') {
1203
+ this.logger.warn(`Penalizing peer ${peerId} for block proposal validation failure`);
1204
+ return { result: TopicValidatorResult.Reject, severity: validationResult.severity };
1205
+ }
1206
+
1207
+ if (validationResult.result === 'ignore') {
1208
+ return { result: TopicValidatorResult.Ignore, obj: block };
1209
+ }
1210
+
1211
+ // Try to add the proposal: this handles existence check, cap check, and adding in one call
1212
+ const { added, alreadyExists, count } = await this.mempools.attestationPool.tryAddBlockProposal(block);
1213
+ const isEquivocated = count !== undefined && count > 1;
1214
+
1215
+ // Duplicate proposal received, no need to re-broadcast
1216
+ if (alreadyExists) {
1217
+ this.logger.debug(`Ignoring duplicate block proposal received`, {
1218
+ ...block.toBlockInfo(),
1219
+ indexWithinCheckpoint: block.indexWithinCheckpoint,
1220
+ proposer: block.getSender()?.toString(),
1221
+ source: peerId.toString(),
1222
+ });
1223
+ return { result: TopicValidatorResult.Ignore, obj: block, metadata: { isEquivocated } };
1224
+ }
1225
+
1226
+ // Too many blocks received for this slot and index, penalize peer and do not re-broadcast
1227
+ if (!added) {
1228
+ this.logger.warn(`Penalizing peer for block proposal exceeding per-position cap`, {
1229
+ ...block.toBlockInfo(),
1230
+ indexWithinCheckpoint: block.indexWithinCheckpoint,
1231
+ count,
1232
+ proposer: block.getSender()?.toString(),
1233
+ source: peerId.toString(),
1234
+ });
1235
+ return {
1236
+ result: TopicValidatorResult.Reject,
1237
+ metadata: { isEquivocated },
1238
+ severity: PeerErrorSeverity.HighToleranceError,
1239
+ };
1240
+ }
1241
+
1242
+ // If this was a duplicate proposal, do not process it, but do invoke the duplicate callback,
1243
+ // and do re-broadcast it so other nodes in the network know to slash the proposer
1244
+ if (isEquivocated) {
1245
+ const proposer = block.getSender();
1246
+ this.logger.warn(`Detected duplicate block proposal (equivocation) at slot ${block.slotNumber}`, {
1247
+ ...block.toBlockInfo(),
1248
+ source: peerId.toString(),
1249
+ proposer: proposer?.toString(),
1250
+ });
1251
+ // Invoke the duplicate callback on the first duplicate spotted only
1252
+ if (proposer && count === 2) {
1253
+ this.duplicateProposalCallback?.({ slot: block.slotNumber, proposer, type: 'block' });
1254
+ }
1255
+ return { result: TopicValidatorResult.Accept, obj: block, metadata: { isEquivocated } };
1256
+ }
1257
+
1258
+ // Otherwise, we're good to go!
1259
+ return { result: TopicValidatorResult.Accept, obj: block };
1260
+ }
1261
+
1038
1262
  // REFACTOR(palla): This method should be moved to the p2p_client or to a separate component,
1039
1263
  // should not be here as it does not deal with p2p networking.
1040
1264
  @trackSpan('Libp2pService.processValidBlockProposal', async block => ({
@@ -1042,7 +1266,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1042
1266
  [Attributes.BLOCK_ARCHIVE]: block.archive.toString(),
1043
1267
  [Attributes.P2P_ID]: await block.p2pMessageLoggingIdentifier().then(i => i.toString()),
1044
1268
  }))
1045
- private async processValidBlockProposal(block: BlockProposal, sender: PeerId) {
1269
+ protected async processValidBlockProposal(block: BlockProposal, sender: PeerId) {
1046
1270
  const slot = block.slotNumber;
1047
1271
  this.logger.verbose(`Received block proposal for slot ${slot} from external peer ${sender.toString()}.`, {
1048
1272
  p2pMessageIdentifier: await block.p2pMessageLoggingIdentifier(),
@@ -1050,30 +1274,14 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1050
1274
  ...block.toBlockInfo(),
1051
1275
  });
1052
1276
 
1053
- // Attempt to add proposal
1054
- try {
1055
- await this.mempools.attestationPool.addBlockProposal(block);
1056
- } catch (err: unknown) {
1057
- // Drop proposals if we hit per-slot cap in the attestation pool; rethrow unknown errors
1058
- if (err instanceof ProposalSlotCapExceededError) {
1059
- this.logger.warn(`Dropping block proposal due to per-slot proposal cap`, {
1060
- slot: String(slot),
1061
- archive: block.archive.toString(),
1062
- error: (err as Error).message,
1063
- });
1064
- return;
1065
- }
1066
- throw err;
1067
- }
1068
-
1069
- // Mark the txs in this proposal as non-evictable
1070
- await this.mempools.txPool.markTxsAsNonEvictable(block.txHashes);
1277
+ // Mark the txs in this proposal as protected
1278
+ await this.mempools.txPool.protectTxs(block.txHashes, block.blockHeader);
1071
1279
 
1072
1280
  // Call the block received callback to validate the proposal.
1073
1281
  // Note: Validators do NOT attest to individual blocks, only to checkpoint proposals.
1074
1282
  const isValid = await this.blockReceivedCallback(block, sender);
1075
1283
  if (!isValid) {
1076
- this.logger.warn(`Block proposal validation failed for block ${block.blockNumber}`, block.toBlockInfo());
1284
+ this.logger.info(`Block proposal validation failed for block ${block.blockNumber}`, block.toBlockInfo());
1077
1285
  }
1078
1286
  }
1079
1287
 
@@ -1081,67 +1289,149 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1081
1289
  * Handle a gossiped checkpoint proposal.
1082
1290
  * Validates and processes the checkpoint proposal, then triggers the callback for attestation.
1083
1291
  */
1084
- private async handleGossipedCheckpointProposal(payloadData: Buffer, msgId: string, source: PeerId): Promise<void> {
1085
- // TODO(palla/mbps): This pattern is repeated across multiple message handlers, consider abstracting it.
1086
- const validationFunc: () => Promise<ReceivedMessageValidationResult<CheckpointProposal>> = async () => {
1087
- const checkpoint = CheckpointProposal.fromBuffer(payloadData);
1088
- const validationResult = await this.validateCheckpointProposal(source, checkpoint);
1089
- const isValid = validationResult.result === 'accept';
1090
- const pool = this.mempools.attestationPool;
1091
-
1092
- const exists = isValid && (await pool.hasCheckpointProposal(checkpoint));
1093
- const canAdd = isValid && (await pool.canAddCheckpointProposal(checkpoint));
1094
-
1095
- this.logger.trace(`Validate propagated checkpoint proposal`, {
1096
- isValid,
1097
- exists,
1098
- canAdd,
1099
- [Attributes.SLOT_NUMBER]: checkpoint.slotNumber.toString(),
1100
- [Attributes.P2P_ID]: source.toString(),
1101
- });
1102
-
1103
- if (validationResult.result === 'reject') {
1104
- return { result: TopicValidatorResult.Reject };
1105
- } else if (validationResult.result === 'ignore' || exists) {
1106
- return { result: TopicValidatorResult.Ignore, obj: checkpoint };
1107
- } else if (!canAdd) {
1108
- this.peerManager.penalizePeer(source, PeerErrorSeverity.MidToleranceError);
1109
- this.logger.warn(`Penalizing peer for checkpoint proposal exceeding per-slot cap`, {
1110
- slot: checkpoint.slotNumber.toString(),
1111
- archive: checkpoint.archive.toString(),
1112
- source: source.toString(),
1113
- });
1114
- return { result: TopicValidatorResult.Reject };
1115
- } else {
1116
- return { result: TopicValidatorResult.Accept, obj: checkpoint };
1117
- }
1118
- };
1119
-
1120
- const { result, obj: checkpoint } = await this.validateReceivedMessage<CheckpointProposal>(
1121
- validationFunc,
1292
+ protected async handleGossipedCheckpointProposal(payloadData: Buffer, msgId: string, source: PeerId): Promise<void> {
1293
+ const {
1294
+ result,
1295
+ obj: checkpoint,
1296
+ metadata: { isEquivocated, processBlock } = {},
1297
+ } = await this.validateReceivedMessage<CheckpointProposal, { isEquivocated: boolean; processBlock: boolean }>(
1298
+ () => this.validateAndStoreCheckpointProposal(source, CheckpointProposal.fromBuffer(payloadData)),
1122
1299
  msgId,
1123
1300
  source,
1124
1301
  TopicType.checkpoint_proposal,
1125
1302
  );
1126
1303
 
1127
- if (result !== TopicValidatorResult.Accept || !checkpoint) {
1304
+ // If the checkpoint contained a valid last block, we process it even if the checkpoint itself is to be rejected
1305
+ // TODO(palla/mbps): Is this ok? Should we be considering a block from a checkpoint that was equivocated?
1306
+ if (processBlock && checkpoint?.getBlockProposal()) {
1307
+ await this.processValidBlockProposal(checkpoint.getBlockProposal()!, source);
1308
+ }
1309
+
1310
+ if (result !== TopicValidatorResult.Accept || !checkpoint || isEquivocated) {
1128
1311
  return;
1129
1312
  }
1130
1313
 
1131
- await this.processValidCheckpointProposal(checkpoint, source);
1314
+ await this.processValidCheckpointProposal(checkpoint.toCore(), source);
1315
+ }
1316
+
1317
+ /**
1318
+ * Validates a checkpoint proposal. Penalizes peer if validation fails. Adds the checkpoint and
1319
+ * its last block (if present) to the mempool if valid. Triggers equivocation detection on both.
1320
+ */
1321
+ @trackSpan('Libp2pService.validateAndStoreCheckpointProposal', (_peerId, checkpoint) => ({
1322
+ [Attributes.SLOT_NUMBER]: checkpoint.slotNumber.toString(),
1323
+ }))
1324
+ protected async validateAndStoreCheckpointProposal(
1325
+ peerId: PeerId,
1326
+ checkpoint: CheckpointProposal,
1327
+ ): Promise<ReceivedMessageValidationResult<CheckpointProposal, { isEquivocated: boolean; processBlock: boolean }>> {
1328
+ const validationResult = await this.checkpointProposalValidator.validate(checkpoint);
1329
+
1330
+ if (validationResult.result === 'reject') {
1331
+ this.logger.warn(`Penalizing peer ${peerId} for checkpoint proposal validation failure`);
1332
+ return { result: TopicValidatorResult.Reject, severity: validationResult.severity };
1333
+ }
1334
+
1335
+ if (validationResult.result === 'ignore') {
1336
+ return { result: TopicValidatorResult.Ignore, obj: checkpoint };
1337
+ }
1338
+
1339
+ // Extract and try to add the block proposal first if present
1340
+ const blockProposal = checkpoint.getBlockProposal();
1341
+ let processBlock = false;
1342
+ if (blockProposal) {
1343
+ this.logger.debug(`Validating block proposal from propagated checkpoint`, {
1344
+ [Attributes.SLOT_NUMBER]: checkpoint.slotNumber.toString(),
1345
+ [Attributes.P2P_ID]: peerId.toString(),
1346
+ });
1347
+ const blockProposalResult = await this.validateAndStoreBlockProposal(peerId, blockProposal);
1348
+ const { obj, metadata: { isEquivocated } = {} } = blockProposalResult;
1349
+ if (blockProposalResult.result === TopicValidatorResult.Reject || !obj || isEquivocated) {
1350
+ this.logger.debug(`Rejecting checkpoint due to invalid last block proposal`, {
1351
+ [Attributes.SLOT_NUMBER]: checkpoint.slotNumber.toString(),
1352
+ [Attributes.P2P_ID]: peerId.toString(),
1353
+ isEquivocated,
1354
+ result: blockProposalResult.result,
1355
+ });
1356
+ return {
1357
+ result: TopicValidatorResult.Reject,
1358
+ severity:
1359
+ 'severity' in blockProposalResult ? blockProposalResult.severity : PeerErrorSeverity.MidToleranceError,
1360
+ };
1361
+ } else if (blockProposalResult.result === TopicValidatorResult.Accept && obj && !isEquivocated) {
1362
+ processBlock = true;
1363
+ }
1364
+ }
1365
+
1366
+ // Try to add the checkpoint proposal core: this handles existence check, cap check, and adding in one call
1367
+ const checkpointCore = checkpoint.toCore();
1368
+ const tryAddResult = await this.mempools.attestationPool.tryAddCheckpointProposal(checkpointCore);
1369
+ const { added, alreadyExists, count } = tryAddResult;
1370
+ const isEquivocated = count !== undefined && count > 1;
1371
+
1372
+ // Duplicate proposal received, do not re-broadcast
1373
+ if (alreadyExists) {
1374
+ this.logger.debug(`Ignoring duplicate checkpoint proposal received`, {
1375
+ ...checkpoint.toCheckpointInfo(),
1376
+ source: peerId.toString(),
1377
+ });
1378
+ return {
1379
+ result: TopicValidatorResult.Ignore,
1380
+ obj: checkpoint,
1381
+ metadata: { isEquivocated, processBlock },
1382
+ };
1383
+ }
1384
+
1385
+ // Too many checkpoint proposals received for this slot, penalize peer and do not re-broadcast
1386
+ // Note: We still return the checkpoint obj so the lastBlock can be processed if valid
1387
+ if (!added) {
1388
+ this.logger.warn(`Penalizing peer for checkpoint proposal exceeding per-slot cap`, {
1389
+ ...checkpoint.toCheckpointInfo(),
1390
+ count,
1391
+ source: peerId.toString(),
1392
+ });
1393
+ return {
1394
+ result: TopicValidatorResult.Reject,
1395
+ obj: checkpoint,
1396
+ metadata: { isEquivocated, processBlock },
1397
+ severity: PeerErrorSeverity.HighToleranceError,
1398
+ };
1399
+ }
1400
+
1401
+ // If this was a duplicate proposal, do not process it, but do invoke the duplicate callback,
1402
+ // and do re-broadcast it so other nodes in the network know to slash the proposer
1403
+ if (isEquivocated) {
1404
+ const proposer = checkpoint.getSender();
1405
+ this.logger.warn(`Detected duplicate checkpoint proposal (equivocation) at slot ${checkpoint.slotNumber}`, {
1406
+ ...checkpoint.toCheckpointInfo(),
1407
+ source: peerId.toString(),
1408
+ proposer: proposer?.toString(),
1409
+ });
1410
+ // Invoke the duplicate callback on the first duplicate spotted only
1411
+ if (proposer && count === 2) {
1412
+ this.duplicateProposalCallback?.({ slot: checkpoint.slotNumber, proposer, type: 'checkpoint' });
1413
+ }
1414
+ return {
1415
+ result: TopicValidatorResult.Accept,
1416
+ obj: checkpoint,
1417
+ metadata: { isEquivocated, processBlock },
1418
+ };
1419
+ }
1420
+
1421
+ // Otherwise, we're good to go!
1422
+ return { result: TopicValidatorResult.Accept, obj: checkpoint, metadata: { processBlock, isEquivocated } };
1132
1423
  }
1133
1424
 
1134
1425
  /**
1135
1426
  * Process a validated checkpoint proposal.
1136
- * Extracts and processes the last block proposal (if present) first, then processes the checkpoint.
1137
- * The block callback is invoked before the checkpoint callback.
1427
+ * Note: The proposal was already added to the pool by tryAddCheckpointProposal in handleGossipedCheckpointProposal.
1138
1428
  */
1139
1429
  @trackSpan('Libp2pService.processValidCheckpointProposal', async checkpoint => ({
1140
1430
  [Attributes.SLOT_NUMBER]: checkpoint.slotNumber,
1141
1431
  [Attributes.BLOCK_ARCHIVE]: checkpoint.archive.toString(),
1142
1432
  [Attributes.P2P_ID]: await checkpoint.p2pMessageLoggingIdentifier().then(i => i.toString()),
1143
1433
  }))
1144
- private async processValidCheckpointProposal(checkpoint: CheckpointProposal, sender: PeerId) {
1434
+ protected async processValidCheckpointProposal(checkpoint: CheckpointProposalCore, sender: PeerId) {
1145
1435
  const slot = checkpoint.slotNumber;
1146
1436
  this.logger.verbose(`Received checkpoint proposal for slot ${slot} from external peer ${sender.toString()}.`, {
1147
1437
  p2pMessageIdentifier: await checkpoint.p2pMessageLoggingIdentifier(),
@@ -1150,37 +1440,14 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1150
1440
  source: sender.toString(),
1151
1441
  });
1152
1442
 
1153
- // Extract block proposal before adding to pool (pool stores them separately)
1154
- const blockProposal = checkpoint.getBlockProposal();
1155
-
1156
- // Add proposal to the pool (this extracts and stores block proposal separately)
1157
- await this.mempools.attestationPool.addCheckpointProposal(checkpoint);
1158
-
1159
- // Mark txs as non-evictable if present (from the last block)
1160
- if (checkpoint.txHashes.length > 0) {
1161
- await this.mempools.txPool.markTxsAsNonEvictable(checkpoint.txHashes);
1162
- }
1163
-
1164
- // If there was a last block proposal, invoke the block callback first for validation.
1165
- // Note: The block proposal is already stored in the pool by addCheckpointProposal.
1166
- if (blockProposal) {
1167
- const isValid = await this.blockReceivedCallback(blockProposal, sender);
1168
- if (!isValid) {
1169
- this.logger.warn(`Block proposal from checkpoint failed validation`, {
1170
- slot: slot.toString(),
1171
- archive: checkpoint.archive.toString(),
1172
- blockNumber: blockProposal.blockNumber.toString(),
1173
- });
1174
- return;
1175
- }
1176
- }
1443
+ await this.allNodesCheckpointReceivedCallback(checkpoint, sender);
1177
1444
 
1178
1445
  // Call the checkpoint received callback with the core version (without lastBlock)
1179
1446
  // to validate and potentially generate attestations
1180
- const attestations = await this.checkpointReceivedCallback(checkpoint.toCore(), sender);
1447
+ const attestations = await this.validatorCheckpointReceivedCallback(checkpoint, sender);
1181
1448
  if (attestations && attestations.length > 0) {
1182
1449
  // If the callback returned attestations, add them to the pool and propagate them
1183
- await this.mempools.attestationPool.addCheckpointAttestations(attestations);
1450
+ await this.mempools.attestationPool.addOwnCheckpointAttestations(attestations);
1184
1451
  for (const attestation of attestations) {
1185
1452
  await this.propagate(attestation);
1186
1453
  }
@@ -1207,9 +1474,9 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1207
1474
  * @returns True if the requested block transactions are valid, false otherwise.
1208
1475
  */
1209
1476
  @trackSpan('Libp2pService.validateRequestedBlockTxs', request => ({
1210
- [Attributes.BLOCK_HASH]: request.blockHash.toString(),
1477
+ [Attributes.BLOCK_ARCHIVE]: request.archiveRoot.toString(),
1211
1478
  }))
1212
- private async validateRequestedBlockTxs(
1479
+ protected async validateRequestedBlockTxs(
1213
1480
  request: BlockTxsRequest,
1214
1481
  response: BlockTxsResponse,
1215
1482
  peerId: PeerId,
@@ -1217,10 +1484,10 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1217
1484
  const requestedTxValidator = this.createRequestedTxValidator();
1218
1485
 
1219
1486
  try {
1220
- if (!response.blockHash.equals(request.blockHash)) {
1487
+ if (!response.archiveRoot.equals(request.archiveRoot)) {
1221
1488
  this.peerManager.penalizePeer(peerId, PeerErrorSeverity.MidToleranceError);
1222
1489
  throw new ValidationError(
1223
- `Received block txs for unexpected block: expected ${request.blockHash.toString()}, got ${response.blockHash.toString()}`,
1490
+ `Received block txs for unexpected archive root: expected ${request.archiveRoot.toString()}, got ${response.archiveRoot.toString()}`,
1224
1491
  );
1225
1492
  }
1226
1493
 
@@ -1250,7 +1517,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1250
1517
  }
1251
1518
 
1252
1519
  // Given proposal (should have locally), ensure returned txs are valid subset and match request indices
1253
- const proposal = await this.mempools.attestationPool.getBlockProposal(request.blockHash.toString());
1520
+ const proposal = await this.mempools.attestationPool.getBlockProposal(request.archiveRoot.toString());
1254
1521
  if (proposal) {
1255
1522
  // Build intersected indices
1256
1523
  const intersectIdx = request.txIndices.getTrueIndices().filter(i => response.txIndices.isSet(i));
@@ -1270,7 +1537,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1270
1537
  } else {
1271
1538
  // No local proposal, cannot check the membership/order of the returned txs
1272
1539
  this.logger.warn(
1273
- `Block proposal not found for block hash ${request.blockHash.toString()}; cannot validate membership/order of returned txs`,
1540
+ `Block proposal not found for archive root ${request.archiveRoot.toString()}; cannot validate membership/order of returned txs`,
1274
1541
  );
1275
1542
  return false;
1276
1543
  }
@@ -1309,7 +1576,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1309
1576
  const requested = new Set(requestedTxHash.map(h => h.toString()));
1310
1577
  const requestedTxValidator = this.createRequestedTxValidator();
1311
1578
 
1312
- //TODO: (mralj) - this is somewhat naive implementation, if single tx is invlid we consider the whole response invalid.
1579
+ //TODO: (mralj) - this is somewhat naive implementation, if single tx is invalid we consider the whole response invalid.
1313
1580
  // I think we should still extract the valid txs and return them, so that we can still use the response.
1314
1581
  try {
1315
1582
  await Promise.all(responseTx.map(tx => this.validateRequestedTx(tx, peerId, requestedTxValidator, requested)));
@@ -1325,75 +1592,13 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1325
1592
  }
1326
1593
  }
1327
1594
 
1328
- /**
1329
- * Validates a BLOCK response.
1330
- *
1331
- * If a local copy exists, enforces hash equality. If missing, rejects (no penalty) since the hash cannot be verified.
1332
- * Penalizes on block number mismatch or hash mismatch.
1333
- *
1334
- * @param requestedBlockNumber - The requested block number.
1335
- * @param responseBlock - The block returned by the peer.
1336
- * @param peerId - The peer that returned the block.
1337
- * @returns True if the response is valid, false otherwise.
1338
- */
1339
- @trackSpan('Libp2pService.validateRequestedBlock', (requestedBlockNumber, _responseBlock) => ({
1340
- [Attributes.BLOCK_NUMBER]: requestedBlockNumber.toString(),
1341
- }))
1342
- private async validateRequestedBlock(
1343
- requestedBlockNumber: Fr,
1344
- responseBlock: L2Block,
1595
+ protected async validateRequestedTx(
1596
+ tx: Tx,
1345
1597
  peerId: PeerId,
1346
- ): Promise<boolean> {
1347
- try {
1348
- const reqNum = Number(requestedBlockNumber.toString());
1349
- if (responseBlock.number !== reqNum) {
1350
- this.peerManager.penalizePeer(peerId, PeerErrorSeverity.LowToleranceError);
1351
- return false;
1352
- }
1353
-
1354
- const local = await this.archiver.getBlock(BlockNumber(reqNum));
1355
- if (!local) {
1356
- // We are missing the local block; we cannot verify the hash yet. Reject without penalizing.
1357
- // TODO: Consider extending this validator to accept an expected hash or
1358
- // performing quorum-based checks when using P2P syncing prior to L1 sync.
1359
- this.logger.warn(`Local block ${reqNum} not found; rejecting BLOCK response without hash verification`);
1360
- return false;
1361
- }
1362
- const [localHash, respHash] = await Promise.all([local.hash(), responseBlock.hash()]);
1363
- if (!localHash.equals(respHash)) {
1364
- this.peerManager.penalizePeer(peerId, PeerErrorSeverity.MidToleranceError);
1365
- return false;
1366
- }
1367
-
1368
- return true;
1369
- } catch (e) {
1370
- this.logger.warn(`Error validating requested block`, e);
1371
- return false;
1372
- }
1373
- }
1374
-
1375
- private createRequestedTxValidator(): TxValidator {
1376
- return new AggregateTxValidator(
1377
- new DataTxValidator(),
1378
- new SizeTxValidator(),
1379
- new MetadataTxValidator({
1380
- l1ChainId: new Fr(this.config.l1ChainId),
1381
- rollupVersion: new Fr(this.config.rollupVersion),
1382
- protocolContractsHash,
1383
- vkTreeRoot: getVKTreeRoot(),
1384
- }),
1385
- new TxProofValidator(this.proofVerifier),
1386
- );
1387
- }
1388
-
1389
- private async validateRequestedTx(tx: Tx, peerId: PeerId, txValidator: TxValidator, requested?: Set<`0x${string}`>) {
1598
+ txValidator: TxValidator,
1599
+ requested?: Set<`0x${string}`>,
1600
+ ) {
1390
1601
  const penalize = (severity: PeerErrorSeverity) => this.peerManager.penalizePeer(peerId, severity);
1391
-
1392
- if (!(await tx.validateTxHash())) {
1393
- penalize(PeerErrorSeverity.MidToleranceError);
1394
- throw new ValidationError(`Received tx with invalid hash ${tx.getTxHash().toString()}.`);
1395
- }
1396
-
1397
1602
  if (requested && !requested.has(tx.getTxHash().toString())) {
1398
1603
  penalize(PeerErrorSeverity.MidToleranceError);
1399
1604
  throw new ValidationError(`Received tx with hash ${tx.getTxHash().toString()} that was not requested.`);
@@ -1406,101 +1611,89 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1406
1611
  }
1407
1612
  }
1408
1613
 
1409
- @trackSpan('Libp2pService.validatePropagatedTx', tx => ({
1410
- [Attributes.TX_HASH]: tx.getTxHash().toString(),
1411
- }))
1412
- private async validatePropagatedTx(tx: Tx, peerId: PeerId): Promise<boolean> {
1413
- const currentBlockNumber = await this.archiver.getBlockNumber();
1414
-
1415
- // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
1416
- const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
1417
- const messageValidators = await this.createMessageValidators(currentBlockNumber, nextSlotTimestamp);
1418
-
1419
- for (const validator of messageValidators) {
1420
- const outcome = await this.runValidations(tx, validator);
1421
-
1422
- if (outcome.allPassed) {
1423
- continue;
1424
- }
1425
- const { name } = outcome.failure;
1426
- let { severity } = outcome.failure;
1427
-
1428
- // Double spend validator has a special case handler
1429
- if (name === 'doubleSpendValidator') {
1430
- const txBlockNumber = BlockNumber(currentBlockNumber + 1); // tx is expected to be in the next block
1431
- severity = await this.handleDoubleSpendFailure(tx, txBlockNumber);
1432
- }
1433
-
1434
- this.peerManager.penalizePeer(peerId, severity);
1435
- return false;
1436
- }
1437
- return true;
1614
+ protected createRequestedTxValidator(): TxValidator {
1615
+ return createTxValidatorForReqResponseReceivedTxs(this.proofVerifier, {
1616
+ l1ChainId: this.config.l1ChainId,
1617
+ rollupVersion: this.config.rollupVersion,
1618
+ });
1438
1619
  }
1439
1620
 
1440
- private async getGasFees(blockNumber: BlockNumber): Promise<GasFees> {
1441
- if (blockNumber === this.feesCache?.blockNumber) {
1442
- return this.feesCache.gasFees;
1443
- }
1444
-
1445
- const header = await this.archiver.getBlockHeader(blockNumber);
1446
- const gasFees = header?.globalVariables.gasFees ?? GasFees.empty();
1447
- this.feesCache = { blockNumber, gasFees };
1448
- return gasFees;
1621
+ private getGasFees(): Promise<GasFees> {
1622
+ return this.blockMinFeesProvider.getCurrentMinFees();
1449
1623
  }
1450
1624
 
1451
- public async validate(txs: Tx[]): Promise<void> {
1452
- const currentBlockNumber = await this.archiver.getBlockNumber();
1625
+ /**
1626
+ * Get the BatchTxRequesterLibP2PService dependencies for creating BatchTxRequester instances
1627
+ */
1628
+ public getBatchTxRequesterService(): BatchTxRequesterLibP2PService {
1629
+ return {
1630
+ reqResp: this.reqresp,
1631
+ connectionSampler: this.reqresp.getConnectionSampler(),
1632
+ txValidatorConfig: {
1633
+ l1ChainId: this.config.l1ChainId,
1634
+ rollupVersion: this.config.rollupVersion,
1635
+ proofVerifier: this.proofVerifier,
1636
+ },
1637
+ peerScoring: this.peerManager,
1638
+ };
1639
+ }
1453
1640
 
1454
- // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
1455
- const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
1456
- const messageValidators = await this.createMessageValidators(currentBlockNumber, nextSlotTimestamp);
1641
+ public async validateTxsReceivedInBlockProposal(txs: Tx[]): Promise<void> {
1642
+ const validator = createTxValidatorForBlockProposalReceivedTxs(
1643
+ this.proofVerifier,
1644
+ { l1ChainId: this.config.l1ChainId, rollupVersion: this.config.rollupVersion },
1645
+ this.logger.getBindings(),
1646
+ );
1457
1647
 
1458
- await Promise.all(
1648
+ const results = await Promise.all(
1459
1649
  txs.map(async tx => {
1460
- for (const validator of messageValidators) {
1461
- const outcome = await this.runValidations(tx, validator);
1462
- if (!outcome.allPassed) {
1463
- throw new Error('Invalid tx detected', { cause: { outcome } });
1464
- }
1465
- }
1650
+ const result = await validator.validateTx(tx);
1651
+ return result.result !== 'invalid';
1466
1652
  }),
1467
1653
  );
1654
+ if (results.some(value => value === false)) {
1655
+ throw new Error('Invalid tx detected');
1656
+ }
1468
1657
  }
1469
1658
 
1470
- /**
1471
- * Create message validators for the given block number and timestamp.
1472
- *
1473
- * Each validator is a pair of a validator and a severity.
1474
- * If a validator fails, the peer is penalized with the severity of the validator.
1475
- *
1476
- * @param currentBlockNumber - The current synced block number.
1477
- * @param nextSlotTimestamp - The timestamp of the next slot (used to validate txs are not expired).
1478
- * @returns The message validators.
1479
- */
1480
- private async createMessageValidators(
1659
+ /** Creates the first stage (fast) validators for gossiped transactions. */
1660
+ protected async createFirstStageMessageValidators(
1481
1661
  currentBlockNumber: BlockNumber,
1482
1662
  nextSlotTimestamp: UInt64,
1483
- ): Promise<Record<string, MessageValidator>[]> {
1484
- const gasFees = await this.getGasFees(currentBlockNumber);
1485
- const allowedInSetup = this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
1486
-
1487
- const blockNumberInWhichTheTxIsConsideredToBeIncluded = BlockNumber(currentBlockNumber + 1);
1488
-
1489
- return createTxMessageValidators(
1663
+ ): Promise<Record<string, TransactionValidator>> {
1664
+ const gasFees = await this.getGasFees();
1665
+ const allowedInSetup = [
1666
+ ...(await getDefaultAllowedSetupFunctions()),
1667
+ ...(this.config.txPublicSetupAllowListExtend ?? []),
1668
+ ];
1669
+ const blockNumber = BlockNumber(currentBlockNumber + 1);
1670
+ const l1Constants = await this.archiver.getL1Constants();
1671
+
1672
+ return createFirstStageTxValidationsForGossipedTransactions(
1490
1673
  nextSlotTimestamp,
1491
- blockNumberInWhichTheTxIsConsideredToBeIncluded,
1674
+ blockNumber,
1492
1675
  this.worldStateSynchronizer,
1493
1676
  gasFees,
1494
1677
  this.config.l1ChainId,
1495
1678
  this.config.rollupVersion,
1496
1679
  protocolContractsHash,
1497
1680
  this.archiver,
1498
- this.proofVerifier,
1499
1681
  !this.config.disableTransactions,
1500
1682
  allowedInSetup,
1683
+ this.logger.getBindings(),
1684
+ {
1685
+ rollupManaLimit: l1Constants.rollupManaLimit,
1686
+ maxBlockL2Gas: this.config.validateMaxL2BlockGas,
1687
+ maxBlockDAGas: this.config.validateMaxDABlockGas,
1688
+ },
1501
1689
  );
1502
1690
  }
1503
1691
 
1692
+ /** Creates the second stage (expensive proof verification) validators for gossiped transactions. */
1693
+ protected createSecondStageMessageValidators(): Record<string, TransactionValidator> {
1694
+ return createSecondStageTxValidationsForGossipedTransactions(this.proofVerifier, this.logger.getBindings());
1695
+ }
1696
+
1504
1697
  /**
1505
1698
  * Run validations on a tx.
1506
1699
  * @param tx - The tx to validate.
@@ -1509,7 +1702,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1509
1702
  */
1510
1703
  private async runValidations(
1511
1704
  tx: Tx,
1512
- messageValidators: Record<string, MessageValidator>,
1705
+ messageValidators: Record<string, TransactionValidator>,
1513
1706
  ): Promise<ValidationOutcome> {
1514
1707
  const validationPromises = Object.entries(messageValidators).map(async ([name, { validator, severity }]) => {
1515
1708
  const { result } = await validator.validateTx(tx);
@@ -1518,8 +1711,10 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1518
1711
 
1519
1712
  // A promise that resolves when all validations have been run
1520
1713
  const allValidations = await Promise.all(validationPromises);
1521
- const failed = allValidations.find(x => !x.isValid);
1522
- if (failed) {
1714
+ const failures = allValidations.filter(x => !x.isValid);
1715
+ if (failures.length > 0) {
1716
+ // Pick the most severe failure (lowest tolerance = harshest penalty)
1717
+ const failed = maxBy(failures, f => PeerErrorSeverityByHarshness.indexOf(f.severity))!;
1523
1718
  return {
1524
1719
  allPassed: false,
1525
1720
  failure: {
@@ -1551,15 +1746,18 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1551
1746
  return PeerErrorSeverity.HighToleranceError;
1552
1747
  }
1553
1748
 
1554
- const snapshotValidator = new DoubleSpendTxValidator({
1555
- nullifiersExist: async (nullifiers: Buffer[]) => {
1556
- const merkleTree = this.worldStateSynchronizer.getSnapshot(
1557
- BlockNumber(blockNumber - this.config.doubleSpendSeverePeerPenaltyWindow),
1558
- );
1559
- const indices = await merkleTree.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, nullifiers);
1560
- return indices.map(index => index !== undefined);
1749
+ const snapshotValidator = new DoubleSpendTxValidator(
1750
+ {
1751
+ nullifiersExist: async (nullifiers: Buffer[]) => {
1752
+ const merkleTree = this.worldStateSynchronizer.getSnapshot(
1753
+ BlockNumber(blockNumber - this.config.doubleSpendSeverePeerPenaltyWindow),
1754
+ );
1755
+ const indices = await merkleTree.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, nullifiers);
1756
+ return indices.map(index => index !== undefined);
1757
+ },
1561
1758
  },
1562
- });
1759
+ this.logger.getBindings(),
1760
+ );
1563
1761
 
1564
1762
  const validSnapshot = await snapshotValidator.validateTx(tx);
1565
1763
  if (validSnapshot.result !== 'valid') {
@@ -1569,74 +1767,6 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
1569
1767
  return PeerErrorSeverity.HighToleranceError;
1570
1768
  }
1571
1769
 
1572
- /**
1573
- * Validate a checkpoint attestation.
1574
- *
1575
- * @param attestation - The checkpoint attestation to validate.
1576
- * @returns True if the checkpoint attestation is valid, false otherwise.
1577
- */
1578
- @trackSpan('Libp2pService.validateCheckpointAttestation', async (_, attestation) => ({
1579
- [Attributes.SLOT_NUMBER]: attestation.payload.header.slotNumber,
1580
- [Attributes.BLOCK_ARCHIVE]: attestation.archive.toString(),
1581
- [Attributes.P2P_ID]: await attestation.p2pMessageLoggingIdentifier().then(i => i.toString()),
1582
- }))
1583
- public async validateCheckpointAttestation(
1584
- peerId: PeerId,
1585
- attestation: CheckpointAttestation,
1586
- ): Promise<P2PValidationResult> {
1587
- const result = await this.checkpointAttestationValidator.validate(attestation);
1588
-
1589
- if (result.result === 'reject') {
1590
- this.logger.debug(`Penalizing peer ${peerId} for checkpoint attestation validation failure`);
1591
- this.peerManager.penalizePeer(peerId, result.severity);
1592
- }
1593
-
1594
- return result;
1595
- }
1596
-
1597
- /**
1598
- * Validate a block proposal.
1599
- *
1600
- * @param block - The block proposal to validate.
1601
- * @returns True if the block proposal is valid, false otherwise.
1602
- */
1603
- @trackSpan('Libp2pService.validateBlockProposal', (_peerId, block) => ({
1604
- [Attributes.SLOT_NUMBER]: block.slotNumber.toString(),
1605
- }))
1606
- public async validateBlockProposal(peerId: PeerId, block: BlockProposal): Promise<P2PValidationResult> {
1607
- const result = await this.blockProposalValidator.validate(block);
1608
-
1609
- if (result.result === 'reject') {
1610
- this.logger.debug(`Penalizing peer ${peerId} for block proposal validation failure`);
1611
- this.peerManager.penalizePeer(peerId, result.severity);
1612
- }
1613
-
1614
- return result;
1615
- }
1616
-
1617
- /**
1618
- * Validate a checkpoint proposal.
1619
- *
1620
- * @param checkpoint - The checkpoint proposal to validate.
1621
- * @returns True if the checkpoint proposal is valid, false otherwise.
1622
- */
1623
- @trackSpan('Libp2pService.validateCheckpointProposal', (_peerId, checkpoint) => ({
1624
- [Attributes.SLOT_NUMBER]: checkpoint.slotNumber.toString(),
1625
- }))
1626
- public async validateCheckpointProposal(
1627
- peerId: PeerId,
1628
- checkpoint: CheckpointProposal,
1629
- ): Promise<P2PValidationResult> {
1630
- const result = await this.checkpointProposalValidator.validate(checkpoint);
1631
-
1632
- if (result.result === 'reject') {
1633
- this.logger.debug(`Penalizing peer ${peerId} for checkpoint proposal validation failure`);
1634
- this.peerManager.penalizePeer(peerId, result.severity);
1635
- }
1636
-
1637
- return result;
1638
- }
1639
-
1640
1770
  public getPeerScore(peerId: PeerId): number {
1641
1771
  return this.node.services.pubsub.score.score(peerId.toString());
1642
1772
  }