Skip to content

Commit 7016c2f

Browse files
committed
Force-close channels on reorg only if the funding is unconfirmed
Currently, if a channel's funding is locked in and then later reorg'd back to half of the channel's minimum-depth we will immediately force-close the channel. However, this can happen at the fork-point while processing a reorg, and generally reorgs do not reduce the block height at all, making this a rather useless endeavor. Ideally we'd never auto-force-close channels at all due to a reorg, instead simply marking it as inactive until the funding transaction is re-confirmed (or allowing the user to attempt to force-close or force-closing once we're confident we have completed reorg processing if we're at risk of losing funds already received in the channel). Sadly, we currently do not support changing a channel's SCID and updating our SCID maps, so we cannot yet remove the automated force-close logic. Still, there is no reason to do it until a funding transaction has been removed from the chain. This implements that change - only force-closeing once a channel's funding transaction has been reorg'd out (still potentially at a reorg's fork point). This continues to imply a 1-confirmation channel will always be force-closed after a reorg of the funding transaction, and will imply a similar behavior with 0-conf channels.
1 parent 9bdce47 commit 7016c2f

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

lightning/src/ln/channel.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4720,10 +4720,14 @@ impl<Signer: Sign> Channel<Signer> {
47204720
}
47214721

47224722
// If we've sent funding_locked (or have both sent and received funding_locked), and
4723-
// the funding transaction's confirmation count has dipped below minimum_depth / 2,
4723+
// the funding transaction has become unconfirmed,
47244724
// close the channel and hope we can get the latest state on chain (because presumably
47254725
// the funding transaction is at least still in the mempool of most nodes).
4726-
if funding_tx_confirmations < self.minimum_depth.unwrap() as i64 / 2 {
4726+
//
4727+
// Note that ideally we wouldn't force-close if we see *any* reorg on a 1-conf channel,
4728+
// but not doing so may lead to the `ChannelManager::short_to_id` map being
4729+
// inconsistent, so we currently have to.
4730+
if funding_tx_confirmations == 0 && self.funding_tx_confirmed_in.is_some() {
47274731
let err_reason = format!("Funding transaction was un-confirmed. Locked at {} confs, now have {} confs.",
47284732
self.minimum_depth.unwrap(), funding_tx_confirmations);
47294733
return Err(ClosureReason::ProcessingError { err: err_reason });

lightning/src/ln/reorg_tests.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
212212
} else {
213213
disconnect_all_blocks(&nodes[0]);
214214
}
215-
if connect_style == ConnectStyle::FullBlockViaListen && !use_funding_unconfirmed {
216-
handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 2 confs.");
217-
} else {
218-
handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
219-
}
215+
handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
220216
check_added_monitors!(nodes[1], 1);
221217
{
222218
let channel_state = nodes[0].node.channel_state.lock().unwrap();
@@ -280,11 +276,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
280276
} else {
281277
disconnect_all_blocks(&nodes[0]);
282278
}
283-
if connect_style == ConnectStyle::FullBlockViaListen && !use_funding_unconfirmed {
284-
handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 2 confs.");
285-
} else {
286-
handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
287-
}
279+
handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
288280
check_added_monitors!(nodes[1], 1);
289281
{
290282
let channel_state = nodes[0].node.channel_state.lock().unwrap();
@@ -297,11 +289,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
297289
*nodes[0].chain_monitor.expect_channel_force_closed.lock().unwrap() = Some((chan.2, true));
298290
nodes[0].node.test_process_background_events(); // Required to free the pending background monitor update
299291
check_added_monitors!(nodes[0], 1);
300-
let expected_err = if connect_style == ConnectStyle::FullBlockViaListen && !use_funding_unconfirmed {
301-
"Funding transaction was un-confirmed. Locked at 6 confs, now have 2 confs."
302-
} else {
303-
"Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs."
304-
};
292+
let expected_err = "Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.";
305293
check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: "Channel closed because of an exception: ".to_owned() + expected_err });
306294
check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: expected_err.to_owned() });
307295
assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);

0 commit comments

Comments
 (0)