cuprated/rpc/
rpc_handler.rs1use std::task::{Context, Poll};
4
5use anyhow::Error;
6use futures::future::BoxFuture;
7use monero_oxide::block::Block;
8use tower::Service;
9
10use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
11use cuprate_consensus::BlockchainContextService;
12use cuprate_pruning::PruningSeed;
13use cuprate_rpc_interface::RpcHandler;
14use cuprate_rpc_types::{
15 bin::{BinRequest, BinResponse},
16 json::{JsonRpcRequest, JsonRpcResponse},
17 other::{OtherRequest, OtherResponse},
18};
19use cuprate_txpool::service::TxpoolReadHandle;
20use cuprate_types::BlockTemplate;
21
22use crate::{rpc::handlers, txpool::IncomingTxHandler};
23
24#[derive(Clone)]
26pub enum BlockchainManagerRequest {
27 PopBlocks { amount: usize },
31
32 Prune,
34
35 Pruned,
37
38 RelayBlock(
40 Box<Block>,
42 ),
43
44 Sync,
46
47 Syncing,
56
57 Synced,
59
60 Target,
62
63 TargetHeight,
65
66 GenerateBlocks {
70 amount_of_blocks: u64,
72 prev_block: Option<[u8; 32]>,
74 starting_nonce: u32,
76 wallet_address: String,
78 },
79
80 NextNeededPruningSeed,
91
92 CreateBlockTemplate {
94 prev_block: [u8; 32],
95 account_public_address: String,
96 extra_nonce: Vec<u8>,
97 },
98
99 Stop,
101}
102
103#[derive(Clone)]
105pub enum BlockchainManagerResponse {
106 Ok,
113
114 PopBlocks { new_height: usize },
116
117 Prune(PruningSeed),
119
120 Pruned(bool),
122
123 Syncing(bool),
125
126 Synced(bool),
128
129 Target(std::time::Duration),
131
132 TargetHeight { height: usize },
134
135 GenerateBlocks {
137 blocks: Vec<[u8; 32]>,
139 height: usize,
141 },
142
143 NextNeededPruningSeed(PruningSeed),
145
146 CreateBlockTemplate(Box<BlockTemplate>),
148}
149
150pub type BlockchainManagerHandle = cuprate_database_service::DatabaseReadService<
152 BlockchainManagerRequest,
153 BlockchainManagerResponse,
154>;
155
156#[derive(Clone)]
158pub struct CupratedRpcHandler {
159 restricted: bool,
163
164 pub blockchain_read: BlockchainReadHandle,
166
167 pub blockchain_context: BlockchainContextService,
169
170 pub txpool_read: TxpoolReadHandle,
172
173 pub tx_handler: IncomingTxHandler,
174}
175
176impl CupratedRpcHandler {
177 pub const fn new(
179 restricted: bool,
180 blockchain_read: BlockchainReadHandle,
181 blockchain_context: BlockchainContextService,
182 txpool_read: TxpoolReadHandle,
183 tx_handler: IncomingTxHandler,
184 ) -> Self {
185 Self {
186 restricted,
187 blockchain_read,
188 blockchain_context,
189 txpool_read,
190 tx_handler,
191 }
192 }
193}
194
195impl RpcHandler for CupratedRpcHandler {
196 fn is_restricted(&self) -> bool {
197 self.restricted
198 }
199}
200
201impl Service<JsonRpcRequest> for CupratedRpcHandler {
202 type Response = JsonRpcResponse;
203 type Error = Error;
204 type Future = BoxFuture<'static, Result<JsonRpcResponse, Error>>;
205
206 fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
207 Poll::Ready(Ok(()))
208 }
209
210 fn call(&mut self, request: JsonRpcRequest) -> Self::Future {
211 let state = self.clone();
212 Box::pin(handlers::json_rpc::map_request(state, request))
213 }
214}
215
216impl Service<BinRequest> for CupratedRpcHandler {
217 type Response = BinResponse;
218 type Error = Error;
219 type Future = BoxFuture<'static, Result<BinResponse, Error>>;
220
221 fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
222 Poll::Ready(Ok(()))
223 }
224
225 fn call(&mut self, request: BinRequest) -> Self::Future {
226 let state = self.clone();
227 Box::pin(handlers::bin::map_request(state, request))
228 }
229}
230
231impl Service<OtherRequest> for CupratedRpcHandler {
232 type Response = OtherResponse;
233 type Error = Error;
234 type Future = BoxFuture<'static, Result<OtherResponse, Error>>;
235
236 fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
237 Poll::Ready(Ok(()))
238 }
239
240 fn call(&mut self, request: OtherRequest) -> Self::Future {
241 let state = self.clone();
242 Box::pin(handlers::other_json::map_request(state, request))
243 }
244}