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#[derive(Debug, Clone)]
19pub struct DummyCoreSyncSvc(CoreSyncData);
20
21impl DummyCoreSyncSvc {
22 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 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 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 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#[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::OwnAddress => AddressBookResponse::OwnAddress(None),
112 AddressBookRequest::Peerlist
113 | AddressBookRequest::PeerlistSize
114 | AddressBookRequest::ConnectionCount
115 | AddressBookRequest::SetBan(_)
116 | AddressBookRequest::GetBans
117 | AddressBookRequest::ConnectionInfo => {
118 todo!("finish https://github.com/Cuprate/cuprate/pull/297")
119 }
120 }))
121 }
122}
123
124#[derive(Debug, Clone)]
126pub struct DummyProtocolRequestHandler;
127
128impl Service<ProtocolRequest> for DummyProtocolRequestHandler {
129 type Response = ProtocolResponse;
130 type Error = tower::BoxError;
131 type Future = Ready<Result<Self::Response, Self::Error>>;
132
133 fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
134 Poll::Ready(Ok(()))
135 }
136
137 fn call(&mut self, _: ProtocolRequest) -> Self::Future {
138 ready(Ok(ProtocolResponse::NA))
139 }
140}