diff --git a/bin/darkfid/src/rpc/misc.rs b/bin/darkfid/src/rpc/misc.rs new file mode 100644 index 000000000..9034adf7b --- /dev/null +++ b/bin/darkfid/src/rpc/misc.rs @@ -0,0 +1,98 @@ +/* This file is part of DarkFi (https://dark.fi) + * + * Copyright (C) 2020-2026 Dyne.org foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +use tinyjson::JsonValue; + +use darkfi::{ + rpc::jsonrpc::{ErrorCode, JsonError, JsonResponse, JsonResult}, + util::time::Timestamp, +}; + +use crate::DarkfiNode; + +impl DarkfiNode { + // RPCAPI: + // Returns current system clock as a UNIX timestamp. + // + // --> {"jsonrpc": "2.0", "method": "clock", "params": [], "id": 1} + // <-- {"jsonrpc": "2.0", "result": 1767015913, "id": 1} + pub async fn clock(&self, id: u16, _params: JsonValue) -> JsonResult { + JsonResponse::new((Timestamp::current_time().inner() as f64).into(), id).into() + } + + // RPCAPI: + // Activate or deactivate dnet in the P2P stack. + // By sending `true`, dnet will be activated, and by sending `false` dnet + // will be deactivated. + // + // Returns `true` on success. + // + // --> {"jsonrpc": "2.0", "method": "dnet.switch", "params": [true], "id": 1} + // <-- {"jsonrpc": "2.0", "result": true, "id": 1} + pub async fn dnet_switch(&self, id: u16, params: JsonValue) -> JsonResult { + let Some(params) = params.get::>() else { + return JsonError::new(ErrorCode::InvalidParams, None, id).into() + }; + if params.len() != 1 || !params[0].is_bool() { + return JsonError::new(ErrorCode::InvalidParams, None, id).into() + } + + let switch = params[0].get::().unwrap(); + + if *switch { + self.p2p_handler.p2p.dnet_enable(); + } else { + self.p2p_handler.p2p.dnet_disable(); + } + + JsonResponse::new(JsonValue::Boolean(true), id).into() + } + + // RPCAPI: + // Initializes a subscription to P2P dnet events. + // Once a subscription is established, `darkfid` will send JSON-RPC + // notifications of new network events to the subscriber. + // + // --> { + // "jsonrpc": "2.0", + // "method": "dnet.subscribe_events", + // "params": [], + // "id": 1 + // } + // <-- { + // "jsonrpc": "2.0", + // "method": "dnet.subscribe_events", + // "params": [ + // { + // "chan": {"Channel": "Info"}, + // "cmd": "command", + // "time": 1767016282 + // } + // ] + // } + pub async fn dnet_subscribe_events(&self, id: u16, params: JsonValue) -> JsonResult { + let Some(params) = params.get::>() else { + return JsonError::new(ErrorCode::InvalidParams, None, id).into() + }; + if !params.is_empty() { + return JsonError::new(ErrorCode::InvalidParams, None, id).into() + } + + self.subscribers.get("dnet").unwrap().clone().into() + } +} diff --git a/bin/darkfid/src/rpc/mod.rs b/bin/darkfid/src/rpc/mod.rs index f4ecdeb6d..298c3041d 100644 --- a/bin/darkfid/src/rpc/mod.rs +++ b/bin/darkfid/src/rpc/mod.rs @@ -20,18 +20,16 @@ use std::collections::HashSet; use async_trait::async_trait; use smol::lock::MutexGuard; -use tinyjson::JsonValue; use tracing::debug; use darkfi::{ net::P2pPtr, rpc::{ - jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult}, + jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResult}, p2p_method::HandlerP2p, server::RequestHandler, }, system::StoppableTaskPtr, - util::time::Timestamp, }; use crate::DarkfiNode; @@ -48,6 +46,9 @@ pub mod stratum; /// HTTP JSON-RPC related methods for merge mining pub mod xmr; +/// Misc JSON-RPC methods +pub mod misc; + /// Default JSON-RPC `RequestHandler` pub struct DefaultRpcHandler; @@ -104,78 +105,6 @@ impl RequestHandler for DarkfiNode { } } -impl DarkfiNode { - // RPCAPI: - // Returns current system clock as a UNIX timestamp. - // - // --> {"jsonrpc": "2.0", "method": "clock", "params": [], "id": 1} - // <-- {"jsonrpc": "2.0", "result": 1767015913, "id": 1} - async fn clock(&self, id: u16, _params: JsonValue) -> JsonResult { - JsonResponse::new((Timestamp::current_time().inner() as f64).into(), id).into() - } - - // RPCAPI: - // Activate or deactivate dnet in the P2P stack. - // By sending `true`, dnet will be activated, and by sending `false` dnet - // will be deactivated. - // - // Returns `true` on success. - // - // --> {"jsonrpc": "2.0", "method": "dnet.switch", "params": [true], "id": 1} - // <-- {"jsonrpc": "2.0", "result": true, "id": 1} - async fn dnet_switch(&self, id: u16, params: JsonValue) -> JsonResult { - let Some(params) = params.get::>() else { - return JsonError::new(ErrorCode::InvalidParams, None, id).into() - }; - if params.len() != 1 || !params[0].is_bool() { - return JsonError::new(ErrorCode::InvalidParams, None, id).into() - } - - let switch = params[0].get::().unwrap(); - - if *switch { - self.p2p_handler.p2p.dnet_enable(); - } else { - self.p2p_handler.p2p.dnet_disable(); - } - - JsonResponse::new(JsonValue::Boolean(true), id).into() - } - - // RPCAPI: - // Initializes a subscription to P2P dnet events. - // Once a subscription is established, `darkfid` will send JSON-RPC - // notifications of new network events to the subscriber. - // - // --> { - // "jsonrpc": "2.0", - // "method": "dnet.subscribe_events", - // "params": [], - // "id": 1 - // } - // <-- { - // "jsonrpc": "2.0", - // "method": "dnet.subscribe_events", - // "params": [ - // { - // "chan": {"Channel": "Info"}, - // "cmd": "command", - // "time": 1767016282 - // } - // ] - // } - pub async fn dnet_subscribe_events(&self, id: u16, params: JsonValue) -> JsonResult { - let Some(params) = params.get::>() else { - return JsonError::new(ErrorCode::InvalidParams, None, id).into() - }; - if !params.is_empty() { - return JsonError::new(ErrorCode::InvalidParams, None, id).into() - } - - self.subscribers.get("dnet").unwrap().clone().into() - } -} - impl HandlerP2p for DarkfiNode { fn p2p(&self) -> P2pPtr { self.p2p_handler.p2p.clone() diff --git a/doc/Makefile b/doc/Makefile index b2ebfa6c4..2db28dcf1 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -19,8 +19,9 @@ all: $(JSONRPC) $(DARKFID_JSONRPC): @echo "# darkfid JSON-RPC API" > $@ - ./build_jsonrpc.py ../bin/darkfid/src/rpc/mod.rs >> $@ - for i in blockchain stratum tx xmr; do \ + @echo "## Methods" >> $@ + @echo "" >> $@ + for i in blockchain tx stratum xmr misc; do \ echo "## $$i methods" >> $@ ;\ ./build_jsonrpc.py ../bin/darkfid/src/rpc/$$i.rs >> $@ ;\ done diff --git a/doc/build_jsonrpc.py b/doc/build_jsonrpc.py index 84232d0e0..2facf7561 100755 --- a/doc/build_jsonrpc.py +++ b/doc/build_jsonrpc.py @@ -106,7 +106,7 @@ def main(path): "response": json.dumps(parsed_res, indent=2), "line": start_line }) - + for m in methods: anchor = m["method"].replace(".", "").replace("/", "").lower() print(f"### `{m['method']}`\n")