Skip to content

Commit 61ff1cd

Browse files
committed
f Use InternalError internally
1 parent f046eaf commit 61ff1cd

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

lightning-transaction-sync/src/error.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,29 @@ use std::fmt;
55
pub enum TxSyncError {
66
/// A transaction sync failed and needs to be retried eventually.
77
Failed,
8+
}
9+
10+
impl std::error::Error for TxSyncError {}
11+
12+
impl fmt::Display for TxSyncError {
13+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14+
match *self {
15+
Self::Failed => write!(f, "Failed to conduct transaction sync."),
16+
}
17+
}
18+
}
19+
20+
#[derive(Debug)]
21+
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
22+
pub(crate) enum InternalError {
23+
/// A transaction sync failed and needs to be retried eventually.
24+
Failed,
825
/// An inconsisteny was encounterd during transaction sync.
926
Inconsistency,
1027
}
1128

12-
impl fmt::Display for TxSyncError {
29+
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
30+
impl fmt::Display for InternalError {
1331
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1432
match *self {
1533
Self::Failed => write!(f, "Failed to conduct transaction sync."),
@@ -20,11 +38,26 @@ impl fmt::Display for TxSyncError {
2038
}
2139
}
2240

23-
impl std::error::Error for TxSyncError {}
41+
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
42+
impl std::error::Error for InternalError {}
2443

2544
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
2645
impl From<esplora_client::Error> for TxSyncError {
2746
fn from(_e: esplora_client::Error) -> Self {
2847
Self::Failed
2948
}
3049
}
50+
51+
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
52+
impl From<esplora_client::Error> for InternalError {
53+
fn from(_e: esplora_client::Error) -> Self {
54+
Self::Failed
55+
}
56+
}
57+
58+
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
59+
impl From<InternalError> for TxSyncError {
60+
fn from(_e: InternalError) -> Self {
61+
Self::Failed
62+
}
63+
}

lightning-transaction-sync/src/esplora.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::TxSyncError;
1+
use crate::error::{TxSyncError, InternalError};
22

33
use lightning::util::logger::Logger;
44
use lightning::{log_error, log_given_level, log_info, log_internal, log_debug, log_trace};
@@ -88,13 +88,13 @@ where
8888
// (Semi-)permanent failure, retry later.
8989
log_error!(self.logger, "Failed during transaction sync, aborting.");
9090
self.pending_sync.store(true, Ordering::SeqCst);
91-
return Err(err);
91+
return Err(TxSyncError::from(err));
9292
}
9393
}
9494

9595
match maybe_await!(self.sync_best_block_updated(&confirmables, &tip_hash)) {
9696
Ok(()) => {}
97-
Err(TxSyncError::Inconsistency) => {
97+
Err(InternalError::Inconsistency) => {
9898
// Immediately restart syncing when we encounter any inconsistencies.
9999
log_debug!(self.logger, "Encountered inconsistency during transaction sync, restarting.");
100100
self.pending_sync.store(true, Ordering::SeqCst);
@@ -103,7 +103,7 @@ where
103103
Err(err) => {
104104
// (Semi-)permanent failure, retry later.
105105
self.pending_sync.store(true, Ordering::SeqCst);
106-
return Err(err);
106+
return Err(TxSyncError::from(err));
107107
}
108108
}
109109
}
@@ -124,7 +124,7 @@ where
124124
spent_outputs,
125125
);
126126
}
127-
Err(TxSyncError::Inconsistency) => {
127+
Err(InternalError::Inconsistency) => {
128128
// Immediately restart syncing when we encounter any inconsistencies.
129129
log_debug!(self.logger, "Encountered inconsistency during transaction sync, restarting.");
130130
self.pending_sync.store(true, Ordering::SeqCst);
@@ -134,7 +134,7 @@ where
134134
// (Semi-)permanent failure, retry later.
135135
log_error!(self.logger, "Failed during transaction sync, aborting.");
136136
self.pending_sync.store(true, Ordering::SeqCst);
137-
return Err(err);
137+
return Err(TxSyncError::from(err));
138138
}
139139
}
140140
*locked_last_sync_hash = Some(tip_hash);
@@ -209,7 +209,7 @@ where
209209
#[maybe_async]
210210
fn sync_best_block_updated(
211211
&self, confirmables: &Vec<&(dyn Confirm + Sync + Send)>, tip_hash: &BlockHash,
212-
) -> Result<(), TxSyncError> {
212+
) -> Result<(), InternalError> {
213213

214214
// Inform the interface of the new block.
215215
let tip_header = maybe_await!(self.client.get_header_by_hash(tip_hash))?;
@@ -221,7 +221,7 @@ where
221221
}
222222
}
223223
} else {
224-
return Err(TxSyncError::Inconsistency);
224+
return Err(InternalError::Inconsistency);
225225
}
226226
Ok(())
227227
}
@@ -250,7 +250,7 @@ where
250250
#[maybe_async]
251251
fn get_confirmed_transactions(
252252
&self,
253-
) -> Result<(Vec<ConfirmedTx>, HashSet<WatchedOutput>), TxSyncError> {
253+
) -> Result<(Vec<ConfirmedTx>, HashSet<WatchedOutput>), InternalError> {
254254

255255
// First, check the confirmation status of registered transactions as well as the
256256
// status of dependent transactions of registered outputs.
@@ -307,22 +307,22 @@ where
307307
#[maybe_async]
308308
fn get_confirmed_tx(
309309
&self, txid: &Txid, expected_block_hash: Option<BlockHash>, known_block_height: Option<u32>,
310-
) -> Result<Option<ConfirmedTx>, TxSyncError> {
310+
) -> Result<Option<ConfirmedTx>, InternalError> {
311311
if let Some(merkle_block) = maybe_await!(self.client.get_merkle_block(&txid))? {
312312
let block_header = merkle_block.header;
313313
let block_hash = block_header.block_hash();
314314
if let Some(expected_block_hash) = expected_block_hash {
315315
if expected_block_hash != block_hash {
316316
log_trace!(self.logger, "Inconsistency: Tx {} expected in block {}, but is confirmed in {}", txid, expected_block_hash, block_hash);
317-
return Err(TxSyncError::Inconsistency);
317+
return Err(InternalError::Inconsistency);
318318
}
319319
}
320320

321321
let mut matches = vec![*txid];
322322
let mut indexes = Vec::new();
323323
let _ = merkle_block.txn.extract_matches(&mut matches, &mut indexes);
324324
debug_assert_eq!(indexes.len(), 1);
325-
let pos = *indexes.get(0).ok_or(TxSyncError::Failed)? as usize;
325+
let pos = *indexes.get(0).ok_or(InternalError::Failed)? as usize;
326326
if let Some(tx) = maybe_await!(self.client.get_tx(&txid))? {
327327
if let Some(block_height) = known_block_height {
328328
// We can take a shortcut here if a previous call already gave us the height.
@@ -336,7 +336,7 @@ where
336336
// If any previously-confirmed block suddenly is no longer confirmed, we found
337337
// an inconsistency and should start over.
338338
log_trace!(self.logger, "Inconsistency: Tx {} was unconfirmed during syncing.", txid);
339-
return Err(TxSyncError::Inconsistency);
339+
return Err(InternalError::Inconsistency);
340340
}
341341
}
342342
}
@@ -346,7 +346,7 @@ where
346346
#[maybe_async]
347347
fn sync_unconfirmed_transactions(
348348
&self, confirmables: &Vec<&(dyn Confirm + Sync + Send)>,
349-
) -> Result<(), TxSyncError> {
349+
) -> Result<(), InternalError> {
350350
// Query the interface for relevant txids and check whether the relevant blocks are still
351351
// in the best chain, mark them unconfirmed otherwise. If the transactions have been
352352
// reconfirmed in another block, we'll confirm them in the next sync iteration.

lightning-transaction-sync/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ extern crate bdk_macros;
1717

1818
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
1919
mod esplora;
20-
mod error;
2120

21+
#[cfg(test)]
22+
mod tests;
23+
24+
mod error;
2225
pub use error::TxSyncError;
2326

2427
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]

0 commit comments

Comments
 (0)