Skip to content

Commit 08e56cf

Browse files
committed
Recalculate listeners' best block heights on each retry
Previously, we would calculate each listener's best block height outside of the `loop`, which will have us retrying `init::synchronize_listeners` with stale data, potentially leading to panicking as we might try to connect blocks from the original height to listeners that might have been advanced by the previous itertation of the `loop` before hitting an error. Here, we mitigate this by simply retrieving the current best block height for each listener at the start of each iteration.
1 parent 9607af3 commit 08e56cf

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

src/chain/mod.rs

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -281,48 +281,49 @@ impl ChainSource {
281281
}
282282
}
283283

284-
let channel_manager_best_block_hash =
285-
channel_manager.current_best_block().block_hash;
286-
let sweeper_best_block_hash = output_sweeper.current_best_block().block_hash;
287-
let onchain_wallet_best_block_hash = onchain_wallet.current_best_block().block_hash;
288-
289-
let mut chain_listeners = vec![
290-
(
291-
onchain_wallet_best_block_hash,
292-
&**onchain_wallet as &(dyn Listen + Send + Sync),
293-
),
294-
(
295-
channel_manager_best_block_hash,
296-
&*channel_manager as &(dyn Listen + Send + Sync),
297-
),
298-
(sweeper_best_block_hash, &*output_sweeper as &(dyn Listen + Send + Sync)),
299-
];
300-
301-
// TODO: Eventually we might want to see if we can synchronize `ChannelMonitor`s
302-
// before giving them to `ChainMonitor` it the first place. However, this isn't
303-
// trivial as we load them on initialization (in the `Builder`) and only gain
304-
// network access during `start`. For now, we just make sure we get the worst known
305-
// block hash and sychronize them via `ChainMonitor`.
306-
if let Some(worst_channel_monitor_block_hash) = chain_monitor
307-
.list_monitors()
308-
.iter()
309-
.flat_map(|(txo, _)| chain_monitor.get_monitor(*txo))
310-
.map(|m| m.current_best_block())
311-
.min_by_key(|b| b.height)
312-
.map(|b| b.block_hash)
313-
{
314-
chain_listeners.push((
315-
worst_channel_monitor_block_hash,
316-
&*chain_monitor as &(dyn Listen + Send + Sync),
317-
));
318-
}
319-
320284
log_info!(
321285
logger,
322286
"Starting initial synchronization of chain listeners. This might take a while..",
323287
);
324288

325289
loop {
290+
let channel_manager_best_block_hash =
291+
channel_manager.current_best_block().block_hash;
292+
let sweeper_best_block_hash = output_sweeper.current_best_block().block_hash;
293+
let onchain_wallet_best_block_hash =
294+
onchain_wallet.current_best_block().block_hash;
295+
296+
let mut chain_listeners = vec![
297+
(
298+
onchain_wallet_best_block_hash,
299+
&**onchain_wallet as &(dyn Listen + Send + Sync),
300+
),
301+
(
302+
channel_manager_best_block_hash,
303+
&*channel_manager as &(dyn Listen + Send + Sync),
304+
),
305+
(sweeper_best_block_hash, &*output_sweeper as &(dyn Listen + Send + Sync)),
306+
];
307+
308+
// TODO: Eventually we might want to see if we can synchronize `ChannelMonitor`s
309+
// before giving them to `ChainMonitor` it the first place. However, this isn't
310+
// trivial as we load them on initialization (in the `Builder`) and only gain
311+
// network access during `start`. For now, we just make sure we get the worst known
312+
// block hash and sychronize them via `ChainMonitor`.
313+
if let Some(worst_channel_monitor_block_hash) = chain_monitor
314+
.list_monitors()
315+
.iter()
316+
.flat_map(|(txo, _)| chain_monitor.get_monitor(*txo))
317+
.map(|m| m.current_best_block())
318+
.min_by_key(|b| b.height)
319+
.map(|b| b.block_hash)
320+
{
321+
chain_listeners.push((
322+
worst_channel_monitor_block_hash,
323+
&*chain_monitor as &(dyn Listen + Send + Sync),
324+
));
325+
}
326+
326327
let mut locked_header_cache = header_cache.lock().await;
327328
let now = SystemTime::now();
328329
match synchronize_listeners(

0 commit comments

Comments
 (0)