rpc.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 tinyjson::JsonValue;
  21. use url::Url;
  22. use darkfi::{
  23. net,
  24. rpc::{
  25. jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult, JsonSubscriber},
  26. p2p_method::HandlerP2p,
  27. server::RequestHandler,
  28. },
  29. };
  30. pub struct JsonRpcInterface {
  31. pub addr: Url,
  32. pub p2p: net::P2pPtr,
  33. pub dnet_sub: JsonSubscriber,
  34. }
  35. #[async_trait]
  36. impl RequestHandler for JsonRpcInterface {
  37. async fn handle_request(&self, req: JsonRequest) -> JsonResult {
  38. debug!(target: "darkirc::rpc", "--> {}", req.stringify().unwrap());
  39. match req.method.as_str() {
  40. "ping" => self.pong(req.id, req.params).await,
  41. "dnet.switch" => self.dnet_switch(req.id, req.params).await,
  42. "dnet.subscribe_events" => self.dnet_subscribe_events(req.id, req.params).await,
  43. "p2p.get_info" => self.p2p_get_info(req.id, req.params).await,
  44. _ => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
  45. }
  46. }
  47. }
  48. impl JsonRpcInterface {
  49. // RPCAPI:
  50. // Activate or deactivate dnet in the P2P stack.
  51. // By sending `true`, dnet will be activated, and by sending `false` dnet
  52. // will be deactivated. Returns `true` on success.
  53. //
  54. // --> {"jsonrpc": "2.0", "method": "dnet.switch", "params": [true], "id": 42}
  55. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  56. async fn dnet_switch(&self, id: u16, params: JsonValue) -> JsonResult {
  57. let params = params.get::<Vec<JsonValue>>().unwrap();
  58. if params.len() != 1 || !params[0].is_bool() {
  59. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  60. }
  61. let switch = params[0].get::<bool>().unwrap();
  62. if *switch {
  63. self.p2p.dnet_enable().await;
  64. } else {
  65. self.p2p.dnet_disable().await;
  66. }
  67. JsonResponse::new(JsonValue::Boolean(true), id).into()
  68. }
  69. // RPCAPI:
  70. // Initializes a subscription to p2p dnet events.
  71. // Once a subscription is established, `darkirc` will send JSON-RPC notifications of
  72. // new network events to the subscriber.
  73. //
  74. // --> {"jsonrpc": "2.0", "method": "dnet.subscribe_events", "params": [], "id": 1}
  75. // <-- {"jsonrpc": "2.0", "method": "dnet.subscribe_events", "params": [`event`]}
  76. pub async fn dnet_subscribe_events(&self, id: u16, params: JsonValue) -> JsonResult {
  77. let params = params.get::<Vec<JsonValue>>().unwrap();
  78. if !params.is_empty() {
  79. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  80. }
  81. self.dnet_sub.clone().into()
  82. }
  83. }
  84. impl HandlerP2p for JsonRpcInterface {
  85. fn p2p(&self) -> net::P2pPtr {
  86. self.p2p.clone()
  87. }
  88. }