rpc.rs 3.2 KB

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