Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lightning-block-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
rest-client = [ "serde_json", "chunked_transfer" ]
rpc-client = [ "serde_json", "chunked_transfer" ]
rest-client = [ "serde_json", "dep:bitreq" ]
rpc-client = [ "serde_json", "dep:bitreq" ]
tokio = [ "dep:tokio", "bitreq?/async" ]

[dependencies]
bitcoin = "0.32.2"
lightning = { version = "0.3.0", path = "../lightning" }
tokio = { version = "1.35", features = [ "io-util", "net", "time", "rt" ], optional = true }
serde_json = { version = "1.0", optional = true }
chunked_transfer = { version = "1.4", optional = true }
bitreq = { version = "0.3", default-features = false, features = ["std"], optional = true }

[dev-dependencies]
lightning = { version = "0.3.0", path = "../lightning", features = ["_test_utils"] }
Expand Down
46 changes: 45 additions & 1 deletion lightning-block-sync/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::http::{BinaryResponse, JsonResponse};
use crate::http::{BinaryResponse, HttpClientError, JsonResponse};
#[cfg(feature = "rpc-client")]
use crate::rpc::RpcClientError;
use crate::utils::hex_to_work;
use crate::{BlockHeaderData, BlockSourceError};

Expand Down Expand Up @@ -35,6 +37,48 @@ impl From<io::Error> for BlockSourceError {
}
}

/// Conversion from `HttpClientError` into `BlockSourceError`.
impl From<HttpClientError> for BlockSourceError {
fn from(e: HttpClientError) -> BlockSourceError {
match &e {
// Transport errors (connection, timeout, etc.) are transient
HttpClientError::Transport(_) => BlockSourceError::transient(e),
// HTTP non-2xx errors are transient - e.g. "not found" must not stop polling
HttpClientError::Http(_) => BlockSourceError::transient(e),
// I/O errors follow the same logic as std::io::Error
HttpClientError::Io(io_error) => match io_error.kind() {
io::ErrorKind::InvalidData => BlockSourceError::persistent(e),
io::ErrorKind::InvalidInput => BlockSourceError::persistent(e),
_ => BlockSourceError::transient(e),
},
}
}
}

/// Conversion from `RpcClientError` into `BlockSourceError`.
#[cfg(feature = "rpc-client")]
impl From<RpcClientError> for BlockSourceError {
fn from(e: RpcClientError) -> BlockSourceError {
match &e {
RpcClientError::Http(http_error) => match http_error {
HttpClientError::Transport(_) => BlockSourceError::transient(e),
// HTTP non-2xx errors are transient
HttpClientError::Http(_) => BlockSourceError::transient(e),
HttpClientError::Io(io_error) => match io_error.kind() {
io::ErrorKind::InvalidData => BlockSourceError::persistent(e),
io::ErrorKind::InvalidInput => BlockSourceError::persistent(e),
_ => BlockSourceError::transient(e),
},
},
// RPC errors are transient
// e.g. "block not found" should not stop polling
RpcClientError::Rpc(_) => BlockSourceError::transient(e),
// Malformed response data is persistent
RpcClientError::InvalidData(_) => BlockSourceError::persistent(e),
}
}
}

/// Parses binary data as a block.
impl TryInto<Block> for BinaryResponse {
type Error = io::Error;
Expand Down
Loading
Loading