Skip to content

Commit 0ab2e2d

Browse files
committed
Stop using RAA-unblocking post event action chan funding outpoints
Historically we indexed channels by `(counterparty_node_id, funding outpoint)` in several pipelines, especially the `ChannelMonitorUpdate` pipeline. This ended up complexifying quite a few things as we always needed to store the full `(counterparty_node_id, funding outpoint, channel_id)` tuple to ensure we can always access a channel no matter its state. Over time we want to move to only the `(counterparty_node_id, channel_id)` tuple as *the* channel index, especially as we move towards V2 channels that have a globally-unique `channel_id` anyway. Here we take one small step towards this, avoiding using the channel funding outpoint in the `EventCompletionAction` pipeline.
1 parent bf73b7b commit 0ab2e2d

File tree

1 file changed

+55
-66
lines changed

1 file changed

+55
-66
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 55 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,6 @@ pub(crate) enum MonitorUpdateCompletionAction {
12021202
/// stored for later processing.
12031203
FreeOtherChannelImmediately {
12041204
downstream_counterparty_node_id: PublicKey,
1205-
downstream_funding_outpoint: OutPoint,
12061205
blocking_action: RAAMonitorUpdateBlockingAction,
12071206
downstream_channel_id: ChannelId,
12081207
},
@@ -1217,11 +1216,8 @@ impl_writeable_tlv_based_enum_upgradable!(MonitorUpdateCompletionAction,
12171216
// *immediately*. However, for simplicity we implement read/write here.
12181217
(1, FreeOtherChannelImmediately) => {
12191218
(0, downstream_counterparty_node_id, required),
1220-
(2, downstream_funding_outpoint, required),
12211219
(4, blocking_action, upgradable_required),
1222-
// Note that by the time we get past the required read above, downstream_funding_outpoint will be
1223-
// filled in, so we can safely unwrap it here.
1224-
(5, downstream_channel_id, (default_value, ChannelId::v1_from_funding_outpoint(downstream_funding_outpoint.0.unwrap()))),
1220+
(5, downstream_channel_id, required),
12251221
},
12261222
(2, EmitEventAndFreeOtherChannel) => {
12271223
(0, event, upgradable_required),
@@ -1238,17 +1234,21 @@ impl_writeable_tlv_based_enum_upgradable!(MonitorUpdateCompletionAction,
12381234
pub(crate) enum EventCompletionAction {
12391235
ReleaseRAAChannelMonitorUpdate {
12401236
counterparty_node_id: PublicKey,
1241-
channel_funding_outpoint: OutPoint,
1237+
// Was required until LDK 0.2. Always filled in as `Some`.
1238+
channel_funding_outpoint: Option<OutPoint>,
12421239
channel_id: ChannelId,
12431240
},
12441241
}
12451242
impl_writeable_tlv_based_enum!(EventCompletionAction,
12461243
(0, ReleaseRAAChannelMonitorUpdate) => {
1247-
(0, channel_funding_outpoint, required),
1244+
(0, channel_funding_outpoint, option),
12481245
(2, counterparty_node_id, required),
1249-
// Note that by the time we get past the required read above, channel_funding_outpoint will be
1250-
// filled in, so we can safely unwrap it here.
1251-
(3, channel_id, (default_value, ChannelId::v1_from_funding_outpoint(channel_funding_outpoint.0.unwrap()))),
1246+
(3, channel_id, (default_value, {
1247+
if channel_funding_outpoint.is_none() {
1248+
Err(DecodeError::InvalidValue)?
1249+
}
1250+
ChannelId::v1_from_funding_outpoint(channel_funding_outpoint.unwrap())
1251+
})),
12521252
}
12531253
);
12541254

@@ -1278,8 +1278,8 @@ impl From<&MPPClaimHTLCSource> for HTLCClaimSource {
12781278

12791279
#[derive(Debug)]
12801280
pub(crate) struct PendingMPPClaim {
1281-
channels_without_preimage: Vec<(PublicKey, OutPoint, ChannelId)>,
1282-
channels_with_preimage: Vec<(PublicKey, OutPoint, ChannelId)>,
1281+
channels_without_preimage: Vec<(PublicKey, ChannelId)>,
1282+
channels_with_preimage: Vec<(PublicKey, ChannelId)>,
12831283
}
12841284

12851285
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
@@ -8066,7 +8066,7 @@ where
80668066
let pending_mpp_claim_ptr_opt = if sources.len() > 1 {
80678067
let mut channels_without_preimage = Vec::with_capacity(mpp_parts.len());
80688068
for part in mpp_parts.iter() {
8069-
let chan = (part.counterparty_node_id, part.funding_txo, part.channel_id);
8069+
let chan = (part.counterparty_node_id, part.channel_id);
80708070
if !channels_without_preimage.contains(&chan) {
80718071
channels_without_preimage.push(chan);
80728072
}
@@ -8304,7 +8304,6 @@ where
83048304
chan_id, action);
83058305
if let MonitorUpdateCompletionAction::FreeOtherChannelImmediately {
83068306
downstream_counterparty_node_id: node_id,
8307-
downstream_funding_outpoint: _,
83088307
blocking_action: blocker,
83098308
downstream_channel_id: channel_id,
83108309
} = action
@@ -8477,7 +8476,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84778476
"We don't support claim_htlc claims during startup - monitors may not be available yet");
84788477
debug_assert_eq!(next_channel_counterparty_node_id, path.hops[0].pubkey);
84798478
let ev_completion_action = EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
8480-
channel_funding_outpoint: next_channel_outpoint,
8479+
channel_funding_outpoint: Some(next_channel_outpoint),
84818480
channel_id: next_channel_id,
84828481
counterparty_node_id: path.hops[0].pubkey,
84838482
};
@@ -8581,7 +8580,6 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
85818580
if let Some(other_chan) = chan_to_release {
85828581
(Some(MonitorUpdateCompletionAction::FreeOtherChannelImmediately {
85838582
downstream_counterparty_node_id: other_chan.counterparty_node_id,
8584-
downstream_funding_outpoint: other_chan.funding_txo,
85858583
downstream_channel_id: other_chan.channel_id,
85868584
blocking_action: other_chan.blocking_action,
85878585
}), None)
@@ -8655,17 +8653,17 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
86558653
if *pending_claim == claim_ptr {
86568654
let mut pending_claim_state_lock = pending_claim.0.lock().unwrap();
86578655
let pending_claim_state = &mut *pending_claim_state_lock;
8658-
pending_claim_state.channels_without_preimage.retain(|(cp, op, cid)| {
8656+
pending_claim_state.channels_without_preimage.retain(|(cp, cid)| {
86598657
let this_claim =
86608658
*cp == counterparty_node_id && *cid == chan_id;
86618659
if this_claim {
8662-
pending_claim_state.channels_with_preimage.push((*cp, *op, *cid));
8660+
pending_claim_state.channels_with_preimage.push((*cp, *cid));
86638661
false
86648662
} else { true }
86658663
});
86668664
if pending_claim_state.channels_without_preimage.is_empty() {
8667-
for (cp, op, cid) in pending_claim_state.channels_with_preimage.iter() {
8668-
let freed_chan = (*cp, *op, *cid, blocker.clone());
8665+
for (cp, cid) in pending_claim_state.channels_with_preimage.iter() {
8666+
let freed_chan = (*cp, *cid, blocker.clone());
86698667
freed_channels.push(freed_chan);
86708668
}
86718669
}
@@ -8717,26 +8715,26 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
87178715
self.pending_events.lock().unwrap().push_back((event, None));
87188716
if let Some(unblocked) = downstream_counterparty_and_funding_outpoint {
87198717
self.handle_monitor_update_release(
8720-
unblocked.counterparty_node_id, unblocked.funding_txo,
8721-
unblocked.channel_id, Some(unblocked.blocking_action),
8718+
unblocked.counterparty_node_id,
8719+
unblocked.channel_id,
8720+
Some(unblocked.blocking_action),
87228721
);
87238722
}
87248723
},
87258724
MonitorUpdateCompletionAction::FreeOtherChannelImmediately {
8726-
downstream_counterparty_node_id, downstream_funding_outpoint, downstream_channel_id, blocking_action,
8725+
downstream_counterparty_node_id, downstream_channel_id, blocking_action,
87278726
} => {
87288727
self.handle_monitor_update_release(
87298728
downstream_counterparty_node_id,
8730-
downstream_funding_outpoint,
87318729
downstream_channel_id,
87328730
Some(blocking_action),
87338731
);
87348732
},
87358733
}
87368734
}
87378735

8738-
for (node_id, funding_outpoint, channel_id, blocker) in freed_channels {
8739-
self.handle_monitor_update_release(node_id, funding_outpoint, channel_id, Some(blocker));
8736+
for (node_id, channel_id, blocker) in freed_channels {
8737+
self.handle_monitor_update_release(node_id, channel_id, Some(blocker));
87408738
}
87418739
}
87428740

@@ -10518,16 +10516,20 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1051810516
#[rustfmt::skip]
1051910517
fn raa_monitor_updates_held(&self,
1052010518
actions_blocking_raa_monitor_updates: &BTreeMap<ChannelId, Vec<RAAMonitorUpdateBlockingAction>>,
10521-
channel_funding_outpoint: OutPoint, channel_id: ChannelId, counterparty_node_id: PublicKey
10519+
channel_id: ChannelId, counterparty_node_id: PublicKey,
1052210520
) -> bool {
1052310521
actions_blocking_raa_monitor_updates
1052410522
.get(&channel_id).map(|v| !v.is_empty()).unwrap_or(false)
1052510523
|| self.pending_events.lock().unwrap().iter().any(|(_, action)| {
10526-
action == &Some(EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
10527-
channel_funding_outpoint,
10528-
channel_id,
10529-
counterparty_node_id,
10530-
})
10524+
if let Some(EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
10525+
channel_funding_outpoint: _,
10526+
channel_id: ev_channel_id,
10527+
counterparty_node_id: ev_counterparty_node_id
10528+
}) = action {
10529+
*ev_channel_id == channel_id && *ev_counterparty_node_id == counterparty_node_id
10530+
} else {
10531+
false
10532+
}
1053110533
})
1053210534
}
1053310535

@@ -10540,14 +10542,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1054010542
let mut peer_state_lck = peer_state_mtx.lock().unwrap();
1054110543
let peer_state = &mut *peer_state_lck;
1054210544

10543-
if let Some(chan) = peer_state.channel_by_id.get(&channel_id) {
10544-
return self.raa_monitor_updates_held(
10545-
&peer_state.actions_blocking_raa_monitor_updates,
10546-
chan.funding().get_funding_txo().unwrap(),
10547-
channel_id,
10548-
counterparty_node_id,
10549-
);
10550-
}
10545+
assert!(peer_state.channel_by_id.contains_key(&channel_id));
10546+
return self.raa_monitor_updates_held(
10547+
&peer_state.actions_blocking_raa_monitor_updates,
10548+
channel_id,
10549+
counterparty_node_id,
10550+
);
1055110551
}
1055210552
false
1055310553
}
@@ -10567,11 +10567,9 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1056710567
if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
1056810568
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
1056910569
let funding_txo_opt = chan.funding.get_funding_txo();
10570-
let mon_update_blocked = if let Some(funding_txo) = funding_txo_opt {
10571-
self.raa_monitor_updates_held(
10572-
&peer_state.actions_blocking_raa_monitor_updates, funding_txo, msg.channel_id,
10573-
*counterparty_node_id)
10574-
} else { false };
10570+
let mon_update_blocked = self.raa_monitor_updates_held(
10571+
&peer_state.actions_blocking_raa_monitor_updates, msg.channel_id,
10572+
*counterparty_node_id);
1057510573
let (htlcs_to_fail, monitor_update_opt) = try_channel_entry!(self, peer_state,
1057610574
chan.revoke_and_ack(&msg, &self.fee_estimator, &&logger, mon_update_blocked), chan_entry);
1057710575
if let Some(monitor_update) = monitor_update_opt {
@@ -12556,10 +12554,10 @@ where
1255612554
/// operation. It will double-check that nothing *else* is also blocking the same channel from
1255712555
/// making progress and then let any blocked [`ChannelMonitorUpdate`]s fly.
1255812556
#[rustfmt::skip]
12559-
fn handle_monitor_update_release(&self, counterparty_node_id: PublicKey,
12560-
channel_funding_outpoint: OutPoint, channel_id: ChannelId,
12561-
mut completed_blocker: Option<RAAMonitorUpdateBlockingAction>) {
12562-
12557+
fn handle_monitor_update_release(
12558+
&self, counterparty_node_id: PublicKey, channel_id: ChannelId,
12559+
mut completed_blocker: Option<RAAMonitorUpdateBlockingAction>,
12560+
) {
1256312561
let logger = WithContext::from(
1256412562
&self.logger, Some(counterparty_node_id), Some(channel_id), None
1256512563
);
@@ -12578,7 +12576,7 @@ where
1257812576
}
1257912577

1258012578
if self.raa_monitor_updates_held(&peer_state.actions_blocking_raa_monitor_updates,
12581-
channel_funding_outpoint, channel_id, counterparty_node_id) {
12579+
channel_id, counterparty_node_id) {
1258212580
// Check that, while holding the peer lock, we don't have anything else
1258312581
// blocking monitor updates for this channel. If we do, release the monitor
1258412582
// update(s) when those blockers complete.
@@ -12590,7 +12588,7 @@ where
1259012588
if let hash_map::Entry::Occupied(mut chan_entry) = peer_state.channel_by_id.entry(
1259112589
channel_id) {
1259212590
if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
12593-
debug_assert_eq!(chan.funding.get_funding_txo().unwrap(), channel_funding_outpoint);
12591+
let channel_funding_outpoint = chan.funding.get_funding_txo().unwrap();
1259412592
if let Some((monitor_update, further_update_exists)) = chan.unblock_next_blocked_monitor_update() {
1259512593
log_debug!(logger, "Unlocking monitor updating for channel {} and updating monitor",
1259612594
channel_id);
@@ -12620,16 +12618,11 @@ where
1262012618
for action in actions {
1262112619
match action {
1262212620
EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
12623-
channel_funding_outpoint,
12621+
channel_funding_outpoint: _,
1262412622
channel_id,
1262512623
counterparty_node_id,
1262612624
} => {
12627-
self.handle_monitor_update_release(
12628-
counterparty_node_id,
12629-
channel_funding_outpoint,
12630-
channel_id,
12631-
None,
12632-
);
12625+
self.handle_monitor_update_release(counterparty_node_id, channel_id, None);
1263312626
},
1263412627
}
1263512628
}
@@ -16403,7 +16396,9 @@ where
1640316396
// `ChannelMonitor` is removed.
1640416397
let compl_action =
1640516398
EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
16406-
channel_funding_outpoint: monitor.get_funding_txo(),
16399+
channel_funding_outpoint: Some(
16400+
monitor.get_funding_txo(),
16401+
),
1640716402
channel_id: monitor.channel_id(),
1640816403
counterparty_node_id: path.hops[0].pubkey,
1640916404
};
@@ -16906,13 +16901,7 @@ where
1690616901
let mut channels_without_preimage = payment_claim
1690716902
.mpp_parts
1690816903
.iter()
16909-
.map(|htlc_info| {
16910-
(
16911-
htlc_info.counterparty_node_id,
16912-
htlc_info.funding_txo,
16913-
htlc_info.channel_id,
16914-
)
16915-
})
16904+
.map(|htlc_info| (htlc_info.counterparty_node_id, htlc_info.channel_id))
1691616905
.collect::<Vec<_>>();
1691716906
// If we have multiple MPP parts which were received over the same channel,
1691816907
// we only track it once as once we get a preimage durably in the

0 commit comments

Comments
 (0)