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