Skip to content

Commit 4832364

Browse files
committed
Consider quiescence state when freeing holding cells upon revoke_and_ack
We previously would avoid freeing our holding cells upon a `revoke_and_ack` if a monitor update was in progress, which we checked explicitly. With quiescence, if we've already sent `stfu`, we're not allowed to make further commitment updates, so we must also avoid freeing our holding cells in such cases.
1 parent f6d6e34 commit 4832364

File tree

2 files changed

+146
-11
lines changed

2 files changed

+146
-11
lines changed

lightning/src/ln/channel.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6081,14 +6081,8 @@ impl<SP: Deref> FundedChannel<SP> where
60816081
self.context.monitor_pending_update_adds.append(&mut pending_update_adds);
60826082

60836083
if self.context.channel_state.is_monitor_update_in_progress() {
6084-
// We can't actually generate a new commitment transaction (incl by freeing holding
6085-
// cells) while we can't update the monitor, so we just return what we have.
60866084
if require_commitment {
60876085
self.context.monitor_pending_commitment_signed = true;
6088-
// When the monitor updating is restored we'll call
6089-
// get_last_commitment_update_for_send(), which does not update state, but we're
6090-
// definitely now awaiting a remote revoke before we can step forward any more, so
6091-
// set it here.
60926086
let mut additional_update = self.build_commitment_no_status_check(logger);
60936087
// build_commitment_no_status_check may bump latest_monitor_id but we want them to be
60946088
// strictly increasing by one, so decrement it here.
@@ -6102,7 +6096,7 @@ impl<SP: Deref> FundedChannel<SP> where
61026096
return_with_htlcs_to_fail!(Vec::new());
61036097
}
61046098

6105-
match self.free_holding_cell_htlcs(fee_estimator, logger) {
6099+
match self.maybe_free_holding_cell_htlcs(fee_estimator, logger) {
61066100
(Some(mut additional_update), htlcs_to_fail) => {
61076101
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
61086102
// strictly increasing by one, so decrement it here.
@@ -6117,17 +6111,32 @@ impl<SP: Deref> FundedChannel<SP> where
61176111
},
61186112
(None, htlcs_to_fail) => {
61196113
if require_commitment {
6114+
// We can't generate a new commitment transaction yet so we just return what we have.
61206115
let mut additional_update = self.build_commitment_no_status_check(logger);
61216116

61226117
// build_commitment_no_status_check may bump latest_monitor_id but we want them to be
61236118
// strictly increasing by one, so decrement it here.
61246119
self.context.latest_monitor_update_id = monitor_update.update_id;
61256120
monitor_update.updates.append(&mut additional_update.updates);
61266121

6127-
log_debug!(logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed. {} monitor update.",
6128-
&self.context.channel_id(),
6129-
update_fail_htlcs.len() + update_fail_malformed_htlcs.len(),
6130-
release_state_str);
6122+
log_debug!(logger, "Received a valid revoke_and_ack for channel {}. {} monitor update.",
6123+
&self.context.channel_id(), release_state_str);
6124+
if self.context.channel_state.can_generate_new_commitment() {
6125+
log_debug!(logger, "Responding with a commitment update with {} HTLCs failed for channel {}",
6126+
update_fail_htlcs.len() + update_fail_malformed_htlcs.len(),
6127+
&self.context.channel_id);
6128+
} else {
6129+
debug_assert!(htlcs_to_fail.is_empty());
6130+
let reason = if self.context.channel_state.is_local_stfu_sent() {
6131+
"exits quiescence"
6132+
} else if self.context.channel_state.is_monitor_update_in_progress() {
6133+
"completes pending monitor update"
6134+
} else {
6135+
"can continue progress"
6136+
};
6137+
log_debug!(logger, "Holding back commitment update until channel {} {}",
6138+
&self.context.channel_id, reason);
6139+
}
61316140

61326141
self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
61336142
return_with_htlcs_to_fail!(htlcs_to_fail);

lightning/src/ln/quiescence_tests.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::chain::ChannelMonitorUpdateStatus;
2+
use crate::events::Event;
23
use crate::events::HTLCDestination;
34
use crate::events::MessageSendEvent;
45
use crate::events::MessageSendEventsProvider;
@@ -247,3 +248,128 @@ fn test_quiescence_waits_for_monitor_update_complete() {
247248
expect_payment_sent(&nodes[0], preimage, None, true, true);
248249
expect_payment_claimed!(&nodes[1], payment_hash, payment_amount);
249250
}
251+
252+
#[test]
253+
fn test_quiescence_updates_go_to_holding_cell() {
254+
quiescence_updates_go_to_holding_cell(false);
255+
quiescence_updates_go_to_holding_cell(true);
256+
}
257+
258+
fn quiescence_updates_go_to_holding_cell(fail_htlc: bool) {
259+
// Test that any updates made to a channel while quiescent go to the holding cell.
260+
let chanmon_cfgs = create_chanmon_cfgs(2);
261+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
262+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
263+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
264+
let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
265+
266+
let node_id_0 = nodes[0].node.get_our_node_id();
267+
let node_id_1 = nodes[1].node.get_our_node_id();
268+
269+
// Send enough to be able to pay from both directions.
270+
let payment_amount = 1_000_000;
271+
send_payment(&nodes[0], &[&nodes[1]], payment_amount * 4);
272+
273+
// Propose quiescence from nodes[1], and immediately try to send a payment. Since its `stfu` has
274+
// already gone out first, the outbound HTLC will go into the holding cell.
275+
nodes[1].node.maybe_propose_quiescence(&node_id_0, &chan_id).unwrap();
276+
let stfu = get_event_msg!(&nodes[1], MessageSendEvent::SendStfu, node_id_0);
277+
278+
let (route1, payment_hash1, payment_preimage1, payment_secret1) =
279+
get_route_and_payment_hash!(&nodes[1], &nodes[0], payment_amount);
280+
let onion1 = RecipientOnionFields::secret_only(payment_secret1);
281+
let payment_id1 = PaymentId(payment_hash1.0);
282+
nodes[1].node.send_payment_with_route(route1, payment_hash1, onion1, payment_id1).unwrap();
283+
check_added_monitors!(&nodes[1], 0);
284+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
285+
286+
// Send a payment in the opposite direction. Since nodes[0] hasn't sent its own `stfu` yet, it's
287+
// allowed to make updates.
288+
let (route2, payment_hash2, payment_preimage2, payment_secret2) =
289+
get_route_and_payment_hash!(&nodes[0], &nodes[1], payment_amount);
290+
let onion2 = RecipientOnionFields::secret_only(payment_secret2);
291+
let payment_id2 = PaymentId(payment_hash2.0);
292+
nodes[0].node.send_payment_with_route(route2, payment_hash2, onion2, payment_id2).unwrap();
293+
check_added_monitors!(&nodes[0], 1);
294+
295+
let update_add = get_htlc_update_msgs!(&nodes[0], node_id_1);
296+
nodes[1].node.handle_update_add_htlc(node_id_0, &update_add.update_add_htlcs[0]);
297+
commitment_signed_dance!(&nodes[1], &nodes[0], update_add.commitment_signed, false);
298+
expect_pending_htlcs_forwardable!(&nodes[1]);
299+
expect_payment_claimable!(nodes[1], payment_hash2, payment_secret2, payment_amount);
300+
301+
// Have nodes[1] attempt to fail/claim nodes[0]'s payment. Since nodes[1] already sent out
302+
// `stfu`, the `update_fail/fulfill` will go into the holding cell.
303+
if fail_htlc {
304+
nodes[1].node.fail_htlc_backwards(&payment_hash2);
305+
let failed_payment = HTLCDestination::FailedPayment { payment_hash: payment_hash2 };
306+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(&nodes[1], vec![failed_payment]);
307+
} else {
308+
nodes[1].node.claim_funds(payment_preimage2);
309+
check_added_monitors(&nodes[1], 1);
310+
}
311+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
312+
313+
// Finish the quiescence handshake.
314+
nodes[0].node.handle_stfu(node_id_1, &stfu);
315+
let stfu = get_event_msg!(&nodes[0], MessageSendEvent::SendStfu, node_id_1);
316+
nodes[1].node.handle_stfu(node_id_0, &stfu);
317+
318+
nodes[0].node.exit_quiescence(&node_id_1, &chan_id).unwrap();
319+
nodes[1].node.exit_quiescence(&node_id_0, &chan_id).unwrap();
320+
321+
// Now that quiescence is over, nodes are allowed to make updates again. nodes[1] will have its
322+
// outbound HTLC finally go out, along with the fail/claim of nodes[0]'s payment.
323+
let update = get_htlc_update_msgs!(&nodes[1], node_id_0);
324+
check_added_monitors(&nodes[1], 1);
325+
nodes[0].node.handle_update_add_htlc(node_id_1, &update.update_add_htlcs[0]);
326+
if fail_htlc {
327+
nodes[0].node.handle_update_fail_htlc(node_id_1, &update.update_fail_htlcs[0]);
328+
} else {
329+
nodes[0].node.handle_update_fulfill_htlc(node_id_1, &update.update_fulfill_htlcs[0]);
330+
}
331+
commitment_signed_dance!(&nodes[0], &nodes[1], update.commitment_signed, false);
332+
333+
if !fail_htlc {
334+
expect_payment_claimed!(nodes[1], payment_hash2, payment_amount);
335+
}
336+
337+
let events = nodes[0].node.get_and_clear_pending_events();
338+
assert_eq!(events.len(), 3);
339+
assert!(events.iter().find(|e| matches!(e, Event::PendingHTLCsForwardable { .. })).is_some());
340+
if fail_htlc {
341+
assert!(events.iter().find(|e| matches!(e, Event::PaymentFailed { .. })).is_some());
342+
assert!(events.iter().find(|e| matches!(e, Event::PaymentPathFailed { .. })).is_some());
343+
} else {
344+
assert!(events.iter().find(|e| matches!(e, Event::PaymentSent { .. })).is_some());
345+
assert!(events.iter().find(|e| matches!(e, Event::PaymentPathSuccessful { .. })).is_some());
346+
check_added_monitors(&nodes[0], 1);
347+
}
348+
nodes[0].node.process_pending_htlc_forwards();
349+
expect_payment_claimable!(nodes[0], payment_hash1, payment_secret1, payment_amount);
350+
351+
if fail_htlc {
352+
nodes[0].node.fail_htlc_backwards(&payment_hash1);
353+
let failed_payment = HTLCDestination::FailedPayment { payment_hash: payment_hash1 };
354+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(&nodes[0], vec![failed_payment]);
355+
} else {
356+
nodes[0].node.claim_funds(payment_preimage1);
357+
}
358+
check_added_monitors(&nodes[0], 1);
359+
360+
let update = get_htlc_update_msgs!(&nodes[0], node_id_1);
361+
if fail_htlc {
362+
nodes[1].node.handle_update_fail_htlc(node_id_0, &update.update_fail_htlcs[0]);
363+
} else {
364+
nodes[1].node.handle_update_fulfill_htlc(node_id_0, &update.update_fulfill_htlcs[0]);
365+
}
366+
commitment_signed_dance!(&nodes[1], &nodes[0], update.commitment_signed, false);
367+
368+
if fail_htlc {
369+
let conditions = PaymentFailedConditions::new();
370+
expect_payment_failed_conditions(&nodes[1], payment_hash1, true, conditions);
371+
} else {
372+
expect_payment_claimed!(nodes[0], payment_hash1, payment_amount);
373+
expect_payment_sent(&nodes[1], payment_preimage1, None, true, true);
374+
}
375+
}

0 commit comments

Comments
 (0)