rpc_misc.rs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 tinyjson::JsonValue;
  19. use darkfi::{
  20. rpc::jsonrpc::{ErrorCode, JsonError, JsonResponse, JsonResult},
  21. util::time::Timestamp,
  22. };
  23. use super::Darkfid;
  24. impl Darkfid {
  25. // RPCAPI:
  26. // Returns current system clock as u64 (string) timestamp
  27. //
  28. // --> {"jsonrpc": "2.0", "method": "clock", "params": [], "id": 1}
  29. // <-- {"jsonrpc": "2.0", "result": "1234"}, "id": 1}
  30. pub async fn misc_clock(&self, id: u16, _params: JsonValue) -> JsonResult {
  31. JsonResponse::new(JsonValue::String(Timestamp::current_time().0.to_string()), id).into()
  32. }
  33. // RPCAPI:
  34. // Activate or deactivate dnet in the sync P2P stack.
  35. // By sending `true`, dnet will be activated, and by sending `false` dnet
  36. // will be deactivated. Returns `true` on success.
  37. //
  38. // --> {"jsonrpc": "2.0", "method": "sync_dnet_switch", "params": [true], "id": 42}
  39. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  40. pub async fn misc_sync_dnet_switch(&self, id: u16, params: JsonValue) -> JsonResult {
  41. let params = params.get::<Vec<JsonValue>>().unwrap();
  42. if params.len() != 1 || !params[0].is_bool() {
  43. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  44. }
  45. let switch = params[0].get::<bool>().unwrap();
  46. if *switch {
  47. self.sync_p2p.as_ref().unwrap().dnet_enable().await;
  48. } else {
  49. self.sync_p2p.as_ref().unwrap().dnet_disable().await;
  50. }
  51. JsonResponse::new(JsonValue::Boolean(true), id).into()
  52. }
  53. // RPCAPI:
  54. // Activate or deactivate dnet in the consensus 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": "consensus_dnet_switch", "params": [true], "id": 42}
  59. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  60. pub async fn misc_consensus_dnet_switch(&self, id: u16, params: JsonValue) -> JsonResult {
  61. let params = params.get::<Vec<JsonValue>>().unwrap();
  62. if params.len() != 1 || !params[0].is_bool() {
  63. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  64. }
  65. let switch = params[0].get::<bool>().unwrap();
  66. if *switch {
  67. self.consensus_p2p.as_ref().unwrap().dnet_enable().await;
  68. } else {
  69. self.consensus_p2p.as_ref().unwrap().dnet_disable().await;
  70. }
  71. JsonResponse::new(JsonValue::Boolean(true), id).into()
  72. }
  73. }