Skip to content

Commit a6e4f17

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 a6e4f17

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
@@ -8312,7 +8316,7 @@ where
83128316
return;
83138317
}
83148318

8315-
mem::drop(peer_state_opt);
8319+
mem::drop(peer_state_lock);
83168320

83178321
log_trace!(logger, "Completing monitor update completion action for channel {} as claim was redundant: {:?}",
83188322
chan_id, action);
@@ -8365,18 +8369,7 @@ where
83658369
}
83668370
}
83678371

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");
8372+
let peer_state = &mut *peer_state_lock;
83808373

83818374
let update_id = if let Some(latest_update_id) =
83828375
peer_state.closed_channel_monitor_update_ids.get_mut(&chan_id)
@@ -8420,7 +8413,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84208413
let payment_hash = payment_preimage.into();
84218414
let logger = WithContext::from(
84228415
&self.logger,
8423-
Some(counterparty_node_id),
8416+
Some(prev_hop.counterparty_node_id),
84248417
Some(chan_id),
84258418
Some(payment_hash),
84268419
);
@@ -8443,10 +8436,10 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84438436
self,
84448437
prev_hop.funding_txo,
84458438
preimage_update,
8446-
peer_state,
8439+
peer_state_lock,
84478440
peer_state,
84488441
per_peer_state,
8449-
counterparty_node_id,
8442+
prev_hop.counterparty_node_id,
84508443
chan_id,
84518444
POST_CHANNEL_CLOSE
84528445
);
@@ -8775,7 +8768,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
87758768
funding_broadcastable: Option<Transaction>,
87768769
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>,
87778770
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>)>) {
8771+
) -> (Option<(u64, PublicKey, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
87798772
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
87808773
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",
87818774
&channel.context.channel_id(),
@@ -8795,7 +8788,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
87958788
let mut htlc_forwards = None;
87968789
if !pending_forwards.is_empty() {
87978790
htlc_forwards = Some((
8798-
short_channel_id, Some(channel.context.get_counterparty_node_id()),
8791+
short_channel_id, channel.context.get_counterparty_node_id(),
87998792
channel.funding.get_funding_txo().unwrap(), channel.context.channel_id(),
88008793
channel.context.get_user_id(), pending_forwards
88018794
));
@@ -10468,23 +10461,22 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1046810461
"Failed to forward incoming HTLC: detected duplicate intercepted payment over short channel id {}",
1046910462
scid
1047010463
);
10464+
let routing = &forward_info.routing;
1047110465
let htlc_source =
1047210466
HTLCSource::PreviousHopData(HTLCPreviousHopData {
1047310467
short_channel_id: prev_short_channel_id,
1047410468
user_channel_id: Some(prev_user_channel_id),
10475-
counterparty_node_id: prev_counterparty_node_id,
10469+
counterparty_node_id: Some(
10470+
prev_counterparty_node_id,
10471+
),
1047610472
outpoint: prev_funding_outpoint,
1047710473
channel_id: prev_channel_id,
1047810474
htlc_id: prev_htlc_id,
1047910475
incoming_packet_shared_secret: forward_info
1048010476
.incoming_shared_secret,
1048110477
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(),
10478+
blinded_failure: routing.blinded_failure(),
10479+
cltv_expiry: routing.incoming_cltv_expiry(),
1048810480
});
1048910481

1049010482
let payment_hash = forward_info.payment_hash;
@@ -13470,7 +13462,7 @@ where
1347013462
htlc_id: htlc.prev_htlc_id,
1347113463
incoming_packet_shared_secret: htlc.forward_info.incoming_shared_secret,
1347213464
phantom_shared_secret: None,
13473-
counterparty_node_id: htlc.prev_counterparty_node_id,
13465+
counterparty_node_id: Some(htlc.prev_counterparty_node_id),
1347413466
outpoint: htlc.prev_funding_outpoint,
1347513467
channel_id: htlc.prev_channel_id,
1347613468
blinded_failure: htlc.forward_info.routing.blinded_failure(),
@@ -14981,7 +14973,7 @@ impl_writeable_tlv_based!(PendingAddHTLCInfo, {
1498114973
// Note that by the time we get past the required read for type 6 above, prev_funding_outpoint will be
1498214974
// filled in, so we can safely unwrap it here.
1498314975
(7, prev_channel_id, (default_value, ChannelId::v1_from_funding_outpoint(prev_funding_outpoint.0.unwrap()))),
14984-
(9, prev_counterparty_node_id, option),
14976+
(9, prev_counterparty_node_id, required),
1498514977
});
1498614978

1498714979
impl Writeable for HTLCForwardInfo {

0 commit comments

Comments
 (0)