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;
20
21use async_trait::async_trait;
22use smol::lock::MutexGuard;
23use tinyjson::JsonValue;
24use tracing::debug;
25
26use darkfi::{
27    net::P2pPtr,
28    rpc::{
29        jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult},
30        p2p_method::HandlerP2p,
31        server::RequestHandler,
32    },
33    system::StoppableTaskPtr,
34    util::time::Timestamp,
35};
36
37use crate::DarkfiNode;
38
39/// Default JSON-RPC `RequestHandler` type
40pub struct DefaultRpcHandler;
41/// HTTP JSON-RPC `RequestHandler` type for p2pool
42pub struct MmRpcHandler;
43
44#[async_trait]
45#[rustfmt::skip]
46impl RequestHandler<DefaultRpcHandler> for DarkfiNode {
47    async fn handle_request(&self, req: JsonRequest) -> JsonResult {
48        debug!(target: "darkfid::rpc", "--> {}", req.stringify().unwrap());
49
50        match req.method.as_str() {
51            // =====================
52            // Miscellaneous methods
53            // =====================
54            "ping" => <DarkfiNode as RequestHandler<DefaultRpcHandler>>::pong(self, req.id, req.params).await,
55            "clock" => self.clock(req.id, req.params).await,
56            "dnet.switch" => self.dnet_switch(req.id, req.params).await,
57            "dnet.subscribe_events" => self.dnet_subscribe_events(req.id, req.params).await,
58            "p2p.get_info" => self.p2p_get_info(req.id, req.params).await,
59
60            // ==================
61            // Blockchain methods
62            // ==================
63            "blockchain.get_block" => self.blockchain_get_block(req.id, req.params).await,
64            "blockchain.get_tx" => self.blockchain_get_tx(req.id, req.params).await,
65            "blockchain.last_confirmed_block" => self.blockchain_last_confirmed_block(req.id, req.params).await,
66            "blockchain.best_fork_next_block_height" => self.blockchain_best_fork_next_block_height(req.id, req.params).await,
67            "blockchain.block_target" => self.blockchain_block_target(req.id, req.params).await,
68            "blockchain.lookup_zkas" => self.blockchain_lookup_zkas(req.id, req.params).await,
69            "blockchain.get_contract_state" => self.blockchain_get_contract_state(req.id, req.params).await,
70            "blockchain.get_contract_state_key" => self.blockchain_get_contract_state_key(req.id, req.params).await,
71            "blockchain.subscribe_blocks" => self.blockchain_subscribe_blocks(req.id, req.params).await,
72            "blockchain.subscribe_txs" =>  self.blockchain_subscribe_txs(req.id, req.params).await,
73            "blockchain.subscribe_proposals" => self.blockchain_subscribe_proposals(req.id, req.params).await,
74
75            // ===================
76            // Transaction methods
77            // ===================
78            "tx.simulate" => self.tx_simulate(req.id, req.params).await,
79            "tx.broadcast" => self.tx_broadcast(req.id, req.params).await,
80            "tx.pending" => self.tx_pending(req.id, req.params).await,
81            "tx.clean_pending" => self.tx_clean_pending(req.id, req.params).await,
82            "tx.calculate_fee" => self.tx_calculate_fee(req.id, req.params).await,
83
84            // =============
85            // Miner methods
86            // =============
87            "miner.get_current_randomx_keys" => self.miner_get_current_randomx_keys(req.id, req.params).await,
88            "miner.get_header" => self.miner_get_header(req.id, req.params).await,
89            "miner.submit_solution" => self.miner_submit_solution(req.id, req.params).await,
90
91            // ==============
92            // Invalid method
93            // ==============
94            _ => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
95        }
96    }
97
98    async fn connections_mut(&self) -> MutexGuard<'life0, HashSet<StoppableTaskPtr>> {
99        self.rpc_connections.lock().await
100    }
101}
102
103#[async_trait]
104#[rustfmt::skip]
105impl RequestHandler<MmRpcHandler> for DarkfiNode {
106    async fn handle_request(&self, req: JsonRequest) -> JsonResult {
107        debug!(target: "darkfid::mm_rpc", "--> {}", req.stringify().unwrap());
108
109        match req.method.as_str() {
110            // ================================================
111            // P2Pool methods requested for Monero Merge Mining
112            // ================================================
113            "merge_mining_get_chain_id" => self.xmr_merge_mining_get_chain_id(req.id, req.params).await,
114            "merge_mining_get_aux_block" => self.xmr_merge_mining_get_aux_block(req.id, req.params).await,
115            "merge_mining_submit_solution" => self.xmr_merge_mining_submit_solution(req.id, req.params).await,
116
117            // ==============
118            // Invalid method
119            // ==============
120            _ => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
121        }
122    }
123
124    async fn connections_mut(&self) -> MutexGuard<'life0, HashSet<StoppableTaskPtr>> {
125        self.mm_rpc_connections.lock().await
126    }
127}
128
129impl DarkfiNode {
130    // RPCAPI:
131    // Returns current system clock as `u64` (String) timestamp.
132    //
133    // --> {"jsonrpc": "2.0", "method": "clock", "params": [], "id": 1}
134    // <-- {"jsonrpc": "2.0", "result": "1234", "id": 1}
135    async fn clock(&self, id: u16, _params: JsonValue) -> JsonResult {
136        JsonResponse::new(JsonValue::String(Timestamp::current_time().inner().to_string()), id)
137            .into()
138    }
139
140    // RPCAPI:
141    // Activate or deactivate dnet in the P2P stack.
142    // By sending `true`, dnet will be activated, and by sending `false` dnet
143    // will be deactivated. Returns `true` on success.
144    //
145    // --> {"jsonrpc": "2.0", "method": "dnet_switch", "params": [true], "id": 42}
146    // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
147    async fn dnet_switch(&self, id: u16, params: JsonValue) -> JsonResult {
148        let params = params.get::<Vec<JsonValue>>().unwrap();
149        if params.len() != 1 || !params[0].is_bool() {
150            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
151        }
152
153        let switch = params[0].get::<bool>().unwrap();
154
155        if *switch {
156            self.p2p_handler.p2p.dnet_enable();
157        } else {
158            self.p2p_handler.p2p.dnet_disable();
159        }
160
161        JsonResponse::new(JsonValue::Boolean(true), id).into()
162    }
163
164    // RPCAPI:
165    // Initializes a subscription to p2p dnet events.
166    // Once a subscription is established, `darkfid` will send JSON-RPC notifications of
167    // new network events to the subscriber.
168    //
169    // --> {"jsonrpc": "2.0", "method": "dnet.subscribe_events", "params": [], "id": 1}
170    // <-- {"jsonrpc": "2.0", "method": "dnet.subscribe_events", "params": [`event`]}
171    pub async fn dnet_subscribe_events(&self, id: u16, params: JsonValue) -> JsonResult {
172        let params = params.get::<Vec<JsonValue>>().unwrap();
173        if !params.is_empty() {
174            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
175        }
176
177        self.subscribers.get("dnet").unwrap().clone().into()
178    }
179}
180
181impl HandlerP2p for DarkfiNode {
182    fn p2p(&self) -> P2pPtr {
183        self.p2p_handler.p2p.clone()
184    }
185}