Skip to content

Commit

Permalink
added get_block json rpc method (#123)
Browse files Browse the repository at this point in the history
* added get_block json rpc method

* fix fmt check errors

* fix clippy warnings

* fix fmt warnings 2

---------

Co-authored-by: essecara <no@example.com>
Co-authored-by: h4sh3d <h4sh3d@protonmail.com>
  • Loading branch information
3 people committed May 30, 2024
1 parent 4e6e923 commit 6d44eef
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -409,6 +411,32 @@ impl DaemonJsonRpcClient {
.count)
}

pub async fn get_block(&self, selector: GetBlockHeaderSelector) -> anyhow::Result<Block> {
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::<Value>(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<BlockHash> {
let res = self
Expand Down

0 comments on commit 6d44eef

Please sign in to comment.