Merge pull request #892 from TheBlueMatt/2021-04-fix-htlc-ser
[rust-lightning] / lightning-block-sync / src / rest.rs
index 3ccd5d482e48290e46007ebf16379a87aceeecc9..e04bb86d4270f9c7aec1bc06f49a62b7af2e056a 100644 (file)
@@ -1,4 +1,12 @@
-use crate::http::{HttpEndpoint, HttpClient};
+//! Simple REST client implementation which implements [`BlockSource`] against a Bitcoin Core REST
+//! endpoint.
+
+use crate::{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 std::convert::TryFrom;
 use std::convert::TryInto;
@@ -11,13 +19,15 @@ pub struct RestClient {
 
 impl RestClient {
        /// Creates a new REST client connected to the given endpoint.
+       ///
+       /// 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)?;
                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>(&mut 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);
@@ -25,6 +35,28 @@ impl RestClient {
        }
 }
 
+impl BlockSource for RestClient {
+       fn get_header<'a>(&'a mut 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> {
+               Box::pin(async move {
+                       let resource_path = format!("block/{}.bin", header_hash.to_hex());
+                       Ok(self.request_resource::<BinaryResponse, _>(&resource_path).await?)
+               })
+       }
+
+       fn get_best_block<'a>(&'a mut self) -> AsyncBlockSourceResult<'a, (BlockHash, Option<u32>)> {
+               Box::pin(async move {
+                       Ok(self.request_resource::<JsonResponse, _>("chaininfo.json").await?)
+               })
+       }
+}
+
 #[cfg(test)]
 mod tests {
        use super::*;
@@ -52,7 +84,7 @@ mod tests {
                let mut 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"),
                }
        }