Skip to content

Commit 72e9f53

Browse files
Merge pull request #1461 from TheBlueMatt/2022-05-unconf-0-not-half
Force-close channels on reorg only if the funding is unconfirmed
2 parents 6418c9e + 1568097 commit 72e9f53

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

lightning/src/ln/channel.rs

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

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

lightning/src/ln/reorg_tests.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,14 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
209209
let relevant_txids = nodes[0].node.get_relevant_txids();
210210
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
211211
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
212+
} else if connect_style == ConnectStyle::FullBlockViaListen {
213+
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
214+
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
215+
disconnect_blocks(&nodes[0], 1);
212216
} else {
213217
disconnect_all_blocks(&nodes[0]);
214218
}
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-
}
219+
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.");
220220
check_added_monitors!(nodes[1], 1);
221221
{
222222
let channel_state = nodes[0].node.channel_state.lock().unwrap();
@@ -277,14 +277,14 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
277277
let relevant_txids = nodes[0].node.get_relevant_txids();
278278
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
279279
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
280+
} else if connect_style == ConnectStyle::FullBlockViaListen {
281+
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
282+
assert_eq!(nodes[0].node.list_channels().len(), 1);
283+
disconnect_blocks(&nodes[0], 1);
280284
} else {
281285
disconnect_all_blocks(&nodes[0]);
282286
}
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-
}
287+
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.");
288288
check_added_monitors!(nodes[1], 1);
289289
{
290290
let channel_state = nodes[0].node.channel_state.lock().unwrap();
@@ -297,11 +297,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
297297
*nodes[0].chain_monitor.expect_channel_force_closed.lock().unwrap() = Some((chan.2, true));
298298
nodes[0].node.test_process_background_events(); // Required to free the pending background monitor update
299299
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-
};
300+
let expected_err = "Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.";
305301
check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: "Channel closed because of an exception: ".to_owned() + expected_err });
306302
check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: expected_err.to_owned() });
307303
assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);

0 commit comments

Comments
 (0)