cuprated/
p2p.rs

1//! P2P
2//!
3//! Will handle initiating the P2P and contains a protocol request handler.
4use futures::{FutureExt, TryFutureExt};
5use tokio::sync::oneshot;
6use tower::ServiceExt;
7
8use cuprate_blockchain::service::BlockchainReadHandle;
9use cuprate_consensus::BlockchainContextService;
10use cuprate_p2p::{NetworkInterface, P2PConfig};
11use cuprate_p2p_core::ClearNet;
12use cuprate_txpool::service::TxpoolReadHandle;
13
14use crate::txpool::IncomingTxHandler;
15
16mod core_sync_service;
17mod network_address;
18pub mod request_handler;
19
20pub use network_address::CrossNetworkInternalPeerId;
21
22/// Starts the P2P clearnet network, returning a [`NetworkInterface`] to interact with it.
23///
24/// A [`oneshot::Sender`] is also returned to provide the [`IncomingTxHandler`], until this is provided network
25/// handshakes can not be completed.
26pub async fn start_clearnet_p2p(
27    blockchain_read_handle: BlockchainReadHandle,
28    blockchain_context_service: BlockchainContextService,
29    txpool_read_handle: TxpoolReadHandle,
30    config: P2PConfig<ClearNet>,
31) -> Result<
32    (
33        NetworkInterface<ClearNet>,
34        oneshot::Sender<IncomingTxHandler>,
35    ),
36    tower::BoxError,
37> {
38    let (incoming_tx_handler_tx, incoming_tx_handler_rx) = oneshot::channel();
39
40    let request_handler_maker = request_handler::P2pProtocolRequestHandlerMaker {
41        blockchain_read_handle,
42        blockchain_context_service: blockchain_context_service.clone(),
43        txpool_read_handle,
44        incoming_tx_handler: None,
45        incoming_tx_handler_fut: incoming_tx_handler_rx.shared(),
46    };
47
48    Ok((
49        cuprate_p2p::initialize_network(
50            request_handler_maker.map_response(|s| s.map_err(Into::into)),
51            core_sync_service::CoreSyncService(blockchain_context_service),
52            config,
53        )
54        .await?,
55        incoming_tx_handler_tx,
56    ))
57}