@aztec/p2p 0.0.1-commit.b2a5d0dd1 → 0.0.1-commit.b3d3157a

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 (243) hide show
  1. package/dest/bootstrap/bootstrap.d.ts +1 -1
  2. package/dest/bootstrap/bootstrap.d.ts.map +1 -1
  3. package/dest/bootstrap/bootstrap.js +9 -1
  4. package/dest/client/factory.d.ts +3 -3
  5. package/dest/client/factory.d.ts.map +1 -1
  6. package/dest/client/factory.js +12 -9
  7. package/dest/client/interface.d.ts +6 -4
  8. package/dest/client/interface.d.ts.map +1 -1
  9. package/dest/client/p2p_client.d.ts +11 -8
  10. package/dest/client/p2p_client.d.ts.map +1 -1
  11. package/dest/client/p2p_client.js +53 -17
  12. package/dest/config.d.ts +43 -17
  13. package/dest/config.d.ts.map +1 -1
  14. package/dest/config.js +48 -8
  15. package/dest/errors/reqresp.error.d.ts +1 -20
  16. package/dest/errors/reqresp.error.d.ts.map +1 -1
  17. package/dest/errors/reqresp.error.js +0 -21
  18. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +93 -55
  19. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  20. package/dest/mem_pools/attestation_pool/attestation_pool.js +258 -195
  21. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +1 -1
  22. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  23. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +175 -59
  24. package/dest/mem_pools/attestation_pool/mocks.d.ts +1 -1
  25. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  26. package/dest/mem_pools/attestation_pool/mocks.js +6 -4
  27. package/dest/mem_pools/instrumentation.d.ts +1 -1
  28. package/dest/mem_pools/instrumentation.d.ts.map +1 -1
  29. package/dest/mem_pools/instrumentation.js +17 -1
  30. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.d.ts +1 -5
  31. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.d.ts.map +1 -1
  32. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.js +1 -7
  33. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +1 -1
  34. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
  35. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +73 -83
  36. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +5 -2
  37. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
  38. package/dest/msg_validators/attestation_validator/attestation_validator.js +18 -2
  39. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +5 -3
  40. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -1
  41. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.js +3 -4
  42. package/dest/msg_validators/clock_tolerance.d.ts +1 -1
  43. package/dest/msg_validators/clock_tolerance.d.ts.map +1 -1
  44. package/dest/msg_validators/clock_tolerance.js +13 -11
  45. package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts +5 -2
  46. package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts.map +1 -1
  47. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts +5 -2
  48. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts.map +1 -1
  49. package/dest/msg_validators/proposal_validator/proposal_validator.d.ts +8 -2
  50. package/dest/msg_validators/proposal_validator/proposal_validator.d.ts.map +1 -1
  51. package/dest/msg_validators/proposal_validator/proposal_validator.js +28 -2
  52. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +1 -1
  53. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
  54. package/dest/msg_validators/tx_validator/aggregate_tx_validator.js +8 -15
  55. package/dest/msg_validators/tx_validator/allowed_public_setup.js +3 -3
  56. package/dest/msg_validators/tx_validator/cached_tx_validator.d.ts +15 -0
  57. package/dest/msg_validators/tx_validator/cached_tx_validator.d.ts.map +1 -0
  58. package/dest/msg_validators/tx_validator/cached_tx_validator.js +19 -0
  59. package/dest/msg_validators/tx_validator/data_validator.d.ts +2 -1
  60. package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
  61. package/dest/msg_validators/tx_validator/data_validator.js +1 -0
  62. package/dest/msg_validators/tx_validator/factory.d.ts +5 -4
  63. package/dest/msg_validators/tx_validator/factory.d.ts.map +1 -1
  64. package/dest/msg_validators/tx_validator/factory.js +11 -7
  65. package/dest/msg_validators/tx_validator/index.d.ts +3 -1
  66. package/dest/msg_validators/tx_validator/index.d.ts.map +1 -1
  67. package/dest/msg_validators/tx_validator/index.js +2 -0
  68. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts +2 -1
  69. package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts.map +1 -1
  70. package/dest/msg_validators/tx_validator/tx_proof_validator.js +2 -0
  71. package/dest/msg_validators/tx_validator/tx_validation_cache.d.ts +48 -0
  72. package/dest/msg_validators/tx_validator/tx_validation_cache.d.ts.map +1 -0
  73. package/dest/msg_validators/tx_validator/tx_validation_cache.js +68 -0
  74. package/dest/services/discv5/discV5_service.d.ts +2 -1
  75. package/dest/services/discv5/discV5_service.d.ts.map +1 -1
  76. package/dest/services/discv5/discV5_service.js +35 -8
  77. package/dest/services/dummy_service.d.ts +10 -17
  78. package/dest/services/dummy_service.d.ts.map +1 -1
  79. package/dest/services/dummy_service.js +9 -18
  80. package/dest/services/libp2p/libp2p_service.d.ts +19 -21
  81. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  82. package/dest/services/libp2p/libp2p_service.js +147 -148
  83. package/dest/services/peer-manager/peer_manager.d.ts +1 -1
  84. package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
  85. package/dest/services/peer-manager/peer_manager.js +1 -0
  86. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts +1 -8
  87. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts.map +1 -1
  88. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.js +35 -44
  89. package/dest/services/reqresp/batch-tx-requester/interface.d.ts +8 -5
  90. package/dest/services/reqresp/batch-tx-requester/interface.d.ts.map +1 -1
  91. package/dest/services/reqresp/batch-tx-requester/tx_validator.d.ts +5 -14
  92. package/dest/services/reqresp/batch-tx-requester/tx_validator.d.ts.map +1 -1
  93. package/dest/services/reqresp/batch-tx-requester/tx_validator.js +6 -20
  94. package/dest/services/reqresp/interface.d.ts +3 -10
  95. package/dest/services/reqresp/interface.d.ts.map +1 -1
  96. package/dest/services/reqresp/interface.js +0 -9
  97. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts +4 -3
  98. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts.map +1 -1
  99. package/dest/services/reqresp/protocols/block_txs/bitvector.js +3 -2
  100. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +1 -1
  101. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -1
  102. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.js +31 -28
  103. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +12 -7
  104. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
  105. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +42 -24
  106. package/dest/services/reqresp/reqresp.d.ts +5 -29
  107. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  108. package/dest/services/reqresp/reqresp.js +16 -206
  109. package/dest/services/service.d.ts +7 -13
  110. package/dest/services/service.d.ts.map +1 -1
  111. package/dest/services/tx_collection/config.d.ts +2 -23
  112. package/dest/services/tx_collection/config.d.ts.map +1 -1
  113. package/dest/services/tx_collection/config.js +2 -55
  114. package/dest/services/tx_collection/file_store_tx_collection.d.ts +12 -28
  115. package/dest/services/tx_collection/file_store_tx_collection.d.ts.map +1 -1
  116. package/dest/services/tx_collection/file_store_tx_collection.js +43 -83
  117. package/dest/services/tx_collection/index.d.ts +2 -3
  118. package/dest/services/tx_collection/index.d.ts.map +1 -1
  119. package/dest/services/tx_collection/index.js +0 -1
  120. package/dest/services/tx_collection/instrumentation.d.ts +1 -1
  121. package/dest/services/tx_collection/instrumentation.d.ts.map +1 -1
  122. package/dest/services/tx_collection/instrumentation.js +0 -2
  123. package/dest/services/tx_collection/tx_collection.d.ts +35 -51
  124. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
  125. package/dest/services/tx_collection/tx_collection.js +275 -119
  126. package/dest/services/tx_collection/tx_collection_sink.d.ts +1 -1
  127. package/dest/services/tx_collection/tx_collection_sink.js +2 -2
  128. package/dest/services/tx_file_store/tx_file_store.d.ts +1 -3
  129. package/dest/services/tx_file_store/tx_file_store.d.ts.map +1 -1
  130. package/dest/services/tx_file_store/tx_file_store.js +4 -14
  131. package/dest/services/tx_provider.d.ts +3 -1
  132. package/dest/services/tx_provider.d.ts.map +1 -1
  133. package/dest/services/tx_provider.js +3 -0
  134. package/dest/test-helpers/make-test-p2p-clients.d.ts +1 -1
  135. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  136. package/dest/test-helpers/make-test-p2p-clients.js +1 -1
  137. package/dest/test-helpers/mock-pubsub.d.ts +13 -7
  138. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  139. package/dest/test-helpers/mock-pubsub.js +12 -37
  140. package/dest/test-helpers/reqresp-nodes.d.ts +4 -5
  141. package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
  142. package/dest/test-helpers/reqresp-nodes.js +11 -15
  143. package/dest/test-helpers/test_tx_provider.d.ts +3 -1
  144. package/dest/test-helpers/test_tx_provider.d.ts.map +1 -1
  145. package/dest/test-helpers/test_tx_provider.js +3 -0
  146. package/dest/test-helpers/testbench-utils.d.ts +12 -14
  147. package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
  148. package/dest/test-helpers/testbench-utils.js +19 -14
  149. package/dest/testbench/p2p_client_testbench_worker.d.ts +3 -5
  150. package/dest/testbench/p2p_client_testbench_worker.d.ts.map +1 -1
  151. package/dest/testbench/p2p_client_testbench_worker.js +13 -24
  152. package/dest/testbench/worker_client_manager.d.ts +3 -6
  153. package/dest/testbench/worker_client_manager.d.ts.map +1 -1
  154. package/dest/testbench/worker_client_manager.js +2 -8
  155. package/dest/util.d.ts +11 -6
  156. package/dest/util.d.ts.map +1 -1
  157. package/dest/util.js +35 -14
  158. package/dest/versioning.d.ts +3 -6
  159. package/dest/versioning.d.ts.map +1 -1
  160. package/dest/versioning.js +3 -24
  161. package/package.json +15 -14
  162. package/src/bootstrap/bootstrap.ts +9 -1
  163. package/src/client/factory.ts +20 -5
  164. package/src/client/interface.ts +6 -10
  165. package/src/client/p2p_client.ts +61 -36
  166. package/src/client/test/{tx_proposal_collector/README.md → p2p_client.batch_tx_requester.bench.README.md} +23 -53
  167. package/src/config.ts +94 -6
  168. package/src/errors/reqresp.error.ts +0 -25
  169. package/src/mem_pools/attestation_pool/attestation_pool.ts +309 -238
  170. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +198 -62
  171. package/src/mem_pools/attestation_pool/mocks.ts +13 -8
  172. package/src/mem_pools/instrumentation.ts +5 -1
  173. package/src/mem_pools/tx_pool_v2/archive/tx_archive.ts +1 -9
  174. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +73 -90
  175. package/src/msg_validators/attestation_validator/attestation_validator.ts +21 -1
  176. package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +11 -7
  177. package/src/msg_validators/clock_tolerance.ts +13 -12
  178. package/src/msg_validators/proposal_validator/block_proposal_validator.ts +9 -2
  179. package/src/msg_validators/proposal_validator/checkpoint_proposal_validator.ts +14 -2
  180. package/src/msg_validators/proposal_validator/proposal_validator.ts +39 -2
  181. package/src/msg_validators/tx_validator/aggregate_tx_validator.ts +3 -12
  182. package/src/msg_validators/tx_validator/allowed_public_setup.ts +3 -3
  183. package/src/msg_validators/tx_validator/cached_tx_validator.ts +31 -0
  184. package/src/msg_validators/tx_validator/data_validator.ts +2 -0
  185. package/src/msg_validators/tx_validator/factory.ts +15 -6
  186. package/src/msg_validators/tx_validator/index.ts +2 -0
  187. package/src/msg_validators/tx_validator/tx_proof_validator.ts +2 -0
  188. package/src/msg_validators/tx_validator/tx_validation_cache.ts +101 -0
  189. package/src/services/discv5/discV5_service.ts +38 -5
  190. package/src/services/dummy_service.ts +12 -43
  191. package/src/services/libp2p/libp2p_service.ts +169 -167
  192. package/src/services/peer-manager/peer_manager.ts +1 -0
  193. package/src/services/reqresp/README.md +0 -14
  194. package/src/services/reqresp/batch-tx-requester/batch_tx_requester.ts +39 -52
  195. package/src/services/reqresp/batch-tx-requester/interface.ts +11 -4
  196. package/src/services/reqresp/batch-tx-requester/tx_validator.ts +12 -25
  197. package/src/services/reqresp/interface.ts +2 -38
  198. package/src/services/reqresp/protocols/block_txs/bitvector.ts +3 -2
  199. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +34 -30
  200. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +47 -20
  201. package/src/services/reqresp/reqresp.ts +11 -251
  202. package/src/services/service.ts +9 -30
  203. package/src/services/tx_collection/config.ts +3 -80
  204. package/src/services/tx_collection/file_store_tx_collection.ts +54 -103
  205. package/src/services/tx_collection/index.ts +1 -6
  206. package/src/services/tx_collection/instrumentation.ts +1 -7
  207. package/src/services/tx_collection/tx_collection.ts +330 -173
  208. package/src/services/tx_collection/tx_collection_sink.ts +2 -2
  209. package/src/services/tx_file_store/tx_file_store.ts +5 -17
  210. package/src/services/tx_provider.ts +5 -0
  211. package/src/test-helpers/make-test-p2p-clients.ts +1 -0
  212. package/src/test-helpers/mock-pubsub.ts +17 -60
  213. package/src/test-helpers/reqresp-nodes.ts +8 -20
  214. package/src/test-helpers/test_tx_provider.ts +5 -0
  215. package/src/test-helpers/testbench-utils.ts +23 -27
  216. package/src/testbench/p2p_client_testbench_worker.ts +16 -42
  217. package/src/testbench/worker_client_manager.ts +4 -19
  218. package/src/util.ts +32 -17
  219. package/src/versioning.ts +3 -33
  220. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.d.ts +0 -2
  221. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.d.ts.map +0 -1
  222. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.js +0 -318
  223. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.d.ts +0 -73
  224. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.d.ts.map +0 -1
  225. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.js +0 -8
  226. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts +0 -64
  227. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts.map +0 -1
  228. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.js +0 -151
  229. package/dest/services/tx_collection/fast_tx_collection.d.ts +0 -51
  230. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +0 -1
  231. package/dest/services/tx_collection/fast_tx_collection.js +0 -311
  232. package/dest/services/tx_collection/proposal_tx_collector.d.ts +0 -48
  233. package/dest/services/tx_collection/proposal_tx_collector.d.ts.map +0 -1
  234. package/dest/services/tx_collection/proposal_tx_collector.js +0 -50
  235. package/dest/services/tx_collection/slow_tx_collection.d.ts +0 -57
  236. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +0 -1
  237. package/dest/services/tx_collection/slow_tx_collection.js +0 -211
  238. package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker.ts +0 -357
  239. package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker_protocol.ts +0 -43
  240. package/src/services/reqresp/connection-sampler/batch_connection_sampler.ts +0 -161
  241. package/src/services/tx_collection/fast_tx_collection.ts +0 -361
  242. package/src/services/tx_collection/proposal_tx_collector.ts +0 -108
  243. package/src/services/tx_collection/slow_tx_collection.ts +0 -266
@@ -53,6 +53,7 @@ import type { TxMetaData } from '../../mem_pools/tx_pool_v2/tx_metadata.js';
53
53
  import { AggregateTxValidator } from './aggregate_tx_validator.js';
54
54
  import { ArchiveCache } from './archive_cache.js';
55
55
  import { type ArchiveSource, BlockHeaderTxValidator } from './block_header_validator.js';
56
+ import { CachedTxValidator } from './cached_tx_validator.js';
56
57
  import { ContractInstanceTxValidator } from './contract_instance_validator.js';
57
58
  import { DataTxValidator } from './data_validator.js';
58
59
  import { DoubleSpendTxValidator, type NullifierSource } from './double_spend_validator.js';
@@ -64,6 +65,7 @@ import { SizeTxValidator } from './size_validator.js';
64
65
  import { TimestampTxValidator } from './timestamp_validator.js';
65
66
  import { TxPermittedValidator } from './tx_permitted_validator.js';
66
67
  import { TxProofValidator } from './tx_proof_validator.js';
68
+ import { TxValidationCache } from './tx_validation_cache.js';
67
69
 
68
70
  /**
69
71
  * A validator paired with a peer penalty severity.
@@ -210,8 +212,9 @@ function createTxValidatorForMinimumTxIntegrityChecks(
210
212
  rollupVersion: number;
211
213
  },
212
214
  bindings?: LoggerBindings,
215
+ cache?: TxValidationCache,
213
216
  ): TxValidator {
214
- return new AggregateTxValidator(
217
+ const aggregate = new AggregateTxValidator(
215
218
  new MetadataTxValidator(
216
219
  {
217
220
  l1ChainId: new Fr(l1ChainId),
@@ -222,10 +225,14 @@ function createTxValidatorForMinimumTxIntegrityChecks(
222
225
  bindings,
223
226
  ),
224
227
  new SizeTxValidator(bindings),
225
- new DataTxValidator(bindings),
228
+ CachedTxValidator.new(new DataTxValidator(bindings), cache),
226
229
  new ContractInstanceTxValidator(bindings),
227
- new TxProofValidator(verifier, bindings),
230
+ CachedTxValidator.new(new TxProofValidator(verifier, bindings), cache),
228
231
  );
232
+
233
+ // This validator is not state-dependent so we can cache it.
234
+ const identifier = Symbol('TxValidatorForMinimumTxIntegrityChecks');
235
+ return CachedTxValidator.newWithIdentifier(aggregate, identifier, cache);
229
236
  }
230
237
 
231
238
  /**
@@ -234,7 +241,7 @@ function createTxValidatorForMinimumTxIntegrityChecks(
234
241
  * are invalid against current state. State-dependent checks happen when the tx
235
242
  * enters the pending pool or during block building.
236
243
  */
237
- export function createTxValidatorForReqResponseReceivedTxs(
244
+ export function createTxValidatorForOnDemandReceivedTxs(
238
245
  verifier: ClientProtocolCircuitVerifier,
239
246
  {
240
247
  l1ChainId,
@@ -244,8 +251,9 @@ export function createTxValidatorForReqResponseReceivedTxs(
244
251
  rollupVersion: number;
245
252
  },
246
253
  bindings?: LoggerBindings,
254
+ cache?: TxValidationCache,
247
255
  ): TxValidator {
248
- return createTxValidatorForMinimumTxIntegrityChecks(verifier, { l1ChainId, rollupVersion }, bindings);
256
+ return createTxValidatorForMinimumTxIntegrityChecks(verifier, { l1ChainId, rollupVersion }, bindings, cache);
249
257
  }
250
258
 
251
259
  /**
@@ -263,8 +271,9 @@ export function createTxValidatorForBlockProposalReceivedTxs(
263
271
  rollupVersion: number;
264
272
  },
265
273
  bindings?: LoggerBindings,
274
+ cache?: TxValidationCache,
266
275
  ): TxValidator {
267
- return createTxValidatorForMinimumTxIntegrityChecks(verifier, { l1ChainId, rollupVersion }, bindings);
276
+ return createTxValidatorForMinimumTxIntegrityChecks(verifier, { l1ChainId, rollupVersion }, bindings, cache);
268
277
  }
269
278
 
270
279
  /**
@@ -14,3 +14,5 @@ export * from './tx_permitted_validator.js';
14
14
  export * from './timestamp_validator.js';
15
15
  export * from './size_validator.js';
16
16
  export * from './factory.js';
17
+ export * from './tx_validation_cache.js';
18
+ export * from './cached_tx_validator.js';
@@ -3,6 +3,8 @@ import type { ClientProtocolCircuitVerifier } from '@aztec/stdlib/interfaces/ser
3
3
  import { TX_ERROR_INVALID_PROOF, Tx, type TxValidationResult, type TxValidator } from '@aztec/stdlib/tx';
4
4
 
5
5
  export class TxProofValidator implements TxValidator<Tx> {
6
+ public readonly identifier: symbol = Symbol('TxProofValidator');
7
+
6
8
  #log: Logger;
7
9
 
8
10
  constructor(
@@ -0,0 +1,101 @@
1
+ import { LruMap } from '@aztec/foundation/collection';
2
+ import { type Logger, createLogger } from '@aztec/foundation/log';
3
+ import type { Tx, TxValidationResult } from '@aztec/stdlib/tx';
4
+
5
+ import { createHash } from 'node:crypto';
6
+
7
+ /**
8
+ * Minimal interface consumed by {@link CachedTxValidator}.
9
+ * Keeping the dependency on an interface lets callers (and tests) substitute any cache implementation.
10
+ */
11
+ export interface ITxValidationCache {
12
+ /** Returns the cached promise if present, otherwise calls `validate`, caches its promise, and returns it. */
13
+ getOrValidate(
14
+ validatorSymbol: symbol,
15
+ tx: Tx,
16
+ validate: () => Promise<TxValidationResult>,
17
+ ): Promise<TxValidationResult>;
18
+ }
19
+
20
+ /**
21
+ * Caches per-validator tx validation results to avoid redundant work across repeated validation calls.
22
+ *
23
+ * The cache key is composed from the validator symbol and tx hash, ensuring results are
24
+ * scoped to the specific validator that produced them.
25
+ *
26
+ * Promises are stored before they are awaited, so concurrent calls for the same pair share
27
+ * a single in-flight validation rather than launching duplicate work.
28
+ *
29
+ * Entries are evicted in least-recently-used order once the cache reaches `maxSize`.
30
+ */
31
+ export class TxValidationCache implements ITxValidationCache {
32
+ #log: Logger;
33
+
34
+ private readonly entries: LruMap<string, Promise<TxValidationResult>>;
35
+ // Remember hashes for known Tx object references to skip rehashing on subsequent lookups.
36
+ // WeakMap holds keys weakly, so an entry doesn't keep the Tx alive once nothing else references it.
37
+ private readonly txHashesCache: WeakMap<Tx, string> = new WeakMap();
38
+
39
+ constructor(maxSize: number) {
40
+ this.entries = new LruMap(maxSize);
41
+ this.#log = createLogger('p2p:tx_validation_cache');
42
+ }
43
+
44
+ /**
45
+ * Computes the cache key scoping a validation result to a specific validator and tx.
46
+ *
47
+ * @param validatorSymbol - The symbol of the validator.
48
+ * @param tx - The tx to compute the key for.
49
+ * @returns The cache key.
50
+ *
51
+ * Note: the key should NOT use the tx.hash because it can't be trusted at this point.
52
+ */
53
+ public key(validatorSymbol: symbol, tx: Tx): string {
54
+ // Serializing the tx dominates cost (~5 ms). If we have already hashed this object, skip toBuffer + hash.
55
+ let hash = this.txHashesCache.get(tx);
56
+ if (hash === undefined) {
57
+ hash = createHash('sha256').update(tx.toBuffer()).digest('hex');
58
+ this.txHashesCache.set(tx, hash);
59
+ }
60
+ return `${Symbol.keyFor(validatorSymbol) ?? validatorSymbol.toString()}:${hash}`;
61
+ }
62
+
63
+ /** Returns the cached promise for the given key, or undefined if not cached. Refreshes recency. */
64
+ public get(key: string): Promise<TxValidationResult> | undefined {
65
+ return this.entries.get(key);
66
+ }
67
+
68
+ /** Stores a validation promise under the given key, evicting the LRU entry if at capacity. */
69
+ public set(key: string, result: Promise<TxValidationResult>): void {
70
+ this.entries.set(key, result);
71
+ }
72
+
73
+ /** Removes the cached validation promise for the given key. */
74
+ public delete(key: string): void {
75
+ this.entries.delete(key);
76
+ }
77
+
78
+ /**
79
+ * Returns the cached promise if present, otherwise calls `validate`, stores its promise
80
+ * immediately (before awaiting), and returns it.
81
+ */
82
+ public getOrValidate(
83
+ validatorSymbol: symbol,
84
+ tx: Tx,
85
+ validate: () => Promise<TxValidationResult>,
86
+ ): Promise<TxValidationResult> {
87
+ const key = this.key(validatorSymbol, tx);
88
+ const cached = this.get(key);
89
+ if (cached !== undefined) {
90
+ this.#log.debug('Returning cached tx validation result', {
91
+ validator: validatorSymbol.toString(),
92
+ txHash: tx.txHash.toString(),
93
+ key: key,
94
+ });
95
+ return cached;
96
+ }
97
+ const promise = validate();
98
+ this.set(key, promise);
99
+ return promise;
100
+ }
101
+ }
@@ -38,6 +38,8 @@ export class DiscV5Service extends EventEmitter implements PeerDiscoveryService
38
38
 
39
39
  private startTime = 0;
40
40
 
41
+ private currentIp: string | undefined;
42
+
41
43
  private handlers = {
42
44
  onMultiaddrUpdated: this.onMultiaddrUpdated.bind(this),
43
45
  onDiscovered: this.onDiscovered.bind(this),
@@ -53,8 +55,10 @@ export class DiscV5Service extends EventEmitter implements PeerDiscoveryService
53
55
  configOverrides: Partial<IDiscv5CreateOptions> = {},
54
56
  ) {
55
57
  super();
58
+
56
59
  const { p2pIp, p2pPort, p2pBroadcastPort, bootstrapNodes, trustedPeers, privatePeers } = config;
57
60
 
61
+ this.currentIp = p2pIp;
58
62
  this.bootstrapNodeEnrs = bootstrapNodes.map(x => ENR.decodeTxt(x));
59
63
  const privatePeerEnrs = new Set(privatePeers);
60
64
  this.trustedPeerEnrs = trustedPeers.filter(x => !privatePeerEnrs.has(x)).map(x => ENR.decodeTxt(x));
@@ -96,7 +100,8 @@ export class DiscV5Service extends EventEmitter implements PeerDiscoveryService
96
100
  lookupTimeout: 2000,
97
101
  requestTimeout: 2000,
98
102
  allowUnverifiedSessions: true,
99
- enrUpdate: !p2pIp ? true : false, // If no p2p IP is set, enrUpdate can automatically resolve it
103
+ enrUpdate: config.queryForIp || !p2pIp,
104
+ pingInterval: config.queryForIp ? 10_000 : 300_000,
100
105
  ...configOverrides.config,
101
106
  },
102
107
  metricsRegistry,
@@ -127,11 +132,34 @@ export class DiscV5Service extends EventEmitter implements PeerDiscoveryService
127
132
  }
128
133
 
129
134
  private onMultiaddrUpdated(m: Multiaddr) {
130
- // We want to update our tcp port to match the udp port
131
- // p2pBroadcastPort is optional on config, however it is set to default within the p2p client factory
132
- const multiAddrTcp = multiaddr(convertToMultiaddr(m.nodeAddress().address, this.config.p2pBroadcastPort!, 'tcp'));
135
+ const newIp = m.nodeAddress().address;
136
+ const previousIp = this.currentIp;
137
+
138
+ if (newIp === previousIp) {
139
+ this.logger.debug('Discv5 confirmed current IP (no change)', { ip: newIp });
140
+ return;
141
+ }
142
+
143
+ const multiAddrTcp = multiaddr(convertToMultiaddr(newIp, this.config.p2pBroadcastPort!, 'tcp'));
133
144
  this.enr.setLocationMultiaddr(multiAddrTcp);
134
- this.logger.info('Multiaddr updated', { multiaddr: multiAddrTcp.toString() });
145
+ this.currentIp = newIp;
146
+
147
+ if (previousIp) {
148
+ this.logger.info('IP address changed, ENR updated', {
149
+ previousIp,
150
+ newIp,
151
+ multiaddr: multiAddrTcp.toString(),
152
+ enr: this.enr.encodeTxt(),
153
+ });
154
+ } else {
155
+ this.logger.info('Initial IP discovered via discv5, ENR updated', {
156
+ ip: newIp,
157
+ multiaddr: multiAddrTcp.toString(),
158
+ enr: this.enr.encodeTxt(),
159
+ });
160
+ }
161
+
162
+ this.emit('ip:changed', newIp);
135
163
  }
136
164
 
137
165
  public async start(): Promise<void> {
@@ -142,12 +170,17 @@ export class DiscV5Service extends EventEmitter implements PeerDiscoveryService
142
170
  await this.discv5.start();
143
171
  this.startTime = Date.now();
144
172
 
173
+ const enrUpdateEnabled = this.config.queryForIp || !this.config.p2pIp;
145
174
  this.logger.info(`DiscV5 service started`, {
146
175
  nodeId: this.enr.nodeId,
147
176
  peerId: this.peerId,
148
177
  enrUdp: await this.enr.getFullMultiaddr('udp'),
149
178
  enrTcp: await this.enr.getFullMultiaddr('tcp'),
150
179
  versions: this.versions,
180
+ enrUpdateEnabled,
181
+ queryForIp: this.config.queryForIp,
182
+ configuredIp: this.config.p2pIp ?? 'none',
183
+ pingIntervalMs: this.config.queryForIp ? 10_000 : 300_000,
151
184
  });
152
185
  this.currentState = PeerDiscoveryState.RUNNING;
153
186
 
@@ -1,6 +1,6 @@
1
1
  import type { EthAddress } from '@aztec/foundation/eth-address';
2
2
  import type { PeerInfo } from '@aztec/stdlib/interfaces/server';
3
- import type { CheckpointProposalCore, Gossipable, PeerErrorSeverity, TopicType } from '@aztec/stdlib/p2p';
3
+ import type { Gossipable, PeerErrorSeverity, TopicType } from '@aztec/stdlib/p2p';
4
4
  import { Tx, TxHash } from '@aztec/stdlib/tx';
5
5
 
6
6
  import type { PeerId } from '@libp2p/interface';
@@ -18,13 +18,13 @@ import type {
18
18
  ReqRespSubProtocol,
19
19
  ReqRespSubProtocolHandler,
20
20
  ReqRespSubProtocolHandlers,
21
- ReqRespSubProtocolValidators,
22
21
  SubProtocolMap,
23
22
  } from './reqresp/interface.js';
24
23
  import type { GoodByeReason } from './reqresp/protocols/goodbye.js';
25
24
  import { ReqRespStatus } from './reqresp/status.js';
26
25
  import {
27
26
  type P2PBlockReceivedCallback,
27
+ type P2PCheckpointAttestationCallback,
28
28
  type P2PCheckpointReceivedCallback,
29
29
  type P2PDuplicateAttestationCallback,
30
30
  type P2PDuplicateProposalCallback,
@@ -37,6 +37,8 @@ import {
37
37
  * A dummy implementation of the P2P Service.
38
38
  */
39
39
  export class DummyP2PService implements P2PService {
40
+ private allNodesCheckpointReceivedCallback?: P2PCheckpointReceivedCallback;
41
+
40
42
  updateConfig(_config: Partial<P2PReqRespConfig>): void {}
41
43
 
42
44
  /** Returns an empty array for peers. */
@@ -87,10 +89,8 @@ export class DummyP2PService implements P2PService {
87
89
  * Register a callback into the validator client for when a checkpoint proposal is received
88
90
  */
89
91
  public registerValidatorCheckpointReceivedCallback(_callback: P2PCheckpointReceivedCallback) {}
90
- public registerAllNodesCheckpointReceivedCallback(_callback: P2PCheckpointReceivedCallback) {}
91
-
92
- public notifyOwnCheckpointProposal(_checkpoint: CheckpointProposalCore): Promise<void> {
93
- return Promise.resolve();
92
+ public registerAllNodesCheckpointReceivedCallback(callback: P2PCheckpointReceivedCallback) {
93
+ this.allNodesCheckpointReceivedCallback = callback;
94
94
  }
95
95
 
96
96
  /**
@@ -103,6 +103,8 @@ export class DummyP2PService implements P2PService {
103
103
  */
104
104
  public registerDuplicateAttestationCallback(_callback: P2PDuplicateAttestationCallback): void {}
105
105
 
106
+ public registerCheckpointAttestationCallback(_callback: P2PCheckpointAttestationCallback): void {}
107
+
106
108
  /**
107
109
  * Sends a request to a peer.
108
110
  * @param _protocol - The protocol to send the request on.
@@ -116,19 +118,6 @@ export class DummyP2PService implements P2PService {
116
118
  return Promise.resolve(undefined);
117
119
  }
118
120
 
119
- /**
120
- * Sends a batch request to a peer.
121
- * @param _protocol - The protocol to send the request on.
122
- * @param _requests - The requests to send.
123
- * @returns The responses from the peer, otherwise undefined.
124
- */
125
- public sendBatchRequest<Protocol extends ReqRespSubProtocol>(
126
- _protocol: Protocol,
127
- _requests: InstanceType<SubProtocolMap[Protocol]['request']>[],
128
- ): Promise<InstanceType<SubProtocolMap[Protocol]['response']>[]> {
129
- return Promise.resolve([]);
130
- }
131
-
132
121
  public sendRequestToPeer(
133
122
  _peerId: PeerId,
134
123
  _subProtocol: ReqRespSubProtocol,
@@ -150,11 +139,7 @@ export class DummyP2PService implements P2PService {
150
139
  return Promise.resolve();
151
140
  }
152
141
 
153
- addReqRespSubProtocol(
154
- _subProtocol: ReqRespSubProtocol,
155
- _handler: ReqRespSubProtocolHandler,
156
- _validator?: ReqRespSubProtocolValidators[ReqRespSubProtocol],
157
- ): Promise<void> {
142
+ addReqRespSubProtocol(_subProtocol: ReqRespSubProtocol, _handler: ReqRespSubProtocolHandler): Promise<void> {
158
143
  return Promise.resolve();
159
144
  }
160
145
 
@@ -183,6 +168,7 @@ export class DummyP2PService implements P2PService {
183
168
  peerScoring: {
184
169
  penalizePeer: (_peerId, _penalty) => {},
185
170
  },
171
+ validateRequestedBlockTxsConsistency: () => Promise.resolve(true),
186
172
  };
187
173
  }
188
174
  }
@@ -288,10 +274,7 @@ export class DummyPeerManager implements PeerManagerInterface {
288
274
  export class DummyReqResp implements ReqRespInterface {
289
275
  updateConfig(_config: Partial<P2PReqRespConfig>): void {}
290
276
  setShouldRejectPeer(): void {}
291
- start(
292
- _subProtocolHandlers: ReqRespSubProtocolHandlers,
293
- _subProtocolValidators: ReqRespSubProtocolValidators,
294
- ): Promise<void> {
277
+ start(_subProtocolHandlers: ReqRespSubProtocolHandlers): Promise<void> {
295
278
  return Promise.resolve();
296
279
  }
297
280
  stop(): Promise<void> {
@@ -303,16 +286,6 @@ export class DummyReqResp implements ReqRespInterface {
303
286
  ): Promise<InstanceType<SubProtocolMap[SubProtocol]['response']> | undefined> {
304
287
  return Promise.resolve(undefined);
305
288
  }
306
- sendBatchRequest<SubProtocol extends ReqRespSubProtocol>(
307
- _subProtocol: SubProtocol,
308
- _requests: InstanceType<SubProtocolMap[SubProtocol]['request']>[],
309
- _pinnedPeer: PeerId | undefined,
310
- _timeoutMs?: number,
311
- _maxPeers?: number,
312
- _maxRetryAttempts?: number,
313
- ): Promise<InstanceType<SubProtocolMap[SubProtocol]['response']>[]> {
314
- return Promise.resolve([]);
315
- }
316
289
  public sendRequestToPeer(
317
290
  _peerId: PeerId,
318
291
  _subProtocol: ReqRespSubProtocol,
@@ -331,11 +304,7 @@ export class DummyReqResp implements ReqRespInterface {
331
304
  };
332
305
  }
333
306
 
334
- addSubProtocol(
335
- _subProtocol: ReqRespSubProtocol,
336
- _handler: ReqRespSubProtocolHandler,
337
- _validator?: ReqRespSubProtocolValidators[ReqRespSubProtocol],
338
- ): Promise<void> {
307
+ addSubProtocol(_subProtocol: ReqRespSubProtocol, _handler: ReqRespSubProtocolHandler): Promise<void> {
339
308
  return Promise.resolve();
340
309
  }
341
310
  }