mirror of
https://github.com/lightningdevkit/rust-lightning.git
synced 2025-02-24 06:57:53 +01:00
rustfmt
: Run on lightning-block-sync/src/rest.rs
This commit is contained in:
parent
26cacb60b5
commit
634821a95d
2 changed files with 33 additions and 20 deletions
|
@ -1,13 +1,13 @@
|
||||||
//! Simple REST client implementation which implements [`BlockSource`] against a Bitcoin Core REST
|
//! Simple REST client implementation which implements [`BlockSource`] against a Bitcoin Core REST
|
||||||
//! endpoint.
|
//! endpoint.
|
||||||
|
|
||||||
use crate::{BlockData, BlockHeaderData, BlockSource, AsyncBlockSourceResult};
|
|
||||||
use crate::http::{BinaryResponse, HttpEndpoint, HttpClient, JsonResponse};
|
|
||||||
use crate::gossip::UtxoSource;
|
|
||||||
use crate::convert::GetUtxosResponse;
|
use crate::convert::GetUtxosResponse;
|
||||||
|
use crate::gossip::UtxoSource;
|
||||||
|
use crate::http::{BinaryResponse, HttpClient, HttpEndpoint, JsonResponse};
|
||||||
|
use crate::{AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource};
|
||||||
|
|
||||||
use bitcoin::OutPoint;
|
|
||||||
use bitcoin::hash_types::BlockHash;
|
use bitcoin::hash_types::BlockHash;
|
||||||
|
use bitcoin::OutPoint;
|
||||||
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
@ -29,11 +29,16 @@ impl RestClient {
|
||||||
|
|
||||||
/// Requests a resource encoded in `F` format and interpreted as type `T`.
|
/// Requests a resource encoded in `F` format and interpreted as type `T`.
|
||||||
pub async fn request_resource<F, T>(&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> {
|
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 host = format!("{}:{}", self.endpoint.host(), self.endpoint.port());
|
||||||
let uri = format!("{}/{}", self.endpoint.path().trim_end_matches("/"), resource_path);
|
let uri = format!("{}/{}", self.endpoint.path().trim_end_matches("/"), resource_path);
|
||||||
let mut client = if let Some(client) = self.client.lock().unwrap().take() { client }
|
let mut client = if let Some(client) = self.client.lock().unwrap().take() {
|
||||||
else { HttpClient::connect(&self.endpoint)? };
|
client
|
||||||
|
} else {
|
||||||
|
HttpClient::connect(&self.endpoint)?
|
||||||
|
};
|
||||||
let res = client.get::<F>(&uri, &host).await?.try_into();
|
let res = client.get::<F>(&uri, &host).await?.try_into();
|
||||||
*self.client.lock().unwrap() = Some(client);
|
*self.client.lock().unwrap() = Some(client);
|
||||||
res
|
res
|
||||||
|
@ -41,29 +46,37 @@ impl RestClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BlockSource for RestClient {
|
impl BlockSource for RestClient {
|
||||||
fn get_header<'a>(&'a 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 {
|
Box::pin(async move {
|
||||||
let resource_path = format!("headers/1/{}.json", header_hash.to_string());
|
let resource_path = format!("headers/1/{}.json", header_hash.to_string());
|
||||||
Ok(self.request_resource::<JsonResponse, _>(&resource_path).await?)
|
Ok(self.request_resource::<JsonResponse, _>(&resource_path).await?)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, BlockData> {
|
fn get_block<'a>(
|
||||||
|
&'a self, header_hash: &'a BlockHash,
|
||||||
|
) -> AsyncBlockSourceResult<'a, BlockData> {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let resource_path = format!("block/{}.bin", header_hash.to_string());
|
let resource_path = format!("block/{}.bin", header_hash.to_string());
|
||||||
Ok(BlockData::FullBlock(self.request_resource::<BinaryResponse, _>(&resource_path).await?))
|
Ok(BlockData::FullBlock(
|
||||||
|
self.request_resource::<BinaryResponse, _>(&resource_path).await?,
|
||||||
|
))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_best_block<'a>(&'a self) -> AsyncBlockSourceResult<'a, (BlockHash, Option<u32>)> {
|
fn get_best_block<'a>(&'a self) -> AsyncBlockSourceResult<'a, (BlockHash, Option<u32>)> {
|
||||||
Box::pin(async move {
|
Box::pin(
|
||||||
Ok(self.request_resource::<JsonResponse, _>("chaininfo.json").await?)
|
async move { Ok(self.request_resource::<JsonResponse, _>("chaininfo.json").await?) },
|
||||||
})
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UtxoSource for RestClient {
|
impl UtxoSource for RestClient {
|
||||||
fn get_block_hash_by_height<'a>(&'a self, block_height: u32) -> AsyncBlockSourceResult<'a, BlockHash> {
|
fn get_block_hash_by_height<'a>(
|
||||||
|
&'a self, block_height: u32,
|
||||||
|
) -> AsyncBlockSourceResult<'a, BlockHash> {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let resource_path = format!("blockhashbyheight/{}.bin", block_height);
|
let resource_path = format!("blockhashbyheight/{}.bin", block_height);
|
||||||
Ok(self.request_resource::<BinaryResponse, _>(&resource_path).await?)
|
Ok(self.request_resource::<BinaryResponse, _>(&resource_path).await?)
|
||||||
|
@ -72,7 +85,8 @@ impl UtxoSource for RestClient {
|
||||||
|
|
||||||
fn is_output_unspent<'a>(&'a self, outpoint: OutPoint) -> AsyncBlockSourceResult<'a, bool> {
|
fn is_output_unspent<'a>(&'a self, outpoint: OutPoint) -> AsyncBlockSourceResult<'a, bool> {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let resource_path = format!("getutxos/{}-{}.json", outpoint.txid.to_string(), outpoint.vout);
|
let resource_path =
|
||||||
|
format!("getutxos/{}-{}.json", outpoint.txid.to_string(), outpoint.vout);
|
||||||
let utxo_result =
|
let utxo_result =
|
||||||
self.request_resource::<JsonResponse, GetUtxosResponse>(&resource_path).await?;
|
self.request_resource::<JsonResponse, GetUtxosResponse>(&resource_path).await?;
|
||||||
Ok(utxo_result.hit_bitmap_nonempty)
|
Ok(utxo_result.hit_bitmap_nonempty)
|
||||||
|
@ -83,8 +97,8 @@ impl UtxoSource for RestClient {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::http::BinaryResponse;
|
|
||||||
use crate::http::client_tests::{HttpServer, MessageBody};
|
use crate::http::client_tests::{HttpServer, MessageBody};
|
||||||
|
use crate::http::BinaryResponse;
|
||||||
use bitcoin::hashes::Hash;
|
use bitcoin::hashes::Hash;
|
||||||
|
|
||||||
/// Parses binary data as a string-encoded `u32`.
|
/// Parses binary data as a string-encoded `u32`.
|
||||||
|
@ -97,7 +111,7 @@ mod tests {
|
||||||
Ok(s) => match u32::from_str_radix(s, 10) {
|
Ok(s) => match u32::from_str_radix(s, 10) {
|
||||||
Err(e) => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e)),
|
Err(e) => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e)),
|
||||||
Ok(n) => Ok(n),
|
Ok(n) => Ok(n),
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,7 +154,7 @@ mod tests {
|
||||||
let server = HttpServer::responding_with_ok(MessageBody::Content(
|
let server = HttpServer::responding_with_ok(MessageBody::Content(
|
||||||
// A real response contains a few more fields, but we actually only look at the
|
// A real response contains a few more fields, but we actually only look at the
|
||||||
// "bitmap" field, so this should suffice for testing
|
// "bitmap" field, so this should suffice for testing
|
||||||
"{\"chainHeight\": 1, \"bitmap\":\"0\",\"utxos\":[]}"
|
"{\"chainHeight\": 1, \"bitmap\":\"0\",\"utxos\":[]}",
|
||||||
));
|
));
|
||||||
let client = RestClient::new(server.endpoint()).unwrap();
|
let client = RestClient::new(server.endpoint()).unwrap();
|
||||||
|
|
||||||
|
@ -154,7 +168,7 @@ mod tests {
|
||||||
let server = HttpServer::responding_with_ok(MessageBody::Content(
|
let server = HttpServer::responding_with_ok(MessageBody::Content(
|
||||||
// A real response contains lots more data, but we actually only look at the "bitmap"
|
// A real response contains lots more data, but we actually only look at the "bitmap"
|
||||||
// field, so this should suffice for testing
|
// field, so this should suffice for testing
|
||||||
"{\"chainHeight\": 1, \"bitmap\":\"1\",\"utxos\":[]}"
|
"{\"chainHeight\": 1, \"bitmap\":\"1\",\"utxos\":[]}",
|
||||||
));
|
));
|
||||||
let client = RestClient::new(server.endpoint()).unwrap();
|
let client = RestClient::new(server.endpoint()).unwrap();
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
./lightning-background-processor/src/lib.rs
|
./lightning-background-processor/src/lib.rs
|
||||||
./lightning-block-sync/src/lib.rs
|
./lightning-block-sync/src/lib.rs
|
||||||
./lightning-block-sync/src/rest.rs
|
|
||||||
./lightning-block-sync/src/rpc.rs
|
./lightning-block-sync/src/rpc.rs
|
||||||
./lightning-block-sync/src/test_utils.rs
|
./lightning-block-sync/src/test_utils.rs
|
||||||
./lightning-block-sync/src/utils.rs
|
./lightning-block-sync/src/utils.rs
|
||||||
|
|
Loading…
Add table
Reference in a new issue