Skip to content

Commit 48c46bf

Browse files
committed
Improve EsploraSyncClient logging
We give some more information while reducing the log levels to make the logging less spammy. We also convert one safe-to-unwrap case from returning an error to unwrapping the value.
1 parent 5b7c1d5 commit 48c46bf

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

lightning-transaction-sync/src/esplora.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::error::{TxSyncError, InternalError};
22
use crate::common::{SyncState, FilterQueue, ConfirmedTx};
33

44
use lightning::util::logger::Logger;
5-
use lightning::{log_error, log_info, log_debug, log_trace};
5+
use lightning::{log_error, log_debug, log_trace};
66
use lightning::chain::WatchedOutput;
77
use lightning::chain::{Confirm, Filter};
88

@@ -14,6 +14,7 @@ use esplora_client::r#async::AsyncClient;
1414
#[cfg(not(feature = "async-interface"))]
1515
use esplora_client::blocking::BlockingClient;
1616

17+
use std::time::Instant;
1718
use std::collections::HashSet;
1819
use core::ops::Deref;
1920

@@ -89,7 +90,10 @@ where
8990
#[cfg(feature = "async-interface")]
9091
let mut sync_state = self.sync_state.lock().await;
9192

92-
log_info!(self.logger, "Starting transaction sync.");
93+
log_trace!(self.logger, "Starting transaction sync.");
94+
let start_time = Instant::now();
95+
let mut num_confirmed = 0;
96+
let mut num_unconfirmed = 0;
9397

9498
let mut tip_hash = maybe_await!(self.client.get_tip_hash())?;
9599

@@ -113,14 +117,21 @@ where
113117
let check_tip_hash = maybe_await!(self.client.get_tip_hash())?;
114118
if check_tip_hash != tip_hash {
115119
tip_hash = check_tip_hash;
120+
121+
log_debug!(self.logger, "Encountered inconsistency during transaction sync, restarting.");
122+
sync_state.pending_sync = true;
116123
continue;
117124
}
118-
125+
num_unconfirmed += unconfirmed_txs.len();
119126
self.sync_unconfirmed_transactions(&mut sync_state, &confirmables, unconfirmed_txs);
120127
},
121128
Err(err) => {
122129
// (Semi-)permanent failure, retry later.
123-
log_error!(self.logger, "Failed during transaction sync, aborting.");
130+
log_error!(self.logger,
131+
"Failed during transaction sync, aborting. Synced so far: {} confirmed, {} unconfirmed.",
132+
num_confirmed,
133+
num_unconfirmed
134+
);
124135
sync_state.pending_sync = true;
125136
return Err(TxSyncError::from(err));
126137
}
@@ -136,6 +147,11 @@ where
136147
}
137148
Err(err) => {
138149
// (Semi-)permanent failure, retry later.
150+
log_error!(self.logger,
151+
"Failed during transaction sync, aborting. Synced so far: {} confirmed, {} unconfirmed.",
152+
num_confirmed,
153+
num_unconfirmed
154+
);
139155
sync_state.pending_sync = true;
140156
return Err(TxSyncError::from(err));
141157
}
@@ -152,6 +168,7 @@ where
152168
continue;
153169
}
154170

171+
num_confirmed += confirmed_txs.len();
155172
self.sync_confirmed_transactions(
156173
&mut sync_state,
157174
&confirmables,
@@ -166,7 +183,11 @@ where
166183
}
167184
Err(err) => {
168185
// (Semi-)permanent failure, retry later.
169-
log_error!(self.logger, "Failed during transaction sync, aborting.");
186+
log_error!(self.logger,
187+
"Failed during transaction sync, aborting. Synced so far: {} confirmed, {} unconfirmed.",
188+
num_confirmed,
189+
num_unconfirmed
190+
);
170191
sync_state.pending_sync = true;
171192
return Err(TxSyncError::from(err));
172193
}
@@ -175,7 +196,8 @@ where
175196
sync_state.pending_sync = false;
176197
}
177198
}
178-
log_info!(self.logger, "Finished transaction sync.");
199+
log_debug!(self.logger, "Finished transaction sync at tip {} in {}ms: {} confirmed, {} unconfirmed.",
200+
tip_hash, start_time.elapsed().as_millis(), num_confirmed, num_unconfirmed);
179201
Ok(())
180202
}
181203

@@ -286,7 +308,8 @@ where
286308
return Err(InternalError::Failed);
287309
}
288310

289-
let pos = *indexes.get(0).ok_or(InternalError::Failed)? as usize;
311+
// unwrap() safety: len() > 0 is checked above
312+
let pos = *indexes.first().unwrap() as usize;
290313
if let Some(tx) = maybe_await!(self.client.get_tx(&txid))? {
291314
if let Some(block_height) = known_block_height {
292315
// We can take a shortcut here if a previous call already gave us the height.

0 commit comments

Comments
 (0)