rpc_blockchain.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 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 darkfi_sdk::crypto::MerkleNode;
  19. use log::{debug, error};
  20. use serde_json::{json, Value};
  21. use darkfi::rpc::jsonrpc::{
  22. ErrorCode::{InternalError, InvalidParams},
  23. JsonError, JsonResponse, JsonResult,
  24. };
  25. use super::Darkfid;
  26. use crate::{server_error, RpcError};
  27. impl Darkfid {
  28. // RPCAPI:
  29. // Queries the blockchain database for a block in the given slot.
  30. // Returns a readable block upon success.
  31. //
  32. // --> {"jsonrpc": "2.0", "method": "blockchain.get_slot", "params": [0], "id": 1}
  33. // <-- {"jsonrpc": "2.0", "result": {...}, "id": 1}
  34. pub async fn blockchain_get_slot(&self, id: Value, params: &[Value]) -> JsonResult {
  35. if params.len() != 1 || !params[0].is_u64() {
  36. return JsonError::new(InvalidParams, None, id).into()
  37. }
  38. let slot = params[0].as_u64().unwrap();
  39. let validator_state = self.validator_state.read().await;
  40. let blocks = match validator_state.blockchain.get_blocks_by_slot(&[slot]) {
  41. Ok(v) => {
  42. drop(validator_state);
  43. v
  44. }
  45. Err(e) => {
  46. error!("[RPC] blockchain.get_slot: Failed fetching block by slot: {}", e);
  47. return JsonError::new(InternalError, None, id).into()
  48. }
  49. };
  50. if blocks.is_empty() {
  51. return server_error(RpcError::UnknownSlot, id, None)
  52. }
  53. // TODO: Return block as JSON
  54. debug!("{:#?}", blocks[0]);
  55. JsonResponse::new(json!(true), id).into()
  56. }
  57. // RPCAPI:
  58. // Queries the blockchain database for all available merkle roots.
  59. //
  60. // --> {"jsonrpc": "2.0", "method": "blockchain.merkle_roots", "params": [], "id": 1}
  61. // <-- {"jsonrpc": "2.0", "result": [..., ..., ...], "id": 1}
  62. pub async fn blockchain_merkle_roots(&self, id: Value, params: &[Value]) -> JsonResult {
  63. if !params.is_empty() {
  64. return JsonError::new(InvalidParams, None, id).into()
  65. }
  66. let validator_state = self.validator_state.read().await;
  67. let roots: Vec<MerkleNode> = match validator_state.blockchain.merkle_roots.get_all() {
  68. Ok(v) => {
  69. drop(validator_state);
  70. v
  71. }
  72. Err(e) => {
  73. error!("[RPC] blockchain.merkle_roots: Failed fetching merkle roots from rootstore: {}", e);
  74. return JsonError::new(InternalError, None, id).into()
  75. }
  76. };
  77. let roots: Vec<String> = roots.iter().map(|x| x.to_string()).collect();
  78. JsonResponse::new(json!(roots), id).into()
  79. }
  80. }