@aztec/p2p 0.0.1-commit.e588bc7e5 → 0.0.1-commit.e5a3663dd
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/factory.d.ts +3 -2
- package/dest/client/factory.d.ts.map +1 -1
- package/dest/client/factory.js +17 -16
- package/dest/client/p2p_client.d.ts +1 -1
- package/dest/client/p2p_client.d.ts.map +1 -1
- package/dest/client/p2p_client.js +9 -2
- package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.js +4 -1
- package/dest/config.d.ts +107 -99
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +16 -7
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +4 -2
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool.js +5 -3
- package/dest/mem_pools/attestation_pool/mocks.d.ts +1 -1
- package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/mocks.js +6 -4
- package/dest/mem_pools/instrumentation.d.ts +1 -1
- package/dest/mem_pools/instrumentation.d.ts.map +1 -1
- package/dest/mem_pools/instrumentation.js +17 -1
- package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts +2 -1
- package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/eviction/index.js +1 -0
- package/dest/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.d.ts +16 -0
- package/dest/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.js +62 -0
- package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.js +2 -2
- package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +4 -1
- package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +5 -2
- package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_metadata.js +8 -5
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +2 -1
- package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +9 -3
- package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
- package/dest/msg_validators/attestation_validator/attestation_validator.js +34 -10
- package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +7 -3
- package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -1
- package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.js +2 -2
- package/dest/msg_validators/clock_tolerance.d.ts +12 -1
- package/dest/msg_validators/clock_tolerance.d.ts.map +1 -1
- package/dest/msg_validators/clock_tolerance.js +57 -0
- package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts +5 -2
- package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts.map +1 -1
- package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts +5 -2
- package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts.map +1 -1
- package/dest/msg_validators/proposal_validator/proposal_validator.d.ts +8 -2
- package/dest/msg_validators/proposal_validator/proposal_validator.d.ts.map +1 -1
- package/dest/msg_validators/proposal_validator/proposal_validator.js +41 -9
- package/dest/msg_validators/tx_validator/archive_cache.js +1 -1
- package/dest/msg_validators/tx_validator/factory.d.ts +2 -2
- package/dest/msg_validators/tx_validator/factory.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/factory.js +3 -3
- package/dest/msg_validators/tx_validator/gas_validator.d.ts +36 -4
- package/dest/msg_validators/tx_validator/gas_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/gas_validator.js +50 -33
- package/dest/services/data_store.d.ts +1 -1
- package/dest/services/data_store.d.ts.map +1 -1
- package/dest/services/data_store.js +5 -5
- package/dest/services/dummy_service.d.ts +2 -1
- package/dest/services/dummy_service.d.ts.map +1 -1
- package/dest/services/dummy_service.js +1 -0
- package/dest/services/gossipsub/topic_score_params.d.ts +13 -2
- package/dest/services/gossipsub/topic_score_params.d.ts.map +1 -1
- package/dest/services/gossipsub/topic_score_params.js +21 -4
- package/dest/services/libp2p/instrumentation.d.ts +3 -1
- package/dest/services/libp2p/instrumentation.d.ts.map +1 -1
- package/dest/services/libp2p/instrumentation.js +14 -0
- package/dest/services/libp2p/libp2p_service.d.ts +7 -18
- package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
- package/dest/services/libp2p/libp2p_service.js +52 -74
- package/dest/services/peer-manager/peer_manager.d.ts +1 -1
- package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
- package/dest/services/peer-manager/peer_manager.js +15 -2
- package/dest/services/peer-manager/peer_scoring.d.ts +3 -1
- package/dest/services/peer-manager/peer_scoring.d.ts.map +1 -1
- package/dest/services/peer-manager/peer_scoring.js +4 -0
- package/dest/services/reqresp/config.d.ts +3 -3
- package/dest/services/reqresp/config.d.ts.map +1 -1
- package/dest/services/reqresp/interface.d.ts +14 -9
- package/dest/services/reqresp/interface.d.ts.map +1 -1
- package/dest/services/reqresp/interface.js +10 -11
- package/dest/services/reqresp/metrics.d.ts +1 -1
- package/dest/services/reqresp/metrics.d.ts.map +1 -1
- package/dest/services/reqresp/metrics.js +0 -1
- package/dest/services/reqresp/protocols/index.d.ts +1 -2
- package/dest/services/reqresp/protocols/index.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/index.js +0 -1
- package/dest/services/reqresp/protocols/tx.d.ts +1 -1
- package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/tx.js +1 -3
- package/dest/services/reqresp/rate-limiter/rate_limits.d.ts +1 -1
- package/dest/services/reqresp/rate-limiter/rate_limits.d.ts.map +1 -1
- package/dest/services/reqresp/rate-limiter/rate_limits.js +0 -10
- package/dest/services/reqresp/reqresp.d.ts +4 -2
- package/dest/services/reqresp/reqresp.d.ts.map +1 -1
- package/dest/services/reqresp/reqresp.js +11 -2
- package/dest/test-helpers/make-test-p2p-clients.d.ts +1 -1
- package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
- package/dest/test-helpers/make-test-p2p-clients.js +4 -1
- package/dest/test-helpers/mock-pubsub.d.ts +11 -3
- package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
- package/dest/test-helpers/mock-pubsub.js +36 -11
- package/dest/test-helpers/reqresp-nodes.d.ts +1 -1
- package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
- package/dest/test-helpers/reqresp-nodes.js +5 -3
- package/dest/test-helpers/testbench-utils.d.ts +1 -1
- package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
- package/dest/test-helpers/testbench-utils.js +1 -0
- package/dest/testbench/p2p_client_testbench_worker.d.ts +1 -1
- package/dest/testbench/p2p_client_testbench_worker.d.ts.map +1 -1
- package/dest/testbench/p2p_client_testbench_worker.js +29 -2
- package/dest/testbench/worker_client_manager.d.ts +8 -1
- package/dest/testbench/worker_client_manager.d.ts.map +1 -1
- package/dest/testbench/worker_client_manager.js +49 -0
- package/package.json +14 -14
- package/src/client/factory.ts +24 -19
- package/src/client/p2p_client.ts +11 -3
- package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker.ts +2 -0
- package/src/config.ts +28 -7
- package/src/mem_pools/attestation_pool/attestation_pool.ts +5 -3
- package/src/mem_pools/attestation_pool/mocks.ts +13 -8
- package/src/mem_pools/instrumentation.ts +5 -1
- package/src/mem_pools/tx_pool_v2/eviction/index.ts +1 -0
- package/src/mem_pools/tx_pool_v2/eviction/insufficient_fee_per_gas_eviction_rule.ts +65 -0
- package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.ts +3 -3
- package/src/mem_pools/tx_pool_v2/interfaces.ts +3 -0
- package/src/mem_pools/tx_pool_v2/tx_metadata.ts +13 -7
- package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +2 -0
- package/src/msg_validators/attestation_validator/attestation_validator.ts +38 -7
- package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +12 -2
- package/src/msg_validators/clock_tolerance.ts +75 -0
- package/src/msg_validators/proposal_validator/block_proposal_validator.ts +11 -2
- package/src/msg_validators/proposal_validator/checkpoint_proposal_validator.ts +16 -2
- package/src/msg_validators/proposal_validator/proposal_validator.ts +47 -7
- package/src/msg_validators/tx_validator/README.md +11 -3
- package/src/msg_validators/tx_validator/archive_cache.ts +1 -1
- package/src/msg_validators/tx_validator/factory.ts +3 -1
- package/src/msg_validators/tx_validator/gas_validator.ts +82 -33
- package/src/services/data_store.ts +5 -13
- package/src/services/dummy_service.ts +1 -0
- package/src/services/gossipsub/topic_score_params.ts +36 -4
- package/src/services/libp2p/instrumentation.ts +14 -0
- package/src/services/libp2p/libp2p_service.ts +52 -70
- package/src/services/peer-manager/peer_manager.ts +17 -2
- package/src/services/peer-manager/peer_scoring.ts +6 -0
- package/src/services/reqresp/config.ts +2 -2
- package/src/services/reqresp/interface.ts +21 -11
- package/src/services/reqresp/metrics.ts +0 -1
- package/src/services/reqresp/protocols/index.ts +0 -1
- package/src/services/reqresp/protocols/tx.ts +1 -3
- package/src/services/reqresp/rate-limiter/rate_limits.ts +0 -10
- package/src/services/reqresp/reqresp.ts +18 -1
- package/src/test-helpers/make-test-p2p-clients.ts +2 -0
- package/src/test-helpers/mock-pubsub.ts +34 -5
- package/src/test-helpers/reqresp-nodes.ts +4 -2
- package/src/test-helpers/testbench-utils.ts +1 -0
- package/src/testbench/p2p_client_testbench_worker.ts +30 -0
- package/src/testbench/worker_client_manager.ts +55 -0
- package/dest/services/reqresp/protocols/block.d.ts +0 -9
- package/dest/services/reqresp/protocols/block.d.ts.map +0 -1
- package/dest/services/reqresp/protocols/block.js +0 -32
- package/src/services/reqresp/protocols/block.ts +0 -37
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
2
|
-
import { L2Block } from '@aztec/stdlib/block';
|
|
3
|
-
import { MAX_L2_BLOCK_SIZE_KB } from '@aztec/stdlib/p2p';
|
|
4
1
|
import { TxArray, TxHashArray } from '@aztec/stdlib/tx';
|
|
5
2
|
|
|
6
3
|
import type { PeerId } from '@libp2p/interface';
|
|
@@ -24,7 +21,6 @@ export const PING_PROTOCOL = '/aztec/req/ping/1.0.0';
|
|
|
24
21
|
export const STATUS_PROTOCOL = '/aztec/req/status/1.0.0';
|
|
25
22
|
export const GOODBYE_PROTOCOL = '/aztec/req/goodbye/1.0.0';
|
|
26
23
|
export const TX_REQ_PROTOCOL = '/aztec/req/tx/1.0.0';
|
|
27
|
-
export const BLOCK_REQ_PROTOCOL = '/aztec/req/block/1.0.0';
|
|
28
24
|
export const AUTH_PROTOCOL = '/aztec/req/auth/1.0.0';
|
|
29
25
|
export const BLOCK_TXS_REQ_PROTOCOL = '/aztec/req/block_txs/1.0.0';
|
|
30
26
|
|
|
@@ -33,7 +29,6 @@ export enum ReqRespSubProtocol {
|
|
|
33
29
|
STATUS = STATUS_PROTOCOL,
|
|
34
30
|
GOODBYE = GOODBYE_PROTOCOL,
|
|
35
31
|
TX = TX_REQ_PROTOCOL,
|
|
36
|
-
BLOCK = BLOCK_REQ_PROTOCOL,
|
|
37
32
|
AUTH = AUTH_PROTOCOL,
|
|
38
33
|
BLOCK_TXS = BLOCK_TXS_REQ_PROTOCOL,
|
|
39
34
|
}
|
|
@@ -100,12 +95,29 @@ export type ReqRespSubProtocolValidators = {
|
|
|
100
95
|
[S in ReqRespSubProtocol]: ResponseValidator<any, any>;
|
|
101
96
|
};
|
|
102
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Protocols that are always allowed without authentication, even when p2pAllowOnlyValidators is enabled.
|
|
100
|
+
* These are needed for the handshake and connection management flow.
|
|
101
|
+
* All other protocols require the remote peer to be authenticated.
|
|
102
|
+
*/
|
|
103
|
+
export const UNAUTHENTICATED_ALLOWED_PROTOCOLS: ReadonlySet<ReqRespSubProtocol> = new Set([
|
|
104
|
+
ReqRespSubProtocol.PING,
|
|
105
|
+
ReqRespSubProtocol.STATUS,
|
|
106
|
+
ReqRespSubProtocol.AUTH,
|
|
107
|
+
ReqRespSubProtocol.GOODBYE,
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Callback that checks whether a peer should be rejected from req/resp data protocols.
|
|
112
|
+
* Returns true if the peer should be rejected (i.e. p2pAllowOnlyValidators is on and peer is unauthenticated).
|
|
113
|
+
*/
|
|
114
|
+
export type ShouldRejectPeer = (peerId: string) => boolean;
|
|
115
|
+
|
|
103
116
|
export const DEFAULT_SUB_PROTOCOL_VALIDATORS: ReqRespSubProtocolValidators = {
|
|
104
117
|
[ReqRespSubProtocol.PING]: noopValidator,
|
|
105
118
|
[ReqRespSubProtocol.STATUS]: noopValidator,
|
|
106
119
|
[ReqRespSubProtocol.TX]: noopValidator,
|
|
107
120
|
[ReqRespSubProtocol.GOODBYE]: noopValidator,
|
|
108
|
-
[ReqRespSubProtocol.BLOCK]: noopValidator,
|
|
109
121
|
[ReqRespSubProtocol.AUTH]: noopValidator,
|
|
110
122
|
[ReqRespSubProtocol.BLOCK_TXS]: noopValidator,
|
|
111
123
|
};
|
|
@@ -203,10 +215,6 @@ export const subProtocolMap = {
|
|
|
203
215
|
request: RequestableBuffer,
|
|
204
216
|
response: RequestableBuffer,
|
|
205
217
|
},
|
|
206
|
-
[ReqRespSubProtocol.BLOCK]: {
|
|
207
|
-
request: Fr, // block number
|
|
208
|
-
response: L2Block,
|
|
209
|
-
},
|
|
210
218
|
[ReqRespSubProtocol.AUTH]: {
|
|
211
219
|
request: AuthRequest,
|
|
212
220
|
response: AuthResponse,
|
|
@@ -229,7 +237,6 @@ export type ExpectedResponseSizeCalculator = (requestBuffer: Buffer) => number;
|
|
|
229
237
|
export const subProtocolSizeCalculators: Record<ReqRespSubProtocol, ExpectedResponseSizeCalculator> = {
|
|
230
238
|
[ReqRespSubProtocol.TX]: calculateTxResponseSize,
|
|
231
239
|
[ReqRespSubProtocol.BLOCK_TXS]: calculateBlockTxsResponseSize,
|
|
232
|
-
[ReqRespSubProtocol.BLOCK]: () => MAX_L2_BLOCK_SIZE_KB,
|
|
233
240
|
[ReqRespSubProtocol.STATUS]: () => 1,
|
|
234
241
|
[ReqRespSubProtocol.PING]: () => 1,
|
|
235
242
|
[ReqRespSubProtocol.AUTH]: () => 1,
|
|
@@ -264,5 +271,8 @@ export interface ReqRespInterface {
|
|
|
264
271
|
|
|
265
272
|
updateConfig(config: Partial<P2PReqRespConfig>): void;
|
|
266
273
|
|
|
274
|
+
/** Sets the callback used to reject unauthenticated peers on gated req/resp protocols. */
|
|
275
|
+
setShouldRejectPeer(checker: ShouldRejectPeer): void;
|
|
276
|
+
|
|
267
277
|
getConnectionSampler(): Pick<ConnectionSampler, 'getPeerListSortedByConnectionCountAsc'>;
|
|
268
278
|
}
|
|
@@ -51,9 +51,7 @@ export function reqRespTxHandler(mempools: MemPools): ReqRespSubProtocolHandler
|
|
|
51
51
|
* Per: https://github.com/AztecProtocol/aztec-packages/issues/15149#issuecomment-2999054485
|
|
52
52
|
* we define Q as max number of transactions per batch, the comment explains why we use 8.
|
|
53
53
|
*/
|
|
54
|
-
|
|
55
|
-
//more info: https://github.com/AztecProtocol/aztec-packages/pull/15516#pullrequestreview-2995474321
|
|
56
|
-
export function chunkTxHashesRequest(hashes: TxHash[], chunkSize = 1): Array<TxHashArray> {
|
|
54
|
+
export function chunkTxHashesRequest(hashes: TxHash[], chunkSize = 8): Array<TxHashArray> {
|
|
57
55
|
return chunk(hashes, chunkSize).map(chunk => new TxHashArray(...chunk));
|
|
58
56
|
}
|
|
59
57
|
|
|
@@ -42,16 +42,6 @@ export const DEFAULT_RATE_LIMITS: ReqRespSubProtocolRateLimits = {
|
|
|
42
42
|
quotaCount: 200,
|
|
43
43
|
},
|
|
44
44
|
},
|
|
45
|
-
[ReqRespSubProtocol.BLOCK]: {
|
|
46
|
-
peerLimit: {
|
|
47
|
-
quotaTimeMs: 1000,
|
|
48
|
-
quotaCount: 2,
|
|
49
|
-
},
|
|
50
|
-
globalLimit: {
|
|
51
|
-
quotaTimeMs: 1000,
|
|
52
|
-
quotaCount: 5,
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
45
|
[ReqRespSubProtocol.GOODBYE]: {
|
|
56
46
|
peerLimit: {
|
|
57
47
|
quotaTimeMs: 1000,
|
|
@@ -34,7 +34,9 @@ import {
|
|
|
34
34
|
type ReqRespSubProtocolHandlers,
|
|
35
35
|
type ReqRespSubProtocolRateLimits,
|
|
36
36
|
type ReqRespSubProtocolValidators,
|
|
37
|
+
type ShouldRejectPeer,
|
|
37
38
|
type SubProtocolMap,
|
|
39
|
+
UNAUTHENTICATED_ALLOWED_PROTOCOLS,
|
|
38
40
|
responseFromBuffer,
|
|
39
41
|
subProtocolSizeCalculators,
|
|
40
42
|
} from './interface.js';
|
|
@@ -72,6 +74,8 @@ export class ReqResp implements ReqRespInterface {
|
|
|
72
74
|
|
|
73
75
|
private snappyTransform: SnappyTransform;
|
|
74
76
|
|
|
77
|
+
private shouldRejectPeer: ShouldRejectPeer | undefined;
|
|
78
|
+
|
|
75
79
|
private metrics: ReqRespMetrics;
|
|
76
80
|
|
|
77
81
|
constructor(
|
|
@@ -108,6 +112,10 @@ export class ReqResp implements ReqRespInterface {
|
|
|
108
112
|
}
|
|
109
113
|
}
|
|
110
114
|
|
|
115
|
+
public setShouldRejectPeer(checker: ShouldRejectPeer): void {
|
|
116
|
+
this.shouldRejectPeer = checker;
|
|
117
|
+
}
|
|
118
|
+
|
|
111
119
|
get tracer() {
|
|
112
120
|
return this.metrics.tracer;
|
|
113
121
|
}
|
|
@@ -462,7 +470,7 @@ export class ReqResp implements ReqRespInterface {
|
|
|
462
470
|
);
|
|
463
471
|
return resp;
|
|
464
472
|
} catch (e: any) {
|
|
465
|
-
this.logger.
|
|
473
|
+
this.logger.debug(`SUBPROTOCOL: ${subProtocol}\n`, e);
|
|
466
474
|
// On error we immediately abort the stream, this is preferred way,
|
|
467
475
|
// because it signals to the sender that error happened, whereas
|
|
468
476
|
// closing the stream only closes our side and is much slower
|
|
@@ -596,6 +604,15 @@ export class ReqResp implements ReqRespInterface {
|
|
|
596
604
|
throw new ReqRespStatusError(ReqRespStatus.RATE_LIMIT_EXCEEDED);
|
|
597
605
|
}
|
|
598
606
|
|
|
607
|
+
// When p2pAllowOnlyValidators is enabled, reject unauthenticated peers on data protocols
|
|
608
|
+
if (
|
|
609
|
+
!UNAUTHENTICATED_ALLOWED_PROTOCOLS.has(protocol) &&
|
|
610
|
+
(this.shouldRejectPeer?.(connection.remotePeer.toString()) ?? false)
|
|
611
|
+
) {
|
|
612
|
+
this.logger.debug(`Rejecting unauthenticated peer ${connection.remotePeer} on gated protocol ${protocol}`);
|
|
613
|
+
throw new ReqRespStatusError(ReqRespStatus.FAILURE);
|
|
614
|
+
}
|
|
615
|
+
|
|
599
616
|
await this.processStream(protocol, incomingStream);
|
|
600
617
|
} catch (err: any) {
|
|
601
618
|
this.metrics.recordResponseError(protocol);
|
|
@@ -5,6 +5,7 @@ import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
|
5
5
|
import { retryUntil } from '@aztec/foundation/retry';
|
|
6
6
|
import { sleep } from '@aztec/foundation/sleep';
|
|
7
7
|
import { openTmpStore } from '@aztec/kv-store/lmdb-v2';
|
|
8
|
+
import { GasFees } from '@aztec/stdlib/gas';
|
|
8
9
|
import type { WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
9
10
|
import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
|
|
10
11
|
|
|
@@ -102,6 +103,7 @@ export async function makeTestP2PClient(
|
|
|
102
103
|
proofVerifier,
|
|
103
104
|
mockWorldState,
|
|
104
105
|
mockEpochCache,
|
|
106
|
+
{ getCurrentMinFees: () => Promise.resolve(GasFees.empty()) },
|
|
105
107
|
'test-p2p-client',
|
|
106
108
|
undefined,
|
|
107
109
|
undefined,
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { EpochCacheInterface } from '@aztec/epoch-cache';
|
|
2
2
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
3
|
+
import { sleep } from '@aztec/foundation/sleep';
|
|
3
4
|
import type { AztecAsyncKVStore } from '@aztec/kv-store';
|
|
4
5
|
import type { L2BlockSource } from '@aztec/stdlib/block';
|
|
5
6
|
import type { ContractDataSource } from '@aztec/stdlib/contract';
|
|
7
|
+
import type { BlockMinFeesProvider } from '@aztec/stdlib/gas';
|
|
6
8
|
import type { ClientProtocolCircuitVerifier, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
7
9
|
import type { TelemetryClient } from '@aztec/telemetry-client';
|
|
8
10
|
|
|
@@ -55,6 +57,7 @@ export function getMockPubSubP2PServiceFactory(
|
|
|
55
57
|
proofVerifier: ClientProtocolCircuitVerifier;
|
|
56
58
|
worldStateSynchronizer: WorldStateSynchronizer;
|
|
57
59
|
peerStore: AztecAsyncKVStore;
|
|
60
|
+
blockMinFeesProvider: BlockMinFeesProvider;
|
|
58
61
|
telemetry: TelemetryClient;
|
|
59
62
|
logger: Logger;
|
|
60
63
|
},
|
|
@@ -75,6 +78,7 @@ export function getMockPubSubP2PServiceFactory(
|
|
|
75
78
|
deps.epochCache,
|
|
76
79
|
deps.proofVerifier,
|
|
77
80
|
deps.worldStateSynchronizer,
|
|
81
|
+
deps.blockMinFeesProvider,
|
|
78
82
|
deps.telemetry,
|
|
79
83
|
deps.logger,
|
|
80
84
|
);
|
|
@@ -100,6 +104,7 @@ class MockReqResp implements ReqRespInterface {
|
|
|
100
104
|
}
|
|
101
105
|
|
|
102
106
|
updateConfig(_config: Partial<P2PReqRespConfig>): void {}
|
|
107
|
+
setShouldRejectPeer(): void {}
|
|
103
108
|
|
|
104
109
|
start(
|
|
105
110
|
subProtocolHandlers: Partial<ReqRespSubProtocolHandlers>,
|
|
@@ -138,6 +143,11 @@ class MockReqResp implements ReqRespInterface {
|
|
|
138
143
|
const responses: InstanceType<SubProtocolMap[SubProtocol]['response']>[] = [];
|
|
139
144
|
const peers = this.network.getReqRespPeers().filter(p => !p.peerId.equals(this.peerId));
|
|
140
145
|
const targetPeers = pinnedPeer ? peers.filter(p => p.peerId.equals(pinnedPeer)) : peers;
|
|
146
|
+
const delayMs = this.network.getPropagationDelayMs();
|
|
147
|
+
|
|
148
|
+
if (delayMs > 0) {
|
|
149
|
+
await sleep(delayMs);
|
|
150
|
+
}
|
|
141
151
|
|
|
142
152
|
for (const request of requests) {
|
|
143
153
|
const requestBuffer = request.toBuffer();
|
|
@@ -174,7 +184,12 @@ class MockReqResp implements ReqRespInterface {
|
|
|
174
184
|
return { status: ReqRespStatus.SUCCESS, data: Buffer.from([]) };
|
|
175
185
|
}
|
|
176
186
|
try {
|
|
187
|
+
const delayMs = this.network.getPropagationDelayMs();
|
|
188
|
+
if (delayMs > 0) {
|
|
189
|
+
await sleep(delayMs);
|
|
190
|
+
}
|
|
177
191
|
const data = await handler(this.peerId, payload);
|
|
192
|
+
|
|
178
193
|
return { status: ReqRespStatus.SUCCESS, data };
|
|
179
194
|
} catch {
|
|
180
195
|
return { status: ReqRespStatus.FAILURE };
|
|
@@ -242,10 +257,10 @@ class MockGossipSubService extends TypedEventEmitter<GossipsubEvents> implements
|
|
|
242
257
|
score: (_peerId: PeerIdStr) => 0,
|
|
243
258
|
};
|
|
244
259
|
|
|
245
|
-
publish(topic: TopicStr, data: Uint8Array, _opts?: PublishOpts): Promise<PublishResult> {
|
|
260
|
+
async publish(topic: TopicStr, data: Uint8Array, _opts?: PublishOpts): Promise<PublishResult> {
|
|
246
261
|
this.logger.debug(`Publishing message on topic ${topic}`, { topic, sender: this.peerId.toString() });
|
|
247
|
-
this.network.publishToPeers(topic, data, this.peerId);
|
|
248
|
-
return
|
|
262
|
+
await this.network.publishToPeers(topic, data, this.peerId);
|
|
263
|
+
return { recipients: this.network.getPeers().filter(peer => !this.peerId.equals(peer)) };
|
|
249
264
|
}
|
|
250
265
|
|
|
251
266
|
receive(msg: GossipsubMessage) {
|
|
@@ -281,7 +296,8 @@ class MockGossipSubService extends TypedEventEmitter<GossipsubEvents> implements
|
|
|
281
296
|
|
|
282
297
|
/**
|
|
283
298
|
* Mock gossip sub network used for testing.
|
|
284
|
-
* All instances of MockGossipSubService connected to the same network
|
|
299
|
+
* All instances of MockGossipSubService connected to the same network receive the same messages,
|
|
300
|
+
* optionally delayed by a configurable propagation time.
|
|
285
301
|
*/
|
|
286
302
|
export class MockGossipSubNetwork {
|
|
287
303
|
private peers: MockGossipSubService[] = [];
|
|
@@ -290,6 +306,15 @@ export class MockGossipSubNetwork {
|
|
|
290
306
|
|
|
291
307
|
private logger = createLogger('p2p:test:mock-gossipsub-network');
|
|
292
308
|
|
|
309
|
+
constructor(
|
|
310
|
+
/** Artificial propagation delay in milliseconds applied to each message delivery. */
|
|
311
|
+
private propagationDelayMs: number = 0,
|
|
312
|
+
) {}
|
|
313
|
+
|
|
314
|
+
public getPropagationDelayMs(): number {
|
|
315
|
+
return this.propagationDelayMs;
|
|
316
|
+
}
|
|
317
|
+
|
|
293
318
|
public getPeers(): PeerId[] {
|
|
294
319
|
return this.peers.map(peer => peer.peerId);
|
|
295
320
|
}
|
|
@@ -306,7 +331,7 @@ export class MockGossipSubNetwork {
|
|
|
306
331
|
return this.reqRespPeers;
|
|
307
332
|
}
|
|
308
333
|
|
|
309
|
-
public publishToPeers(topic: TopicStr, data: Uint8Array, sender: PeerId): void {
|
|
334
|
+
public async publishToPeers(topic: TopicStr, data: Uint8Array, sender: PeerId): Promise<void> {
|
|
310
335
|
const msgId = (this.nextMsgId++).toString();
|
|
311
336
|
this.logger.debug(`Network is distributing message on topic ${topic}`, {
|
|
312
337
|
topic,
|
|
@@ -315,6 +340,10 @@ export class MockGossipSubNetwork {
|
|
|
315
340
|
msgId,
|
|
316
341
|
});
|
|
317
342
|
|
|
343
|
+
if (this.propagationDelayMs > 0) {
|
|
344
|
+
await sleep(this.propagationDelayMs);
|
|
345
|
+
}
|
|
346
|
+
|
|
318
347
|
const gossipSubMsg: GossipsubMessage = { msgId, msg: { type: 'unsigned', topic, data }, propagationSource: sender };
|
|
319
348
|
for (const peer of this.peers) {
|
|
320
349
|
if (peer.subscribedTopics.has(topic)) {
|
|
@@ -6,6 +6,7 @@ import { openTmpStore } from '@aztec/kv-store/lmdb-v2';
|
|
|
6
6
|
import type { L2BlockSource } from '@aztec/stdlib/block';
|
|
7
7
|
import { type ChainConfig, emptyChainConfig } from '@aztec/stdlib/config';
|
|
8
8
|
import type { ContractDataSource } from '@aztec/stdlib/contract';
|
|
9
|
+
import { GasFees } from '@aztec/stdlib/gas';
|
|
9
10
|
import type {
|
|
10
11
|
ClientProtocolCircuitVerifier,
|
|
11
12
|
IVCProofVerificationResult,
|
|
@@ -153,6 +154,8 @@ export async function createTestLibP2PService(
|
|
|
153
154
|
epochCache,
|
|
154
155
|
);
|
|
155
156
|
|
|
157
|
+
reqresp.setShouldRejectPeer(peerId => peerManager.shouldDisableP2PGossip(peerId));
|
|
158
|
+
|
|
156
159
|
p2pNode.services.pubsub.score.params.appSpecificWeight = APP_SPECIFIC_WEIGHT;
|
|
157
160
|
p2pNode.services.pubsub.score.params.appSpecificScore = (peerId: string) =>
|
|
158
161
|
peerManager.shouldDisableP2PGossip(peerId) ? -Infinity : peerManager.getPeerScore(peerId);
|
|
@@ -168,6 +171,7 @@ export async function createTestLibP2PService(
|
|
|
168
171
|
epochCache,
|
|
169
172
|
proofVerifier,
|
|
170
173
|
worldStateSynchronizer,
|
|
174
|
+
{ getCurrentMinFees: () => Promise.resolve(GasFees.empty()) },
|
|
171
175
|
telemetry,
|
|
172
176
|
);
|
|
173
177
|
}
|
|
@@ -187,7 +191,6 @@ export const MOCK_SUB_PROTOCOL_HANDLERS: ReqRespSubProtocolHandlers = {
|
|
|
187
191
|
[ReqRespSubProtocol.STATUS]: (_msg: any) => Promise.resolve(Buffer.from('status')),
|
|
188
192
|
[ReqRespSubProtocol.TX]: (_msg: any) => Promise.resolve(Buffer.from('tx')),
|
|
189
193
|
[ReqRespSubProtocol.GOODBYE]: (_msg: any) => Promise.resolve(Buffer.from('goodbye')),
|
|
190
|
-
[ReqRespSubProtocol.BLOCK]: (_msg: any) => Promise.resolve(Buffer.from('block')),
|
|
191
194
|
[ReqRespSubProtocol.AUTH]: (_msg: any) => Promise.resolve(Buffer.from('auth')),
|
|
192
195
|
[ReqRespSubProtocol.BLOCK_TXS]: (_msg: any) => Promise.resolve(Buffer.from('block_txs')),
|
|
193
196
|
};
|
|
@@ -199,7 +202,6 @@ export const MOCK_SUB_PROTOCOL_VALIDATORS: ReqRespSubProtocolValidators = {
|
|
|
199
202
|
[ReqRespSubProtocol.STATUS]: noopValidator,
|
|
200
203
|
[ReqRespSubProtocol.TX]: noopValidator,
|
|
201
204
|
[ReqRespSubProtocol.GOODBYE]: noopValidator,
|
|
202
|
-
[ReqRespSubProtocol.BLOCK]: noopValidator,
|
|
203
205
|
[ReqRespSubProtocol.AUTH]: noopValidator,
|
|
204
206
|
[ReqRespSubProtocol.BLOCK_TXS]: noopValidator,
|
|
205
207
|
};
|
|
@@ -287,6 +287,7 @@ export function createMockEpochCache(): EpochCacheInterface {
|
|
|
287
287
|
nowMs: 0n,
|
|
288
288
|
}),
|
|
289
289
|
isProposerPipeliningEnabled: () => false,
|
|
290
|
+
pipeliningOffset: () => 0,
|
|
290
291
|
computeProposerIndex: () => 0n,
|
|
291
292
|
getCurrentAndNextSlot: () => ({ currentSlot: SlotNumber.ZERO, nextSlot: SlotNumber.ZERO }),
|
|
292
293
|
getTargetAndNextSlot: () => ({ targetSlot: SlotNumber.ZERO, nextSlot: SlotNumber.ZERO }),
|
|
@@ -17,6 +17,7 @@ import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
|
|
|
17
17
|
import { protocolContractsHash } from '@aztec/protocol-contracts';
|
|
18
18
|
import type { L2BlockSource } from '@aztec/stdlib/block';
|
|
19
19
|
import type { ContractDataSource } from '@aztec/stdlib/contract';
|
|
20
|
+
import { GasFees } from '@aztec/stdlib/gas';
|
|
20
21
|
import type { ClientProtocolCircuitVerifier, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
21
22
|
import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
|
|
22
23
|
import { type BlockProposal, P2PMessage } from '@aztec/stdlib/p2p';
|
|
@@ -116,6 +117,7 @@ class TestLibP2PService extends LibP2PService {
|
|
|
116
117
|
epochCache,
|
|
117
118
|
proofVerifier,
|
|
118
119
|
worldStateSynchronizer,
|
|
120
|
+
{ getCurrentMinFees: () => Promise.resolve(GasFees.empty()) },
|
|
119
121
|
telemetry,
|
|
120
122
|
logger,
|
|
121
123
|
);
|
|
@@ -204,6 +206,25 @@ function installUnlimitedRateLimits(client: P2PClient): void {
|
|
|
204
206
|
rateLimiter.allow = () => RateLimitStatus.Allowed;
|
|
205
207
|
}
|
|
206
208
|
|
|
209
|
+
/** Resets peer scores to prevent cross-case contamination in benchmarks. */
|
|
210
|
+
function resetPeerScores(client: P2PClient): void {
|
|
211
|
+
const peerManager = (client as any).p2pService.peerManager;
|
|
212
|
+
const peerScoring = peerManager?.peerScoring;
|
|
213
|
+
if (peerScoring?.resetAllScores) {
|
|
214
|
+
peerScoring.resetAllScores();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Returns the number of connected peers for connectivity checks. */
|
|
219
|
+
function getConnectedPeerCount(client: P2PClient): number {
|
|
220
|
+
const p2pService = (client as any).p2pService;
|
|
221
|
+
const connectionSampler = p2pService?.reqresp?.getConnectionSampler?.();
|
|
222
|
+
if (connectionSampler?.getPeerListSortedByConnectionCountAsc) {
|
|
223
|
+
return connectionSampler.getPeerListSortedByConnectionCountAsc().length;
|
|
224
|
+
}
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
|
|
207
228
|
async function runAggregatorBenchmark(
|
|
208
229
|
client: P2PClient,
|
|
209
230
|
blockProposal: BlockProposal,
|
|
@@ -398,6 +419,7 @@ process.on('message', async msg => {
|
|
|
398
419
|
proofVerifier as ClientProtocolCircuitVerifier,
|
|
399
420
|
worldState,
|
|
400
421
|
epochCache,
|
|
422
|
+
{ getCurrentMinFees: () => Promise.resolve(GasFees.empty()) },
|
|
401
423
|
'test-p2p-bench-worker',
|
|
402
424
|
undefined,
|
|
403
425
|
telemetry as TelemetryClient,
|
|
@@ -452,6 +474,13 @@ process.on('message', async msg => {
|
|
|
452
474
|
}
|
|
453
475
|
break;
|
|
454
476
|
|
|
477
|
+
case 'GET_PEER_COUNT':
|
|
478
|
+
process.send!({
|
|
479
|
+
type: 'PEER_COUNT',
|
|
480
|
+
count: workerClient ? getConnectedPeerCount(workerClient) : 0,
|
|
481
|
+
});
|
|
482
|
+
break;
|
|
483
|
+
|
|
455
484
|
case 'BENCH_REQRESP': {
|
|
456
485
|
const benchCmd = cmd as BenchReqRespCommand;
|
|
457
486
|
if (!workerClient || !workerTxPool || !workerAttestationPool || !workerConfig || !workerLogger) {
|
|
@@ -468,6 +497,7 @@ process.on('message', async msg => {
|
|
|
468
497
|
// Reset state before each benchmark run to avoid cross-run contamination
|
|
469
498
|
workerTxPool.resetState();
|
|
470
499
|
workerAttestationPool.resetState();
|
|
500
|
+
resetPeerScores(workerClient);
|
|
471
501
|
|
|
472
502
|
installUnlimitedRateLimits(workerClient);
|
|
473
503
|
|
|
@@ -424,6 +424,61 @@ class WorkerClientManager {
|
|
|
424
424
|
this.logger.info('All worker processes cleaned up');
|
|
425
425
|
}
|
|
426
426
|
|
|
427
|
+
/**
|
|
428
|
+
* Checks that the aggregator (client 0) has sufficient peer connections before running a benchmark.
|
|
429
|
+
* This prevents benchmark cases from starting with degraded connectivity after a previous case
|
|
430
|
+
* caused connection failures.
|
|
431
|
+
*/
|
|
432
|
+
async waitForConnectivity(minPeers: number, timeoutMs: number = 15_000): Promise<number> {
|
|
433
|
+
const waitInterval = 1000;
|
|
434
|
+
let waited = 0;
|
|
435
|
+
|
|
436
|
+
while (waited < timeoutMs) {
|
|
437
|
+
const count = await this.getPeerCount(0, 5000);
|
|
438
|
+
if (count >= minPeers) {
|
|
439
|
+
this.logger.info(`Connectivity check passed: ${count}/${minPeers} peers connected`);
|
|
440
|
+
return count;
|
|
441
|
+
}
|
|
442
|
+
this.logger.debug(`Waiting for connectivity: ${count}/${minPeers} (waited ${waited}ms)`);
|
|
443
|
+
await sleep(waitInterval);
|
|
444
|
+
waited += waitInterval;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const finalCount = await this.getPeerCount(0, 5000);
|
|
448
|
+
this.logger.warn(`Connectivity check: only ${finalCount}/${minPeers} peers after ${timeoutMs}ms`);
|
|
449
|
+
return finalCount;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
private getPeerCount(clientIndex: number, timeoutMs: number): Promise<number> {
|
|
453
|
+
return new Promise<number>(resolve => {
|
|
454
|
+
let resolved = false;
|
|
455
|
+
|
|
456
|
+
const handler = (msg: any) => {
|
|
457
|
+
if (resolved) {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
if (msg.type === 'PEER_COUNT') {
|
|
461
|
+
resolved = true;
|
|
462
|
+
clearTimeout(timeout);
|
|
463
|
+
this.processes[clientIndex].off('message', handler);
|
|
464
|
+
resolve(msg.count as number);
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
const timeout = setTimeout(() => {
|
|
469
|
+
if (resolved) {
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
resolved = true;
|
|
473
|
+
this.processes[clientIndex].off('message', handler);
|
|
474
|
+
resolve(0);
|
|
475
|
+
}, timeoutMs);
|
|
476
|
+
|
|
477
|
+
this.processes[clientIndex].on('message', handler);
|
|
478
|
+
this.processes[clientIndex].send({ type: 'GET_PEER_COUNT' });
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
|
|
427
482
|
/**
|
|
428
483
|
* Run a req/resp benchmark across all worker clients.
|
|
429
484
|
*
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { L2BlockSource } from '@aztec/stdlib/block';
|
|
2
|
-
import type { ReqRespSubProtocolHandler } from '../interface.js';
|
|
3
|
-
/**
|
|
4
|
-
* Handler for L2 Block requests
|
|
5
|
-
* @param l2BlockSource - source for L2 blocks
|
|
6
|
-
* @returns the Block request handler
|
|
7
|
-
* */
|
|
8
|
-
export declare function reqRespBlockHandler(l2BlockSource: L2BlockSource): ReqRespSubProtocolHandler;
|
|
9
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmxvY2suZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9zZXJ2aWNlcy9yZXFyZXNwL3Byb3RvY29scy9ibG9jay50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFFQSxPQUFPLEtBQUssRUFBRSxhQUFhLEVBQUUsTUFBTSxxQkFBcUIsQ0FBQztBQUl6RCxPQUFPLEtBQUssRUFBRSx5QkFBeUIsRUFBRSxNQUFNLGlCQUFpQixDQUFDO0FBR2pFOzs7O0tBSUs7QUFDTCx3QkFBZ0IsbUJBQW1CLENBQUMsYUFBYSxFQUFFLGFBQWEsR0FBRyx5QkFBeUIsQ0FzQjNGIn0=
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"block.d.ts","sourceRoot":"","sources":["../../../../src/services/reqresp/protocols/block.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAIzD,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AAGjE;;;;KAIK;AACL,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,aAAa,GAAG,yBAAyB,CAsB3F"}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
|
-
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
|
-
import { ReqRespStatus, ReqRespStatusError } from '../status.js';
|
|
4
|
-
/**
|
|
5
|
-
* Handler for L2 Block requests
|
|
6
|
-
* @param l2BlockSource - source for L2 blocks
|
|
7
|
-
* @returns the Block request handler
|
|
8
|
-
* */ export function reqRespBlockHandler(l2BlockSource) {
|
|
9
|
-
/**
|
|
10
|
-
* @param peerId - the peer ID of the requester
|
|
11
|
-
* @param msg - the block request message, which is expected to contain valid block number as a Buffer
|
|
12
|
-
* @returns a Buffer containing the requested block data, or an empty Buffer if the block is not found
|
|
13
|
-
* @throws ReqRespStatusError if the input msg is not a valid block number
|
|
14
|
-
* */ return async (_peerId, msg)=>{
|
|
15
|
-
let blockNumber;
|
|
16
|
-
try {
|
|
17
|
-
blockNumber = Fr.fromBuffer(msg);
|
|
18
|
-
} catch (err) {
|
|
19
|
-
throw new ReqRespStatusError(ReqRespStatus.BADLY_FORMED_REQUEST, {
|
|
20
|
-
cause: err
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
try {
|
|
24
|
-
const foundBlock = await l2BlockSource.getBlock(BlockNumber(Number(blockNumber)));
|
|
25
|
-
return foundBlock ? foundBlock.toBuffer() : Buffer.alloc(0);
|
|
26
|
-
} catch (err) {
|
|
27
|
-
throw new ReqRespStatusError(ReqRespStatus.INTERNAL_ERROR, {
|
|
28
|
-
cause: err
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
|
-
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
|
-
import type { L2BlockSource } from '@aztec/stdlib/block';
|
|
4
|
-
|
|
5
|
-
import type { PeerId } from '@libp2p/interface';
|
|
6
|
-
|
|
7
|
-
import type { ReqRespSubProtocolHandler } from '../interface.js';
|
|
8
|
-
import { ReqRespStatus, ReqRespStatusError } from '../status.js';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Handler for L2 Block requests
|
|
12
|
-
* @param l2BlockSource - source for L2 blocks
|
|
13
|
-
* @returns the Block request handler
|
|
14
|
-
* */
|
|
15
|
-
export function reqRespBlockHandler(l2BlockSource: L2BlockSource): ReqRespSubProtocolHandler {
|
|
16
|
-
/**
|
|
17
|
-
* @param peerId - the peer ID of the requester
|
|
18
|
-
* @param msg - the block request message, which is expected to contain valid block number as a Buffer
|
|
19
|
-
* @returns a Buffer containing the requested block data, or an empty Buffer if the block is not found
|
|
20
|
-
* @throws ReqRespStatusError if the input msg is not a valid block number
|
|
21
|
-
* */
|
|
22
|
-
return async (_peerId: PeerId, msg: Buffer) => {
|
|
23
|
-
let blockNumber: Fr;
|
|
24
|
-
try {
|
|
25
|
-
blockNumber = Fr.fromBuffer(msg);
|
|
26
|
-
} catch (err: any) {
|
|
27
|
-
throw new ReqRespStatusError(ReqRespStatus.BADLY_FORMED_REQUEST, { cause: err });
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
try {
|
|
31
|
-
const foundBlock = await l2BlockSource.getBlock(BlockNumber(Number(blockNumber)));
|
|
32
|
-
return foundBlock ? foundBlock.toBuffer() : Buffer.alloc(0);
|
|
33
|
-
} catch (err: any) {
|
|
34
|
-
throw new ReqRespStatusError(ReqRespStatus.INTERNAL_ERROR, { cause: err });
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
}
|