Skip to content

Commit 6e2abc0

Browse files
committed
Block monitor updates to ensure preimages are in each MPP part
If we claim an MPP payment and only persist some of the `ChannelMonitorUpdate`s which include the preimage prior to shutting down, we may be in a state where some of our `ChannelMonitor`s have the preimage for a payment while others do not. This, it turns out, is actually mostly safe - on startup `ChanelManager` will detect there's a payment it has as unclaimed but there's a corresponding payment preimage in a `ChannelMonitor` and go claim the other MPP parts. This works so long as the `ChannelManager` has been persisted after the payment has been received but prior to the `PaymentClaimable` event being processed (and the claim itself occurring). This is not always true and certainly not required on our API, but our `lightning-background-processor` today does persist prior to event handling so is generally true subject to some race conditions. In order to address this we need to use copy payment preimages across channels irrespective of the `ChannelManager`'s payment state, but this introduces another wrinkle - if one channel makes substantial progress while other channel(s) are still waiting to get the payment preimage in `ChannelMonitor`(s) while the `ChannelManager` hasn't been persisted after the payment was received, we may end up without the preimage on disk. Here, we address this issue by using the new `RAAMonitorUpdateBlockingAction` variant for this specific case. We block persistence of an RAA `ChannelMonitorUpdate` which may remove the preimage from disk until all channels have had the preimage added to their `ChannelMonitor`. We do this only in-memory (and not on disk) as we can recreate this blocker during the startup re-claim logic. This will enable us to claim MPP parts without using the `ChannelManager`'s payment state in later work.
1 parent 14e5a98 commit 6e2abc0

File tree

1 file changed

+143
-15
lines changed

1 file changed

+143
-15
lines changed

lightning/src/ln/channelmanager.rs

+143-15
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,13 @@ pub(crate) enum MonitorUpdateCompletionAction {
799799
/// [`events::Event::PaymentClaimed`] to the user if we haven't yet generated such an event for
800800
/// this payment. Note that this is only best-effort. On restart it's possible such a duplicate
801801
/// event can be generated.
802-
PaymentClaimed { payment_hash: PaymentHash },
802+
PaymentClaimed {
803+
payment_hash: PaymentHash,
804+
/// A pending MPP claim which hasn't yet completed.
805+
///
806+
/// Not written to disk.
807+
pending_mpp_claim: Option<(PublicKey, ChannelId, u64, PendingMPPClaimPointer)>,
808+
},
803809
/// Indicates an [`events::Event`] should be surfaced to the user and possibly resume the
804810
/// operation of another channel.
805811
///
@@ -833,7 +839,10 @@ pub(crate) enum MonitorUpdateCompletionAction {
833839
}
834840

835841
impl_writeable_tlv_based_enum_upgradable!(MonitorUpdateCompletionAction,
836-
(0, PaymentClaimed) => { (0, payment_hash, required) },
842+
(0, PaymentClaimed) => {
843+
(0, payment_hash, required),
844+
(9999999999, pending_mpp_claim, (static_value, None)),
845+
},
837846
// Note that FreeOtherChannelImmediately should never be written - we were supposed to free
838847
// *immediately*. However, for simplicity we implement read/write here.
839848
(1, FreeOtherChannelImmediately) => {
@@ -6259,13 +6268,44 @@ where
62596268
return;
62606269
}
62616270
if valid_mpp {
6262-
for htlc in sources.drain(..) {
6271+
let mut pending_claim_ptr_opt = None;
6272+
let mut source_claim_pairs = Vec::with_capacity(sources.len());
6273+
if sources.len() > 1 {
6274+
let mut pending_claims = PendingMPPClaim {
6275+
channels_without_preimage: Vec::new(),
6276+
channels_with_preimage: Vec::new(),
6277+
};
6278+
for htlc in sources.drain(..) {
6279+
if let Some(cp_id) = htlc.prev_hop.counterparty_node_id {
6280+
let htlc_id = htlc.prev_hop.htlc_id;
6281+
let chan_id = htlc.prev_hop.channel_id;
6282+
let chan_outpoint = htlc.prev_hop.outpoint;
6283+
pending_claims.channels_without_preimage.push((cp_id, chan_outpoint, chan_id, htlc_id));
6284+
source_claim_pairs.push((htlc, Some((cp_id, chan_id, htlc_id))));
6285+
}
6286+
}
6287+
pending_claim_ptr_opt = Some(Arc::new(Mutex::new(pending_claims)));
6288+
} else {
6289+
for htlc in sources.drain(..) {
6290+
source_claim_pairs.push((htlc, None));
6291+
}
6292+
}
6293+
for (htlc, mpp_claim) in source_claim_pairs.drain(..) {
6294+
let mut pending_mpp_claim = None;
6295+
let pending_claim_ptr = pending_claim_ptr_opt.as_ref().map(|pending_claim| {
6296+
pending_mpp_claim = mpp_claim.map(|(cp_id, chan_id, htlc_id)|
6297+
(cp_id, chan_id, htlc_id, PendingMPPClaimPointer(Arc::clone(pending_claim)))
6298+
);
6299+
RAAMonitorUpdateBlockingAction::ClaimedMPPPayment {
6300+
pending_claim: PendingMPPClaimPointer(Arc::clone(pending_claim)),
6301+
}
6302+
});
62636303
let prev_hop_chan_id = htlc.prev_hop.channel_id;
62646304
if let Err((pk, err)) = self.claim_funds_from_hop(
62656305
htlc.prev_hop, payment_preimage,
62666306
|_, definitely_duplicate| {
62676307
debug_assert!(!definitely_duplicate, "We shouldn't claim duplicatively from a payment");
6268-
Some(MonitorUpdateCompletionAction::PaymentClaimed { payment_hash })
6308+
(Some(MonitorUpdateCompletionAction::PaymentClaimed { payment_hash, pending_mpp_claim }), pending_claim_ptr)
62696309
}
62706310
) {
62716311
if let msgs::ErrorAction::IgnoreError = err.err.action {
@@ -6296,7 +6336,7 @@ where
62966336
}
62976337
}
62986338

6299-
fn claim_funds_from_hop<ComplFunc: FnOnce(Option<u64>, bool) -> Option<MonitorUpdateCompletionAction>>(&self,
6339+
fn claim_funds_from_hop<ComplFunc: FnOnce(Option<u64>, bool) -> (Option<MonitorUpdateCompletionAction>, Option<RAAMonitorUpdateBlockingAction>)>(&self,
63006340
prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage, completion_action: ComplFunc)
63016341
-> Result<(), (PublicKey, MsgHandleErrInternal)> {
63026342
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
@@ -6335,11 +6375,15 @@ where
63356375

63366376
match fulfill_res {
63376377
UpdateFulfillCommitFetch::NewClaim { htlc_value_msat, monitor_update } => {
6338-
if let Some(action) = completion_action(Some(htlc_value_msat), false) {
6378+
let (action_opt, raa_blocker_opt) = completion_action(Some(htlc_value_msat), false);
6379+
if let Some(action) = action_opt {
63396380
log_trace!(logger, "Tracking monitor update completion action for channel {}: {:?}",
63406381
chan_id, action);
63416382
peer_state.monitor_update_blocked_actions.entry(chan_id).or_insert(Vec::new()).push(action);
63426383
}
6384+
if let Some(raa_blocker) = raa_blocker_opt {
6385+
peer_state.actions_blocking_raa_monitor_updates.entry(chan_id).or_insert_with(Vec::new).push(raa_blocker);
6386+
}
63436387
if !during_init {
63446388
handle_new_monitor_update!(self, prev_hop.outpoint, monitor_update, peer_state_lock,
63456389
peer_state, per_peer_state, chan);
@@ -6357,11 +6401,16 @@ where
63576401
}
63586402
}
63596403
UpdateFulfillCommitFetch::DuplicateClaim {} => {
6360-
let action = if let Some(action) = completion_action(None, true) {
6404+
let (action_opt, raa_blocker_opt) = completion_action(None, true);
6405+
let action = if let Some(action) = action_opt {
63616406
action
63626407
} else {
63636408
return Ok(());
63646409
};
6410+
if let Some(raa_blocker) = raa_blocker_opt {
6411+
debug_assert!(peer_state.actions_blocking_raa_monitor_updates.get(&chan_id).unwrap().contains(&raa_blocker));
6412+
}
6413+
63656414
mem::drop(peer_state_lock);
63666415

63676416
log_trace!(logger, "Completing monitor update completion action for channel {} as claim was redundant: {:?}",
@@ -6448,7 +6497,47 @@ where
64486497
// `ChannelMonitor` we've provided the above update to. Instead, note that `Event`s are
64496498
// generally always allowed to be duplicative (and it's specifically noted in
64506499
// `PaymentForwarded`).
6451-
self.handle_monitor_update_completion_actions(completion_action(None, false));
6500+
let (action_opt, raa_blocker_opt) = completion_action(None, false);
6501+
6502+
if let Some(raa_blocker) = raa_blocker_opt {
6503+
let counterparty_node_id = prev_hop.counterparty_node_id.or_else(||
6504+
// prev_hop.counterparty_node_id is always available for payments received after
6505+
// LDK 0.0.123, but for those received on 0.0.123 and claimed later, we need to
6506+
// look up the counterparty in the `action_opt`, if possible.
6507+
if let Some(action) = &action_opt {
6508+
if let MonitorUpdateCompletionAction::PaymentClaimed { pending_mpp_claim, .. } = action {
6509+
if let Some((node_id, _, _, _)) = pending_mpp_claim {
6510+
Some(*node_id)
6511+
} else { None }
6512+
} else { None }
6513+
} else { None });
6514+
if let Some(counterparty_node_id) = counterparty_node_id {
6515+
// TODO: Avoid always blocking the world for the write lock here.
6516+
let mut per_peer_state = self.per_peer_state.write().unwrap();
6517+
let peer_state_mutex = per_peer_state.entry(counterparty_node_id).or_insert_with(||
6518+
Mutex::new(PeerState {
6519+
channel_by_id: new_hash_map(),
6520+
inbound_channel_request_by_id: new_hash_map(),
6521+
latest_features: InitFeatures::empty(),
6522+
pending_msg_events: Vec::new(),
6523+
in_flight_monitor_updates: BTreeMap::new(),
6524+
monitor_update_blocked_actions: BTreeMap::new(),
6525+
actions_blocking_raa_monitor_updates: BTreeMap::new(),
6526+
is_connected: false,
6527+
}));
6528+
let mut peer_state = peer_state_mutex.lock().unwrap();
6529+
6530+
peer_state.actions_blocking_raa_monitor_updates
6531+
.entry(prev_hop.channel_id)
6532+
.or_insert_with(Vec::new)
6533+
.push(raa_blocker);
6534+
} else {
6535+
debug_assert!(false,
6536+
"RAA ChannelMonitorUpdate blockers are only set with PaymentClaimed completion actions, so we should always have a counterparty node id");
6537+
}
6538+
}
6539+
6540+
self.handle_monitor_update_completion_actions(action_opt);
64526541
Ok(())
64536542
}
64546543

@@ -6548,16 +6637,16 @@ where
65486637
}
65496638
}), "{:?}", *background_events);
65506639
}
6551-
None
6640+
(None, None)
65526641
} else if definitely_duplicate {
65536642
if let Some(other_chan) = chan_to_release {
6554-
Some(MonitorUpdateCompletionAction::FreeOtherChannelImmediately {
6643+
(Some(MonitorUpdateCompletionAction::FreeOtherChannelImmediately {
65556644
downstream_counterparty_node_id: other_chan.counterparty_node_id,
65566645
downstream_funding_outpoint: other_chan.funding_txo,
65576646
downstream_channel_id: other_chan.channel_id,
65586647
blocking_action: other_chan.blocking_action,
6559-
})
6560-
} else { None }
6648+
}), None)
6649+
} else { (None, None) }
65616650
} else {
65626651
let total_fee_earned_msat = if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
65636652
if let Some(claimed_htlc_value) = htlc_claim_value_msat {
@@ -6566,7 +6655,7 @@ where
65666655
} else { None };
65676656
debug_assert!(skimmed_fee_msat <= total_fee_earned_msat,
65686657
"skimmed_fee_msat must always be included in total_fee_earned_msat");
6569-
Some(MonitorUpdateCompletionAction::EmitEventAndFreeOtherChannel {
6658+
(Some(MonitorUpdateCompletionAction::EmitEventAndFreeOtherChannel {
65706659
event: events::Event::PaymentForwarded {
65716660
prev_channel_id: Some(prev_channel_id),
65726661
next_channel_id: Some(next_channel_id),
@@ -6578,7 +6667,7 @@ where
65786667
outbound_amount_forwarded_msat: forwarded_htlc_value_msat,
65796668
},
65806669
downstream_counterparty_and_funding_outpoint: chan_to_release,
6581-
})
6670+
}), None)
65826671
}
65836672
});
65846673
if let Err((pk, err)) = res {
@@ -6599,9 +6688,44 @@ where
65996688
debug_assert_ne!(self.claimable_payments.held_by_thread(), LockHeldState::HeldByThread);
66006689
debug_assert_ne!(self.per_peer_state.held_by_thread(), LockHeldState::HeldByThread);
66016690

6691+
let mut freed_channels = Vec::new();
6692+
66026693
for action in actions.into_iter() {
66036694
match action {
6604-
MonitorUpdateCompletionAction::PaymentClaimed { payment_hash } => {
6695+
MonitorUpdateCompletionAction::PaymentClaimed { payment_hash, pending_mpp_claim } => {
6696+
if let Some((counterparty_node_id, chan_id, htlc_id, claim_ptr)) = pending_mpp_claim {
6697+
let per_peer_state = self.per_peer_state.read().unwrap();
6698+
per_peer_state.get(&counterparty_node_id).map(|peer_state_mutex| {
6699+
let mut peer_state = peer_state_mutex.lock().unwrap();
6700+
let blockers_entry = peer_state.actions_blocking_raa_monitor_updates.entry(chan_id);
6701+
if let btree_map::Entry::Occupied(mut blockers) = blockers_entry {
6702+
blockers.get_mut().retain(|blocker|
6703+
if let &RAAMonitorUpdateBlockingAction::ClaimedMPPPayment { pending_claim } = &blocker {
6704+
if *pending_claim == claim_ptr {
6705+
let mut pending_claim_state_lock = pending_claim.0.lock().unwrap();
6706+
let pending_claim_state = &mut *pending_claim_state_lock;
6707+
pending_claim_state.channels_without_preimage.retain(|(cp, outp, cid, hid)| {
6708+
if *cp == counterparty_node_id && *cid == chan_id && *hid == htlc_id {
6709+
pending_claim_state.channels_with_preimage.push((*cp, *outp, *cid));
6710+
false
6711+
} else { true }
6712+
});
6713+
if pending_claim_state.channels_without_preimage.is_empty() {
6714+
for (cp, outp, cid) in pending_claim_state.channels_with_preimage.iter() {
6715+
freed_channels.push((*cp, *outp, *cid, blocker.clone()));
6716+
}
6717+
}
6718+
!pending_claim_state.channels_without_preimage.is_empty()
6719+
} else { true }
6720+
} else { true }
6721+
);
6722+
if blockers.get().is_empty() {
6723+
blockers.remove();
6724+
}
6725+
}
6726+
});
6727+
}
6728+
66056729
let payment = self.claimable_payments.lock().unwrap().pending_claiming_payments.remove(&payment_hash);
66066730
if let Some(ClaimingPayment {
66076731
amount_msat,
@@ -6645,6 +6769,10 @@ where
66456769
},
66466770
}
66476771
}
6772+
6773+
for (node_id, funding_outpoint, channel_id, blocker) in freed_channels {
6774+
self.handle_monitor_update_release(node_id, funding_outpoint, channel_id, Some(blocker));
6775+
}
66486776
}
66496777

66506778
/// Handles a channel reentering a functional state, either due to reconnect or a monitor

0 commit comments

Comments
 (0)