rpc.rs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. use std::collections::HashSet;
  19. use async_trait::async_trait;
  20. use darkfi::{
  21. event_graph::util::recreate_from_replayer_log,
  22. net::P2pPtr,
  23. rpc::{
  24. jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult},
  25. p2p_method::HandlerP2p,
  26. server::RequestHandler,
  27. util::JsonValue,
  28. },
  29. system::StoppableTaskPtr,
  30. };
  31. use smol::lock::MutexGuard;
  32. use tracing::debug;
  33. use super::DarkIrc;
  34. #[async_trait]
  35. impl RequestHandler<()> for DarkIrc {
  36. async fn handle_request(&self, req: JsonRequest) -> JsonResult {
  37. debug!(target: "darkirc::rpc", "--> {}", req.stringify().unwrap());
  38. match req.method.as_str() {
  39. "ping" => self.pong(req.id, req.params).await,
  40. "dnet.switch" => self.dnet_switch(req.id, req.params).await,
  41. "dnet.subscribe_events" => self.dnet_subscribe_events(req.id, req.params).await,
  42. "p2p.get_info" => self.p2p_get_info(req.id, req.params).await,
  43. "deg.switch" => self.deg_switch(req.id, req.params).await,
  44. "deg.subscribe_events" => self.deg_subscribe_events(req.id, req.params).await,
  45. "eventgraph.get_info" => self.eg_get_info(req.id, req.params).await,
  46. "eventgraph.replay" => self.eg_rep_info(req.id, req.params).await,
  47. _ => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
  48. }
  49. }
  50. async fn connections_mut(&self) -> MutexGuard<'life0, HashSet<StoppableTaskPtr>> {
  51. self.rpc_connections.lock().await
  52. }
  53. }
  54. impl DarkIrc {
  55. // RPCAPI:
  56. // Activate or deactivate dnet in the P2P stack.
  57. // By sending `true`, dnet will be activated, and by sending `false` dnet
  58. // will be deactivated. Returns `true` on success.
  59. //
  60. // --> {"jsonrpc": "2.0", "method": "dnet.switch", "params": [true], "id": 42}
  61. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  62. async fn dnet_switch(&self, id: u16, params: JsonValue) -> JsonResult {
  63. let Some(params) = params.get::<Vec<JsonValue>>() else {
  64. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  65. };
  66. if params.len() != 1 || !params[0].is_bool() {
  67. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  68. }
  69. let switch = params[0].get::<bool>().unwrap();
  70. if *switch {
  71. self.p2p.dnet_enable();
  72. } else {
  73. self.p2p.dnet_disable();
  74. }
  75. JsonResponse::new(JsonValue::Boolean(true), id).into()
  76. }
  77. // RPCAPI:
  78. // Initializes a subscription to p2p dnet events.
  79. // Once a subscription is established, `darkirc` will send JSON-RPC notifications of
  80. // new network events to the subscriber.
  81. //
  82. // --> {"jsonrpc": "2.0", "method": "dnet.subscribe_events", "params": [], "id": 1}
  83. // <-- {"jsonrpc": "2.0", "method": "dnet.subscribe_events", "params": [`event`]}
  84. pub async fn dnet_subscribe_events(&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.is_empty() {
  89. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  90. }
  91. self.dnet_sub.clone().into()
  92. }
  93. // RPCAPI:
  94. // Initializes a subscription to deg events.
  95. // Once a subscription is established, apps using eventgraph will send JSON-RPC notifications of
  96. // new eventgraph events to the subscriber.
  97. //
  98. // --> {"jsonrpc": "2.0", "method": "deg.subscribe_events", "params": [], "id": 1}
  99. // <-- {"jsonrpc": "2.0", "method": "deg.subscribe_events", "params": [`event`]}
  100. pub async fn deg_subscribe_events(&self, id: u16, params: JsonValue) -> JsonResult {
  101. let Some(params) = params.get::<Vec<JsonValue>>() else {
  102. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  103. };
  104. if !params.is_empty() {
  105. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  106. }
  107. self.deg_sub.clone().into()
  108. }
  109. // RPCAPI:
  110. // Activate or deactivate deg in the EVENTGRAPH.
  111. // By sending `true`, deg will be activated, and by sending `false` deg
  112. // will be deactivated. Returns `true` on success.
  113. //
  114. // --> {"jsonrpc": "2.0", "method": "deg.switch", "params": [true], "id": 42}
  115. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  116. async fn deg_switch(&self, id: u16, params: JsonValue) -> JsonResult {
  117. let Some(params) = params.get::<Vec<JsonValue>>() else {
  118. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  119. };
  120. if params.len() != 1 || !params[0].is_bool() {
  121. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  122. }
  123. let switch = params[0].get::<bool>().unwrap();
  124. if *switch {
  125. self.event_graph.deg_enable().await;
  126. } else {
  127. self.event_graph.deg_disable().await;
  128. }
  129. JsonResponse::new(JsonValue::Boolean(true), id).into()
  130. }
  131. // RPCAPI:
  132. // Get EVENTGRAPH info.
  133. //
  134. // --> {"jsonrpc": "2.0", "method": "deg.switch", "params": [true], "id": 42}
  135. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  136. async fn eg_get_info(&self, id: u16, params: JsonValue) -> JsonResult {
  137. let Some(params_) = params.get::<Vec<JsonValue>>() else {
  138. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  139. };
  140. if !params_.is_empty() {
  141. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  142. }
  143. self.event_graph.eventgraph_info(id, params).await
  144. }
  145. // RPCAPI:
  146. // Get replayed EVENTGRAPH info.
  147. //
  148. // --> {"jsonrpc": "2.0", "method": "eventgraph.replay", "params": ..., "id": 42}
  149. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  150. async fn eg_rep_info(&self, id: u16, params: JsonValue) -> JsonResult {
  151. let Some(params_) = params.get::<Vec<JsonValue>>() else {
  152. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  153. };
  154. if !params_.is_empty() {
  155. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  156. }
  157. recreate_from_replayer_log(&self.replay_datastore).await
  158. }
  159. }
  160. impl HandlerP2p for DarkIrc {
  161. fn p2p(&self) -> P2pPtr {
  162. self.p2p.clone()
  163. }
  164. }