@aztec/p2p 0.0.0-test.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/README.md +7 -0
- package/dest/bootstrap/bootstrap.d.ts +38 -0
- package/dest/bootstrap/bootstrap.d.ts.map +1 -0
- package/dest/bootstrap/bootstrap.js +123 -0
- package/dest/client/factory.d.ts +21 -0
- package/dest/client/factory.d.ts.map +1 -0
- package/dest/client/factory.js +37 -0
- package/dest/client/index.d.ts +3 -0
- package/dest/client/index.d.ts.map +1 -0
- package/dest/client/index.js +2 -0
- package/dest/client/p2p_client.d.ts +314 -0
- package/dest/client/p2p_client.d.ts.map +1 -0
- package/dest/client/p2p_client.js +505 -0
- package/dest/config.d.ts +180 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +193 -0
- package/dest/enr/generate-enr.d.ts +9 -0
- package/dest/enr/generate-enr.d.ts.map +1 -0
- package/dest/enr/generate-enr.js +30 -0
- package/dest/enr/index.d.ts +2 -0
- package/dest/enr/index.d.ts.map +1 -0
- package/dest/enr/index.js +1 -0
- 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 +30 -0
- package/dest/index.d.ts +8 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +7 -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 +6 -0
- 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 +195 -0
- 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 +2 -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 +112 -0
- 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 +129 -0
- package/dest/mem_pools/attestation_pool/mocks.d.ts +19 -0
- package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -0
- package/dest/mem_pools/attestation_pool/mocks.js +33 -0
- package/dest/mem_pools/index.d.ts +4 -0
- package/dest/mem_pools/index.d.ts.map +1 -0
- package/dest/mem_pools/index.js +1 -0
- package/dest/mem_pools/instrumentation.d.ts +30 -0
- package/dest/mem_pools/instrumentation.d.ts.map +1 -0
- package/dest/mem_pools/instrumentation.js +84 -0
- package/dest/mem_pools/interface.d.ts +11 -0
- package/dest/mem_pools/interface.d.ts.map +1 -0
- package/dest/mem_pools/interface.js +3 -0
- 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 +245 -0
- 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 +3 -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 +141 -0
- 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 +10 -0
- 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 +3 -0
- 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 +169 -0
- 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 +19 -0
- 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 +21 -0
- 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/index.d.ts +4 -0
- package/dest/msg_validators/index.d.ts.map +1 -0
- package/dest/msg_validators/index.js +3 -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 +31 -0
- 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 +26 -0
- 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 +107 -0
- 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 +41 -0
- 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 +6 -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 +44 -0
- package/dest/msg_validators/tx_validator/tx_proof_validator.d.ts +9 -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 +25 -0
- 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 +188 -0
- package/dest/services/discv5/discV5_service.d.ts +42 -0
- package/dest/services/discv5/discV5_service.d.ts.map +1 -0
- package/dest/services/discv5/discV5_service.js +214 -0
- package/dest/services/dummy_service.d.ts +85 -0
- package/dest/services/dummy_service.d.ts.map +1 -0
- package/dest/services/dummy_service.js +92 -0
- package/dest/services/encoding.d.ts +31 -0
- package/dest/services/encoding.d.ts.map +1 -0
- package/dest/services/encoding.js +66 -0
- package/dest/services/gossipsub/scoring.d.ts +7 -0
- package/dest/services/gossipsub/scoring.d.ts.map +1 -0
- package/dest/services/gossipsub/scoring.js +10 -0
- package/dest/services/index.d.ts +3 -0
- package/dest/services/index.d.ts.map +1 -0
- package/dest/services/index.js +2 -0
- package/dest/services/libp2p/libp2p_service.d.ts +186 -0
- package/dest/services/libp2p/libp2p_service.d.ts.map +1 -0
- package/dest/services/libp2p/libp2p_service.js +712 -0
- 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 +33 -0
- package/dest/services/peer-manager/peer_manager.d.ts +94 -0
- package/dest/services/peer-manager/peer_manager.d.ts.map +1 -0
- package/dest/services/peer-manager/peer_manager.js +445 -0
- 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 +86 -0
- 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 +20 -0
- 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 +88 -0
- 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 +181 -0
- 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 +4 -0
- package/dest/services/reqresp/interface.d.ts +116 -0
- package/dest/services/reqresp/interface.d.ts.map +1 -0
- package/dest/services/reqresp/interface.js +84 -0
- 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 +55 -0
- 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 +8 -0
- 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 +87 -0
- 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 +7 -0
- 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 +7 -0
- 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 +7 -0
- 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 +20 -0
- 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 +184 -0
- 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 +54 -0
- package/dest/services/reqresp/reqresp.d.ts +166 -0
- package/dest/services/reqresp/reqresp.d.ts.map +1 -0
- package/dest/services/reqresp/reqresp.js +516 -0
- package/dest/services/reqresp/status.d.ts +31 -0
- package/dest/services/reqresp/status.d.ts.map +1 -0
- package/dest/services/reqresp/status.js +51 -0
- package/dest/services/service.d.ts +87 -0
- package/dest/services/service.d.ts.map +1 -0
- package/dest/services/service.js +5 -0
- package/dest/test-helpers/generate-peer-id-private-keys.d.ts +7 -0
- package/dest/test-helpers/generate-peer-id-private-keys.d.ts.map +1 -0
- package/dest/test-helpers/generate-peer-id-private-keys.js +13 -0
- package/dest/test-helpers/get-ports.d.ts +7 -0
- package/dest/test-helpers/get-ports.d.ts.map +1 -0
- package/dest/test-helpers/get-ports.js +8 -0
- package/dest/test-helpers/index.d.ts +6 -0
- package/dest/test-helpers/index.d.ts.map +1 -0
- package/dest/test-helpers/index.js +5 -0
- package/dest/test-helpers/make-enrs.d.ts +16 -0
- package/dest/test-helpers/make-enrs.d.ts.map +1 -0
- package/dest/test-helpers/make-enrs.js +32 -0
- package/dest/test-helpers/make-test-p2p-clients.d.ts +36 -0
- package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -0
- package/dest/test-helpers/make-test-p2p-clients.js +68 -0
- package/dest/test-helpers/reqresp-nodes.d.ts +66 -0
- package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -0
- package/dest/test-helpers/reqresp-nodes.js +207 -0
- package/dest/testbench/p2p_client_testbench_worker.d.ts +2 -0
- package/dest/testbench/p2p_client_testbench_worker.d.ts.map +1 -0
- package/dest/testbench/p2p_client_testbench_worker.js +132 -0
- package/dest/testbench/parse_log_file.d.ts +2 -0
- package/dest/testbench/parse_log_file.d.ts.map +1 -0
- package/dest/testbench/parse_log_file.js +131 -0
- package/dest/testbench/testbench.d.ts +2 -0
- package/dest/testbench/testbench.d.ts.map +1 -0
- package/dest/testbench/testbench.js +61 -0
- package/dest/testbench/worker_client_manager.d.ts +56 -0
- package/dest/testbench/worker_client_manager.d.ts.map +1 -0
- package/dest/testbench/worker_client_manager.js +266 -0
- package/dest/types/index.d.ts +32 -0
- package/dest/types/index.d.ts.map +1 -0
- package/dest/types/index.js +28 -0
- package/dest/util.d.ts +53 -0
- package/dest/util.d.ts.map +1 -0
- package/dest/util.js +140 -0
- package/dest/versioning.d.ts +12 -0
- package/dest/versioning.d.ts.map +1 -0
- package/dest/versioning.js +33 -0
- package/package.json +127 -0
- package/src/bootstrap/bootstrap.ts +146 -0
- package/src/client/factory.ts +89 -0
- package/src/client/index.ts +2 -0
- package/src/client/p2p_client.ts +754 -0
- package/src/config.ts +371 -0
- package/src/enr/generate-enr.ts +39 -0
- package/src/enr/index.ts +1 -0
- package/src/errors/reqresp.error.ts +35 -0
- package/src/index.ts +7 -0
- package/src/mem_pools/attestation_pool/attestation_pool.ts +62 -0
- package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +230 -0
- package/src/mem_pools/attestation_pool/index.ts +2 -0
- package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +159 -0
- package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +161 -0
- package/src/mem_pools/attestation_pool/mocks.ts +44 -0
- package/src/mem_pools/index.ts +3 -0
- package/src/mem_pools/instrumentation.ts +126 -0
- package/src/mem_pools/interface.ts +12 -0
- package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +309 -0
- package/src/mem_pools/tx_pool/index.ts +3 -0
- package/src/mem_pools/tx_pool/memory_tx_pool.ts +174 -0
- package/src/mem_pools/tx_pool/priority.ts +13 -0
- package/src/mem_pools/tx_pool/tx_pool.ts +76 -0
- package/src/mem_pools/tx_pool/tx_pool_test_suite.ts +130 -0
- package/src/msg_validators/attestation_validator/attestation_validator.ts +26 -0
- package/src/msg_validators/attestation_validator/index.ts +1 -0
- package/src/msg_validators/block_proposal_validator/block_proposal_validator.ts +29 -0
- package/src/msg_validators/block_proposal_validator/index.ts +1 -0
- package/src/msg_validators/index.ts +3 -0
- package/src/msg_validators/tx_validator/aggregate_tx_validator.ts +32 -0
- package/src/msg_validators/tx_validator/block_header_validator.ts +25 -0
- package/src/msg_validators/tx_validator/data_validator.ts +106 -0
- package/src/msg_validators/tx_validator/double_spend_validator.ts +38 -0
- package/src/msg_validators/tx_validator/index.ts +6 -0
- package/src/msg_validators/tx_validator/metadata_validator.ts +48 -0
- package/src/msg_validators/tx_validator/tx_proof_validator.ts +18 -0
- package/src/services/data_store.ts +235 -0
- package/src/services/discv5/discV5_service.ts +256 -0
- package/src/services/dummy_service.ts +134 -0
- package/src/services/encoding.ts +79 -0
- package/src/services/gossipsub/scoring.ts +13 -0
- package/src/services/index.ts +2 -0
- package/src/services/libp2p/libp2p_service.ts +871 -0
- package/src/services/peer-manager/metrics.ts +41 -0
- package/src/services/peer-manager/peer_manager.ts +530 -0
- package/src/services/peer-manager/peer_scoring.ts +105 -0
- package/src/services/reqresp/config.ts +35 -0
- package/src/services/reqresp/connection-sampler/batch_connection_sampler.ts +94 -0
- package/src/services/reqresp/connection-sampler/connection_sampler.ts +217 -0
- package/src/services/reqresp/index.ts +4 -0
- package/src/services/reqresp/interface.ts +185 -0
- package/src/services/reqresp/metrics.ts +57 -0
- package/src/services/reqresp/protocols/block.ts +15 -0
- package/src/services/reqresp/protocols/goodbye.ts +101 -0
- package/src/services/reqresp/protocols/index.ts +8 -0
- package/src/services/reqresp/protocols/ping.ts +8 -0
- package/src/services/reqresp/protocols/status.ts +8 -0
- package/src/services/reqresp/protocols/tx.ts +29 -0
- package/src/services/reqresp/rate-limiter/index.ts +1 -0
- package/src/services/reqresp/rate-limiter/rate_limiter.ts +228 -0
- package/src/services/reqresp/rate-limiter/rate_limits.ts +55 -0
- package/src/services/reqresp/reqresp.ts +661 -0
- package/src/services/reqresp/status.ts +59 -0
- package/src/services/service.ts +112 -0
- package/src/test-helpers/generate-peer-id-private-keys.ts +15 -0
- package/src/test-helpers/get-ports.ts +8 -0
- package/src/test-helpers/index.ts +5 -0
- package/src/test-helpers/make-enrs.ts +44 -0
- package/src/test-helpers/make-test-p2p-clients.ts +122 -0
- package/src/test-helpers/reqresp-nodes.ts +289 -0
- package/src/testbench/README.md +20 -0
- package/src/testbench/p2p_client_testbench_worker.ts +152 -0
- package/src/testbench/parse_log_file.ts +175 -0
- package/src/testbench/testbench.ts +66 -0
- package/src/testbench/worker_client_manager.ts +318 -0
- package/src/types/index.ts +36 -0
- package/src/util.ts +196 -0
- package/src/versioning.ts +50 -0
|
@@ -0,0 +1,754 @@
|
|
|
1
|
+
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
3
|
+
import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton } from '@aztec/kv-store';
|
|
4
|
+
import type { L2Block, L2BlockId, L2BlockSource, L2BlockStreamEvent, L2Tips } from '@aztec/stdlib/block';
|
|
5
|
+
import type { P2PApi, PeerInfo, ProverCoordination } from '@aztec/stdlib/interfaces/server';
|
|
6
|
+
import type { BlockAttestation, BlockProposal, P2PClientType } from '@aztec/stdlib/p2p';
|
|
7
|
+
import type { Tx, TxHash } from '@aztec/stdlib/tx';
|
|
8
|
+
import {
|
|
9
|
+
Attributes,
|
|
10
|
+
type TelemetryClient,
|
|
11
|
+
TraceableL2BlockStream,
|
|
12
|
+
WithTracer,
|
|
13
|
+
getTelemetryClient,
|
|
14
|
+
trackSpan,
|
|
15
|
+
} from '@aztec/telemetry-client';
|
|
16
|
+
|
|
17
|
+
import type { ENR } from '@chainsafe/enr';
|
|
18
|
+
|
|
19
|
+
import { type P2PConfig, getP2PDefaultConfig } from '../config.js';
|
|
20
|
+
import type { AttestationPool } from '../mem_pools/attestation_pool/attestation_pool.js';
|
|
21
|
+
import type { MemPools } from '../mem_pools/interface.js';
|
|
22
|
+
import type { TxPool } from '../mem_pools/tx_pool/index.js';
|
|
23
|
+
import { ReqRespSubProtocol } from '../services/reqresp/interface.js';
|
|
24
|
+
import type { P2PService } from '../services/service.js';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Enum defining the possible states of the p2p client.
|
|
28
|
+
*/
|
|
29
|
+
export enum P2PClientState {
|
|
30
|
+
IDLE,
|
|
31
|
+
SYNCHING,
|
|
32
|
+
RUNNING,
|
|
33
|
+
STOPPED,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The synchronization status of the P2P client.
|
|
38
|
+
*/
|
|
39
|
+
export interface P2PSyncState {
|
|
40
|
+
/**
|
|
41
|
+
* The current state of the p2p client.
|
|
42
|
+
*/
|
|
43
|
+
state: P2PClientState;
|
|
44
|
+
/**
|
|
45
|
+
* The block number that the p2p client is synced to.
|
|
46
|
+
*/
|
|
47
|
+
syncedToL2Block: L2BlockId;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Interface of a P2P client.
|
|
52
|
+
**/
|
|
53
|
+
export type P2P<T extends P2PClientType = P2PClientType.Full> = ProverCoordination &
|
|
54
|
+
P2PApi<T> & {
|
|
55
|
+
/**
|
|
56
|
+
* Broadcasts a block proposal to other peers.
|
|
57
|
+
*
|
|
58
|
+
* @param proposal - the block proposal
|
|
59
|
+
*/
|
|
60
|
+
broadcastProposal(proposal: BlockProposal): void;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Registers a callback from the validator client that determines how to behave when
|
|
64
|
+
* foreign block proposals are received
|
|
65
|
+
*
|
|
66
|
+
* @param handler - A function taking a received block proposal and producing an attestation
|
|
67
|
+
*/
|
|
68
|
+
// REVIEW: https://github.com/AztecProtocol/aztec-packages/issues/7963
|
|
69
|
+
// ^ This pattern is not my favorite (md)
|
|
70
|
+
registerBlockProposalHandler(handler: (block: BlockProposal) => Promise<BlockAttestation | undefined>): void;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Request a list of transactions from another peer by their tx hashes.
|
|
74
|
+
* @param txHashes - Hashes of the txs to query.
|
|
75
|
+
* @returns A list of transactions or undefined if the transactions are not found.
|
|
76
|
+
*/
|
|
77
|
+
requestTxs(txHashes: TxHash[]): Promise<(Tx | undefined)[]>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Request a transaction from another peer by its tx hash.
|
|
81
|
+
* @param txHash - Hash of the tx to query.
|
|
82
|
+
*/
|
|
83
|
+
requestTxByHash(txHash: TxHash): Promise<Tx | undefined>;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Verifies the 'tx' and, if valid, adds it to local tx pool and forwards it to other peers.
|
|
87
|
+
* @param tx - The transaction.
|
|
88
|
+
**/
|
|
89
|
+
sendTx(tx: Tx): Promise<void>;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Deletes 'txs' from the pool, given hashes.
|
|
93
|
+
* NOT used if we use sendTx as reconcileTxPool will handle this.
|
|
94
|
+
* @param txHashes - Hashes to check.
|
|
95
|
+
**/
|
|
96
|
+
deleteTxs(txHashes: TxHash[]): Promise<void>;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Returns a transaction in the transaction pool by its hash.
|
|
100
|
+
* @param txHash - Hash of tx to return.
|
|
101
|
+
* @returns A single tx or undefined.
|
|
102
|
+
*/
|
|
103
|
+
getTxByHashFromPool(txHash: TxHash): Promise<Tx | undefined>;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Returns a transaction in the transaction pool by its hash, requesting it from the network if it is not found.
|
|
107
|
+
* @param txHash - Hash of tx to return.
|
|
108
|
+
* @returns A single tx or undefined.
|
|
109
|
+
*/
|
|
110
|
+
getTxByHash(txHash: TxHash): Promise<Tx | undefined>;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Returns an archived transaction from the transaction pool by its hash.
|
|
114
|
+
* @param txHash - Hash of tx to return.
|
|
115
|
+
* @returns A single tx or undefined.
|
|
116
|
+
*/
|
|
117
|
+
getArchivedTxByHash(txHash: TxHash): Promise<Tx | undefined>;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Returns whether the given tx hash is flagged as pending or mined.
|
|
121
|
+
* @param txHash - Hash of the tx to query.
|
|
122
|
+
* @returns Pending or mined depending on its status, or undefined if not found.
|
|
123
|
+
*/
|
|
124
|
+
getTxStatus(txHash: TxHash): Promise<'pending' | 'mined' | undefined>;
|
|
125
|
+
|
|
126
|
+
/** Returns an iterator over pending txs on the mempool. */
|
|
127
|
+
iteratePendingTxs(): AsyncIterableIterator<Tx>;
|
|
128
|
+
|
|
129
|
+
/** Returns the number of pending txs in the mempool. */
|
|
130
|
+
getPendingTxCount(): Promise<number>;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Starts the p2p client.
|
|
134
|
+
* @returns A promise signalling the completion of the block sync.
|
|
135
|
+
*/
|
|
136
|
+
start(): Promise<void>;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Stops the p2p client.
|
|
140
|
+
* @returns A promise signalling the completion of the stop process.
|
|
141
|
+
*/
|
|
142
|
+
stop(): Promise<void>;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Indicates if the p2p client is ready for transaction submission.
|
|
146
|
+
* @returns A boolean flag indicating readiness.
|
|
147
|
+
*/
|
|
148
|
+
isReady(): boolean;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Returns the current status of the p2p client.
|
|
152
|
+
*/
|
|
153
|
+
getStatus(): Promise<P2PSyncState>;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Returns the ENR of this node, if any.
|
|
157
|
+
*/
|
|
158
|
+
getEnr(): ENR | undefined;
|
|
159
|
+
|
|
160
|
+
/** Identifies a p2p client. */
|
|
161
|
+
isP2PClient(): true;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The P2P client implementation.
|
|
166
|
+
*/
|
|
167
|
+
export class P2PClient<T extends P2PClientType = P2PClientType.Full>
|
|
168
|
+
extends WithTracer
|
|
169
|
+
implements P2P, P2P<P2PClientType.Prover>
|
|
170
|
+
{
|
|
171
|
+
/** The JS promise that will be running to keep the client's data in sync. Can be interrupted if the client is stopped. */
|
|
172
|
+
private runningPromise!: Promise<void>;
|
|
173
|
+
|
|
174
|
+
private currentState = P2PClientState.IDLE;
|
|
175
|
+
private syncPromise = Promise.resolve();
|
|
176
|
+
private syncResolve?: () => void = undefined;
|
|
177
|
+
private latestBlockNumberAtStart = -1;
|
|
178
|
+
private provenBlockNumberAtStart = -1;
|
|
179
|
+
|
|
180
|
+
private synchedBlockHashes: AztecAsyncMap<number, string>;
|
|
181
|
+
private synchedLatestBlockNumber: AztecAsyncSingleton<number>;
|
|
182
|
+
private synchedProvenBlockNumber: AztecAsyncSingleton<number>;
|
|
183
|
+
|
|
184
|
+
private txPool: TxPool;
|
|
185
|
+
private attestationPool: T extends P2PClientType.Full ? AttestationPool : undefined;
|
|
186
|
+
|
|
187
|
+
/** How many slots to keep attestations for. */
|
|
188
|
+
private keepAttestationsInPoolFor: number;
|
|
189
|
+
/** How many slots to keep proven txs for. */
|
|
190
|
+
private keepProvenTxsFor: number;
|
|
191
|
+
|
|
192
|
+
private blockStream;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* In-memory P2P client constructor.
|
|
196
|
+
* @param store - The client's instance of the KV store.
|
|
197
|
+
* @param l2BlockSource - P2P client's source for fetching existing blocks.
|
|
198
|
+
* @param txPool - The client's instance of a transaction pool. Defaults to in-memory implementation.
|
|
199
|
+
* @param p2pService - The concrete instance of p2p networking to use.
|
|
200
|
+
* @param keepProvenTxsFor - How many blocks have to pass after a block is proven before its txs are deleted (zero to delete immediately once proven).
|
|
201
|
+
* @param log - A logger.
|
|
202
|
+
*/
|
|
203
|
+
constructor(
|
|
204
|
+
_clientType: T,
|
|
205
|
+
store: AztecAsyncKVStore,
|
|
206
|
+
private l2BlockSource: L2BlockSource,
|
|
207
|
+
mempools: MemPools<T>,
|
|
208
|
+
private p2pService: P2PService,
|
|
209
|
+
config: Partial<P2PConfig> = {},
|
|
210
|
+
telemetry: TelemetryClient = getTelemetryClient(),
|
|
211
|
+
private log = createLogger('p2p'),
|
|
212
|
+
) {
|
|
213
|
+
super(telemetry, 'P2PClient');
|
|
214
|
+
|
|
215
|
+
const { keepProvenTxsInPoolFor, blockCheckIntervalMS, blockRequestBatchSize, keepAttestationsInPoolFor } = {
|
|
216
|
+
...getP2PDefaultConfig(),
|
|
217
|
+
...config,
|
|
218
|
+
};
|
|
219
|
+
this.keepProvenTxsFor = keepProvenTxsInPoolFor;
|
|
220
|
+
this.keepAttestationsInPoolFor = keepAttestationsInPoolFor;
|
|
221
|
+
|
|
222
|
+
const tracer = telemetry.getTracer('P2PL2BlockStream');
|
|
223
|
+
const logger = createLogger('p2p:l2-block-stream');
|
|
224
|
+
this.blockStream = new TraceableL2BlockStream(l2BlockSource, this, this, tracer, 'P2PL2BlockStream', logger, {
|
|
225
|
+
batchSize: blockRequestBatchSize,
|
|
226
|
+
pollIntervalMS: blockCheckIntervalMS,
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
this.synchedBlockHashes = store.openMap('p2p_pool_block_hashes');
|
|
230
|
+
this.synchedLatestBlockNumber = store.openSingleton('p2p_pool_last_l2_block');
|
|
231
|
+
this.synchedProvenBlockNumber = store.openSingleton('p2p_pool_last_proven_l2_block');
|
|
232
|
+
|
|
233
|
+
this.txPool = mempools.txPool;
|
|
234
|
+
this.attestationPool = mempools.attestationPool!;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
public isP2PClient(): true {
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
public getPeers(includePending?: boolean): Promise<PeerInfo[]> {
|
|
242
|
+
return Promise.resolve(this.p2pService.getPeers(includePending));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
public getL2BlockHash(number: number): Promise<string | undefined> {
|
|
246
|
+
return this.synchedBlockHashes.getAsync(number);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
public async getL2Tips(): Promise<L2Tips> {
|
|
250
|
+
const latestBlockNumber = await this.getSyncedLatestBlockNum();
|
|
251
|
+
let latestBlockHash: string | undefined;
|
|
252
|
+
const provenBlockNumber = await this.getSyncedProvenBlockNum();
|
|
253
|
+
let provenBlockHash: string | undefined;
|
|
254
|
+
|
|
255
|
+
if (latestBlockNumber > 0) {
|
|
256
|
+
latestBlockHash = await this.synchedBlockHashes.getAsync(latestBlockNumber);
|
|
257
|
+
if (typeof latestBlockHash === 'undefined') {
|
|
258
|
+
this.log.warn(`Block hash for latest block ${latestBlockNumber} not found`);
|
|
259
|
+
throw new Error();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (provenBlockNumber > 0) {
|
|
264
|
+
provenBlockHash = await this.synchedBlockHashes.getAsync(provenBlockNumber);
|
|
265
|
+
if (typeof provenBlockHash === 'undefined') {
|
|
266
|
+
this.log.warn(`Block hash for proven block ${provenBlockNumber} not found`);
|
|
267
|
+
throw new Error();
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return Promise.resolve({
|
|
272
|
+
latest: { hash: latestBlockHash!, number: latestBlockNumber },
|
|
273
|
+
proven: { hash: provenBlockHash!, number: provenBlockNumber },
|
|
274
|
+
finalized: { hash: provenBlockHash!, number: provenBlockNumber },
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
|
|
279
|
+
this.log.debug(`Handling block stream event ${event.type}`);
|
|
280
|
+
switch (event.type) {
|
|
281
|
+
case 'blocks-added':
|
|
282
|
+
await this.handleLatestL2Blocks(event.blocks);
|
|
283
|
+
break;
|
|
284
|
+
case 'chain-finalized':
|
|
285
|
+
// TODO (alexg): I think we can prune the block hashes map here
|
|
286
|
+
break;
|
|
287
|
+
case 'chain-proven': {
|
|
288
|
+
const from = (await this.getSyncedProvenBlockNum()) + 1;
|
|
289
|
+
const limit = event.blockNumber - from + 1;
|
|
290
|
+
await this.handleProvenL2Blocks(await this.l2BlockSource.getBlocks(from, limit));
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
case 'chain-pruned':
|
|
294
|
+
await this.handlePruneL2Blocks(event.blockNumber);
|
|
295
|
+
break;
|
|
296
|
+
default: {
|
|
297
|
+
const _: never = event;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
#assertIsReady() {
|
|
304
|
+
// this.log.info('Checking if p2p client is ready, current state: ', this.currentState);
|
|
305
|
+
if (!this.isReady()) {
|
|
306
|
+
throw new Error('P2P client not ready');
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Starts the P2P client.
|
|
312
|
+
* @returns An empty promise signalling the synching process.
|
|
313
|
+
*/
|
|
314
|
+
public async start() {
|
|
315
|
+
if (this.currentState === P2PClientState.STOPPED) {
|
|
316
|
+
throw new Error('P2P client already stopped');
|
|
317
|
+
}
|
|
318
|
+
if (this.currentState !== P2PClientState.IDLE) {
|
|
319
|
+
return this.syncPromise;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// get the current latest block numbers
|
|
323
|
+
this.latestBlockNumberAtStart = await this.l2BlockSource.getBlockNumber();
|
|
324
|
+
this.provenBlockNumberAtStart = await this.l2BlockSource.getProvenBlockNumber();
|
|
325
|
+
|
|
326
|
+
const syncedLatestBlock = (await this.getSyncedLatestBlockNum()) + 1;
|
|
327
|
+
const syncedProvenBlock = (await this.getSyncedProvenBlockNum()) + 1;
|
|
328
|
+
|
|
329
|
+
// if there are blocks to be retrieved, go to a synching state
|
|
330
|
+
if (syncedLatestBlock <= this.latestBlockNumberAtStart || syncedProvenBlock <= this.provenBlockNumberAtStart) {
|
|
331
|
+
this.setCurrentState(P2PClientState.SYNCHING);
|
|
332
|
+
this.syncPromise = new Promise(resolve => {
|
|
333
|
+
this.syncResolve = resolve;
|
|
334
|
+
});
|
|
335
|
+
this.log.verbose(`Starting sync from ${syncedLatestBlock} (last proven ${syncedProvenBlock})`);
|
|
336
|
+
} else {
|
|
337
|
+
// if no blocks to be retrieved, go straight to running
|
|
338
|
+
this.setCurrentState(P2PClientState.RUNNING);
|
|
339
|
+
this.syncPromise = Promise.resolve();
|
|
340
|
+
await this.p2pService.start();
|
|
341
|
+
this.log.debug(`Block ${syncedLatestBlock} (proven ${syncedProvenBlock}) already beyond current block`);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
this.blockStream.start();
|
|
345
|
+
this.log.verbose(`Started block downloader from block ${syncedLatestBlock}`);
|
|
346
|
+
|
|
347
|
+
return this.syncPromise;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Allows consumers to stop the instance of the P2P client.
|
|
352
|
+
* 'ready' will now return 'false' and the running promise that keeps the client synced is interrupted.
|
|
353
|
+
*/
|
|
354
|
+
public async stop() {
|
|
355
|
+
this.log.debug('Stopping p2p client...');
|
|
356
|
+
await this.p2pService.stop();
|
|
357
|
+
this.log.debug('Stopped p2p service');
|
|
358
|
+
await this.blockStream.stop();
|
|
359
|
+
this.log.debug('Stopped block downloader');
|
|
360
|
+
await this.runningPromise;
|
|
361
|
+
this.setCurrentState(P2PClientState.STOPPED);
|
|
362
|
+
this.log.info('P2P client stopped.');
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
@trackSpan('p2pClient.broadcastProposal', async proposal => ({
|
|
366
|
+
[Attributes.BLOCK_NUMBER]: proposal.blockNumber.toNumber(),
|
|
367
|
+
[Attributes.SLOT_NUMBER]: proposal.slotNumber.toNumber(),
|
|
368
|
+
[Attributes.BLOCK_ARCHIVE]: proposal.archive.toString(),
|
|
369
|
+
[Attributes.P2P_ID]: (await proposal.p2pMessageIdentifier()).toString(),
|
|
370
|
+
}))
|
|
371
|
+
public broadcastProposal(proposal: BlockProposal): void {
|
|
372
|
+
this.log.verbose(`Broadcasting proposal for slot ${proposal.slotNumber.toNumber()} to peers`);
|
|
373
|
+
return this.p2pService.propagate(proposal);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
public async getAttestationsForSlot(slot: bigint, proposalId: string): Promise<BlockAttestation[]> {
|
|
377
|
+
return (await this.attestationPool?.getAttestationsForSlot(slot, proposalId)) ?? [];
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// REVIEW: https://github.com/AztecProtocol/aztec-packages/issues/7963
|
|
381
|
+
// ^ This pattern is not my favorite (md)
|
|
382
|
+
public registerBlockProposalHandler(handler: (block: BlockProposal) => Promise<BlockAttestation | undefined>): void {
|
|
383
|
+
this.p2pService.registerBlockReceivedCallback(handler);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Requests the transactions with the given hashes from the network.
|
|
388
|
+
*
|
|
389
|
+
* If a transaction can be retrieved, it will be returned, if not an undefined
|
|
390
|
+
* will be returned. In place.
|
|
391
|
+
*
|
|
392
|
+
* @param txHashes - The hashes of the transactions to request.
|
|
393
|
+
* @returns A promise that resolves to an array of transactions or undefined.
|
|
394
|
+
*/
|
|
395
|
+
public async requestTxs(txHashes: TxHash[]): Promise<(Tx | undefined)[]> {
|
|
396
|
+
const res = await this.p2pService.sendBatchRequest(ReqRespSubProtocol.TX, txHashes);
|
|
397
|
+
return Promise.resolve(res ?? []);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Uses the Request Response protocol to request a transaction from the network.
|
|
402
|
+
*
|
|
403
|
+
* If the underlying request response protocol fails, then we return undefined.
|
|
404
|
+
* If it succeeds then we add the transaction to our transaction pool and return.
|
|
405
|
+
*
|
|
406
|
+
* @param txHash - The hash of the transaction to request.
|
|
407
|
+
* @returns A promise that resolves to a transaction or undefined.
|
|
408
|
+
*/
|
|
409
|
+
public async requestTxByHash(txHash: TxHash): Promise<Tx | undefined> {
|
|
410
|
+
const tx = await this.p2pService.sendRequest(ReqRespSubProtocol.TX, txHash);
|
|
411
|
+
|
|
412
|
+
if (tx) {
|
|
413
|
+
this.log.debug(`Received tx ${txHash.toString()} from peer`);
|
|
414
|
+
await this.txPool.addTxs([tx]);
|
|
415
|
+
} else {
|
|
416
|
+
this.log.debug(`Failed to receive tx ${txHash.toString()} from peer`);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return tx;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Uses the batched Request Response protocol to request a set of transactions from the network.
|
|
424
|
+
*/
|
|
425
|
+
public async requestTxsByHash(txHashes: TxHash[]): Promise<Tx[]> {
|
|
426
|
+
const txs = (await this.p2pService.sendBatchRequest(ReqRespSubProtocol.TX, txHashes)) ?? [];
|
|
427
|
+
await this.txPool.addTxs(txs);
|
|
428
|
+
const txHashesStr = txHashes.map(tx => tx.toString()).join(', ');
|
|
429
|
+
this.log.debug(`Received batched txs ${txHashesStr} (${txs.length} / ${txHashes.length}}) from peers`);
|
|
430
|
+
return txs as Tx[];
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
public getPendingTxs(): Promise<Tx[]> {
|
|
434
|
+
return Promise.resolve(this.getTxs('pending'));
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
public async getPendingTxCount(): Promise<number> {
|
|
438
|
+
const pendingTxs = await this.txPool.getPendingTxHashes();
|
|
439
|
+
return pendingTxs.length;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
public async *iteratePendingTxs(): AsyncIterableIterator<Tx> {
|
|
443
|
+
for (const txHash of await this.txPool.getPendingTxHashes()) {
|
|
444
|
+
const tx = await this.txPool.getTxByHash(txHash);
|
|
445
|
+
if (tx) {
|
|
446
|
+
yield tx;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Returns all transactions in the transaction pool.
|
|
453
|
+
* @returns An array of Txs.
|
|
454
|
+
*/
|
|
455
|
+
public async getTxs(filter: 'all' | 'pending' | 'mined'): Promise<Tx[]> {
|
|
456
|
+
if (filter === 'all') {
|
|
457
|
+
return this.txPool.getAllTxs();
|
|
458
|
+
} else if (filter === 'mined') {
|
|
459
|
+
const minedHashes = await this.txPool.getMinedTxHashes();
|
|
460
|
+
const minedTx = await Promise.all(minedHashes.map(([txHash]) => this.txPool.getTxByHash(txHash)));
|
|
461
|
+
return minedTx.filter((tx): tx is Tx => !!tx);
|
|
462
|
+
} else if (filter === 'pending') {
|
|
463
|
+
const pendingHashses = await this.txPool.getPendingTxHashes();
|
|
464
|
+
const pendingTxs = await Promise.all(pendingHashses.map(txHash => this.txPool.getTxByHash(txHash)));
|
|
465
|
+
return pendingTxs.filter((tx): tx is Tx => !!tx);
|
|
466
|
+
} else {
|
|
467
|
+
const _: never = filter;
|
|
468
|
+
throw new Error(`Unknown filter ${filter}`);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Returns a transaction in the transaction pool by its hash.
|
|
474
|
+
* @param txHash - Hash of the transaction to look for in the pool.
|
|
475
|
+
* @returns A single tx or undefined.
|
|
476
|
+
*/
|
|
477
|
+
getTxByHashFromPool(txHash: TxHash): Promise<Tx | undefined> {
|
|
478
|
+
return this.txPool.getTxByHash(txHash);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Returns a transaction in the transaction pool by its hash.
|
|
483
|
+
* If the transaction is not in the pool, it will be requested from the network.
|
|
484
|
+
* @param txHash - Hash of the transaction to look for in the pool.
|
|
485
|
+
* @returns A single tx or undefined.
|
|
486
|
+
*/
|
|
487
|
+
async getTxByHash(txHash: TxHash): Promise<Tx | undefined> {
|
|
488
|
+
const tx = await this.txPool.getTxByHash(txHash);
|
|
489
|
+
if (tx) {
|
|
490
|
+
return tx;
|
|
491
|
+
}
|
|
492
|
+
return this.requestTxByHash(txHash);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Returns transactions in the transaction pool by hash.
|
|
497
|
+
* If a transaction is not in the pool, it will be requested from the network.
|
|
498
|
+
* @param txHashes - Hashes of the transactions to look for.
|
|
499
|
+
* @returns The txs found, not necessarily on the same order as the hashes.
|
|
500
|
+
*/
|
|
501
|
+
async getTxsByHash(txHashes: TxHash[]): Promise<Tx[]> {
|
|
502
|
+
const txs = await Promise.all(txHashes.map(txHash => this.txPool.getTxByHash(txHash)));
|
|
503
|
+
const missingTxHashes = txs
|
|
504
|
+
.map((tx, index) => [tx, index] as const)
|
|
505
|
+
.filter(([tx, _index]) => !tx)
|
|
506
|
+
.map(([_tx, index]) => txHashes[index]);
|
|
507
|
+
|
|
508
|
+
if (missingTxHashes.length === 0) {
|
|
509
|
+
return txs as Tx[];
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
const missingTxs = await this.requestTxsByHash(missingTxHashes);
|
|
513
|
+
return txs.filter((tx): tx is Tx => !!tx).concat(missingTxs);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Returns an archived transaction in the transaction pool by its hash.
|
|
518
|
+
* @param txHash - Hash of the archived transaction to look for.
|
|
519
|
+
* @returns A single tx or undefined.
|
|
520
|
+
*/
|
|
521
|
+
getArchivedTxByHash(txHash: TxHash): Promise<Tx | undefined> {
|
|
522
|
+
return this.txPool.getArchivedTxByHash(txHash);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Verifies the 'tx' and, if valid, adds it to local tx pool and forwards it to other peers.
|
|
527
|
+
* @param tx - The tx to verify.
|
|
528
|
+
* @returns Empty promise.
|
|
529
|
+
**/
|
|
530
|
+
public async sendTx(tx: Tx): Promise<void> {
|
|
531
|
+
this.#assertIsReady();
|
|
532
|
+
await this.txPool.addTxs([tx]);
|
|
533
|
+
this.p2pService.propagate(tx);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Returns whether the given tx hash is flagged as pending or mined.
|
|
538
|
+
* @param txHash - Hash of the tx to query.
|
|
539
|
+
* @returns Pending or mined depending on its status, or undefined if not found.
|
|
540
|
+
*/
|
|
541
|
+
public getTxStatus(txHash: TxHash): Promise<'pending' | 'mined' | undefined> {
|
|
542
|
+
return this.txPool.getTxStatus(txHash);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
public getEnr(): ENR | undefined {
|
|
546
|
+
return this.p2pService.getEnr();
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
public getEncodedEnr(): Promise<string | undefined> {
|
|
550
|
+
return Promise.resolve(this.p2pService.getEnr()?.encodeTxt());
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Deletes the 'txs' from the pool.
|
|
555
|
+
* NOT used if we use sendTx as reconcileTxPool will handle this.
|
|
556
|
+
* @param txHashes - Hashes of the transactions to delete.
|
|
557
|
+
* @returns Empty promise.
|
|
558
|
+
**/
|
|
559
|
+
public async deleteTxs(txHashes: TxHash[]): Promise<void> {
|
|
560
|
+
this.#assertIsReady();
|
|
561
|
+
await this.txPool.deleteTxs(txHashes);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Public function to check if the p2p client is fully synced and ready to receive txs.
|
|
566
|
+
* @returns True if the P2P client is ready to receive txs.
|
|
567
|
+
*/
|
|
568
|
+
public isReady() {
|
|
569
|
+
return this.currentState === P2PClientState.RUNNING;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Public function to check the latest block number that the P2P client is synced to.
|
|
574
|
+
* @returns Block number of latest L2 Block we've synced with.
|
|
575
|
+
*/
|
|
576
|
+
public async getSyncedLatestBlockNum(): Promise<number> {
|
|
577
|
+
return (await this.synchedLatestBlockNumber.getAsync()) ?? INITIAL_L2_BLOCK_NUM - 1;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Public function to check the latest proven block number that the P2P client is synced to.
|
|
582
|
+
* @returns Block number of latest proven L2 Block we've synced with.
|
|
583
|
+
*/
|
|
584
|
+
public async getSyncedProvenBlockNum(): Promise<number> {
|
|
585
|
+
return (await this.synchedProvenBlockNumber.getAsync()) ?? INITIAL_L2_BLOCK_NUM - 1;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* Method to check the status the p2p client.
|
|
590
|
+
* @returns Information about p2p client status: state & syncedToBlockNum.
|
|
591
|
+
*/
|
|
592
|
+
public async getStatus(): Promise<P2PSyncState> {
|
|
593
|
+
const blockNumber = await this.getSyncedLatestBlockNum();
|
|
594
|
+
const blockHash =
|
|
595
|
+
blockNumber === 0
|
|
596
|
+
? ''
|
|
597
|
+
: await this.l2BlockSource
|
|
598
|
+
.getBlockHeader(blockNumber)
|
|
599
|
+
.then(header => header?.hash())
|
|
600
|
+
.then(hash => hash?.toString());
|
|
601
|
+
|
|
602
|
+
return {
|
|
603
|
+
state: this.currentState,
|
|
604
|
+
syncedToL2Block: { number: blockNumber, hash: blockHash },
|
|
605
|
+
} as P2PSyncState;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Mark all txs from these blocks as mined.
|
|
610
|
+
* @param blocks - A list of existing blocks with txs that the P2P client needs to ensure the tx pool is reconciled with.
|
|
611
|
+
* @returns Empty promise.
|
|
612
|
+
*/
|
|
613
|
+
private async markTxsAsMinedFromBlocks(blocks: L2Block[]): Promise<void> {
|
|
614
|
+
for (const block of blocks) {
|
|
615
|
+
const txHashes = block.body.txEffects.map(txEffect => txEffect.txHash);
|
|
616
|
+
await this.txPool.markAsMined(txHashes, block.number);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* Deletes txs from these blocks.
|
|
622
|
+
* @param blocks - A list of existing blocks with txs that the P2P client needs to ensure the tx pool is reconciled with.
|
|
623
|
+
* @returns Empty promise.
|
|
624
|
+
*/
|
|
625
|
+
private async deleteTxsFromBlocks(blocks: L2Block[]): Promise<void> {
|
|
626
|
+
this.log.debug(`Deleting txs from blocks ${blocks[0].number} to ${blocks[blocks.length - 1].number}`);
|
|
627
|
+
for (const block of blocks) {
|
|
628
|
+
const txHashes = block.body.txEffects.map(txEffect => txEffect.txHash);
|
|
629
|
+
await this.txPool.deleteTxs(txHashes);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* Handles new mined blocks by marking the txs in them as mined.
|
|
635
|
+
* @param blocks - A list of existing blocks with txs that the P2P client needs to ensure the tx pool is reconciled with.
|
|
636
|
+
* @returns Empty promise.
|
|
637
|
+
*/
|
|
638
|
+
private async handleLatestL2Blocks(blocks: L2Block[]): Promise<void> {
|
|
639
|
+
if (!blocks.length) {
|
|
640
|
+
return Promise.resolve();
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
await this.markTxsAsMinedFromBlocks(blocks);
|
|
644
|
+
const lastBlockNum = blocks[blocks.length - 1].number;
|
|
645
|
+
await Promise.all(
|
|
646
|
+
blocks.map(async block => this.synchedBlockHashes.set(block.number, (await block.hash()).toString())),
|
|
647
|
+
);
|
|
648
|
+
await this.synchedLatestBlockNumber.set(lastBlockNum);
|
|
649
|
+
this.log.verbose(`Synched to latest block ${lastBlockNum}`);
|
|
650
|
+
await this.startServiceIfSynched();
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* Handles new proven blocks by deleting the txs in them, or by deleting the txs in blocks `keepProvenTxsFor` ago.
|
|
655
|
+
* @param blocks - A list of proven L2 blocks.
|
|
656
|
+
* @returns Empty promise.
|
|
657
|
+
*/
|
|
658
|
+
private async handleProvenL2Blocks(blocks: L2Block[]): Promise<void> {
|
|
659
|
+
if (!blocks.length) {
|
|
660
|
+
return Promise.resolve();
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
const firstBlockNum = blocks[0].number;
|
|
664
|
+
const lastBlockNum = blocks[blocks.length - 1].number;
|
|
665
|
+
const lastBlockSlot = blocks[blocks.length - 1].header.globalVariables.slotNumber.toBigInt();
|
|
666
|
+
|
|
667
|
+
// If keepProvenTxsFor is 0, we delete all txs from all proven blocks.
|
|
668
|
+
if (this.keepProvenTxsFor === 0) {
|
|
669
|
+
await this.deleteTxsFromBlocks(blocks);
|
|
670
|
+
} else if (lastBlockNum - this.keepProvenTxsFor >= INITIAL_L2_BLOCK_NUM) {
|
|
671
|
+
const fromBlock = Math.max(INITIAL_L2_BLOCK_NUM, firstBlockNum - this.keepProvenTxsFor);
|
|
672
|
+
const toBlock = lastBlockNum - this.keepProvenTxsFor;
|
|
673
|
+
const limit = toBlock - fromBlock + 1;
|
|
674
|
+
const blocksToDeleteTxsFrom = await this.l2BlockSource.getBlocks(fromBlock, limit, true);
|
|
675
|
+
await this.deleteTxsFromBlocks(blocksToDeleteTxsFrom);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// We delete attestations older than the last block slot minus the number of slots we want to keep in the pool.
|
|
679
|
+
const lastBlockSlotMinusKeepAttestationsInPoolFor = lastBlockSlot - BigInt(this.keepAttestationsInPoolFor);
|
|
680
|
+
if (lastBlockSlotMinusKeepAttestationsInPoolFor >= BigInt(INITIAL_L2_BLOCK_NUM)) {
|
|
681
|
+
await this.attestationPool?.deleteAttestationsOlderThan(lastBlockSlotMinusKeepAttestationsInPoolFor);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
await this.synchedProvenBlockNumber.set(lastBlockNum);
|
|
685
|
+
this.log.debug(`Synched to proven block ${lastBlockNum}`);
|
|
686
|
+
|
|
687
|
+
await this.startServiceIfSynched();
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* Updates the tx pool after a chain prune.
|
|
692
|
+
* @param latestBlock - The block number the chain was pruned to.
|
|
693
|
+
*/
|
|
694
|
+
private async handlePruneL2Blocks(latestBlock: number): Promise<void> {
|
|
695
|
+
const txsToDelete: TxHash[] = [];
|
|
696
|
+
for (const tx of await this.txPool.getAllTxs()) {
|
|
697
|
+
// every tx that's been generated against a block that has now been pruned is no longer valid
|
|
698
|
+
if (tx.data.constants.historicalHeader.globalVariables.blockNumber.toNumber() > latestBlock) {
|
|
699
|
+
txsToDelete.push(await tx.getTxHash());
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
this.log.info(
|
|
704
|
+
`Detected chain prune. Removing invalid txs count=${
|
|
705
|
+
txsToDelete.length
|
|
706
|
+
} newLatestBlock=${latestBlock} previousLatestBlock=${this.getSyncedLatestBlockNum()}`,
|
|
707
|
+
);
|
|
708
|
+
|
|
709
|
+
// delete invalid txs (both pending and mined)
|
|
710
|
+
await this.txPool.deleteTxs(txsToDelete);
|
|
711
|
+
|
|
712
|
+
// everything left in the mined set was built against a block on the proven chain so its still valid
|
|
713
|
+
// move back to pending the txs that were reorged out of the chain
|
|
714
|
+
// NOTE: we can't move _all_ txs back to pending because the tx pool could keep hold of mined txs for longer
|
|
715
|
+
// (see this.keepProvenTxsFor)
|
|
716
|
+
const txsToMoveToPending: TxHash[] = [];
|
|
717
|
+
for (const [txHash, blockNumber] of await this.txPool.getMinedTxHashes()) {
|
|
718
|
+
if (blockNumber > latestBlock) {
|
|
719
|
+
txsToMoveToPending.push(txHash);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
this.log.info(`Moving ${txsToMoveToPending.length} mined txs back to pending`);
|
|
724
|
+
await this.txPool.markMinedAsPending(txsToMoveToPending);
|
|
725
|
+
|
|
726
|
+
await this.synchedLatestBlockNumber.set(latestBlock);
|
|
727
|
+
// no need to update block hashes, as they will be updated as new blocks are added
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
private async startServiceIfSynched() {
|
|
731
|
+
if (
|
|
732
|
+
this.currentState === P2PClientState.SYNCHING &&
|
|
733
|
+
(await this.getSyncedLatestBlockNum()) >= this.latestBlockNumberAtStart &&
|
|
734
|
+
(await this.getSyncedProvenBlockNum()) >= this.provenBlockNumberAtStart
|
|
735
|
+
) {
|
|
736
|
+
this.log.debug(`Synched to blocks at start`);
|
|
737
|
+
this.setCurrentState(P2PClientState.RUNNING);
|
|
738
|
+
if (this.syncResolve !== undefined) {
|
|
739
|
+
this.syncResolve();
|
|
740
|
+
await this.p2pService.start();
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* Method to set the value of the current state.
|
|
747
|
+
* @param newState - New state value.
|
|
748
|
+
*/
|
|
749
|
+
private setCurrentState(newState: P2PClientState) {
|
|
750
|
+
const oldState = this.currentState;
|
|
751
|
+
this.currentState = newState;
|
|
752
|
+
this.log.debug(`Moved from state ${P2PClientState[oldState]} to ${P2PClientState[this.currentState]}`);
|
|
753
|
+
}
|
|
754
|
+
}
|