darkfid/rpc/
mod.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2026 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 tracing::debug;
24
25use darkfi::{
26    rpc::{
27        jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResult},
28        server::RequestHandler,
29    },
30    system::StoppableTaskPtr,
31};
32
33use crate::DarkfiNode;
34
35/// Blockchain related methods
36mod blockchain;
37
38/// Transactions related methods
39mod tx;
40
41/// Stratum JSON-RPC related methods for native mining
42pub mod stratum;
43
44/// HTTP JSON-RPC related methods for merge mining
45pub mod xmr;
46
47/// Misc JSON-RPC methods
48pub mod misc;
49
50/// Node management JSON-RPC methods
51pub mod management;
52
53/// Default JSON-RPC `RequestHandler`
54pub struct DefaultRpcHandler;
55
56#[async_trait]
57#[rustfmt::skip]
58impl RequestHandler<DefaultRpcHandler> for DarkfiNode {
59    async fn handle_request(&self, req: JsonRequest) -> JsonResult {
60        debug!(target: "darkfid::rpc", "--> {}", req.stringify().unwrap());
61
62        match req.method.as_str() {
63            // =====================
64            // Miscellaneous methods
65            // =====================
66            "ping" => <DarkfiNode as RequestHandler<DefaultRpcHandler>>::pong(self, req.id, req.params).await,
67            "clock" => self.clock(req.id, req.params).await,
68
69            // ==================
70            // Blockchain methods
71            // ==================
72            "blockchain.get_block" => self.blockchain_get_block(req.id, req.params).await,
73            "blockchain.get_tx" => self.blockchain_get_tx(req.id, req.params).await,
74            "blockchain.last_confirmed_block" => self.blockchain_last_confirmed_block(req.id, req.params).await,
75            "blockchain.best_fork_next_block_height" => self.blockchain_best_fork_next_block_height(req.id, req.params).await,
76            "blockchain.block_target" => self.blockchain_block_target(req.id, req.params).await,
77            "blockchain.lookup_wasm" => self.blockchain_lookup_wasm(req.id, req.params).await,
78            "blockchain.lookup_zkas" => self.blockchain_lookup_zkas(req.id, req.params).await,
79            "blockchain.get_contract_state" => self.blockchain_get_contract_state(req.id, req.params).await,
80            "blockchain.get_contract_state_key" => self.blockchain_get_contract_state_key(req.id, req.params).await,
81            "blockchain.subscribe_blocks" => self.blockchain_subscribe_blocks(req.id, req.params).await,
82            "blockchain.subscribe_txs" =>  self.blockchain_subscribe_txs(req.id, req.params).await,
83            "blockchain.subscribe_proposals" => self.blockchain_subscribe_proposals(req.id, req.params).await,
84
85            // ===================
86            // Transaction methods
87            // ===================
88            "tx.simulate" => self.tx_simulate(req.id, req.params).await,
89            "tx.broadcast" => self.tx_broadcast(req.id, req.params).await,
90            "tx.pending" => self.tx_pending(req.id, req.params).await,
91            "tx.clean_pending" => self.tx_clean_pending(req.id, req.params).await,
92            "tx.calculate_fee" => self.tx_calculate_fee(req.id, req.params).await,
93
94            // ==============
95            // Invalid method
96            // ==============
97            _ => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
98        }
99    }
100
101    async fn connections_mut(&self) -> MutexGuard<'life0, HashSet<StoppableTaskPtr>> {
102        self.rpc_connections.lock().await
103    }
104}