@aztec/p2p 0.0.1-commit.cd76b27 → 0.0.1-commit.ce4f8c4f2
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/README.md +129 -3
- package/dest/client/factory.d.ts +5 -6
- package/dest/client/factory.d.ts.map +1 -1
- package/dest/client/factory.js +28 -26
- package/dest/client/interface.d.ts +6 -13
- package/dest/client/interface.d.ts.map +1 -1
- package/dest/client/p2p_client.d.ts +5 -13
- package/dest/client/p2p_client.d.ts.map +1 -1
- package/dest/client/p2p_client.js +25 -92
- package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.js +4 -5
- package/dest/config.d.ts +33 -15
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +86 -37
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +4 -4
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool.js +8 -4
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +6 -6
- package/dest/mem_pools/instrumentation.d.ts +4 -2
- package/dest/mem_pools/instrumentation.d.ts.map +1 -1
- package/dest/mem_pools/instrumentation.js +16 -14
- package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts +1 -1
- package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.js +2 -1
- package/dest/mem_pools/tx_pool/priority.d.ts +2 -2
- package/dest/mem_pools/tx_pool/priority.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/priority.js +4 -4
- package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts +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 +3 -1
- package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts +1 -1
- package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.js +3 -2
- package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts +1 -1
- package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.js +2 -0
- package/dest/mem_pools/tx_pool_v2/eviction/interfaces.d.ts +7 -1
- package/dest/mem_pools/tx_pool_v2/eviction/interfaces.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.js +2 -2
- package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts +2 -2
- package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.js +10 -6
- package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.d.ts +1 -1
- package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.js +8 -6
- package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.d.ts +2 -2
- package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.js +2 -2
- package/dest/mem_pools/tx_pool_v2/index.d.ts +2 -2
- package/dest/mem_pools/tx_pool_v2/index.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/index.js +1 -1
- package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +15 -9
- package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/interfaces.js +3 -1
- package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +48 -11
- package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_metadata.js +81 -17
- package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_pool_indices.js +26 -44
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +5 -3
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2.js +6 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +3 -2
- 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 +196 -151
- package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +1 -1
- package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
- package/dest/msg_validators/attestation_validator/attestation_validator.js +5 -4
- package/dest/msg_validators/clock_tolerance.d.ts +1 -1
- package/dest/msg_validators/clock_tolerance.d.ts.map +1 -1
- package/dest/msg_validators/clock_tolerance.js +4 -3
- package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts +6 -4
- package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts.map +1 -1
- package/dest/msg_validators/proposal_validator/block_proposal_validator.js +10 -2
- package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts +6 -4
- package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts.map +1 -1
- package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.js +16 -2
- package/dest/msg_validators/proposal_validator/proposal_validator.d.ts +13 -8
- package/dest/msg_validators/proposal_validator/proposal_validator.d.ts.map +1 -1
- package/dest/msg_validators/proposal_validator/proposal_validator.js +53 -41
- package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +2 -2
- package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/aggregate_tx_validator.js +3 -3
- package/dest/msg_validators/tx_validator/allowed_public_setup.d.ts +2 -1
- package/dest/msg_validators/tx_validator/allowed_public_setup.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/allowed_public_setup.js +24 -20
- package/dest/msg_validators/tx_validator/allowed_setup_helpers.d.ts +17 -0
- package/dest/msg_validators/tx_validator/allowed_setup_helpers.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/allowed_setup_helpers.js +24 -0
- package/dest/msg_validators/tx_validator/contract_instance_validator.d.ts +9 -0
- package/dest/msg_validators/tx_validator/contract_instance_validator.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/contract_instance_validator.js +48 -0
- package/dest/msg_validators/tx_validator/data_validator.d.ts +1 -1
- package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/data_validator.js +35 -2
- package/dest/msg_validators/tx_validator/factory.d.ts +133 -6
- package/dest/msg_validators/tx_validator/factory.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/factory.js +247 -60
- package/dest/msg_validators/tx_validator/fee_payer_balance.d.ts +1 -1
- package/dest/msg_validators/tx_validator/fee_payer_balance.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/fee_payer_balance.js +6 -2
- package/dest/msg_validators/tx_validator/gas_validator.d.ts +67 -3
- package/dest/msg_validators/tx_validator/gas_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/gas_validator.js +104 -37
- package/dest/msg_validators/tx_validator/index.d.ts +3 -1
- package/dest/msg_validators/tx_validator/index.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/index.js +2 -0
- package/dest/msg_validators/tx_validator/metadata_validator.d.ts +1 -1
- package/dest/msg_validators/tx_validator/metadata_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/metadata_validator.js +4 -4
- package/dest/msg_validators/tx_validator/nullifier_cache.d.ts +14 -0
- package/dest/msg_validators/tx_validator/nullifier_cache.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/nullifier_cache.js +24 -0
- package/dest/msg_validators/tx_validator/phases_validator.d.ts +22 -2
- package/dest/msg_validators/tx_validator/phases_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/phases_validator.js +72 -24
- package/dest/services/discv5/discV5_service.d.ts +1 -1
- package/dest/services/discv5/discV5_service.d.ts.map +1 -1
- package/dest/services/discv5/discV5_service.js +4 -2
- package/dest/services/dummy_service.d.ts +2 -3
- package/dest/services/dummy_service.d.ts.map +1 -1
- package/dest/services/dummy_service.js +1 -4
- package/dest/services/encoding.d.ts +6 -2
- package/dest/services/encoding.d.ts.map +1 -1
- package/dest/services/encoding.js +14 -8
- package/dest/services/libp2p/libp2p_service.d.ts +20 -20
- package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
- package/dest/services/libp2p/libp2p_service.js +221 -143
- package/dest/services/peer-manager/metrics.d.ts +3 -1
- package/dest/services/peer-manager/metrics.d.ts.map +1 -1
- package/dest/services/peer-manager/metrics.js +6 -0
- 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 +6 -3
- package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts +11 -8
- package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts.map +1 -1
- package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.js +82 -101
- package/dest/services/reqresp/batch-tx-requester/interface.d.ts +3 -2
- package/dest/services/reqresp/batch-tx-requester/interface.d.ts.map +1 -1
- package/dest/services/reqresp/batch-tx-requester/missing_txs.d.ts +5 -4
- package/dest/services/reqresp/batch-tx-requester/missing_txs.d.ts.map +1 -1
- package/dest/services/reqresp/batch-tx-requester/missing_txs.js +13 -7
- package/dest/services/reqresp/batch-tx-requester/peer_collection.d.ts +19 -11
- package/dest/services/reqresp/batch-tx-requester/peer_collection.d.ts.map +1 -1
- package/dest/services/reqresp/batch-tx-requester/peer_collection.js +52 -15
- package/dest/services/reqresp/batch-tx-requester/tx_validator.js +2 -2
- package/dest/services/reqresp/reqresp.d.ts +1 -1
- package/dest/services/reqresp/reqresp.d.ts.map +1 -1
- package/dest/services/reqresp/reqresp.js +19 -10
- package/dest/services/service.d.ts +8 -2
- package/dest/services/service.d.ts.map +1 -1
- package/dest/services/tx_collection/fast_tx_collection.d.ts +1 -4
- package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
- package/dest/services/tx_collection/fast_tx_collection.js +57 -73
- package/dest/services/tx_collection/proposal_tx_collector.d.ts +6 -7
- package/dest/services/tx_collection/proposal_tx_collector.d.ts.map +1 -1
- package/dest/services/tx_collection/proposal_tx_collector.js +4 -4
- package/dest/services/tx_collection/request_tracker.d.ts +53 -0
- package/dest/services/tx_collection/request_tracker.d.ts.map +1 -0
- package/dest/services/tx_collection/request_tracker.js +84 -0
- package/dest/services/tx_collection/slow_tx_collection.js +1 -1
- package/dest/services/tx_collection/tx_collection.d.ts +3 -6
- package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
- package/dest/services/tx_provider.d.ts +3 -3
- package/dest/services/tx_provider.d.ts.map +1 -1
- package/dest/services/tx_provider.js +4 -4
- package/dest/test-helpers/make-test-p2p-clients.d.ts +5 -6
- package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
- package/dest/test-helpers/make-test-p2p-clients.js +1 -2
- package/dest/test-helpers/mock-pubsub.d.ts +7 -3
- package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
- package/dest/test-helpers/mock-pubsub.js +11 -3
- package/dest/test-helpers/reqresp-nodes.d.ts +2 -3
- package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
- package/dest/test-helpers/reqresp-nodes.js +2 -2
- package/dest/test-helpers/testbench-utils.d.ts +2 -2
- package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
- package/dest/test-helpers/testbench-utils.js +22 -3
- package/dest/testbench/p2p_client_testbench_worker.js +10 -9
- package/dest/testbench/worker_client_manager.d.ts +3 -1
- package/dest/testbench/worker_client_manager.d.ts.map +1 -1
- package/dest/testbench/worker_client_manager.js +6 -2
- package/dest/util.d.ts +9 -4
- package/dest/util.d.ts.map +1 -1
- package/dest/util.js +2 -9
- package/package.json +14 -14
- package/src/client/factory.ts +45 -45
- package/src/client/interface.ts +5 -19
- package/src/client/p2p_client.ts +26 -122
- package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker.ts +5 -8
- package/src/config.ts +125 -43
- package/src/mem_pools/attestation_pool/attestation_pool.ts +8 -7
- package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +6 -6
- package/src/mem_pools/instrumentation.ts +17 -13
- package/src/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.ts +2 -1
- package/src/mem_pools/tx_pool/priority.ts +4 -4
- package/src/mem_pools/tx_pool/tx_pool_test_suite.ts +3 -1
- package/src/mem_pools/tx_pool_v2/README.md +9 -1
- package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.ts +3 -2
- package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.ts +3 -0
- package/src/mem_pools/tx_pool_v2/eviction/interfaces.ts +11 -1
- package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.ts +2 -2
- package/src/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.ts +10 -6
- package/src/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.ts +15 -6
- package/src/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.ts +2 -1
- package/src/mem_pools/tx_pool_v2/index.ts +1 -1
- package/src/mem_pools/tx_pool_v2/interfaces.ts +16 -8
- package/src/mem_pools/tx_pool_v2/tx_metadata.ts +115 -20
- package/src/mem_pools/tx_pool_v2/tx_pool_indices.ts +29 -43
- package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +17 -2
- package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +207 -154
- package/src/msg_validators/attestation_validator/README.md +49 -0
- package/src/msg_validators/attestation_validator/attestation_validator.ts +5 -4
- package/src/msg_validators/clock_tolerance.ts +4 -3
- package/src/msg_validators/proposal_validator/README.md +123 -0
- package/src/msg_validators/proposal_validator/block_proposal_validator.ts +14 -4
- package/src/msg_validators/proposal_validator/checkpoint_proposal_validator.ts +20 -7
- package/src/msg_validators/proposal_validator/proposal_validator.ts +69 -45
- package/src/msg_validators/tx_validator/README.md +119 -0
- package/src/msg_validators/tx_validator/aggregate_tx_validator.ts +3 -3
- package/src/msg_validators/tx_validator/allowed_public_setup.ts +22 -27
- package/src/msg_validators/tx_validator/allowed_setup_helpers.ts +31 -0
- package/src/msg_validators/tx_validator/contract_instance_validator.ts +56 -0
- package/src/msg_validators/tx_validator/data_validator.ts +42 -1
- package/src/msg_validators/tx_validator/factory.ts +394 -78
- package/src/msg_validators/tx_validator/fee_payer_balance.ts +6 -2
- package/src/msg_validators/tx_validator/gas_validator.ts +123 -27
- package/src/msg_validators/tx_validator/index.ts +2 -0
- package/src/msg_validators/tx_validator/metadata_validator.ts +12 -4
- package/src/msg_validators/tx_validator/nullifier_cache.ts +30 -0
- package/src/msg_validators/tx_validator/phases_validator.ts +82 -27
- package/src/services/discv5/discV5_service.ts +4 -2
- package/src/services/dummy_service.ts +1 -5
- package/src/services/encoding.ts +14 -7
- package/src/services/libp2p/libp2p_service.ts +235 -166
- package/src/services/peer-manager/metrics.ts +7 -0
- package/src/services/peer-manager/peer_manager.ts +7 -3
- package/src/services/reqresp/README.md +229 -0
- package/src/services/reqresp/batch-tx-requester/README.md +46 -7
- package/src/services/reqresp/batch-tx-requester/batch_tx_requester.ts +78 -111
- package/src/services/reqresp/batch-tx-requester/interface.ts +2 -1
- package/src/services/reqresp/batch-tx-requester/missing_txs.ts +13 -6
- package/src/services/reqresp/batch-tx-requester/peer_collection.ts +68 -24
- package/src/services/reqresp/batch-tx-requester/tx_validator.ts +2 -2
- package/src/services/reqresp/reqresp.ts +22 -12
- package/src/services/service.ts +8 -1
- package/src/services/tx_collection/fast_tx_collection.ts +57 -83
- package/src/services/tx_collection/proposal_tx_collector.ts +8 -13
- package/src/services/tx_collection/request_tracker.ts +127 -0
- package/src/services/tx_collection/slow_tx_collection.ts +1 -1
- package/src/services/tx_collection/tx_collection.ts +3 -5
- package/src/services/tx_provider.ts +2 -2
- package/src/test-helpers/make-test-p2p-clients.ts +1 -3
- package/src/test-helpers/mock-pubsub.ts +12 -6
- package/src/test-helpers/reqresp-nodes.ts +3 -6
- package/src/test-helpers/testbench-utils.ts +30 -4
- package/src/testbench/p2p_client_testbench_worker.ts +7 -12
- package/src/testbench/worker_client_manager.ts +13 -5
- package/src/util.ts +9 -13
- package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts +0 -23
- package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts.map +0 -1
- package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.js +0 -212
- package/dest/services/tx_collection/missing_txs_tracker.d.ts +0 -32
- package/dest/services/tx_collection/missing_txs_tracker.d.ts.map +0 -1
- package/dest/services/tx_collection/missing_txs_tracker.js +0 -27
- package/src/msg_validators/proposal_validator/proposal_validator_test_suite.ts +0 -230
- package/src/services/tx_collection/missing_txs_tracker.ts +0 -52
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import { chunkWrapAround } from '@aztec/foundation/collection';
|
|
2
|
-
import { TimeoutError } from '@aztec/foundation/error';
|
|
3
2
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
4
3
|
import { FifoMemoryQueue, type ISemaphore, Semaphore } from '@aztec/foundation/queue';
|
|
5
4
|
import { sleep } from '@aztec/foundation/sleep';
|
|
6
|
-
import { DateProvider
|
|
5
|
+
import { DateProvider } from '@aztec/foundation/timer';
|
|
7
6
|
import { PeerErrorSeverity } from '@aztec/stdlib/p2p';
|
|
8
7
|
import { Tx, TxArray, TxHash } from '@aztec/stdlib/tx';
|
|
9
8
|
|
|
10
9
|
import type { PeerId } from '@libp2p/interface';
|
|
11
|
-
import { peerIdFromString } from '@libp2p/peer-id';
|
|
12
10
|
|
|
13
|
-
import type {
|
|
11
|
+
import type { IRequestTracker } from '../../tx_collection/request_tracker.js';
|
|
14
12
|
import { ReqRespSubProtocol } from '.././interface.js';
|
|
15
13
|
import { BlockTxsRequest, BlockTxsResponse, type BlockTxsSource } from '.././protocols/index.js';
|
|
16
14
|
import { ReqRespStatus } from '.././status.js';
|
|
@@ -43,16 +41,14 @@ import { BatchRequestTxValidator, type IBatchRequestTxValidator } from './tx_val
|
|
|
43
41
|
* - Is the peer which was unable to send us successful response N times in a row
|
|
44
42
|
* */
|
|
45
43
|
export class BatchTxRequester {
|
|
44
|
+
private readonly requestTracker: IRequestTracker;
|
|
46
45
|
private readonly blockTxsSource: BlockTxsSource;
|
|
47
46
|
private readonly pinnedPeer: PeerId | undefined;
|
|
48
|
-
private readonly timeoutMs: number;
|
|
49
47
|
private readonly p2pService: BatchTxRequesterLibP2PService;
|
|
50
48
|
private readonly logger: Logger;
|
|
51
|
-
private readonly dateProvider: DateProvider;
|
|
52
49
|
private readonly opts: BatchTxRequesterOptions;
|
|
53
50
|
private readonly peers: IPeerCollection;
|
|
54
51
|
private readonly txsMetadata: ITxMetadataCollection;
|
|
55
|
-
private readonly deadline: number;
|
|
56
52
|
private readonly smartRequesterSemaphore: ISemaphore;
|
|
57
53
|
private readonly txQueue: FifoMemoryQueue<Tx>;
|
|
58
54
|
private readonly txValidator: IBatchRequestTxValidator;
|
|
@@ -61,21 +57,19 @@ export class BatchTxRequester {
|
|
|
61
57
|
private readonly txBatchSize: number;
|
|
62
58
|
|
|
63
59
|
constructor(
|
|
64
|
-
|
|
60
|
+
requestTracker: IRequestTracker,
|
|
65
61
|
blockTxsSource: BlockTxsSource,
|
|
66
62
|
pinnedPeer: PeerId | undefined,
|
|
67
|
-
timeoutMs: number,
|
|
68
63
|
p2pService: BatchTxRequesterLibP2PService,
|
|
69
64
|
logger?: Logger,
|
|
70
65
|
dateProvider?: DateProvider,
|
|
71
66
|
opts?: BatchTxRequesterOptions,
|
|
72
67
|
) {
|
|
68
|
+
this.requestTracker = requestTracker;
|
|
73
69
|
this.blockTxsSource = blockTxsSource;
|
|
74
70
|
this.pinnedPeer = pinnedPeer;
|
|
75
|
-
this.timeoutMs = timeoutMs;
|
|
76
71
|
this.p2pService = p2pService;
|
|
77
72
|
this.logger = logger ?? createLogger('p2p:reqresp_batch');
|
|
78
|
-
this.dateProvider = dateProvider ?? new DateProvider();
|
|
79
73
|
this.opts = opts ?? {};
|
|
80
74
|
|
|
81
75
|
this.smartParallelWorkerCount =
|
|
@@ -83,24 +77,22 @@ export class BatchTxRequester {
|
|
|
83
77
|
this.dumbParallelWorkerCount =
|
|
84
78
|
this.opts.dumbParallelWorkerCount ?? DEFAULT_BATCH_TX_REQUESTER_DUMB_PARALLEL_WORKER_COUNT;
|
|
85
79
|
this.txBatchSize = this.opts.txBatchSize ?? DEFAULT_BATCH_TX_REQUESTER_TX_BATCH_SIZE;
|
|
86
|
-
this.deadline = this.dateProvider.now() + this.timeoutMs;
|
|
87
80
|
this.txQueue = new FifoMemoryQueue(this.logger);
|
|
88
81
|
this.txValidator = this.opts.txValidator ?? new BatchRequestTxValidator(this.p2pService.txValidatorConfig);
|
|
89
82
|
|
|
90
83
|
if (this.opts.peerCollection) {
|
|
91
84
|
this.peers = this.opts.peerCollection;
|
|
92
85
|
} else {
|
|
93
|
-
const initialPeers = this.p2pService.connectionSampler.getPeerListSortedByConnectionCountAsc();
|
|
94
86
|
const badPeerThreshold = this.opts.badPeerThreshold ?? DEFAULT_BATCH_TX_REQUESTER_BAD_PEER_THRESHOLD;
|
|
95
87
|
this.peers = new PeerCollection(
|
|
96
|
-
|
|
88
|
+
this.p2pService.connectionSampler,
|
|
97
89
|
this.pinnedPeer,
|
|
98
|
-
|
|
90
|
+
dateProvider ?? new DateProvider(),
|
|
99
91
|
badPeerThreshold,
|
|
100
92
|
this.p2pService.peerScoring,
|
|
101
93
|
);
|
|
102
94
|
}
|
|
103
|
-
this.txsMetadata = new MissingTxMetadataCollection(
|
|
95
|
+
this.txsMetadata = new MissingTxMetadataCollection(requestTracker, this.txBatchSize);
|
|
104
96
|
this.smartRequesterSemaphore = this.opts.semaphore ?? new Semaphore(0);
|
|
105
97
|
}
|
|
106
98
|
|
|
@@ -108,40 +100,30 @@ export class BatchTxRequester {
|
|
|
108
100
|
* Fetches all missing transactions and yields them one by one
|
|
109
101
|
* */
|
|
110
102
|
public async *run(): AsyncGenerator<Tx, Tx | undefined, unknown> {
|
|
111
|
-
// Our timeout is represented in milliseconds but queue expects seconds
|
|
112
|
-
// We also want to make sure we wait at least 1 second in case of very low timeouts
|
|
113
|
-
const timeoutQueueAfter = Math.max(Math.ceil(this.timeoutMs / 1_000), 1);
|
|
114
103
|
try {
|
|
115
104
|
if (this.txsMetadata.getMissingTxHashes().size === 0) {
|
|
116
105
|
return undefined;
|
|
117
106
|
}
|
|
118
107
|
|
|
119
|
-
// Start workers in background
|
|
120
|
-
const workersPromise =
|
|
121
|
-
|
|
122
|
-
this.
|
|
123
|
-
|
|
108
|
+
// Start workers in background. Workers stop themselves via requestTracker.checkCancelled().
|
|
109
|
+
const workersPromise = Promise.allSettled([
|
|
110
|
+
this.smartRequester(),
|
|
111
|
+
this.dumbRequester(),
|
|
112
|
+
this.pinnedPeerRequester(),
|
|
113
|
+
]).finally(() => {
|
|
124
114
|
this.txQueue.end();
|
|
125
115
|
});
|
|
126
116
|
|
|
117
|
+
// Yield txs as workers put them on the queue. The queue's end() drains remaining items
|
|
118
|
+
// before returning null, so we don't lose any txs.
|
|
127
119
|
while (true) {
|
|
128
|
-
const tx = await this.txQueue.get(
|
|
120
|
+
const tx = await this.txQueue.get();
|
|
129
121
|
|
|
130
|
-
// null indicates that the queue has ended
|
|
131
122
|
if (tx === null) {
|
|
132
123
|
break;
|
|
133
124
|
}
|
|
134
125
|
|
|
135
126
|
yield tx;
|
|
136
|
-
|
|
137
|
-
if (this.shouldStop()) {
|
|
138
|
-
// Drain queue before ending
|
|
139
|
-
let remaining;
|
|
140
|
-
while ((remaining = this.txQueue.getImmediate()) !== undefined) {
|
|
141
|
-
yield remaining;
|
|
142
|
-
}
|
|
143
|
-
break;
|
|
144
|
-
}
|
|
145
127
|
}
|
|
146
128
|
|
|
147
129
|
this.unlockSmartRequesterSemaphores();
|
|
@@ -227,7 +209,6 @@ export class BatchTxRequester {
|
|
|
227
209
|
* Starts dumb worker loops
|
|
228
210
|
* */
|
|
229
211
|
private async dumbRequester() {
|
|
230
|
-
const nextPeerIndex = this.makeRoundRobinIndexer();
|
|
231
212
|
const nextBatchIndex = this.makeRoundRobinIndexer();
|
|
232
213
|
|
|
233
214
|
// Chunk missing tx hashes into batches of txBatchSize, wrapping around to ensure no peer gets less than txBatchSize
|
|
@@ -263,15 +244,9 @@ export class BatchTxRequester {
|
|
|
263
244
|
return { blockRequest, txs };
|
|
264
245
|
};
|
|
265
246
|
|
|
266
|
-
const
|
|
267
|
-
const peers = this.peers.getDumbPeersToQuery();
|
|
268
|
-
const idx = nextPeerIndex(() => peers.length);
|
|
269
|
-
return idx === undefined ? undefined : peerIdFromString(peers[idx]);
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
const workerCount = Math.min(this.dumbParallelWorkerCount, this.peers.getAllPeers().size);
|
|
247
|
+
const workerCount = this.dumbParallelWorkerCount;
|
|
273
248
|
const workers = Array.from({ length: workerCount }, (_, index) =>
|
|
274
|
-
this.dumbWorkerLoop(
|
|
249
|
+
this.dumbWorkerLoop(this.peers.nextDumbPeerToQuery.bind(this.peers), makeRequest, index + 1),
|
|
275
250
|
);
|
|
276
251
|
|
|
277
252
|
await Promise.allSettled(workers);
|
|
@@ -332,14 +307,6 @@ export class BatchTxRequester {
|
|
|
332
307
|
* Starts smart worker loops
|
|
333
308
|
* */
|
|
334
309
|
private async smartRequester() {
|
|
335
|
-
const nextPeerIndex = this.makeRoundRobinIndexer();
|
|
336
|
-
|
|
337
|
-
const nextPeer = () => {
|
|
338
|
-
const peers = this.peers.getSmartPeersToQuery();
|
|
339
|
-
const idx = nextPeerIndex(() => peers.length);
|
|
340
|
-
return idx === undefined ? undefined : peerIdFromString(peers[idx]);
|
|
341
|
-
};
|
|
342
|
-
|
|
343
310
|
const makeRequest = (pid: PeerId) => {
|
|
344
311
|
const txs = this.txsMetadata.getTxsToRequestFromThePeer(pid);
|
|
345
312
|
const blockRequest = BlockTxsRequest.fromTxsSourceAndMissingTxs(this.blockTxsSource, txs);
|
|
@@ -350,9 +317,8 @@ export class BatchTxRequester {
|
|
|
350
317
|
return { blockRequest, txs };
|
|
351
318
|
};
|
|
352
319
|
|
|
353
|
-
const workers = Array.from(
|
|
354
|
-
|
|
355
|
-
(_, index) => this.smartWorkerLoop(nextPeer, makeRequest, index + 1),
|
|
320
|
+
const workers = Array.from({ length: this.smartParallelWorkerCount }, (_, index) =>
|
|
321
|
+
this.smartWorkerLoop(this.peers.nextSmartPeerToQuery.bind(this.peers), makeRequest, index + 1),
|
|
356
322
|
);
|
|
357
323
|
|
|
358
324
|
await Promise.allSettled(workers);
|
|
@@ -378,7 +344,10 @@ export class BatchTxRequester {
|
|
|
378
344
|
) {
|
|
379
345
|
try {
|
|
380
346
|
this.logger.trace(`Smart worker ${workerIndex} started`);
|
|
381
|
-
await
|
|
347
|
+
await Promise.race([this.smartRequesterSemaphore.acquire(), this.requestTracker.cancellationToken]);
|
|
348
|
+
if (this.requestTracker.checkCancelled()) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
382
351
|
this.logger.trace(`Smart worker ${workerIndex} acquired semaphore`);
|
|
383
352
|
|
|
384
353
|
while (!this.shouldStop()) {
|
|
@@ -387,30 +356,25 @@ export class BatchTxRequester {
|
|
|
387
356
|
if (weRanOutOfPeersToQuery) {
|
|
388
357
|
this.logger.debug(`Worker loop smart: No more peers to query`);
|
|
389
358
|
|
|
390
|
-
// If
|
|
391
|
-
|
|
392
|
-
const
|
|
393
|
-
if (
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
const thereAreSomeRateLimitedSmartPeers = nextSmartPeerDelay !== undefined;
|
|
397
|
-
if (thereAreSomeRateLimitedSmartPeers) {
|
|
398
|
-
await this.sleepClampedToDeadline(nextSmartPeerDelay);
|
|
399
|
-
continue;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
this.logger.debug(`Worker loop smart: No more smart peers to query killing ${workerIndex}`);
|
|
403
|
-
break;
|
|
359
|
+
// If we have rate limited peers wait for them.
|
|
360
|
+
const nextSmartPeerDelay = this.peers.getNextSmartPeerAvailabilityDelayMs();
|
|
361
|
+
const thereAreSomeRateLimitedSmartPeers = nextSmartPeerDelay !== undefined;
|
|
362
|
+
if (thereAreSomeRateLimitedSmartPeers) {
|
|
363
|
+
await this.sleepClampedToDeadline(nextSmartPeerDelay);
|
|
364
|
+
continue;
|
|
404
365
|
}
|
|
405
366
|
|
|
406
|
-
// Otherwise there are still some dumb peers that could become smart.
|
|
407
367
|
// We end up here when all known smart peers became temporarily unavailable via combination of
|
|
408
368
|
// (bad, in-flight, or rate-limited) or in some weird scenario all current smart peers turn bad which is permanent
|
|
409
|
-
// but dumb peers
|
|
369
|
+
// but there are dumb peers that could be promoted
|
|
370
|
+
// or new peer can join as dumb and be promoted later
|
|
410
371
|
//
|
|
411
372
|
// When a dumb peer responds with valid txIndices, it gets
|
|
412
373
|
// promoted to smart and releases the semaphore, waking this worker.
|
|
413
|
-
await
|
|
374
|
+
await Promise.race([this.smartRequesterSemaphore.acquire(), this.requestTracker.cancellationToken]);
|
|
375
|
+
if (this.requestTracker.checkCancelled()) {
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
414
378
|
this.logger.debug(`Worker loop smart: acquired next smart peer`);
|
|
415
379
|
continue;
|
|
416
380
|
}
|
|
@@ -437,11 +401,7 @@ export class BatchTxRequester {
|
|
|
437
401
|
});
|
|
438
402
|
}
|
|
439
403
|
} catch (err: any) {
|
|
440
|
-
|
|
441
|
-
this.logger.debug(`Smart worker ${workerIndex} timed out waiting for semaphore`);
|
|
442
|
-
} else {
|
|
443
|
-
this.logger.error(`Smart worker ${workerIndex} encountered an error: ${err}`);
|
|
444
|
-
}
|
|
404
|
+
this.logger.error(`Smart worker ${workerIndex} encountered an error: ${err}`);
|
|
445
405
|
} finally {
|
|
446
406
|
this.logger.debug(`Smart worker ${workerIndex} finished`);
|
|
447
407
|
}
|
|
@@ -489,9 +449,18 @@ export class BatchTxRequester {
|
|
|
489
449
|
* this implies we will query these peers couple of more times and give them a chance to "redeem" themselves before completely ignoring them
|
|
490
450
|
*/
|
|
491
451
|
private handleFailResponseFromPeer(peerId: PeerId, responseStatus: ReqRespStatus) {
|
|
492
|
-
//TODO: Should we ban these peers?
|
|
493
452
|
if (responseStatus === ReqRespStatus.FAILURE || responseStatus === ReqRespStatus.UNKNOWN) {
|
|
494
453
|
this.peers.penalisePeer(peerId, PeerErrorSeverity.HighToleranceError);
|
|
454
|
+
this.peers.markPeerDumb(peerId);
|
|
455
|
+
this.txsMetadata.clearPeerData(peerId);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// NOT_FOUND means the peer pruned its block proposal — it can no longer serve
|
|
460
|
+
// index-based requests, but this is a legitimate state so we don't penalize.
|
|
461
|
+
if (responseStatus === ReqRespStatus.NOT_FOUND) {
|
|
462
|
+
this.peers.markPeerDumb(peerId);
|
|
463
|
+
this.txsMetadata.clearPeerData(peerId);
|
|
495
464
|
return;
|
|
496
465
|
}
|
|
497
466
|
|
|
@@ -545,6 +514,9 @@ export class BatchTxRequester {
|
|
|
545
514
|
});
|
|
546
515
|
|
|
547
516
|
if (hasInvalidTx) {
|
|
517
|
+
this.logger.warn(`Penalizing peer ${peerId.toString()} for sending invalid transactions in batch response`, {
|
|
518
|
+
peerId,
|
|
519
|
+
});
|
|
548
520
|
this.peers.penalisePeer(peerId, PeerErrorSeverity.LowToleranceError);
|
|
549
521
|
} else {
|
|
550
522
|
// If we have received successful response from the peer, they have "redeemed" themselves and not considered bad anymore
|
|
@@ -581,10 +553,9 @@ export class BatchTxRequester {
|
|
|
581
553
|
return;
|
|
582
554
|
}
|
|
583
555
|
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
if (!this.isBlockResponseValid(response)) {
|
|
556
|
+
const hasArchiveRootMismatch = this.blockTxsSource.archive.toString() !== response.archiveRoot.toString();
|
|
557
|
+
if (hasArchiveRootMismatch) {
|
|
558
|
+
this.handleArchiveRootMismatch(peerId, response);
|
|
588
559
|
return;
|
|
589
560
|
}
|
|
590
561
|
|
|
@@ -599,18 +570,28 @@ export class BatchTxRequester {
|
|
|
599
570
|
this.markTxsPeerHas(peerId, response);
|
|
600
571
|
|
|
601
572
|
// Unblock smart workers
|
|
602
|
-
|
|
603
|
-
this.smartRequesterSemaphore.release();
|
|
604
|
-
}
|
|
573
|
+
this.smartRequesterSemaphore.release();
|
|
605
574
|
}
|
|
606
575
|
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
576
|
+
/**
|
|
577
|
+
* Handles an archive root mismatch between local state and peer response.
|
|
578
|
+
*
|
|
579
|
+
* - Response archive is Fr.ZERO (peer pruned proposal, legitimate): marks peer dumb.
|
|
580
|
+
* - Non-zero archive mismatch (malicious response): penalises + marks dumb.
|
|
581
|
+
*/
|
|
582
|
+
private handleArchiveRootMismatch(peerId: PeerId, response: BlockTxsResponse): void {
|
|
583
|
+
if (!response.archiveRoot.isZero()) {
|
|
584
|
+
this.peers.penalisePeer(peerId, PeerErrorSeverity.LowToleranceError);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
this.peers.markPeerDumb(peerId);
|
|
588
|
+
this.txsMetadata.clearPeerData(peerId);
|
|
611
589
|
}
|
|
612
590
|
|
|
613
591
|
private peerHasSomeTxsWeAreMissing(_peerId: PeerId, response: BlockTxsResponse): boolean {
|
|
592
|
+
if (response.txIndices.isEmpty()) {
|
|
593
|
+
return false;
|
|
594
|
+
}
|
|
614
595
|
const txsPeerHas = new Set(this.extractHashesPeerHasFromResponse(response).map(h => h.toString()));
|
|
615
596
|
return this.txsMetadata.getMissingTxHashes().intersection(txsPeerHas).size > 0;
|
|
616
597
|
}
|
|
@@ -659,27 +640,14 @@ export class BatchTxRequester {
|
|
|
659
640
|
}
|
|
660
641
|
|
|
661
642
|
/*
|
|
662
|
-
*
|
|
663
|
-
|
|
664
|
-
return this.txsMetadata.getMissingTxHashes().size == 0;
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
/*
|
|
668
|
-
* Checks if the BatchTxRequester should stop fetching missing txs
|
|
669
|
-
* Conditions for stopping are:
|
|
670
|
-
* - There have been no missing transactions to start with
|
|
671
|
-
* - All transactions have been fetched
|
|
672
|
-
* - The deadline has been hit (no more time to fetch)
|
|
673
|
-
* - This process has been cancelled via abortSignal
|
|
674
|
-
*
|
|
675
|
-
* @returns true if BatchTxRequester should stop, otherwise false*/
|
|
643
|
+
* Checks if the BatchTxRequester should stop fetching missing txs.
|
|
644
|
+
* Delegates to requestTracker which covers: deadline hit, all txs fetched, or external cancellation. */
|
|
676
645
|
private shouldStop() {
|
|
677
|
-
|
|
678
|
-
if (aborted) {
|
|
646
|
+
if (this.requestTracker.checkCancelled()) {
|
|
679
647
|
this.unlockSmartRequesterSemaphores();
|
|
680
648
|
}
|
|
681
649
|
|
|
682
|
-
return
|
|
650
|
+
return this.requestTracker.checkCancelled();
|
|
683
651
|
}
|
|
684
652
|
|
|
685
653
|
/*
|
|
@@ -697,10 +665,9 @@ export class BatchTxRequester {
|
|
|
697
665
|
* This ensures we don't sleep past the deadline.
|
|
698
666
|
* */
|
|
699
667
|
private async sleepClampedToDeadline(durationMs: number) {
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
if (thereIsTimeRemaining) {
|
|
703
|
-
await sleep(Math.min(durationMs, remaining));
|
|
668
|
+
if (this.requestTracker.checkCancelled()) {
|
|
669
|
+
return;
|
|
704
670
|
}
|
|
671
|
+
await Promise.race([sleep(durationMs), this.requestTracker.cancellationToken]);
|
|
705
672
|
}
|
|
706
673
|
}
|
|
@@ -23,6 +23,8 @@ export interface ITxMetadataCollection {
|
|
|
23
23
|
alreadyFetched(txHash: TxHash): boolean;
|
|
24
24
|
// Returns true if tx was marked as fetched, false if it was already marked as fetched
|
|
25
25
|
markPeerHas(peerId: PeerId, txHashes: TxHash[]): void;
|
|
26
|
+
/** Remove all tx metadata associations for a peer (e.g. on demotion from smart to dumb). */
|
|
27
|
+
clearPeerData(peerId: PeerId): void;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
/**
|
|
@@ -47,7 +49,6 @@ export interface BatchTxRequesterOptions {
|
|
|
47
49
|
//Injectable for testing purposes
|
|
48
50
|
semaphore?: ISemaphore;
|
|
49
51
|
peerCollection?: IPeerCollection;
|
|
50
|
-
abortSignal?: AbortSignal;
|
|
51
52
|
/** Optional tx validator for testing - if not provided, one is created from p2pService.txValidatorConfig */
|
|
52
53
|
txValidator?: IBatchRequestTxValidator;
|
|
53
54
|
}
|
|
@@ -2,7 +2,7 @@ import { type Tx, TxHash } from '@aztec/stdlib/tx';
|
|
|
2
2
|
|
|
3
3
|
import type { PeerId } from '@libp2p/interface';
|
|
4
4
|
|
|
5
|
-
import type {
|
|
5
|
+
import type { IRequestTracker } from '../../tx_collection/request_tracker.js';
|
|
6
6
|
import { DEFAULT_BATCH_TX_REQUESTER_TX_BATCH_SIZE } from './config.js';
|
|
7
7
|
import type { ITxMetadataCollection } from './interface.js';
|
|
8
8
|
|
|
@@ -41,10 +41,10 @@ export class MissingTxMetadataCollection implements ITxMetadataCollection {
|
|
|
41
41
|
private txMetadata = new Map<string, MissingTxMetadata>();
|
|
42
42
|
|
|
43
43
|
constructor(
|
|
44
|
-
private
|
|
44
|
+
private requestTracker: IRequestTracker,
|
|
45
45
|
private readonly txBatchSize: number = DEFAULT_BATCH_TX_REQUESTER_TX_BATCH_SIZE,
|
|
46
46
|
) {
|
|
47
|
-
|
|
47
|
+
requestTracker.missingTxHashes.forEach(hash => this.txMetadata.set(hash, new MissingTxMetadata(hash)));
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
public getPrioritizingNotInFlightAndLowerRequestCount(txs: string[]): MissingTxMetadata[] {
|
|
@@ -65,7 +65,7 @@ export class MissingTxMetadataCollection implements ITxMetadataCollection {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
public getMissingTxHashes(): Set<string> {
|
|
68
|
-
return this.
|
|
68
|
+
return this.requestTracker.missingTxHashes;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
public getTxsPeerHas(peer: PeerId): Set<string> {
|
|
@@ -128,7 +128,7 @@ export class MissingTxMetadataCollection implements ITxMetadataCollection {
|
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
public alreadyFetched(txHash: TxHash): boolean {
|
|
131
|
-
return !this.
|
|
131
|
+
return !this.requestTracker.isMissing(txHash.toString());
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
public markFetched(peerId: PeerId, tx: Tx): boolean {
|
|
@@ -144,7 +144,7 @@ export class MissingTxMetadataCollection implements ITxMetadataCollection {
|
|
|
144
144
|
}
|
|
145
145
|
|
|
146
146
|
txMeta.peers.add(peerId.toString());
|
|
147
|
-
return this.
|
|
147
|
+
return this.requestTracker.markFetched(tx);
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
public markPeerHas(peerId: PeerId, txHash: TxHash[]) {
|
|
@@ -158,4 +158,11 @@ export class MissingTxMetadataCollection implements ITxMetadataCollection {
|
|
|
158
158
|
}
|
|
159
159
|
});
|
|
160
160
|
}
|
|
161
|
+
|
|
162
|
+
public clearPeerData(peerId: PeerId) {
|
|
163
|
+
const peerIdStr = peerId.toString();
|
|
164
|
+
for (const txMeta of this.txMetadata.values()) {
|
|
165
|
+
txMeta.peers.delete(peerIdStr);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
161
168
|
}
|
|
@@ -2,18 +2,23 @@ import type { DateProvider } from '@aztec/foundation/timer';
|
|
|
2
2
|
import type { PeerErrorSeverity } from '@aztec/stdlib/p2p';
|
|
3
3
|
|
|
4
4
|
import type { PeerId } from '@libp2p/interface';
|
|
5
|
+
import { peerIdFromString } from '@libp2p/peer-id';
|
|
5
6
|
|
|
7
|
+
import type { ConnectionSampler } from '../connection-sampler/connection_sampler.js';
|
|
6
8
|
import { DEFAULT_BATCH_TX_REQUESTER_BAD_PEER_THRESHOLD } from './config.js';
|
|
7
9
|
import type { IPeerPenalizer } from './interface.js';
|
|
8
10
|
|
|
9
11
|
export const RATE_LIMIT_EXCEEDED_PEER_CACHE_TTL = 1000; // 1s
|
|
10
12
|
|
|
11
13
|
export interface IPeerCollection {
|
|
12
|
-
getAllPeers(): Set<string>;
|
|
13
|
-
getSmartPeers(): Set<string>;
|
|
14
14
|
markPeerSmart(peerId: PeerId): void;
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
markPeerDumb(peerId: PeerId): void;
|
|
16
|
+
|
|
17
|
+
/** Sample next peer in round-robin fashion. No smart peers if returns undefined */
|
|
18
|
+
nextSmartPeerToQuery(): PeerId | undefined;
|
|
19
|
+
/** Sample next peer in round-robin fashion. No dumb peers if returns undefined */
|
|
20
|
+
nextDumbPeerToQuery(): PeerId | undefined;
|
|
21
|
+
|
|
17
22
|
thereAreSomeDumbRatelimitExceededPeers(): boolean;
|
|
18
23
|
penalisePeer(peerId: PeerId, severity: PeerErrorSeverity): void;
|
|
19
24
|
unMarkPeerAsBad(peerId: PeerId): void;
|
|
@@ -28,8 +33,6 @@ export interface IPeerCollection {
|
|
|
28
33
|
}
|
|
29
34
|
|
|
30
35
|
export class PeerCollection implements IPeerCollection {
|
|
31
|
-
private readonly peers;
|
|
32
|
-
|
|
33
36
|
private readonly smartPeers = new Set<string>();
|
|
34
37
|
private readonly inFlightPeers = new Set<string>();
|
|
35
38
|
private readonly rateLimitExceededPeers = new Map<string, number>();
|
|
@@ -37,46 +40,64 @@ export class PeerCollection implements IPeerCollection {
|
|
|
37
40
|
private readonly badPeers = new Set<string>();
|
|
38
41
|
|
|
39
42
|
constructor(
|
|
40
|
-
|
|
43
|
+
private readonly connectionSampler: Pick<ConnectionSampler, 'getPeerListSortedByConnectionCountAsc'>,
|
|
41
44
|
private readonly pinnedPeerId: PeerId | undefined,
|
|
42
45
|
private readonly dateProvider: DateProvider,
|
|
43
46
|
private readonly badPeerThreshold: number = DEFAULT_BATCH_TX_REQUESTER_BAD_PEER_THRESHOLD,
|
|
44
47
|
private readonly peerPenalizer?: IPeerPenalizer,
|
|
45
48
|
) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
// Pinned peer is treaded specially, always mark it as in-flight
|
|
49
|
+
// Pinned peer is treated specially, always mark it as in-flight
|
|
49
50
|
// and never return it as part of smart/dumb peers
|
|
50
51
|
if (this.pinnedPeerId) {
|
|
51
52
|
const peerIdStr = this.pinnedPeerId.toString();
|
|
52
53
|
this.inFlightPeers.add(peerIdStr);
|
|
53
|
-
this.peers.delete(peerIdStr);
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
public
|
|
58
|
-
|
|
57
|
+
public markPeerSmart(peerId: PeerId): void {
|
|
58
|
+
this.smartPeers.add(peerId.toString());
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
public
|
|
62
|
-
|
|
61
|
+
public markPeerDumb(peerId: PeerId): void {
|
|
62
|
+
this.smartPeers.delete(peerId.toString());
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
// We keep track of all peers that are queried for peer sampling algorithm
|
|
66
|
+
private queriedSmartPeers: Set<string> = new Set<string>();
|
|
67
|
+
private queriedDumbPeers: Set<string> = new Set<string>();
|
|
68
|
+
|
|
69
|
+
private static nextPeer(allPeers: Set<string>, queried: Set<string>): PeerId | undefined {
|
|
70
|
+
if (allPeers.size === 0) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
const availablePeers = allPeers.difference(queried);
|
|
74
|
+
let [first] = availablePeers;
|
|
75
|
+
if (first === undefined) {
|
|
76
|
+
// We queried all peers. Start over
|
|
77
|
+
[first] = allPeers;
|
|
78
|
+
queried.clear();
|
|
79
|
+
}
|
|
80
|
+
queried.add(first);
|
|
81
|
+
return peerIdFromString(first);
|
|
67
82
|
}
|
|
68
83
|
|
|
69
|
-
public
|
|
70
|
-
return
|
|
84
|
+
public nextSmartPeerToQuery(): PeerId | undefined {
|
|
85
|
+
return PeerCollection.nextPeer(this.availableSmartPeers, this.queriedSmartPeers);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public nextDumbPeerToQuery(): PeerId | undefined {
|
|
89
|
+
return PeerCollection.nextPeer(this.availableDumbPeers, this.queriedDumbPeers);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private get availableSmartPeers(): Set<string> {
|
|
93
|
+
return this.peers.intersection(
|
|
71
94
|
this.smartPeers.difference(this.getBadPeers().union(this.inFlightPeers).union(this.getRateLimitExceededPeers())),
|
|
72
95
|
);
|
|
73
96
|
}
|
|
74
97
|
|
|
75
|
-
|
|
76
|
-
return
|
|
77
|
-
this.
|
|
78
|
-
this.smartPeers.union(this.getBadPeers()).union(this.inFlightPeers).union(this.getRateLimitExceededPeers()),
|
|
79
|
-
),
|
|
98
|
+
private get availableDumbPeers(): Set<string> {
|
|
99
|
+
return this.peers.difference(
|
|
100
|
+
this.smartPeers.union(this.getBadPeers()).union(this.inFlightPeers).union(this.getRateLimitExceededPeers()),
|
|
80
101
|
);
|
|
81
102
|
}
|
|
82
103
|
|
|
@@ -202,4 +223,27 @@ export class PeerCollection implements IPeerCollection {
|
|
|
202
223
|
|
|
203
224
|
return minExpiry! - now;
|
|
204
225
|
}
|
|
226
|
+
|
|
227
|
+
private orderedPeers: Set<string> = new Set();
|
|
228
|
+
|
|
229
|
+
private get peers(): Set<string> {
|
|
230
|
+
const pinnedStr = this.pinnedPeerId?.toString();
|
|
231
|
+
const currentlyConnected = new Set(
|
|
232
|
+
this.connectionSampler
|
|
233
|
+
.getPeerListSortedByConnectionCountAsc()
|
|
234
|
+
.map(p => p.toString())
|
|
235
|
+
.filter(p => p !== pinnedStr),
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
// Remove disconnected peers, preserving order of the rest.
|
|
239
|
+
this.orderedPeers = this.orderedPeers.intersection(currentlyConnected);
|
|
240
|
+
|
|
241
|
+
// Append newly connected peers at the end (lowest priority).
|
|
242
|
+
for (const peer of currentlyConnected) {
|
|
243
|
+
if (!this.orderedPeers.has(peer)) {
|
|
244
|
+
this.orderedPeers.add(peer);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return this.orderedPeers;
|
|
248
|
+
}
|
|
205
249
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ClientProtocolCircuitVerifier } from '@aztec/stdlib/interfaces/server';
|
|
2
2
|
import { Tx, type TxValidationResult, type TxValidator } from '@aztec/stdlib/tx';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { createTxValidatorForReqResponseReceivedTxs } from '../../../msg_validators/index.js';
|
|
5
5
|
|
|
6
6
|
export interface BatchRequestTxValidatorConfig {
|
|
7
7
|
l1ChainId: number;
|
|
@@ -29,7 +29,7 @@ export class BatchRequestTxValidator implements IBatchRequestTxValidator {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
static createRequestedTxValidator(config: BatchRequestTxValidatorConfig): TxValidator {
|
|
32
|
-
return
|
|
32
|
+
return createTxValidatorForReqResponseReceivedTxs(config.proofVerifier, {
|
|
33
33
|
l1ChainId: config.l1ChainId,
|
|
34
34
|
rollupVersion: config.rollupVersion,
|
|
35
35
|
});
|