Skip to content

Commit 8dbdd15

Browse files
committed
Handle mon update completion actions even with update(s) is blocked
If we complete a `ChannelMonitorUpdate` persistence but there are blocked `ChannelMonitorUpdate`s in the channel, we'll skip all the post-monitor-update logic entirely. While its correct that we can't resume the channel (as it expected the monitor updates it generated to complete, even if they ended up blocked), the post-update actions are a `channelmanager.rs` concept - they cannot be tied to blocked updates because `channelmanager.rs` doesn't even see blocked updates. This can lead to a channel getting stuck waiting on itself. In a production environment, an LDK user saw a case where: (a) an MPP payment was received over several channels, let's call them A + B. (b) channel B got into `AwaitingRAA` due to unrelated operations, (c) the MPP payment was claimed, with async monitor updating, (d) the `revoke_and_ack` we were waiting on was delivered, but the resulting `ChannelMonitorUpdate` was blocked due to the pending claim having inserted an RAA-blocking action, (e) the preimage `ChannelMonitorUpdate` generated for channel B completed persistence, which did nothing due to the blocked `ChannelMonitorUpdate`. (f) the `Event::PaymentClaimed` event was handled but it, too, failed to unblock the channel. Instead, here, we simply process post-update actions when an update completes, even if there are pending blocked updates. We do not fully unblock the channel, of course. Backport of 8f4a4d2 Conflicts due to upstream macro rewrite resolved in: * lightning/src/ln/channelmanager.rs
1 parent a16bec0 commit 8dbdd15

File tree

3 files changed

+298
-162
lines changed

3 files changed

+298
-162
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ use crate::chain::transaction::OutPoint;
1919
use crate::chain::{ChannelMonitorUpdateStatus, Listen, Watch};
2020
use crate::events::{ClosureReason, Event, HTLCHandlingFailureType, PaymentPurpose};
2121
use crate::ln::channel::AnnouncementSigsState;
22-
use crate::ln::channelmanager::{PaymentId, RAACommitmentOrder, RecipientOnionFields};
22+
use crate::ln::channelmanager::{PaymentId, RAACommitmentOrder, RecipientOnionFields, Retry};
2323
use crate::ln::msgs;
2424
use crate::ln::msgs::{
2525
BaseMessageHandler, ChannelMessageHandler, MessageSendEvent, RoutingMessageHandler,
2626
};
2727
use crate::ln::types::ChannelId;
28+
use crate::routing::router::{PaymentParameters, RouteParameters};
2829
use crate::sign::NodeSigner;
2930
use crate::util::native_async::FutureQueue;
3031
use crate::util::persist::{
@@ -3542,7 +3543,7 @@ fn do_test_blocked_chan_preimage_release(completion_mode: BlockedUpdateComplMode
35423543
assert!(a.is_none());
35433544

35443545
nodes[1].node.handle_revoke_and_ack(node_a_id, &raa);
3545-
check_added_monitors(&nodes[1], 0);
3546+
check_added_monitors(&nodes[1], 1);
35463547
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
35473548
}
35483549

@@ -3561,8 +3562,8 @@ fn do_test_blocked_chan_preimage_release(completion_mode: BlockedUpdateComplMode
35613562
panic!();
35623563
}
35633564

3564-
// The event processing should release the last RAA updates on both channels.
3565-
check_added_monitors(&nodes[1], 2);
3565+
// The event processing should release the last RAA update.
3566+
check_added_monitors(&nodes[1], 1);
35663567

35673568
// When we fetch the next update the message getter will generate the next update for nodes[2],
35683569
// generating a further monitor update.
@@ -5058,3 +5059,111 @@ fn native_async_persist() {
50585059
panic!();
50595060
}
50605061
}
5062+
5063+
#[test]
5064+
fn test_mpp_claim_to_holding_cell() {
5065+
// Previously, if an MPP payment was claimed while one channel was AwaitingRAA (causing the
5066+
// HTLC claim to go into the holding cell), and the RAA came in before the async monitor
5067+
// update with the preimage completed, the channel could hang waiting on itself.
5068+
// This tests that behavior.
5069+
let chanmon_cfgs = create_chanmon_cfgs(4);
5070+
let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
5071+
let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
5072+
let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
5073+
5074+
let node_b_id = nodes[1].node.get_our_node_id();
5075+
let node_c_id = nodes[2].node.get_our_node_id();
5076+
let node_d_id = nodes[3].node.get_our_node_id();
5077+
5078+
// First open channels in a diamond and deliver the MPP payment.
5079+
let chan_1_scid = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
5080+
let chan_2_scid = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
5081+
let (chan_3_update, _, chan_3_id, ..) = create_announced_chan_between_nodes(&nodes, 1, 3);
5082+
let chan_3_scid = chan_3_update.contents.short_channel_id;
5083+
let (chan_4_update, _, chan_4_id, ..) = create_announced_chan_between_nodes(&nodes, 2, 3);
5084+
let chan_4_scid = chan_4_update.contents.short_channel_id;
5085+
5086+
let (mut route, paymnt_hash_1, preimage_1, payment_secret) =
5087+
get_route_and_payment_hash!(&nodes[0], nodes[3], 500_000);
5088+
let path = route.paths[0].clone();
5089+
route.paths.push(path);
5090+
route.paths[0].hops[0].pubkey = node_b_id;
5091+
route.paths[0].hops[0].short_channel_id = chan_1_scid;
5092+
route.paths[0].hops[1].short_channel_id = chan_3_scid;
5093+
route.paths[0].hops[1].fee_msat = 250_000;
5094+
route.paths[1].hops[0].pubkey = node_c_id;
5095+
route.paths[1].hops[0].short_channel_id = chan_2_scid;
5096+
route.paths[1].hops[1].short_channel_id = chan_4_scid;
5097+
route.paths[1].hops[1].fee_msat = 250_000;
5098+
let paths = &[&[&nodes[1], &nodes[3]][..], &[&nodes[2], &nodes[3]][..]];
5099+
send_along_route_with_secret(&nodes[0], route, paths, 500_000, paymnt_hash_1, payment_secret);
5100+
5101+
// Put the C <-> D channel into AwaitingRaa
5102+
let (preimage_2, paymnt_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[3]);
5103+
let onion = RecipientOnionFields::secret_only(payment_secret_2);
5104+
let id = PaymentId([42; 32]);
5105+
let pay_params = PaymentParameters::from_node_id(node_d_id, TEST_FINAL_CLTV);
5106+
let route_params = RouteParameters::from_payment_params_and_value(pay_params, 400_000);
5107+
nodes[2].node.send_payment(paymnt_hash_2, onion, id, route_params, Retry::Attempts(0)).unwrap();
5108+
check_added_monitors(&nodes[2], 1);
5109+
5110+
let mut payment_event = SendEvent::from_node(&nodes[2]);
5111+
nodes[3].node.handle_update_add_htlc(node_c_id, &payment_event.msgs[0]);
5112+
nodes[3].node.handle_commitment_signed_batch_test(node_c_id, &payment_event.commitment_msg);
5113+
check_added_monitors(&nodes[3], 1);
5114+
5115+
let (raa, cs) = get_revoke_commit_msgs(&nodes[3], &node_c_id);
5116+
nodes[2].node.handle_revoke_and_ack(node_d_id, &raa);
5117+
check_added_monitors(&nodes[2], 1);
5118+
5119+
nodes[2].node.handle_commitment_signed_batch_test(node_d_id, &cs);
5120+
check_added_monitors(&nodes[2], 1);
5121+
5122+
let cs_raa = get_event_msg!(nodes[2], MessageSendEvent::SendRevokeAndACK, node_d_id);
5123+
5124+
// Now claim the payment, completing both channel monitor updates async
5125+
// In the current code, the C <-> D channel happens to be the `durable_preimage_channel`,
5126+
// improving coverage somewhat but it isn't strictly critical to the test.
5127+
chanmon_cfgs[3].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
5128+
chanmon_cfgs[3].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
5129+
nodes[3].node.claim_funds(preimage_1);
5130+
check_added_monitors(&nodes[3], 2);
5131+
5132+
// Complete the B <-> D monitor update, freeing the first fulfill.
5133+
let (latest_id, _) = get_latest_mon_update_id(&nodes[3], chan_3_id);
5134+
nodes[3].chain_monitor.chain_monitor.channel_monitor_updated(chan_3_id, latest_id).unwrap();
5135+
let mut b_claim = get_htlc_update_msgs(&nodes[3], &node_b_id);
5136+
5137+
// When we deliver the pre-claim RAA, node D will shove the monitor update into the blocked
5138+
// state since we have a pending MPP payment which is blocking RAA monitor updates.
5139+
nodes[3].node.handle_revoke_and_ack(node_c_id, &cs_raa);
5140+
check_added_monitors(&nodes[3], 0);
5141+
5142+
// Finally, complete the C <-> D monitor update. Previously, this unlock failed to be processed
5143+
// due to the existence of the blocked RAA update above.
5144+
let (latest_id, _) = get_latest_mon_update_id(&nodes[3], chan_4_id);
5145+
nodes[3].chain_monitor.chain_monitor.channel_monitor_updated(chan_4_id, latest_id).unwrap();
5146+
// Once we process monitor events (in this case by checking for the `PaymentClaimed` event, the
5147+
// RAA monitor update blocked above will be released.
5148+
expect_payment_claimed!(nodes[3], paymnt_hash_1, 500_000);
5149+
check_added_monitors(&nodes[3], 1);
5150+
5151+
// After the RAA monitor update completes, the C <-> D channel will be able to generate its
5152+
// fulfill updates as well.
5153+
let mut c_claim = get_htlc_update_msgs(&nodes[3], &node_c_id);
5154+
check_added_monitors(&nodes[3], 1);
5155+
5156+
// Finally, clear all the pending payments.
5157+
let path = [&[&nodes[1], &nodes[3]][..], &[&nodes[2], &nodes[3]][..]];
5158+
let mut args = ClaimAlongRouteArgs::new(&nodes[0], &path[..], preimage_1);
5159+
let b_claim_msgs = (b_claim.update_fulfill_htlcs.pop().unwrap(), b_claim.commitment_signed);
5160+
let c_claim_msgs = (c_claim.update_fulfill_htlcs.pop().unwrap(), c_claim.commitment_signed);
5161+
let claims = vec![(b_claim_msgs, node_b_id), (c_claim_msgs, node_c_id)];
5162+
pass_claimed_payment_along_route_from_ev(250_000, claims, args);
5163+
5164+
expect_payment_sent(&nodes[0], preimage_1, None, true, true);
5165+
5166+
expect_and_process_pending_htlcs(&nodes[3], false);
5167+
expect_payment_claimable!(nodes[3], paymnt_hash_2, payment_secret_2, 400_000);
5168+
claim_payment(&nodes[2], &[&nodes[3]], preimage_2);
5169+
}

0 commit comments

Comments
 (0)