rpc.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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. net::P2pPtr,
  22. rpc::{
  23. jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult},
  24. p2p_method::HandlerP2p,
  25. server::RequestHandler,
  26. util::JsonValue,
  27. },
  28. system::StoppableTaskPtr,
  29. };
  30. use log::debug;
  31. use smol::lock::MutexGuard;
  32. use super::DarkIrc;
  33. #[async_trait]
  34. impl RequestHandler for DarkIrc {
  35. async fn handle_request(&self, req: JsonRequest) -> JsonResult {
  36. debug!(target: "darkirc::rpc", "--> {}", req.stringify().unwrap());
  37. match req.method.as_str() {
  38. "ping" => self.pong(req.id, req.params).await,
  39. "dnet.switch" => self.dnet_switch(req.id, req.params).await,
  40. "dnet.subscribe_events" => self.dnet_subscribe_events(req.id, req.params).await,
  41. // TODO: Make this optional
  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. _ => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
  47. }
  48. }
  49. async fn connections_mut(&self) -> MutexGuard<'_, HashSet<StoppableTaskPtr>> {
  50. self.rpc_connections.lock().await
  51. }
  52. }
  53. impl DarkIrc {
  54. // RPCAPI:
  55. // Activate or deactivate dnet in the P2P stack.
  56. // By sending `true`, dnet will be activated, and by sending `false` dnet
  57. // will be deactivated. Returns `true` on success.
  58. //
  59. // --> {"jsonrpc": "2.0", "method": "dnet.switch", "params": [true], "id": 42}
  60. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  61. async fn dnet_switch(&self, id: u16, params: JsonValue) -> JsonResult {
  62. let params = params.get::<Vec<JsonValue>>().unwrap();
  63. if params.len() != 1 || !params[0].is_bool() {
  64. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  65. }
  66. let switch = params[0].get::<bool>().unwrap();
  67. if *switch {
  68. self.p2p.dnet_enable().await;
  69. } else {
  70. self.p2p.dnet_disable().await;
  71. }
  72. JsonResponse::new(JsonValue::Boolean(true), id).into()
  73. }
  74. // RPCAPI:
  75. // Initializes a subscription to p2p dnet events.
  76. // Once a subscription is established, `darkirc` will send JSON-RPC notifications of
  77. // new network events to the subscriber.
  78. //
  79. // --> {"jsonrpc": "2.0", "method": "dnet.subscribe_events", "params": [], "id": 1}
  80. // <-- {"jsonrpc": "2.0", "method": "dnet.subscribe_events", "params": [`event`]}
  81. pub async fn dnet_subscribe_events(&self, id: u16, params: JsonValue) -> JsonResult {
  82. let params = params.get::<Vec<JsonValue>>().unwrap();
  83. if !params.is_empty() {
  84. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  85. }
  86. self.dnet_sub.clone().into()
  87. }
  88. // RPCAPI:
  89. // Initializes a subscription to deg events.
  90. // Once a subscription is established, apps using eventgraph will send JSON-RPC notifications of
  91. // new eventgraph events to the subscriber.
  92. //
  93. // --> {"jsonrpc": "2.0", "method": "deg.subscribe_events", "params": [], "id": 1}
  94. // <-- {"jsonrpc": "2.0", "method": "deg.subscribe_events", "params": [`event`]}
  95. pub async fn deg_subscribe_events(&self, id: u16, params: JsonValue) -> JsonResult {
  96. let params = params.get::<Vec<JsonValue>>().unwrap();
  97. if !params.is_empty() {
  98. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  99. }
  100. self.deg_sub.clone().into()
  101. }
  102. // RPCAPI:
  103. // Activate or deactivate deg in the EVENTGRAPH.
  104. // By sending `true`, deg will be activated, and by sending `false` deg
  105. // will be deactivated. Returns `true` on success.
  106. //
  107. // --> {"jsonrpc": "2.0", "method": "deg.switch", "params": [true], "id": 42}
  108. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  109. async fn deg_switch(&self, id: u16, params: JsonValue) -> JsonResult {
  110. let params = params.get::<Vec<JsonValue>>().unwrap();
  111. if params.len() != 1 || !params[0].is_bool() {
  112. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  113. }
  114. let switch = params[0].get::<bool>().unwrap();
  115. if *switch {
  116. self.event_graph.deg_enable().await;
  117. } else {
  118. self.event_graph.deg_disable().await;
  119. }
  120. JsonResponse::new(JsonValue::Boolean(true), id).into()
  121. }
  122. // RPCAPI:
  123. // Get EVENTGRAPH info.
  124. //
  125. // --> {"jsonrpc": "2.0", "method": "deg.switch", "params": [true], "id": 42}
  126. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  127. async fn eg_get_info(&self, id: u16, params: JsonValue) -> JsonResult {
  128. let params_ = params.get::<Vec<JsonValue>>().unwrap();
  129. if !params_.is_empty() {
  130. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  131. }
  132. self.event_graph.eventgraph_info(id, params).await
  133. }
  134. }
  135. impl HandlerP2p for DarkIrc {
  136. fn p2p(&self) -> P2pPtr {
  137. self.p2p.clone()
  138. }
  139. }