X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-block-sync%2Fsrc%2Frest.rs;h=74a460a7ab505d24d70582dece828638c373e593;hb=4b70921c958181e43dc056dc05ef59427d13f2af;hp=4c19388275fb935f18301600e211c118aeccda5b;hpb=e6c922803f8d1f0303dd8a8f9300e32de55e1fc9;p=rust-lightning diff --git a/lightning-block-sync/src/rest.rs b/lightning-block-sync/src/rest.rs index 4c193882..74a460a7 100644 --- a/lightning-block-sync/src/rest.rs +++ b/lightning-block-sync/src/rest.rs @@ -1,20 +1,22 @@ //! 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 crate::gossip::UtxoSource; +use crate::convert::GetUtxosResponse; -use bitcoin::blockdata::block::Block; +use bitcoin::OutPoint; use bitcoin::hash_types::BlockHash; -use bitcoin::hashes::hex::ToHex; 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: HttpClient, + client: Mutex>, } impl RestClient { @@ -22,46 +24,68 @@ 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 = 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`. - pub async fn request_resource(&mut self, resource_path: &str) -> std::io::Result + pub async fn request_resource(&self, resource_path: &str) -> std::io::Result 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.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 } } impl BlockSource for RestClient { - fn get_header<'a>(&'a mut self, header_hash: &'a BlockHash, _height: Option) -> AsyncBlockSourceResult<'a, BlockHeaderData> { + fn get_header<'a>(&'a self, header_hash: &'a BlockHash, _height: Option) -> AsyncBlockSourceResult<'a, BlockHeaderData> { Box::pin(async move { - let resource_path = format!("headers/1/{}.json", header_hash.to_hex()); + let resource_path = format!("headers/1/{}.json", header_hash.to_string()); Ok(self.request_resource::(&resource_path).await?) }) } - fn get_block<'a>(&'a mut 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?) + let resource_path = format!("block/{}.bin", header_hash.to_string()); + Ok(BlockData::FullBlock(self.request_resource::(&resource_path).await?)) }) } - fn get_best_block<'a>(&'a mut self) -> AsyncBlockSourceResult<'a, (BlockHash, Option)> { + fn get_best_block<'a>(&'a self) -> AsyncBlockSourceResult<'a, (BlockHash, Option)> { Box::pin(async move { Ok(self.request_resource::("chaininfo.json").await?) }) } } +impl UtxoSource for RestClient { + fn get_block_hash_by_height<'a>(&'a self, block_height: u32) -> AsyncBlockSourceResult<'a, BlockHash> { + Box::pin(async move { + let resource_path = format!("blockhashbyheight/{}.bin", block_height); + Ok(self.request_resource::(&resource_path).await?) + }) + } + + fn is_output_unspent<'a>(&'a self, outpoint: OutPoint) -> AsyncBlockSourceResult<'a, bool> { + Box::pin(async move { + let resource_path = format!("getutxos/{}-{}.json", outpoint.txid.to_string(), outpoint.vout); + let utxo_result = + self.request_resource::(&resource_path).await?; + Ok(utxo_result.hit_bitmap_nonempty) + }) + } +} + #[cfg(test)] mod tests { use super::*; use crate::http::BinaryResponse; use crate::http::client_tests::{HttpServer, MessageBody}; + use bitcoin::hashes::Hash; /// Parses binary data as a string-encoded `u32`. impl TryInto for BinaryResponse { @@ -81,10 +105,10 @@ mod tests { #[tokio::test] async fn request_unknown_resource() { let server = HttpServer::responding_with_not_found(); - let mut client = RestClient::new(server.endpoint()).unwrap(); + let client = RestClient::new(server.endpoint()).unwrap(); match client.request_resource::("/").await { - Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::NotFound), + Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::Other), Ok(_) => panic!("Expected error"), } } @@ -92,7 +116,7 @@ mod tests { #[tokio::test] async fn request_malformed_resource() { let server = HttpServer::responding_with_ok(MessageBody::Content("foo")); - let mut client = RestClient::new(server.endpoint()).unwrap(); + let client = RestClient::new(server.endpoint()).unwrap(); match client.request_resource::("/").await { Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::InvalidData), @@ -103,11 +127,39 @@ mod tests { #[tokio::test] async fn request_valid_resource() { let server = HttpServer::responding_with_ok(MessageBody::Content(42)); - let mut client = RestClient::new(server.endpoint()).unwrap(); + let client = RestClient::new(server.endpoint()).unwrap(); match client.request_resource::("/").await { Err(e) => panic!("Unexpected error: {:?}", e), Ok(n) => assert_eq!(n, 42), } } + + #[tokio::test] + async fn parses_negative_getutxos() { + let server = HttpServer::responding_with_ok(MessageBody::Content( + // A real response contains a few more fields, but we actually only look at the + // "bitmap" field, so this should suffice for testing + "{\"chainHeight\": 1, \"bitmap\":\"0\",\"utxos\":[]}" + )); + let client = RestClient::new(server.endpoint()).unwrap(); + + let outpoint = OutPoint::new(bitcoin::Txid::from_byte_array([0; 32]), 0); + let unspent_output = client.is_output_unspent(outpoint).await.unwrap(); + assert_eq!(unspent_output, false); + } + + #[tokio::test] + async fn parses_positive_getutxos() { + let server = HttpServer::responding_with_ok(MessageBody::Content( + // A real response contains lots more data, but we actually only look at the "bitmap" + // field, so this should suffice for testing + "{\"chainHeight\": 1, \"bitmap\":\"1\",\"utxos\":[]}" + )); + let client = RestClient::new(server.endpoint()).unwrap(); + + let outpoint = OutPoint::new(bitcoin::Txid::from_byte_array([0; 32]), 0); + let unspent_output = client.is_output_unspent(outpoint).await.unwrap(); + assert_eq!(unspent_output, true); + } }