Skip to content

Commit f94de18

Browse files
committed
Delay RAA-after-next processing until PaymentSent is are handled
In 0ad1f4c we fixed a nasty bug where a failure to persist a `ChannelManager` faster than a `ChannelMonitor` could result in the loss of a `PaymentSent` event, eventually resulting in a `PaymentFailed` instead! As noted in that commit, there's still some risk, though its been substantially reduced - if we receive an `update_fulfill_htlc` message for an outbound payment, and persist the initial removal `ChannelMonitorUpdate`, then respond with our own `commitment_signed` + `revoke_and_ack`, followed by receiving our peer's final `revoke_and_ack`, and then persist the `ChannelMonitorUpdate` generated from that, all prior to completing a `ChannelManager` persistence, we'll still forget the HTLC and eventually trigger a `PaymentFailed` rather than the correct `PaymentSent`. Here we fully fix the issue by delaying the final `ChannelMonitorUpdate` persistence until the `PaymentSent` event has been processed and document the fact that a spurious `PaymentFailed` event can still be generated for a sent payment. The original fix in 0ad1f4c is still incredibly useful here, allowing us to avoid blocking the first `ChannelMonitorUpdate` until the event processing completes, as this would cause us to add event-processing delay in our general commitment update latency. Instead, we ultimately race the user handling the `PaymentSent` event with how long it takes our `revoke_and_ack` + `commitment_signed` to make it to our counterparty and receive the response `revoke_and_ack`. This should give the user plenty of time to handle the event before we need to make progress. Sadly, because we change our `ChannelMonitorUpdate` semantics, this change requires a number of test changes, avoiding checking for a post-RAA `ChannelMonitorUpdate` until after we process a `PaymentSent` event. Note that this does not apply to payments we learned the preimage for on-chain - ensuring `PaymentSent` events from such resolutions will be addressed in a future PR. Thus, tests which resolve payments on-chain switch to a direct call to the `expect_payment_sent` function with the claim-expected flag unset.
1 parent 9852b84 commit f94de18

14 files changed

+293
-101
lines changed

lightning-invoice/src/utils.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -1091,22 +1091,7 @@ mod test {
10911091
let payment_preimage_opt = if user_generated_pmt_hash { None } else { Some(payment_preimage) };
10921092
expect_payment_claimable!(&nodes[fwd_idx], payment_hash, payment_secret, payment_amt, payment_preimage_opt, route.paths[0].last().unwrap().pubkey);
10931093
do_claim_payment_along_route(&nodes[0], &[&vec!(&nodes[fwd_idx])[..]], false, payment_preimage);
1094-
let events = nodes[0].node.get_and_clear_pending_events();
1095-
assert_eq!(events.len(), 2);
1096-
match events[0] {
1097-
Event::PaymentSent { payment_preimage: ref ev_preimage, payment_hash: ref ev_hash, ref fee_paid_msat, .. } => {
1098-
assert_eq!(payment_preimage, *ev_preimage);
1099-
assert_eq!(payment_hash, *ev_hash);
1100-
assert_eq!(fee_paid_msat, &Some(0));
1101-
},
1102-
_ => panic!("Unexpected event")
1103-
}
1104-
match events[1] {
1105-
Event::PaymentPathSuccessful { payment_hash: hash, .. } => {
1106-
assert_eq!(hash, Some(payment_hash));
1107-
},
1108-
_ => panic!("Unexpected event")
1109-
}
1094+
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
11101095
}
11111096

11121097
#[test]

lightning/src/chain/chainmonitor.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ mod tests {
788788
use bitcoin::{BlockHeader, TxMerkleNode};
789789
use bitcoin::hashes::Hash;
790790
use crate::{check_added_monitors, check_closed_broadcast, check_closed_event};
791-
use crate::{expect_payment_sent, expect_payment_claimed, expect_payment_sent_without_paths, expect_payment_path_successful, get_event_msg};
791+
use crate::{expect_payment_claimed, expect_payment_path_successful, get_event_msg};
792792
use crate::{get_htlc_update_msgs, get_local_commitment_txn, get_revoke_commit_msgs, get_route_and_payment_hash, unwrap_send_err};
793793
use crate::chain::{ChannelMonitorUpdateStatus, Confirm, Watch};
794794
use crate::chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS;
@@ -871,7 +871,7 @@ mod tests {
871871

872872
let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
873873
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
874-
expect_payment_sent_without_paths!(nodes[0], payment_preimage_1);
874+
expect_payment_sent(&nodes[0], payment_preimage_1, None, false, false);
875875
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &updates.commitment_signed);
876876
check_added_monitors!(nodes[0], 1);
877877
let (as_first_raa, as_first_update) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
@@ -884,7 +884,7 @@ mod tests {
884884
let bs_first_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
885885

886886
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_second_updates.update_fulfill_htlcs[0]);
887-
expect_payment_sent_without_paths!(nodes[0], payment_preimage_2);
887+
expect_payment_sent(&nodes[0], payment_preimage_2, None, false, false);
888888
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_updates.commitment_signed);
889889
check_added_monitors!(nodes[0], 1);
890890
nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_first_raa);
@@ -972,7 +972,7 @@ mod tests {
972972
}
973973
}
974974

975-
expect_payment_sent!(nodes[0], payment_preimage);
975+
expect_payment_sent(&nodes[0], payment_preimage, None, true, false);
976976
}
977977

978978
#[test]

lightning/src/ln/chanmon_update_fail_tests.rs

+72-7
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,7 @@ fn claim_while_disconnected_monitor_update_fail() {
13711371
MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
13721372
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
13731373
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
1374+
expect_payment_sent(&nodes[0], payment_preimage_1, None, false, false);
13741375
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &updates.commitment_signed);
13751376
check_added_monitors!(nodes[0], 1);
13761377

@@ -1408,7 +1409,7 @@ fn claim_while_disconnected_monitor_update_fail() {
14081409

14091410
nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
14101411
check_added_monitors!(nodes[0], 1);
1411-
expect_payment_sent!(nodes[0], payment_preimage_1);
1412+
expect_payment_path_successful!(nodes[0]);
14121413

14131414
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
14141415
}
@@ -2140,7 +2141,7 @@ fn test_fail_htlc_on_broadcast_after_claim() {
21402141
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_id_2 }]);
21412142

21422143
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
2143-
expect_payment_sent_without_paths!(nodes[0], payment_preimage);
2144+
expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
21442145
commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, true, true);
21452146
expect_payment_path_successful!(nodes[0]);
21462147
}
@@ -2376,7 +2377,7 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
23762377
assert!(updates.update_fee.is_none());
23772378
assert_eq!(updates.update_fulfill_htlcs.len(), 1);
23782379
nodes[1].node.handle_update_fulfill_htlc(&nodes[0].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
2379-
expect_payment_sent_without_paths!(nodes[1], payment_preimage_0);
2380+
expect_payment_sent(&nodes[1], payment_preimage_0, None, false, false);
23802381
assert_eq!(updates.update_add_htlcs.len(), 1);
23812382
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
23822383
updates.commitment_signed
@@ -2393,7 +2394,7 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
23932394
expect_payment_claimable!(nodes[1], payment_hash_1, payment_secret_1, 100000);
23942395
check_added_monitors!(nodes[1], 1);
23952396

2396-
commitment_signed_dance!(nodes[1], nodes[0], (), false, true, false);
2397+
commitment_signed_dance!(nodes[1], nodes[0], (), false, true, false, false);
23972398

23982399
let events = nodes[1].node.get_and_clear_pending_events();
23992400
assert_eq!(events.len(), 2);
@@ -2493,7 +2494,7 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
24932494
bs_updates = Some(get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()));
24942495
assert_eq!(bs_updates.as_ref().unwrap().update_fulfill_htlcs.len(), 1);
24952496
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.as_ref().unwrap().update_fulfill_htlcs[0]);
2496-
expect_payment_sent_without_paths!(nodes[0], payment_preimage);
2497+
expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
24972498
if htlc_status == HTLCStatusAtDupClaim::Cleared {
24982499
commitment_signed_dance!(nodes[0], nodes[1], &bs_updates.as_ref().unwrap().commitment_signed, false);
24992500
expect_payment_path_successful!(nodes[0]);
@@ -2520,7 +2521,7 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
25202521
bs_updates = Some(get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()));
25212522
assert_eq!(bs_updates.as_ref().unwrap().update_fulfill_htlcs.len(), 1);
25222523
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.as_ref().unwrap().update_fulfill_htlcs[0]);
2523-
expect_payment_sent_without_paths!(nodes[0], payment_preimage);
2524+
expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
25242525
}
25252526
if htlc_status != HTLCStatusAtDupClaim::Cleared {
25262527
commitment_signed_dance!(nodes[0], nodes[1], &bs_updates.as_ref().unwrap().commitment_signed, false);
@@ -2717,7 +2718,7 @@ fn double_temp_error() {
27172718
assert_eq!(node_id, nodes[0].node.get_our_node_id());
27182719
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_1);
27192720
check_added_monitors!(nodes[0], 0);
2720-
expect_payment_sent_without_paths!(nodes[0], payment_preimage_1);
2721+
expect_payment_sent(&nodes[0], payment_preimage_1, None, false, false);
27212722
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed_b1);
27222723
check_added_monitors!(nodes[0], 1);
27232724
nodes[0].node.process_pending_htlc_forwards();
@@ -2940,3 +2941,67 @@ fn test_inbound_reload_without_init_mon() {
29402941
do_test_inbound_reload_without_init_mon(false, true);
29412942
do_test_inbound_reload_without_init_mon(false, false);
29422943
}
2944+
2945+
#[test]
2946+
fn test_blocked_chan_preimage_release() {
2947+
// Test that even if a channel's `ChannelMonitorUpdate` flow is blocked waiting on an event to
2948+
// be handled HTLC preimage `ChannelMonitorUpdate`s will still go out.
2949+
let chanmon_cfgs = create_chanmon_cfgs(3);
2950+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
2951+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
2952+
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
2953+
2954+
create_announced_chan_between_nodes(&nodes, 0, 1).2;
2955+
create_announced_chan_between_nodes(&nodes, 1, 2).2;
2956+
2957+
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5_000_000);
2958+
2959+
// Tee up two payments in opposite directions across nodes[1], one it sent to generate a
2960+
// PaymentSent event and one it forwards.
2961+
let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[1], &[&nodes[2]], 1_000_000);
2962+
let (payment_preimage_2, payment_hash_2, _) = route_payment(&nodes[2], &[&nodes[1], &nodes[0]], 1_000_000);
2963+
2964+
// Claim the first payment to get a `PaymentSent` event (but don't handle it yet).
2965+
nodes[2].node.claim_funds(payment_preimage_1);
2966+
check_added_monitors(&nodes[2], 1);
2967+
expect_payment_claimed!(nodes[2], payment_hash_1, 1_000_000);
2968+
2969+
let cs_htlc_fulfill_updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
2970+
nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &cs_htlc_fulfill_updates.update_fulfill_htlcs[0]);
2971+
commitment_signed_dance!(nodes[1], nodes[2], cs_htlc_fulfill_updates.commitment_signed, false);
2972+
check_added_monitors(&nodes[1], 0);
2973+
2974+
// Now claim the second payment on nodes[0], which will ultimately result in nodes[1] trying to
2975+
// claim an HTLC on its channel with nodes[2], but that channel is blocked on the above
2976+
// `PaymentSent` event.
2977+
nodes[0].node.claim_funds(payment_preimage_2);
2978+
check_added_monitors(&nodes[0], 1);
2979+
expect_payment_claimed!(nodes[0], payment_hash_2, 1_000_000);
2980+
2981+
let as_htlc_fulfill_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2982+
nodes[1].node.handle_update_fulfill_htlc(&nodes[0].node.get_our_node_id(), &as_htlc_fulfill_updates.update_fulfill_htlcs[0]);
2983+
check_added_monitors(&nodes[1], 1); // We generate only a preimage monitor update
2984+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2985+
2986+
// Finish the CS dance between nodes[0] and nodes[1].
2987+
commitment_signed_dance!(nodes[1], nodes[0], as_htlc_fulfill_updates.commitment_signed, false);
2988+
check_added_monitors(&nodes[1], 0);
2989+
2990+
let events = nodes[1].node.get_and_clear_pending_events();
2991+
assert_eq!(events.len(), 3);
2992+
if let Event::PaymentSent { .. } = events[0] {} else { panic!(); }
2993+
if let Event::PaymentPathSuccessful { .. } = events[2] {} else { panic!(); }
2994+
if let Event::PaymentForwarded { .. } = events[1] {} else { panic!(); }
2995+
2996+
// The event processing should let the last RAA update fly.
2997+
check_added_monitors(&nodes[1], 1);
2998+
2999+
// When we fetch the next update the message getter will generate the next update for nodes[2],
3000+
// generating a further monitor update.
3001+
let bs_htlc_fulfill_updates = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
3002+
check_added_monitors(&nodes[1], 1);
3003+
3004+
nodes[2].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_htlc_fulfill_updates.update_fulfill_htlcs[0]);
3005+
commitment_signed_dance!(nodes[2], nodes[1], bs_htlc_fulfill_updates.commitment_signed, false);
3006+
expect_payment_sent(&nodes[2], payment_preimage_2, None, true, true);
3007+
}

lightning/src/ln/channel.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -3427,7 +3427,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
34273427
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
34283428
/// generating an appropriate error *after* the channel state has been updated based on the
34293429
/// revoke_and_ack message.
3430-
pub fn revoke_and_ack<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result<(Vec<(HTLCSource, PaymentHash)>, Option<&ChannelMonitorUpdate>), ChannelError>
3430+
pub fn revoke_and_ack<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L, hold_mon_update: bool)
3431+
-> Result<(Vec<(HTLCSource, PaymentHash)>, Option<&ChannelMonitorUpdate>), ChannelError>
34313432
where L::Target: Logger,
34323433
{
34333434
if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
@@ -3624,7 +3625,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36243625
self.monitor_pending_failures.append(&mut revoked_htlcs);
36253626
self.monitor_pending_finalized_fulfills.append(&mut finalized_claimed_htlcs);
36263627
log_debug!(logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply.", log_bytes!(self.channel_id()));
3627-
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
3628+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown) && !hold_mon_update;
36283629
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
36293630
update: monitor_update, flown: fly_monitor,
36303631
});
@@ -3641,7 +3642,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36413642
monitor_update.updates.append(&mut additional_update.updates);
36423643

36433644
self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
3644-
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
3645+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown) && !hold_mon_update;
36453646
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
36463647
update: monitor_update, flown: fly_monitor,
36473648
});
@@ -3660,7 +3661,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36603661
log_debug!(logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed.",
36613662
log_bytes!(self.channel_id()), update_fail_htlcs.len() + update_fail_malformed_htlcs.len());
36623663
self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
3663-
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
3664+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown) && !hold_mon_update;
36643665
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
36653666
update: monitor_update, flown: fly_monitor,
36663667
});
@@ -3669,7 +3670,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36693670
} else {
36703671
log_debug!(logger, "Received a valid revoke_and_ack for channel {} with no reply necessary.", log_bytes!(self.channel_id()));
36713672
self.monitor_updating_paused(false, false, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
3672-
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
3673+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown) && !hold_mon_update;
36733674
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
36743675
update: monitor_update, flown: fly_monitor,
36753676
});

0 commit comments

Comments
 (0)