1use std::task::{Context, Poll};
4
5use anyhow::Error;
6use futures::future::BoxFuture;
7use monero_serai::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, TxpoolWriteHandle};
20use cuprate_types::{AddAuxPow, AuxPow, HardFork};
21
22use crate::rpc::{bin, json, other};
23
24#[derive(Clone)]
26#[expect(clippy::large_enum_variant)]
27pub enum BlockchainManagerRequest {
28 PopBlocks { amount: usize },
32
33 Prune,
35
36 Pruned,
38
39 RelayBlock(Block),
41
42 Syncing,
51
52 Synced,
54
55 Target,
57
58 TargetHeight,
60
61 GenerateBlocks {
65 amount_of_blocks: u64,
67 prev_block: [u8; 32],
69 starting_nonce: u32,
71 wallet_address: String,
73 },
74
75 NextNeededPruningSeed,
86}
87
88#[derive(Clone)]
90pub enum BlockchainManagerResponse {
91 Ok,
97
98 PopBlocks { new_height: usize },
100
101 Prune(PruningSeed),
103
104 Pruned(bool),
106
107 Syncing(bool),
109
110 Synced(bool),
112
113 Target(std::time::Duration),
115
116 TargetHeight { height: usize },
118
119 GenerateBlocks {
121 blocks: Vec<[u8; 32]>,
123 height: usize,
125 },
126
127 NextNeededPruningSeed(PruningSeed),
131}
132
133pub type BlockchainManagerHandle = cuprate_database_service::DatabaseReadService<
135 BlockchainManagerRequest,
136 BlockchainManagerResponse,
137>;
138
139#[derive(Clone)]
141pub struct CupratedRpcHandler {
142 restricted: bool,
146
147 pub blockchain_read: BlockchainReadHandle,
149
150 pub blockchain_context: BlockchainContextService,
152
153 pub blockchain_manager: BlockchainManagerHandle,
155
156 pub txpool_read: TxpoolReadHandle,
158
159 pub txpool_manager: std::convert::Infallible,
161}
162
163impl CupratedRpcHandler {
164 pub const fn new(
166 restricted: bool,
167 blockchain_read: BlockchainReadHandle,
168 blockchain_context: BlockchainContextService,
169 blockchain_manager: BlockchainManagerHandle,
170 txpool_read: TxpoolReadHandle,
171 txpool_manager: std::convert::Infallible,
172 ) -> Self {
173 Self {
174 restricted,
175 blockchain_read,
176 blockchain_context,
177 blockchain_manager,
178 txpool_read,
179 txpool_manager,
180 }
181 }
182}
183
184impl RpcHandler for CupratedRpcHandler {
185 fn restricted(&self) -> bool {
186 self.restricted
187 }
188}
189
190impl Service<JsonRpcRequest> for CupratedRpcHandler {
191 type Response = JsonRpcResponse;
192 type Error = Error;
193 type Future = BoxFuture<'static, Result<JsonRpcResponse, Error>>;
194
195 fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
196 Poll::Ready(Ok(()))
197 }
198
199 fn call(&mut self, request: JsonRpcRequest) -> Self::Future {
200 let state = self.clone();
201 Box::pin(json::map_request(state, request))
202 }
203}
204
205impl Service<BinRequest> for CupratedRpcHandler {
206 type Response = BinResponse;
207 type Error = Error;
208 type Future = BoxFuture<'static, Result<BinResponse, Error>>;
209
210 fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
211 Poll::Ready(Ok(()))
212 }
213
214 fn call(&mut self, request: BinRequest) -> Self::Future {
215 let state = self.clone();
216 Box::pin(bin::map_request(state, request))
217 }
218}
219
220impl Service<OtherRequest> for CupratedRpcHandler {
221 type Response = OtherResponse;
222 type Error = Error;
223 type Future = BoxFuture<'static, Result<OtherResponse, Error>>;
224
225 fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
226 Poll::Ready(Ok(()))
227 }
228
229 fn call(&mut self, request: OtherRequest) -> Self::Future {
230 let state = self.clone();
231 Box::pin(other::map_request(state, request))
232 }
233}