cuprated/txpool/
dandelion.rs

1use std::time::Duration;
2
3use cuprate_dandelion_tower::{
4    pool::DandelionPoolService, DandelionConfig, DandelionRouter, Graph,
5};
6use cuprate_p2p::NetworkInterface;
7use cuprate_p2p_core::ClearNet;
8use cuprate_txpool::service::{TxpoolReadHandle, TxpoolWriteHandle};
9
10use crate::{
11    p2p::CrossNetworkInternalPeerId,
12    txpool::incoming_tx::{DandelionTx, TxId},
13};
14
15mod diffuse_service;
16mod stem_service;
17mod tx_store;
18
19/// The configuration used for [`cuprate_dandelion_tower`].
20///
21/// TODO: should we expose this to users of cuprated? probably not.
22const DANDELION_CONFIG: DandelionConfig = DandelionConfig {
23    time_between_hop: Duration::from_millis(175),
24    epoch_duration: Duration::from_secs(10 * 60),
25    fluff_probability: 0.12,
26    graph: Graph::FourRegular,
27};
28
29/// A [`DandelionRouter`] with all generic types defined.
30type ConcreteDandelionRouter = DandelionRouter<
31    stem_service::OutboundPeerStream,
32    diffuse_service::DiffuseService,
33    CrossNetworkInternalPeerId,
34    stem_service::StemPeerService<ClearNet>,
35    DandelionTx,
36>;
37
38/// Starts the dandelion pool manager task and returns a handle to send txs to broadcast.
39pub fn start_dandelion_pool_manager(
40    router: ConcreteDandelionRouter,
41    txpool_read_handle: TxpoolReadHandle,
42    txpool_write_handle: TxpoolWriteHandle,
43) -> DandelionPoolService<DandelionTx, TxId, CrossNetworkInternalPeerId> {
44    cuprate_dandelion_tower::pool::start_dandelion_pool_manager(
45        // TODO: make this constant configurable?
46        32,
47        router,
48        tx_store::TxStoreService {
49            txpool_read_handle,
50            txpool_write_handle,
51        },
52        DANDELION_CONFIG,
53    )
54}
55
56/// Creates a [`DandelionRouter`] from a [`NetworkInterface`].
57pub fn dandelion_router(clear_net: NetworkInterface<ClearNet>) -> ConcreteDandelionRouter {
58    DandelionRouter::new(
59        diffuse_service::DiffuseService {
60            clear_net_broadcast_service: clear_net.broadcast_svc(),
61        },
62        stem_service::OutboundPeerStream::new(clear_net),
63        DANDELION_CONFIG,
64    )
65}