@aztec/p2p 1.2.1 → 2.0.0-nightly.20250813

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 (221) hide show
  1. package/dest/client/factory.d.ts +5 -1
  2. package/dest/client/factory.d.ts.map +1 -1
  3. package/dest/client/factory.js +29 -12
  4. package/dest/client/interface.d.ts +8 -13
  5. package/dest/client/interface.d.ts.map +1 -1
  6. package/dest/client/p2p_client.d.ts +18 -22
  7. package/dest/client/p2p_client.d.ts.map +1 -1
  8. package/dest/client/p2p_client.js +86 -83
  9. package/dest/config.d.ts +30 -1
  10. package/dest/config.d.ts.map +1 -1
  11. package/dest/config.js +34 -1
  12. package/dest/index.d.ts +1 -0
  13. package/dest/index.d.ts.map +1 -1
  14. package/dest/index.js +1 -0
  15. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +13 -1
  16. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  17. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  18. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +117 -10
  19. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +4 -1
  20. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +1 -1
  21. package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +22 -1
  22. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +4 -1
  23. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +1 -1
  24. package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +21 -1
  25. package/dest/mem_pools/attestation_pool/mocks.d.ts +1 -2
  26. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  27. package/dest/mem_pools/attestation_pool/mocks.js +2 -10
  28. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts +8 -3
  29. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts.map +1 -1
  30. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.js +64 -37
  31. package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts +8 -3
  32. package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts.map +1 -1
  33. package/dest/mem_pools/tx_pool/memory_tx_pool.js +18 -10
  34. package/dest/mem_pools/tx_pool/tx_pool.d.ts +11 -2
  35. package/dest/mem_pools/tx_pool/tx_pool.d.ts.map +1 -1
  36. package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts.map +1 -1
  37. package/dest/mem_pools/tx_pool/tx_pool_test_suite.js +73 -44
  38. package/dest/msg_validators/attestation_validator/attestation_validator.js +1 -1
  39. package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
  40. package/dest/msg_validators/tx_validator/block_header_validator.js +2 -2
  41. package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
  42. package/dest/msg_validators/tx_validator/data_validator.js +35 -59
  43. package/dest/msg_validators/tx_validator/double_spend_validator.js +2 -2
  44. package/dest/msg_validators/tx_validator/gas_validator.js +4 -4
  45. package/dest/msg_validators/tx_validator/metadata_validator.d.ts.map +1 -1
  46. package/dest/msg_validators/tx_validator/metadata_validator.js +21 -21
  47. package/dest/msg_validators/tx_validator/phases_validator.js +3 -3
  48. package/dest/msg_validators/tx_validator/tx_proof_validator.js +3 -3
  49. package/dest/services/discv5/discV5_service.d.ts.map +1 -1
  50. package/dest/services/discv5/discV5_service.js +4 -1
  51. package/dest/services/dummy_service.d.ts +13 -3
  52. package/dest/services/dummy_service.d.ts.map +1 -1
  53. package/dest/services/dummy_service.js +26 -3
  54. package/dest/services/index.d.ts +3 -1
  55. package/dest/services/index.d.ts.map +1 -1
  56. package/dest/services/index.js +3 -1
  57. package/dest/services/libp2p/libp2p_service.d.ts +13 -20
  58. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  59. package/dest/services/libp2p/libp2p_service.js +123 -46
  60. package/dest/services/peer-manager/interface.d.ts +8 -1
  61. package/dest/services/peer-manager/interface.d.ts.map +1 -1
  62. package/dest/services/peer-manager/peer_manager.d.ts +70 -3
  63. package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
  64. package/dest/services/peer-manager/peer_manager.js +369 -39
  65. package/dest/services/reqresp/config.d.ts +3 -3
  66. package/dest/services/reqresp/config.d.ts.map +1 -1
  67. package/dest/services/reqresp/config.js +3 -3
  68. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts +3 -4
  69. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts.map +1 -1
  70. package/dest/services/reqresp/connection-sampler/connection_sampler.js +35 -52
  71. package/dest/services/reqresp/index.d.ts +2 -1
  72. package/dest/services/reqresp/index.d.ts.map +1 -1
  73. package/dest/services/reqresp/index.js +2 -1
  74. package/dest/services/reqresp/interface.d.ts +61 -10
  75. package/dest/services/reqresp/interface.d.ts.map +1 -1
  76. package/dest/services/reqresp/interface.js +41 -6
  77. package/dest/services/reqresp/protocols/auth.d.ts +43 -0
  78. package/dest/services/reqresp/protocols/auth.d.ts.map +1 -0
  79. package/dest/services/reqresp/protocols/auth.js +71 -0
  80. package/dest/services/reqresp/protocols/block.d.ts +5 -0
  81. package/dest/services/reqresp/protocols/block.d.ts.map +1 -1
  82. package/dest/services/reqresp/protocols/block.js +28 -5
  83. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts +30 -0
  84. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts.map +1 -0
  85. package/dest/services/reqresp/protocols/block_txs/bitvector.js +75 -0
  86. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +11 -0
  87. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -0
  88. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.js +39 -0
  89. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +49 -0
  90. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -0
  91. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +75 -0
  92. package/dest/services/reqresp/protocols/block_txs/index.d.ts +4 -0
  93. package/dest/services/reqresp/protocols/block_txs/index.d.ts.map +1 -0
  94. package/dest/services/reqresp/protocols/block_txs/index.js +3 -0
  95. package/dest/services/reqresp/protocols/goodbye.js +3 -5
  96. package/dest/services/reqresp/protocols/index.d.ts +2 -0
  97. package/dest/services/reqresp/protocols/index.d.ts.map +1 -1
  98. package/dest/services/reqresp/protocols/index.js +2 -0
  99. package/dest/services/reqresp/protocols/status.d.ts +2 -0
  100. package/dest/services/reqresp/protocols/status.d.ts.map +1 -1
  101. package/dest/services/reqresp/protocols/status.js +7 -0
  102. package/dest/services/reqresp/protocols/tx.d.ts +12 -1
  103. package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
  104. package/dest/services/reqresp/protocols/tx.js +34 -6
  105. package/dest/services/reqresp/rate-limiter/rate_limits.d.ts.map +1 -1
  106. package/dest/services/reqresp/rate-limiter/rate_limits.js +20 -0
  107. package/dest/services/reqresp/reqresp.d.ts +37 -39
  108. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  109. package/dest/services/reqresp/reqresp.js +220 -220
  110. package/dest/services/reqresp/status.d.ts +8 -3
  111. package/dest/services/reqresp/status.d.ts.map +1 -1
  112. package/dest/services/reqresp/status.js +6 -2
  113. package/dest/services/service.d.ts +10 -10
  114. package/dest/services/service.d.ts.map +1 -1
  115. package/dest/services/tx_collection/config.d.ts +25 -0
  116. package/dest/services/tx_collection/config.d.ts.map +1 -0
  117. package/dest/services/tx_collection/config.js +58 -0
  118. package/dest/services/tx_collection/fast_tx_collection.d.ts +56 -0
  119. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -0
  120. package/dest/services/tx_collection/fast_tx_collection.js +295 -0
  121. package/dest/services/tx_collection/index.d.ts +3 -0
  122. package/dest/services/tx_collection/index.d.ts.map +1 -0
  123. package/dest/services/tx_collection/index.js +2 -0
  124. package/dest/services/tx_collection/instrumentation.d.ts +10 -0
  125. package/dest/services/tx_collection/instrumentation.d.ts.map +1 -0
  126. package/dest/services/tx_collection/instrumentation.js +34 -0
  127. package/dest/services/tx_collection/slow_tx_collection.d.ts +54 -0
  128. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -0
  129. package/dest/services/tx_collection/slow_tx_collection.js +176 -0
  130. package/dest/services/tx_collection/tx_collection.d.ts +109 -0
  131. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -0
  132. package/dest/services/tx_collection/tx_collection.js +127 -0
  133. package/dest/services/tx_collection/tx_collection_sink.d.ts +30 -0
  134. package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -0
  135. package/dest/services/tx_collection/tx_collection_sink.js +81 -0
  136. package/dest/services/tx_collection/tx_source.d.ts +18 -0
  137. package/dest/services/tx_collection/tx_source.d.ts.map +1 -0
  138. package/dest/services/tx_collection/tx_source.js +31 -0
  139. package/dest/services/tx_provider.d.ts +49 -0
  140. package/dest/services/tx_provider.d.ts.map +1 -0
  141. package/dest/services/tx_provider.js +206 -0
  142. package/dest/services/{tx_collect_instrumentation.d.ts → tx_provider_instrumentation.d.ts} +2 -2
  143. package/dest/services/tx_provider_instrumentation.d.ts.map +1 -0
  144. package/dest/services/{tx_collect_instrumentation.js → tx_provider_instrumentation.js} +5 -5
  145. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  146. package/dest/test-helpers/make-test-p2p-clients.js +4 -3
  147. package/dest/test-helpers/mock-pubsub.d.ts +2 -1
  148. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  149. package/dest/test-helpers/mock-pubsub.js +2 -1
  150. package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
  151. package/dest/test-helpers/reqresp-nodes.js +8 -4
  152. package/dest/testbench/p2p_client_testbench_worker.js +11 -5
  153. package/dest/util.d.ts +1 -1
  154. package/dest/util.d.ts.map +1 -1
  155. package/package.json +14 -15
  156. package/src/client/factory.ts +87 -12
  157. package/src/client/interface.ts +19 -15
  158. package/src/client/p2p_client.ts +108 -92
  159. package/src/config.ts +52 -1
  160. package/src/index.ts +1 -0
  161. package/src/mem_pools/attestation_pool/attestation_pool.ts +15 -1
  162. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +155 -4
  163. package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +28 -1
  164. package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +28 -2
  165. package/src/mem_pools/attestation_pool/mocks.ts +1 -3
  166. package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +59 -41
  167. package/src/mem_pools/tx_pool/memory_tx_pool.ts +19 -9
  168. package/src/mem_pools/tx_pool/tx_pool.ts +7 -2
  169. package/src/mem_pools/tx_pool/tx_pool_test_suite.ts +63 -40
  170. package/src/msg_validators/attestation_validator/attestation_validator.ts +1 -1
  171. package/src/msg_validators/tx_validator/block_header_validator.ts +2 -2
  172. package/src/msg_validators/tx_validator/data_validator.ts +36 -27
  173. package/src/msg_validators/tx_validator/double_spend_validator.ts +2 -2
  174. package/src/msg_validators/tx_validator/gas_validator.ts +4 -4
  175. package/src/msg_validators/tx_validator/metadata_validator.ts +22 -28
  176. package/src/msg_validators/tx_validator/phases_validator.ts +2 -2
  177. package/src/msg_validators/tx_validator/tx_proof_validator.ts +2 -2
  178. package/src/services/discv5/discV5_service.ts +4 -1
  179. package/src/services/dummy_service.ts +44 -4
  180. package/src/services/index.ts +3 -1
  181. package/src/services/libp2p/libp2p_service.ts +147 -55
  182. package/src/services/peer-manager/interface.ts +10 -1
  183. package/src/services/peer-manager/peer_manager.ts +441 -41
  184. package/src/services/reqresp/config.ts +3 -3
  185. package/src/services/reqresp/connection-sampler/connection_sampler.ts +38 -63
  186. package/src/services/reqresp/index.ts +2 -0
  187. package/src/services/reqresp/interface.ts +63 -17
  188. package/src/services/reqresp/protocols/auth.ts +83 -0
  189. package/src/services/reqresp/protocols/block.ts +24 -3
  190. package/src/services/reqresp/protocols/block_txs/bitvector.ts +90 -0
  191. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +53 -0
  192. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +79 -0
  193. package/src/services/reqresp/protocols/block_txs/index.ts +3 -0
  194. package/src/services/reqresp/protocols/goodbye.ts +3 -3
  195. package/src/services/reqresp/protocols/index.ts +2 -0
  196. package/src/services/reqresp/protocols/status.ts +20 -0
  197. package/src/services/reqresp/protocols/tx.ts +35 -6
  198. package/src/services/reqresp/rate-limiter/rate_limits.ts +20 -0
  199. package/src/services/reqresp/reqresp.ts +294 -264
  200. package/src/services/reqresp/status.ts +9 -3
  201. package/src/services/service.ts +23 -14
  202. package/src/services/tx_collection/config.ts +84 -0
  203. package/src/services/tx_collection/fast_tx_collection.ts +338 -0
  204. package/src/services/tx_collection/index.ts +2 -0
  205. package/src/services/tx_collection/instrumentation.ts +43 -0
  206. package/src/services/tx_collection/slow_tx_collection.ts +232 -0
  207. package/src/services/tx_collection/tx_collection.ts +214 -0
  208. package/src/services/tx_collection/tx_collection_sink.ts +98 -0
  209. package/src/services/tx_collection/tx_source.ts +37 -0
  210. package/src/services/tx_provider.ts +215 -0
  211. package/src/services/{tx_collect_instrumentation.ts → tx_provider_instrumentation.ts} +5 -5
  212. package/src/test-helpers/make-test-p2p-clients.ts +4 -2
  213. package/src/test-helpers/mock-pubsub.ts +1 -0
  214. package/src/test-helpers/reqresp-nodes.ts +7 -1
  215. package/src/testbench/p2p_client_testbench_worker.ts +9 -2
  216. package/src/util.ts +1 -1
  217. package/dest/services/tx_collect_instrumentation.d.ts.map +0 -1
  218. package/dest/services/tx_collector.d.ts +0 -23
  219. package/dest/services/tx_collector.d.ts.map +0 -1
  220. package/dest/services/tx_collector.js +0 -95
  221. package/src/services/tx_collector.ts +0 -134
@@ -0,0 +1,176 @@
1
+ import { chunk } from '@aztec/foundation/collection';
2
+ import { createLogger } from '@aztec/foundation/log';
3
+ import { boundInclusive } from '@aztec/foundation/number';
4
+ import { RunningPromise } from '@aztec/foundation/promise';
5
+ import { DateProvider } from '@aztec/foundation/timer';
6
+ import { getEpochAtSlot, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
7
+ import { TxHash } from '@aztec/stdlib/tx';
8
+ import { ReqRespSubProtocol } from '../reqresp/interface.js';
9
+ import { chunkTxHashesRequest } from '../reqresp/protocols/tx.js';
10
+ export class SlowTxCollection {
11
+ reqResp;
12
+ nodes;
13
+ txCollectionSink;
14
+ fastCollection;
15
+ constants;
16
+ config;
17
+ dateProvider;
18
+ log;
19
+ /** Map from tx hash to missing tx info to collect via slow loop */ missingTxs;
20
+ /** One slow collection loop for each node tx source we have */ nodesSlowCollectionLoops;
21
+ /** Slow collection loop for reqresp which collects txs from peers */ reqrespSlowCollectionLoop;
22
+ constructor(reqResp, nodes, txCollectionSink, fastCollection, constants, config, dateProvider = new DateProvider(), log = createLogger('p2p:tx_collection_service')){
23
+ this.reqResp = reqResp;
24
+ this.nodes = nodes;
25
+ this.txCollectionSink = txCollectionSink;
26
+ this.fastCollection = fastCollection;
27
+ this.constants = constants;
28
+ this.config = config;
29
+ this.dateProvider = dateProvider;
30
+ this.log = log;
31
+ this.missingTxs = new Map();
32
+ this.nodesSlowCollectionLoops = this.nodes.map((node)=>new RunningPromise(()=>this.collectMissingTxsFromNode(node), this.log, this.config.txCollectionSlowNodesIntervalMs));
33
+ this.reqrespSlowCollectionLoop = new RunningPromise(()=>this.collectMissingTxsViaReqResp(), this.log, this.config.txCollectionSlowReqRespIntervalMs);
34
+ }
35
+ getMissingTxHashes() {
36
+ return Array.from(this.missingTxs.keys()).map(TxHash.fromString);
37
+ }
38
+ start() {
39
+ this.nodesSlowCollectionLoops.forEach((loop)=>loop.start());
40
+ this.reqrespSlowCollectionLoop.start();
41
+ }
42
+ async stop() {
43
+ await Promise.all([
44
+ this.reqrespSlowCollectionLoop.stop(),
45
+ ...this.nodesSlowCollectionLoops.map((loop)=>loop.stop())
46
+ ]);
47
+ }
48
+ async trigger() {
49
+ await Promise.all([
50
+ this.reqrespSlowCollectionLoop.trigger(),
51
+ ...this.nodesSlowCollectionLoops.map((loop)=>loop.trigger())
52
+ ]);
53
+ }
54
+ /** Starts collecting the given tx hashes for the given L2Block in the slow loop */ startCollecting(block, txHashes) {
55
+ const slot = block.header.getSlot();
56
+ const deadline = this.getDeadlineForSlot(slot);
57
+ if (+deadline < this.dateProvider.now()) {
58
+ this.log.debug(`Skipping collection of txs for block ${block.number} at slot ${slot} as it is already expired`, {
59
+ blockNumber: block.number,
60
+ slot: slot.toString(),
61
+ txHashes: txHashes.map((txHash)=>txHash.toString()),
62
+ deadline: +deadline,
63
+ now: this.dateProvider.now()
64
+ });
65
+ }
66
+ for (const txHash of txHashes){
67
+ this.missingTxs.set(txHash.toString(), {
68
+ blockNumber: block.number,
69
+ deadline: this.getDeadlineForSlot(block.header.getSlot()),
70
+ readyForReqResp: this.nodes.length === 0
71
+ });
72
+ }
73
+ }
74
+ /** Entrypoint for the node slow collection loop */ async collectMissingTxsFromNode(node) {
75
+ // If we have any fast requests, we skip the slow collection for this node if configured to do so
76
+ const requests = this.fastCollection.getFastCollectionRequests();
77
+ if (this.config.txCollectionDisableSlowDuringFastRequests && requests.size > 0) {
78
+ this.log.trace(`Skipping node slow collection due to active fast requests`);
79
+ return;
80
+ }
81
+ // Gather all missing txs that are not in fast collection and request them from the node
82
+ const missingTxs = this.getMissingTxsForSlowCollection();
83
+ const missingTxHashes = missingTxs.map(([txHash])=>txHash).map(TxHash.fromString);
84
+ if (missingTxHashes.length === 0) {
85
+ return;
86
+ }
87
+ // Request in chunks to avoid hitting RPC limits
88
+ for (const batch of chunk(missingTxHashes, this.config.txCollectionNodeRpcMaxBatchSize)){
89
+ await this.txCollectionSink.collect((txHashes)=>node.getTxsByHash(txHashes), batch, {
90
+ description: `node ${node.getInfo()}`,
91
+ node: node.getInfo(),
92
+ method: 'slow-node-rpc'
93
+ });
94
+ }
95
+ // Mark every tx that is still missing as ready for reqresp.
96
+ // Note that we can just mark all requested txs as ready for reqresp, without filtering out the ones
97
+ // we retrieved, since the ones already found will have been removed from `missingTxs`.
98
+ missingTxs.forEach(([_txHash, info])=>{
99
+ info.readyForReqResp = true;
100
+ });
101
+ }
102
+ /** Entrypoint for the reqresp slow collection loop */ async collectMissingTxsViaReqResp() {
103
+ // If we have any fast requests, we skip the slow collection if configured to do so
104
+ const requests = this.fastCollection.getFastCollectionRequests();
105
+ if (this.config.txCollectionDisableSlowDuringFastRequests && requests.size > 0) {
106
+ this.log.trace(`Skipping reqresp slow collection due to active fast requests`);
107
+ return;
108
+ }
109
+ // Gather all missing txs that are not in fast collection and are ready for reqresp
110
+ // A tx is flagged as ready for reqresp if it has been requested from a node at least once
111
+ const missingTxs = this.getMissingTxsForSlowCollection({
112
+ onlyReqRespReady: true
113
+ });
114
+ if (missingTxs.length === 0) {
115
+ return;
116
+ }
117
+ const pinnedPeer = undefined;
118
+ const timeoutMs = this.config.txCollectionSlowReqRespTimeoutMs;
119
+ const maxPeers = boundInclusive(Math.ceil(missingTxs.length / 3), 4, 16);
120
+ const maxRetryAttempts = 3;
121
+ // Send a batch request via reqresp for the missing txs
122
+ await this.txCollectionSink.collect(async (txHashes)=>{
123
+ const txs = await this.reqResp.sendBatchRequest(ReqRespSubProtocol.TX, chunkTxHashesRequest(txHashes), pinnedPeer, timeoutMs, maxPeers, maxRetryAttempts);
124
+ return txs.flat();
125
+ }, missingTxs.map(([txHash])=>TxHash.fromString(txHash)), {
126
+ description: 'slow reqresp',
127
+ timeoutMs,
128
+ method: 'slow-req-resp'
129
+ });
130
+ }
131
+ /** Retrieves all missing txs for the slow collection process. This is, all missing txs that are not part of a fast request. */ getMissingTxsForSlowCollection(opts = {}) {
132
+ // Remove expired txs from missingTxs
133
+ const now = this.dateProvider.now();
134
+ const expiredTxs = Array.from(this.missingTxs.entries()).filter(([_, value])=>+value.deadline < now);
135
+ expiredTxs.forEach(([txHash])=>this.missingTxs.delete(txHash));
136
+ // Gather all txs that are marked for fast collection, we do not want to collect them via slow collection.
137
+ // There are some situations where a tx is in both slow and fast collection, for example when a prover node
138
+ // is fast-collecting missing txs for proving an epoch, and still has the tx in the slow collection loops
139
+ // from mined unproven blocks it has seen in the past.
140
+ const fastRequests = this.fastCollection.getFastCollectionRequests();
141
+ const fastCollectionTxs = new Set(...Array.from(fastRequests.values()).flatMap((r)=>r.missingTxHashes));
142
+ // Return all missing txs that are not in fastCollectionTxs and are ready for reqresp if requested
143
+ return Array.from(this.missingTxs.entries()).filter(([txHash, _])=>!fastCollectionTxs.has(txHash)).filter(([_, value])=>!opts.onlyReqRespReady || value.readyForReqResp);
144
+ }
145
+ /** Stop collecting the given txs since we have found them. Called whenever tx pool emits a tx-added event. */ foundTxs(txs) {
146
+ for (const txHash of txs.map((tx)=>tx.txHash)){
147
+ this.missingTxs.delete(txHash.toString());
148
+ }
149
+ }
150
+ /**
151
+ * Stop collecting all txs for blocks less than or requal to the block number specified.
152
+ * To be called when we no longer care about gathering txs up to a certain block, eg when they become proven or finalized.
153
+ */ stopCollectingForBlocksUpTo(blockNumber) {
154
+ for (const [txHash, info] of this.missingTxs.entries()){
155
+ if (info.blockNumber <= blockNumber) {
156
+ this.missingTxs.delete(txHash);
157
+ }
158
+ }
159
+ }
160
+ /**
161
+ * Stop collecting all txs for blocks greater than the block number specified.
162
+ * To be called when there is a chain prune and previously mined txs are no longer relevant.
163
+ */ stopCollectingForBlocksAfter(blockNumber) {
164
+ for (const [txHash, info] of this.missingTxs.entries()){
165
+ if (info.blockNumber > blockNumber) {
166
+ this.missingTxs.delete(txHash);
167
+ }
168
+ }
169
+ }
170
+ /** Computes the proof submission deadline for a given slot, a tx mined in this slot is no longer interesting after this deadline */ getDeadlineForSlot(slotNumber) {
171
+ const epoch = getEpochAtSlot(slotNumber, this.constants);
172
+ const submissionEndEpoch = epoch + BigInt(this.constants.proofSubmissionEpochs);
173
+ const submissionEndTimestamp = getTimestampRangeForEpoch(submissionEndEpoch, this.constants)[1];
174
+ return new Date(Number(submissionEndTimestamp) * 1000);
175
+ }
176
+ }
@@ -0,0 +1,109 @@
1
+ import { type Logger } from '@aztec/foundation/log';
2
+ import { type PromiseWithResolvers } from '@aztec/foundation/promise';
3
+ import { DateProvider } from '@aztec/foundation/timer';
4
+ import type { BlockInfo, L2Block } from '@aztec/stdlib/block';
5
+ import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
6
+ import type { BlockProposal } from '@aztec/stdlib/p2p';
7
+ import { Tx, TxHash } from '@aztec/stdlib/tx';
8
+ import { type TelemetryClient } from '@aztec/telemetry-client';
9
+ import type { PeerId } from '@libp2p/interface';
10
+ import type { TxPool } from '../../mem_pools/index.js';
11
+ import type { TxPoolEvents } from '../../mem_pools/tx_pool/tx_pool.js';
12
+ import type { ReqRespInterface } from '../reqresp/interface.js';
13
+ import type { TxCollectionConfig } from './config.js';
14
+ import { FastTxCollection } from './fast_tx_collection.js';
15
+ import { SlowTxCollection } from './slow_tx_collection.js';
16
+ import type { TxSource } from './tx_source.js';
17
+ export type CollectionMethod = 'fast-req-resp' | 'fast-node-rpc' | 'slow-req-resp' | 'slow-node-rpc';
18
+ export type MissingTxInfo = {
19
+ blockNumber: number;
20
+ deadline: Date;
21
+ readyForReqResp: boolean;
22
+ };
23
+ export type FastCollectionRequestInput = {
24
+ type: 'block';
25
+ block: L2Block;
26
+ } | {
27
+ type: 'proposal';
28
+ blockProposal: BlockProposal;
29
+ };
30
+ export type FastCollectionRequest = FastCollectionRequestInput & {
31
+ missingTxHashes: Set<string>;
32
+ deadline: Date;
33
+ blockInfo: BlockInfo;
34
+ promise: PromiseWithResolvers<void>;
35
+ foundTxs: Map<string, Tx>;
36
+ };
37
+ /**
38
+ * Coordinates tx collection from remote RPC nodes and reqresp.
39
+ *
40
+ * The slow collection loops are used for periodically collecting missing txs from mined blocks,
41
+ * from remote RPC nodes and via reqresp. The fast collection methods are used to quickly gather
42
+ * txs, usually for attesting to block proposals or preparing to prove an epoch. The slow and fast
43
+ * collection instances both send their txs to the collection sink, which handles metrics and adds
44
+ * them to the tx pool. Whenever a tx is added to either the sink or the pool, this service is notified
45
+ * via events and notifies the slow and fast collection loops to stop collecting that tx, so that we don't
46
+ * collect the same tx multiple times.
47
+ */
48
+ export declare class TxCollection {
49
+ private readonly reqResp;
50
+ private readonly nodes;
51
+ private readonly constants;
52
+ private readonly txPool;
53
+ private readonly config;
54
+ private readonly dateProvider;
55
+ private readonly log;
56
+ /** Slow collection background loops */
57
+ protected readonly slowCollection: SlowTxCollection;
58
+ /** Fast collection methods */
59
+ protected readonly fastCollection: FastTxCollection;
60
+ /** Loop for periodically reconciling found transactions from the tx pool in case we missed some */
61
+ private readonly reconcileFoundTxsLoop;
62
+ /** Handles txs found by the slow and fast collection loops */
63
+ private readonly txCollectionSink;
64
+ /** Handler for the txs-added event from the tx pool */
65
+ protected readonly handleTxsAddedToPool: TxPoolEvents['txs-added'];
66
+ /** Handler for the txs-added event from the tx collection sink */
67
+ protected readonly handleTxsFound: TxPoolEvents['txs-added'];
68
+ constructor(reqResp: Pick<ReqRespInterface, 'sendBatchRequest'>, nodes: TxSource[], constants: L1RollupConstants, txPool: TxPool, config: TxCollectionConfig, dateProvider?: DateProvider, telemetryClient?: TelemetryClient, log?: Logger);
69
+ /** Starts all collection loops. */
70
+ start(): Promise<void>;
71
+ /** Stops all activity. */
72
+ stop(): Promise<void>;
73
+ /** Force trigger the slow collection and reconciliation loops */
74
+ trigger(): Promise<void>;
75
+ /** Returns L1 rollup constants. */
76
+ getConstants(): L1RollupConstants;
77
+ /** Starts collecting the given tx hashes for the given L2Block in the slow loop */
78
+ startCollecting(block: L2Block, txHashes: TxHash[]): void;
79
+ /** Collects the set of txs for the given block proposal as fast as possible */
80
+ collectFastForProposal(blockProposal: BlockProposal, txHashes: TxHash[] | string[], opts: {
81
+ deadline: Date;
82
+ pinnedPeer?: PeerId;
83
+ }): Promise<Tx[]>;
84
+ /** Collects the set of txs for the given mined block as fast as possible */
85
+ collectFastForBlock(block: L2Block, txHashes: TxHash[] | string[], opts: {
86
+ deadline: Date;
87
+ pinnedPeer?: PeerId;
88
+ }): Promise<Tx[]>;
89
+ /** Collects the set of txs for the given proposal or block as fast as possible */
90
+ collectFastFor(input: FastCollectionRequestInput, txHashes: TxHash[] | string[], opts: {
91
+ deadline: Date;
92
+ pinnedPeer?: PeerId;
93
+ }): Promise<Tx[]>;
94
+ /** Mark the given txs as found. Stops collecting them. */
95
+ private foundTxs;
96
+ /**
97
+ * Stop collecting all txs for blocks less than or requal to the block number specified.
98
+ * To be called when we no longer care about gathering txs up to a certain block, eg when they become proven or finalized.
99
+ */
100
+ stopCollectingForBlocksUpTo(blockNumber: number): void;
101
+ /**
102
+ * Stop collecting all txs for blocks greater than the block number specified.
103
+ * To be called when there is a chain prune and previously mined txs are no longer relevant.
104
+ */
105
+ stopCollectingForBlocksAfter(blockNumber: number): void;
106
+ /** Every now and then, check if the pool has received one of the txs we are looking for, just to catch any race conditions */
107
+ private reconcileFoundTxsWithPool;
108
+ }
109
+ //# sourceMappingURL=tx_collection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tx_collection.d.ts","sourceRoot":"","sources":["../../../src/services/tx_collection/tx_collection.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,127 @@
1
+ import { compactArray } from '@aztec/foundation/collection';
2
+ import { createLogger } from '@aztec/foundation/log';
3
+ import { RunningPromise } from '@aztec/foundation/promise';
4
+ import { DateProvider } from '@aztec/foundation/timer';
5
+ import { getTelemetryClient } from '@aztec/telemetry-client';
6
+ import { FastTxCollection } from './fast_tx_collection.js';
7
+ import { SlowTxCollection } from './slow_tx_collection.js';
8
+ import { TxCollectionSink } from './tx_collection_sink.js';
9
+ /**
10
+ * Coordinates tx collection from remote RPC nodes and reqresp.
11
+ *
12
+ * The slow collection loops are used for periodically collecting missing txs from mined blocks,
13
+ * from remote RPC nodes and via reqresp. The fast collection methods are used to quickly gather
14
+ * txs, usually for attesting to block proposals or preparing to prove an epoch. The slow and fast
15
+ * collection instances both send their txs to the collection sink, which handles metrics and adds
16
+ * them to the tx pool. Whenever a tx is added to either the sink or the pool, this service is notified
17
+ * via events and notifies the slow and fast collection loops to stop collecting that tx, so that we don't
18
+ * collect the same tx multiple times.
19
+ */ export class TxCollection {
20
+ reqResp;
21
+ nodes;
22
+ constants;
23
+ txPool;
24
+ config;
25
+ dateProvider;
26
+ log;
27
+ /** Slow collection background loops */ slowCollection;
28
+ /** Fast collection methods */ fastCollection;
29
+ /** Loop for periodically reconciling found transactions from the tx pool in case we missed some */ reconcileFoundTxsLoop;
30
+ /** Handles txs found by the slow and fast collection loops */ txCollectionSink;
31
+ /** Handler for the txs-added event from the tx pool */ handleTxsAddedToPool;
32
+ /** Handler for the txs-added event from the tx collection sink */ handleTxsFound;
33
+ constructor(reqResp, nodes, constants, txPool, config, dateProvider = new DateProvider(), telemetryClient = getTelemetryClient(), log = createLogger('p2p:tx_collection_service')){
34
+ this.reqResp = reqResp;
35
+ this.nodes = nodes;
36
+ this.constants = constants;
37
+ this.txPool = txPool;
38
+ this.config = config;
39
+ this.dateProvider = dateProvider;
40
+ this.log = log;
41
+ this.txCollectionSink = new TxCollectionSink(this.txPool, telemetryClient, this.log);
42
+ this.fastCollection = new FastTxCollection(this.reqResp, this.nodes, this.txCollectionSink, this.config, this.dateProvider, this.log);
43
+ this.slowCollection = new SlowTxCollection(this.reqResp, this.nodes, this.txCollectionSink, this.fastCollection, constants, this.config, this.dateProvider, this.log);
44
+ this.reconcileFoundTxsLoop = new RunningPromise(()=>this.reconcileFoundTxsWithPool(), this.log, this.config.txCollectionReconcileIntervalMs);
45
+ this.handleTxsFound = (args)=>{
46
+ this.foundTxs(args.txs);
47
+ };
48
+ this.txCollectionSink.on('txs-added', this.handleTxsFound);
49
+ this.handleTxsAddedToPool = (args)=>{
50
+ const { txs, source } = args;
51
+ if (source !== 'tx-collection') {
52
+ this.foundTxs(txs);
53
+ }
54
+ };
55
+ this.txPool.on('txs-added', this.handleTxsAddedToPool);
56
+ }
57
+ /** Starts all collection loops. */ start() {
58
+ this.slowCollection.start();
59
+ this.reconcileFoundTxsLoop.start();
60
+ // TODO(palla/txs): Collect mined unproven tx hashes for txs we dont have in the pool and populate missingTxs on startup
61
+ return Promise.resolve();
62
+ }
63
+ /** Stops all activity. */ async stop() {
64
+ await Promise.all([
65
+ this.slowCollection.stop(),
66
+ this.fastCollection.stop(),
67
+ this.reconcileFoundTxsLoop.stop()
68
+ ]);
69
+ this.txPool.removeListener('txs-added', this.handleTxsAddedToPool);
70
+ this.txCollectionSink.removeListener('txs-added', this.handleTxsFound);
71
+ }
72
+ /** Force trigger the slow collection and reconciliation loops */ async trigger() {
73
+ await Promise.all([
74
+ this.reconcileFoundTxsLoop.trigger(),
75
+ this.slowCollection.trigger()
76
+ ]);
77
+ }
78
+ /** Returns L1 rollup constants. */ getConstants() {
79
+ return this.constants;
80
+ }
81
+ /** Starts collecting the given tx hashes for the given L2Block in the slow loop */ startCollecting(block, txHashes) {
82
+ return this.slowCollection.startCollecting(block, txHashes);
83
+ }
84
+ /** Collects the set of txs for the given block proposal as fast as possible */ collectFastForProposal(blockProposal, txHashes, opts) {
85
+ return this.collectFastFor({
86
+ type: 'proposal',
87
+ blockProposal
88
+ }, txHashes, opts);
89
+ }
90
+ /** Collects the set of txs for the given mined block as fast as possible */ collectFastForBlock(block, txHashes, opts) {
91
+ return this.collectFastFor({
92
+ type: 'block',
93
+ block
94
+ }, txHashes, opts);
95
+ }
96
+ /** Collects the set of txs for the given proposal or block as fast as possible */ collectFastFor(input, txHashes, opts) {
97
+ return this.fastCollection.collectFastFor(input, txHashes, opts);
98
+ }
99
+ /** Mark the given txs as found. Stops collecting them. */ foundTxs(txs) {
100
+ this.slowCollection.foundTxs(txs);
101
+ this.fastCollection.foundTxs(txs);
102
+ }
103
+ /**
104
+ * Stop collecting all txs for blocks less than or requal to the block number specified.
105
+ * To be called when we no longer care about gathering txs up to a certain block, eg when they become proven or finalized.
106
+ */ stopCollectingForBlocksUpTo(blockNumber) {
107
+ this.slowCollection.stopCollectingForBlocksUpTo(blockNumber);
108
+ this.fastCollection.stopCollectingForBlocksUpTo(blockNumber);
109
+ }
110
+ /**
111
+ * Stop collecting all txs for blocks greater than the block number specified.
112
+ * To be called when there is a chain prune and previously mined txs are no longer relevant.
113
+ */ stopCollectingForBlocksAfter(blockNumber) {
114
+ this.slowCollection.stopCollectingForBlocksAfter(blockNumber);
115
+ this.fastCollection.stopCollectingForBlocksAfter(blockNumber);
116
+ }
117
+ /** Every now and then, check if the pool has received one of the txs we are looking for, just to catch any race conditions */ async reconcileFoundTxsWithPool() {
118
+ const missingTxHashes = this.slowCollection.getMissingTxHashes();
119
+ const foundTxs = compactArray(await this.txPool.getTxsByHash(missingTxHashes));
120
+ if (foundTxs.length > 0) {
121
+ this.log.verbose(`Found ${foundTxs.length} txs in the pool during reconciliation`, {
122
+ foundTxs: foundTxs.map((t)=>t.getTxHash().toString())
123
+ });
124
+ this.foundTxs(foundTxs);
125
+ }
126
+ }
127
+ }
@@ -0,0 +1,30 @@
1
+ import type { Logger } from '@aztec/foundation/log';
2
+ import type { TypedEventEmitter } from '@aztec/foundation/types';
3
+ import { Tx, type TxHash } from '@aztec/stdlib/tx';
4
+ import type { TelemetryClient } from '@aztec/telemetry-client';
5
+ import type { TxPool, TxPoolEvents } from '../../mem_pools/tx_pool/tx_pool.js';
6
+ import type { CollectionMethod } from './tx_collection.js';
7
+ declare const TxCollectionSink_base: new () => TypedEventEmitter<TxPoolEvents>;
8
+ /**
9
+ * Executes collection requests from the fast and slow collection loops, and handles collected txs
10
+ * by adding them to the tx pool and emitting events, as well as handling logging and metrics.
11
+ */
12
+ export declare class TxCollectionSink extends TxCollectionSink_base {
13
+ private readonly txPool;
14
+ private readonly log;
15
+ private readonly instrumentation;
16
+ constructor(txPool: TxPool, telemetryClient: TelemetryClient, log: Logger);
17
+ collect(collectValidTxsFn: (txHashes: TxHash[]) => Promise<(Tx | undefined)[]>, requested: TxHash[], info: Record<string, any> & {
18
+ description: string;
19
+ method: CollectionMethod;
20
+ }): Promise<{
21
+ txs: Tx[];
22
+ } | {
23
+ txs: Tx[];
24
+ requested: TxHash[];
25
+ duration: number;
26
+ }>;
27
+ private foundTxs;
28
+ }
29
+ export {};
30
+ //# sourceMappingURL=tx_collection_sink.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tx_collection_sink.d.ts","sourceRoot":"","sources":["../../../src/services/tx_collection/tx_collection_sink.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAEpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,EAAE,EAAE,EAAE,KAAK,MAAM,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAI/D,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAE/E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;qCAMJ,UAAU,iBAAiB,CAAC,YAAY,CAAC;AAJhG;;;GAGG;AACH,qBAAa,gBAAiB,SAAQ,qBAA2D;IAI7F,OAAO,CAAC,QAAQ,CAAC,MAAM;IAEvB,OAAO,CAAC,QAAQ,CAAC,GAAG;IALtB,OAAO,CAAC,QAAQ,CAAC,eAAe,CAA8B;gBAG3C,MAAM,EAAE,MAAM,EAC/B,eAAe,EAAE,eAAe,EACf,GAAG,EAAE,MAAM;IAMjB,OAAO,CAClB,iBAAiB,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,EAAE,CAAC,EACtE,SAAS,EAAE,MAAM,EAAE,EACnB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,gBAAgB,CAAA;KAAE;;;;;;;YA0CjE,QAAQ;CAwBvB"}
@@ -0,0 +1,81 @@
1
+ import { elapsed } from '@aztec/foundation/timer';
2
+ import EventEmitter from 'node:events';
3
+ import { TxCollectionInstrumentation } from './instrumentation.js';
4
+ /**
5
+ * Executes collection requests from the fast and slow collection loops, and handles collected txs
6
+ * by adding them to the tx pool and emitting events, as well as handling logging and metrics.
7
+ */ export class TxCollectionSink extends EventEmitter {
8
+ txPool;
9
+ log;
10
+ instrumentation;
11
+ constructor(txPool, telemetryClient, log){
12
+ super(), this.txPool = txPool, this.log = log;
13
+ this.instrumentation = new TxCollectionInstrumentation(telemetryClient, 'TxCollection');
14
+ }
15
+ async collect(collectValidTxsFn, requested, info) {
16
+ this.log.trace(`Requesting ${requested.length} txs via ${info.description}`, {
17
+ ...info,
18
+ requestedTxs: requested.map((t)=>t.toString())
19
+ });
20
+ // Execute collection function and measure the time taken, catching any errors.
21
+ const [duration, txs] = await elapsed(async ()=>{
22
+ try {
23
+ const response = await collectValidTxsFn(requested);
24
+ return response.filter((tx)=>tx !== undefined);
25
+ } catch (err) {
26
+ this.log.error(`Error collecting txs via ${info.description}`, err, {
27
+ ...info,
28
+ requestedTxs: requested.map((hash)=>hash.toString())
29
+ });
30
+ return [];
31
+ }
32
+ });
33
+ if (txs.length === 0) {
34
+ this.log.trace(`No txs found via ${info.description}`, {
35
+ ...info,
36
+ requestedTxs: requested.map((t)=>t.toString())
37
+ });
38
+ return {
39
+ txs,
40
+ requested,
41
+ duration
42
+ };
43
+ }
44
+ this.log.verbose(`Collected ${txs.length} txs out of ${requested.length} requested via ${info.description} in ${duration}ms`, {
45
+ ...info,
46
+ duration,
47
+ txs: txs.map((t)=>t.getTxHash().toString()),
48
+ requestedTxs: requested.map((t)=>t.toString())
49
+ });
50
+ return await this.foundTxs(txs, {
51
+ ...info,
52
+ duration
53
+ });
54
+ }
55
+ async foundTxs(txs, info) {
56
+ // Report metrics for the collection
57
+ this.instrumentation.increaseTxsFor(info.method, txs.length, info.duration);
58
+ // Mark txs as found in the slow missing txs set and all fast requests
59
+ this.emit('txs-added', {
60
+ txs
61
+ });
62
+ // Add the txs to the tx pool (should not fail, but we catch it just in case)
63
+ try {
64
+ await this.txPool.addTxs(txs, {
65
+ source: `tx-collection`
66
+ });
67
+ } catch (err) {
68
+ this.log.error(`Error adding txs to the pool via ${info.description}`, err, {
69
+ ...info,
70
+ txs: txs.map((tx)=>tx.txHash.toString())
71
+ });
72
+ // Return no txs since none have been added
73
+ return {
74
+ txs: []
75
+ };
76
+ }
77
+ return {
78
+ txs
79
+ };
80
+ }
81
+ }
@@ -0,0 +1,18 @@
1
+ import type { ChainConfig } from '@aztec/stdlib/config';
2
+ import { type AztecNode } from '@aztec/stdlib/interfaces/client';
3
+ import type { Tx, TxHash } from '@aztec/stdlib/tx';
4
+ import { type ComponentsVersions } from '@aztec/stdlib/versioning';
5
+ export interface TxSource {
6
+ getInfo(): string;
7
+ getTxsByHash(txHashes: TxHash[]): Promise<(Tx | undefined)[]>;
8
+ }
9
+ export declare class NodeRpcTxSource implements TxSource {
10
+ private readonly client;
11
+ private readonly info;
12
+ constructor(client: Pick<AztecNode, 'getTxsByHash'>, info: string);
13
+ static fromUrl(nodeUrl: string, versions: ComponentsVersions): NodeRpcTxSource;
14
+ getInfo(): string;
15
+ getTxsByHash(txHashes: TxHash[]): Promise<(Tx | undefined)[]>;
16
+ }
17
+ export declare function createNodeRpcTxSources(urls: string[], chainConfig: ChainConfig): NodeRpcTxSource[];
18
+ //# sourceMappingURL=tx_source.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tx_source.d.ts","sourceRoot":"","sources":["../../../src/services/tx_collection/tx_source.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,KAAK,SAAS,EAAyB,MAAM,iCAAiC,CAAC;AACxF,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,KAAK,kBAAkB,EAAmC,MAAM,0BAA0B,CAAC;AAGpG,MAAM,WAAW,QAAQ;IACvB,OAAO,IAAI,MAAM,CAAC;IAClB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,EAAE,CAAC,CAAC;CAC/D;AAED,qBAAa,eAAgB,YAAW,QAAQ;IAE5C,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI;gBADJ,MAAM,EAAE,IAAI,CAAC,SAAS,EAAE,cAAc,CAAC,EACvC,IAAI,EAAE,MAAM;WAGjB,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,kBAAkB,GAAG,eAAe;IAK9E,OAAO;IAIP,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,EAAE,CAAC;CAGrE;AAED,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,WAAW,qBAG9E"}
@@ -0,0 +1,31 @@
1
+ import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
2
+ import { protocolContractTreeRoot } from '@aztec/protocol-contracts';
3
+ import { createAztecNodeClient } from '@aztec/stdlib/interfaces/client';
4
+ import { getComponentsVersionsFromConfig } from '@aztec/stdlib/versioning';
5
+ import { makeTracedFetch } from '@aztec/telemetry-client';
6
+ export class NodeRpcTxSource {
7
+ client;
8
+ info;
9
+ constructor(client, info){
10
+ this.client = client;
11
+ this.info = info;
12
+ }
13
+ static fromUrl(nodeUrl, versions) {
14
+ const client = createAztecNodeClient(nodeUrl, versions, makeTracedFetch([
15
+ 1,
16
+ 2,
17
+ 3
18
+ ], false));
19
+ return new NodeRpcTxSource(client, nodeUrl);
20
+ }
21
+ getInfo() {
22
+ return this.info;
23
+ }
24
+ getTxsByHash(txHashes) {
25
+ return this.client.getTxsByHash(txHashes);
26
+ }
27
+ }
28
+ export function createNodeRpcTxSources(urls, chainConfig) {
29
+ const versions = getComponentsVersionsFromConfig(chainConfig, protocolContractTreeRoot, getVKTreeRoot());
30
+ return urls.map((url)=>NodeRpcTxSource.fromUrl(url, versions));
31
+ }
@@ -0,0 +1,49 @@
1
+ import { type Logger } from '@aztec/foundation/log';
2
+ import type { L2Block } from '@aztec/stdlib/block';
3
+ import type { ITxProvider } from '@aztec/stdlib/interfaces/server';
4
+ import type { BlockProposal } from '@aztec/stdlib/p2p';
5
+ import { Tx, TxHash } from '@aztec/stdlib/tx';
6
+ import { type TelemetryClient } from '@aztec/telemetry-client';
7
+ import type { PeerId } from '@libp2p/interface';
8
+ import type { P2PClient } from '../client/p2p_client.js';
9
+ import type { TxPool } from '../mem_pools/index.js';
10
+ import type { TxCollection } from './tx_collection/tx_collection.js';
11
+ import { TxProviderInstrumentation } from './tx_provider_instrumentation.js';
12
+ /**
13
+ * Gathers and returns txs given a block proposal, block, or their hashes.
14
+ * Loads available txs from the tx pool, and relies on a TxCollection service to collect txs from the network and other nodes.
15
+ */
16
+ export declare class TxProvider implements ITxProvider {
17
+ private txCollection;
18
+ private txPool;
19
+ private txValidator;
20
+ private log;
21
+ protected instrumentation: TxProviderInstrumentation;
22
+ constructor(txCollection: TxCollection, txPool: TxPool, txValidator: Pick<P2PClient, 'validate'>, log?: Logger, client?: TelemetryClient);
23
+ /** Returns txs from the tx pool given their hashes.*/
24
+ getAvailableTxs(txHashes: TxHash[]): Promise<{
25
+ txs: Tx[];
26
+ missingTxs: TxHash[];
27
+ }>;
28
+ /** Gathers txs from the tx pool, proposal body, remote rpc nodes, and reqresp. */
29
+ getTxsForBlockProposal(blockProposal: BlockProposal, opts: {
30
+ pinnedPeer: PeerId | undefined;
31
+ deadline: Date;
32
+ }): Promise<{
33
+ txs: Tx[];
34
+ missingTxs: TxHash[];
35
+ }>;
36
+ /** Gathers txs from the tx pool, remote rpc nodes, and reqresp. */
37
+ getTxsForBlock(block: L2Block, opts: {
38
+ deadline: Date;
39
+ }): Promise<{
40
+ txs: Tx[];
41
+ missingTxs: TxHash[];
42
+ }>;
43
+ private getOrderedTxsFromAllSources;
44
+ private orderTxs;
45
+ private getTxsFromAllSources;
46
+ private extractFromProposal;
47
+ private processProposalTxs;
48
+ }
49
+ //# sourceMappingURL=tx_provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tx_provider.d.ts","sourceRoot":"","sources":["../../src/services/tx_provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,uBAAuB,CAAC;AAElE,OAAO,KAAK,EAAa,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,KAAK,eAAe,EAAsB,MAAM,yBAAyB,CAAC;AAEnF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,KAAK,EAA8B,YAAY,EAAE,MAAM,kCAAkC,CAAC;AACjG,OAAO,EAAE,yBAAyB,EAAE,MAAM,kCAAkC,CAAC;AAE7E;;;GAGG;AACH,qBAAa,UAAW,YAAW,WAAW;IAI1C,OAAO,CAAC,YAAY;IACpB,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,GAAG;IANb,SAAS,CAAC,eAAe,EAAE,yBAAyB,CAAC;gBAG3C,YAAY,EAAE,YAAY,EAC1B,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,IAAI,CAAC,SAAS,EAAE,UAAU,CAAC,EACxC,GAAG,GAAE,MAAyC,EACtD,MAAM,GAAE,eAAsC;IAKhD,sDAAsD;IACzC,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAAE,GAAG,EAAE,EAAE,EAAE,CAAC;QAAC,UAAU,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IAoB9F,kFAAkF;IAC3E,sBAAsB,CAC3B,aAAa,EAAE,aAAa,EAC5B,IAAI,EAAE;QAAE,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;QAAC,QAAQ,EAAE,IAAI,CAAA;KAAE,GACvD,OAAO,CAAC;QAAE,GAAG,EAAE,EAAE,EAAE,CAAC;QAAC,UAAU,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IAS/C,mEAAmE;IAC5D,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE;QAAE,QAAQ,EAAE,IAAI,CAAA;KAAE,GAAG,OAAO,CAAC;QAAE,GAAG,EAAE,EAAE,EAAE,CAAC;QAAC,UAAU,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;YAS/F,2BAA2B;IAgCzC,OAAO,CAAC,QAAQ;YAKF,oBAAoB;IAsFlC,OAAO,CAAC,mBAAmB;YAOb,kBAAkB;CAOjC"}