@aztec/p2p 0.0.1-commit.f2ce05ee → 0.0.1-commit.f8ca9b2f3

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 (163) hide show
  1. package/dest/client/factory.d.ts +3 -3
  2. package/dest/client/factory.d.ts.map +1 -1
  3. package/dest/client/factory.js +38 -7
  4. package/dest/client/interface.d.ts +26 -15
  5. package/dest/client/interface.d.ts.map +1 -1
  6. package/dest/client/p2p_client.d.ts +31 -35
  7. package/dest/client/p2p_client.d.ts.map +1 -1
  8. package/dest/client/p2p_client.js +97 -138
  9. package/dest/config.d.ts +10 -2
  10. package/dest/config.d.ts.map +1 -1
  11. package/dest/config.js +9 -0
  12. package/dest/index.d.ts +2 -1
  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 +18 -11
  16. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  17. package/dest/mem_pools/attestation_pool/attestation_pool.js +62 -37
  18. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +1 -1
  19. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  20. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +53 -53
  21. package/dest/mem_pools/attestation_pool/index.d.ts +2 -2
  22. package/dest/mem_pools/attestation_pool/index.d.ts.map +1 -1
  23. package/dest/mem_pools/attestation_pool/index.js +1 -1
  24. package/dest/mem_pools/index.d.ts +2 -1
  25. package/dest/mem_pools/index.d.ts.map +1 -1
  26. package/dest/mem_pools/interface.d.ts +3 -3
  27. package/dest/mem_pools/interface.d.ts.map +1 -1
  28. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts +87 -0
  29. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts.map +1 -0
  30. package/dest/mem_pools/tx_pool_v2/deleted_pool.js +180 -0
  31. package/dest/mem_pools/tx_pool_v2/index.d.ts +2 -1
  32. package/dest/mem_pools/tx_pool_v2/index.d.ts.map +1 -1
  33. package/dest/mem_pools/tx_pool_v2/index.js +1 -0
  34. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +3 -3
  35. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
  36. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +25 -3
  37. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
  38. package/dest/mem_pools/tx_pool_v2/tx_metadata.js +36 -4
  39. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +1 -1
  40. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
  41. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +77 -74
  42. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +3 -3
  43. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
  44. package/dest/msg_validators/tx_validator/block_header_validator.d.ts +16 -3
  45. package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
  46. package/dest/msg_validators/tx_validator/block_header_validator.js +1 -1
  47. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts +13 -3
  48. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts.map +1 -1
  49. package/dest/msg_validators/tx_validator/double_spend_validator.js +4 -4
  50. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +20 -4
  51. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -1
  52. package/dest/msg_validators/tx_validator/timestamp_validator.js +2 -2
  53. package/dest/services/dummy_service.d.ts +6 -2
  54. package/dest/services/dummy_service.d.ts.map +1 -1
  55. package/dest/services/dummy_service.js +3 -0
  56. package/dest/services/encoding.d.ts +2 -2
  57. package/dest/services/encoding.d.ts.map +1 -1
  58. package/dest/services/encoding.js +2 -2
  59. package/dest/services/libp2p/libp2p_service.d.ts +11 -3
  60. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  61. package/dest/services/libp2p/libp2p_service.js +71 -35
  62. package/dest/services/reqresp/interface.d.ts +10 -1
  63. package/dest/services/reqresp/interface.d.ts.map +1 -1
  64. package/dest/services/reqresp/interface.js +15 -1
  65. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +3 -3
  66. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -1
  67. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +7 -1
  68. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
  69. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +15 -0
  70. package/dest/services/reqresp/protocols/tx.d.ts +7 -1
  71. package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
  72. package/dest/services/reqresp/protocols/tx.js +20 -0
  73. package/dest/services/reqresp/reqresp.d.ts +1 -1
  74. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  75. package/dest/services/reqresp/reqresp.js +11 -4
  76. package/dest/services/service.d.ts +18 -1
  77. package/dest/services/service.d.ts.map +1 -1
  78. package/dest/services/tx_collection/config.d.ts +7 -1
  79. package/dest/services/tx_collection/config.d.ts.map +1 -1
  80. package/dest/services/tx_collection/config.js +16 -0
  81. package/dest/services/tx_collection/fast_tx_collection.d.ts +3 -1
  82. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
  83. package/dest/services/tx_collection/fast_tx_collection.js +17 -3
  84. package/dest/services/tx_collection/file_store_tx_collection.d.ts +44 -0
  85. package/dest/services/tx_collection/file_store_tx_collection.d.ts.map +1 -0
  86. package/dest/services/tx_collection/file_store_tx_collection.js +118 -0
  87. package/dest/services/tx_collection/file_store_tx_source.d.ts +27 -0
  88. package/dest/services/tx_collection/file_store_tx_source.d.ts.map +1 -0
  89. package/dest/services/tx_collection/file_store_tx_source.js +57 -0
  90. package/dest/services/tx_collection/index.d.ts +2 -1
  91. package/dest/services/tx_collection/index.d.ts.map +1 -1
  92. package/dest/services/tx_collection/index.js +1 -0
  93. package/dest/services/tx_collection/slow_tx_collection.d.ts +3 -1
  94. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -1
  95. package/dest/services/tx_collection/slow_tx_collection.js +48 -19
  96. package/dest/services/tx_collection/tx_collection.d.ts +17 -7
  97. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
  98. package/dest/services/tx_collection/tx_collection.js +58 -2
  99. package/dest/services/tx_collection/tx_collection_sink.d.ts +15 -6
  100. package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -1
  101. package/dest/services/tx_collection/tx_collection_sink.js +13 -7
  102. package/dest/services/tx_file_store/config.d.ts +1 -3
  103. package/dest/services/tx_file_store/config.d.ts.map +1 -1
  104. package/dest/services/tx_file_store/config.js +0 -4
  105. package/dest/services/tx_file_store/tx_file_store.d.ts +3 -3
  106. package/dest/services/tx_file_store/tx_file_store.d.ts.map +1 -1
  107. package/dest/services/tx_provider.d.ts +3 -3
  108. package/dest/services/tx_provider.d.ts.map +1 -1
  109. package/dest/services/tx_provider.js +5 -4
  110. package/dest/test-helpers/make-test-p2p-clients.d.ts +3 -3
  111. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  112. package/dest/test-helpers/mock-pubsub.d.ts +27 -1
  113. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  114. package/dest/test-helpers/mock-pubsub.js +97 -2
  115. package/dest/test-helpers/testbench-utils.d.ts +30 -24
  116. package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
  117. package/dest/test-helpers/testbench-utils.js +87 -35
  118. package/dest/testbench/p2p_client_testbench_worker.js +1 -1
  119. package/package.json +14 -14
  120. package/src/client/factory.ts +65 -8
  121. package/src/client/interface.ts +26 -13
  122. package/src/client/p2p_client.ts +123 -158
  123. package/src/config.ts +16 -0
  124. package/src/index.ts +1 -0
  125. package/src/mem_pools/attestation_pool/attestation_pool.ts +87 -44
  126. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +57 -53
  127. package/src/mem_pools/attestation_pool/index.ts +3 -3
  128. package/src/mem_pools/index.ts +3 -0
  129. package/src/mem_pools/interface.ts +2 -2
  130. package/src/mem_pools/tx_pool_v2/README.md +59 -9
  131. package/src/mem_pools/tx_pool_v2/deleted_pool.ts +234 -0
  132. package/src/mem_pools/tx_pool_v2/index.ts +1 -0
  133. package/src/mem_pools/tx_pool_v2/interfaces.ts +2 -2
  134. package/src/mem_pools/tx_pool_v2/tx_metadata.ts +51 -5
  135. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +90 -77
  136. package/src/msg_validators/tx_validator/aggregate_tx_validator.ts +2 -2
  137. package/src/msg_validators/tx_validator/block_header_validator.ts +15 -3
  138. package/src/msg_validators/tx_validator/double_spend_validator.ts +11 -6
  139. package/src/msg_validators/tx_validator/timestamp_validator.ts +19 -14
  140. package/src/services/dummy_service.ts +6 -0
  141. package/src/services/encoding.ts +2 -2
  142. package/src/services/libp2p/libp2p_service.ts +70 -37
  143. package/src/services/reqresp/interface.ts +26 -1
  144. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +2 -2
  145. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +17 -0
  146. package/src/services/reqresp/protocols/tx.ts +22 -0
  147. package/src/services/reqresp/reqresp.ts +13 -3
  148. package/src/services/service.ts +20 -0
  149. package/src/services/tx_collection/config.ts +26 -0
  150. package/src/services/tx_collection/fast_tx_collection.ts +14 -2
  151. package/src/services/tx_collection/file_store_tx_collection.ts +152 -0
  152. package/src/services/tx_collection/file_store_tx_source.ts +70 -0
  153. package/src/services/tx_collection/index.ts +1 -0
  154. package/src/services/tx_collection/slow_tx_collection.ts +55 -26
  155. package/src/services/tx_collection/tx_collection.ts +78 -12
  156. package/src/services/tx_collection/tx_collection_sink.ts +17 -7
  157. package/src/services/tx_file_store/config.ts +0 -6
  158. package/src/services/tx_file_store/tx_file_store.ts +4 -4
  159. package/src/services/tx_provider.ts +8 -7
  160. package/src/test-helpers/make-test-p2p-clients.ts +3 -3
  161. package/src/test-helpers/mock-pubsub.ts +133 -3
  162. package/src/test-helpers/testbench-utils.ts +100 -40
  163. package/src/testbench/p2p_client_testbench_worker.ts +1 -1
@@ -1,12 +1,15 @@
1
1
  import { GENESIS_BLOCK_HEADER_HASH } from '@aztec/constants';
2
+ import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
3
  import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
3
4
  import { createLogger } from '@aztec/foundation/log';
5
+ import { RunningPromise } from '@aztec/foundation/promise';
4
6
  import { DateProvider } from '@aztec/foundation/timer';
5
7
  import type { AztecAsyncKVStore, AztecAsyncSingleton } from '@aztec/kv-store';
6
8
  import { L2TipsKVStore } from '@aztec/kv-store/stores';
7
9
  import {
8
10
  type EthAddress,
9
11
  type L2Block,
12
+ type L2BlockId,
10
13
  type L2BlockSource,
11
14
  L2BlockStream,
12
15
  type L2BlockStreamEvent,
@@ -22,7 +25,7 @@ import {
22
25
  type CheckpointProposal,
23
26
  type P2PClientType,
24
27
  } from '@aztec/stdlib/p2p';
25
- import type { Tx, TxHash } from '@aztec/stdlib/tx';
28
+ import type { BlockHeader, Tx, TxHash } from '@aztec/stdlib/tx';
26
29
  import { Attributes, type TelemetryClient, WithTracer, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
27
30
 
28
31
  import type { PeerId } from '@libp2p/interface';
@@ -31,7 +34,7 @@ import type { ENR } from '@nethermindeth/enr';
31
34
  import { type P2PConfig, getP2PDefaultConfig } from '../config.js';
32
35
  import type { AttestationPoolApi } from '../mem_pools/attestation_pool/attestation_pool.js';
33
36
  import type { MemPools } from '../mem_pools/interface.js';
34
- import type { TxPool } from '../mem_pools/tx_pool/index.js';
37
+ import type { TxPoolV2 } from '../mem_pools/tx_pool_v2/interfaces.js';
35
38
  import type { AuthRequest, StatusMessage } from '../services/index.js';
36
39
  import {
37
40
  ReqRespSubProtocol,
@@ -40,6 +43,7 @@ import {
40
43
  } from '../services/reqresp/interface.js';
41
44
  import { chunkTxHashesRequest } from '../services/reqresp/protocols/tx.js';
42
45
  import type {
46
+ DuplicateAttestationInfo,
43
47
  DuplicateProposalInfo,
44
48
  P2PBlockReceivedCallback,
45
49
  P2PCheckpointReceivedCallback,
@@ -70,7 +74,7 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
70
74
  private l2Tips: L2TipsStore;
71
75
  private synchedLatestSlot: AztecAsyncSingleton<bigint>;
72
76
 
73
- private txPool: TxPool;
77
+ private txPool: TxPoolV2;
74
78
  private attestationPool: AttestationPoolApi;
75
79
 
76
80
  private config: P2PConfig;
@@ -81,14 +85,12 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
81
85
 
82
86
  private validatorAddresses: EthAddress[] = [];
83
87
 
84
- /**
85
- * In-memory P2P client constructor.
86
- * @param store - The client's instance of the KV store.
87
- * @param l2BlockSource - P2P client's source for fetching existing blocks.
88
- * @param txPool - The client's instance of a transaction pool. Defaults to in-memory implementation.
89
- * @param p2pService - The concrete instance of p2p networking to use.
90
- * @param log - A logger.
91
- */
88
+ /** Tracks the last slot for which we called prepareForSlot */
89
+ private lastSlotProcessed: SlotNumber = SlotNumber.ZERO;
90
+
91
+ /** Polls for slot changes and calls prepareForSlot on the tx pool */
92
+ private slotMonitor: RunningPromise | undefined;
93
+
92
94
  constructor(
93
95
  _clientType: T,
94
96
  private store: AztecAsyncKVStore,
@@ -97,6 +99,7 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
97
99
  private p2pService: P2PService,
98
100
  private txCollection: TxCollection,
99
101
  private txFileStore: TxFileStore | undefined,
102
+ private epochCache: EpochCacheInterface,
100
103
  config: Partial<P2PConfig> = {},
101
104
  private _dateProvider: DateProvider = new DateProvider(),
102
105
  private telemetry: TelemetryClient = getTelemetryClient(),
@@ -166,10 +169,9 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
166
169
  return this.l2Tips.getL2BlockHash(number);
167
170
  }
168
171
 
169
- public updateP2PConfig(config: Partial<P2PConfig>): Promise<void> {
170
- this.txPool.updateConfig(config);
172
+ public async updateP2PConfig(config: Partial<P2PConfig>): Promise<void> {
173
+ await this.txPool.updateConfig(config);
171
174
  this.p2pService.updateConfig(config);
172
- return Promise.resolve();
173
175
  }
174
176
 
175
177
  public getL2Tips(): Promise<L2Tips> {
@@ -198,7 +200,7 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
198
200
  break;
199
201
  case 'chain-pruned':
200
202
  this.txCollection.stopCollectingForBlocksAfter(event.block.number);
201
- await this.handlePruneL2Blocks(event.block.number);
203
+ await this.handlePruneL2Blocks(event.block);
202
204
  break;
203
205
  case 'chain-checkpointed':
204
206
  break;
@@ -214,7 +216,6 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
214
216
  }
215
217
 
216
218
  #assertIsReady() {
217
- // this.log.info('Checking if p2p client is ready, current state: ', this.currentState);
218
219
  if (!this.isReady()) {
219
220
  throw new Error('P2P client not ready');
220
221
  }
@@ -232,6 +233,9 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
232
233
  return this.syncPromise;
233
234
  }
234
235
 
236
+ // Start the tx pool first, as it needs to hydrate state from persistence
237
+ await this.txPool.start();
238
+
235
239
  // get the current latest block numbers
236
240
  const latestBlockNumbers = await this.l2BlockSource.getL2Tips();
237
241
  this.latestBlockNumberAtStart = latestBlockNumbers.proposed.number;
@@ -282,6 +286,15 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
282
286
  this.blockStream!.start();
283
287
  await this.txCollection.start();
284
288
  this.txFileStore?.start();
289
+
290
+ // Start slot monitor to call prepareForSlot when the slot changes
291
+ this.slotMonitor = new RunningPromise(
292
+ () => this.maybeCallPrepareForSlot(),
293
+ this.log,
294
+ this.config.slotCheckIntervalMS,
295
+ );
296
+ this.slotMonitor.start();
297
+
285
298
  return this.syncPromise;
286
299
  }
287
300
 
@@ -312,6 +325,8 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
312
325
  */
313
326
  public async stop() {
314
327
  this.log.debug('Stopping p2p client...');
328
+ await this.slotMonitor?.stop();
329
+ this.log.debug('Stopped slot monitor');
315
330
  await tryStop(this.txCollection);
316
331
  this.log.debug('Stopped tx collection service');
317
332
  await this.txFileStore?.stop();
@@ -320,6 +335,8 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
320
335
  this.log.debug('Stopped p2p service');
321
336
  await this.blockStream?.stop();
322
337
  this.log.debug('Stopped block downloader');
338
+ await this.txPool.stop();
339
+ this.log.debug('Stopped tx pool');
323
340
  await this.runningPromise;
324
341
  this.setCurrentState(P2PClientState.STOPPED);
325
342
  this.log.info('P2P client stopped');
@@ -339,9 +356,17 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
339
356
  public async broadcastProposal(proposal: BlockProposal): Promise<void> {
340
357
  this.log.verbose(`Broadcasting proposal for slot ${proposal.slotNumber} to peers`);
341
358
  // Store our own proposal so we can respond to req/resp requests for it
342
- const { totalForPosition } = await this.attestationPool.tryAddBlockProposal(proposal);
343
- if (totalForPosition > 1) {
344
- throw new Error(`Attempted to broadcast a duplicate block proposal for slot ${proposal.slotNumber}`);
359
+ const { count } = await this.attestationPool.tryAddBlockProposal(proposal);
360
+ if (count > 1) {
361
+ if (this.config.broadcastEquivocatedProposals) {
362
+ this.log.warn(`Broadcasting equivocated block proposal for slot ${proposal.slotNumber}`, {
363
+ slot: proposal.slotNumber,
364
+ archive: proposal.archive.toString(),
365
+ count,
366
+ });
367
+ } else {
368
+ throw new Error(`Attempted to broadcast a duplicate block proposal for slot ${proposal.slotNumber}`);
369
+ }
345
370
  }
346
371
  return this.p2pService.propagate(proposal);
347
372
  }
@@ -393,10 +418,14 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
393
418
  this.p2pService.registerDuplicateProposalCallback(callback);
394
419
  }
395
420
 
421
+ public registerDuplicateAttestationCallback(callback: (info: DuplicateAttestationInfo) => void): void {
422
+ this.p2pService.registerDuplicateAttestationCallback(callback);
423
+ }
424
+
396
425
  /**
397
426
  * Uses the batched Request Response protocol to request a set of transactions from the network.
398
427
  */
399
- public async requestTxsByHash(txHashes: TxHash[], pinnedPeerId: PeerId | undefined): Promise<Tx[]> {
428
+ private async requestTxsByHash(txHashes: TxHash[], pinnedPeerId: PeerId | undefined): Promise<Tx[]> {
400
429
  const timeoutMs = 8000; // Longer timeout for now
401
430
  const maxRetryAttempts = 10; // Keep retrying within the timeout
402
431
  const requests = chunkTxHashesRequest(txHashes);
@@ -413,7 +442,7 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
413
442
 
414
443
  const txs = txBatches.flat();
415
444
  if (txs.length > 0) {
416
- await this.txPool.addTxs(txs);
445
+ await this.txPool.addPendingTxs(txs);
417
446
  }
418
447
 
419
448
  const txHashesStr = txHashes.map(tx => tx.toString()).join(', ');
@@ -423,79 +452,40 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
423
452
  return txs;
424
453
  }
425
454
 
426
- public getPendingTxs(limit?: number, after?: TxHash): Promise<Tx[]> {
427
- return this.getTxs('pending', limit, after);
428
- }
429
-
430
- public getPendingTxCount(): Promise<number> {
431
- return this.txPool.getPendingTxCount();
432
- }
433
-
434
- public async *iteratePendingTxs(): AsyncIterableIterator<Tx> {
435
- for (const txHash of await this.txPool.getPendingTxHashes()) {
436
- const tx = await this.txPool.getTxByHash(txHash);
437
- if (tx) {
438
- yield tx;
439
- }
440
- }
441
- }
442
-
443
- /**
444
- * Returns all transactions in the transaction pool.
445
- * @param filter - The type of txs to return
446
- * @param limit - How many txs to return
447
- * @param after - If paginating, the last known tx hash. Will return txs after this hash
448
- * @returns An array of Txs.
449
- */
450
- public async getTxs(filter: 'all' | 'pending' | 'mined', limit?: number, after?: TxHash): Promise<Tx[]> {
455
+ public async getPendingTxs(limit?: number, after?: TxHash): Promise<Tx[]> {
451
456
  if (limit !== undefined && limit <= 0) {
452
457
  throw new TypeError('limit must be greater than 0');
453
458
  }
454
459
 
455
- let txs: Tx[] | undefined = undefined;
456
- let txHashes: TxHash[];
457
-
458
- if (filter === 'all') {
459
- txs = await this.txPool.getAllTxs();
460
- txHashes = await Promise.all(txs.map(tx => tx.getTxHash()));
461
- } else if (filter === 'mined') {
462
- const minedTxHashes = await this.txPool.getMinedTxHashes();
463
- txHashes = minedTxHashes.map(([txHash]) => txHash);
464
- } else if (filter === 'pending') {
465
- txHashes = await this.txPool.getPendingTxHashes();
466
- } else {
467
- const _: never = filter;
468
- throw new Error(`Unknown filter ${filter}`);
469
- }
460
+ let txHashes = await this.txPool.getPendingTxHashes();
470
461
 
471
462
  let startIndex = 0;
472
- let endIndex: number | undefined = undefined;
473
-
474
463
  if (after) {
475
464
  startIndex = txHashes.findIndex(txHash => after.equals(txHash));
476
-
477
- // if we can't find the last tx in our set then return an empty array as pagination is no longer valid.
478
465
  if (startIndex === -1) {
479
466
  return [];
480
467
  }
481
-
482
- // increment by one because we don't want to return the same tx again
483
468
  startIndex++;
484
469
  }
485
470
 
486
- if (limit !== undefined) {
487
- endIndex = startIndex + limit;
488
- }
489
-
471
+ const endIndex = limit !== undefined ? startIndex + limit : undefined;
490
472
  txHashes = txHashes.slice(startIndex, endIndex);
491
- if (txs) {
492
- txs = txs.slice(startIndex, endIndex);
493
- } else {
494
- const maybeTxs = await Promise.all(txHashes.map(txHash => this.txPool.getTxByHash(txHash)));
495
- txs = maybeTxs.filter((tx): tx is Tx => !!tx);
496
- }
497
473
 
498
- return txs;
474
+ const maybeTxs = await Promise.all(txHashes.map(txHash => this.txPool.getTxByHash(txHash)));
475
+ return maybeTxs.filter((tx): tx is Tx => !!tx);
476
+ }
477
+
478
+ public getPendingTxCount(): Promise<number> {
479
+ return this.txPool.getPendingTxCount();
480
+ }
481
+
482
+ public async *iteratePendingTxs(): AsyncIterableIterator<Tx> {
483
+ for (const txHash of await this.txPool.getPendingTxHashes()) {
484
+ const tx = await this.txPool.getTxByHash(txHash);
485
+ if (tx) {
486
+ yield tx;
487
+ }
488
+ }
499
489
  }
500
490
 
501
491
  /**
@@ -573,15 +563,19 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
573
563
  }
574
564
 
575
565
  /**
576
- * Verifies the 'tx' and, if valid, adds it to local tx pool and forwards it to other peers.
577
- * @param tx - The tx to verify.
566
+ * Accepts a transaction, adds it to local tx pool and forwards it to other peers.
567
+ * @param tx - The tx to send.
578
568
  * @returns Empty promise.
579
569
  **/
580
570
  public async sendTx(tx: Tx): Promise<void> {
581
- const addedCount = await this.addTxsToPool([tx]);
582
- const txAddedSuccessfully = addedCount === 1;
583
- if (txAddedSuccessfully) {
571
+ this.#assertIsReady();
572
+ const result = await this.txPool.addPendingTxs([tx]);
573
+ if (result.accepted.length === 1) {
584
574
  await this.p2pService.propagate(tx);
575
+ } else {
576
+ this.log.warn(
577
+ `Tx ${tx.getTxHash()} not propagated: accepted=${result.accepted.length} ignored=${result.ignored.length} rejected=${result.rejected.length}`,
578
+ );
585
579
  }
586
580
  }
587
581
 
@@ -591,7 +585,7 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
591
585
  **/
592
586
  public async addTxsToPool(txs: Tx[]): Promise<number> {
593
587
  this.#assertIsReady();
594
- return await this.txPool.addTxs(txs);
588
+ return (await this.txPool.addPendingTxs(txs)).accepted.length;
595
589
  }
596
590
 
597
591
  /**
@@ -599,8 +593,9 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
599
593
  * @param txHash - Hash of the tx to query.
600
594
  * @returns Pending or mined depending on its status, or undefined if not found.
601
595
  */
602
- public getTxStatus(txHash: TxHash): Promise<'pending' | 'mined' | 'deleted' | undefined> {
603
- return this.txPool.getTxStatus(txHash);
596
+ public async getTxStatus(txHash: TxHash): Promise<'pending' | 'mined' | 'deleted' | undefined> {
597
+ const status = await this.txPool.getTxStatus(txHash);
598
+ return status === 'protected' ? 'pending' : status;
604
599
  }
605
600
 
606
601
  public getEnr(): ENR | undefined {
@@ -612,14 +607,12 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
612
607
  }
613
608
 
614
609
  /**
615
- * Deletes the 'txs' from the pool.
616
- * NOT used if we use sendTx as reconcileTxPool will handle this.
617
- * @param txHashes - Hashes of the transactions to delete.
618
- * @returns Empty promise.
610
+ * Handles failed transaction execution by removing txs from the pool.
611
+ * @param txHashes - Hashes of the transactions that failed execution.
619
612
  **/
620
- public async deleteTxs(txHashes: TxHash[]): Promise<void> {
613
+ public async handleFailedExecution(txHashes: TxHash[]): Promise<void> {
621
614
  this.#assertIsReady();
622
- await this.txPool.deleteTxs(txHashes);
615
+ await this.txPool.handleFailedExecution(txHashes);
623
616
  }
624
617
 
625
618
  /**
@@ -679,14 +672,13 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
679
672
  }
680
673
 
681
674
  /**
682
- * Mark all txs from these blocks as mined.
675
+ * Handles mined blocks by marking the txs in them as mined.
683
676
  * @param blocks - A list of existing blocks with txs that the P2P client needs to ensure the tx pool is reconciled with.
684
677
  * @returns Empty promise.
685
678
  */
686
- private async markTxsAsMinedFromBlocks(blocks: L2Block[]): Promise<void> {
679
+ private async handleMinedBlocks(blocks: L2Block[]): Promise<void> {
687
680
  for (const block of blocks) {
688
- const txHashes = block.body.txEffects.map(txEffect => txEffect.txHash);
689
- await this.txPool.markAsMined(txHashes, block.header);
681
+ await this.txPool.handleMinedBlock(block);
690
682
  }
691
683
  }
692
684
 
@@ -697,16 +689,14 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
697
689
  */
698
690
  private async handleLatestL2Blocks(blocks: L2Block[]): Promise<void> {
699
691
  if (!blocks.length) {
700
- return Promise.resolve();
692
+ return;
701
693
  }
702
694
 
703
- await this.markTxsAsMinedFromBlocks(blocks);
704
- await this.txPool.clearNonEvictableTxs();
695
+ await this.handleMinedBlocks(blocks);
696
+ await this.maybeCallPrepareForSlot();
705
697
  await this.startCollectingMissingTxs(blocks);
706
-
707
698
  const lastBlock = blocks.at(-1)!;
708
699
  await this.synchedLatestSlot.set(BigInt(lastBlock.header.getSlot()));
709
- this.log.verbose(`Synched to latest block ${lastBlock.number}`);
710
700
  }
711
701
 
712
702
  /** Request txs for unproven blocks so the prover node has more chances to get them. */
@@ -745,68 +735,31 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
745
735
  */
746
736
  private async handleFinalizedL2Blocks(blocks: L2Block[]): Promise<void> {
747
737
  if (!blocks.length) {
748
- return Promise.resolve();
738
+ return;
749
739
  }
750
- this.log.debug(`Handling finalized blocks ${blocks.length} up to ${blocks.at(-1)?.number}`);
751
-
752
- const lastBlockNum = blocks[blocks.length - 1].number;
753
- const lastBlockSlot = blocks[blocks.length - 1].header.getSlot();
754
-
755
- const txHashes = blocks.flatMap(block => block.body.txEffects.map(txEffect => txEffect.txHash));
756
- this.log.debug(`Deleting ${txHashes.length} txs from pool from finalized blocks up to ${lastBlockNum}`);
757
- await this.txPool.deleteTxs(txHashes, { permanently: true });
758
- await this.txPool.cleanupDeletedMinedTxs(lastBlockNum);
759
740
 
760
- await this.attestationPool.deleteOlderThan(lastBlockSlot);
761
-
762
- this.log.debug(`Synched to finalized block ${lastBlockNum} at slot ${lastBlockSlot}`);
741
+ // Finalization is monotonic, so we only need to call with the last block
742
+ const lastBlock = blocks.at(-1)!;
743
+ await this.txPool.handleFinalizedBlock(lastBlock.header);
744
+ await this.attestationPool.deleteOlderThan(lastBlock.header.getSlot());
763
745
  }
764
746
 
765
747
  /**
766
748
  * Updates the tx pool after a chain prune.
767
- * @param latestBlock - The block number the chain was pruned to.
749
+ * @param latestBlock - The block ID the chain was pruned to.
768
750
  */
769
- private async handlePruneL2Blocks(latestBlock: BlockNumber): Promise<void> {
770
- const txsToDelete = new Map<string, TxHash>();
771
- const minedTxs = await this.txPool.getMinedTxHashes();
772
-
773
- // Find transactions that reference pruned blocks in their historical header
774
- for (const tx of await this.txPool.getAllTxs()) {
775
- // every tx that's been generated against a block that has now been pruned is no longer valid
776
- if (tx.data.constants.anchorBlockHeader.globalVariables.blockNumber > latestBlock) {
777
- const txHash = tx.getTxHash();
778
- txsToDelete.set(txHash.toString(), txHash);
779
- }
780
- }
781
-
782
- this.log.info(`Detected chain prune. Removing ${txsToDelete.size} txs built against pruned blocks.`, {
783
- newLatestBlock: latestBlock,
784
- previousLatestBlock: await this.getSyncedLatestBlockNum(),
785
- txsToDelete: Array.from(txsToDelete.keys()),
786
- });
787
-
788
- // delete invalid txs (both pending and mined)
789
- await this.txPool.deleteTxs(Array.from(txsToDelete.values()));
790
-
791
- // everything left in the mined set was built against a block on the proven chain so its still valid
792
- // move back to pending the txs that were reorged out of the chain, unless txPoolDeleteTxsAfterReorg is set,
793
- // in which case we clean them up to avoid potential reorg loops
794
- // NOTE: we can't move _all_ txs back to pending because the tx pool could keep hold of mined txs for longer
795
- // (see this.keepProvenTxsFor)
796
- const minedTxsFromReorg: TxHash[] = [];
797
- for (const [txHash, blockNumber] of minedTxs) {
798
- // We keep the txsToDelete out of this list as they have already been deleted above
799
- if (blockNumber > latestBlock && !txsToDelete.has(txHash.toString())) {
800
- minedTxsFromReorg.push(txHash);
801
- }
802
- }
751
+ private async handlePruneL2Blocks(latestBlock: L2BlockId): Promise<void> {
752
+ await this.txPool.handlePrunedBlocks(latestBlock);
753
+ }
803
754
 
804
- if (this.config.txPoolDeleteTxsAfterReorg) {
805
- this.log.info(`Deleting ${minedTxsFromReorg.length} mined txs from reorg`);
806
- await this.txPool.deleteTxs(minedTxsFromReorg);
807
- } else {
808
- await this.txPool.markMinedAsPending(minedTxsFromReorg, latestBlock);
755
+ /** Checks if the slot has changed and calls prepareForSlot if so. */
756
+ private async maybeCallPrepareForSlot(): Promise<void> {
757
+ const { currentSlot } = this.epochCache.getCurrentAndNextSlot();
758
+ if (currentSlot <= this.lastSlotProcessed) {
759
+ return;
809
760
  }
761
+ this.lastSlotProcessed = currentSlot;
762
+ await this.txPool.prepareForSlot(currentSlot);
810
763
  }
811
764
 
812
765
  private async startServiceIfSynched() {
@@ -851,11 +804,23 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
851
804
  }
852
805
 
853
806
  /**
854
- * Marks transactions as non-evictable in the pool.
855
- * @param txHashes - Hashes of the transactions to mark as non-evictable.
807
+ * Protects existing transactions by hash for a given slot.
808
+ * Returns hashes of transactions that weren't found in the pool.
809
+ * @param txHashes - Hashes of the transactions to protect.
810
+ * @param blockHeader - The block header providing slot context.
811
+ * @returns Hashes of transactions not found in the pool.
812
+ */
813
+ public protectTxs(txHashes: TxHash[], blockHeader: BlockHeader): Promise<TxHash[]> {
814
+ return this.txPool.protectTxs(txHashes, blockHeader);
815
+ }
816
+
817
+ /**
818
+ * Prepares the pool for a new slot.
819
+ * Unprotects transactions from earlier slots and validates them.
820
+ * @param slotNumber - The slot number to prepare for
856
821
  */
857
- public markTxsAsNonEvictable(txHashes: TxHash[]): Promise<void> {
858
- return this.txPool.markTxsAsNonEvictable(txHashes);
822
+ public async prepareForSlot(slotNumber: SlotNumber): Promise<void> {
823
+ await this.txPool.prepareForSlot(slotNumber);
859
824
  }
860
825
 
861
826
  public handleAuthRequestFromPeer(authRequest: AuthRequest, peerId: PeerId): Promise<StatusMessage> {
package/src/config.ts CHANGED
@@ -45,6 +45,9 @@ export interface P2PConfig
45
45
  /** The frequency in which to check for new L2 blocks. */
46
46
  blockCheckIntervalMS: number;
47
47
 
48
+ /** The frequency in which to check for new L2 slots. */
49
+ slotCheckIntervalMS: number;
50
+
48
51
  /** The number of blocks to fetch in a single batch. */
49
52
  blockRequestBatchSize: number;
50
53
 
@@ -184,6 +187,9 @@ export interface P2PConfig
184
187
 
185
188
  /** Whether to run in fisherman mode: validates all proposals and attestations but does not broadcast attestations or participate in consensus */
186
189
  fishermanMode: boolean;
190
+
191
+ /** Broadcast block proposals even when a conflicting proposal for the same slot already exists in the pool (for testing purposes only). */
192
+ broadcastEquivocatedProposals?: boolean;
187
193
  }
188
194
 
189
195
  export const DEFAULT_P2P_PORT = 40400;
@@ -204,6 +210,11 @@ export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
204
210
  description: 'The frequency in which to check for new L2 blocks.',
205
211
  ...numberConfigHelper(100),
206
212
  },
213
+ slotCheckIntervalMS: {
214
+ env: 'P2P_SLOT_CHECK_INTERVAL_MS',
215
+ description: 'The frequency in which to check for new L2 slots.',
216
+ ...numberConfigHelper(1000),
217
+ },
207
218
  debugDisableColocationPenalty: {
208
219
  env: 'DEBUG_P2P_DISABLE_COLOCATION_PENALTY',
209
220
  description: 'DEBUG: Disable colocation penalty - NEVER set to true in production',
@@ -448,6 +459,11 @@ export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
448
459
  'Whether to run in fisherman mode: validates all proposals and attestations but does not broadcast attestations or participate in consensus.',
449
460
  ...booleanConfigHelper(false),
450
461
  },
462
+ broadcastEquivocatedProposals: {
463
+ description:
464
+ 'Broadcast block proposals even when a conflicting proposal for the same slot already exists in the pool (for testing purposes only).',
465
+ ...booleanConfigHelper(false),
466
+ },
451
467
  ...sharedSequencerConfigMappings,
452
468
  ...p2pReqRespConfigMappings,
453
469
  ...batchTxRequesterConfigMappings,
package/src/index.ts CHANGED
@@ -7,5 +7,6 @@ export * from './enr/index.js';
7
7
  export * from './config.js';
8
8
  export * from './mem_pools/attestation_pool/index.js';
9
9
  export * from './mem_pools/tx_pool/index.js';
10
+ export * from './mem_pools/tx_pool_v2/index.js';
10
11
  export * from './msg_validators/index.js';
11
12
  export * from './services/index.js';