Skip to content

Commit 5801a9d

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 e01663a commit 5801a9d

File tree

1 file changed

+45
-53
lines changed

1 file changed

+45
-53
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 45 additions & 53 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,
@@ -8214,16 +8221,13 @@ where
82148221

82158222
// Note here that `peer_state_opt` is always `Some` if `prev_hop.counterparty_node_id` is
82168223
// `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-
});
8224+
let mut peer_state_lock = per_peer_state
8225+
.get(&prev_hop.counterparty_node_id)
8226+
.map(|peer_mutex| peer_mutex.lock().unwrap())
8227+
.expect(MISSING_MON_ERROR);
82248228

8225-
if let Some(peer_state_lock) = peer_state_opt.as_mut() {
8226-
let peer_state = &mut **peer_state_lock;
8229+
{
8230+
let peer_state = &mut *peer_state_lock;
82278231
if let hash_map::Entry::Occupied(mut chan_entry) =
82288232
peer_state.channel_by_id.entry(chan_id)
82298233
{
@@ -8261,7 +8265,7 @@ where
82618265
self,
82628266
prev_hop.funding_txo,
82638267
monitor_update,
8264-
peer_state_opt,
8268+
peer_state_lock,
82658269
peer_state,
82668270
per_peer_state,
82678271
chan
@@ -8293,7 +8297,7 @@ where
82938297
return;
82948298
};
82958299

8296-
mem::drop(peer_state_opt);
8300+
mem::drop(peer_state_lock);
82978301

82988302
log_trace!(logger, "Completing monitor update completion action for channel {} as claim was redundant: {:?}",
82998303
chan_id, action);
@@ -8346,18 +8350,7 @@ where
83468350
}
83478351
}
83488352

8349-
if prev_hop.counterparty_node_id.is_none() {
8350-
let payment_hash: PaymentHash = payment_preimage.into();
8351-
panic!(
8352-
"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.",
8353-
payment_hash,
8354-
payment_preimage,
8355-
);
8356-
}
8357-
let counterparty_node_id =
8358-
prev_hop.counterparty_node_id.expect("Checked immediately above");
8359-
let mut peer_state = peer_state_opt
8360-
.expect("peer_state_opt is always Some when the counterparty_node_id is Some");
8353+
let peer_state = &mut *peer_state_lock;
83618354

83628355
let update_id = if let Some(latest_update_id) =
83638356
peer_state.closed_channel_monitor_update_ids.get_mut(&chan_id)
@@ -8401,7 +8394,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84018394
let payment_hash = payment_preimage.into();
84028395
let logger = WithContext::from(
84038396
&self.logger,
8404-
Some(counterparty_node_id),
8397+
Some(prev_hop.counterparty_node_id),
84058398
Some(chan_id),
84068399
Some(payment_hash),
84078400
);
@@ -8424,10 +8417,10 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84248417
self,
84258418
prev_hop.funding_txo,
84268419
preimage_update,
8427-
peer_state,
8420+
peer_state_lock,
84288421
peer_state,
84298422
per_peer_state,
8430-
counterparty_node_id,
8423+
prev_hop.counterparty_node_id,
84318424
chan_id,
84328425
POST_CHANNEL_CLOSE
84338426
);
@@ -8756,7 +8749,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
87568749
funding_broadcastable: Option<Transaction>,
87578750
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>,
87588751
tx_signatures: Option<msgs::TxSignatures>, tx_abort: Option<msgs::TxAbort>,
8759-
) -> (Option<(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
8752+
) -> (Option<(u64, PublicKey, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
87608753
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
87618754
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",
87628755
&channel.context.channel_id(),
@@ -8776,7 +8769,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
87768769
let mut htlc_forwards = None;
87778770
if !pending_forwards.is_empty() {
87788771
htlc_forwards = Some((
8779-
short_channel_id, Some(channel.context.get_counterparty_node_id()),
8772+
short_channel_id, channel.context.get_counterparty_node_id(),
87808773
channel.funding.get_funding_txo().unwrap(), channel.context.channel_id(),
87818774
channel.context.get_user_id(), pending_forwards
87828775
));
@@ -10449,23 +10442,22 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1044910442
"Failed to forward incoming HTLC: detected duplicate intercepted payment over short channel id {}",
1045010443
scid
1045110444
);
10445+
let routing = &forward_info.routing;
1045210446
let htlc_source =
1045310447
HTLCSource::PreviousHopData(HTLCPreviousHopData {
1045410448
short_channel_id: prev_short_channel_id,
1045510449
user_channel_id: Some(prev_user_channel_id),
10456-
counterparty_node_id: prev_counterparty_node_id,
10450+
counterparty_node_id: Some(
10451+
prev_counterparty_node_id,
10452+
),
1045710453
outpoint: prev_funding_outpoint,
1045810454
channel_id: prev_channel_id,
1045910455
htlc_id: prev_htlc_id,
1046010456
incoming_packet_shared_secret: forward_info
1046110457
.incoming_shared_secret,
1046210458
phantom_shared_secret: None,
10463-
blinded_failure: forward_info
10464-
.routing
10465-
.blinded_failure(),
10466-
cltv_expiry: forward_info
10467-
.routing
10468-
.incoming_cltv_expiry(),
10459+
blinded_failure: routing.blinded_failure(),
10460+
cltv_expiry: routing.incoming_cltv_expiry(),
1046910461
});
1047010462

1047110463
let payment_hash = forward_info.payment_hash;
@@ -13451,7 +13443,7 @@ where
1345113443
htlc_id: htlc.prev_htlc_id,
1345213444
incoming_packet_shared_secret: htlc.forward_info.incoming_shared_secret,
1345313445
phantom_shared_secret: None,
13454-
counterparty_node_id: htlc.prev_counterparty_node_id,
13446+
counterparty_node_id: Some(htlc.prev_counterparty_node_id),
1345513447
outpoint: htlc.prev_funding_outpoint,
1345613448
channel_id: htlc.prev_channel_id,
1345713449
blinded_failure: htlc.forward_info.routing.blinded_failure(),
@@ -14962,7 +14954,7 @@ impl_writeable_tlv_based!(PendingAddHTLCInfo, {
1496214954
// Note that by the time we get past the required read for type 6 above, prev_funding_outpoint will be
1496314955
// filled in, so we can safely unwrap it here.
1496414956
(7, prev_channel_id, (default_value, ChannelId::v1_from_funding_outpoint(prev_funding_outpoint.0.unwrap()))),
14965-
(9, prev_counterparty_node_id, option),
14957+
(9, prev_counterparty_node_id, required),
1496614958
});
1496714959

1496814960
impl Writeable for HTLCForwardInfo {

0 commit comments

Comments
 (0)