@aztec/p2p 0.23.0 → 0.26.1
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.
- package/dest/client/mocks.d.ts +12 -6
- package/dest/client/mocks.d.ts.map +1 -1
- package/dest/client/mocks.js +25 -10
- package/dest/client/p2p_client.d.ts.map +1 -1
- package/dest/client/p2p_client.js +3 -2
- package/dest/service/libp2p_service.js +4 -4
- package/dest/service/tx_messages.d.ts.map +1 -1
- package/dest/service/tx_messages.js +3 -3
- package/dest/tx_pool/memory_tx_pool.d.ts.map +1 -1
- package/dest/tx_pool/memory_tx_pool.js +4 -3
- package/dest/tx_pool/tx_pool_test_suite.d.ts.map +1 -1
- package/dest/tx_pool/tx_pool_test_suite.js +6 -6
- package/package.json +5 -5
- package/src/bootstrap/bootstrap.ts +116 -0
- package/src/client/index.ts +20 -0
- package/src/client/mocks.ts +103 -0
- package/src/client/p2p_client.ts +327 -0
- package/src/config.ts +113 -0
- package/src/index.ts +5 -0
- package/src/service/dummy_service.ts +36 -0
- package/src/service/index.ts +2 -0
- package/src/service/known_txs.ts +56 -0
- package/src/service/libp2p_service.ts +397 -0
- package/src/service/service.ts +30 -0
- package/src/service/tx_messages.ts +216 -0
- package/src/tx_pool/aztec_kv_tx_pool.ts +99 -0
- package/src/tx_pool/index.ts +3 -0
- package/src/tx_pool/memory_tx_pool.ts +87 -0
- package/src/tx_pool/tx_pool.ts +44 -0
- package/src/tx_pool/tx_pool_test_suite.ts +57 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { L2Block, L2BlockSource, TxEffect, TxHash, TxReceipt, TxStatus } from '@aztec/circuit-types';
|
|
2
|
+
import { EthAddress } from '@aztec/circuits.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A mocked implementation of L2BlockSource to be used in p2p tests.
|
|
6
|
+
*/
|
|
7
|
+
export class MockBlockSource implements L2BlockSource {
|
|
8
|
+
private l2Blocks: L2Block[] = [];
|
|
9
|
+
private txEffects: TxEffect[] = [];
|
|
10
|
+
|
|
11
|
+
constructor(private numBlocks = 100) {
|
|
12
|
+
for (let i = 0; i < this.numBlocks; i++) {
|
|
13
|
+
const block = L2Block.random(i);
|
|
14
|
+
this.l2Blocks.push(block);
|
|
15
|
+
this.txEffects.push(...block.getTxs());
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Method to fetch the rollup contract address at the base-layer.
|
|
21
|
+
* @returns The rollup address.
|
|
22
|
+
*/
|
|
23
|
+
getRollupAddress(): Promise<EthAddress> {
|
|
24
|
+
return Promise.resolve(EthAddress.random());
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Method to fetch the registry contract address at the base-layer.
|
|
29
|
+
* @returns The registry address.
|
|
30
|
+
*/
|
|
31
|
+
getRegistryAddress(): Promise<EthAddress> {
|
|
32
|
+
return Promise.resolve(EthAddress.random());
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Gets the number of the latest L2 block processed by the block source implementation.
|
|
37
|
+
* @returns In this mock instance, returns the number of L2 blocks that we've mocked.
|
|
38
|
+
*/
|
|
39
|
+
public getBlockNumber() {
|
|
40
|
+
return Promise.resolve(this.l2Blocks.length - 1);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Gets an l2 block.
|
|
45
|
+
* @param number - The block number to return (inclusive).
|
|
46
|
+
* @returns The requested L2 block.
|
|
47
|
+
*/
|
|
48
|
+
public getBlock(number: number) {
|
|
49
|
+
return Promise.resolve(this.l2Blocks[number]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
54
|
+
* @param from - Number of the first block to return (inclusive).
|
|
55
|
+
* @param limit - The maximum number of blocks to return.
|
|
56
|
+
* @returns The requested mocked L2 blocks.
|
|
57
|
+
*/
|
|
58
|
+
public getBlocks(from: number, limit: number) {
|
|
59
|
+
return Promise.resolve(this.l2Blocks.slice(from, from + limit));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Gets a tx effect.
|
|
64
|
+
* @param txHash - The hash of a transaction which resulted in the returned tx effect.
|
|
65
|
+
* @returns The requested tx effect.
|
|
66
|
+
*/
|
|
67
|
+
public getTxEffect(txHash: TxHash) {
|
|
68
|
+
const txEffect = this.txEffects.find(tx => tx.txHash.equals(txHash));
|
|
69
|
+
return Promise.resolve(txEffect);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Gets a receipt of a settled tx.
|
|
74
|
+
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
75
|
+
* @returns The requested tx receipt (or undefined if not found).
|
|
76
|
+
*/
|
|
77
|
+
public getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
|
|
78
|
+
for (const block of this.l2Blocks) {
|
|
79
|
+
for (const txEffect of block.body.txEffects) {
|
|
80
|
+
if (txEffect.txHash.equals(txHash)) {
|
|
81
|
+
return Promise.resolve(new TxReceipt(txHash, TxStatus.MINED, '', block.hash().toBuffer(), block.number));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return Promise.resolve(undefined);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Starts the block source. In this mock implementation, this is a noop.
|
|
90
|
+
* @returns A promise that signals the initialization of the l2 block source on completion.
|
|
91
|
+
*/
|
|
92
|
+
public start(): Promise<void> {
|
|
93
|
+
return Promise.resolve();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Stops the block source. In this mock implementation, this is a noop.
|
|
98
|
+
* @returns A promise that signals the l2 block source is now stopped.
|
|
99
|
+
*/
|
|
100
|
+
public stop(): Promise<void> {
|
|
101
|
+
return Promise.resolve();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { L2Block, L2BlockContext, L2BlockDownloader, L2BlockSource, Tx, TxHash } from '@aztec/circuit-types';
|
|
2
|
+
import { INITIAL_L2_BLOCK_NUM } from '@aztec/circuits.js/constants';
|
|
3
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
4
|
+
import { AztecKVStore, AztecSingleton } from '@aztec/kv-store';
|
|
5
|
+
|
|
6
|
+
import { getP2PConfigEnvVars } from '../config.js';
|
|
7
|
+
import { P2PService } from '../service/service.js';
|
|
8
|
+
import { TxPool } from '../tx_pool/index.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Enum defining the possible states of the p2p client.
|
|
12
|
+
*/
|
|
13
|
+
export enum P2PClientState {
|
|
14
|
+
IDLE,
|
|
15
|
+
SYNCHING,
|
|
16
|
+
RUNNING,
|
|
17
|
+
STOPPED,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The synchronization status of the P2P client.
|
|
22
|
+
*/
|
|
23
|
+
export interface P2PSyncState {
|
|
24
|
+
/**
|
|
25
|
+
* The current state of the p2p client.
|
|
26
|
+
*/
|
|
27
|
+
state: P2PClientState;
|
|
28
|
+
/**
|
|
29
|
+
* The block number that the p2p client is synced to.
|
|
30
|
+
*/
|
|
31
|
+
syncedToL2Block: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Interface of a P2P client.
|
|
36
|
+
**/
|
|
37
|
+
export interface P2P {
|
|
38
|
+
/**
|
|
39
|
+
* Verifies the 'tx' and, if valid, adds it to local tx pool and forwards it to other peers.
|
|
40
|
+
* @param tx - The transaction.
|
|
41
|
+
**/
|
|
42
|
+
sendTx(tx: Tx): Promise<void>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Deletes 'txs' from the pool, given hashes.
|
|
46
|
+
* NOT used if we use sendTx as reconcileTxPool will handle this.
|
|
47
|
+
* @param txHashes - Hashes to check.
|
|
48
|
+
**/
|
|
49
|
+
deleteTxs(txHashes: TxHash[]): Promise<void>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Returns all transactions in the transaction pool.
|
|
53
|
+
* @returns An array of Txs.
|
|
54
|
+
*/
|
|
55
|
+
getTxs(): Promise<Tx[]>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns a transaction in the transaction pool by its hash.
|
|
59
|
+
* @param txHash - Hash of tx to return.
|
|
60
|
+
* @returns A single tx or undefined.
|
|
61
|
+
*/
|
|
62
|
+
getTxByHash(txHash: TxHash): Promise<Tx | undefined>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Starts the p2p client.
|
|
66
|
+
* @returns A promise signalling the completion of the block sync.
|
|
67
|
+
*/
|
|
68
|
+
start(): Promise<void>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Stops the p2p client.
|
|
72
|
+
* @returns A promise signalling the completion of the stop process.
|
|
73
|
+
*/
|
|
74
|
+
stop(): Promise<void>;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Indicates if the p2p client is ready for transaction submission.
|
|
78
|
+
* @returns A boolean flag indicating readiness.
|
|
79
|
+
*/
|
|
80
|
+
isReady(): Promise<boolean>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Returns the current status of the p2p client.
|
|
84
|
+
*/
|
|
85
|
+
getStatus(): Promise<P2PSyncState>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The P2P client implementation.
|
|
90
|
+
*/
|
|
91
|
+
export class P2PClient implements P2P {
|
|
92
|
+
/**
|
|
93
|
+
* L2 Block download that p2p client uses to stay in sync with latest blocks.
|
|
94
|
+
*/
|
|
95
|
+
private blockDownloader: L2BlockDownloader;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Property that indicates whether the client is running.
|
|
99
|
+
*/
|
|
100
|
+
private stopping = false;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The JS promise that will be running to keep the client's data in sync. Can be interrupted if the client is stopped.
|
|
104
|
+
*/
|
|
105
|
+
private runningPromise!: Promise<void>;
|
|
106
|
+
|
|
107
|
+
private currentState = P2PClientState.IDLE;
|
|
108
|
+
private syncPromise = Promise.resolve();
|
|
109
|
+
private latestBlockNumberAtStart = -1;
|
|
110
|
+
private syncResolve?: () => void = undefined;
|
|
111
|
+
private synchedBlockNumber: AztecSingleton<number>;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* In-memory P2P client constructor.
|
|
115
|
+
* @param store - The client's instance of the KV store.
|
|
116
|
+
* @param l2BlockSource - P2P client's source for fetching existing blocks.
|
|
117
|
+
* @param txPool - The client's instance of a transaction pool. Defaults to in-memory implementation.
|
|
118
|
+
* @param p2pService - The concrete instance of p2p networking to use.
|
|
119
|
+
* @param log - A logger.
|
|
120
|
+
*/
|
|
121
|
+
constructor(
|
|
122
|
+
store: AztecKVStore,
|
|
123
|
+
private l2BlockSource: L2BlockSource,
|
|
124
|
+
private txPool: TxPool,
|
|
125
|
+
private p2pService: P2PService,
|
|
126
|
+
private log = createDebugLogger('aztec:p2p'),
|
|
127
|
+
) {
|
|
128
|
+
const { p2pBlockCheckIntervalMS: checkInterval, p2pL2QueueSize } = getP2PConfigEnvVars();
|
|
129
|
+
this.blockDownloader = new L2BlockDownloader(l2BlockSource, p2pL2QueueSize, checkInterval);
|
|
130
|
+
this.synchedBlockNumber = store.openSingleton('p2p_pool_last_l2_block');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Starts the P2P client.
|
|
135
|
+
* @returns An empty promise signalling the synching process.
|
|
136
|
+
*/
|
|
137
|
+
public async start() {
|
|
138
|
+
if (this.currentState === P2PClientState.STOPPED) {
|
|
139
|
+
throw new Error('P2P client already stopped');
|
|
140
|
+
}
|
|
141
|
+
if (this.currentState !== P2PClientState.IDLE) {
|
|
142
|
+
return this.syncPromise;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// get the current latest block number
|
|
146
|
+
this.latestBlockNumberAtStart = await this.l2BlockSource.getBlockNumber();
|
|
147
|
+
|
|
148
|
+
const blockToDownloadFrom = this.getSyncedBlockNum() + 1;
|
|
149
|
+
|
|
150
|
+
// if there are blocks to be retrieved, go to a synching state
|
|
151
|
+
if (blockToDownloadFrom <= this.latestBlockNumberAtStart) {
|
|
152
|
+
this.setCurrentState(P2PClientState.SYNCHING);
|
|
153
|
+
this.syncPromise = new Promise(resolve => {
|
|
154
|
+
this.syncResolve = resolve;
|
|
155
|
+
});
|
|
156
|
+
this.log(`Starting sync from ${blockToDownloadFrom}, latest block ${this.latestBlockNumberAtStart}`);
|
|
157
|
+
} else {
|
|
158
|
+
// if no blocks to be retrieved, go straight to running
|
|
159
|
+
this.setCurrentState(P2PClientState.RUNNING);
|
|
160
|
+
this.syncPromise = Promise.resolve();
|
|
161
|
+
await this.p2pService.start();
|
|
162
|
+
this.log(`Next block ${blockToDownloadFrom} already beyond latest block at ${this.latestBlockNumberAtStart}`);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// publish any txs in TxPool after its doing initial sync
|
|
166
|
+
this.syncPromise = this.syncPromise.then(() => this.publishStoredTxs());
|
|
167
|
+
|
|
168
|
+
// start looking for further blocks
|
|
169
|
+
const blockProcess = async () => {
|
|
170
|
+
while (!this.stopping) {
|
|
171
|
+
const blocks = await this.blockDownloader.getBlocks();
|
|
172
|
+
await this.handleL2Blocks(blocks);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
this.runningPromise = blockProcess();
|
|
176
|
+
this.blockDownloader.start(blockToDownloadFrom);
|
|
177
|
+
this.log(`Started block downloader from block ${blockToDownloadFrom}`);
|
|
178
|
+
|
|
179
|
+
return this.syncPromise;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Allows consumers to stop the instance of the P2P client.
|
|
184
|
+
* 'ready' will now return 'false' and the running promise that keeps the client synced is interrupted.
|
|
185
|
+
*/
|
|
186
|
+
public async stop() {
|
|
187
|
+
this.log('Stopping p2p client...');
|
|
188
|
+
this.stopping = true;
|
|
189
|
+
await this.p2pService.stop();
|
|
190
|
+
this.log('Stopped p2p service');
|
|
191
|
+
await this.blockDownloader.stop();
|
|
192
|
+
this.log('Stopped block downloader');
|
|
193
|
+
await this.runningPromise;
|
|
194
|
+
this.setCurrentState(P2PClientState.STOPPED);
|
|
195
|
+
this.log('P2P client stopped...');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Returns all transactions in the transaction pool.
|
|
200
|
+
* @returns An array of Txs.
|
|
201
|
+
*/
|
|
202
|
+
public getTxs(): Promise<Tx[]> {
|
|
203
|
+
return Promise.resolve(this.txPool.getAllTxs());
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Returns a transaction in the transaction pool by its hash.
|
|
208
|
+
* @param txHash - Hash of the transaction to look for in the pool.
|
|
209
|
+
* @returns A single tx or undefined.
|
|
210
|
+
*/
|
|
211
|
+
getTxByHash(txHash: TxHash): Promise<Tx | undefined> {
|
|
212
|
+
return Promise.resolve(this.txPool.getTxByHash(txHash));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Verifies the 'tx' and, if valid, adds it to local tx pool and forwards it to other peers.
|
|
217
|
+
* @param tx - The tx to verify.
|
|
218
|
+
* @returns Empty promise.
|
|
219
|
+
**/
|
|
220
|
+
public async sendTx(tx: Tx): Promise<void> {
|
|
221
|
+
const ready = await this.isReady();
|
|
222
|
+
if (!ready) {
|
|
223
|
+
throw new Error('P2P client not ready');
|
|
224
|
+
}
|
|
225
|
+
await this.txPool.addTxs([tx]);
|
|
226
|
+
this.p2pService.propagateTx(tx);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Deletes the 'txs' from the pool.
|
|
231
|
+
* NOT used if we use sendTx as reconcileTxPool will handle this.
|
|
232
|
+
* @param txHashes - Hashes of the transactions to delete.
|
|
233
|
+
* @returns Empty promise.
|
|
234
|
+
**/
|
|
235
|
+
public async deleteTxs(txHashes: TxHash[]): Promise<void> {
|
|
236
|
+
const ready = await this.isReady();
|
|
237
|
+
if (!ready) {
|
|
238
|
+
throw new Error('P2P client not ready');
|
|
239
|
+
}
|
|
240
|
+
await this.txPool.deleteTxs(txHashes);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Public function to check if the p2p client is fully synced and ready to receive txs.
|
|
245
|
+
* @returns True if the P2P client is ready to receive txs.
|
|
246
|
+
*/
|
|
247
|
+
public isReady() {
|
|
248
|
+
return Promise.resolve(this.currentState === P2PClientState.RUNNING);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Public function to check the latest block number that the P2P client is synced to.
|
|
253
|
+
* @returns Block number of latest L2 Block we've synced with.
|
|
254
|
+
*/
|
|
255
|
+
public getSyncedBlockNum() {
|
|
256
|
+
return this.synchedBlockNumber.get() ?? INITIAL_L2_BLOCK_NUM - 1;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Method to check the status the p2p client.
|
|
261
|
+
* @returns Information about p2p client status: state & syncedToBlockNum.
|
|
262
|
+
*/
|
|
263
|
+
public getStatus(): Promise<P2PSyncState> {
|
|
264
|
+
return Promise.resolve({
|
|
265
|
+
state: this.currentState,
|
|
266
|
+
syncedToL2Block: this.getSyncedBlockNum(),
|
|
267
|
+
} as P2PSyncState);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Internal method that uses the provided blocks to check against the client's tx pool.
|
|
272
|
+
* @param blocks - A list of existing blocks with txs that the P2P client needs to ensure the tx pool is reconciled with.
|
|
273
|
+
* @returns Empty promise.
|
|
274
|
+
*/
|
|
275
|
+
private async reconcileTxPool(blocks: L2Block[]): Promise<void> {
|
|
276
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
277
|
+
const blockContext = new L2BlockContext(blocks[i]);
|
|
278
|
+
const txHashes = blockContext.getTxHashes();
|
|
279
|
+
await this.txPool.deleteTxs(txHashes);
|
|
280
|
+
this.p2pService.settledTxs(txHashes);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Method for processing new blocks.
|
|
286
|
+
* @param blocks - A list of existing blocks with txs that the P2P client needs to ensure the tx pool is reconciled with.
|
|
287
|
+
* @returns Empty promise.
|
|
288
|
+
*/
|
|
289
|
+
private async handleL2Blocks(blocks: L2Block[]): Promise<void> {
|
|
290
|
+
if (!blocks.length) {
|
|
291
|
+
return Promise.resolve();
|
|
292
|
+
}
|
|
293
|
+
await this.reconcileTxPool(blocks);
|
|
294
|
+
const lastBlockNum = blocks[blocks.length - 1].number;
|
|
295
|
+
await this.synchedBlockNumber.set(lastBlockNum);
|
|
296
|
+
this.log(`Synched to block ${lastBlockNum}`);
|
|
297
|
+
|
|
298
|
+
if (this.currentState === P2PClientState.SYNCHING && lastBlockNum >= this.latestBlockNumberAtStart) {
|
|
299
|
+
this.setCurrentState(P2PClientState.RUNNING);
|
|
300
|
+
if (this.syncResolve !== undefined) {
|
|
301
|
+
this.syncResolve();
|
|
302
|
+
await this.p2pService.start();
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Method to set the value of the current state.
|
|
309
|
+
* @param newState - New state value.
|
|
310
|
+
*/
|
|
311
|
+
private setCurrentState(newState: P2PClientState) {
|
|
312
|
+
this.currentState = newState;
|
|
313
|
+
this.log(`Moved to state ${P2PClientState[this.currentState]}`);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
private async publishStoredTxs() {
|
|
317
|
+
if (!this.isReady()) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const txs = this.txPool.getAllTxs();
|
|
322
|
+
if (txs.length > 0) {
|
|
323
|
+
this.log(`Publishing ${txs.length} previously stored txs`);
|
|
324
|
+
await Promise.all(txs.map(tx => this.p2pService.propagateTx(tx)));
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* P2P client configuration values.
|
|
3
|
+
*/
|
|
4
|
+
export interface P2PConfig {
|
|
5
|
+
/**
|
|
6
|
+
* A flag dictating whether the P2P subsystem should be enabled.
|
|
7
|
+
*/
|
|
8
|
+
p2pEnabled: boolean;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The frequency in which to check.
|
|
12
|
+
*/
|
|
13
|
+
p2pBlockCheckIntervalMS: number;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Size of queue of L2 blocks to store.
|
|
17
|
+
*/
|
|
18
|
+
p2pL2QueueSize: number;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The tcp port on which the P2P service should listen for connections.
|
|
22
|
+
*/
|
|
23
|
+
tcpListenPort: number;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The tcp IP on which the P2P service should listen for connections.
|
|
27
|
+
*/
|
|
28
|
+
tcpListenIp: string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* An optional peer id private key. If blank, will generate a random key.
|
|
32
|
+
*/
|
|
33
|
+
peerIdPrivateKey?: string;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A list of bootstrap peers to connect to.
|
|
37
|
+
*/
|
|
38
|
+
bootstrapNodes: string[];
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Protocol identifier for transaction gossiping.
|
|
42
|
+
*/
|
|
43
|
+
transactionProtocol: string;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Hostname to announce.
|
|
47
|
+
*/
|
|
48
|
+
announceHostname?: string;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Port to announce.
|
|
52
|
+
*/
|
|
53
|
+
announcePort?: number;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Optional specification to run as a client in the Kademlia routing protocol.
|
|
57
|
+
*/
|
|
58
|
+
clientKADRouting: boolean;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Whether to enable NAT from libp2p (ignored for bootstrap node).
|
|
62
|
+
*/
|
|
63
|
+
enableNat?: boolean;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The minimum number of peers (a peer count below this will cause the node to look for more peers)
|
|
67
|
+
*/
|
|
68
|
+
minPeerCount: number;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The maximum number of peers (a peer count above this will cause the node to refuse connection attempts)
|
|
72
|
+
*/
|
|
73
|
+
maxPeerCount: number;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Gets the config values for p2p client from environment variables.
|
|
78
|
+
* @returns The config values for p2p client.
|
|
79
|
+
*/
|
|
80
|
+
export function getP2PConfigEnvVars(): P2PConfig {
|
|
81
|
+
const {
|
|
82
|
+
P2P_ENABLED,
|
|
83
|
+
P2P_BLOCK_CHECK_INTERVAL_MS,
|
|
84
|
+
P2P_L2_BLOCK_QUEUE_SIZE,
|
|
85
|
+
P2P_TCP_LISTEN_PORT,
|
|
86
|
+
P2P_TCP_LISTEN_IP,
|
|
87
|
+
PEER_ID_PRIVATE_KEY,
|
|
88
|
+
BOOTSTRAP_NODES,
|
|
89
|
+
P2P_ANNOUNCE_HOSTNAME,
|
|
90
|
+
P2P_ANNOUNCE_PORT,
|
|
91
|
+
P2P_KAD_CLIENT,
|
|
92
|
+
P2P_NAT_ENABLED,
|
|
93
|
+
P2P_MIN_PEERS,
|
|
94
|
+
P2P_MAX_PEERS,
|
|
95
|
+
} = process.env;
|
|
96
|
+
const envVars: P2PConfig = {
|
|
97
|
+
p2pEnabled: P2P_ENABLED === 'true',
|
|
98
|
+
p2pBlockCheckIntervalMS: P2P_BLOCK_CHECK_INTERVAL_MS ? +P2P_BLOCK_CHECK_INTERVAL_MS : 100,
|
|
99
|
+
p2pL2QueueSize: P2P_L2_BLOCK_QUEUE_SIZE ? +P2P_L2_BLOCK_QUEUE_SIZE : 1000,
|
|
100
|
+
tcpListenPort: P2P_TCP_LISTEN_PORT ? +P2P_TCP_LISTEN_PORT : 40400,
|
|
101
|
+
tcpListenIp: P2P_TCP_LISTEN_IP ? P2P_TCP_LISTEN_IP : '0.0.0.0',
|
|
102
|
+
peerIdPrivateKey: PEER_ID_PRIVATE_KEY,
|
|
103
|
+
bootstrapNodes: BOOTSTRAP_NODES ? BOOTSTRAP_NODES.split(',') : [],
|
|
104
|
+
transactionProtocol: '',
|
|
105
|
+
announceHostname: P2P_ANNOUNCE_HOSTNAME,
|
|
106
|
+
announcePort: P2P_ANNOUNCE_PORT ? +P2P_ANNOUNCE_PORT : undefined,
|
|
107
|
+
clientKADRouting: P2P_KAD_CLIENT === 'true',
|
|
108
|
+
enableNat: P2P_NAT_ENABLED === 'true',
|
|
109
|
+
minPeerCount: P2P_MIN_PEERS ? +P2P_MIN_PEERS : 10,
|
|
110
|
+
maxPeerCount: P2P_MAX_PEERS ? +P2P_MAX_PEERS : 100,
|
|
111
|
+
};
|
|
112
|
+
return envVars;
|
|
113
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Tx, TxHash } from '@aztec/circuit-types';
|
|
2
|
+
|
|
3
|
+
import { P2PService } from './service.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A dummy implementation of the P2P Service.
|
|
7
|
+
*/
|
|
8
|
+
export class DummyP2PService implements P2PService {
|
|
9
|
+
/**
|
|
10
|
+
* Starts the dummy implementation.
|
|
11
|
+
* @returns A resolved promise.
|
|
12
|
+
*/
|
|
13
|
+
public start() {
|
|
14
|
+
return Promise.resolve();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Stops the dummy implementation.
|
|
19
|
+
* @returns A resolved promise.
|
|
20
|
+
*/
|
|
21
|
+
public stop() {
|
|
22
|
+
return Promise.resolve();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Called to have the given transaction propagated through the P2P network.
|
|
27
|
+
* @param _ - The transaction to be propagated.
|
|
28
|
+
*/
|
|
29
|
+
public propagateTx(_: Tx) {}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Called upon receipt of settled transactions.
|
|
33
|
+
* @param _ - The hashes of the settled transactions.
|
|
34
|
+
*/
|
|
35
|
+
public settledTxs(_: TxHash[]) {}
|
|
36
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { PeerId } from '@libp2p/interface-peer-id';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Keeps a record of which Peers have 'seen' which transactions.
|
|
5
|
+
*/
|
|
6
|
+
export class KnownTxLookup {
|
|
7
|
+
private lookup: { [key: string]: { [key: string]: boolean } } = {};
|
|
8
|
+
|
|
9
|
+
constructor() {}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Inform this lookup that a peer has 'seen' a transaction.
|
|
13
|
+
* @param peerId - The peerId of the peer that has 'seen' the transaction.
|
|
14
|
+
* @param txHash - The thHash of the 'seen' transaction.
|
|
15
|
+
*/
|
|
16
|
+
public addPeerForTx(peerId: PeerId, txHash: string) {
|
|
17
|
+
const peerIdAsString = peerId.toString();
|
|
18
|
+
const existingLookup = this.lookup[txHash];
|
|
19
|
+
if (existingLookup === undefined) {
|
|
20
|
+
const newLookup: { [key: string]: boolean } = {};
|
|
21
|
+
newLookup[peerIdAsString] = true;
|
|
22
|
+
this.lookup[txHash] = newLookup;
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
existingLookup[peerIdAsString] = true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Determine if a peer has 'seen' a transaction.
|
|
30
|
+
* @param peerId - The peerId of the peer.
|
|
31
|
+
* @param txHash - The thHash of the transaction.
|
|
32
|
+
* @returns A boolean indicating if the transaction has been 'seen' by the peer.
|
|
33
|
+
*/
|
|
34
|
+
public hasPeerSeenTx(peerId: PeerId, txHash: string) {
|
|
35
|
+
const existingLookup = this.lookup[txHash];
|
|
36
|
+
if (existingLookup === undefined) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
const peerIdAsString = peerId.toString();
|
|
40
|
+
return !!existingLookup[peerIdAsString];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Updates the lookup from the result of settled txs
|
|
45
|
+
* These txs will be cleared out of the lookup.
|
|
46
|
+
* It is possible that some txs could still be gossiped for a
|
|
47
|
+
* short period of time meaning they come back into this lookup
|
|
48
|
+
* but this should be infrequent and cause no undesirable effects
|
|
49
|
+
* @param txHashes - The hashes of the newly settled transactions
|
|
50
|
+
*/
|
|
51
|
+
public handleSettledTxs(txHashes: string[]) {
|
|
52
|
+
for (const txHash of txHashes) {
|
|
53
|
+
delete this.lookup[txHash];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|