From 6d44eefa8849e1e1e19f16f1a2a12a5bb55ac514 Mon Sep 17 00:00:00 2001 From: Piit Date: Thu, 30 May 2024 10:41:29 +0200 Subject: [PATCH] added get_block json rpc method (#123) * added get_block json rpc method * fix fmt check errors * fix clippy warnings * fix fmt warnings 2 --------- Co-authored-by: essecara Co-authored-by: h4sh3d --- src/lib.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 5e2534e..f3c3b00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,6 +63,8 @@ pub use self::{models::*, util::*}; use jsonrpc_core::types::{Id, *}; use monero::{ + blockdata::block::Block, + consensus::deserialize, cryptonote::{hash::Hash as CryptoNoteHash, subaddress}, util::{address::PaymentId, amount}, Address, Amount, @@ -409,6 +411,32 @@ impl DaemonJsonRpcClient { .count) } + pub async fn get_block(&self, selector: GetBlockHeaderSelector) -> anyhow::Result { + let (request, params) = match selector { + GetBlockHeaderSelector::Hash(hash) => ( + "get_block", + RpcParams::map( + Some(("hash", serde_json::to_value(HashString(hash)).unwrap())).into_iter(), + ), + ), + GetBlockHeaderSelector::Height(height) => ( + "get_block", + RpcParams::map(Some(("height", height.into())).into_iter()), + ), + GetBlockHeaderSelector::Last => ("get_block", RpcParams::None), + }; + + match self.inner.request::(request, params).await { + Ok(res) => { + let block_str = res["blob"].as_str().unwrap(); + let block_hex = hex::decode(block_str).unwrap(); + let block: Block = deserialize(&block_hex).unwrap(); + Ok(block) + } + Err(e) => Err(anyhow::Error::msg(format!("Can not fetch block: {}", e))), + } + } + /// Look up a block's hash by its height. pub async fn on_get_block_hash(&self, height: u64) -> anyhow::Result { let res = self