Skip to content

Commit 168899f

Browse files
committed
Don't send init closing_signed too early after final HTLC removal
If we remove an HTLC (or fee update), commit, and receive our counterparty's `revoke_and_ack`, we remove all knowledge of said HTLC (or fee update). However, the latest local commitment transaction that we can broadcast still contains the HTLC (or old fee), thus we are not eligible for initiating the `closing_signed` negotiation if we're shutting down and are generally expecting a counterparty `commitment_signed` immediately. Because we don't have any tracking of these updates in the `Channel` (only the `ChannelMonitor` is aware of the HTLC being in our latest local commitment transaction), we'd previously send a `closing_signed` too early, causing LDK<->LDK channels with an HTLC pending towards the channel initiator at the time of `shutdown` to always fail to cooperatively close. To fix this race, we add an additional unpersisted bool to `Channel` and use that to gate sending the initial `closing_signed`.
1 parent 36af1f0 commit 168899f

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed

lightning/src/ln/channel.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,19 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
774774
#[cfg(not(test))]
775775
closing_fee_limits: Option<(u64, u64)>,
776776

777+
/// If we remove an HTLC (or fee update), commit, and receive our counterparty's
778+
/// `revoke_and_ack`, we remove all knowledge of said HTLC (or fee update). However, the latest
779+
/// local commitment transaction that we can broadcast still contains the HTLC (or old fee)
780+
/// until we receive a further `commitment_signed`. Thus we are not eligible for initiating the
781+
/// `closing_signed` negotiation if we're expecting a counterparty `commitment_signed`.
782+
///
783+
/// To ensure we don't send a `closing_signed` too early, we track this state here, waiting
784+
/// until we see a `commitment_signed` before doing so.
785+
///
786+
/// We don't bother to persist this - we anticipate this state won't last longer than a few
787+
/// milliseconds, so any accidental force-closes here should be exceedingly rare.
788+
expecting_peer_commitment_signed: bool,
789+
777790
/// The hash of the block in which the funding transaction was included.
778791
funding_tx_confirmed_in: Option<BlockHash>,
779792
funding_tx_confirmation_height: u32,
@@ -3061,6 +3074,7 @@ impl<SP: Deref> Channel<SP> where
30613074
};
30623075

30633076
self.context.cur_holder_commitment_transaction_number -= 1;
3077+
self.context.expecting_peer_commitment_signed = false;
30643078
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
30653079
// build_commitment_no_status_check() next which will reset this to RAAFirst.
30663080
self.context.resend_order = RAACommitmentOrder::CommitmentFirst;
@@ -3326,6 +3340,7 @@ impl<SP: Deref> Channel<SP> where
33263340
// Take references explicitly so that we can hold multiple references to self.context.
33273341
let pending_inbound_htlcs: &mut Vec<_> = &mut self.context.pending_inbound_htlcs;
33283342
let pending_outbound_htlcs: &mut Vec<_> = &mut self.context.pending_outbound_htlcs;
3343+
let expecting_peer_commitment_signed = &mut self.context.expecting_peer_commitment_signed;
33293344

33303345
// We really shouldnt have two passes here, but retain gives a non-mutable ref (Rust bug)
33313346
pending_inbound_htlcs.retain(|htlc| {
@@ -3334,6 +3349,7 @@ impl<SP: Deref> Channel<SP> where
33343349
if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
33353350
value_to_self_msat_diff += htlc.amount_msat as i64;
33363351
}
3352+
*expecting_peer_commitment_signed = true;
33373353
false
33383354
} else { true }
33393355
});
@@ -3393,6 +3409,7 @@ impl<SP: Deref> Channel<SP> where
33933409
if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
33943410
log_trace!(logger, " ...promoting outbound LocalAnnounced {} to Committed", &htlc.payment_hash);
33953411
htlc.state = OutboundHTLCState::Committed;
3412+
*expecting_peer_commitment_signed = true;
33963413
}
33973414
if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut outcome) = &mut htlc.state {
33983415
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
@@ -3413,6 +3430,7 @@ impl<SP: Deref> Channel<SP> where
34133430
log_trace!(logger, " ...promoting outbound fee update {} to Committed", feerate);
34143431
self.context.feerate_per_kw = feerate;
34153432
self.context.pending_update_fee = None;
3433+
self.context.expecting_peer_commitment_signed = true;
34163434
},
34173435
FeeUpdateState::RemoteAnnounced => { debug_assert!(!self.context.is_outbound()); },
34183436
FeeUpdateState::AwaitingRemoteRevokeToAnnounce => {
@@ -4153,6 +4171,19 @@ impl<SP: Deref> Channel<SP> where
41534171
return Ok((None, None));
41544172
}
41554173

4174+
// If we're waiting on a monitor persistence, that implies we're also waiting to send some
4175+
// message to our counterparty (probably a `revoke_and_ack`). In such a case, we shouldn't
4176+
// initiate `closing_signed` negotiation until we're clear of all pending messages.
4177+
if (self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32)) != 0 {
4178+
return Ok((None, None));
4179+
}
4180+
4181+
// If we're waiting on a counterparty `commitment_signed` to clear some updates from our
4182+
// local commitment transaction, we can't yet initiate `closing_signed` negotiation.
4183+
if self.context.expecting_peer_commitment_signed {
4184+
return Ok((None, None));
4185+
}
4186+
41564187
let (our_min_fee, our_max_fee) = self.calculate_closing_fee_limits(fee_estimator);
41574188

41584189
assert!(self.context.shutdown_scriptpubkey.is_some());
@@ -5733,6 +5764,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
57335764

57345765
last_sent_closing_fee: None,
57355766
pending_counterparty_closing_signed: None,
5767+
expecting_peer_commitment_signed: false,
57365768
closing_fee_limits: None,
57375769
target_closing_feerate_sats_per_kw: None,
57385770

@@ -6380,6 +6412,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
63806412

63816413
last_sent_closing_fee: None,
63826414
pending_counterparty_closing_signed: None,
6415+
expecting_peer_commitment_signed: false,
63836416
closing_fee_limits: None,
63846417
target_closing_feerate_sats_per_kw: None,
63856418

@@ -7473,6 +7506,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
74737506

74747507
last_sent_closing_fee: None,
74757508
pending_counterparty_closing_signed: None,
7509+
expecting_peer_commitment_signed: false,
74767510
closing_fee_limits: None,
74777511
target_closing_feerate_sats_per_kw,
74787512

lightning/src/ln/shutdown_tests.rs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
//! Tests of our shutdown and closing_signed negotiation logic.
1111
1212
use crate::sign::{EntropySource, SignerProvider};
13+
use crate::chain::ChannelMonitorUpdateStatus;
1314
use crate::chain::transaction::OutPoint;
14-
use crate::events::{MessageSendEvent, MessageSendEventsProvider, ClosureReason};
15+
use crate::events::{MessageSendEvent, HTLCDestination, MessageSendEventsProvider, ClosureReason};
1516
use crate::ln::channelmanager::{self, PaymentSendFailure, PaymentId, RecipientOnionFields, ChannelShutdownState, ChannelDetails};
1617
use crate::routing::router::{PaymentParameters, get_route, RouteParameters};
1718
use crate::ln::msgs;
@@ -1214,3 +1215,102 @@ fn simple_target_feerate_shutdown() {
12141215
check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
12151216
check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
12161217
}
1218+
1219+
fn do_outbound_update_no_early_closing_signed(use_htlc: bool) {
1220+
// Previously, if we have a pending inbound HTLC (or fee update) on a channel which has
1221+
// initiated shutdown, we'd send our initial closing_signed immediately after receiving the
1222+
// peer's last RAA to remove the HTLC/fee update, but before receiving their final
1223+
// commitment_signed for a commitment without the HTLC/with the new fee. This caused at least
1224+
// LDK peers to force-close as we initiated closing_signed prior to the channel actually being
1225+
// fully empty of pending updates/HTLCs.
1226+
let chanmon_cfgs = create_chanmon_cfgs(2);
1227+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1228+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1229+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1230+
1231+
let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
1232+
1233+
send_payment(&nodes[0], &[&nodes[1]], 1_000_000);
1234+
let payment_hash_opt = if use_htlc {
1235+
Some(route_payment(&nodes[1], &[&nodes[0]], 10_000).1)
1236+
} else {
1237+
None
1238+
};
1239+
1240+
if use_htlc {
1241+
nodes[0].node.fail_htlc_backwards(&payment_hash_opt.unwrap());
1242+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[0],
1243+
[HTLCDestination::FailedPayment { payment_hash: payment_hash_opt.unwrap() }]);
1244+
} else {
1245+
*chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap() *= 10;
1246+
nodes[0].node.timer_tick_occurred();
1247+
}
1248+
let updates = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
1249+
check_added_monitors(&nodes[0], 1);
1250+
1251+
nodes[1].node.close_channel(&chan_id, &nodes[0].node.get_our_node_id()).unwrap();
1252+
let node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
1253+
nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
1254+
let node_1_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
1255+
1256+
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
1257+
nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_1_shutdown);
1258+
1259+
if use_htlc {
1260+
nodes[1].node.handle_update_fail_htlc(&nodes[0].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
1261+
} else {
1262+
nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &updates.update_fee.unwrap());
1263+
}
1264+
nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed);
1265+
check_added_monitors(&nodes[1], 1);
1266+
let (bs_raa, bs_cs) = get_revoke_commit_msgs(&nodes[1], &nodes[0].node.get_our_node_id());
1267+
1268+
nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
1269+
check_added_monitors(&nodes[0], 1);
1270+
1271+
// At this point the Channel on nodes[0] has no record of any HTLCs but the latest
1272+
// broadcastable commitment does contain the HTLC (but only the ChannelMonitor knows this).
1273+
// Thus, the channel should not yet initiate closing_signed negotiation (but previously did).
1274+
assert_eq!(nodes[0].node.get_and_clear_pending_msg_events(), Vec::new());
1275+
1276+
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
1277+
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs);
1278+
check_added_monitors(&nodes[0], 1);
1279+
assert_eq!(nodes[0].node.get_and_clear_pending_msg_events(), Vec::new());
1280+
1281+
expect_channel_shutdown_state!(nodes[0], chan_id, ChannelShutdownState::ResolvingHTLCs);
1282+
assert_eq!(nodes[0].node.get_and_clear_pending_msg_events(), Vec::new());
1283+
let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id).unwrap().clone();
1284+
nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
1285+
1286+
let as_raa_closing_signed = nodes[0].node.get_and_clear_pending_msg_events();
1287+
assert_eq!(as_raa_closing_signed.len(), 2);
1288+
1289+
if let MessageSendEvent::SendRevokeAndACK { msg, .. } = &as_raa_closing_signed[0] {
1290+
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &msg);
1291+
check_added_monitors(&nodes[1], 1);
1292+
if use_htlc {
1293+
expect_payment_failed!(nodes[1], payment_hash_opt.unwrap(), true);
1294+
}
1295+
} else { panic!("Unexpected message {:?}", as_raa_closing_signed[0]); }
1296+
1297+
if let MessageSendEvent::SendClosingSigned { msg, .. } = &as_raa_closing_signed[1] {
1298+
nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &msg);
1299+
} else { panic!("Unexpected message {:?}", as_raa_closing_signed[1]); }
1300+
1301+
let bs_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
1302+
nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &bs_closing_signed);
1303+
let (_, as_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
1304+
nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &as_2nd_closing_signed.unwrap());
1305+
let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
1306+
assert!(node_1_none.is_none());
1307+
1308+
check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1309+
check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
1310+
}
1311+
1312+
#[test]
1313+
fn outbound_update_no_early_closing_signed() {
1314+
do_outbound_update_no_early_closing_signed(true);
1315+
do_outbound_update_no_early_closing_signed(false);
1316+
}

0 commit comments

Comments
 (0)