X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-block-sync%2Fsrc%2Frest.rs;fp=lightning-block-sync%2Fsrc%2Frest.rs;h=3c2e76e23d7d5ee2eebd4a957185a34fef629583;hb=85fdfaaa9c4421d8fbeb89202508ea7477730eb0;hp=3ccd5d482e48290e46007ebf16379a87aceeecc9;hpb=6ca3b8ed496290cd9b6c49525d57a887a735a914;p=rust-lightning diff --git a/lightning-block-sync/src/rest.rs b/lightning-block-sync/src/rest.rs index 3ccd5d48..3c2e76e2 100644 --- a/lightning-block-sync/src/rest.rs +++ b/lightning-block-sync/src/rest.rs @@ -1,4 +1,9 @@ -use crate::http::{HttpEndpoint, HttpClient}; +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,6 +16,8 @@ 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 { let client = HttpClient::connect(&endpoint)?; Ok(Self { endpoint, client }) @@ -25,6 +32,28 @@ impl RestClient { } } +impl BlockSource for RestClient { + fn get_header<'a>(&'a mut 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()); + Ok(self.request_resource::(&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::(&resource_path).await?) + }) + } + + fn get_best_block<'a>(&'a mut self) -> AsyncBlockSourceResult<'a, (BlockHash, Option)> { + Box::pin(async move { + Ok(self.request_resource::("chaininfo.json").await?) + }) + } +} + #[cfg(test)] mod tests { use super::*;