Skip to content

Commit

Permalink
get_block_binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Danconnolly committed May 20, 2024
1 parent 4780576 commit 0a003b5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ name = "bitcoinsv_rpc"
path = "src/lib.rs"

[dependencies]
tokio = { version = ">=1.23.1", features = ["full"] }
bitcoinsv-rpc-json = { version = "0.19.6", path = "../json" }
log = "0.4.5"
jsonrpc = { version = "0.16.0", features = ["minreq_http"]}
Expand Down
14 changes: 9 additions & 5 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use log::Level::{Debug, Trace, Warn};
use bitcoinsv::bitcoin::{BlockHeader, FullBlockStream, Tx, TxHash, BlockHash};
use bitcoinsv::util::Amount;
use bitcoinsv_rpc_json::GetNetworkInfoResult;

use tokio::io::{AsyncRead};
use crate::error::*;
use crate::json;

Expand Down Expand Up @@ -226,16 +226,20 @@ pub trait RpcApi: Sized {
self.call("getbestblockhash", &[])
}


/// Fetch a complete block from the node.
/// Fetch a complete block from the node, returning a binary reader.
///
/// todo: This method of using getblock over the RPC interface is a terrible way to get blocks.
/// It will use at least three times the size of the block in RAM on the client machine.
/// Twice to retrieve the hex representation of the block and once to deserialize that to binary.
async fn get_block(&self, hash: &BlockHash) -> Result<FullBlockStream> {
async fn get_block_binary(&self, hash: &BlockHash) -> Result<Box<dyn AsyncRead + Unpin + Send>> {
let hex: String = self.call("getblock", &[into_json(hash)?, 0.into()])?;
let buf = hex::decode(hex)?;
let f = FullBlockStream::new(Box::new(Cursor::new(buf))).await?;
return Ok(Box::new(Cursor::new(buf)));
}

/// Fetch a complete block from the node, returning a FullBlockStream.
async fn get_block(&self, hash: &BlockHash) -> Result<FullBlockStream> {
let f = FullBlockStream::new(Box::new(self.get_block_binary(hash).await.unwrap())).await?;
Ok(f)
}

Expand Down

0 comments on commit 0a003b5

Please sign in to comment.