rpc.rs 3.3 KB

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