rpc.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 async_trait::async_trait;
  19. use log::debug;
  20. use serde_json::{json, Value};
  21. use darkfi::{
  22. net,
  23. rpc::{
  24. jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult},
  25. server::RequestHandler,
  26. },
  27. };
  28. use crate::Darkfid;
  29. #[async_trait]
  30. impl RequestHandler for Darkfid {
  31. async fn handle_request(&self, req: JsonRequest) -> JsonResult {
  32. if req.params.as_array().is_none() {
  33. return JsonError::new(ErrorCode::InvalidRequest, None, req.id).into()
  34. }
  35. let params = req.params.as_array().unwrap();
  36. debug!(target: "RPC", "--> {}", serde_json::to_string(&req).unwrap());
  37. match req.method.as_str() {
  38. Some("ping") => self.pong(req.id, params).await,
  39. Some("dnet_switch") => self.dnet_switch(req.id, params).await,
  40. Some("dnet_info") => self.dnet_info(req.id, params).await,
  41. Some(_) | None => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
  42. }
  43. }
  44. }
  45. impl Darkfid {
  46. // RPCAPI:
  47. // Replies to a ping method.
  48. // --> {"jsonrpc": "2.0", "method": "ping", "params": [], "id": 42}
  49. // <-- {"jsonrpc": "2.0", "result": "pong", "id": 42}
  50. async fn pong(&self, id: Value, _params: &[Value]) -> JsonResult {
  51. JsonResponse::new(json!("pong"), id).into()
  52. }
  53. // RPCAPI:
  54. // Activate or deactivate dnet in the P2P stack.
  55. // By sending `true`, dnet will be activated, and by sending `false` dnet
  56. // will be deactivated. Returns `true` on success.
  57. //
  58. // --> {"jsonrpc": "2.0", "method": "dnet_switch", "params": [true], "id": 42}
  59. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  60. async fn dnet_switch(&self, id: Value, params: &[Value]) -> JsonResult {
  61. if params.len() != 1 && params[0].as_bool().is_none() {
  62. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  63. }
  64. if params[0].as_bool().unwrap() {
  65. self.sync_p2p.dnet_enable().await;
  66. if self.consensus_p2p.is_some() {
  67. self.consensus_p2p.clone().unwrap().dnet_enable().await;
  68. }
  69. } else {
  70. self.sync_p2p.dnet_disable().await;
  71. if self.consensus_p2p.is_some() {
  72. self.consensus_p2p.clone().unwrap().dnet_disable().await;
  73. }
  74. }
  75. JsonResponse::new(json!(true), id).into()
  76. }
  77. // RPCAPI:
  78. // Retrieves P2P network information.
  79. //
  80. // --> {"jsonrpc": "2.0", "method": "dnet_info", "params": [], "id": 42}
  81. // <-- {"jsonrpc": "2.0", result": {"nodeID": [], "nodeinfo": [], "id": 42}
  82. async fn dnet_info(&self, id: Value, _params: &[Value]) -> JsonResult {
  83. let mut dnet_info = self.sync_p2p.dnet_info().await;
  84. if self.consensus_p2p.is_some() {
  85. dnet_info.extend(self.consensus_p2p.clone().unwrap().dnet_info().await);
  86. }
  87. JsonResponse::new(net::P2p::map_dnet_info(dnet_info), id).into()
  88. }
  89. }