rpc.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 std::collections::HashSet;
  19. use async_trait::async_trait;
  20. use log::debug;
  21. use smol::lock::MutexGuard;
  22. use tinyjson::JsonValue;
  23. use darkfi::{
  24. rpc::{
  25. jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult},
  26. server::RequestHandler,
  27. },
  28. system::StoppableTaskPtr,
  29. util::time::Timestamp,
  30. };
  31. use crate::Darkfid;
  32. #[async_trait]
  33. #[rustfmt::skip]
  34. impl RequestHandler for Darkfid {
  35. async fn handle_request(&self, req: JsonRequest) -> JsonResult {
  36. debug!(target: "darkfid::rpc", "--> {}", req.stringify().unwrap());
  37. match req.method.as_str() {
  38. // =====================
  39. // Miscellaneous methods
  40. // =====================
  41. "ping" => return self.pong(req.id, req.params).await,
  42. "clock" => return self.clock(req.id, req.params).await,
  43. "sync_dnet_switch" => return self.sync_dnet_switch(req.id, req.params).await,
  44. "consensus_dnet_switch" => return self.consensus_dnet_switch(req.id, req.params).await,
  45. // ==================
  46. // Blockchain methods
  47. // ==================
  48. "blockchain.get_slot" => return self.blockchain_get_slot(req.id, req.params).await,
  49. "blockchain.get_tx" => return self.blockchain_get_tx(req.id, req.params).await,
  50. "blockchain.last_known_slot" => return self.blockchain_last_known_slot(req.id, req.params).await,
  51. "blockchain.lookup_zkas" => return self.blockchain_lookup_zkas(req.id, req.params).await,
  52. "blockchain.subscribe_blocks" => return self.blockchain_subscribe_blocks(req.id, req.params).await,
  53. "blockchain.subscribe_txs" => return self.blockchain_subscribe_txs(req.id, req.params).await,
  54. "blockchain.subscribe_proposals" => return self.blockchain_subscribe_proposals(req.id, req.params).await,
  55. "merge_mining_get_chain_id" => return self.merge_mining_get_chain_id(req.id, req.params).await,
  56. // ===================
  57. // Transaction methods
  58. // ===================
  59. "tx.simulate" => return self.tx_simulate(req.id, req.params).await,
  60. "tx.broadcast" => return self.tx_broadcast(req.id, req.params).await,
  61. "tx.pending" => return self.tx_pending(req.id, req.params).await,
  62. "tx.clean_pending" => return self.tx_pending(req.id, req.params).await,
  63. // ==============
  64. // Invalid method
  65. // ==============
  66. _ => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
  67. }
  68. }
  69. async fn connections_mut(&self) -> MutexGuard<'_, HashSet<StoppableTaskPtr>> {
  70. self.rpc_connections.lock().await
  71. }
  72. }
  73. impl Darkfid {
  74. // RPCAPI:
  75. // Returns current system clock as `u64` (String) timestamp.
  76. //
  77. // --> {"jsonrpc": "2.0", "method": "clock", "params": [], "id": 1}
  78. // <-- {"jsonrpc": "2.0", "result": "1234", "id": 1}
  79. async fn clock(&self, id: u16, _params: JsonValue) -> JsonResult {
  80. JsonResponse::new(JsonValue::String(Timestamp::current_time().0.to_string()), id).into()
  81. }
  82. // RPCAPI:
  83. // Activate or deactivate dnet in the sync P2P stack.
  84. // By sending `true`, dnet will be activated, and by sending `false` dnet
  85. // will be deactivated. Returns `true` on success.
  86. //
  87. // --> {"jsonrpc": "2.0", "method": "sync_dnet_switch", "params": [true], "id": 42}
  88. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  89. async fn sync_dnet_switch(&self, id: u16, params: JsonValue) -> JsonResult {
  90. let params = params.get::<Vec<JsonValue>>().unwrap();
  91. if params.len() != 1 || !params[0].is_bool() {
  92. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  93. }
  94. let switch = params[0].get::<bool>().unwrap();
  95. if *switch {
  96. self.sync_p2p.dnet_enable().await;
  97. } else {
  98. self.sync_p2p.dnet_disable().await;
  99. }
  100. JsonResponse::new(JsonValue::Boolean(true), id).into()
  101. }
  102. // RPCAPI:
  103. // Activate or deactivate dnet in the consensus P2P stack.
  104. // By sending `true`, dnet will be activated, and by sending `false` dnet
  105. // will be deactivated. Returns `true` on success.
  106. //
  107. // --> {"jsonrpc": "2.0", "method": "consensus_dnet_switch", "params": [true], "id": 42}
  108. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  109. async fn consensus_dnet_switch(&self, id: u16, params: JsonValue) -> JsonResult {
  110. let params = params.get::<Vec<JsonValue>>().unwrap();
  111. if params.len() != 1 || !params[0].is_bool() {
  112. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  113. }
  114. if self.consensus_p2p.is_some() {
  115. let switch = params[0].get::<bool>().unwrap();
  116. if *switch {
  117. self.consensus_p2p.clone().unwrap().dnet_enable().await;
  118. } else {
  119. self.consensus_p2p.clone().unwrap().dnet_disable().await;
  120. }
  121. }
  122. JsonResponse::new(JsonValue::Boolean(true), id).into()
  123. }
  124. }