Skip to content

Commit f624047

Browse files
committed
Marginally simplify types for req'd counterparty_node_id in claim
In 0.1 we started requiring `counterparty_node_id` to be filled in in various previous-hop datastructures when claiming HTLCs. While we can't switch `HTLCSource`'s `HTLCPreviousHopData::counterparty_node_id` to required (as it might cause us to fail to read old `ChannelMonitor`s which still hold `HTLCSource`s we no longer need to claim), we can at least start requiring the field in `PendingAddHTLCInfo` and `HTLCClaimSource`. This simplifies `claim_mpp_part` marginally.
1 parent 0a23664 commit f624047

File tree

1 file changed

+45
-55
lines changed

1 file changed

+45
-55
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ pub(super) struct PendingAddHTLCInfo {
450450
// Note that this may be an outbound SCID alias for the associated channel.
451451
prev_short_channel_id: u64,
452452
prev_htlc_id: u64,
453-
prev_counterparty_node_id: Option<PublicKey>,
453+
prev_counterparty_node_id: PublicKey,
454454
prev_channel_id: ChannelId,
455455
prev_funding_outpoint: OutPoint,
456456
prev_user_channel_id: u128,
@@ -670,7 +670,7 @@ impl_writeable_tlv_based_enum!(SentHTLCId,
670670

671671
// (src_channel_id, src_counterparty_node_id, src_funding_outpoint, src_chan_id, src_user_chan_id)
672672
type PerSourcePendingForward =
673-
(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>);
673+
(u64, PublicKey, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>);
674674

675675
type FailedHTLCForward = (HTLCSource, PaymentHash, HTLCFailReason, HTLCHandlingFailureType);
676676

@@ -1258,7 +1258,7 @@ impl_writeable_tlv_based_enum!(EventCompletionAction,
12581258
/// drop this and merge the two, however doing so may break upgrades for nodes which have pending
12591259
/// forwarded payments.
12601260
struct HTLCClaimSource {
1261-
counterparty_node_id: Option<PublicKey>,
1261+
counterparty_node_id: PublicKey,
12621262
funding_txo: OutPoint,
12631263
channel_id: ChannelId,
12641264
htlc_id: u64,
@@ -1267,7 +1267,7 @@ struct HTLCClaimSource {
12671267
impl From<&MPPClaimHTLCSource> for HTLCClaimSource {
12681268
fn from(o: &MPPClaimHTLCSource) -> HTLCClaimSource {
12691269
HTLCClaimSource {
1270-
counterparty_node_id: Some(o.counterparty_node_id),
1270+
counterparty_node_id: o.counterparty_node_id,
12711271
funding_txo: o.funding_txo,
12721272
channel_id: o.channel_id,
12731273
htlc_id: o.htlc_id,
@@ -6149,7 +6149,7 @@ where
61496149
user_channel_id: Some(payment.prev_user_channel_id),
61506150
outpoint: payment.prev_funding_outpoint,
61516151
channel_id: payment.prev_channel_id,
6152-
counterparty_node_id: payment.prev_counterparty_node_id,
6152+
counterparty_node_id: Some(payment.prev_counterparty_node_id),
61536153
htlc_id: payment.prev_htlc_id,
61546154
incoming_packet_shared_secret: payment.forward_info.incoming_shared_secret,
61556155
phantom_shared_secret: None,
@@ -6322,7 +6322,7 @@ where
63226322
// proposed to as a batch.
63236323
let pending_forwards = (
63246324
incoming_scid,
6325-
Some(incoming_counterparty_node_id),
6325+
incoming_counterparty_node_id,
63266326
incoming_funding_txo,
63276327
incoming_channel_id,
63286328
incoming_user_channel_id,
@@ -6511,7 +6511,7 @@ where
65116511
user_channel_id: Some(prev_user_channel_id),
65126512
channel_id: prev_channel_id,
65136513
outpoint: prev_funding_outpoint,
6514-
counterparty_node_id: prev_counterparty_node_id,
6514+
counterparty_node_id: Some(prev_counterparty_node_id),
65156515
htlc_id: prev_htlc_id,
65166516
incoming_packet_shared_secret: incoming_shared_secret,
65176517
phantom_shared_secret: phantom_ss,
@@ -6724,7 +6724,7 @@ where
67246724
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
67256725
short_channel_id: prev_short_channel_id,
67266726
user_channel_id: Some(prev_user_channel_id),
6727-
counterparty_node_id: prev_counterparty_node_id,
6727+
counterparty_node_id: Some(prev_counterparty_node_id),
67286728
channel_id: prev_channel_id,
67296729
outpoint: prev_funding_outpoint,
67306730
htlc_id: prev_htlc_id,
@@ -7035,7 +7035,7 @@ where
70357035
prev_hop: HTLCPreviousHopData {
70367036
short_channel_id: prev_short_channel_id,
70377037
user_channel_id: Some(prev_user_channel_id),
7038-
counterparty_node_id: prev_counterparty_node_id,
7038+
counterparty_node_id: Some(prev_counterparty_node_id),
70397039
channel_id: prev_channel_id,
70407040
outpoint: prev_funding_outpoint,
70417041
htlc_id: prev_htlc_id,
@@ -8080,13 +8080,12 @@ where
80808080
let payment_info = Some(PaymentClaimDetails { mpp_parts, claiming_payment });
80818081
for htlc in sources {
80828082
let this_mpp_claim =
8083-
pending_mpp_claim_ptr_opt.as_ref().and_then(|pending_mpp_claim| {
8084-
if let Some(cp_id) = htlc.prev_hop.counterparty_node_id {
8085-
let claim_ptr = PendingMPPClaimPointer(Arc::clone(pending_mpp_claim));
8086-
Some((cp_id, htlc.prev_hop.channel_id, claim_ptr))
8087-
} else {
8088-
None
8089-
}
8083+
pending_mpp_claim_ptr_opt.as_ref().map(|pending_mpp_claim| {
8084+
let counterparty_id = htlc.prev_hop.counterparty_node_id;
8085+
let counterparty_id = counterparty_id
8086+
.expect("Prior to upgrading to LDK 0.1, all pending HTLCs forwarded by LDK 0.0.123 or before must be resolved. It appears at least one claimable payment was not resolved. Please downgrade to LDK 0.0.125 and resolve the HTLC by claiming the payment prior to upgrading.");
8087+
let claim_ptr = PendingMPPClaimPointer(Arc::clone(pending_mpp_claim));
8088+
(counterparty_id, htlc.prev_hop.channel_id, claim_ptr)
80908089
});
80918090
let raa_blocker = pending_mpp_claim_ptr_opt.as_ref().map(|pending_claim| {
80928091
RAAMonitorUpdateBlockingAction::ClaimedMPPPayment {
@@ -8168,6 +8167,14 @@ where
81688167
let short_to_chan_info = self.short_to_chan_info.read().unwrap();
81698168
short_to_chan_info.get(&prev_hop.short_channel_id).map(|(cp_id, _)| *cp_id)
81708169
});
8170+
let counterparty_node_id = if let Some(node_id) = counterparty_node_id {
8171+
node_id
8172+
} else {
8173+
let payment_hash: PaymentHash = payment_preimage.into();
8174+
panic!(
8175+
"Prior to upgrading to LDK 0.1, all pending HTLCs forwarded by LDK 0.0.123 or before must be resolved. It appears at least the HTLC with payment_hash {payment_hash} (preimage {payment_preimage}) was not resolved. Please downgrade to LDK 0.0.125 and resolve the HTLC prior to upgrading.",
8176+
);
8177+
};
81718178

81728179
let htlc_source = HTLCClaimSource {
81738180
counterparty_node_id,
@@ -8212,18 +8219,13 @@ where
82128219
const MISSING_MON_ERROR: &'static str =
82138220
"If we're going to claim an HTLC against a channel, we should always have *some* state for the channel, even if just the latest ChannelMonitor update_id. This failure indicates we need to claim an HTLC from a channel for which we did not have a ChannelMonitor at startup and didn't create one while running.";
82148221

8215-
// Note here that `peer_state_opt` is always `Some` if `prev_hop.counterparty_node_id` is
8216-
// `Some`. This is relied on in the closed-channel case below.
8217-
let mut peer_state_opt =
8218-
prev_hop.counterparty_node_id.as_ref().map(|counterparty_node_id| {
8219-
per_peer_state
8220-
.get(counterparty_node_id)
8221-
.map(|peer_mutex| peer_mutex.lock().unwrap())
8222-
.expect(MISSING_MON_ERROR)
8223-
});
8222+
let mut peer_state_lock = per_peer_state
8223+
.get(&prev_hop.counterparty_node_id)
8224+
.map(|peer_mutex| peer_mutex.lock().unwrap())
8225+
.expect(MISSING_MON_ERROR);
82248226

8225-
if let Some(peer_state_lock) = peer_state_opt.as_mut() {
8226-
let peer_state = &mut **peer_state_lock;
8227+
{
8228+
let peer_state = &mut *peer_state_lock;
82278229
if let hash_map::Entry::Occupied(mut chan_entry) =
82288230
peer_state.channel_by_id.entry(chan_id)
82298231
{
@@ -8261,7 +8263,7 @@ where
82618263
self,
82628264
prev_hop.funding_txo,
82638265
monitor_update,
8264-
peer_state_opt,
8266+
peer_state_lock,
82658267
peer_state,
82668268
per_peer_state,
82678269
chan
@@ -8312,7 +8314,7 @@ where
83128314
return;
83138315
}
83148316

8315-
mem::drop(peer_state_opt);
8317+
mem::drop(peer_state_lock);
83168318

83178319
log_trace!(logger, "Completing monitor update completion action for channel {} as claim was redundant: {:?}",
83188320
chan_id, action);
@@ -8365,18 +8367,7 @@ where
83658367
}
83668368
}
83678369

8368-
if prev_hop.counterparty_node_id.is_none() {
8369-
let payment_hash: PaymentHash = payment_preimage.into();
8370-
panic!(
8371-
"Prior to upgrading to LDK 0.1, all pending HTLCs forwarded by LDK 0.0.123 or before must be resolved. It appears at least the HTLC with payment_hash {} (preimage {}) was not resolved. Please downgrade to LDK 0.0.125 and resolve the HTLC prior to upgrading.",
8372-
payment_hash,
8373-
payment_preimage,
8374-
);
8375-
}
8376-
let counterparty_node_id =
8377-
prev_hop.counterparty_node_id.expect("Checked immediately above");
8378-
let mut peer_state = peer_state_opt
8379-
.expect("peer_state_opt is always Some when the counterparty_node_id is Some");
8370+
let peer_state = &mut *peer_state_lock;
83808371

83818372
let update_id = if let Some(latest_update_id) =
83828373
peer_state.closed_channel_monitor_update_ids.get_mut(&chan_id)
@@ -8420,7 +8411,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84208411
let payment_hash = payment_preimage.into();
84218412
let logger = WithContext::from(
84228413
&self.logger,
8423-
Some(counterparty_node_id),
8414+
Some(prev_hop.counterparty_node_id),
84248415
Some(chan_id),
84258416
Some(payment_hash),
84268417
);
@@ -8443,10 +8434,10 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84438434
self,
84448435
prev_hop.funding_txo,
84458436
preimage_update,
8446-
peer_state,
8437+
peer_state_lock,
84478438
peer_state,
84488439
per_peer_state,
8449-
counterparty_node_id,
8440+
prev_hop.counterparty_node_id,
84508441
chan_id,
84518442
POST_CHANNEL_CLOSE
84528443
);
@@ -8775,7 +8766,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
87758766
funding_broadcastable: Option<Transaction>,
87768767
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>,
87778768
tx_signatures: Option<msgs::TxSignatures>, tx_abort: Option<msgs::TxAbort>,
8778-
) -> (Option<(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
8769+
) -> (Option<(u64, PublicKey, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
87798770
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
87808771
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement, {} tx_signatures, {} tx_abort",
87818772
&channel.context.channel_id(),
@@ -8795,7 +8786,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
87958786
let mut htlc_forwards = None;
87968787
if !pending_forwards.is_empty() {
87978788
htlc_forwards = Some((
8798-
short_channel_id, Some(channel.context.get_counterparty_node_id()),
8789+
short_channel_id, channel.context.get_counterparty_node_id(),
87998790
channel.funding.get_funding_txo().unwrap(), channel.context.channel_id(),
88008791
channel.context.get_user_id(), pending_forwards
88018792
));
@@ -10468,23 +10459,22 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1046810459
"Failed to forward incoming HTLC: detected duplicate intercepted payment over short channel id {}",
1046910460
scid
1047010461
);
10462+
let routing = &forward_info.routing;
1047110463
let htlc_source =
1047210464
HTLCSource::PreviousHopData(HTLCPreviousHopData {
1047310465
short_channel_id: prev_short_channel_id,
1047410466
user_channel_id: Some(prev_user_channel_id),
10475-
counterparty_node_id: prev_counterparty_node_id,
10467+
counterparty_node_id: Some(
10468+
prev_counterparty_node_id,
10469+
),
1047610470
outpoint: prev_funding_outpoint,
1047710471
channel_id: prev_channel_id,
1047810472
htlc_id: prev_htlc_id,
1047910473
incoming_packet_shared_secret: forward_info
1048010474
.incoming_shared_secret,
1048110475
phantom_shared_secret: None,
10482-
blinded_failure: forward_info
10483-
.routing
10484-
.blinded_failure(),
10485-
cltv_expiry: forward_info
10486-
.routing
10487-
.incoming_cltv_expiry(),
10476+
blinded_failure: routing.blinded_failure(),
10477+
cltv_expiry: routing.incoming_cltv_expiry(),
1048810478
});
1048910479

1049010480
let payment_hash = forward_info.payment_hash;
@@ -13470,7 +13460,7 @@ where
1347013460
htlc_id: htlc.prev_htlc_id,
1347113461
incoming_packet_shared_secret: htlc.forward_info.incoming_shared_secret,
1347213462
phantom_shared_secret: None,
13473-
counterparty_node_id: htlc.prev_counterparty_node_id,
13463+
counterparty_node_id: Some(htlc.prev_counterparty_node_id),
1347413464
outpoint: htlc.prev_funding_outpoint,
1347513465
channel_id: htlc.prev_channel_id,
1347613466
blinded_failure: htlc.forward_info.routing.blinded_failure(),
@@ -14981,7 +14971,7 @@ impl_writeable_tlv_based!(PendingAddHTLCInfo, {
1498114971
// Note that by the time we get past the required read for type 6 above, prev_funding_outpoint will be
1498214972
// filled in, so we can safely unwrap it here.
1498314973
(7, prev_channel_id, (default_value, ChannelId::v1_from_funding_outpoint(prev_funding_outpoint.0.unwrap()))),
14984-
(9, prev_counterparty_node_id, option),
14974+
(9, prev_counterparty_node_id, required),
1498514975
});
1498614976

1498714977
impl Writeable for HTLCForwardInfo {

0 commit comments

Comments
 (0)