cuprate_p2p_core/client/handshaker/builder/
dummy.rs

1use std::{
2    future::{ready, Ready},
3    task::{Context, Poll},
4};
5
6use tower::Service;
7
8use cuprate_wire::CoreSyncData;
9
10use crate::{
11    services::{
12        AddressBookRequest, AddressBookResponse, CoreSyncDataRequest, CoreSyncDataResponse,
13    },
14    NetworkZone, ProtocolRequest, ProtocolResponse,
15};
16
17/// A dummy core sync service that just returns static [`CoreSyncData`].
18#[derive(Debug, Clone)]
19pub struct DummyCoreSyncSvc(CoreSyncData);
20
21impl DummyCoreSyncSvc {
22    /// Returns a [`DummyCoreSyncSvc`] that will just return the mainnet genesis [`CoreSyncData`].
23    pub const fn static_mainnet_genesis() -> Self {
24        Self(CoreSyncData {
25            cumulative_difficulty: 1,
26            cumulative_difficulty_top64: 0,
27            current_height: 1,
28            pruning_seed: 0,
29            top_id: hex_literal::hex!(
30                "418015bb9ae982a1975da7d79277c2705727a56894ba0fb246adaabb1f4632e3"
31            ),
32            top_version: 1,
33        })
34    }
35
36    /// Returns a [`DummyCoreSyncSvc`] that will just return the testnet genesis [`CoreSyncData`].
37    pub const fn static_testnet_genesis() -> Self {
38        Self(CoreSyncData {
39            cumulative_difficulty: 1,
40            cumulative_difficulty_top64: 0,
41            current_height: 1,
42            pruning_seed: 0,
43            top_id: hex_literal::hex!(
44                "48ca7cd3c8de5b6a4d53d2861fbdaedca141553559f9be9520068053cda8430b"
45            ),
46            top_version: 1,
47        })
48    }
49
50    /// Returns a [`DummyCoreSyncSvc`] that will just return the stagenet genesis [`CoreSyncData`].
51    pub const fn static_stagenet_genesis() -> Self {
52        Self(CoreSyncData {
53            cumulative_difficulty: 1,
54            cumulative_difficulty_top64: 0,
55            current_height: 1,
56            pruning_seed: 0,
57            top_id: hex_literal::hex!(
58                "76ee3cc98646292206cd3e86f74d88b4dcc1d937088645e9b0cbca84b7ce74eb"
59            ),
60            top_version: 1,
61        })
62    }
63
64    /// Returns a [`DummyCoreSyncSvc`] that will return the provided [`CoreSyncData`].
65    pub const fn static_custom(data: CoreSyncData) -> Self {
66        Self(data)
67    }
68}
69
70impl Service<CoreSyncDataRequest> for DummyCoreSyncSvc {
71    type Response = CoreSyncDataResponse;
72    type Error = tower::BoxError;
73    type Future = Ready<Result<Self::Response, Self::Error>>;
74
75    fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
76        Poll::Ready(Ok(()))
77    }
78
79    fn call(&mut self, _: CoreSyncDataRequest) -> Self::Future {
80        ready(Ok(CoreSyncDataResponse(self.0.clone())))
81    }
82}
83
84/// A dummy address book that doesn't actually keep track of peers.
85#[derive(Debug, Clone)]
86pub struct DummyAddressBook;
87
88impl<N: NetworkZone> Service<AddressBookRequest<N>> for DummyAddressBook {
89    type Response = AddressBookResponse<N>;
90    type Error = tower::BoxError;
91    type Future = Ready<Result<Self::Response, Self::Error>>;
92
93    fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
94        Poll::Ready(Ok(()))
95    }
96
97    fn call(&mut self, req: AddressBookRequest<N>) -> Self::Future {
98        ready(Ok(match req {
99            AddressBookRequest::GetWhitePeers(_) => AddressBookResponse::Peers(vec![]),
100            AddressBookRequest::TakeRandomGrayPeer { .. }
101            | AddressBookRequest::TakeRandomPeer { .. }
102            | AddressBookRequest::TakeRandomWhitePeer { .. } => {
103                return ready(Err("dummy address book does not hold peers".into()));
104            }
105            AddressBookRequest::NewConnection { .. } | AddressBookRequest::IncomingPeerList(_) => {
106                AddressBookResponse::Ok
107            }
108            AddressBookRequest::GetBan(_) => AddressBookResponse::GetBan {
109                unban_instant: None,
110            },
111            AddressBookRequest::PeerlistSize
112            | AddressBookRequest::ConnectionCount
113            | AddressBookRequest::SetBan(_)
114            | AddressBookRequest::GetBans
115            | AddressBookRequest::ConnectionInfo => {
116                todo!("finish https://github.com/Cuprate/cuprate/pull/297")
117            }
118        }))
119    }
120}
121
122/// A dummy protocol request handler.
123#[derive(Debug, Clone)]
124pub struct DummyProtocolRequestHandler;
125
126impl Service<ProtocolRequest> for DummyProtocolRequestHandler {
127    type Response = ProtocolResponse;
128    type Error = tower::BoxError;
129    type Future = Ready<Result<Self::Response, Self::Error>>;
130
131    fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
132        Poll::Ready(Ok(()))
133    }
134
135    fn call(&mut self, _: ProtocolRequest) -> Self::Future {
136        ready(Ok(ProtocolResponse::NA))
137    }
138}