From 0a003b5b77cf1137d9f6c57e3f8f3f87453cdee7 Mon Sep 17 00:00:00 2001 From: Daniel Connolly Date: Mon, 20 May 2024 07:44:09 +0000 Subject: [PATCH] get_block_binary --- Cargo.lock | 1 + client/Cargo.toml | 1 + client/src/client.rs | 14 +++++++++----- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 80256688..597962a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,6 +104,7 @@ dependencies = [ "serde", "serde_json", "tempfile", + "tokio", ] [[package]] diff --git a/client/Cargo.toml b/client/Cargo.toml index 1adaf61f..7ba58294 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -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"]} diff --git a/client/src/client.rs b/client/src/client.rs index efa93c8f..fb95bf83 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -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; @@ -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 { + async fn get_block_binary(&self, hash: &BlockHash) -> Result> { 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 { + let f = FullBlockStream::new(Box::new(self.get_block_binary(hash).await.unwrap())).await?; Ok(f) }