@aztec/p2p 0.85.0 → 0.86.0

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 (90) hide show
  1. package/dest/bootstrap/bootstrap.d.ts.map +1 -1
  2. package/dest/bootstrap/bootstrap.js +6 -3
  3. package/dest/client/p2p_client.d.ts +41 -3
  4. package/dest/client/p2p_client.d.ts.map +1 -1
  5. package/dest/client/p2p_client.js +58 -18
  6. package/dest/config.d.ts +13 -2
  7. package/dest/config.d.ts.map +1 -1
  8. package/dest/config.js +15 -3
  9. package/dest/enr/generate-enr.d.ts +1 -1
  10. package/dest/enr/generate-enr.d.ts.map +1 -1
  11. package/dest/enr/generate-enr.js +2 -2
  12. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  13. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +11 -11
  14. package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +2 -2
  15. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +1 -1
  16. package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +4 -4
  17. package/dest/mem_pools/attestation_pool/mocks.d.ts +1 -1
  18. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  19. package/dest/mem_pools/attestation_pool/mocks.js +2 -2
  20. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts +3 -0
  21. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts.map +1 -1
  22. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.js +18 -0
  23. package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts +3 -0
  24. package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts.map +1 -1
  25. package/dest/mem_pools/tx_pool/memory_tx_pool.js +9 -0
  26. package/dest/mem_pools/tx_pool/tx_pool.d.ts +17 -0
  27. package/dest/mem_pools/tx_pool/tx_pool.d.ts.map +1 -1
  28. package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts.map +1 -1
  29. package/dest/mem_pools/tx_pool/tx_pool_test_suite.js +57 -0
  30. package/dest/msg_validators/attestation_validator/attestation_validator.js +1 -1
  31. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts +1 -0
  32. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts.map +1 -1
  33. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.js +6 -1
  34. package/dest/services/discv5/discV5_service.d.ts +2 -1
  35. package/dest/services/discv5/discV5_service.d.ts.map +1 -1
  36. package/dest/services/discv5/discV5_service.js +22 -7
  37. package/dest/services/dummy_service.d.ts +2 -2
  38. package/dest/services/dummy_service.d.ts.map +1 -1
  39. package/dest/services/dummy_service.js +2 -2
  40. package/dest/services/libp2p/libp2p_service.d.ts +3 -1
  41. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  42. package/dest/services/libp2p/libp2p_service.js +28 -12
  43. package/dest/services/peer-manager/peer_manager.d.ts +21 -2
  44. package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
  45. package/dest/services/peer-manager/peer_manager.js +63 -18
  46. package/dest/services/reqresp/connection-sampler/batch_connection_sampler.js +2 -2
  47. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts +17 -1
  48. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts.map +1 -1
  49. package/dest/services/reqresp/connection-sampler/connection_sampler.js +84 -36
  50. package/dest/services/reqresp/reqresp.d.ts +2 -2
  51. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  52. package/dest/services/reqresp/reqresp.js +7 -2
  53. package/dest/services/reqresp/status.d.ts +2 -1
  54. package/dest/services/reqresp/status.d.ts.map +1 -1
  55. package/dest/services/reqresp/status.js +3 -0
  56. package/dest/services/service.d.ts +4 -4
  57. package/dest/services/service.d.ts.map +1 -1
  58. package/dest/test-helpers/make-test-p2p-clients.d.ts +6 -1
  59. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  60. package/dest/test-helpers/make-test-p2p-clients.js +19 -2
  61. package/dest/testbench/p2p_client_testbench_worker.js +4 -1
  62. package/dest/util.d.ts.map +1 -1
  63. package/dest/util.js +5 -1
  64. package/package.json +12 -14
  65. package/src/bootstrap/bootstrap.ts +8 -4
  66. package/src/client/p2p_client.ts +212 -131
  67. package/src/config.ts +34 -4
  68. package/src/enr/generate-enr.ts +2 -2
  69. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +11 -15
  70. package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +2 -2
  71. package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +4 -4
  72. package/src/mem_pools/attestation_pool/mocks.ts +3 -3
  73. package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +21 -0
  74. package/src/mem_pools/tx_pool/memory_tx_pool.ts +11 -0
  75. package/src/mem_pools/tx_pool/tx_pool.ts +20 -0
  76. package/src/mem_pools/tx_pool/tx_pool_test_suite.ts +43 -0
  77. package/src/msg_validators/attestation_validator/attestation_validator.ts +1 -1
  78. package/src/msg_validators/block_proposal_validator/block_proposal_validator.ts +10 -1
  79. package/src/services/discv5/discV5_service.ts +32 -6
  80. package/src/services/dummy_service.ts +2 -2
  81. package/src/services/libp2p/libp2p_service.ts +37 -12
  82. package/src/services/peer-manager/peer_manager.ts +79 -22
  83. package/src/services/reqresp/connection-sampler/batch_connection_sampler.ts +2 -2
  84. package/src/services/reqresp/connection-sampler/connection_sampler.ts +82 -41
  85. package/src/services/reqresp/reqresp.ts +12 -6
  86. package/src/services/reqresp/status.ts +3 -0
  87. package/src/services/service.ts +4 -4
  88. package/src/test-helpers/make-test-p2p-clients.ts +20 -2
  89. package/src/testbench/p2p_client_testbench_worker.ts +3 -0
  90. package/src/util.ts +6 -1
@@ -1,5 +1,6 @@
1
1
  import { MockL2BlockSource } from '@aztec/archiver/test';
2
2
  import { createLogger } from '@aztec/foundation/log';
3
+ import { sleep } from '@aztec/foundation/sleep';
3
4
  import { openTmpStore } from '@aztec/kv-store/lmdb-v2';
4
5
  import { P2PClientType } from '@aztec/stdlib/p2p';
5
6
  import { createP2PClient } from '../client/index.js';
@@ -21,6 +22,7 @@ import { AlwaysFalseCircuitVerifier, AlwaysTrueCircuitVerifier } from './reqresp
21
22
  p2pEnabled: true,
22
23
  peerIdPrivateKey,
23
24
  p2pIp: `127.0.0.1`,
25
+ listenAddress: `127.0.0.1`,
24
26
  p2pPort: port,
25
27
  bootstrapNodes: peers,
26
28
  peerCheckIntervalMS: 1000,
@@ -50,7 +52,15 @@ import { AlwaysFalseCircuitVerifier, AlwaysTrueCircuitVerifier } from './reqresp
50
52
  */ export async function makeTestP2PClients(numberOfPeers, testConfig) {
51
53
  const clients = [];
52
54
  const peerIdPrivateKeys = generatePeerIdPrivateKeys(numberOfPeers);
53
- const ports = await getPorts(numberOfPeers);
55
+ let ports = [];
56
+ while(true){
57
+ try {
58
+ ports = await getPorts(numberOfPeers);
59
+ break;
60
+ } catch (err) {
61
+ await sleep(1000);
62
+ }
63
+ }
54
64
  const peerEnrs = await makeEnrs(peerIdPrivateKeys, ports, testConfig.p2pBaseConfig);
55
65
  for(let i = 0; i < numberOfPeers; i++){
56
66
  const client = await makeTestP2PClient(peerIdPrivateKeys[i], ports[i], peerEnrs, {
@@ -60,5 +70,12 @@ import { AlwaysFalseCircuitVerifier, AlwaysTrueCircuitVerifier } from './reqresp
60
70
  clients.push(client);
61
71
  }
62
72
  await Promise.all(clients.map((client)=>client.isReady()));
63
- return clients;
73
+ return clients.map((client, index)=>{
74
+ return {
75
+ client,
76
+ peerPrivateKey: peerIdPrivateKeys[index],
77
+ port: ports[index],
78
+ enr: peerEnrs[index]
79
+ };
80
+ });
64
81
  }
@@ -28,7 +28,10 @@ function mockTxPool() {
28
28
  getAllTxHashes: ()=>Promise.resolve([]),
29
29
  getPendingTxHashes: ()=>Promise.resolve([]),
30
30
  getMinedTxHashes: ()=>Promise.resolve([]),
31
- getTxStatus: ()=>Promise.resolve(TxStatus.PENDING)
31
+ getTxStatus: ()=>Promise.resolve(TxStatus.PENDING),
32
+ getTxsByHash: ()=>Promise.resolve([]),
33
+ hasTxs: ()=>Promise.resolve([]),
34
+ setMaxTxPoolSize: ()=>Promise.resolve()
32
35
  };
33
36
  }
34
37
  function mockAttestationPool() {
@@ -1 +1 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,KAAK,EAAE,iBAAiB,EAAuB,MAAM,iBAAiB,CAAC;AAC9E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAE9D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAE7D,OAAO,KAAK,EAAE,MAAM,EAAc,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAIpE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGrC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAI7C,MAAM,WAAW,YAAa,SAAQ,MAAM;IAC1C,QAAQ,EAAE;QACR,MAAM,EAAE,SAAS,CAAC;QAClB,UAAU,EAAE;YACV,iBAAiB,EAAE,iBAAiB,CAAC;SACtC,CAAC;KACH,CAAC;CACH;AAED;;;;;;;6CAO6C;AAC7C,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAOjG;AAED;;GAEG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC,CAInD;AAED,wBAAsB,yBAAyB,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAW9F;AAcD,wBAAsB,2BAA2B,CAC/C,OAAO,EAAE,SAAS,GAAG,eAAe,GACnC,OAAO,CAAC,SAAS,GAAG,eAAe,CAAC,CActC;AAED;;;;;;;;GAQG;AACH,wBAAsB,mBAAmB,CACvC,MAAM,EAAE;IAAE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAAC,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,CAAA;CAAE,EAC5F,KAAK,EAAE,iBAAiB,EACxB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,CA2DjB;AAED;;;;GAIG;AACH,wBAAsB,gCAAgC,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAS1F"}
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,KAAK,EAAE,iBAAiB,EAAuB,MAAM,iBAAiB,CAAC;AAC9E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAE9D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAE7D,OAAO,KAAK,EAAE,MAAM,EAAc,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAIpE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGrC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAI7C,MAAM,WAAW,YAAa,SAAQ,MAAM;IAC1C,QAAQ,EAAE;QACR,MAAM,EAAE,SAAS,CAAC;QAClB,UAAU,EAAE;YACV,iBAAiB,EAAE,iBAAiB,CAAC;SACtC,CAAC;KACH,CAAC;CACH;AAED;;;;;;;6CAO6C;AAC7C,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAOjG;AAED;;GAEG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC,CAInD;AAED,wBAAsB,yBAAyB,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAW9F;AAcD,wBAAsB,2BAA2B,CAC/C,OAAO,EAAE,SAAS,GAAG,eAAe,GACnC,OAAO,CAAC,SAAS,GAAG,eAAe,CAAC,CAmBtC;AAED;;;;;;;;GAQG;AACH,wBAAsB,mBAAmB,CACvC,MAAM,EAAE;IAAE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAAC,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,CAAA;CAAE,EAC5F,KAAK,EAAE,iBAAiB,EACxB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,CA2DjB;AAED;;;;GAIG;AACH,wBAAsB,gCAAgC,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAS1F"}
package/dest/util.js CHANGED
@@ -52,7 +52,11 @@ export async function configureP2PClientAddresses(_config) {
52
52
  const config = {
53
53
  ..._config
54
54
  };
55
- const { p2pIp, queryForIp } = config;
55
+ const { p2pIp, queryForIp, p2pBroadcastPort, p2pPort } = config;
56
+ // If no broadcast port is provided, use the given p2p port as the broadcast port
57
+ if (!p2pBroadcastPort) {
58
+ config.p2pBroadcastPort = p2pPort;
59
+ }
56
60
  // check if no announce IP was provided
57
61
  if (!p2pIp) {
58
62
  if (queryForIp) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/p2p",
3
- "version": "0.85.0",
3
+ "version": "0.86.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/index.js",
@@ -22,8 +22,6 @@
22
22
  "build": "yarn clean && tsc -b",
23
23
  "build:dev": "tsc -b --watch",
24
24
  "clean": "rm -rf ./dest .tsbuildinfo",
25
- "formatting": "run -T prettier --check ./src && run -T eslint ./src",
26
- "formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src",
27
25
  "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}",
28
26
  "start": "node ./dest",
29
27
  "start:dev": "tsc-watch -p tsconfig.json --onSuccess 'yarn start'"
@@ -65,16 +63,16 @@
65
63
  ]
66
64
  },
67
65
  "dependencies": {
68
- "@aztec/constants": "0.85.0",
69
- "@aztec/epoch-cache": "0.85.0",
70
- "@aztec/foundation": "0.85.0",
71
- "@aztec/kv-store": "0.85.0",
72
- "@aztec/noir-contracts.js": "0.85.0",
73
- "@aztec/noir-protocol-circuits-types": "0.85.0",
74
- "@aztec/protocol-contracts": "0.85.0",
75
- "@aztec/simulator": "0.85.0",
76
- "@aztec/stdlib": "0.85.0",
77
- "@aztec/telemetry-client": "0.85.0",
66
+ "@aztec/constants": "0.86.0",
67
+ "@aztec/epoch-cache": "0.86.0",
68
+ "@aztec/foundation": "0.86.0",
69
+ "@aztec/kv-store": "0.86.0",
70
+ "@aztec/noir-contracts.js": "0.86.0",
71
+ "@aztec/noir-protocol-circuits-types": "0.86.0",
72
+ "@aztec/protocol-contracts": "0.86.0",
73
+ "@aztec/simulator": "0.86.0",
74
+ "@aztec/stdlib": "0.86.0",
75
+ "@aztec/telemetry-client": "0.86.0",
78
76
  "@chainsafe/discv5": "9.0.0",
79
77
  "@chainsafe/enr": "3.0.0",
80
78
  "@chainsafe/libp2p-gossipsub": "13.0.0",
@@ -103,7 +101,7 @@
103
101
  "xxhash-wasm": "^1.1.0"
104
102
  },
105
103
  "devDependencies": {
106
- "@aztec/archiver": "0.85.0",
104
+ "@aztec/archiver": "0.86.0",
107
105
  "@jest/globals": "^29.5.0",
108
106
  "@types/jest": "^29.5.0",
109
107
  "@types/node": "^18.14.6",
@@ -31,19 +31,23 @@ export class BootstrapNode implements P2PBootstrapApi {
31
31
  * @returns An empty promise.
32
32
  */
33
33
  public async start(config: BootnodeConfig) {
34
- const { p2pIp, p2pPort, listenAddress } = config;
35
- const listenAddrUdp = multiaddr(convertToMultiaddr(listenAddress, p2pPort, 'udp'));
36
-
34
+ const { p2pIp, p2pPort, listenAddress, p2pBroadcastPort } = config;
37
35
  if (!p2pIp) {
38
36
  throw new Error('You need to provide a P2P IP address.');
39
37
  }
40
38
 
39
+ if (!p2pBroadcastPort) {
40
+ config.p2pBroadcastPort = p2pPort;
41
+ }
42
+
43
+ const listenAddrUdp = multiaddr(convertToMultiaddr(listenAddress, config.p2pBroadcastPort!, 'udp'));
44
+
41
45
  const peerIdPrivateKey = await getPeerIdPrivateKey(config, this.store, this.logger);
42
46
 
43
47
  const { enr: ourEnr, peerId } = await createBootnodeENRandPeerId(
44
48
  peerIdPrivateKey,
45
49
  p2pIp,
46
- p2pPort,
50
+ config.p2pBroadcastPort!,
47
51
  config.l1ChainId,
48
52
  );
49
53
  this.peerId = peerId;
@@ -10,7 +10,7 @@ import type {
10
10
  PublishedL2Block,
11
11
  } from '@aztec/stdlib/block';
12
12
  import type { ContractDataSource } from '@aztec/stdlib/contract';
13
- import type { P2PApi, PeerInfo, ProverCoordination } from '@aztec/stdlib/interfaces/server';
13
+ import type { P2PApi, PeerInfo } from '@aztec/stdlib/interfaces/server';
14
14
  import { BlockAttestation, type BlockProposal, ConsensusPayload, type P2PClientType } from '@aztec/stdlib/p2p';
15
15
  import type { Tx, TxHash } from '@aztec/stdlib/tx';
16
16
  import {
@@ -58,116 +58,144 @@ export interface P2PSyncState {
58
58
  /**
59
59
  * Interface of a P2P client.
60
60
  **/
61
- export type P2P<T extends P2PClientType = P2PClientType.Full> = ProverCoordination &
62
- P2PApi<T> & {
63
- /**
64
- * Broadcasts a block proposal to other peers.
65
- *
66
- * @param proposal - the block proposal
67
- */
68
- broadcastProposal(proposal: BlockProposal): void;
69
-
70
- /**
71
- * Registers a callback from the validator client that determines how to behave when
72
- * foreign block proposals are received
73
- *
74
- * @param handler - A function taking a received block proposal and producing an attestation
75
- */
76
- // REVIEW: https://github.com/AztecProtocol/aztec-packages/issues/7963
77
- // ^ This pattern is not my favorite (md)
78
- registerBlockProposalHandler(handler: (block: BlockProposal) => Promise<BlockAttestation | undefined>): void;
79
-
80
- /**
81
- * Request a list of transactions from another peer by their tx hashes.
82
- * @param txHashes - Hashes of the txs to query.
83
- * @returns A list of transactions or undefined if the transactions are not found.
84
- */
85
- requestTxs(txHashes: TxHash[]): Promise<(Tx | undefined)[]>;
86
-
87
- /**
88
- * Request a transaction from another peer by its tx hash.
89
- * @param txHash - Hash of the tx to query.
90
- */
91
- requestTxByHash(txHash: TxHash): Promise<Tx | undefined>;
92
-
93
- /**
94
- * Verifies the 'tx' and, if valid, adds it to local tx pool and forwards it to other peers.
95
- * @param tx - The transaction.
96
- **/
97
- sendTx(tx: Tx): Promise<void>;
98
-
99
- /**
100
- * Deletes 'txs' from the pool, given hashes.
101
- * NOT used if we use sendTx as reconcileTxPool will handle this.
102
- * @param txHashes - Hashes to check.
103
- **/
104
- deleteTxs(txHashes: TxHash[]): Promise<void>;
105
-
106
- /**
107
- * Returns a transaction in the transaction pool by its hash.
108
- * @param txHash - Hash of tx to return.
109
- * @returns A single tx or undefined.
110
- */
111
- getTxByHashFromPool(txHash: TxHash): Promise<Tx | undefined>;
112
-
113
- /**
114
- * Returns a transaction in the transaction pool by its hash, requesting it from the network if it is not found.
115
- * @param txHash - Hash of tx to return.
116
- * @returns A single tx or undefined.
117
- */
118
- getTxByHash(txHash: TxHash): Promise<Tx | undefined>;
119
-
120
- /**
121
- * Returns an archived transaction from the transaction pool by its hash.
122
- * @param txHash - Hash of tx to return.
123
- * @returns A single tx or undefined.
124
- */
125
- getArchivedTxByHash(txHash: TxHash): Promise<Tx | undefined>;
126
-
127
- /**
128
- * Returns whether the given tx hash is flagged as pending or mined.
129
- * @param txHash - Hash of the tx to query.
130
- * @returns Pending or mined depending on its status, or undefined if not found.
131
- */
132
- getTxStatus(txHash: TxHash): Promise<'pending' | 'mined' | undefined>;
133
-
134
- /** Returns an iterator over pending txs on the mempool. */
135
- iteratePendingTxs(): AsyncIterableIterator<Tx>;
136
-
137
- /** Returns the number of pending txs in the mempool. */
138
- getPendingTxCount(): Promise<number>;
139
-
140
- /**
141
- * Starts the p2p client.
142
- * @returns A promise signalling the completion of the block sync.
143
- */
144
- start(): Promise<void>;
145
-
146
- /**
147
- * Stops the p2p client.
148
- * @returns A promise signalling the completion of the stop process.
149
- */
150
- stop(): Promise<void>;
151
-
152
- /**
153
- * Indicates if the p2p client is ready for transaction submission.
154
- * @returns A boolean flag indicating readiness.
155
- */
156
- isReady(): boolean;
157
-
158
- /**
159
- * Returns the current status of the p2p client.
160
- */
161
- getStatus(): Promise<P2PSyncState>;
162
-
163
- /**
164
- * Returns the ENR of this node, if any.
165
- */
166
- getEnr(): ENR | undefined;
167
-
168
- /** Identifies a p2p client. */
169
- isP2PClient(): true;
170
- };
61
+ export type P2P<T extends P2PClientType = P2PClientType.Full> = P2PApi<T> & {
62
+ /**
63
+ * Broadcasts a block proposal to other peers.
64
+ *
65
+ * @param proposal - the block proposal
66
+ */
67
+ broadcastProposal(proposal: BlockProposal): void;
68
+
69
+ /**
70
+ * Registers a callback from the validator client that determines how to behave when
71
+ * foreign block proposals are received
72
+ *
73
+ * @param handler - A function taking a received block proposal and producing an attestation
74
+ */
75
+ // REVIEW: https://github.com/AztecProtocol/aztec-packages/issues/7963
76
+ // ^ This pattern is not my favorite (md)
77
+ registerBlockProposalHandler(handler: (block: BlockProposal) => Promise<BlockAttestation | undefined>): void;
78
+
79
+ /**
80
+ * Request a list of transactions from another peer by their tx hashes.
81
+ * @param txHashes - Hashes of the txs to query.
82
+ * @returns A list of transactions or undefined if the transactions are not found.
83
+ */
84
+ requestTxs(txHashes: TxHash[]): Promise<(Tx | undefined)[]>;
85
+
86
+ /**
87
+ * Request a transaction from another peer by its tx hash.
88
+ * @param txHash - Hash of the tx to query.
89
+ */
90
+ requestTxByHash(txHash: TxHash): Promise<Tx | undefined>;
91
+
92
+ /**
93
+ * Verifies the 'tx' and, if valid, adds it to local tx pool and forwards it to other peers.
94
+ * @param tx - The transaction.
95
+ **/
96
+ sendTx(tx: Tx): Promise<void>;
97
+
98
+ /**
99
+ * Adds transactions to the pool. Does not send to peers or validate the tx.
100
+ * @param txs - The transactions.
101
+ **/
102
+ addTxs(txs: Tx[]): Promise<void>;
103
+
104
+ /**
105
+ * Deletes 'txs' from the pool, given hashes.
106
+ * NOT used if we use sendTx as reconcileTxPool will handle this.
107
+ * @param txHashes - Hashes to check.
108
+ **/
109
+ deleteTxs(txHashes: TxHash[]): Promise<void>;
110
+
111
+ /**
112
+ * Returns a transaction in the transaction pool by its hash.
113
+ * @param txHash - Hash of tx to return.
114
+ * @returns A single tx or undefined.
115
+ */
116
+ getTxByHashFromPool(txHash: TxHash): Promise<Tx | undefined>;
117
+
118
+ /**
119
+ * Returns transactions in the transaction pool by hash.
120
+ * @param txHashes - Hashes of txs to return.
121
+ * @returns An array of txs or undefined.
122
+ */
123
+ getTxsByHashFromPool(txHashes: TxHash[]): Promise<(Tx | undefined)[]>;
124
+
125
+ /**
126
+ * Checks if transactions exist in the pool
127
+ * @param txHashes - The hashes of the transactions to check for
128
+ * @returns True or False for each hash
129
+ */
130
+ hasTxsInPool(txHashes: TxHash[]): Promise<boolean[]>;
131
+
132
+ /**
133
+ * Returns a transaction in the transaction pool by its hash, requesting it from the network if it is not found.
134
+ * @param txHash - Hash of tx to return.
135
+ * @returns A single tx or undefined.
136
+ */
137
+ getTxByHash(txHash: TxHash): Promise<Tx | undefined>;
138
+
139
+ /**
140
+ * Returns transactions in the transaction pool by hash, requesting from the network if not found.
141
+ * @param txHashes - Hashes of tx to return.
142
+ * @returns An array of tx or undefined.
143
+ */
144
+ getTxsByHash(txHashes: TxHash[]): Promise<(Tx | undefined)[]>;
145
+
146
+ /**
147
+ * Returns an archived transaction from the transaction pool by its hash.
148
+ * @param txHash - Hash of tx to return.
149
+ * @returns A single tx or undefined.
150
+ */
151
+ getArchivedTxByHash(txHash: TxHash): Promise<Tx | undefined>;
152
+
153
+ /**
154
+ * Returns whether the given tx hash is flagged as pending or mined.
155
+ * @param txHash - Hash of the tx to query.
156
+ * @returns Pending or mined depending on its status, or undefined if not found.
157
+ */
158
+ getTxStatus(txHash: TxHash): Promise<'pending' | 'mined' | undefined>;
159
+
160
+ /** Returns an iterator over pending txs on the mempool. */
161
+ iteratePendingTxs(): AsyncIterableIterator<Tx>;
162
+
163
+ /** Returns the number of pending txs in the mempool. */
164
+ getPendingTxCount(): Promise<number>;
165
+
166
+ /**
167
+ * Starts the p2p client.
168
+ * @returns A promise signalling the completion of the block sync.
169
+ */
170
+ start(): Promise<void>;
171
+
172
+ /**
173
+ * Stops the p2p client.
174
+ * @returns A promise signalling the completion of the stop process.
175
+ */
176
+ stop(): Promise<void>;
177
+
178
+ /**
179
+ * Indicates if the p2p client is ready for transaction submission.
180
+ * @returns A boolean flag indicating readiness.
181
+ */
182
+ isReady(): boolean;
183
+
184
+ /**
185
+ * Returns the current status of the p2p client.
186
+ */
187
+ getStatus(): Promise<P2PSyncState>;
188
+
189
+ /**
190
+ * Returns the ENR of this node, if any.
191
+ */
192
+ getEnr(): ENR | undefined;
193
+
194
+ /** Identifies a p2p client. */
195
+ isP2PClient(): true;
196
+
197
+ updateP2PConfig(config: Partial<P2PConfig>): Promise<void>;
198
+ };
171
199
 
172
200
  /**
173
201
  * The P2P client implementation.
@@ -200,6 +228,8 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
200
228
 
201
229
  private blockStream;
202
230
 
231
+ private config: P2PConfig;
232
+
203
233
  /**
204
234
  * In-memory P2P client constructor.
205
235
  * @param store - The client's instance of the KV store.
@@ -221,10 +251,13 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
221
251
  ) {
222
252
  super(telemetry, 'P2PClient');
223
253
 
224
- const { keepProvenTxsInPoolFor, blockCheckIntervalMS, blockRequestBatchSize, keepAttestationsInPoolFor } = {
254
+ this.config = {
225
255
  ...getP2PDefaultConfig(),
226
256
  ...config,
227
257
  };
258
+
259
+ const { keepProvenTxsInPoolFor, blockCheckIntervalMS, blockRequestBatchSize, keepAttestationsInPoolFor } =
260
+ this.config;
228
261
  this.keepProvenTxsFor = keepProvenTxsInPoolFor;
229
262
  this.keepAttestationsInPoolFor = keepAttestationsInPoolFor;
230
263
 
@@ -256,6 +289,13 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
256
289
  return this.synchedBlockHashes.getAsync(number);
257
290
  }
258
291
 
292
+ public async updateP2PConfig(config: Partial<P2PConfig>): Promise<void> {
293
+ if (typeof config.maxTxPoolSize === 'number' && this.config.maxTxPoolSize !== config.maxTxPoolSize) {
294
+ await this.txPool.setMaxTxPoolSize(config.maxTxPoolSize);
295
+ this.config.maxTxPoolSize = config.maxTxPoolSize;
296
+ }
297
+ }
298
+
259
299
  public async getL2Tips(): Promise<L2Tips> {
260
300
  const latestBlockNumber = await this.getSyncedLatestBlockNum();
261
301
  let latestBlockHash: string | undefined;
@@ -445,12 +485,17 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
445
485
  /**
446
486
  * Uses the batched Request Response protocol to request a set of transactions from the network.
447
487
  */
448
- public async requestTxsByHash(txHashes: TxHash[]): Promise<Tx[]> {
449
- const txs = (await this.p2pService.sendBatchRequest(ReqRespSubProtocol.TX, txHashes)) ?? [];
450
- await this.txPool.addTxs(txs);
488
+ public async requestTxsByHash(txHashes: TxHash[]): Promise<(Tx | undefined)[]> {
489
+ const txs = await this.p2pService.sendBatchRequest(ReqRespSubProtocol.TX, txHashes);
490
+
491
+ // Some transactions may return undefined, so we filter them out
492
+ const filteredTxs = txs.filter((tx): tx is Tx => !!tx);
493
+ await this.txPool.addTxs(filteredTxs);
451
494
  const txHashesStr = txHashes.map(tx => tx.toString()).join(', ');
452
495
  this.log.debug(`Received batched txs ${txHashesStr} (${txs.length} / ${txHashes.length}}) from peers`);
453
- return txs as Tx[];
496
+
497
+ // We return all transactions, even the not found ones to the caller, such they can handle missing items themselves.
498
+ return txs;
454
499
  }
455
500
 
456
501
  public getPendingTxs(): Promise<Tx[]> {
@@ -501,6 +546,19 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
501
546
  return this.txPool.getTxByHash(txHash);
502
547
  }
503
548
 
549
+ /**
550
+ * Returns transactions in the transaction pool by hash.
551
+ * @param txHashes - Hashes of the transactions to look for.
552
+ * @returns The txs found, not necessarily on the same order as the hashes.
553
+ */
554
+ getTxsByHashFromPool(txHashes: TxHash[]): Promise<(Tx | undefined)[]> {
555
+ return this.txPool.getTxsByHash(txHashes);
556
+ }
557
+
558
+ hasTxsInPool(txHashes: TxHash[]): Promise<boolean[]> {
559
+ return this.txPool.hasTxs(txHashes);
560
+ }
561
+
504
562
  /**
505
563
  * Returns a transaction in the transaction pool by its hash.
506
564
  * If the transaction is not in the pool, it will be requested from the network.
@@ -533,7 +591,8 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
533
591
  }
534
592
 
535
593
  const missingTxs = await this.requestTxsByHash(missingTxHashes);
536
- return txs.filter((tx): tx is Tx => !!tx).concat(missingTxs);
594
+ const fetchedMissingTxs = missingTxs.filter((tx): tx is Tx => !!tx);
595
+ return txs.filter((tx): tx is Tx => !!tx).concat(fetchedMissingTxs);
537
596
  }
538
597
 
539
598
  /**
@@ -551,11 +610,19 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
551
610
  * @returns Empty promise.
552
611
  **/
553
612
  public async sendTx(tx: Tx): Promise<void> {
554
- this.#assertIsReady();
555
- await this.txPool.addTxs([tx]);
613
+ await this.addTxs([tx]);
556
614
  this.p2pService.propagate(tx);
557
615
  }
558
616
 
617
+ /**
618
+ * Adds transactions to the pool. Does not send to peers or validate the txs.
619
+ * @param txs - The transactions.
620
+ **/
621
+ public async addTxs(txs: Tx[]): Promise<void> {
622
+ this.#assertIsReady();
623
+ await this.txPool.addTxs(txs);
624
+ }
625
+
559
626
  /**
560
627
  * Returns whether the given tx hash is flagged as pending or mined.
561
628
  * @param txHash - Hash of the tx to query.
@@ -732,36 +799,50 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
732
799
  * @param latestBlock - The block number the chain was pruned to.
733
800
  */
734
801
  private async handlePruneL2Blocks(latestBlock: number): Promise<void> {
735
- const txsToDelete: TxHash[] = [];
802
+ // NOTE: temporary fix for alphanet, deleting ALL txs that were in the epoch from the pool #13723
803
+ // TODO: undo once fixed: #13770
804
+ const txsToDelete = new Set<TxHash>();
805
+ const minedTxs = await this.txPool.getMinedTxHashes();
806
+ for (const [txHash, blockNumber] of minedTxs) {
807
+ if (blockNumber > latestBlock) {
808
+ txsToDelete.add(txHash);
809
+ }
810
+ }
811
+
812
+ // Find transactions that reference pruned blocks in their historical header
736
813
  for (const tx of await this.txPool.getAllTxs()) {
737
814
  // every tx that's been generated against a block that has now been pruned is no longer valid
738
815
  if (tx.data.constants.historicalHeader.globalVariables.blockNumber.toNumber() > latestBlock) {
739
- txsToDelete.push(await tx.getTxHash());
816
+ const txHash = await tx.getTxHash();
817
+ txsToDelete.add(txHash);
740
818
  }
741
819
  }
742
820
 
743
821
  this.log.info(
744
822
  `Detected chain prune. Removing invalid txs count=${
745
- txsToDelete.length
823
+ txsToDelete.size
746
824
  } newLatestBlock=${latestBlock} previousLatestBlock=${await this.getSyncedLatestBlockNum()}`,
747
825
  );
748
826
 
749
827
  // delete invalid txs (both pending and mined)
750
- await this.txPool.deleteTxs(txsToDelete);
828
+ await this.txPool.deleteTxs(Array.from(txsToDelete));
751
829
 
752
830
  // everything left in the mined set was built against a block on the proven chain so its still valid
753
831
  // move back to pending the txs that were reorged out of the chain
754
832
  // NOTE: we can't move _all_ txs back to pending because the tx pool could keep hold of mined txs for longer
755
833
  // (see this.keepProvenTxsFor)
756
- const txsToMoveToPending: TxHash[] = [];
757
- for (const [txHash, blockNumber] of await this.txPool.getMinedTxHashes()) {
758
- if (blockNumber > latestBlock) {
759
- txsToMoveToPending.push(txHash);
760
- }
761
- }
762
834
 
763
- this.log.info(`Moving ${txsToMoveToPending.length} mined txs back to pending`);
764
- await this.txPool.markMinedAsPending(txsToMoveToPending);
835
+ // NOTE: given current fix for alphanet, the code below is redundant as all these txs will be deleted.
836
+ // TODO: bring back once fixed: #13770
837
+ // const txsToMoveToPending: TxHash[] = [];
838
+ // for (const [txHash, blockNumber] of minedTxs) {
839
+ // if (blockNumber > latestBlock) {
840
+ // txsToMoveToPending.push(txHash);
841
+ // }
842
+ // }
843
+
844
+ // this.log.info(`Moving ${txsToMoveToPending.length} mined txs back to pending`);
845
+ // await this.txPool.markMinedAsPending(txsToMoveToPending);
765
846
 
766
847
  await this.synchedLatestBlockNumber.set(latestBlock);
767
848
  // no need to update block hashes, as they will be updated as new blocks are added