rpc.rs 3.5 KB

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