cuprate_rpc_interface/
rpc_handler_dummy.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! Dummy implementation of [`RpcHandler`].

//---------------------------------------------------------------------------------------------------- Use
use std::task::Poll;

use anyhow::Error;
use futures::channel::oneshot::channel;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use tower::Service;

use cuprate_helper::asynch::InfallibleOneshotReceiver;
use cuprate_rpc_types::{
    bin::{BinRequest, BinResponse},
    json::{JsonRpcRequest, JsonRpcResponse},
    other::{OtherRequest, OtherResponse},
};

use crate::rpc_handler::RpcHandler;

//---------------------------------------------------------------------------------------------------- RpcHandlerDummy
/// An [`RpcHandler`] that always returns [`Default::default`].
///
/// This `struct` implements [`RpcHandler`], and always responds
/// with the response `struct` set to [`Default::default`].
///
/// See the [`crate`] documentation for example usage.
///
/// This is mostly used for testing purposes and can
/// be disabled by disable the `dummy` feature flag.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct RpcHandlerDummy {
    /// Should this RPC server be [restricted](RpcHandler::restricted)?
    ///
    /// The dummy will honor this [`bool`]
    /// on restricted methods/endpoints.
    pub restricted: bool,
}

impl RpcHandler for RpcHandlerDummy {
    fn restricted(&self) -> bool {
        self.restricted
    }
}

impl Service<JsonRpcRequest> for RpcHandlerDummy {
    type Response = JsonRpcResponse;
    type Error = Error;
    type Future = InfallibleOneshotReceiver<Result<JsonRpcResponse, Error>>;

    fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: JsonRpcRequest) -> Self::Future {
        use cuprate_rpc_types::json::JsonRpcRequest as Req;
        use cuprate_rpc_types::json::JsonRpcResponse as Resp;

        #[expect(clippy::default_trait_access)]
        let resp = match req {
            Req::GetBlockCount(_) => Resp::GetBlockCount(Default::default()),
            Req::OnGetBlockHash(_) => Resp::OnGetBlockHash(Default::default()),
            Req::SubmitBlock(_) => Resp::SubmitBlock(Default::default()),
            Req::GenerateBlocks(_) => Resp::GenerateBlocks(Default::default()),
            Req::GetLastBlockHeader(_) => Resp::GetLastBlockHeader(Default::default()),
            Req::GetBlockHeaderByHash(_) => Resp::GetBlockHeaderByHash(Default::default()),
            Req::GetBlockHeaderByHeight(_) => Resp::GetBlockHeaderByHeight(Default::default()),
            Req::GetBlockHeadersRange(_) => Resp::GetBlockHeadersRange(Default::default()),
            Req::GetBlock(_) => Resp::GetBlock(Default::default()),
            Req::GetConnections(_) => Resp::GetConnections(Default::default()),
            Req::GetInfo(_) => Resp::GetInfo(Default::default()),
            Req::HardForkInfo(_) => Resp::HardForkInfo(Default::default()),
            Req::SetBans(_) => Resp::SetBans(Default::default()),
            Req::GetBans(_) => Resp::GetBans(Default::default()),
            Req::Banned(_) => Resp::Banned(Default::default()),
            Req::FlushTransactionPool(_) => Resp::FlushTransactionPool(Default::default()),
            Req::GetOutputHistogram(_) => Resp::GetOutputHistogram(Default::default()),
            Req::GetCoinbaseTxSum(_) => Resp::GetCoinbaseTxSum(Default::default()),
            Req::GetVersion(_) => Resp::GetVersion(Default::default()),
            Req::GetFeeEstimate(_) => Resp::GetFeeEstimate(Default::default()),
            Req::GetAlternateChains(_) => Resp::GetAlternateChains(Default::default()),
            Req::RelayTx(_) => Resp::RelayTx(Default::default()),
            Req::SyncInfo(_) => Resp::SyncInfo(Default::default()),
            Req::GetTransactionPoolBacklog(_) => {
                Resp::GetTransactionPoolBacklog(Default::default())
            }
            Req::GetMinerData(_) => Resp::GetMinerData(Default::default()),
            Req::PruneBlockchain(_) => Resp::PruneBlockchain(Default::default()),
            Req::CalcPow(_) => Resp::CalcPow(Default::default()),
            Req::FlushCache(_) => Resp::FlushCache(Default::default()),
            Req::AddAuxPow(_) => Resp::AddAuxPow(Default::default()),
            Req::GetTxIdsLoose(_) => Resp::GetTxIdsLoose(Default::default()),
        };

        let (tx, rx) = channel();
        drop(tx.send(Ok(resp)));
        InfallibleOneshotReceiver::from(rx)
    }
}

impl Service<BinRequest> for RpcHandlerDummy {
    type Response = BinResponse;
    type Error = Error;
    type Future = InfallibleOneshotReceiver<Result<BinResponse, Error>>;

    fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: BinRequest) -> Self::Future {
        use cuprate_rpc_types::bin::BinRequest as Req;
        use cuprate_rpc_types::bin::BinResponse as Resp;

        #[expect(clippy::default_trait_access)]
        let resp = match req {
            Req::GetBlocks(_) => Resp::GetBlocks(Default::default()),
            Req::GetBlocksByHeight(_) => Resp::GetBlocksByHeight(Default::default()),
            Req::GetHashes(_) => Resp::GetHashes(Default::default()),
            Req::GetOutputIndexes(_) => Resp::GetOutputIndexes(Default::default()),
            Req::GetOuts(_) => Resp::GetOuts(Default::default()),
            Req::GetTransactionPoolHashes(_) => Resp::GetTransactionPoolHashes(Default::default()),
            Req::GetOutputDistribution(_) => Resp::GetOutputDistribution(Default::default()),
        };

        let (tx, rx) = channel();
        drop(tx.send(Ok(resp)));
        InfallibleOneshotReceiver::from(rx)
    }
}

impl Service<OtherRequest> for RpcHandlerDummy {
    type Response = OtherResponse;
    type Error = Error;
    type Future = InfallibleOneshotReceiver<Result<OtherResponse, Error>>;

    fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: OtherRequest) -> Self::Future {
        use cuprate_rpc_types::other::OtherRequest as Req;
        use cuprate_rpc_types::other::OtherResponse as Resp;

        #[expect(clippy::default_trait_access)]
        let resp = match req {
            Req::GetHeight(_) => Resp::GetHeight(Default::default()),
            Req::GetTransactions(_) => Resp::GetTransactions(Default::default()),
            Req::GetAltBlocksHashes(_) => Resp::GetAltBlocksHashes(Default::default()),
            Req::IsKeyImageSpent(_) => Resp::IsKeyImageSpent(Default::default()),
            Req::SendRawTransaction(_) => Resp::SendRawTransaction(Default::default()),
            Req::StartMining(_) => Resp::StartMining(Default::default()),
            Req::StopMining(_) => Resp::StopMining(Default::default()),
            Req::MiningStatus(_) => Resp::MiningStatus(Default::default()),
            Req::SaveBc(_) => Resp::SaveBc(Default::default()),
            Req::GetPeerList(_) => Resp::GetPeerList(Default::default()),
            Req::SetLogHashRate(_) => Resp::SetLogHashRate(Default::default()),
            Req::SetLogLevel(_) => Resp::SetLogLevel(Default::default()),
            Req::SetLogCategories(_) => Resp::SetLogCategories(Default::default()),
            Req::SetBootstrapDaemon(_) => Resp::SetBootstrapDaemon(Default::default()),
            Req::GetTransactionPool(_) => Resp::GetTransactionPool(Default::default()),
            Req::GetTransactionPoolStats(_) => Resp::GetTransactionPoolStats(Default::default()),
            Req::StopDaemon(_) => Resp::StopDaemon(Default::default()),
            Req::GetLimit(_) => Resp::GetLimit(Default::default()),
            Req::SetLimit(_) => Resp::SetLimit(Default::default()),
            Req::OutPeers(_) => Resp::OutPeers(Default::default()),
            Req::InPeers(_) => Resp::InPeers(Default::default()),
            Req::GetNetStats(_) => Resp::GetNetStats(Default::default()),
            Req::GetOuts(_) => Resp::GetOuts(Default::default()),
            Req::Update(_) => Resp::Update(Default::default()),
            Req::PopBlocks(_) => Resp::PopBlocks(Default::default()),
            Req::GetTransactionPoolHashes(_) => Resp::GetTransactionPoolHashes(Default::default()),
            Req::GetPublicNodes(_) => Resp::GetPublicNodes(Default::default()),
        };

        let (tx, rx) = channel();
        drop(tx.send(Ok(resp)));
        InfallibleOneshotReceiver::from(rx)
    }
}