Skip to content

Commit a240136

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 1708036 commit a240136

File tree

2 files changed

+150
-27
lines changed

2 files changed

+150
-27
lines changed

lightning/src/ln/channel.rs

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

60516051
self.context.monitor_pending_update_adds.append(&mut pending_update_adds);
60526052

6053-
if self.context.channel_state.is_monitor_update_in_progress() {
6054-
// We can't actually generate a new commitment transaction (incl by freeing holding
6055-
// cells) while we can't update the monitor, so we just return what we have.
6056-
if require_commitment {
6057-
self.context.monitor_pending_commitment_signed = true;
6058-
// When the monitor updating is restored we'll call
6059-
// get_last_commitment_update_for_send(), which does not update state, but we're
6060-
// definitely now awaiting a remote revoke before we can step forward any more, so
6061-
// set it here.
6062-
let mut additional_update = self.build_commitment_no_status_check(logger);
6063-
// build_commitment_no_status_check may bump latest_monitor_id but we want them to be
6064-
// strictly increasing by one, so decrement it here.
6065-
self.context.latest_monitor_update_id = monitor_update.update_id;
6066-
monitor_update.updates.append(&mut additional_update.updates);
6067-
}
6068-
self.context.monitor_pending_forwards.append(&mut to_forward_infos);
6069-
self.context.monitor_pending_failures.append(&mut revoked_htlcs);
6070-
self.context.monitor_pending_finalized_fulfills.append(&mut finalized_claimed_htlcs);
6071-
log_debug!(logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply.", &self.context.channel_id());
6072-
return_with_htlcs_to_fail!(Vec::new());
6073-
}
6074-
6075-
match self.free_holding_cell_htlcs(fee_estimator, logger) {
6053+
match self.maybe_free_holding_cell_htlcs(fee_estimator, logger) {
60766054
(Some(mut additional_update), htlcs_to_fail) => {
60776055
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
60786056
// strictly increasing by one, so decrement it here.
@@ -6087,17 +6065,36 @@ impl<SP: Deref> FundedChannel<SP> where
60876065
},
60886066
(None, htlcs_to_fail) => {
60896067
if require_commitment {
6068+
// We can't generate a new commitment transaction yet so we just return what we
6069+
// have. When the monitor updating is restored we'll call
6070+
// get_last_commitment_update_for_send(), which does not update state, but we're
6071+
// definitely now awaiting a remote revoke before we can step forward any more,
6072+
// so set it here.
60906073
let mut additional_update = self.build_commitment_no_status_check(logger);
60916074

60926075
// build_commitment_no_status_check may bump latest_monitor_id but we want them to be
60936076
// strictly increasing by one, so decrement it here.
60946077
self.context.latest_monitor_update_id = monitor_update.update_id;
60956078
monitor_update.updates.append(&mut additional_update.updates);
60966079

6097-
log_debug!(logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed. {} monitor update.",
6098-
&self.context.channel_id(),
6099-
update_fail_htlcs.len() + update_fail_malformed_htlcs.len(),
6100-
release_state_str);
6080+
log_debug!(logger, "Received a valid revoke_and_ack for channel {}. {} monitor update.",
6081+
&self.context.channel_id(), release_state_str);
6082+
if self.context.channel_state.can_generate_new_commitment() {
6083+
log_debug!(logger, "Responding with a commitment update with {} HTLCs failed for channel {}",
6084+
update_fail_htlcs.len() + update_fail_malformed_htlcs.len(),
6085+
&self.context.channel_id);
6086+
} else {
6087+
debug_assert!(htlcs_to_fail.is_empty());
6088+
let reason = if self.context.channel_state.is_local_stfu_sent() {
6089+
"exits quiescence"
6090+
} else if self.context.channel_state.is_monitor_update_in_progress() {
6091+
"completes pending monitor update"
6092+
} else {
6093+
"can continue progress"
6094+
};
6095+
log_debug!(logger, "Holding back commitment update until channel {} {}",
6096+
&self.context.channel_id, reason);
6097+
}
61016098

61026099
self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
61036100
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;
@@ -284,3 +285,128 @@ 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+
let events = nodes[0].node.get_and_clear_pending_events();
375+
assert_eq!(events.len(), 3);
376+
assert!(events.iter().find(|e| matches!(e, Event::PendingHTLCsForwardable { .. })).is_some());
377+
if fail_htlc {
378+
assert!(events.iter().find(|e| matches!(e, Event::PaymentFailed { .. })).is_some());
379+
assert!(events.iter().find(|e| matches!(e, Event::PaymentPathFailed { .. })).is_some());
380+
} else {
381+
assert!(events.iter().find(|e| matches!(e, Event::PaymentSent { .. })).is_some());
382+
assert!(events.iter().find(|e| matches!(e, Event::PaymentPathSuccessful { .. })).is_some());
383+
check_added_monitors(&nodes[0], 1);
384+
}
385+
nodes[0].node.process_pending_htlc_forwards();
386+
expect_payment_claimable!(nodes[0], payment_hash1, payment_secret1, payment_amount);
387+
388+
if fail_htlc {
389+
nodes[0].node.fail_htlc_backwards(&payment_hash1);
390+
let failed_payment = HTLCDestination::FailedPayment { payment_hash: payment_hash1 };
391+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(&nodes[0], vec![failed_payment]);
392+
} else {
393+
nodes[0].node.claim_funds(payment_preimage1);
394+
}
395+
check_added_monitors(&nodes[0], 1);
396+
397+
let update = get_htlc_update_msgs!(&nodes[0], node_id_1);
398+
if fail_htlc {
399+
nodes[1].node.handle_update_fail_htlc(node_id_0, &update.update_fail_htlcs[0]);
400+
} else {
401+
nodes[1].node.handle_update_fulfill_htlc(node_id_0, &update.update_fulfill_htlcs[0]);
402+
}
403+
commitment_signed_dance!(&nodes[1], &nodes[0], update.commitment_signed, false);
404+
405+
if fail_htlc {
406+
let conditions = PaymentFailedConditions::new();
407+
expect_payment_failed_conditions(&nodes[1], payment_hash1, true, conditions);
408+
} else {
409+
expect_payment_claimed!(nodes[0], payment_hash1, payment_amount);
410+
expect_payment_sent(&nodes[1], payment_preimage1, None, true, true);
411+
}
412+
}

0 commit comments

Comments
 (0)