darkfid/
rpc.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use std::{collections::HashSet, time::Instant};
20
21use async_trait::async_trait;
22use log::{debug, error, info, warn};
23use smol::lock::MutexGuard;
24use tinyjson::JsonValue;
25use url::Url;
26
27use darkfi::{
28    net::P2pPtr,
29    rpc::{
30        client::RpcChadClient,
31        jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult},
32        p2p_method::HandlerP2p,
33        server::RequestHandler,
34    },
35    system::{sleep, ExecutorPtr, StoppableTaskPtr},
36    util::time::Timestamp,
37    Error, Result,
38};
39
40use crate::{
41    error::{server_error, RpcError},
42    DarkfiNode,
43};
44
45/// Default JSON-RPC `RequestHandler` type
46pub struct DefaultRpcHandler;
47/// HTTP JSON-RPC `RequestHandler` type for p2pool
48pub struct MmRpcHandler;
49
50/// Structure to hold a JSON-RPC client and its config,
51/// so we can recreate it in case of an error.
52pub struct MinerRpcClient {
53    endpoint: Url,
54    ex: ExecutorPtr,
55    client: Option<RpcChadClient>,
56}
57
58impl MinerRpcClient {
59    pub async fn new(endpoint: Url, ex: ExecutorPtr) -> Self {
60        let client = match RpcChadClient::new(endpoint.clone(), ex.clone()).await {
61            Ok(c) => Some(c),
62            Err(_) => {
63                warn!(target: "darkfid::Darkfid::init", "Failed to initialize miner daemon rpc client, will try later");
64                None
65            }
66        };
67        Self { endpoint, ex, client }
68    }
69
70    /// Stop the client.
71    pub async fn stop(&self) {
72        if let Some(ref client) = self.client {
73            client.stop().await
74        }
75    }
76}
77
78#[async_trait]
79#[rustfmt::skip]
80impl RequestHandler<DefaultRpcHandler> for DarkfiNode {
81    async fn handle_request(&self, req: JsonRequest) -> JsonResult {
82        debug!(target: "darkfid::rpc", "--> {}", req.stringify().unwrap());
83
84        match req.method.as_str() {
85            // =====================
86            // Miscellaneous methods
87            // =====================
88            "ping" => <DarkfiNode as RequestHandler<DefaultRpcHandler>>::pong(self, req.id, req.params).await,
89            "clock" => self.clock(req.id, req.params).await,
90            "ping_miner" => self.ping_miner(req.id, req.params).await,
91            "dnet.switch" => self.dnet_switch(req.id, req.params).await,
92            "dnet.subscribe_events" => self.dnet_subscribe_events(req.id, req.params).await,
93            "p2p.get_info" => self.p2p_get_info(req.id, req.params).await,
94
95            // ==================
96            // Blockchain methods
97            // ==================
98            "blockchain.get_block" => self.blockchain_get_block(req.id, req.params).await,
99            "blockchain.get_tx" => self.blockchain_get_tx(req.id, req.params).await,
100            "blockchain.last_confirmed_block" => self.blockchain_last_confirmed_block(req.id, req.params).await,
101            "blockchain.best_fork_next_block_height" => self.blockchain_best_fork_next_block_height(req.id, req.params).await,
102            "blockchain.block_target" => self.blockchain_block_target(req.id, req.params).await,
103            "blockchain.lookup_zkas" => self.blockchain_lookup_zkas(req.id, req.params).await,
104            "blockchain.get_contract_state" => self.blockchain_get_contract_state(req.id, req.params).await,
105            "blockchain.get_contract_state_key" => self.blockchain_get_contract_state_key(req.id, req.params).await,
106            "blockchain.subscribe_blocks" => self.blockchain_subscribe_blocks(req.id, req.params).await,
107            "blockchain.subscribe_txs" =>  self.blockchain_subscribe_txs(req.id, req.params).await,
108            "blockchain.subscribe_proposals" => self.blockchain_subscribe_proposals(req.id, req.params).await,
109
110            // ===================
111            // Transaction methods
112            // ===================
113            "tx.simulate" => self.tx_simulate(req.id, req.params).await,
114            "tx.broadcast" => self.tx_broadcast(req.id, req.params).await,
115            "tx.pending" => self.tx_pending(req.id, req.params).await,
116            "tx.clean_pending" => self.tx_pending(req.id, req.params).await,
117            "tx.calculate_fee" => self.tx_calculate_fee(req.id, req.params).await,
118
119            // ==============
120            // Invalid method
121            // ==============
122            _ => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
123        }
124    }
125
126    async fn connections_mut(&self) -> MutexGuard<'life0, HashSet<StoppableTaskPtr>> {
127        self.rpc_connections.lock().await
128    }
129}
130
131#[async_trait]
132#[rustfmt::skip]
133impl RequestHandler<MmRpcHandler> for DarkfiNode {
134    async fn handle_request(&self, req: JsonRequest) -> JsonResult {
135        debug!(target: "darkfid::mm_rpc", "--> {}", req.stringify().unwrap());
136
137        match req.method.as_str() {
138            // ================================================
139            // P2Pool methods requested for Monero Merge Mining
140            // ================================================
141            "merge_mining_get_chain_id" => self.xmr_merge_mining_get_chain_id(req.id, req.params).await,
142
143            // ==============
144            // Invalid method
145            // ==============
146            _ => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
147        }
148    }
149
150    async fn connections_mut(&self) -> MutexGuard<'life0, HashSet<StoppableTaskPtr>> {
151        self.mm_rpc_connections.lock().await
152    }
153}
154
155impl DarkfiNode {
156    // RPCAPI:
157    // Returns current system clock as `u64` (String) timestamp.
158    //
159    // --> {"jsonrpc": "2.0", "method": "clock", "params": [], "id": 1}
160    // <-- {"jsonrpc": "2.0", "result": "1234", "id": 1}
161    async fn clock(&self, id: u16, _params: JsonValue) -> JsonResult {
162        JsonResponse::new(JsonValue::String(Timestamp::current_time().inner().to_string()), id)
163            .into()
164    }
165
166    // RPCAPI:
167    // Activate or deactivate dnet in the P2P stack.
168    // By sending `true`, dnet will be activated, and by sending `false` dnet
169    // will be deactivated. Returns `true` on success.
170    //
171    // --> {"jsonrpc": "2.0", "method": "dnet_switch", "params": [true], "id": 42}
172    // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
173    async fn dnet_switch(&self, id: u16, params: JsonValue) -> JsonResult {
174        let params = params.get::<Vec<JsonValue>>().unwrap();
175        if params.len() != 1 || !params[0].is_bool() {
176            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
177        }
178
179        let switch = params[0].get::<bool>().unwrap();
180
181        if *switch {
182            self.p2p_handler.p2p.dnet_enable();
183        } else {
184            self.p2p_handler.p2p.dnet_disable();
185        }
186
187        JsonResponse::new(JsonValue::Boolean(true), id).into()
188    }
189
190    // RPCAPI:
191    // Initializes a subscription to p2p dnet events.
192    // Once a subscription is established, `darkfid` will send JSON-RPC notifications of
193    // new network events to the subscriber.
194    //
195    // --> {"jsonrpc": "2.0", "method": "dnet.subscribe_events", "params": [], "id": 1}
196    // <-- {"jsonrpc": "2.0", "method": "dnet.subscribe_events", "params": [`event`]}
197    pub async fn dnet_subscribe_events(&self, id: u16, params: JsonValue) -> JsonResult {
198        let params = params.get::<Vec<JsonValue>>().unwrap();
199        if !params.is_empty() {
200            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
201        }
202
203        self.subscribers.get("dnet").unwrap().clone().into()
204    }
205
206    // RPCAPI:
207    // Pings configured miner daemon for liveness.
208    // Returns `true` on success.
209    //
210    // --> {"jsonrpc": "2.0", "method": "ping_miner", "params": [], "id": 1}
211    // <-- {"jsonrpc": "2.0", "result": "true", "id": 1}
212    async fn ping_miner(&self, id: u16, _params: JsonValue) -> JsonResult {
213        if let Err(e) = self.ping_miner_daemon().await {
214            error!(target: "darkfid::rpc::ping_miner", "Failed to ping miner daemon: {}", e);
215            return server_error(RpcError::PingFailed, id, None)
216        }
217        JsonResponse::new(JsonValue::Boolean(true), id).into()
218    }
219
220    /// Ping configured miner daemon JSON-RPC endpoint.
221    pub async fn ping_miner_daemon(&self) -> Result<()> {
222        debug!(target: "darkfid::ping_miner_daemon", "Pinging miner daemon...");
223        self.miner_daemon_request("ping", &JsonValue::Array(vec![])).await?;
224        Ok(())
225    }
226
227    /// Auxiliary function to execute a request towards the configured miner daemon JSON-RPC endpoint.
228    pub async fn miner_daemon_request(
229        &self,
230        method: &str,
231        params: &JsonValue,
232    ) -> Result<JsonValue> {
233        let Some(ref rpc_client) = self.rpc_client else { return Err(Error::RpcClientStopped) };
234        debug!(target: "darkfid::rpc::miner_daemon_request", "Executing request {} with params: {:?}", method, params);
235        let latency = Instant::now();
236        let req = JsonRequest::new(method, params.clone());
237        let lock = rpc_client.lock().await;
238        let Some(ref client) = lock.client else { return Err(Error::RpcClientStopped) };
239        let rep = client.request(req).await?;
240        drop(lock);
241        let latency = latency.elapsed();
242        debug!(target: "darkfid::rpc::miner_daemon_request", "Got reply: {:?}", rep);
243        debug!(target: "darkfid::rpc::miner_daemon_request", "Latency: {:?}", latency);
244        Ok(rep)
245    }
246
247    /// Auxiliary function to execute a request towards the configured miner daemon JSON-RPC endpoint,
248    /// but in case of failure, sleep and retry until connection is re-established.
249    pub async fn miner_daemon_request_with_retry(
250        &self,
251        method: &str,
252        params: &JsonValue,
253    ) -> JsonValue {
254        loop {
255            // Try to execute the request using current client
256            match self.miner_daemon_request(method, params).await {
257                Ok(v) => return v,
258                Err(e) => {
259                    error!(target: "darkfid::rpc::miner_daemon_request_with_retry", "Failed to execute miner daemon request: {}", e);
260                }
261            }
262            loop {
263                // Sleep a bit before retrying
264                info!(target: "darkfid::rpc::miner_daemon_request_with_retry", "Sleeping so we can retry later");
265                sleep(10).await;
266                // Create a new client
267                let mut rpc_client = self.rpc_client.as_ref().unwrap().lock().await;
268                let Ok(client) =
269                    RpcChadClient::new(rpc_client.endpoint.clone(), rpc_client.ex.clone()).await
270                else {
271                    error!(target: "darkfid::rpc::miner_daemon_request_with_retry", "Failed to initialize miner daemon rpc client, check if minerd is running");
272                    drop(rpc_client);
273                    continue
274                };
275                info!(target: "darkfid::rpc::miner_daemon_request_with_retry", "Connection re-established!");
276                // Set the new client as the daemon one
277                rpc_client.client = Some(client);
278                break;
279            }
280        }
281    }
282}
283
284impl HandlerP2p for DarkfiNode {
285    fn p2p(&self) -> P2pPtr {
286        self.p2p_handler.p2p.clone()
287    }
288}