cuprated/txpool/dandelion/
diffuse_service.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::{
    future::{ready, Ready},
    task::{Context, Poll},
};

use futures::FutureExt;
use tower::Service;

use cuprate_dandelion_tower::traits::DiffuseRequest;
use cuprate_p2p::{BroadcastRequest, BroadcastSvc};
use cuprate_p2p_core::ClearNet;

use crate::txpool::dandelion::DandelionTx;

/// The dandelion diffusion service.
pub struct DiffuseService {
    pub clear_net_broadcast_service: BroadcastSvc<ClearNet>,
}

impl Service<DiffuseRequest<DandelionTx>> for DiffuseService {
    type Response = ();
    type Error = tower::BoxError;
    type Future = Ready<Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.clear_net_broadcast_service
            .poll_ready(cx)
            .map_err(Into::into)
    }

    fn call(&mut self, req: DiffuseRequest<DandelionTx>) -> Self::Future {
        // TODO: the dandelion crate should pass along where we got the tx from.
        let Ok(()) = self
            .clear_net_broadcast_service
            .call(BroadcastRequest::Transaction {
                tx_bytes: req.0 .0,
                direction: None,
                received_from: None,
            })
            .into_inner();

        ready(Ok(()))
    }
}