@aztec/p2p 1.2.1 → 2.0.0-nightly.20250813
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 +5 -1
- package/dest/client/factory.d.ts.map +1 -1
- package/dest/client/factory.js +29 -12
- package/dest/client/interface.d.ts +8 -13
- package/dest/client/interface.d.ts.map +1 -1
- package/dest/client/p2p_client.d.ts +18 -22
- package/dest/client/p2p_client.d.ts.map +1 -1
- package/dest/client/p2p_client.js +86 -83
- package/dest/config.d.ts +30 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +34 -1
- package/dest/index.d.ts +1 -0
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +13 -1
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +117 -10
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +4 -1
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +22 -1
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +4 -1
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +21 -1
- package/dest/mem_pools/attestation_pool/mocks.d.ts +1 -2
- package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/mocks.js +2 -10
- package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts +8 -3
- package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.js +64 -37
- package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts +8 -3
- package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/memory_tx_pool.js +18 -10
- package/dest/mem_pools/tx_pool/tx_pool.d.ts +11 -2
- package/dest/mem_pools/tx_pool/tx_pool.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/tx_pool_test_suite.js +73 -44
- package/dest/msg_validators/attestation_validator/attestation_validator.js +1 -1
- package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/block_header_validator.js +2 -2
- package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/data_validator.js +35 -59
- package/dest/msg_validators/tx_validator/double_spend_validator.js +2 -2
- package/dest/msg_validators/tx_validator/gas_validator.js +4 -4
- package/dest/msg_validators/tx_validator/metadata_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/metadata_validator.js +21 -21
- package/dest/msg_validators/tx_validator/phases_validator.js +3 -3
- package/dest/msg_validators/tx_validator/tx_proof_validator.js +3 -3
- package/dest/services/discv5/discV5_service.d.ts.map +1 -1
- package/dest/services/discv5/discV5_service.js +4 -1
- package/dest/services/dummy_service.d.ts +13 -3
- package/dest/services/dummy_service.d.ts.map +1 -1
- package/dest/services/dummy_service.js +26 -3
- package/dest/services/index.d.ts +3 -1
- package/dest/services/index.d.ts.map +1 -1
- package/dest/services/index.js +3 -1
- package/dest/services/libp2p/libp2p_service.d.ts +13 -20
- package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
- package/dest/services/libp2p/libp2p_service.js +123 -46
- package/dest/services/peer-manager/interface.d.ts +8 -1
- package/dest/services/peer-manager/interface.d.ts.map +1 -1
- package/dest/services/peer-manager/peer_manager.d.ts +70 -3
- package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
- package/dest/services/peer-manager/peer_manager.js +369 -39
- package/dest/services/reqresp/config.d.ts +3 -3
- package/dest/services/reqresp/config.d.ts.map +1 -1
- package/dest/services/reqresp/config.js +3 -3
- package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts +3 -4
- package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts.map +1 -1
- package/dest/services/reqresp/connection-sampler/connection_sampler.js +35 -52
- package/dest/services/reqresp/index.d.ts +2 -1
- package/dest/services/reqresp/index.d.ts.map +1 -1
- package/dest/services/reqresp/index.js +2 -1
- package/dest/services/reqresp/interface.d.ts +61 -10
- package/dest/services/reqresp/interface.d.ts.map +1 -1
- package/dest/services/reqresp/interface.js +41 -6
- package/dest/services/reqresp/protocols/auth.d.ts +43 -0
- package/dest/services/reqresp/protocols/auth.d.ts.map +1 -0
- package/dest/services/reqresp/protocols/auth.js +71 -0
- package/dest/services/reqresp/protocols/block.d.ts +5 -0
- package/dest/services/reqresp/protocols/block.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/block.js +28 -5
- package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts +30 -0
- package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts.map +1 -0
- package/dest/services/reqresp/protocols/block_txs/bitvector.js +75 -0
- package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +11 -0
- package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -0
- package/dest/services/reqresp/protocols/block_txs/block_txs_handler.js +39 -0
- package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +49 -0
- package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -0
- package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +75 -0
- package/dest/services/reqresp/protocols/block_txs/index.d.ts +4 -0
- package/dest/services/reqresp/protocols/block_txs/index.d.ts.map +1 -0
- package/dest/services/reqresp/protocols/block_txs/index.js +3 -0
- package/dest/services/reqresp/protocols/goodbye.js +3 -5
- package/dest/services/reqresp/protocols/index.d.ts +2 -0
- package/dest/services/reqresp/protocols/index.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/index.js +2 -0
- package/dest/services/reqresp/protocols/status.d.ts +2 -0
- package/dest/services/reqresp/protocols/status.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/status.js +7 -0
- package/dest/services/reqresp/protocols/tx.d.ts +12 -1
- package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/tx.js +34 -6
- package/dest/services/reqresp/rate-limiter/rate_limits.d.ts.map +1 -1
- package/dest/services/reqresp/rate-limiter/rate_limits.js +20 -0
- package/dest/services/reqresp/reqresp.d.ts +37 -39
- package/dest/services/reqresp/reqresp.d.ts.map +1 -1
- package/dest/services/reqresp/reqresp.js +220 -220
- package/dest/services/reqresp/status.d.ts +8 -3
- package/dest/services/reqresp/status.d.ts.map +1 -1
- package/dest/services/reqresp/status.js +6 -2
- package/dest/services/service.d.ts +10 -10
- package/dest/services/service.d.ts.map +1 -1
- package/dest/services/tx_collection/config.d.ts +25 -0
- package/dest/services/tx_collection/config.d.ts.map +1 -0
- package/dest/services/tx_collection/config.js +58 -0
- package/dest/services/tx_collection/fast_tx_collection.d.ts +56 -0
- package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -0
- package/dest/services/tx_collection/fast_tx_collection.js +295 -0
- package/dest/services/tx_collection/index.d.ts +3 -0
- package/dest/services/tx_collection/index.d.ts.map +1 -0
- package/dest/services/tx_collection/index.js +2 -0
- package/dest/services/tx_collection/instrumentation.d.ts +10 -0
- package/dest/services/tx_collection/instrumentation.d.ts.map +1 -0
- package/dest/services/tx_collection/instrumentation.js +34 -0
- package/dest/services/tx_collection/slow_tx_collection.d.ts +54 -0
- package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -0
- package/dest/services/tx_collection/slow_tx_collection.js +176 -0
- package/dest/services/tx_collection/tx_collection.d.ts +109 -0
- package/dest/services/tx_collection/tx_collection.d.ts.map +1 -0
- package/dest/services/tx_collection/tx_collection.js +127 -0
- package/dest/services/tx_collection/tx_collection_sink.d.ts +30 -0
- package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -0
- package/dest/services/tx_collection/tx_collection_sink.js +81 -0
- package/dest/services/tx_collection/tx_source.d.ts +18 -0
- package/dest/services/tx_collection/tx_source.d.ts.map +1 -0
- package/dest/services/tx_collection/tx_source.js +31 -0
- package/dest/services/tx_provider.d.ts +49 -0
- package/dest/services/tx_provider.d.ts.map +1 -0
- package/dest/services/tx_provider.js +206 -0
- package/dest/services/{tx_collect_instrumentation.d.ts → tx_provider_instrumentation.d.ts} +2 -2
- package/dest/services/tx_provider_instrumentation.d.ts.map +1 -0
- package/dest/services/{tx_collect_instrumentation.js → tx_provider_instrumentation.js} +5 -5
- package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
- package/dest/test-helpers/make-test-p2p-clients.js +4 -3
- package/dest/test-helpers/mock-pubsub.d.ts +2 -1
- package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
- package/dest/test-helpers/mock-pubsub.js +2 -1
- package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
- package/dest/test-helpers/reqresp-nodes.js +8 -4
- package/dest/testbench/p2p_client_testbench_worker.js +11 -5
- package/dest/util.d.ts +1 -1
- package/dest/util.d.ts.map +1 -1
- package/package.json +14 -15
- package/src/client/factory.ts +87 -12
- package/src/client/interface.ts +19 -15
- package/src/client/p2p_client.ts +108 -92
- package/src/config.ts +52 -1
- package/src/index.ts +1 -0
- package/src/mem_pools/attestation_pool/attestation_pool.ts +15 -1
- package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +155 -4
- package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +28 -1
- package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +28 -2
- package/src/mem_pools/attestation_pool/mocks.ts +1 -3
- package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +59 -41
- package/src/mem_pools/tx_pool/memory_tx_pool.ts +19 -9
- package/src/mem_pools/tx_pool/tx_pool.ts +7 -2
- package/src/mem_pools/tx_pool/tx_pool_test_suite.ts +63 -40
- package/src/msg_validators/attestation_validator/attestation_validator.ts +1 -1
- package/src/msg_validators/tx_validator/block_header_validator.ts +2 -2
- package/src/msg_validators/tx_validator/data_validator.ts +36 -27
- package/src/msg_validators/tx_validator/double_spend_validator.ts +2 -2
- package/src/msg_validators/tx_validator/gas_validator.ts +4 -4
- package/src/msg_validators/tx_validator/metadata_validator.ts +22 -28
- package/src/msg_validators/tx_validator/phases_validator.ts +2 -2
- package/src/msg_validators/tx_validator/tx_proof_validator.ts +2 -2
- package/src/services/discv5/discV5_service.ts +4 -1
- package/src/services/dummy_service.ts +44 -4
- package/src/services/index.ts +3 -1
- package/src/services/libp2p/libp2p_service.ts +147 -55
- package/src/services/peer-manager/interface.ts +10 -1
- package/src/services/peer-manager/peer_manager.ts +441 -41
- package/src/services/reqresp/config.ts +3 -3
- package/src/services/reqresp/connection-sampler/connection_sampler.ts +38 -63
- package/src/services/reqresp/index.ts +2 -0
- package/src/services/reqresp/interface.ts +63 -17
- package/src/services/reqresp/protocols/auth.ts +83 -0
- package/src/services/reqresp/protocols/block.ts +24 -3
- package/src/services/reqresp/protocols/block_txs/bitvector.ts +90 -0
- package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +53 -0
- package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +79 -0
- package/src/services/reqresp/protocols/block_txs/index.ts +3 -0
- package/src/services/reqresp/protocols/goodbye.ts +3 -3
- package/src/services/reqresp/protocols/index.ts +2 -0
- package/src/services/reqresp/protocols/status.ts +20 -0
- package/src/services/reqresp/protocols/tx.ts +35 -6
- package/src/services/reqresp/rate-limiter/rate_limits.ts +20 -0
- package/src/services/reqresp/reqresp.ts +294 -264
- package/src/services/reqresp/status.ts +9 -3
- package/src/services/service.ts +23 -14
- package/src/services/tx_collection/config.ts +84 -0
- package/src/services/tx_collection/fast_tx_collection.ts +338 -0
- package/src/services/tx_collection/index.ts +2 -0
- package/src/services/tx_collection/instrumentation.ts +43 -0
- package/src/services/tx_collection/slow_tx_collection.ts +232 -0
- package/src/services/tx_collection/tx_collection.ts +214 -0
- package/src/services/tx_collection/tx_collection_sink.ts +98 -0
- package/src/services/tx_collection/tx_source.ts +37 -0
- package/src/services/tx_provider.ts +215 -0
- package/src/services/{tx_collect_instrumentation.ts → tx_provider_instrumentation.ts} +5 -5
- package/src/test-helpers/make-test-p2p-clients.ts +4 -2
- package/src/test-helpers/mock-pubsub.ts +1 -0
- package/src/test-helpers/reqresp-nodes.ts +7 -1
- package/src/testbench/p2p_client_testbench_worker.ts +9 -2
- package/src/util.ts +1 -1
- package/dest/services/tx_collect_instrumentation.d.ts.map +0 -1
- package/dest/services/tx_collector.d.ts +0 -23
- package/dest/services/tx_collector.d.ts.map +0 -1
- package/dest/services/tx_collector.js +0 -95
- package/src/services/tx_collector.ts +0 -134
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { booleanConfigHelper, numberConfigHelper } from '@aztec/foundation/config';
|
|
2
|
-
export const DEFAULT_INDIVIDUAL_REQUEST_TIMEOUT_MS =
|
|
3
|
-
export const DEFAULT_OVERALL_REQUEST_TIMEOUT_MS =
|
|
4
|
-
export const DEFAULT_REQRESP_DIAL_TIMEOUT_MS =
|
|
2
|
+
export const DEFAULT_INDIVIDUAL_REQUEST_TIMEOUT_MS = 10_000;
|
|
3
|
+
export const DEFAULT_OVERALL_REQUEST_TIMEOUT_MS = 10_000; // Not currently used
|
|
4
|
+
export const DEFAULT_REQRESP_DIAL_TIMEOUT_MS = 5_000;
|
|
5
5
|
export const DEFAULT_OPTIMISTIC_NEGOTIATION = false;
|
|
6
6
|
// For use in tests.
|
|
7
7
|
export const DEFAULT_P2P_REQRESP_CONFIG = {
|
|
@@ -15,10 +15,9 @@ export declare class ConnectionSampler {
|
|
|
15
15
|
private readonly logger;
|
|
16
16
|
private readonly opts;
|
|
17
17
|
private cleanupInterval;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
private
|
|
21
|
-
private dialQueue;
|
|
18
|
+
protected readonly activeConnectionsCount: Map<string, number>;
|
|
19
|
+
protected readonly streams: Set<Stream>;
|
|
20
|
+
private abortOnStop;
|
|
22
21
|
constructor(libp2p: Libp2p, sampler: RandomSampler, logger?: import("@aztec/foundation/log").Logger, opts?: {
|
|
23
22
|
cleanupIntervalMs?: number;
|
|
24
23
|
p2pOptimisticNegotiation?: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection_sampler.d.ts","sourceRoot":"","sources":["../../../../src/services/reqresp/connection-sampler/connection_sampler.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"connection_sampler.d.ts","sourceRoot":"","sources":["../../../../src/services/reqresp/connection-sampler/connection_sampler.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAIhE,qBAAa,aAAa;IACxB,MAAM,CAAC,GAAG,EAAE,MAAM;CAGnB;AAED;;;;;;GAMG;AACH,qBAAa,iBAAiB;IAY1B,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IAdvB,OAAO,CAAC,eAAe,CAAiB;IAGxC,SAAS,CAAC,QAAQ,CAAC,sBAAsB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAa;IAG3E,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC,MAAM,CAAC,CAAa;IAEpD,OAAO,CAAC,WAAW,CAA0C;gBAG1C,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,aAAa,EACtB,MAAM,yCAAiD,EACvD,IAAI,GAAE;QAAE,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAAC,wBAAwB,CAAC,EAAE,OAAO,CAAA;KAAO;IAQhG;;OAEG;IACG,IAAI;IAWV;;;;;OAKG;IACH,OAAO,CAAC,SAAS,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,SAAS;IAO7D;;;;;;;;;;;OAWG;IACH,eAAe,CACb,KAAK,EAAE,MAAM,EAAE,EACf,SAAS,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,GAC/B;QACD,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;QACzB,YAAY,EAAE,MAAM,EAAE,CAAC;KACxB;IAuCD;;;;;;OAMG;IACH,gBAAgB,CAAC,cAAc,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,EAAE;IA0CpF;;;;;;;OAOG;IACG,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAuBvF;;OAEG;IACG,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAmC1C;;OAEG;YACW,uBAAuB;CAqBtC"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { AbortError
|
|
1
|
+
import { AbortError } from '@aztec/foundation/error';
|
|
2
2
|
import { createLogger } from '@aztec/foundation/log';
|
|
3
|
-
import { SerialQueue } from '@aztec/foundation/queue';
|
|
4
3
|
const MAX_SAMPLE_ATTEMPTS = 4;
|
|
5
4
|
export class RandomSampler {
|
|
6
5
|
random(max) {
|
|
@@ -19,33 +18,27 @@ export class RandomSampler {
|
|
|
19
18
|
logger;
|
|
20
19
|
opts;
|
|
21
20
|
cleanupInterval;
|
|
22
|
-
|
|
21
|
+
// Map from stringified peer id to number of active connections
|
|
23
22
|
activeConnectionsCount;
|
|
23
|
+
// eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
|
|
24
24
|
streams;
|
|
25
|
-
|
|
26
|
-
dialQueue;
|
|
25
|
+
abortOnStop;
|
|
27
26
|
constructor(libp2p, sampler, logger = createLogger('p2p:reqresp:connection-sampler'), opts = {}){
|
|
28
27
|
this.libp2p = libp2p;
|
|
29
28
|
this.sampler = sampler;
|
|
30
29
|
this.logger = logger;
|
|
31
30
|
this.opts = opts;
|
|
32
|
-
this.dialAttempts = [];
|
|
33
31
|
this.activeConnectionsCount = new Map();
|
|
34
32
|
this.streams = new Set();
|
|
35
|
-
this.
|
|
33
|
+
this.abortOnStop = new AbortController();
|
|
36
34
|
this.cleanupInterval = setInterval(()=>void this.cleanupStaleConnections(), this.opts.cleanupIntervalMs ?? 60_000);
|
|
37
|
-
this.dialQueue.start();
|
|
38
35
|
}
|
|
39
36
|
/**
|
|
40
37
|
* Stops the cleanup job and closes all active connections
|
|
41
38
|
*/ async stop() {
|
|
42
39
|
this.logger.info('Stopping connection sampler');
|
|
40
|
+
this.abortOnStop.abort(new AbortError('Connection sampler stopped'));
|
|
43
41
|
clearInterval(this.cleanupInterval);
|
|
44
|
-
for (const attempt of this.dialAttempts){
|
|
45
|
-
attempt.abort(new AbortError('Connection sampler stopped'));
|
|
46
|
-
}
|
|
47
|
-
this.dialAttempts = [];
|
|
48
|
-
await this.dialQueue.end();
|
|
49
42
|
// Close all active streams
|
|
50
43
|
const closePromises = Array.from(this.streams.values()).map((stream)=>this.close(stream));
|
|
51
44
|
await Promise.all(closePromises);
|
|
@@ -85,7 +78,7 @@ export class RandomSampler {
|
|
|
85
78
|
for(let attempts = 0; attempts < MAX_SAMPLE_ATTEMPTS && peers.length > 0; attempts++){
|
|
86
79
|
const randomIndex = this.sampler.random(peers.length);
|
|
87
80
|
const peer = peers[randomIndex];
|
|
88
|
-
const hasActiveConnections = (this.activeConnectionsCount.get(peer) ?? 0) > 0;
|
|
81
|
+
const hasActiveConnections = (this.activeConnectionsCount.get(peer.toString()) ?? 0) > 0;
|
|
89
82
|
const isExcluded = excluding?.get(peer.toString()) ?? false;
|
|
90
83
|
// Remove this peer from consideration
|
|
91
84
|
peers.splice(randomIndex, 1);
|
|
@@ -129,6 +122,7 @@ export class RandomSampler {
|
|
|
129
122
|
// Only sample as many peers as we have available
|
|
130
123
|
numberToSample = Math.min(numberToSample, peers.length);
|
|
131
124
|
const batch = [];
|
|
125
|
+
// eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
|
|
132
126
|
const withActiveConnections = new Set();
|
|
133
127
|
for(let i = 0; i < numberToSample; i++){
|
|
134
128
|
const { peer, sampledPeers } = this.getPeerFromList(peers, excluding);
|
|
@@ -164,40 +158,27 @@ export class RandomSampler {
|
|
|
164
158
|
* @param timeout - Abort connection if it takes too long
|
|
165
159
|
* @returns The stream
|
|
166
160
|
*/ async dialProtocol(peerId, protocol, timeout) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
peerId: peerId.toString(),
|
|
189
|
-
activeConnectionsCount: updatedActiveConnectionsCount
|
|
190
|
-
});
|
|
191
|
-
return stream;
|
|
192
|
-
} finally{
|
|
193
|
-
if (timeoutHandle) {
|
|
194
|
-
clearTimeout(timeoutHandle);
|
|
195
|
-
}
|
|
196
|
-
const idx = this.dialAttempts.indexOf(abortController);
|
|
197
|
-
if (idx > -1) {
|
|
198
|
-
this.dialAttempts.splice(idx, 1);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
161
|
+
const stream = await this.libp2p.dialProtocol(peerId, protocol, {
|
|
162
|
+
signal: AbortSignal.any(timeout ? [
|
|
163
|
+
this.abortOnStop.signal,
|
|
164
|
+
AbortSignal.timeout(timeout)
|
|
165
|
+
] : [
|
|
166
|
+
this.abortOnStop.signal
|
|
167
|
+
]),
|
|
168
|
+
negotiateFully: !this.opts.p2pOptimisticNegotiation
|
|
169
|
+
});
|
|
170
|
+
stream.metadata.peerId = peerId;
|
|
171
|
+
this.streams.add(stream);
|
|
172
|
+
const peerIdString = peerId.toString();
|
|
173
|
+
const updatedActiveConnectionsCount = (this.activeConnectionsCount.get(peerIdString) ?? 0) + 1;
|
|
174
|
+
this.activeConnectionsCount.set(peerIdString, updatedActiveConnectionsCount);
|
|
175
|
+
this.logger.trace('Dialed protocol', {
|
|
176
|
+
streamId: stream.id,
|
|
177
|
+
protocol,
|
|
178
|
+
peerId: peerIdString,
|
|
179
|
+
activeConnectionsCount: updatedActiveConnectionsCount
|
|
180
|
+
});
|
|
181
|
+
return stream;
|
|
201
182
|
}
|
|
202
183
|
/**
|
|
203
184
|
* Closes a stream and updates the active connections count
|
|
@@ -209,12 +190,12 @@ export class RandomSampler {
|
|
|
209
190
|
if (!peerId) {
|
|
210
191
|
this.logger.warn(`Stream ${stream.id} does not have a peerId set`);
|
|
211
192
|
} else {
|
|
212
|
-
updatedActiveConnectionsCount = (this.activeConnectionsCount.get(peerId) ?? 1) - 1;
|
|
213
|
-
this.activeConnectionsCount.set(peerId, updatedActiveConnectionsCount);
|
|
193
|
+
updatedActiveConnectionsCount = (this.activeConnectionsCount.get(peerId.toString()) ?? 1) - 1;
|
|
194
|
+
this.activeConnectionsCount.set(peerId.toString(), updatedActiveConnectionsCount);
|
|
214
195
|
}
|
|
215
196
|
this.logger.trace('Closing connection', {
|
|
216
197
|
streamId: stream.id,
|
|
217
|
-
peerId: peerId
|
|
198
|
+
peerId: peerId?.toString(),
|
|
218
199
|
protocol: stream.protocol,
|
|
219
200
|
activeConnectionsCount: updatedActiveConnectionsCount
|
|
220
201
|
});
|
|
@@ -224,6 +205,8 @@ export class RandomSampler {
|
|
|
224
205
|
await stream.close();
|
|
225
206
|
} catch (error) {
|
|
226
207
|
this.logger.error(`Failed to close connection to peer ${peerId ?? 'unknown'} with stream id ${stream.id}`, error);
|
|
208
|
+
// graceful close failed, abort the stream
|
|
209
|
+
stream.abort(new AbortError('Failed to close stream gracefully'));
|
|
227
210
|
} finally{
|
|
228
211
|
this.streams.delete(stream);
|
|
229
212
|
}
|
|
@@ -239,7 +222,7 @@ export class RandomSampler {
|
|
|
239
222
|
const peerId = stream.metadata.peerId;
|
|
240
223
|
if (!peerId) {
|
|
241
224
|
this.logger.warn(`Stream ${stream.id} does not have a peerId set`);
|
|
242
|
-
} else if (this.activeConnectionsCount.get(peerId) === 0) {
|
|
225
|
+
} else if (this.activeConnectionsCount.get(peerId.toString()) === 0) {
|
|
243
226
|
await this.close(stream);
|
|
244
227
|
this.logger.debug('Cleaned up stale connection', {
|
|
245
228
|
streamId: stream.id,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/reqresp/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/reqresp/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,cAAc,sBAAsB,CAAC;AACrC,cAAc,gBAAgB,CAAC"}
|
|
@@ -1,17 +1,27 @@
|
|
|
1
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { L2Block } from '@aztec/stdlib/block';
|
|
3
|
+
import { TxArray, TxHashArray } from '@aztec/stdlib/tx';
|
|
1
4
|
import type { PeerId } from '@libp2p/interface';
|
|
2
5
|
import type { P2PReqRespConfig } from './config.js';
|
|
6
|
+
import { AuthRequest, AuthResponse } from './protocols/auth.js';
|
|
7
|
+
import { BlockTxsRequest, BlockTxsResponse } from './protocols/block_txs/block_txs_reqresp.js';
|
|
8
|
+
import { StatusMessage } from './protocols/status.js';
|
|
3
9
|
import type { ReqRespStatus } from './status.js';
|
|
4
10
|
export declare const PING_PROTOCOL = "/aztec/req/ping/0.1.0";
|
|
5
11
|
export declare const STATUS_PROTOCOL = "/aztec/req/status/0.1.0";
|
|
6
12
|
export declare const GOODBYE_PROTOCOL = "/aztec/req/goodbye/0.1.0";
|
|
7
13
|
export declare const TX_REQ_PROTOCOL = "/aztec/req/tx/0.1.0";
|
|
8
14
|
export declare const BLOCK_REQ_PROTOCOL = "/aztec/req/block/0.1.0";
|
|
15
|
+
export declare const AUTH_PROTOCOL = "/aztec/req/auth/0.1.0";
|
|
16
|
+
export declare const BLOCK_TXS_REQ_PROTOCOL = "/aztec/req/block_txs/0.1.0";
|
|
9
17
|
export declare enum ReqRespSubProtocol {
|
|
10
18
|
PING = "/aztec/req/ping/0.1.0",
|
|
11
19
|
STATUS = "/aztec/req/status/0.1.0",
|
|
12
20
|
GOODBYE = "/aztec/req/goodbye/0.1.0",
|
|
13
21
|
TX = "/aztec/req/tx/0.1.0",
|
|
14
|
-
BLOCK = "/aztec/req/block/0.1.0"
|
|
22
|
+
BLOCK = "/aztec/req/block/0.1.0",
|
|
23
|
+
AUTH = "/aztec/req/auth/0.1.0",
|
|
24
|
+
BLOCK_TXS = "/aztec/req/block_txs/0.1.0"
|
|
15
25
|
}
|
|
16
26
|
/**
|
|
17
27
|
* A handler for a sub protocol
|
|
@@ -24,12 +34,15 @@ export type ReqRespSubProtocolHandler = (peerId: PeerId, msg: Buffer) => Promise
|
|
|
24
34
|
export type ReqRespSubProtocolRateLimits = Record<ReqRespSubProtocol, ProtocolRateLimitQuota>;
|
|
25
35
|
/**
|
|
26
36
|
* The response from the ReqResp protocol
|
|
27
|
-
* Consists of a status
|
|
37
|
+
* Consists of a status
|
|
38
|
+
* And, optionally, a data buffer (in case status is SUCCESS)
|
|
28
39
|
*/
|
|
29
|
-
export
|
|
30
|
-
status: ReqRespStatus;
|
|
40
|
+
export type ReqRespResponse = {
|
|
41
|
+
status: ReqRespStatus.SUCCESS;
|
|
31
42
|
data: Buffer;
|
|
32
|
-
}
|
|
43
|
+
} | {
|
|
44
|
+
status: Exclude<ReqRespStatus, ReqRespStatus.SUCCESS>;
|
|
45
|
+
};
|
|
33
46
|
/**
|
|
34
47
|
* A rate limit quota
|
|
35
48
|
*/
|
|
@@ -63,13 +76,21 @@ export type ReqRespSubProtocolValidators = {
|
|
|
63
76
|
[S in ReqRespSubProtocol]: ResponseValidator<any, any>;
|
|
64
77
|
};
|
|
65
78
|
export declare const DEFAULT_SUB_PROTOCOL_VALIDATORS: ReqRespSubProtocolValidators;
|
|
79
|
+
export declare class ValidationError extends Error {
|
|
80
|
+
constructor(message: string);
|
|
81
|
+
}
|
|
66
82
|
/**
|
|
67
83
|
* Sub protocol map determines the request and response types for each
|
|
68
84
|
* Req Resp protocol
|
|
69
85
|
*/
|
|
70
86
|
export type SubProtocolMap = {
|
|
71
|
-
[S in ReqRespSubProtocol]: RequestResponsePair<
|
|
87
|
+
[S in ReqRespSubProtocol]: RequestResponsePair<InstanceType<(typeof subProtocolMap)[S]['request']>, InstanceType<(typeof subProtocolMap)[S]['response']>>;
|
|
72
88
|
};
|
|
89
|
+
/**
|
|
90
|
+
* Default handler for unimplemented sub protocols, this SHOULD be overwritten
|
|
91
|
+
* by the service, but is provided as a fallback
|
|
92
|
+
*/
|
|
93
|
+
export declare const defaultHandler: (_msg: any) => Promise<Buffer>;
|
|
73
94
|
/**
|
|
74
95
|
* Default sub protocol handlers - this SHOULD be overwritten by the service,
|
|
75
96
|
*/
|
|
@@ -93,6 +114,7 @@ interface RequestResponsePair<Req extends {
|
|
|
93
114
|
fromBuffer(buffer: Buffer): Res;
|
|
94
115
|
};
|
|
95
116
|
}
|
|
117
|
+
export declare function responseFromBuffer<P extends ReqRespSubProtocol>(proto: P, buffer: Buffer): InstanceType<(typeof subProtocolMap)[P]['response']>;
|
|
96
118
|
/**
|
|
97
119
|
* RequestableBuffer is a wrapper around a buffer that allows it to be
|
|
98
120
|
* used in generic request response protocols
|
|
@@ -110,12 +132,41 @@ export declare class RequestableBuffer {
|
|
|
110
132
|
* This defines the request and response types for each sub protocol, used primarily
|
|
111
133
|
* as a type rather than an object
|
|
112
134
|
*/
|
|
113
|
-
export declare const subProtocolMap:
|
|
135
|
+
export declare const subProtocolMap: {
|
|
136
|
+
"/aztec/req/ping/0.1.0": {
|
|
137
|
+
request: typeof RequestableBuffer;
|
|
138
|
+
response: typeof RequestableBuffer;
|
|
139
|
+
};
|
|
140
|
+
"/aztec/req/status/0.1.0": {
|
|
141
|
+
request: typeof StatusMessage;
|
|
142
|
+
response: typeof StatusMessage;
|
|
143
|
+
};
|
|
144
|
+
"/aztec/req/tx/0.1.0": {
|
|
145
|
+
request: typeof TxHashArray;
|
|
146
|
+
response: typeof TxArray;
|
|
147
|
+
};
|
|
148
|
+
"/aztec/req/goodbye/0.1.0": {
|
|
149
|
+
request: typeof RequestableBuffer;
|
|
150
|
+
response: typeof RequestableBuffer;
|
|
151
|
+
};
|
|
152
|
+
"/aztec/req/block/0.1.0": {
|
|
153
|
+
request: typeof Fr;
|
|
154
|
+
response: typeof L2Block;
|
|
155
|
+
};
|
|
156
|
+
"/aztec/req/auth/0.1.0": {
|
|
157
|
+
request: typeof AuthRequest;
|
|
158
|
+
response: typeof AuthResponse;
|
|
159
|
+
};
|
|
160
|
+
"/aztec/req/block_txs/0.1.0": {
|
|
161
|
+
request: typeof BlockTxsRequest;
|
|
162
|
+
response: typeof BlockTxsResponse;
|
|
163
|
+
};
|
|
164
|
+
};
|
|
114
165
|
export interface ReqRespInterface {
|
|
115
|
-
start(subProtocolHandlers: ReqRespSubProtocolHandlers
|
|
166
|
+
start(subProtocolHandlers: Partial<ReqRespSubProtocolHandlers>, subProtocolValidators: ReqRespSubProtocolValidators): Promise<void>;
|
|
167
|
+
addSubProtocol(subProtocol: ReqRespSubProtocol, handler: ReqRespSubProtocolHandler, validator?: ReqRespSubProtocolValidators[ReqRespSubProtocol]): Promise<void>;
|
|
116
168
|
stop(): Promise<void>;
|
|
117
|
-
|
|
118
|
-
sendBatchRequest<SubProtocol extends ReqRespSubProtocol>(subProtocol: SubProtocol, requests: InstanceType<SubProtocolMap[SubProtocol]['request']>[], pinnedPeer: PeerId | undefined, timeoutMs?: number, maxPeers?: number, maxRetryAttempts?: number): Promise<(InstanceType<SubProtocolMap[SubProtocol]['response']> | undefined)[]>;
|
|
169
|
+
sendBatchRequest<SubProtocol extends ReqRespSubProtocol>(subProtocol: SubProtocol, requests: InstanceType<SubProtocolMap[SubProtocol]['request']>[], pinnedPeer: PeerId | undefined, timeoutMs?: number, maxPeers?: number, maxRetryAttempts?: number): Promise<InstanceType<SubProtocolMap[SubProtocol]['response']>[]>;
|
|
119
170
|
sendRequestToPeer(peerId: PeerId, subProtocol: ReqRespSubProtocol, payload: Buffer, dialTimeout?: number): Promise<ReqRespResponse>;
|
|
120
171
|
updateConfig(config: Partial<P2PReqRespConfig>): void;
|
|
121
172
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../../src/services/reqresp/interface.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../../src/services/reqresp/interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAExD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAC/F,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAKjD,eAAO,MAAM,aAAa,0BAA0B,CAAC;AACrD,eAAO,MAAM,eAAe,4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AAY,CAAC,MAAM,EAAE,OAAO,CAAC,gBAAgB,CAAC,GAAG,IAAI,CAAC;CACvD"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Fr } from '@aztec/foundation/fields';
|
|
2
2
|
import { L2Block } from '@aztec/stdlib/block';
|
|
3
|
-
import {
|
|
3
|
+
import { TxArray, TxHashArray } from '@aztec/stdlib/tx';
|
|
4
|
+
import { AuthRequest, AuthResponse } from './protocols/auth.js';
|
|
5
|
+
import { BlockTxsRequest, BlockTxsResponse } from './protocols/block_txs/block_txs_reqresp.js';
|
|
4
6
|
import { StatusMessage } from './protocols/status.js';
|
|
5
7
|
/*
|
|
6
8
|
* Request Response Sub Protocols
|
|
@@ -9,12 +11,16 @@ export const STATUS_PROTOCOL = '/aztec/req/status/0.1.0';
|
|
|
9
11
|
export const GOODBYE_PROTOCOL = '/aztec/req/goodbye/0.1.0';
|
|
10
12
|
export const TX_REQ_PROTOCOL = '/aztec/req/tx/0.1.0';
|
|
11
13
|
export const BLOCK_REQ_PROTOCOL = '/aztec/req/block/0.1.0';
|
|
14
|
+
export const AUTH_PROTOCOL = '/aztec/req/auth/0.1.0';
|
|
15
|
+
export const BLOCK_TXS_REQ_PROTOCOL = '/aztec/req/block_txs/0.1.0';
|
|
12
16
|
export var ReqRespSubProtocol = /*#__PURE__*/ function(ReqRespSubProtocol) {
|
|
13
17
|
ReqRespSubProtocol[ReqRespSubProtocol["PING"] = PING_PROTOCOL] = "PING";
|
|
14
18
|
ReqRespSubProtocol[ReqRespSubProtocol["STATUS"] = STATUS_PROTOCOL] = "STATUS";
|
|
15
19
|
ReqRespSubProtocol[ReqRespSubProtocol["GOODBYE"] = GOODBYE_PROTOCOL] = "GOODBYE";
|
|
16
20
|
ReqRespSubProtocol[ReqRespSubProtocol["TX"] = TX_REQ_PROTOCOL] = "TX";
|
|
17
21
|
ReqRespSubProtocol[ReqRespSubProtocol["BLOCK"] = BLOCK_REQ_PROTOCOL] = "BLOCK";
|
|
22
|
+
ReqRespSubProtocol[ReqRespSubProtocol["AUTH"] = AUTH_PROTOCOL] = "AUTH";
|
|
23
|
+
ReqRespSubProtocol[ReqRespSubProtocol["BLOCK_TXS"] = BLOCK_TXS_REQ_PROTOCOL] = "BLOCK_TXS";
|
|
18
24
|
return ReqRespSubProtocol;
|
|
19
25
|
}({});
|
|
20
26
|
export const noopValidator = ()=>Promise.resolve(true);
|
|
@@ -23,12 +29,20 @@ export const DEFAULT_SUB_PROTOCOL_VALIDATORS = {
|
|
|
23
29
|
[ReqRespSubProtocol.STATUS]: noopValidator,
|
|
24
30
|
[ReqRespSubProtocol.TX]: noopValidator,
|
|
25
31
|
[ReqRespSubProtocol.GOODBYE]: noopValidator,
|
|
26
|
-
[ReqRespSubProtocol.BLOCK]: noopValidator
|
|
32
|
+
[ReqRespSubProtocol.BLOCK]: noopValidator,
|
|
33
|
+
[ReqRespSubProtocol.AUTH]: noopValidator,
|
|
34
|
+
[ReqRespSubProtocol.BLOCK_TXS]: noopValidator
|
|
27
35
|
};
|
|
36
|
+
/*
|
|
37
|
+
* Helper class to sub-protocol validation error*/ export class ValidationError extends Error {
|
|
38
|
+
constructor(message){
|
|
39
|
+
super(message);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
28
42
|
/**
|
|
29
43
|
* Default handler for unimplemented sub protocols, this SHOULD be overwritten
|
|
30
44
|
* by the service, but is provided as a fallback
|
|
31
|
-
*/ const defaultHandler = (_msg)=>{
|
|
45
|
+
*/ export const defaultHandler = (_msg)=>{
|
|
32
46
|
return Promise.resolve(Buffer.from('unimplemented'));
|
|
33
47
|
};
|
|
34
48
|
/**
|
|
@@ -38,8 +52,21 @@ export const DEFAULT_SUB_PROTOCOL_VALIDATORS = {
|
|
|
38
52
|
[ReqRespSubProtocol.STATUS]: defaultHandler,
|
|
39
53
|
[ReqRespSubProtocol.TX]: defaultHandler,
|
|
40
54
|
[ReqRespSubProtocol.GOODBYE]: defaultHandler,
|
|
41
|
-
[ReqRespSubProtocol.BLOCK]: defaultHandler
|
|
55
|
+
[ReqRespSubProtocol.BLOCK]: defaultHandler,
|
|
56
|
+
[ReqRespSubProtocol.AUTH]: defaultHandler,
|
|
57
|
+
[ReqRespSubProtocol.BLOCK_TXS]: defaultHandler
|
|
42
58
|
};
|
|
59
|
+
/*
|
|
60
|
+
* Small helper function which parses buffer into specific response type
|
|
61
|
+
* It is needed to make TypeScript happy, as it cannot infer the type from the buffer
|
|
62
|
+
*
|
|
63
|
+
* @param proto - The sub protocol to parse the response for
|
|
64
|
+
* @param buffer - The buffer to parse
|
|
65
|
+
*
|
|
66
|
+
* @returns - The parsed response object
|
|
67
|
+
* */ export function responseFromBuffer(proto, buffer) {
|
|
68
|
+
return subProtocolMap[proto].response.fromBuffer(buffer);
|
|
69
|
+
}
|
|
43
70
|
/**
|
|
44
71
|
* RequestableBuffer is a wrapper around a buffer that allows it to be
|
|
45
72
|
* used in generic request response protocols
|
|
@@ -71,8 +98,8 @@ export const DEFAULT_SUB_PROTOCOL_VALIDATORS = {
|
|
|
71
98
|
response: StatusMessage
|
|
72
99
|
},
|
|
73
100
|
[ReqRespSubProtocol.TX]: {
|
|
74
|
-
request:
|
|
75
|
-
response:
|
|
101
|
+
request: TxHashArray,
|
|
102
|
+
response: TxArray
|
|
76
103
|
},
|
|
77
104
|
[ReqRespSubProtocol.GOODBYE]: {
|
|
78
105
|
request: RequestableBuffer,
|
|
@@ -81,5 +108,13 @@ export const DEFAULT_SUB_PROTOCOL_VALIDATORS = {
|
|
|
81
108
|
[ReqRespSubProtocol.BLOCK]: {
|
|
82
109
|
request: Fr,
|
|
83
110
|
response: L2Block
|
|
111
|
+
},
|
|
112
|
+
[ReqRespSubProtocol.AUTH]: {
|
|
113
|
+
request: AuthRequest,
|
|
114
|
+
response: AuthResponse
|
|
115
|
+
},
|
|
116
|
+
[ReqRespSubProtocol.BLOCK_TXS]: {
|
|
117
|
+
request: BlockTxsRequest,
|
|
118
|
+
response: BlockTxsResponse
|
|
84
119
|
}
|
|
85
120
|
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Buffer32 } from '@aztec/foundation/buffer';
|
|
2
|
+
import { Signature } from '@aztec/foundation/eth-signature';
|
|
3
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
4
|
+
import { BufferReader } from '@aztec/foundation/serialize';
|
|
5
|
+
import { StatusMessage } from './status.js';
|
|
6
|
+
export declare const VALIDATOR_AUTH_DOMAIN_SEPARATOR = "Aztec Validator Challenge:";
|
|
7
|
+
export declare const VALIDATOR_AUTH_FULL_CHALLENGE_ENCODED_LENGTH: number;
|
|
8
|
+
export declare class AuthRequest {
|
|
9
|
+
readonly status: StatusMessage;
|
|
10
|
+
readonly challenge: Fr;
|
|
11
|
+
constructor(status: StatusMessage, challenge: Fr);
|
|
12
|
+
/**
|
|
13
|
+
* Deserializes the AuthRequest object from a Buffer.
|
|
14
|
+
* @param buffer - Buffer or BufferReader object to deserialize.
|
|
15
|
+
* @returns An instance of StatusMessage.
|
|
16
|
+
*/
|
|
17
|
+
static fromBuffer(buffer: Buffer | BufferReader): AuthRequest;
|
|
18
|
+
/**
|
|
19
|
+
* Serializes the AuthRequest object into a Buffer.
|
|
20
|
+
* @returns Buffer representation of the StatusMessage object.
|
|
21
|
+
*/
|
|
22
|
+
toBuffer(): Buffer<ArrayBufferLike>;
|
|
23
|
+
getPayloadToSign(): Buffer32;
|
|
24
|
+
static random(): AuthRequest;
|
|
25
|
+
}
|
|
26
|
+
export declare class AuthResponse {
|
|
27
|
+
readonly status: StatusMessage;
|
|
28
|
+
readonly signature: Signature;
|
|
29
|
+
constructor(status: StatusMessage, signature: Signature);
|
|
30
|
+
/**
|
|
31
|
+
* Deserializes the AuthResponse object from a Buffer.
|
|
32
|
+
* @param buffer - Buffer or BufferReader object to deserialize.
|
|
33
|
+
* @returns An instance of StatusMessage.
|
|
34
|
+
*/
|
|
35
|
+
static fromBuffer(buffer: Buffer | BufferReader): AuthResponse;
|
|
36
|
+
/**
|
|
37
|
+
* Serializes the AuthRequest object into a Buffer.
|
|
38
|
+
* @returns Buffer representation of the StatusMessage object.
|
|
39
|
+
*/
|
|
40
|
+
toBuffer(): Buffer<ArrayBufferLike>;
|
|
41
|
+
static random(): AuthResponse;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../../../src/services/reqresp/protocols/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAEpD,OAAO,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;AAC5D,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAqB,MAAM,6BAA6B,CAAC;AAE9E,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,eAAO,MAAM,+BAA+B,+BAA+B,CAAC;AAC5E,eAAO,MAAM,4CAA4C,QACe,CAAC;AAMzE,qBAAa,WAAW;IAEpB,QAAQ,CAAC,MAAM,EAAE,aAAa;IAC9B,QAAQ,CAAC,SAAS,EAAE,EAAE;gBADb,MAAM,EAAE,aAAa,EACrB,SAAS,EAAE,EAAE;IAGxB;;;;OAIG;IACH,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,GAAG,WAAW;IAQ7D;;;OAGG;IACH,QAAQ;IAIR,gBAAgB,IAAI,QAAQ;IAK5B,MAAM,CAAC,MAAM,IAAI,WAAW;CAG7B;AAED,qBAAa,YAAY;IAErB,QAAQ,CAAC,MAAM,EAAE,aAAa;IAC9B,QAAQ,CAAC,SAAS,EAAE,SAAS;gBADpB,MAAM,EAAE,aAAa,EACrB,SAAS,EAAE,SAAS;IAE/B;;;;OAIG;IACH,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,GAAG,YAAY;IAQ9D;;;OAGG;IACH,QAAQ;IAIR,MAAM,CAAC,MAAM,IAAI,YAAY;CAG9B"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Buffer32 } from '@aztec/foundation/buffer';
|
|
2
|
+
import { keccak256 } from '@aztec/foundation/crypto';
|
|
3
|
+
import { Signature } from '@aztec/foundation/eth-signature';
|
|
4
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
5
|
+
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
6
|
+
import { StatusMessage } from './status.js';
|
|
7
|
+
export const VALIDATOR_AUTH_DOMAIN_SEPARATOR = 'Aztec Validator Challenge:';
|
|
8
|
+
export const VALIDATOR_AUTH_FULL_CHALLENGE_ENCODED_LENGTH = VALIDATOR_AUTH_DOMAIN_SEPARATOR.length + Fr.random().toString().length;
|
|
9
|
+
/*
|
|
10
|
+
* P2P Auth Message
|
|
11
|
+
* Superset of the StatusMessage, used to establish a handshake between peers and authenticate them.
|
|
12
|
+
*/ export class AuthRequest {
|
|
13
|
+
status;
|
|
14
|
+
challenge;
|
|
15
|
+
constructor(status, challenge){
|
|
16
|
+
this.status = status;
|
|
17
|
+
this.challenge = challenge;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Deserializes the AuthRequest object from a Buffer.
|
|
21
|
+
* @param buffer - Buffer or BufferReader object to deserialize.
|
|
22
|
+
* @returns An instance of StatusMessage.
|
|
23
|
+
*/ static fromBuffer(buffer) {
|
|
24
|
+
const reader = BufferReader.asReader(buffer);
|
|
25
|
+
return new AuthRequest(StatusMessage.fromBuffer(reader), Fr.fromBuffer(reader));
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Serializes the AuthRequest object into a Buffer.
|
|
29
|
+
* @returns Buffer representation of the StatusMessage object.
|
|
30
|
+
*/ toBuffer() {
|
|
31
|
+
return serializeToBuffer([
|
|
32
|
+
this.status,
|
|
33
|
+
this.challenge
|
|
34
|
+
]);
|
|
35
|
+
}
|
|
36
|
+
getPayloadToSign() {
|
|
37
|
+
const fullChallenge = VALIDATOR_AUTH_DOMAIN_SEPARATOR + this.challenge.toString();
|
|
38
|
+
return Buffer32.fromBuffer(keccak256(Buffer.from(fullChallenge, 'utf-8')));
|
|
39
|
+
}
|
|
40
|
+
static random() {
|
|
41
|
+
return new AuthRequest(StatusMessage.random(), Fr.random());
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export class AuthResponse {
|
|
45
|
+
status;
|
|
46
|
+
signature;
|
|
47
|
+
constructor(status, signature){
|
|
48
|
+
this.status = status;
|
|
49
|
+
this.signature = signature;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Deserializes the AuthResponse object from a Buffer.
|
|
53
|
+
* @param buffer - Buffer or BufferReader object to deserialize.
|
|
54
|
+
* @returns An instance of StatusMessage.
|
|
55
|
+
*/ static fromBuffer(buffer) {
|
|
56
|
+
const reader = BufferReader.asReader(buffer);
|
|
57
|
+
return new AuthResponse(StatusMessage.fromBuffer(reader), Signature.fromBuffer(reader));
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Serializes the AuthRequest object into a Buffer.
|
|
61
|
+
* @returns Buffer representation of the StatusMessage object.
|
|
62
|
+
*/ toBuffer() {
|
|
63
|
+
return serializeToBuffer([
|
|
64
|
+
this.status,
|
|
65
|
+
this.signature
|
|
66
|
+
]);
|
|
67
|
+
}
|
|
68
|
+
static random() {
|
|
69
|
+
return new AuthResponse(StatusMessage.random(), Signature.random());
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import type { L2BlockSource } from '@aztec/stdlib/block';
|
|
2
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
|
+
* */
|
|
3
8
|
export declare function reqRespBlockHandler(l2BlockSource: L2BlockSource): ReqRespSubProtocolHandler;
|
|
4
9
|
//# sourceMappingURL=block.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block.d.ts","sourceRoot":"","sources":["../../../../src/services/reqresp/protocols/block.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAIzD,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"block.d.ts","sourceRoot":"","sources":["../../../../src/services/reqresp/protocols/block.ts"],"names":[],"mappings":"AACA,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,8 +1,31 @@
|
|
|
1
1
|
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
import { ReqRespStatus, ReqRespStatusError } from '../status.js';
|
|
3
|
+
/**
|
|
4
|
+
* Handler for L2 Block requests
|
|
5
|
+
* @param l2BlockSource - source for L2 blocks
|
|
6
|
+
* @returns the Block request handler
|
|
7
|
+
* */ export function reqRespBlockHandler(l2BlockSource) {
|
|
8
|
+
/**
|
|
9
|
+
* @param peerId - the peer ID of the requester
|
|
10
|
+
* @param msg - the block request message, which is expected to contain valid block number as a Buffer
|
|
11
|
+
* @returns a Buffer containing the requested block data, or an empty Buffer if the block is not found
|
|
12
|
+
* @throws ReqRespStatusError if the input msg is not a valid block number
|
|
13
|
+
* */ return async (_peerId, msg)=>{
|
|
14
|
+
let blockNumber;
|
|
15
|
+
try {
|
|
16
|
+
blockNumber = Fr.fromBuffer(msg);
|
|
17
|
+
} catch (err) {
|
|
18
|
+
throw new ReqRespStatusError(ReqRespStatus.BADLY_FORMED_REQUEST, {
|
|
19
|
+
cause: err
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
const foundBlock = await l2BlockSource.getBlock(Number(blockNumber));
|
|
24
|
+
return foundBlock ? foundBlock.toBuffer() : Buffer.alloc(0);
|
|
25
|
+
} catch (err) {
|
|
26
|
+
throw new ReqRespStatusError(ReqRespStatus.INTERNAL_ERROR, {
|
|
27
|
+
cause: err
|
|
28
|
+
});
|
|
29
|
+
}
|
|
7
30
|
};
|
|
8
31
|
}
|