@aztec/p2p 0.55.0 → 0.56.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.
- package/dest/attestation_pool/memory_attestation_pool.d.ts.map +1 -1
- package/dest/attestation_pool/memory_attestation_pool.js +8 -7
- package/dest/attestation_pool/mocks.d.ts.map +1 -1
- package/dest/attestation_pool/mocks.js +6 -5
- package/dest/client/index.d.ts +2 -2
- package/dest/client/index.d.ts.map +1 -1
- package/dest/client/index.js +43 -38
- package/dest/client/p2p_client.d.ts.map +1 -1
- package/dest/client/p2p_client.js +1 -3
- package/dest/config.d.ts +49 -0
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +66 -2
- package/dest/index.d.ts +1 -0
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -1
- package/dest/mocks/index.d.ts +13 -4
- package/dest/mocks/index.d.ts.map +1 -1
- package/dest/mocks/index.js +26 -9
- package/dest/service/libp2p_service.d.ts +25 -11
- package/dest/service/libp2p_service.d.ts.map +1 -1
- package/dest/service/libp2p_service.js +143 -28
- package/dest/service/peer_manager.d.ts +9 -13
- package/dest/service/peer_manager.d.ts.map +1 -1
- package/dest/service/peer_manager.js +15 -1
- package/dest/service/peer_scoring.d.ts +32 -0
- package/dest/service/peer_scoring.d.ts.map +1 -0
- package/dest/service/peer_scoring.js +67 -0
- package/dest/service/reqresp/interface.d.ts +7 -0
- package/dest/service/reqresp/interface.d.ts.map +1 -1
- package/dest/service/reqresp/interface.js +7 -1
- package/dest/service/reqresp/rate_limiter/rate_limiter.d.ts +13 -3
- package/dest/service/reqresp/rate_limiter/rate_limiter.d.ts.map +1 -1
- package/dest/service/reqresp/rate_limiter/rate_limiter.js +29 -7
- package/dest/service/reqresp/reqresp.d.ts +56 -5
- package/dest/service/reqresp/reqresp.d.ts.map +1 -1
- package/dest/service/reqresp/reqresp.js +73 -9
- package/dest/tx_validator/aggregate_tx_validator.d.ts +8 -0
- package/dest/tx_validator/aggregate_tx_validator.d.ts.map +1 -0
- package/dest/tx_validator/aggregate_tx_validator.js +32 -0
- package/dest/tx_validator/data_validator.d.ts +7 -0
- package/dest/tx_validator/data_validator.d.ts.map +1 -0
- package/dest/tx_validator/data_validator.js +50 -0
- package/dest/tx_validator/double_spend_validator.d.ts +13 -0
- package/dest/tx_validator/double_spend_validator.d.ts.map +1 -0
- package/dest/tx_validator/double_spend_validator.js +56 -0
- package/dest/tx_validator/index.d.ts +6 -0
- package/dest/tx_validator/index.d.ts.map +1 -0
- package/dest/tx_validator/index.js +6 -0
- package/dest/tx_validator/metadata_validator.d.ts +11 -0
- package/dest/tx_validator/metadata_validator.d.ts.map +1 -0
- package/dest/tx_validator/metadata_validator.js +53 -0
- package/dest/tx_validator/tx_proof_validator.d.ts +9 -0
- package/dest/tx_validator/tx_proof_validator.d.ts.map +1 -0
- package/dest/tx_validator/tx_proof_validator.js +29 -0
- package/dest/util.d.ts +7 -0
- package/dest/util.d.ts.map +1 -1
- package/dest/util.js +1 -1
- package/package.json +6 -6
- package/src/attestation_pool/memory_attestation_pool.ts +7 -6
- package/src/attestation_pool/mocks.ts +6 -4
- package/src/client/index.ts +65 -47
- package/src/client/p2p_client.ts +0 -2
- package/src/config.ts +127 -0
- package/src/index.ts +1 -0
- package/src/mocks/index.ts +35 -7
- package/src/service/libp2p_service.ts +182 -36
- package/src/service/peer_manager.ts +23 -4
- package/src/service/peer_scoring.ts +81 -0
- package/src/service/reqresp/interface.ts +20 -0
- package/src/service/reqresp/rate_limiter/rate_limiter.ts +30 -7
- package/src/service/reqresp/reqresp.ts +82 -8
- package/src/tx_validator/aggregate_tx_validator.ts +34 -0
- package/src/tx_validator/data_validator.ts +65 -0
- package/src/tx_validator/double_spend_validator.ts +69 -0
- package/src/tx_validator/index.ts +5 -0
- package/src/tx_validator/metadata_validator.ts +65 -0
- package/src/tx_validator/tx_proof_validator.ts +28 -0
- package/src/util.ts +8 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { BlockAttestation,
|
|
1
|
+
import { BlockAttestation, ConsensusPayload, TxHash } from '@aztec/circuit-types';
|
|
2
2
|
import { makeHeader } from '@aztec/circuits.js/testing';
|
|
3
|
+
import { Signature } from '@aztec/foundation/eth-signature';
|
|
3
4
|
import { Fr } from '@aztec/foundation/fields';
|
|
4
|
-
import { serializeToBuffer } from '@aztec/foundation/serialize';
|
|
5
5
|
|
|
6
6
|
import { type PrivateKeyAccount } from 'viem';
|
|
7
7
|
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
|
|
@@ -28,9 +28,11 @@ export const mockAttestation = async (signer: PrivateKeyAccount, slot: number =
|
|
|
28
28
|
const archive = Fr.random();
|
|
29
29
|
const txs = [0, 1, 2, 3, 4, 5].map(() => TxHash.random());
|
|
30
30
|
|
|
31
|
-
const
|
|
31
|
+
const payload = new ConsensusPayload(header, archive, txs);
|
|
32
|
+
|
|
33
|
+
const message: `0x${string}` = `0x${payload.getPayloadToSign().toString('hex')}`;
|
|
32
34
|
const sigString = await signer.signMessage({ message });
|
|
33
35
|
|
|
34
36
|
const signature = Signature.from0xString(sigString);
|
|
35
|
-
return new BlockAttestation(
|
|
37
|
+
return new BlockAttestation(payload, signature);
|
|
36
38
|
};
|
package/src/client/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ClientProtocolCircuitVerifier, L2BlockSource, WorldStateSynchronizer } from '@aztec/circuit-types';
|
|
2
2
|
import { createDebugLogger } from '@aztec/foundation/log';
|
|
3
3
|
import { type AztecKVStore } from '@aztec/kv-store';
|
|
4
4
|
import { type DataStoreConfig, createStore } from '@aztec/kv-store/utils';
|
|
@@ -17,69 +17,87 @@ import { getPublicIp, resolveAddressIfNecessary, splitAddressPort } from '../uti
|
|
|
17
17
|
export * from './p2p_client.js';
|
|
18
18
|
|
|
19
19
|
export const createP2PClient = async (
|
|
20
|
-
|
|
20
|
+
_config: P2PConfig & DataStoreConfig,
|
|
21
21
|
attestationsPool: AttestationPool,
|
|
22
22
|
l2BlockSource: L2BlockSource,
|
|
23
|
+
proofVerifier: ClientProtocolCircuitVerifier,
|
|
24
|
+
worldStateSynchronizer: WorldStateSynchronizer,
|
|
23
25
|
telemetry: TelemetryClient = new NoopTelemetryClient(),
|
|
24
26
|
deps: { txPool?: TxPool; store?: AztecKVStore } = {},
|
|
25
27
|
) => {
|
|
28
|
+
let config = { ..._config };
|
|
26
29
|
const store = deps.store ?? (await createStore('p2p', config, createDebugLogger('aztec:p2p:lmdb')));
|
|
27
30
|
const txPool = deps.txPool ?? new AztecKVTxPool(store, telemetry);
|
|
28
31
|
|
|
29
32
|
let p2pService;
|
|
30
33
|
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const {
|
|
35
|
-
tcpAnnounceAddress: configTcpAnnounceAddress,
|
|
36
|
-
udpAnnounceAddress: configUdpAnnounceAddress,
|
|
37
|
-
queryForIp,
|
|
38
|
-
} = config;
|
|
39
|
-
|
|
40
|
-
config.tcpAnnounceAddress = configTcpAnnounceAddress
|
|
41
|
-
? await resolveAddressIfNecessary(configTcpAnnounceAddress)
|
|
42
|
-
: undefined;
|
|
43
|
-
config.udpAnnounceAddress = configUdpAnnounceAddress
|
|
44
|
-
? await resolveAddressIfNecessary(configUdpAnnounceAddress)
|
|
45
|
-
: undefined;
|
|
46
|
-
|
|
47
|
-
// create variable for re-use if needed
|
|
48
|
-
let publicIp;
|
|
49
|
-
|
|
50
|
-
// check if no announce IP was provided
|
|
51
|
-
const splitTcpAnnounceAddress = splitAddressPort(configTcpAnnounceAddress || '', true);
|
|
52
|
-
if (splitTcpAnnounceAddress.length == 2 && splitTcpAnnounceAddress[0] === '') {
|
|
53
|
-
if (queryForIp) {
|
|
54
|
-
publicIp = await getPublicIp();
|
|
55
|
-
const tcpAnnounceAddress = `${publicIp}:${splitTcpAnnounceAddress[1]}`;
|
|
56
|
-
config.tcpAnnounceAddress = tcpAnnounceAddress;
|
|
57
|
-
} else {
|
|
58
|
-
throw new Error(
|
|
59
|
-
`Invalid announceTcpAddress provided: ${configTcpAnnounceAddress}. Expected format: <addr>:<port>`,
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const splitUdpAnnounceAddress = splitAddressPort(configUdpAnnounceAddress || '', true);
|
|
65
|
-
if (splitUdpAnnounceAddress.length == 2 && splitUdpAnnounceAddress[0] === '') {
|
|
66
|
-
// If announceUdpAddress is not provided, use announceTcpAddress
|
|
67
|
-
if (!queryForIp && config.tcpAnnounceAddress) {
|
|
68
|
-
config.udpAnnounceAddress = config.tcpAnnounceAddress;
|
|
69
|
-
} else if (queryForIp) {
|
|
70
|
-
const udpPublicIp = publicIp || (await getPublicIp());
|
|
71
|
-
const udpAnnounceAddress = `${udpPublicIp}:${splitUdpAnnounceAddress[1]}`;
|
|
72
|
-
config.udpAnnounceAddress = udpAnnounceAddress;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
34
|
+
if (_config.p2pEnabled) {
|
|
35
|
+
config = await configureP2PClientAddresses(_config);
|
|
75
36
|
|
|
76
37
|
// Create peer discovery service
|
|
77
38
|
const peerId = await createLibP2PPeerId(config.peerIdPrivateKey);
|
|
78
39
|
const discoveryService = new DiscV5Service(peerId, config);
|
|
79
40
|
|
|
80
|
-
p2pService = await LibP2PService.new(
|
|
41
|
+
p2pService = await LibP2PService.new(
|
|
42
|
+
config,
|
|
43
|
+
discoveryService,
|
|
44
|
+
peerId,
|
|
45
|
+
txPool,
|
|
46
|
+
attestationsPool,
|
|
47
|
+
l2BlockSource,
|
|
48
|
+
proofVerifier,
|
|
49
|
+
worldStateSynchronizer,
|
|
50
|
+
store,
|
|
51
|
+
);
|
|
81
52
|
} else {
|
|
82
53
|
p2pService = new DummyP2PService();
|
|
83
54
|
}
|
|
84
55
|
return new P2PClient(store, l2BlockSource, txPool, attestationsPool, p2pService, config.keepProvenTxsInPoolFor);
|
|
85
56
|
};
|
|
57
|
+
|
|
58
|
+
async function configureP2PClientAddresses(_config: P2PConfig & DataStoreConfig): Promise<P2PConfig & DataStoreConfig> {
|
|
59
|
+
const config = { ..._config };
|
|
60
|
+
const {
|
|
61
|
+
tcpAnnounceAddress: configTcpAnnounceAddress,
|
|
62
|
+
udpAnnounceAddress: configUdpAnnounceAddress,
|
|
63
|
+
queryForIp,
|
|
64
|
+
} = config;
|
|
65
|
+
|
|
66
|
+
config.tcpAnnounceAddress = configTcpAnnounceAddress
|
|
67
|
+
? await resolveAddressIfNecessary(configTcpAnnounceAddress)
|
|
68
|
+
: undefined;
|
|
69
|
+
config.udpAnnounceAddress = configUdpAnnounceAddress
|
|
70
|
+
? await resolveAddressIfNecessary(configUdpAnnounceAddress)
|
|
71
|
+
: undefined;
|
|
72
|
+
|
|
73
|
+
// create variable for re-use if needed
|
|
74
|
+
let publicIp;
|
|
75
|
+
|
|
76
|
+
// check if no announce IP was provided
|
|
77
|
+
const splitTcpAnnounceAddress = splitAddressPort(configTcpAnnounceAddress || '', true);
|
|
78
|
+
if (splitTcpAnnounceAddress.length == 2 && splitTcpAnnounceAddress[0] === '') {
|
|
79
|
+
if (queryForIp) {
|
|
80
|
+
publicIp = await getPublicIp();
|
|
81
|
+
const tcpAnnounceAddress = `${publicIp}:${splitTcpAnnounceAddress[1]}`;
|
|
82
|
+
config.tcpAnnounceAddress = tcpAnnounceAddress;
|
|
83
|
+
} else {
|
|
84
|
+
throw new Error(
|
|
85
|
+
`Invalid announceTcpAddress provided: ${configTcpAnnounceAddress}. Expected format: <addr>:<port>`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const splitUdpAnnounceAddress = splitAddressPort(configUdpAnnounceAddress || '', true);
|
|
91
|
+
if (splitUdpAnnounceAddress.length == 2 && splitUdpAnnounceAddress[0] === '') {
|
|
92
|
+
// If announceUdpAddress is not provided, use announceTcpAddress
|
|
93
|
+
if (!queryForIp && config.tcpAnnounceAddress) {
|
|
94
|
+
config.udpAnnounceAddress = config.tcpAnnounceAddress;
|
|
95
|
+
} else if (queryForIp) {
|
|
96
|
+
const udpPublicIp = publicIp || (await getPublicIp());
|
|
97
|
+
const udpAnnounceAddress = `${udpPublicIp}:${splitUdpAnnounceAddress[1]}`;
|
|
98
|
+
config.udpAnnounceAddress = udpAnnounceAddress;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return config;
|
|
103
|
+
}
|
package/src/client/p2p_client.ts
CHANGED
|
@@ -319,8 +319,6 @@ export class P2PClient implements P2P {
|
|
|
319
319
|
|
|
320
320
|
this.log.debug(`Requested ${txHash.toString()} from peer | success = ${!!tx}`);
|
|
321
321
|
if (tx) {
|
|
322
|
-
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/8485): This check is not sufficient to validate the transaction. We need to validate the entire proof.
|
|
323
|
-
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/8483): alter peer scoring system for a validator that returns an invalid transcation
|
|
324
322
|
await this.txPool.addTxs([tx]);
|
|
325
323
|
}
|
|
326
324
|
|
package/src/config.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
type ConfigMappingsType,
|
|
3
3
|
booleanConfigHelper,
|
|
4
4
|
getConfigFromMappings,
|
|
5
|
+
getDefaultConfig,
|
|
5
6
|
numberConfigHelper,
|
|
6
7
|
pickConfigMappings,
|
|
7
8
|
} from '@aztec/foundation/config';
|
|
@@ -89,6 +90,66 @@ export interface P2PConfig extends P2PReqRespConfig {
|
|
|
89
90
|
|
|
90
91
|
/** How many blocks have to pass after a block is proven before its txs are deleted (zero to delete immediately once proven) */
|
|
91
92
|
keepProvenTxsInPoolFor: number;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The interval of the gossipsub heartbeat to perform maintenance tasks.
|
|
96
|
+
*/
|
|
97
|
+
gossipsubInterval: number;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The D parameter for the gossipsub protocol.
|
|
101
|
+
*/
|
|
102
|
+
gossipsubD: number;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The Dlo parameter for the gossipsub protocol.
|
|
106
|
+
*/
|
|
107
|
+
gossipsubDlo: number;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The Dhi parameter for the gossipsub protocol.
|
|
111
|
+
*/
|
|
112
|
+
gossipsubDhi: number;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The number of gossipsub interval message cache windows to keep.
|
|
116
|
+
*/
|
|
117
|
+
gossipsubMcacheLength: number;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* How many message cache windows to include when gossiping with other pears.
|
|
121
|
+
*/
|
|
122
|
+
gossipsubMcacheGossip: number;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The 'age' (in # of L2 blocks) of a processed tx after which we heavily penalize a peer for re-sending it.
|
|
126
|
+
*/
|
|
127
|
+
severePeerPenaltyBlockLength: number;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The weight of the tx topic for the gossipsub protocol. This determines how much the score for this specific topic contributes to the overall peer score.
|
|
131
|
+
*/
|
|
132
|
+
gossipsubTxTopicWeight: number;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* This is the weight applied to the penalty for delivering invalid messages.
|
|
136
|
+
*/
|
|
137
|
+
gossipsubTxInvalidMessageDeliveriesWeight: number;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* determines how quickly the penalty for invalid message deliveries decays over time. Between 0 and 1.
|
|
141
|
+
*/
|
|
142
|
+
gossipsubTxInvalidMessageDeliveriesDecay: number;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The values for the peer scoring system. Passed as a comma separated list of values in order: low, mid, high tolerance errors.
|
|
146
|
+
*/
|
|
147
|
+
peerPenaltyValues: number[];
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The chain id of the L1 chain.
|
|
151
|
+
*/
|
|
152
|
+
l1ChainId: number;
|
|
92
153
|
}
|
|
93
154
|
|
|
94
155
|
export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
|
|
@@ -172,6 +233,68 @@ export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
|
|
|
172
233
|
'How many blocks have to pass after a block is proven before its txs are deleted (zero to delete immediately once proven)',
|
|
173
234
|
...numberConfigHelper(0),
|
|
174
235
|
},
|
|
236
|
+
gossipsubInterval: {
|
|
237
|
+
env: 'P2P_GOSSIPSUB_INTERVAL_MS',
|
|
238
|
+
description: 'The interval of the gossipsub heartbeat to perform maintenance tasks.',
|
|
239
|
+
...numberConfigHelper(1_000),
|
|
240
|
+
},
|
|
241
|
+
gossipsubD: {
|
|
242
|
+
env: 'P2P_GOSSIPSUB_D',
|
|
243
|
+
description: 'The D parameter for the gossipsub protocol.',
|
|
244
|
+
...numberConfigHelper(8),
|
|
245
|
+
},
|
|
246
|
+
gossipsubDlo: {
|
|
247
|
+
env: 'P2P_GOSSIPSUB_DLO',
|
|
248
|
+
description: 'The Dlo parameter for the gossipsub protocol.',
|
|
249
|
+
...numberConfigHelper(4),
|
|
250
|
+
},
|
|
251
|
+
gossipsubDhi: {
|
|
252
|
+
env: 'P2P_GOSSIPSUB_DHI',
|
|
253
|
+
description: 'The Dhi parameter for the gossipsub protocol.',
|
|
254
|
+
...numberConfigHelper(12),
|
|
255
|
+
},
|
|
256
|
+
gossipsubMcacheLength: {
|
|
257
|
+
env: 'P2P_GOSSIPSUB_MCACHE_LENGTH',
|
|
258
|
+
description: 'The number of gossipsub interval message cache windows to keep.',
|
|
259
|
+
...numberConfigHelper(5),
|
|
260
|
+
},
|
|
261
|
+
gossipsubMcacheGossip: {
|
|
262
|
+
env: 'P2P_GOSSIPSUB_MCACHE_GOSSIP',
|
|
263
|
+
description: 'How many message cache windows to include when gossiping with other pears.',
|
|
264
|
+
...numberConfigHelper(3),
|
|
265
|
+
},
|
|
266
|
+
gossipsubTxTopicWeight: {
|
|
267
|
+
env: 'P2P_GOSSIPSUB_TX_TOPIC_WEIGHT',
|
|
268
|
+
description: 'The weight of the tx topic for the gossipsub protocol.',
|
|
269
|
+
...numberConfigHelper(1),
|
|
270
|
+
},
|
|
271
|
+
gossipsubTxInvalidMessageDeliveriesWeight: {
|
|
272
|
+
env: 'P2P_GOSSIPSUB_TX_INVALID_MESSAGE_DELIVERIES_WEIGHT',
|
|
273
|
+
description: 'The weight of the tx invalid message deliveries for the gossipsub protocol.',
|
|
274
|
+
...numberConfigHelper(-20),
|
|
275
|
+
},
|
|
276
|
+
gossipsubTxInvalidMessageDeliveriesDecay: {
|
|
277
|
+
env: 'P2P_GOSSIPSUB_TX_INVALID_MESSAGE_DELIVERIES_DECAY',
|
|
278
|
+
description: 'Determines how quickly the penalty for invalid message deliveries decays over time. Between 0 and 1.',
|
|
279
|
+
...numberConfigHelper(0.5),
|
|
280
|
+
},
|
|
281
|
+
peerPenaltyValues: {
|
|
282
|
+
env: 'P2P_PEER_PENALTY_VALUES',
|
|
283
|
+
parseEnv: (val: string) => val.split(',').map(Number),
|
|
284
|
+
description:
|
|
285
|
+
'The values for the peer scoring system. Passed as a comma separated list of values in order: low, mid, high tolerance errors.',
|
|
286
|
+
defaultValue: [2, 10, 50],
|
|
287
|
+
},
|
|
288
|
+
severePeerPenaltyBlockLength: {
|
|
289
|
+
env: 'P2P_SEVERE_PEER_PENALTY_BLOCK_LENGTH',
|
|
290
|
+
description: 'The "age" (in L2 blocks) of a tx after which we heavily penalize a peer for sending it.',
|
|
291
|
+
...numberConfigHelper(30),
|
|
292
|
+
},
|
|
293
|
+
l1ChainId: {
|
|
294
|
+
env: 'L1_CHAIN_ID',
|
|
295
|
+
description: 'The chain id of the L1 chain.',
|
|
296
|
+
...numberConfigHelper(31337),
|
|
297
|
+
},
|
|
175
298
|
...p2pReqRespConfigMappings,
|
|
176
299
|
};
|
|
177
300
|
|
|
@@ -183,6 +306,10 @@ export function getP2PConfigEnvVars(): P2PConfig {
|
|
|
183
306
|
return getConfigFromMappings<P2PConfig>(p2pConfigMappings);
|
|
184
307
|
}
|
|
185
308
|
|
|
309
|
+
export function getP2PDefaultConfig(): P2PConfig {
|
|
310
|
+
return getDefaultConfig<P2PConfig>(p2pConfigMappings);
|
|
311
|
+
}
|
|
312
|
+
|
|
186
313
|
/**
|
|
187
314
|
* Required P2P config values for a bootstrap node.
|
|
188
315
|
*/
|
package/src/index.ts
CHANGED
package/src/mocks/index.ts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
|
+
import { type ClientProtocolCircuitVerifier, type Tx } from '@aztec/circuit-types';
|
|
2
|
+
|
|
1
3
|
import { noise } from '@chainsafe/libp2p-noise';
|
|
2
4
|
import { yamux } from '@chainsafe/libp2p-yamux';
|
|
3
5
|
import { bootstrap } from '@libp2p/bootstrap';
|
|
4
6
|
import { tcp } from '@libp2p/tcp';
|
|
5
7
|
import { type Libp2p, type Libp2pOptions, createLibp2p } from 'libp2p';
|
|
6
8
|
|
|
9
|
+
import { type PeerManager } from '../service/peer_manager.js';
|
|
7
10
|
import { type P2PReqRespConfig } from '../service/reqresp/config.js';
|
|
8
11
|
import { pingHandler, statusHandler } from '../service/reqresp/handlers.js';
|
|
9
12
|
import {
|
|
10
13
|
PING_PROTOCOL,
|
|
11
14
|
type ReqRespSubProtocolHandlers,
|
|
15
|
+
type ReqRespSubProtocolValidators,
|
|
12
16
|
STATUS_PROTOCOL,
|
|
13
17
|
TX_REQ_PROTOCOL,
|
|
18
|
+
noopValidator,
|
|
14
19
|
} from '../service/reqresp/interface.js';
|
|
15
20
|
import { ReqResp } from '../service/reqresp/reqresp.js';
|
|
16
21
|
|
|
@@ -56,18 +61,29 @@ export const MOCK_SUB_PROTOCOL_HANDLERS: ReqRespSubProtocolHandlers = {
|
|
|
56
61
|
[TX_REQ_PROTOCOL]: (_msg: any) => Promise.resolve(Uint8Array.from(Buffer.from('tx'))),
|
|
57
62
|
};
|
|
58
63
|
|
|
64
|
+
// By default, all requests are valid
|
|
65
|
+
// If you want to test an invalid response, you can override the validator
|
|
66
|
+
export const MOCK_SUB_PROTOCOL_VALIDATORS: ReqRespSubProtocolValidators = {
|
|
67
|
+
[PING_PROTOCOL]: noopValidator,
|
|
68
|
+
[STATUS_PROTOCOL]: noopValidator,
|
|
69
|
+
[TX_REQ_PROTOCOL]: noopValidator,
|
|
70
|
+
};
|
|
71
|
+
|
|
59
72
|
/**
|
|
60
73
|
* @param numberOfNodes - the number of nodes to create
|
|
61
74
|
* @returns An array of the created nodes
|
|
62
75
|
*/
|
|
63
|
-
export const createNodes = async (numberOfNodes: number): Promise<ReqRespNode[]> => {
|
|
64
|
-
return await Promise.all(Array.from({ length: numberOfNodes }, () => createReqResp()));
|
|
76
|
+
export const createNodes = async (peerManager: PeerManager, numberOfNodes: number): Promise<ReqRespNode[]> => {
|
|
77
|
+
return await Promise.all(Array.from({ length: numberOfNodes }, () => createReqResp(peerManager)));
|
|
65
78
|
};
|
|
66
79
|
|
|
67
|
-
|
|
68
|
-
|
|
80
|
+
export const startNodes = async (
|
|
81
|
+
nodes: ReqRespNode[],
|
|
82
|
+
subProtocolHandlers = MOCK_SUB_PROTOCOL_HANDLERS,
|
|
83
|
+
subProtocolValidators = MOCK_SUB_PROTOCOL_VALIDATORS,
|
|
84
|
+
) => {
|
|
69
85
|
for (const node of nodes) {
|
|
70
|
-
await node.req.start(subProtocolHandlers);
|
|
86
|
+
await node.req.start(subProtocolHandlers, subProtocolValidators);
|
|
71
87
|
}
|
|
72
88
|
};
|
|
73
89
|
|
|
@@ -79,13 +95,13 @@ export const stopNodes = async (nodes: ReqRespNode[]): Promise<void> => {
|
|
|
79
95
|
};
|
|
80
96
|
|
|
81
97
|
// Create a req resp node, exposing the underlying p2p node
|
|
82
|
-
export const createReqResp = async (): Promise<ReqRespNode> => {
|
|
98
|
+
export const createReqResp = async (peerManager: PeerManager): Promise<ReqRespNode> => {
|
|
83
99
|
const p2p = await createLibp2pNode();
|
|
84
100
|
const config: P2PReqRespConfig = {
|
|
85
101
|
overallRequestTimeoutMs: 4000,
|
|
86
102
|
individualRequestTimeoutMs: 2000,
|
|
87
103
|
};
|
|
88
|
-
const req = new ReqResp(config, p2p);
|
|
104
|
+
const req = new ReqResp(config, p2p, peerManager);
|
|
89
105
|
return {
|
|
90
106
|
p2p,
|
|
91
107
|
req,
|
|
@@ -104,3 +120,15 @@ export const connectToPeers = async (nodes: ReqRespNode[]): Promise<void> => {
|
|
|
104
120
|
}
|
|
105
121
|
}
|
|
106
122
|
};
|
|
123
|
+
|
|
124
|
+
// Mock circuit verifier for testing - reimplementation from bb to avoid dependency
|
|
125
|
+
export class AlwaysTrueCircuitVerifier implements ClientProtocolCircuitVerifier {
|
|
126
|
+
verifyProof(_tx: Tx): Promise<boolean> {
|
|
127
|
+
return Promise.resolve(true);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
export class AlwaysFalseCircuitVerifier implements ClientProtocolCircuitVerifier {
|
|
131
|
+
verifyProof(_tx: Tx): Promise<boolean> {
|
|
132
|
+
return Promise.resolve(false);
|
|
133
|
+
}
|
|
134
|
+
}
|