mod.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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 smol::lock::MutexGuard;
  21. use tracing::debug;
  22. use darkfi::{
  23. rpc::{
  24. jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResult},
  25. server::RequestHandler,
  26. },
  27. system::StoppableTaskPtr,
  28. };
  29. use crate::DarkfiNode;
  30. /// Blockchain related methods
  31. mod blockchain;
  32. /// Transactions related methods
  33. mod tx;
  34. /// Stratum JSON-RPC related methods for native mining
  35. pub mod stratum;
  36. /// HTTP JSON-RPC related methods for merge mining
  37. pub mod xmr;
  38. /// Misc JSON-RPC methods
  39. pub mod misc;
  40. /// Node management JSON-RPC methods
  41. pub mod management;
  42. /// Default JSON-RPC `RequestHandler`
  43. pub struct DefaultRpcHandler;
  44. #[async_trait]
  45. #[rustfmt::skip]
  46. impl RequestHandler<DefaultRpcHandler> for DarkfiNode {
  47. async fn handle_request(&self, req: JsonRequest) -> JsonResult {
  48. debug!(target: "darkfid::rpc", "--> {}", req.stringify().unwrap());
  49. match req.method.as_str() {
  50. // =====================
  51. // Miscellaneous methods
  52. // =====================
  53. "ping" => <DarkfiNode as RequestHandler<DefaultRpcHandler>>::pong(self, req.id, req.params).await,
  54. "clock" => self.clock(req.id, req.params).await,
  55. // ==================
  56. // Blockchain methods
  57. // ==================
  58. "blockchain.get_block" => self.blockchain_get_block(req.id, req.params).await,
  59. "blockchain.get_tx" => self.blockchain_get_tx(req.id, req.params).await,
  60. "blockchain.get_difficulty" => self.blockchain_get_difficulty(req.id, req.params).await,
  61. "blockchain.last_confirmed_block" => self.blockchain_last_confirmed_block(req.id, req.params).await,
  62. "blockchain.best_fork_next_block_height" => self.blockchain_best_fork_next_block_height(req.id, req.params).await,
  63. "blockchain.block_target" => self.blockchain_block_target(req.id, req.params).await,
  64. "blockchain.lookup_wasm" => self.blockchain_lookup_wasm(req.id, req.params).await,
  65. "blockchain.lookup_zkas" => self.blockchain_lookup_zkas(req.id, req.params).await,
  66. "blockchain.get_contract_state" => self.blockchain_get_contract_state(req.id, req.params).await,
  67. "blockchain.get_contract_state_key" => self.blockchain_get_contract_state_key(req.id, req.params).await,
  68. "blockchain.subscribe_blocks" => self.blockchain_subscribe_blocks(req.id, req.params).await,
  69. "blockchain.subscribe_txs" => self.blockchain_subscribe_txs(req.id, req.params).await,
  70. "blockchain.subscribe_proposals" => self.blockchain_subscribe_proposals(req.id, req.params).await,
  71. // ===================
  72. // Transaction methods
  73. // ===================
  74. "tx.simulate" => self.tx_simulate(req.id, req.params).await,
  75. "tx.broadcast" => self.tx_broadcast(req.id, req.params).await,
  76. "tx.pending" => self.tx_pending(req.id, req.params).await,
  77. "tx.rebroadcast_pending" => self.tx_rebroadcast_pending(req.id, req.params).await,
  78. "tx.clean_pending" => self.tx_clean_pending(req.id, req.params).await,
  79. "tx.calculate_fee" => self.tx_calculate_fee(req.id, req.params).await,
  80. // ==============
  81. // Invalid method
  82. // ==============
  83. _ => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
  84. }
  85. }
  86. async fn connections_mut(&self) -> MutexGuard<'life0, HashSet<StoppableTaskPtr>> {
  87. self.rpc_connections.lock().await
  88. }
  89. }