Skip to content

Commit 20877b3

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. Along the way, we also remove the special handling of in-progress monitor updates now that it behaves the same as the handling of being quiescent.
1 parent 99670ec commit 20877b3

File tree

2 files changed

+153
-27
lines changed

2 files changed

+153
-27
lines changed

lightning/src/ln/channel.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6081,29 +6081,7 @@ impl<SP: Deref> FundedChannel<SP> where
60816081

60826082
self.context.monitor_pending_update_adds.append(&mut pending_update_adds);
60836083

6084-
if self.context.channel_state.is_monitor_update_in_progress() {
6085-
// We can't actually generate a new commitment transaction (incl by freeing holding
6086-
// cells) while we can't update the monitor, so we just return what we have.
6087-
if require_commitment {
6088-
self.context.monitor_pending_commitment_signed = true;
6089-
// When the monitor updating is restored we'll call
6090-
// get_last_commitment_update_for_send(), which does not update state, but we're
6091-
// definitely now awaiting a remote revoke before we can step forward any more, so
6092-
// set it here.
6093-
let mut additional_update = self.build_commitment_no_status_check(logger);
6094-
// build_commitment_no_status_check may bump latest_monitor_id but we want them to be
6095-
// strictly increasing by one, so decrement it here.
6096-
self.context.latest_monitor_update_id = monitor_update.update_id;
6097-
monitor_update.updates.append(&mut additional_update.updates);
6098-
}
6099-
self.context.monitor_pending_forwards.append(&mut to_forward_infos);
6100-
self.context.monitor_pending_failures.append(&mut revoked_htlcs);
6101-
self.context.monitor_pending_finalized_fulfills.append(&mut finalized_claimed_htlcs);
6102-
log_debug!(logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply.", &self.context.channel_id());
6103-
return_with_htlcs_to_fail!(Vec::new());
6104-
}
6105-
6106-
match self.free_holding_cell_htlcs(fee_estimator, logger) {
6084+
match self.maybe_free_holding_cell_htlcs(fee_estimator, logger) {
61076085
(Some(mut additional_update), htlcs_to_fail) => {
61086086
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
61096087
// strictly increasing by one, so decrement it here.
@@ -6118,17 +6096,36 @@ impl<SP: Deref> FundedChannel<SP> where
61186096
},
61196097
(None, htlcs_to_fail) => {
61206098
if require_commitment {
6099+
// We can't generate a new commitment transaction yet so we just return what we
6100+
// have. When the monitor updating is restored we'll call
6101+
// get_last_commitment_update_for_send(), which does not update state, but we're
6102+
// definitely now awaiting a remote revoke before we can step forward any more,
6103+
// so set it here.
61216104
let mut additional_update = self.build_commitment_no_status_check(logger);
61226105

61236106
// build_commitment_no_status_check may bump latest_monitor_id but we want them to be
61246107
// strictly increasing by one, so decrement it here.
61256108
self.context.latest_monitor_update_id = monitor_update.update_id;
61266109
monitor_update.updates.append(&mut additional_update.updates);
61276110

6128-
log_debug!(logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed. {} monitor update.",
6129-
&self.context.channel_id(),
6130-
update_fail_htlcs.len() + update_fail_malformed_htlcs.len(),
6131-
release_state_str);
6111+
log_debug!(logger, "Received a valid revoke_and_ack for channel {}. {} monitor update.",
6112+
&self.context.channel_id(), release_state_str);
6113+
if self.context.channel_state.can_generate_new_commitment() {
6114+
log_debug!(logger, "Responding with a commitment update with {} HTLCs failed for channel {}",
6115+
update_fail_htlcs.len() + update_fail_malformed_htlcs.len(),
6116+
&self.context.channel_id);
6117+
} else {
6118+
debug_assert!(htlcs_to_fail.is_empty());
6119+
let reason = if self.context.channel_state.is_local_stfu_sent() {
6120+
"exits quiescence"
6121+
} else if self.context.channel_state.is_monitor_update_in_progress() {
6122+
"completes pending monitor update"
6123+
} else {
6124+
"can continue progress"
6125+
};
6126+
log_debug!(logger, "Holding back commitment update until channel {} {}",
6127+
&self.context.channel_id, reason);
6128+
}
61326129

61336130
self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
61346131
return_with_htlcs_to_fail!(htlcs_to_fail);

lightning/src/ln/quiescence_tests.rs

Lines changed: 129 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;
@@ -284,3 +285,131 @@ fn test_quiescence_tracks_monitor_update_in_progress_and_waits_for_async_signer(
284285
let _ = get_htlc_update_msgs!(&nodes[0], node_id_1);
285286
check_added_monitors(&nodes[0], 1);
286287
}
288+
289+
#[test]
290+
fn test_quiescence_updates_go_to_holding_cell() {
291+
quiescence_updates_go_to_holding_cell(false);
292+
quiescence_updates_go_to_holding_cell(true);
293+
}
294+
295+
fn quiescence_updates_go_to_holding_cell(fail_htlc: bool) {
296+
// Test that any updates made to a channel while quiescent go to the holding cell.
297+
let chanmon_cfgs = create_chanmon_cfgs(2);
298+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
299+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
300+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
301+
let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
302+
303+
let node_id_0 = nodes[0].node.get_our_node_id();
304+
let node_id_1 = nodes[1].node.get_our_node_id();
305+
306+
// Send enough to be able to pay from both directions.
307+
let payment_amount = 1_000_000;
308+
send_payment(&nodes[0], &[&nodes[1]], payment_amount * 4);
309+
310+
// Propose quiescence from nodes[1], and immediately try to send a payment. Since its `stfu` has
311+
// already gone out first, the outbound HTLC will go into the holding cell.
312+
nodes[1].node.maybe_propose_quiescence(&node_id_0, &chan_id).unwrap();
313+
let stfu = get_event_msg!(&nodes[1], MessageSendEvent::SendStfu, node_id_0);
314+
315+
let (route1, payment_hash1, payment_preimage1, payment_secret1) =
316+
get_route_and_payment_hash!(&nodes[1], &nodes[0], payment_amount);
317+
let onion1 = RecipientOnionFields::secret_only(payment_secret1);
318+
let payment_id1 = PaymentId(payment_hash1.0);
319+
nodes[1].node.send_payment_with_route(route1, payment_hash1, onion1, payment_id1).unwrap();
320+
check_added_monitors!(&nodes[1], 0);
321+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
322+
323+
// Send a payment in the opposite direction. Since nodes[0] hasn't sent its own `stfu` yet, it's
324+
// allowed to make updates.
325+
let (route2, payment_hash2, payment_preimage2, payment_secret2) =
326+
get_route_and_payment_hash!(&nodes[0], &nodes[1], payment_amount);
327+
let onion2 = RecipientOnionFields::secret_only(payment_secret2);
328+
let payment_id2 = PaymentId(payment_hash2.0);
329+
nodes[0].node.send_payment_with_route(route2, payment_hash2, onion2, payment_id2).unwrap();
330+
check_added_monitors!(&nodes[0], 1);
331+
332+
let update_add = get_htlc_update_msgs!(&nodes[0], node_id_1);
333+
nodes[1].node.handle_update_add_htlc(node_id_0, &update_add.update_add_htlcs[0]);
334+
commitment_signed_dance!(&nodes[1], &nodes[0], update_add.commitment_signed, false);
335+
expect_pending_htlcs_forwardable!(&nodes[1]);
336+
expect_payment_claimable!(nodes[1], payment_hash2, payment_secret2, payment_amount);
337+
338+
// Have nodes[1] attempt to fail/claim nodes[0]'s payment. Since nodes[1] already sent out
339+
// `stfu`, the `update_fail/fulfill` will go into the holding cell.
340+
if fail_htlc {
341+
nodes[1].node.fail_htlc_backwards(&payment_hash2);
342+
let failed_payment = HTLCDestination::FailedPayment { payment_hash: payment_hash2 };
343+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(&nodes[1], vec![failed_payment]);
344+
} else {
345+
nodes[1].node.claim_funds(payment_preimage2);
346+
check_added_monitors(&nodes[1], 1);
347+
}
348+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
349+
350+
// Finish the quiescence handshake.
351+
nodes[0].node.handle_stfu(node_id_1, &stfu);
352+
let stfu = get_event_msg!(&nodes[0], MessageSendEvent::SendStfu, node_id_1);
353+
nodes[1].node.handle_stfu(node_id_0, &stfu);
354+
355+
nodes[0].node.exit_quiescence(&node_id_1, &chan_id).unwrap();
356+
nodes[1].node.exit_quiescence(&node_id_0, &chan_id).unwrap();
357+
358+
// Now that quiescence is over, nodes are allowed to make updates again. nodes[1] will have its
359+
// outbound HTLC finally go out, along with the fail/claim of nodes[0]'s payment.
360+
let update = get_htlc_update_msgs!(&nodes[1], node_id_0);
361+
check_added_monitors(&nodes[1], 1);
362+
nodes[0].node.handle_update_add_htlc(node_id_1, &update.update_add_htlcs[0]);
363+
if fail_htlc {
364+
nodes[0].node.handle_update_fail_htlc(node_id_1, &update.update_fail_htlcs[0]);
365+
} else {
366+
nodes[0].node.handle_update_fulfill_htlc(node_id_1, &update.update_fulfill_htlcs[0]);
367+
}
368+
commitment_signed_dance!(&nodes[0], &nodes[1], update.commitment_signed, false);
369+
370+
if !fail_htlc {
371+
expect_payment_claimed!(nodes[1], payment_hash2, payment_amount);
372+
}
373+
374+
// The payment from nodes[0] should now be seen as failed/successful.
375+
let events = nodes[0].node.get_and_clear_pending_events();
376+
assert_eq!(events.len(), 3);
377+
assert!(events.iter().find(|e| matches!(e, Event::PendingHTLCsForwardable { .. })).is_some());
378+
if fail_htlc {
379+
assert!(events.iter().find(|e| matches!(e, Event::PaymentFailed { .. })).is_some());
380+
assert!(events.iter().find(|e| matches!(e, Event::PaymentPathFailed { .. })).is_some());
381+
} else {
382+
assert!(events.iter().find(|e| matches!(e, Event::PaymentSent { .. })).is_some());
383+
assert!(events.iter().find(|e| matches!(e, Event::PaymentPathSuccessful { .. })).is_some());
384+
check_added_monitors(&nodes[0], 1);
385+
}
386+
nodes[0].node.process_pending_htlc_forwards();
387+
expect_payment_claimable!(nodes[0], payment_hash1, payment_secret1, payment_amount);
388+
389+
// Have nodes[0] fail/claim nodes[1]'s payment.
390+
if fail_htlc {
391+
nodes[0].node.fail_htlc_backwards(&payment_hash1);
392+
let failed_payment = HTLCDestination::FailedPayment { payment_hash: payment_hash1 };
393+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(&nodes[0], vec![failed_payment]);
394+
} else {
395+
nodes[0].node.claim_funds(payment_preimage1);
396+
}
397+
check_added_monitors(&nodes[0], 1);
398+
399+
let update = get_htlc_update_msgs!(&nodes[0], node_id_1);
400+
if fail_htlc {
401+
nodes[1].node.handle_update_fail_htlc(node_id_0, &update.update_fail_htlcs[0]);
402+
} else {
403+
nodes[1].node.handle_update_fulfill_htlc(node_id_0, &update.update_fulfill_htlcs[0]);
404+
}
405+
commitment_signed_dance!(&nodes[1], &nodes[0], update.commitment_signed, false);
406+
407+
// The payment from nodes[1] should now be seen as failed/successful.
408+
if fail_htlc {
409+
let conditions = PaymentFailedConditions::new();
410+
expect_payment_failed_conditions(&nodes[1], payment_hash1, true, conditions);
411+
} else {
412+
expect_payment_claimed!(nodes[0], payment_hash1, payment_amount);
413+
expect_payment_sent(&nodes[1], payment_preimage1, None, true, true);
414+
}
415+
}

0 commit comments

Comments
 (0)