@aztec/p2p 0.75.0-commit.c03ba01a2a4122e43e90d5133ba017e54b90e9d2 → 0.75.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/bootstrap/bootstrap.d.ts +38 -0
- package/dest/bootstrap/bootstrap.d.ts.map +1 -0
- package/dest/bootstrap/bootstrap.js +29 -41
- package/dest/client/factory.d.ts +19 -0
- package/dest/client/factory.d.ts.map +1 -0
- package/dest/client/factory.js +10 -8
- package/dest/client/index.d.ts +3 -0
- package/dest/client/index.d.ts.map +1 -0
- package/dest/client/index.js +1 -0
- package/dest/client/p2p_client.d.ts +332 -0
- package/dest/client/p2p_client.d.ts.map +1 -0
- package/dest/client/p2p_client.js +535 -513
- package/dest/config.d.ts +171 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +39 -44
- package/dest/errors/reqresp.error.d.ts +28 -0
- package/dest/errors/reqresp.error.d.ts.map +1 -0
- package/dest/errors/reqresp.error.js +10 -6
- package/dest/index.d.ts +9 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +1 -0
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +57 -0
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -0
- package/dest/mem_pools/attestation_pool/attestation_pool.js +2 -6
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +3 -0
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -0
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +32 -64
- package/dest/mem_pools/attestation_pool/index.d.ts +3 -0
- package/dest/mem_pools/attestation_pool/index.d.ts.map +1 -0
- package/dest/mem_pools/attestation_pool/index.js +1 -0
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +22 -0
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +1 -0
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +19 -22
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +17 -0
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +1 -0
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +26 -22
- package/dest/mem_pools/attestation_pool/mocks.d.ts +18 -0
- package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -0
- package/dest/mem_pools/attestation_pool/mocks.js +6 -10
- package/dest/mem_pools/epoch_proof_quote_pool/epoch_proof_quote_pool.d.ts +7 -0
- package/dest/mem_pools/epoch_proof_quote_pool/epoch_proof_quote_pool.d.ts.map +1 -0
- package/dest/mem_pools/epoch_proof_quote_pool/epoch_proof_quote_pool.js +2 -1
- package/dest/mem_pools/epoch_proof_quote_pool/index.d.ts +4 -0
- package/dest/mem_pools/epoch_proof_quote_pool/index.d.ts.map +1 -0
- package/dest/mem_pools/epoch_proof_quote_pool/index.js +1 -0
- package/dest/mem_pools/epoch_proof_quote_pool/memory_epoch_proof_quote_pool.d.ts +12 -0
- package/dest/mem_pools/epoch_proof_quote_pool/memory_epoch_proof_quote_pool.d.ts.map +1 -0
- package/dest/mem_pools/epoch_proof_quote_pool/memory_epoch_proof_quote_pool.js +4 -5
- package/dest/mem_pools/epoch_proof_quote_pool/test_utils.d.ts +8 -0
- package/dest/mem_pools/epoch_proof_quote_pool/test_utils.d.ts.map +1 -0
- package/dest/mem_pools/epoch_proof_quote_pool/test_utils.js +3 -2
- package/dest/mem_pools/index.d.ts +5 -0
- package/dest/mem_pools/index.d.ts.map +1 -0
- package/dest/mem_pools/index.js +2 -1
- package/dest/mem_pools/instrumentation.d.ts +31 -0
- package/dest/mem_pools/instrumentation.d.ts.map +1 -0
- package/dest/mem_pools/instrumentation.js +42 -37
- package/dest/mem_pools/interface.d.ts +13 -0
- package/dest/mem_pools/interface.d.ts.map +1 -0
- package/dest/mem_pools/interface.js +2 -3
- package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts +66 -0
- package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.js +134 -127
- package/dest/mem_pools/tx_pool/index.d.ts +4 -0
- package/dest/mem_pools/tx_pool/index.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/index.js +1 -0
- package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts +56 -0
- package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/memory_tx_pool.js +43 -45
- package/dest/mem_pools/tx_pool/priority.d.ts +8 -0
- package/dest/mem_pools/tx_pool/priority.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/priority.js +3 -1
- package/dest/mem_pools/tx_pool/tx_pool.d.ts +66 -0
- package/dest/mem_pools/tx_pool/tx_pool.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/tx_pool.js +2 -3
- package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts +7 -0
- package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/tx_pool_test_suite.js +37 -107
- package/dest/mocks/index.d.ts +54 -0
- package/dest/mocks/index.d.ts.map +1 -0
- package/dest/mocks/index.js +38 -47
- package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +8 -0
- package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -0
- package/dest/msg_validators/attestation_validator/attestation_validator.js +3 -3
- package/dest/msg_validators/attestation_validator/index.d.ts +2 -0
- package/dest/msg_validators/attestation_validator/index.d.ts.map +1 -0
- package/dest/msg_validators/attestation_validator/index.js +1 -0
- package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts +8 -0
- package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts.map +1 -0
- package/dest/msg_validators/block_proposal_validator/block_proposal_validator.js +2 -2
- package/dest/msg_validators/block_proposal_validator/index.d.ts +2 -0
- package/dest/msg_validators/block_proposal_validator/index.d.ts.map +1 -0
- package/dest/msg_validators/block_proposal_validator/index.js +1 -0
- package/dest/msg_validators/epoch_proof_quote_validator/epoch_proof_quote_validator.d.ts +8 -0
- package/dest/msg_validators/epoch_proof_quote_validator/epoch_proof_quote_validator.d.ts.map +1 -0
- package/dest/msg_validators/epoch_proof_quote_validator/epoch_proof_quote_validator.js +2 -2
- package/dest/msg_validators/epoch_proof_quote_validator/index.d.ts +2 -0
- package/dest/msg_validators/epoch_proof_quote_validator/index.d.ts.map +1 -0
- package/dest/msg_validators/epoch_proof_quote_validator/index.js +1 -0
- package/dest/msg_validators/index.d.ts +4 -0
- package/dest/msg_validators/index.d.ts.map +1 -0
- package/dest/msg_validators/index.js +1 -0
- package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +7 -0
- package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/aggregate_tx_validator.js +11 -9
- package/dest/msg_validators/tx_validator/block_header_validator.d.ts +11 -0
- package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/block_header_validator.js +12 -17
- package/dest/msg_validators/tx_validator/data_validator.d.ts +6 -0
- package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/data_validator.js +32 -41
- package/dest/msg_validators/tx_validator/double_spend_validator.d.ts +12 -0
- package/dest/msg_validators/tx_validator/double_spend_validator.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/double_spend_validator.js +14 -22
- package/dest/msg_validators/tx_validator/index.d.ts +7 -0
- package/dest/msg_validators/tx_validator/index.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/index.js +1 -0
- package/dest/msg_validators/tx_validator/metadata_validator.d.ts +10 -0
- package/dest/msg_validators/tx_validator/metadata_validator.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/metadata_validator.js +26 -29
- package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts +8 -0
- package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/tx_proof_validator.js +12 -17
- package/dest/services/data_store.d.ts +27 -0
- package/dest/services/data_store.d.ts.map +1 -0
- package/dest/services/data_store.js +57 -57
- package/dest/services/discv5/discV5_service.d.ts +36 -0
- package/dest/services/discv5/discV5_service.d.ts.map +1 -0
- package/dest/services/discv5/discV5_service.js +23 -31
- package/dest/services/dummy_service.d.ts +82 -0
- package/dest/services/dummy_service.d.ts.map +1 -0
- package/dest/services/dummy_service.js +58 -40
- package/dest/services/encoding.d.ts +31 -0
- package/dest/services/encoding.d.ts.map +1 -0
- package/dest/services/encoding.js +9 -10
- package/dest/services/index.d.ts +3 -0
- package/dest/services/index.d.ts.map +1 -0
- package/dest/services/index.js +1 -0
- package/dest/services/libp2p/libp2p_service.d.ts +225 -0
- package/dest/services/libp2p/libp2p_service.d.ts.map +1 -0
- package/dest/services/libp2p/libp2p_service.js +695 -709
- package/dest/services/peer-manager/metrics.d.ts +12 -0
- package/dest/services/peer-manager/metrics.d.ts.map +1 -0
- package/dest/services/peer-manager/metrics.js +7 -14
- package/dest/services/peer-manager/peer_manager.d.ts +76 -0
- package/dest/services/peer-manager/peer_manager.d.ts.map +1 -0
- package/dest/services/peer-manager/peer_manager.js +342 -340
- package/dest/services/peer-manager/peer_scoring.d.ts +28 -0
- package/dest/services/peer-manager/peer_scoring.d.ts.map +1 -0
- package/dest/services/peer-manager/peer_scoring.js +18 -20
- package/dest/services/reqresp/config.d.ts +16 -0
- package/dest/services/reqresp/config.d.ts.map +1 -0
- package/dest/services/reqresp/config.js +5 -4
- package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts +45 -0
- package/dest/services/reqresp/connection-sampler/batch_connection_sampler.d.ts.map +1 -0
- package/dest/services/reqresp/connection-sampler/batch_connection_sampler.js +28 -35
- package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts +61 -0
- package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts.map +1 -0
- package/dest/services/reqresp/connection-sampler/connection_sampler.js +59 -60
- package/dest/services/reqresp/index.d.ts +6 -0
- package/dest/services/reqresp/index.d.ts.map +1 -0
- package/dest/services/reqresp/index.js +3 -1
- package/dest/services/reqresp/interface.d.ts +107 -0
- package/dest/services/reqresp/interface.d.ts.map +1 -0
- package/dest/services/reqresp/interface.js +30 -25
- package/dest/services/reqresp/metrics.d.ts +15 -0
- package/dest/services/reqresp/metrics.d.ts.map +1 -0
- package/dest/services/reqresp/metrics.js +10 -23
- package/dest/services/reqresp/protocols/block.d.ts +4 -0
- package/dest/services/reqresp/protocols/block.d.ts.map +1 -0
- package/dest/services/reqresp/protocols/block.js +2 -1
- package/dest/services/reqresp/protocols/goodbye.d.ts +51 -0
- package/dest/services/reqresp/protocols/goodbye.d.ts.map +1 -0
- package/dest/services/reqresp/protocols/goodbye.js +40 -35
- package/dest/services/reqresp/protocols/index.d.ts +9 -0
- package/dest/services/reqresp/protocols/index.d.ts.map +1 -0
- package/dest/services/reqresp/protocols/index.js +3 -1
- package/dest/services/reqresp/protocols/ping.d.ts +9 -0
- package/dest/services/reqresp/protocols/ping.d.ts.map +1 -0
- package/dest/services/reqresp/protocols/ping.js +3 -1
- package/dest/services/reqresp/protocols/status.d.ts +9 -0
- package/dest/services/reqresp/protocols/status.d.ts.map +1 -0
- package/dest/services/reqresp/protocols/status.js +3 -1
- package/dest/services/reqresp/protocols/tx.d.ts +13 -0
- package/dest/services/reqresp/protocols/tx.d.ts.map +1 -0
- package/dest/services/reqresp/protocols/tx.js +8 -5
- package/dest/services/reqresp/rate-limiter/index.d.ts +2 -0
- package/dest/services/reqresp/rate-limiter/index.d.ts.map +1 -0
- package/dest/services/reqresp/rate-limiter/index.js +1 -0
- package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts +102 -0
- package/dest/services/reqresp/rate-limiter/rate_limiter.d.ts.map +1 -0
- package/dest/services/reqresp/rate-limiter/rate_limiter.js +36 -42
- package/dest/services/reqresp/rate-limiter/rate_limits.d.ts +3 -0
- package/dest/services/reqresp/rate-limiter/rate_limits.d.ts.map +1 -0
- package/dest/services/reqresp/rate-limiter/rate_limits.js +17 -16
- package/dest/services/reqresp/reqresp.d.ts +161 -0
- package/dest/services/reqresp/reqresp.d.ts.map +1 -0
- package/dest/services/reqresp/reqresp.js +395 -461
- package/dest/services/service.d.ts +85 -0
- package/dest/services/service.d.ts.map +1 -0
- package/dest/services/service.js +4 -3
- package/dest/services/types.d.ts +38 -0
- package/dest/services/types.d.ts.map +1 -0
- package/dest/services/types.js +23 -16
- package/dest/util.d.ts +53 -0
- package/dest/util.d.ts.map +1 -0
- package/dest/util.js +34 -23
- package/package.json +8 -8
- package/src/mem_pools/index.ts +3 -3
- package/src/mem_pools/instrumentation.ts +2 -3
- package/src/services/reqresp/interface.ts +0 -11
- package/src/services/reqresp/rate-limiter/rate_limiter.ts +1 -3
- package/src/services/reqresp/rate-limiter/rate_limits.ts +2 -2
- package/src/services/reqresp/reqresp.ts +24 -111
- package/dest/services/reqresp/status.js +0 -51
- package/src/services/reqresp/status.ts +0 -59
|
@@ -22,8 +22,7 @@ import { ConnectionSampler } from './connection-sampler/connection_sampler.js';
|
|
|
22
22
|
import {
|
|
23
23
|
DEFAULT_SUB_PROTOCOL_HANDLERS,
|
|
24
24
|
DEFAULT_SUB_PROTOCOL_VALIDATORS,
|
|
25
|
-
type
|
|
26
|
-
ReqRespSubProtocol,
|
|
25
|
+
type ReqRespSubProtocol,
|
|
27
26
|
type ReqRespSubProtocolHandlers,
|
|
28
27
|
type ReqRespSubProtocolValidators,
|
|
29
28
|
type SubProtocolMap,
|
|
@@ -31,7 +30,6 @@ import {
|
|
|
31
30
|
} from './interface.js';
|
|
32
31
|
import { ReqRespMetrics } from './metrics.js';
|
|
33
32
|
import { RequestResponseRateLimiter } from './rate-limiter/rate_limiter.js';
|
|
34
|
-
import { ReqRespStatus, ReqRespStatusError, parseStatusChunk, prettyPrintReqRespStatus } from './status.js';
|
|
35
33
|
|
|
36
34
|
/**
|
|
37
35
|
* The Request Response Service
|
|
@@ -192,17 +190,10 @@ export class ReqResp {
|
|
|
192
190
|
this.logger.trace(`Sending request to peer: ${peer.toString()}`);
|
|
193
191
|
const response = await this.sendRequestToPeer(peer, subProtocol, requestBuffer);
|
|
194
192
|
|
|
195
|
-
if (response && response.status !== ReqRespStatus.SUCCESS) {
|
|
196
|
-
this.logger.debug(
|
|
197
|
-
`Request to peer ${peer.toString()} failed with status ${prettyPrintReqRespStatus(response.status)}`,
|
|
198
|
-
);
|
|
199
|
-
continue;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
193
|
// If we get a response, return it, otherwise we iterate onto the next peer
|
|
203
194
|
// We do not consider it a success if we have an empty buffer
|
|
204
|
-
if (response && response.
|
|
205
|
-
const object = subProtocolMap[subProtocol].response.fromBuffer(response
|
|
195
|
+
if (response && response.length > 0) {
|
|
196
|
+
const object = subProtocolMap[subProtocol].response.fromBuffer(response);
|
|
206
197
|
// The response validator handles peer punishment within
|
|
207
198
|
const isValid = await responseValidator(request, object, peer);
|
|
208
199
|
if (!isValid) {
|
|
@@ -320,22 +311,8 @@ export class ReqResp {
|
|
|
320
311
|
for (const index of indices) {
|
|
321
312
|
const response = await this.sendRequestToPeer(peer, subProtocol, requestBuffers[index]);
|
|
322
313
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
this.logger.debug(
|
|
326
|
-
`Request to peer ${peer.toString()} failed with status ${prettyPrintReqRespStatus(
|
|
327
|
-
response.status,
|
|
328
|
-
)}`,
|
|
329
|
-
);
|
|
330
|
-
|
|
331
|
-
// If we hit a rate limit or some failure, we remove the peer and return the results,
|
|
332
|
-
// they will be split among remaining peers and the new sampled peer
|
|
333
|
-
batchSampler.removePeerAndReplace(peer);
|
|
334
|
-
return { peer, results: peerResults };
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
if (response && response.data.length > 0) {
|
|
338
|
-
const object = subProtocolMap[subProtocol].response.fromBuffer(response.data);
|
|
314
|
+
if (response && response.length > 0) {
|
|
315
|
+
const object = subProtocolMap[subProtocol].response.fromBuffer(response);
|
|
339
316
|
const isValid = await responseValidator(requests[index], object, peer);
|
|
340
317
|
|
|
341
318
|
if (isValid) {
|
|
@@ -417,7 +394,7 @@ export class ReqResp {
|
|
|
417
394
|
peerId: PeerId,
|
|
418
395
|
subProtocol: ReqRespSubProtocol,
|
|
419
396
|
payload: Buffer,
|
|
420
|
-
): Promise<
|
|
397
|
+
): Promise<Buffer | undefined> {
|
|
421
398
|
let stream: Stream | undefined;
|
|
422
399
|
try {
|
|
423
400
|
this.metrics.recordRequestSent(subProtocol);
|
|
@@ -425,8 +402,8 @@ export class ReqResp {
|
|
|
425
402
|
stream = await this.connectionSampler.dialProtocol(peerId, subProtocol);
|
|
426
403
|
|
|
427
404
|
// Open the stream with a timeout
|
|
428
|
-
const result = await executeTimeout<
|
|
429
|
-
(): Promise<
|
|
405
|
+
const result = await executeTimeout<Buffer>(
|
|
406
|
+
(): Promise<Buffer> => pipe([payload], stream!, this.readMessage.bind(this)),
|
|
430
407
|
this.individualRequestTimeoutMs,
|
|
431
408
|
() => new IndividualReqRespTimeoutError(),
|
|
432
409
|
);
|
|
@@ -470,15 +447,7 @@ export class ReqResp {
|
|
|
470
447
|
* Categorize the error and log it.
|
|
471
448
|
*/
|
|
472
449
|
private categorizeError(e: any, peerId: PeerId, subProtocol: ReqRespSubProtocol): PeerErrorSeverity | undefined {
|
|
473
|
-
// Non
|
|
474
|
-
if (subProtocol === ReqRespSubProtocol.GOODBYE) {
|
|
475
|
-
this.logger.debug('Error encountered on goodbye sub protocol, no penalty', {
|
|
476
|
-
peerId: peerId.toString(),
|
|
477
|
-
subProtocol,
|
|
478
|
-
});
|
|
479
|
-
return undefined;
|
|
480
|
-
}
|
|
481
|
-
|
|
450
|
+
// Non pubishable errors
|
|
482
451
|
// We do not punish a collective timeout, as the node triggers this interupt, independent of the peer's behaviour
|
|
483
452
|
const logTags = {
|
|
484
453
|
peerId: peerId.toString(),
|
|
@@ -523,45 +492,14 @@ export class ReqResp {
|
|
|
523
492
|
|
|
524
493
|
/**
|
|
525
494
|
* Read a message returned from a stream into a single buffer
|
|
526
|
-
*
|
|
527
|
-
* The message is split into two components
|
|
528
|
-
* - The first chunk should contain a control byte, indicating the status of the response see `ReqRespStatus`
|
|
529
|
-
* - The second chunk should contain the response data
|
|
530
495
|
*/
|
|
531
|
-
private async readMessage(source: AsyncIterable<Uint8ArrayList>): Promise<
|
|
532
|
-
let statusBuffer: ReqRespStatus | undefined;
|
|
496
|
+
private async readMessage(source: AsyncIterable<Uint8ArrayList>): Promise<Buffer> {
|
|
533
497
|
const chunks: Uint8Array[] = [];
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
for await (const chunk of source) {
|
|
537
|
-
if (statusBuffer === undefined) {
|
|
538
|
-
const firstChunkBuffer = chunk.subarray();
|
|
539
|
-
statusBuffer = parseStatusChunk(firstChunkBuffer);
|
|
540
|
-
} else {
|
|
541
|
-
chunks.push(chunk.subarray());
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
const messageData = Buffer.concat(chunks);
|
|
546
|
-
const message: Buffer = this.snappyTransform.inboundTransformNoTopic(messageData);
|
|
547
|
-
|
|
548
|
-
return {
|
|
549
|
-
status: statusBuffer ?? ReqRespStatus.UNKNOWN,
|
|
550
|
-
data: message,
|
|
551
|
-
};
|
|
552
|
-
} catch (e: any) {
|
|
553
|
-
this.logger.debug(`Reading message failed: ${e.message}`);
|
|
554
|
-
|
|
555
|
-
let status = ReqRespStatus.UNKNOWN;
|
|
556
|
-
if (e instanceof ReqRespStatusError) {
|
|
557
|
-
status = e.status;
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
return {
|
|
561
|
-
status,
|
|
562
|
-
data: Buffer.from([]),
|
|
563
|
-
};
|
|
498
|
+
for await (const chunk of source) {
|
|
499
|
+
chunks.push(chunk.subarray());
|
|
564
500
|
}
|
|
501
|
+
const messageData = Buffer.concat(chunks);
|
|
502
|
+
return this.snappyTransform.inboundTransformNoTopic(messageData);
|
|
565
503
|
}
|
|
566
504
|
|
|
567
505
|
/**
|
|
@@ -587,28 +525,25 @@ export class ReqResp {
|
|
|
587
525
|
private async streamHandler(protocol: ReqRespSubProtocol, { stream, connection }: IncomingStreamData) {
|
|
588
526
|
this.metrics.recordRequestReceived(protocol);
|
|
589
527
|
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
this.logger.warn(`Rate limit exceeded for ${protocol} from ${connection.remotePeer}`);
|
|
528
|
+
// Store a reference to from this for the async generator
|
|
529
|
+
if (!this.rateLimiter.allow(protocol, connection.remotePeer)) {
|
|
530
|
+
this.logger.warn(`Rate limit exceeded for ${protocol} from ${connection.remotePeer}`);
|
|
594
531
|
|
|
595
|
-
|
|
596
|
-
|
|
532
|
+
// TODO(#8483): handle changing peer scoring for failed rate limit, maybe differentiate between global and peer limits here when punishing
|
|
533
|
+
await stream.close();
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
597
536
|
|
|
598
|
-
|
|
599
|
-
|
|
537
|
+
const handler = this.subProtocolHandlers[protocol];
|
|
538
|
+
const transform = this.snappyTransform;
|
|
600
539
|
|
|
540
|
+
try {
|
|
601
541
|
await pipe(
|
|
602
542
|
stream,
|
|
603
543
|
async function* (source: any) {
|
|
604
544
|
for await (const chunkList of source) {
|
|
605
545
|
const msg = Buffer.from(chunkList.subarray());
|
|
606
546
|
const response = await handler(connection.remotePeer, msg);
|
|
607
|
-
|
|
608
|
-
// Send success code first, then the response
|
|
609
|
-
const successChunk = Buffer.from([ReqRespStatus.SUCCESS]);
|
|
610
|
-
yield new Uint8Array(successChunk);
|
|
611
|
-
|
|
612
547
|
yield new Uint8Array(transform.outboundTransformNoTopic(response));
|
|
613
548
|
}
|
|
614
549
|
},
|
|
@@ -617,30 +552,8 @@ export class ReqResp {
|
|
|
617
552
|
} catch (e: any) {
|
|
618
553
|
this.logger.warn(e);
|
|
619
554
|
this.metrics.recordResponseError(protocol);
|
|
620
|
-
|
|
621
|
-
// If we receive a known error, we use the error status in the response chunk, otherwise we categorize as unknown
|
|
622
|
-
let errorStatus = ReqRespStatus.UNKNOWN;
|
|
623
|
-
if (e instanceof ReqRespStatusError) {
|
|
624
|
-
errorStatus = e.status;
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
const sendErrorChunk = this.sendErrorChunk(errorStatus);
|
|
628
|
-
|
|
629
|
-
// Return and yield the response chunk
|
|
630
|
-
await pipe(
|
|
631
|
-
stream,
|
|
632
|
-
async function* (_source: any) {
|
|
633
|
-
yield* sendErrorChunk;
|
|
634
|
-
},
|
|
635
|
-
stream,
|
|
636
|
-
);
|
|
637
555
|
} finally {
|
|
638
556
|
await stream.close();
|
|
639
557
|
}
|
|
640
558
|
}
|
|
641
|
-
|
|
642
|
-
private async *sendErrorChunk(error: ReqRespStatus): AsyncIterable<Uint8Array> {
|
|
643
|
-
const errorChunk = Buffer.from([error]);
|
|
644
|
-
yield new Uint8Array(errorChunk);
|
|
645
|
-
}
|
|
646
559
|
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The error codes for the ReqResp protocol
|
|
3
|
-
*/ export var ReqRespStatus = /*#__PURE__*/ function(ReqRespStatus) {
|
|
4
|
-
ReqRespStatus[ReqRespStatus["SUCCESS"] = 0] = "SUCCESS";
|
|
5
|
-
ReqRespStatus[ReqRespStatus["RATE_LIMIT_EXCEEDED"] = 1] = "RATE_LIMIT_EXCEEDED";
|
|
6
|
-
ReqRespStatus[ReqRespStatus["BADLY_FORMED_REQUEST"] = 2] = "BADLY_FORMED_REQUEST";
|
|
7
|
-
ReqRespStatus[ReqRespStatus["UNKNOWN"] = 127] = "UNKNOWN";
|
|
8
|
-
return ReqRespStatus;
|
|
9
|
-
}({});
|
|
10
|
-
export class ReqRespStatusError extends Error {
|
|
11
|
-
/**
|
|
12
|
-
* The status code
|
|
13
|
-
*/ status;
|
|
14
|
-
constructor(status){
|
|
15
|
-
super(`ReqResp Error: ${prettyPrintReqRespStatus(status)}`);
|
|
16
|
-
this.status = status;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Parse the status chunk
|
|
21
|
-
* @param chunk
|
|
22
|
-
* @returns
|
|
23
|
-
*
|
|
24
|
-
* @throws ReqRespStatusError if the chunk is not valid
|
|
25
|
-
*/ export function parseStatusChunk(chunk) {
|
|
26
|
-
if (chunk.length !== 1) {
|
|
27
|
-
throw new ReqRespStatusError(127);
|
|
28
|
-
}
|
|
29
|
-
const status = chunk[0];
|
|
30
|
-
// Check if status is a valid ReqRespStatus value
|
|
31
|
-
if (!(status in ReqRespStatus)) {
|
|
32
|
-
throw new ReqRespStatusError(127);
|
|
33
|
-
}
|
|
34
|
-
return status;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Pretty print the ReqResp status
|
|
38
|
-
* @param status
|
|
39
|
-
* @returns
|
|
40
|
-
*/ export function prettyPrintReqRespStatus(status) {
|
|
41
|
-
switch(status){
|
|
42
|
-
case 0:
|
|
43
|
-
return 'SUCCESS';
|
|
44
|
-
case 1:
|
|
45
|
-
return 'RATE_LIMIT_EXCEEDED';
|
|
46
|
-
case 2:
|
|
47
|
-
return 'BADLY_FORMED_REQUEST';
|
|
48
|
-
case 127:
|
|
49
|
-
return 'UNKNOWN';
|
|
50
|
-
}
|
|
51
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The error codes for the ReqResp protocol
|
|
3
|
-
*/
|
|
4
|
-
export enum ReqRespStatus {
|
|
5
|
-
SUCCESS = 0,
|
|
6
|
-
RATE_LIMIT_EXCEEDED = 1,
|
|
7
|
-
BADLY_FORMED_REQUEST = 2,
|
|
8
|
-
UNKNOWN = 127,
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export class ReqRespStatusError extends Error {
|
|
12
|
-
/**
|
|
13
|
-
* The status code
|
|
14
|
-
*/
|
|
15
|
-
status: ReqRespStatus;
|
|
16
|
-
|
|
17
|
-
constructor(status: ReqRespStatus) {
|
|
18
|
-
super(`ReqResp Error: ${prettyPrintReqRespStatus(status)}`);
|
|
19
|
-
this.status = status;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Parse the status chunk
|
|
25
|
-
* @param chunk
|
|
26
|
-
* @returns
|
|
27
|
-
*
|
|
28
|
-
* @throws ReqRespStatusError if the chunk is not valid
|
|
29
|
-
*/
|
|
30
|
-
export function parseStatusChunk(chunk: Uint8Array): ReqRespStatus {
|
|
31
|
-
if (chunk.length !== 1) {
|
|
32
|
-
throw new ReqRespStatusError(ReqRespStatus.UNKNOWN);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const status = chunk[0];
|
|
36
|
-
// Check if status is a valid ReqRespStatus value
|
|
37
|
-
if (!(status in ReqRespStatus)) {
|
|
38
|
-
throw new ReqRespStatusError(ReqRespStatus.UNKNOWN);
|
|
39
|
-
}
|
|
40
|
-
return status as ReqRespStatus;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Pretty print the ReqResp status
|
|
45
|
-
* @param status
|
|
46
|
-
* @returns
|
|
47
|
-
*/
|
|
48
|
-
export function prettyPrintReqRespStatus(status: ReqRespStatus) {
|
|
49
|
-
switch (status) {
|
|
50
|
-
case ReqRespStatus.SUCCESS:
|
|
51
|
-
return 'SUCCESS';
|
|
52
|
-
case ReqRespStatus.RATE_LIMIT_EXCEEDED:
|
|
53
|
-
return 'RATE_LIMIT_EXCEEDED';
|
|
54
|
-
case ReqRespStatus.BADLY_FORMED_REQUEST:
|
|
55
|
-
return 'BADLY_FORMED_REQUEST';
|
|
56
|
-
case ReqRespStatus.UNKNOWN:
|
|
57
|
-
return 'UNKNOWN';
|
|
58
|
-
}
|
|
59
|
-
}
|