Merge pull request #2102 from douglaz/node_info_addresses
[rust-lightning] / lightning-block-sync / src / rest.rs
index 3c2e76e23d7d5ee2eebd4a957185a34fef629583..c73b23b600c2925e3e0a085a99fc75560527f064 100644 (file)
@@ -1,17 +1,21 @@
-use crate::{BlockHeaderData, BlockSource, AsyncBlockSourceResult};
+//! Simple REST client implementation which implements [`BlockSource`] against a Bitcoin Core REST
+//! endpoint.
+
+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_util::lock::Mutex;
+
 use std::convert::TryFrom;
 use std::convert::TryInto;
 
 /// A simple REST client for requesting resources using HTTP `GET`.
 pub struct RestClient {
        endpoint: HttpEndpoint,
-       client: HttpClient,
+       client: Mutex<HttpClient>,
 }
 
 impl RestClient {
@@ -19,35 +23,35 @@ 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<Self> {
-               let client = HttpClient::connect(&endpoint)?;
+               let client = Mutex::new(HttpClient::connect(&endpoint)?);
                Ok(Self { endpoint, client })
        }
 
        /// Requests a resource encoded in `F` format and interpreted as type `T`.
-       async fn request_resource<F, T>(&mut self, resource_path: &str) -> std::io::Result<T>
+       pub async fn request_resource<F, T>(&self, resource_path: &str) -> std::io::Result<T>
        where F: TryFrom<Vec<u8>, Error = std::io::Error> + TryInto<T, Error = std::io::Error> {
                let host = format!("{}:{}", self.endpoint.host(), self.endpoint.port());
                let uri = format!("{}/{}", self.endpoint.path().trim_end_matches("/"), resource_path);
-               self.client.get::<F>(&uri, &host).await?.try_into()
+               self.client.lock().await.get::<F>(&uri, &host).await?.try_into()
        }
 }
 
 impl BlockSource for RestClient {
-       fn get_header<'a>(&'a mut self, header_hash: &'a BlockHash, _height: Option<u32>) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
+       fn get_header<'a>(&'a self, header_hash: &'a BlockHash, _height: Option<u32>) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
                Box::pin(async move {
                        let resource_path = format!("headers/1/{}.json", header_hash.to_hex());
                        Ok(self.request_resource::<JsonResponse, _>(&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::<BinaryResponse, _>(&resource_path).await?)
+                       Ok(BlockData::FullBlock(self.request_resource::<BinaryResponse, _>(&resource_path).await?))
                })
        }
 
-       fn get_best_block<'a>(&'a mut self) -> AsyncBlockSourceResult<'a, (BlockHash, Option<u32>)> {
+       fn get_best_block<'a>(&'a self) -> AsyncBlockSourceResult<'a, (BlockHash, Option<u32>)> {
                Box::pin(async move {
                        Ok(self.request_resource::<JsonResponse, _>("chaininfo.json").await?)
                })
@@ -78,10 +82,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::<BinaryResponse, u32>("/").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"),
                }
        }
@@ -89,7 +93,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::<BinaryResponse, u32>("/").await {
                        Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::InvalidData),
@@ -100,7 +104,7 @@ 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::<BinaryResponse, u32>("/").await {
                        Err(e) => panic!("Unexpected error: {:?}", e),