X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-block-sync%2Fsrc%2Frest.rs;h=4300893013c06d7a3ee81c715b256066f4d28333;hb=64c58a565b9a4165d795ae7db608e1dc149829ca;hp=2ddfed7dad84b12dc43d5ac318b31b223ca74051;hpb=711bcefbcc0999543d9622c030ff7dc8118fc26f;p=rust-lightning diff --git a/lightning-block-sync/src/rest.rs b/lightning-block-sync/src/rest.rs index 2ddfed7d..43008930 100644 --- a/lightning-block-sync/src/rest.rs +++ b/lightning-block-sync/src/rest.rs @@ -1,22 +1,20 @@ //! Simple REST client implementation which implements [`BlockSource`] against a Bitcoin Core REST //! endpoint. -use crate::{BlockHeaderData, BlockSource, AsyncBlockSourceResult}; +use crate::{BlockData, BlockHeaderData, BlockSource, AsyncBlockSourceResult}; use crate::http::{BinaryResponse, HttpEndpoint, HttpClient, JsonResponse}; -use bitcoin::blockdata::block::Block; use bitcoin::hash_types::BlockHash; use bitcoin::hashes::hex::ToHex; -use futures::lock::Mutex; - use std::convert::TryFrom; use std::convert::TryInto; +use std::sync::Mutex; /// A simple REST client for requesting resources using HTTP `GET`. pub struct RestClient { endpoint: HttpEndpoint, - client: Mutex, + client: Mutex>, } impl RestClient { @@ -24,8 +22,7 @@ impl RestClient { /// /// The endpoint should contain the REST path component (e.g., http://127.0.0.1:8332/rest). pub fn new(endpoint: HttpEndpoint) -> std::io::Result { - let client = Mutex::new(HttpClient::connect(&endpoint)?); - Ok(Self { endpoint, client }) + Ok(Self { endpoint, client: Mutex::new(None) }) } /// Requests a resource encoded in `F` format and interpreted as type `T`. @@ -33,7 +30,11 @@ impl RestClient { where F: TryFrom, Error = std::io::Error> + TryInto { let host = format!("{}:{}", self.endpoint.host(), self.endpoint.port()); let uri = format!("{}/{}", self.endpoint.path().trim_end_matches("/"), resource_path); - self.client.lock().await.get::(&uri, &host).await?.try_into() + let mut client = if let Some(client) = self.client.lock().unwrap().take() { client } + else { HttpClient::connect(&self.endpoint)? }; + let res = client.get::(&uri, &host).await?.try_into(); + *self.client.lock().unwrap() = Some(client); + res } } @@ -45,10 +46,10 @@ impl BlockSource for RestClient { }) } - fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block> { + fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, BlockData> { Box::pin(async move { let resource_path = format!("block/{}.bin", header_hash.to_hex()); - Ok(self.request_resource::(&resource_path).await?) + Ok(BlockData::FullBlock(self.request_resource::(&resource_path).await?)) }) }