@aztec/p2p 0.1.0-alpha22 → 0.1.0-alpha39

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.
@@ -1,4 +1,5 @@
1
1
  import { L2BlockSource } from '@aztec/types';
2
+
2
3
  import { LibP2PService, P2PClient, P2PConfig, TxPool } from '../index.js';
3
4
  import { DummyP2PService } from '../service/dummy_service.js';
4
5
 
@@ -1,3 +1,4 @@
1
+ import { EthAddress } from '@aztec/circuits.js';
1
2
  import { L2Block, L2BlockSource } from '@aztec/types';
2
3
 
3
4
  /**
@@ -13,6 +14,14 @@ export class MockBlockSource implements L2BlockSource {
13
14
  }
14
15
  }
15
16
 
17
+ /**
18
+ * Method to fetch the rollup contract address at the base-layer.
19
+ * @returns The rollup address.
20
+ */
21
+ getRollupAddress(): Promise<EthAddress> {
22
+ return Promise.resolve(EthAddress.random());
23
+ }
24
+
16
25
  /**
17
26
  * Gets the number of the latest L2 block processed by the block source implementation.
18
27
  * @returns In this mock instance, returns the number of L2 blocks that we've mocked.
@@ -22,13 +31,22 @@ export class MockBlockSource implements L2BlockSource {
22
31
  }
23
32
 
24
33
  /**
25
- * Gets the `take` amount of L2 blocks starting from `from`.
34
+ * Gets an l2 block.
35
+ * @param number - The block number to return (inclusive).
36
+ * @returns The requested L2 block.
37
+ */
38
+ public getL2Block(number: number) {
39
+ return Promise.resolve(this.l2Blocks[number]);
40
+ }
41
+
42
+ /**
43
+ * Gets up to `limit` amount of L2 blocks starting from `from`.
26
44
  * @param from - Number of the first block to return (inclusive).
27
- * @param take - The number of blocks to return.
45
+ * @param limit - The maximum number of blocks to return.
28
46
  * @returns The requested mocked L2 blocks.
29
47
  */
30
- public getL2Blocks(from: number, take: number) {
31
- return Promise.resolve(this.l2Blocks.slice(from, from + take));
48
+ public getL2Blocks(from: number, limit: number) {
49
+ return Promise.resolve(this.l2Blocks.slice(from, from + limit));
32
50
  }
33
51
 
34
52
  /**
@@ -1,10 +1,11 @@
1
- import { expect, jest } from '@jest/globals';
2
1
  import { L2BlockSource, mockTx } from '@aztec/types';
3
2
 
4
- import { P2PClient } from './p2p_client.js';
3
+ import { expect, jest } from '@jest/globals';
4
+
5
+ import { P2PService } from '../index.js';
5
6
  import { TxPool } from '../tx_pool/index.js';
6
7
  import { MockBlockSource } from './mocks.js';
7
- import { P2PService } from '../index.js';
8
+ import { P2PClient } from './p2p_client.js';
8
9
 
9
10
  /**
10
11
  * Mockify helper for testing purposes.
@@ -1,10 +1,9 @@
1
- import { L2Block, L2BlockContext, L2BlockDownloader, L2BlockSource } from '@aztec/types';
2
- import { Tx, TxHash } from '@aztec/types';
1
+ import { createDebugLogger } from '@aztec/foundation/log';
2
+ import { L2Block, L2BlockContext, L2BlockDownloader, L2BlockSource, Tx, TxHash } from '@aztec/types';
3
3
 
4
- import { TxPool } from '../tx_pool/index.js';
5
4
  import { getP2PConfigEnvVars } from '../config.js';
6
- import { createDebugLogger } from '@aztec/foundation/log';
7
5
  import { P2PService } from '../service/service.js';
6
+ import { TxPool } from '../tx_pool/index.js';
8
7
 
9
8
  /**
10
9
  * Enum defining the possible states of the p2p client.
@@ -36,12 +35,14 @@ export interface P2PSyncState {
36
35
  export interface P2P {
37
36
  /**
38
37
  * Verifies the 'tx' and, if valid, adds it to local tx pool and forwards it to other peers.
38
+ * @param tx - The transaction.
39
39
  **/
40
40
  sendTx(tx: Tx): Promise<void>;
41
41
 
42
42
  /**
43
43
  * Deletes 'txs' from the pool, given hashes.
44
44
  * NOT used if we use sendTx as reconcileTxPool will handle this.
45
+ * @param txHashes - Hashes to check.
45
46
  **/
46
47
  deleteTxs(txHashes: TxHash[]): Promise<void>;
47
48
 
@@ -53,6 +54,7 @@ export interface P2P {
53
54
 
54
55
  /**
55
56
  * Returns a transaction in the transaction pool by its hash.
57
+ * @param txHash - Hash of tx to return.
56
58
  * @returns A single tx or undefined.
57
59
  */
58
60
  getTxByhash(txHash: TxHash): Promise<Tx | undefined>;
@@ -75,7 +77,7 @@ export interface P2P {
75
77
  */
76
78
  isReady(): Promise<boolean>;
77
79
 
78
- /*
80
+ /**
79
81
  * Returns the current status of the p2p client.
80
82
  */
81
83
  getStatus(): Promise<P2PSyncState>;
@@ -123,7 +125,7 @@ export class P2PClient implements P2P {
123
125
  private p2pService: P2PService,
124
126
  private log = createDebugLogger('aztec:p2p'),
125
127
  ) {
126
- const { checkInterval, l2QueueSize } = getP2PConfigEnvVars();
128
+ const { p2pBlockCheckIntervalMS: checkInterval, l2QueueSize } = getP2PConfigEnvVars();
127
129
  this.blockDownloader = new L2BlockDownloader(l2BlockSource, l2QueueSize, checkInterval);
128
130
  }
129
131
 
package/src/config.ts CHANGED
@@ -10,7 +10,7 @@ export interface P2PConfig {
10
10
  /**
11
11
  * The frequency in which to check.
12
12
  */
13
- checkInterval: number;
13
+ p2pBlockCheckIntervalMS: number;
14
14
 
15
15
  /**
16
16
  * Size of queue of L2 blocks to store.
@@ -80,7 +80,7 @@ export interface P2PConfig {
80
80
  export function getP2PConfigEnvVars(): P2PConfig {
81
81
  const {
82
82
  P2P_ENABLED,
83
- P2P_CHECK_INTERVAL,
83
+ P2P_BLOCK_CHECK_INTERVAL_MS,
84
84
  P2P_L2_BLOCK_QUEUE_SIZE,
85
85
  P2P_TCP_LISTEN_PORT,
86
86
  P2P_TCP_LISTEN_IP,
@@ -95,7 +95,7 @@ export function getP2PConfigEnvVars(): P2PConfig {
95
95
  } = process.env;
96
96
  const envVars: P2PConfig = {
97
97
  p2pEnabled: P2P_ENABLED === 'true',
98
- checkInterval: P2P_CHECK_INTERVAL ? +P2P_CHECK_INTERVAL : 100,
98
+ p2pBlockCheckIntervalMS: P2P_BLOCK_CHECK_INTERVAL_MS ? +P2P_BLOCK_CHECK_INTERVAL_MS : 100,
99
99
  l2QueueSize: P2P_L2_BLOCK_QUEUE_SIZE ? +P2P_L2_BLOCK_QUEUE_SIZE : 1000,
100
100
  tcpListenPort: P2P_TCP_LISTEN_PORT ? +P2P_TCP_LISTEN_PORT : 0,
101
101
  tcpListenIp: P2P_TCP_LISTEN_IP ? P2P_TCP_LISTEN_IP : '0.0.0.0',
@@ -1,4 +1,5 @@
1
1
  import { Tx, TxHash } from '@aztec/types';
2
+
2
3
  import { P2PService } from './service.js';
3
4
 
4
5
  /**
@@ -1,9 +1,11 @@
1
+ import { randomBytes } from '@aztec/foundation/crypto';
2
+ import { TxHash } from '@aztec/types';
3
+
1
4
  import { expect } from '@jest/globals';
5
+ import { Ed25519PeerId, PeerId } from '@libp2p/interface-peer-id';
2
6
  import { mock } from 'jest-mock-extended';
3
- import { PeerId, Ed25519PeerId } from '@libp2p/interface-peer-id';
7
+
4
8
  import { KnownTxLookup } from './known_txs.js';
5
- import { TxHash } from '@aztec/types';
6
- import { randomBytes } from '@aztec/foundation/crypto';
7
9
 
8
10
  const createMockPeerId = (peerId: string): PeerId => {
9
11
  return mock<Ed25519PeerId>({
@@ -1,20 +1,26 @@
1
- import { Libp2p, Libp2pOptions, ServiceFactoryMap, createLibp2p } from 'libp2p';
2
- import { tcp } from '@libp2p/tcp';
1
+ import { SerialQueue } from '@aztec/foundation/fifo';
2
+ import { createDebugLogger } from '@aztec/foundation/log';
3
+ import { Tx, TxHash } from '@aztec/types';
4
+
3
5
  import { noise } from '@chainsafe/libp2p-noise';
4
6
  import { yamux } from '@chainsafe/libp2p-yamux';
5
- import { mplex } from '@libp2p/mplex';
6
7
  import { bootstrap } from '@libp2p/bootstrap';
7
- import { DualKadDHT, kadDHT } from '@libp2p/kad-dht';
8
- import { createEd25519PeerId, createFromProtobuf, exportToProtobuf } from '@libp2p/peer-id-factory';
9
8
  import type { ServiceMap } from '@libp2p/interface-libp2p';
10
9
  import { PeerId } from '@libp2p/interface-peer-id';
11
10
  import { IncomingStreamData } from '@libp2p/interface-registrar';
12
- import { P2PService } from './service.js';
13
- import { createDebugLogger } from '@aztec/foundation/log';
14
- import { SerialQueue } from '@aztec/foundation/fifo';
15
- import { P2PConfig } from '../config.js';
16
- import { Tx, TxHash } from '@aztec/types';
11
+ import { DualKadDHT, kadDHT } from '@libp2p/kad-dht';
12
+ import { mplex } from '@libp2p/mplex';
13
+ import { createEd25519PeerId, createFromProtobuf, exportToProtobuf } from '@libp2p/peer-id-factory';
14
+ import { tcp } from '@libp2p/tcp';
17
15
  import { pipe } from 'it-pipe';
16
+ import { Libp2p, Libp2pOptions, ServiceFactoryMap, createLibp2p } from 'libp2p';
17
+ import { autoNATService } from 'libp2p/autonat';
18
+ import { identifyService } from 'libp2p/identify';
19
+
20
+ import { P2PConfig } from '../config.js';
21
+ import { TxPool } from '../index.js';
22
+ import { KnownTxLookup } from './known_txs.js';
23
+ import { P2PService } from './service.js';
18
24
  import {
19
25
  Messages,
20
26
  createGetTransactionsRequestMessage,
@@ -25,10 +31,6 @@ import {
25
31
  decodeTransactionsMessage,
26
32
  getEncodedMessage,
27
33
  } from './tx_messages.js';
28
- import { KnownTxLookup } from './known_txs.js';
29
- import { TxPool } from '../index.js';
30
- import { autoNATService } from 'libp2p/autonat';
31
- import { identifyService } from 'libp2p/identify';
32
34
 
33
35
  const INITIAL_PEER_REFRESH_INTERVAL = 20000;
34
36
 
@@ -1,9 +1,11 @@
1
1
  import { MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, Proof } from '@aztec/circuits.js';
2
2
  import { makeKernelPublicInputs, makePublicCallRequest } from '@aztec/circuits.js/factories';
3
3
  import { EncodedContractFunction, Tx, TxHash, TxL2Logs } from '@aztec/types';
4
+
4
5
  import { expect } from '@jest/globals';
5
6
  import { randomBytes } from 'crypto';
6
7
  import times from 'lodash.times';
8
+
7
9
  import {
8
10
  Messages,
9
11
  createGetTransactionsRequestMessage,
@@ -1,4 +1,5 @@
1
1
  import { mockTx } from '@aztec/types';
2
+
2
3
  import { InMemoryTxPool } from './index.js';
3
4
 
4
5
  describe('In-Memory TX pool', () => {