fud/rpc/
management.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, sync::Arc};
20
21use async_trait::async_trait;
22use smol::lock::{Mutex, MutexGuard};
23use tinyjson::JsonValue;
24use tracing::debug;
25
26use darkfi::{
27    net::P2pPtr,
28    rpc::{
29        jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult, JsonSubscriber},
30        p2p_method::HandlerP2p,
31        server::RequestHandler,
32    },
33    system::StoppableTaskPtr,
34};
35
36use crate::Fud;
37
38pub struct ManagementRpcInterface {
39    fud: Arc<Fud>,
40    rpc_connections: Mutex<HashSet<StoppableTaskPtr>>,
41    dnet_sub: JsonSubscriber,
42}
43
44#[async_trait]
45impl RequestHandler<()> for ManagementRpcInterface {
46    async fn handle_request(&self, req: JsonRequest) -> JsonResult {
47        debug!(target: "fud::rpc::management_rpc", "--> {}", req.stringify().unwrap());
48
49        match req.method.as_str() {
50            "ping" => self.pong(req.id, req.params).await,
51            "dnet.switch" => self.dnet_switch(req.id, req.params).await,
52            "dnet.subscribe_events" => self.dnet_subscribe_events(req.id, req.params).await,
53            "p2p.get_info" => self.p2p_get_info(req.id, req.params).await,
54            _ => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
55        }
56    }
57
58    async fn connections_mut(&self) -> MutexGuard<'life0, HashSet<StoppableTaskPtr>> {
59        self.rpc_connections.lock().await
60    }
61}
62
63impl HandlerP2p for ManagementRpcInterface {
64    fn p2p(&self) -> P2pPtr {
65        self.fud.p2p.clone()
66    }
67}
68
69/// Fud management RPC methods
70impl ManagementRpcInterface {
71    pub fn new(fud: Arc<Fud>, dnet_sub: JsonSubscriber) -> Self {
72        Self { fud, rpc_connections: Mutex::new(HashSet::new()), dnet_sub }
73    }
74
75    // RPCAPI:
76    // Activate or deactivate dnet in the P2P stack.
77    // By sending `true`, dnet will be activated, and by sending `false` dnet
78    // will be deactivated.
79    //
80    // Returns `true` on success.
81    //
82    // --> {"jsonrpc": "2.0", "method": "dnet.switch", "params": [true], "id": 1}
83    // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
84    pub async fn dnet_switch(&self, id: u16, params: JsonValue) -> JsonResult {
85        let Some(params) = params.get::<Vec<JsonValue>>() else {
86            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
87        };
88        if params.len() != 1 || !params[0].is_bool() {
89            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
90        }
91
92        let switch = params[0].get::<bool>().unwrap();
93
94        if *switch {
95            self.fud.p2p.dnet_enable();
96        } else {
97            self.fud.p2p.dnet_disable();
98        }
99
100        JsonResponse::new(JsonValue::Boolean(true), id).into()
101    }
102
103    // RPCAPI:
104    // Initializes a subscription to P2P dnet events.
105    // Once a subscription is established, `fud` will send JSON-RPC
106    // notifications of new network events to the subscriber.
107    //
108    // --> {
109    //       "jsonrpc": "2.0",
110    //       "method": "dnet.subscribe_events",
111    //       "params": [],
112    //       "id": 1
113    //     }
114    // <-- {
115    //       "jsonrpc": "2.0",
116    //       "method": "dnet.subscribe_events",
117    //       "params": [
118    //         {
119    //           "chan": {"Channel": "Info"},
120    //           "cmd": "command",
121    //           "time": 1767016282
122    //         }
123    //       ]
124    //     }
125    pub async fn dnet_subscribe_events(&self, id: u16, params: JsonValue) -> JsonResult {
126        let Some(params) = params.get::<Vec<JsonValue>>() else {
127            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
128        };
129        if !params.is_empty() {
130            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
131        }
132
133        self.dnet_sub.clone().into()
134    }
135}