Skip to content

Commit 84c997d

Browse files
DRY HTLCPreviousHopData creation
In upcoming commits, we will be adding several more conversions from PendingAddHTLCInfo into HTLCPreviousHopData. This conversion gets repeated all over the ChannelManager already, so lay some groundwork by DRYing it up.
1 parent e3db976 commit 84c997d

File tree

1 file changed

+93
-134
lines changed

1 file changed

+93
-134
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 93 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,27 @@ pub(super) struct PendingAddHTLCInfo {
449449
prev_user_channel_id: u128,
450450
}
451451

452+
impl PendingAddHTLCInfo {
453+
fn htlc_previous_hop_data(&self) -> HTLCPreviousHopData {
454+
let phantom_shared_secret = match self.forward_info.routing {
455+
PendingHTLCRouting::Receive { phantom_shared_secret, .. } => phantom_shared_secret,
456+
_ => None,
457+
};
458+
HTLCPreviousHopData {
459+
short_channel_id: self.prev_short_channel_id,
460+
user_channel_id: Some(self.prev_user_channel_id),
461+
outpoint: self.prev_funding_outpoint,
462+
channel_id: self.prev_channel_id,
463+
counterparty_node_id: Some(self.prev_counterparty_node_id),
464+
htlc_id: self.prev_htlc_id,
465+
incoming_packet_shared_secret: self.forward_info.incoming_shared_secret,
466+
phantom_shared_secret,
467+
blinded_failure: self.forward_info.routing.blinded_failure(),
468+
cltv_expiry: self.forward_info.routing.incoming_cltv_expiry(),
469+
}
470+
}
471+
}
472+
452473
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
453474
pub(super) enum HTLCForwardInfo {
454475
AddHTLC(PendingAddHTLCInfo),
@@ -6294,20 +6315,8 @@ where
62946315
err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.0))
62956316
})?;
62966317

6297-
if let PendingHTLCRouting::Forward { short_channel_id, incoming_cltv_expiry, .. } = payment.forward_info.routing {
6298-
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
6299-
short_channel_id: payment.prev_short_channel_id,
6300-
user_channel_id: Some(payment.prev_user_channel_id),
6301-
outpoint: payment.prev_funding_outpoint,
6302-
channel_id: payment.prev_channel_id,
6303-
counterparty_node_id: Some(payment.prev_counterparty_node_id),
6304-
htlc_id: payment.prev_htlc_id,
6305-
incoming_packet_shared_secret: payment.forward_info.incoming_shared_secret,
6306-
phantom_shared_secret: None,
6307-
blinded_failure: payment.forward_info.routing.blinded_failure(),
6308-
cltv_expiry: incoming_cltv_expiry,
6309-
});
6310-
6318+
if let PendingHTLCRouting::Forward { short_channel_id, .. } = payment.forward_info.routing {
6319+
let htlc_source = HTLCSource::PreviousHopData(payment.htlc_previous_hop_data());
63116320
let reason = HTLCFailReason::from_failure_code(LocalHTLCFailureReason::UnknownNextPeer);
63126321
let destination = HTLCHandlingFailureType::InvalidForward { requested_forward_scid: short_channel_id };
63136322
self.fail_htlc_backwards_internal(&htlc_source, &payment.forward_info.payment_hash, &reason, destination);
@@ -6629,24 +6638,24 @@ where
66296638
) {
66306639
for forward_info in forward_infos {
66316640
match forward_info {
6632-
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
6633-
prev_short_channel_id,
6634-
prev_htlc_id,
6635-
prev_channel_id,
6636-
prev_funding_outpoint,
6637-
prev_user_channel_id,
6638-
prev_counterparty_node_id,
6639-
forward_info:
6640-
PendingHTLCInfo {
6641-
routing,
6642-
incoming_shared_secret,
6643-
payment_hash,
6644-
outgoing_amt_msat,
6645-
outgoing_cltv_value,
6646-
..
6647-
},
6648-
}) => {
6649-
let cltv_expiry = routing.incoming_cltv_expiry();
6641+
HTLCForwardInfo::AddHTLC(payment) => {
6642+
let PendingAddHTLCInfo {
6643+
prev_short_channel_id,
6644+
prev_htlc_id,
6645+
prev_channel_id,
6646+
prev_funding_outpoint,
6647+
prev_user_channel_id,
6648+
prev_counterparty_node_id,
6649+
forward_info:
6650+
PendingHTLCInfo {
6651+
ref routing,
6652+
incoming_shared_secret,
6653+
payment_hash,
6654+
outgoing_amt_msat,
6655+
outgoing_cltv_value,
6656+
..
6657+
},
6658+
} = payment;
66506659
let logger = WithContext::from(
66516660
&self.logger,
66526661
forwarding_counterparty,
@@ -6657,19 +6666,11 @@ where
66576666
|msg, reason, err_data, phantom_ss, next_hop_unknown| {
66586667
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", msg);
66596668

6660-
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
6661-
short_channel_id: prev_short_channel_id,
6662-
user_channel_id: Some(prev_user_channel_id),
6663-
channel_id: prev_channel_id,
6664-
outpoint: prev_funding_outpoint,
6665-
counterparty_node_id: Some(prev_counterparty_node_id),
6666-
htlc_id: prev_htlc_id,
6667-
incoming_packet_shared_secret: incoming_shared_secret,
6668-
phantom_shared_secret: phantom_ss,
6669-
blinded_failure: routing.blinded_failure(),
6670-
cltv_expiry,
6671-
});
6672-
6669+
let mut prev_hop = payment.htlc_previous_hop_data();
6670+
// Override the phantom shared secret because it wasn't set in the originating
6671+
// `PendingAddHTLCInfo` above, it was calculated below after detecting this as a
6672+
// phantom payment.
6673+
prev_hop.phantom_shared_secret = phantom_ss;
66736674
let failure_type = if next_hop_unknown {
66746675
HTLCHandlingFailureType::InvalidForward {
66756676
requested_forward_scid: short_chan_id,
@@ -6679,7 +6680,7 @@ where
66796680
};
66806681

66816682
failed_forwards.push((
6682-
htlc_source,
6683+
HTLCSource::PreviousHopData(prev_hop),
66836684
payment_hash,
66846685
HTLCFailReason::reason(reason, err_data),
66856686
failure_type,
@@ -6848,43 +6849,29 @@ where
68486849
let mut draining_pending_forwards = pending_forwards.drain(..);
68496850
while let Some(forward_info) = draining_pending_forwards.next() {
68506851
let queue_fail_htlc_res = match forward_info {
6851-
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
6852-
prev_short_channel_id,
6853-
prev_htlc_id,
6854-
prev_channel_id,
6855-
prev_funding_outpoint,
6856-
prev_user_channel_id,
6857-
prev_counterparty_node_id,
6858-
forward_info:
6859-
PendingHTLCInfo {
6860-
incoming_shared_secret,
6861-
payment_hash,
6862-
outgoing_amt_msat,
6863-
outgoing_cltv_value,
6864-
routing:
6865-
PendingHTLCRouting::Forward {
6866-
ref onion_packet,
6867-
blinded,
6868-
incoming_cltv_expiry,
6869-
..
6870-
},
6871-
skimmed_fee_msat,
6872-
..
6852+
HTLCForwardInfo::AddHTLC(ref payment) => {
6853+
let htlc_source = HTLCSource::PreviousHopData(payment.htlc_previous_hop_data());
6854+
let PendingAddHTLCInfo {
6855+
prev_short_channel_id,
6856+
forward_info:
6857+
PendingHTLCInfo {
6858+
payment_hash,
6859+
outgoing_amt_msat,
6860+
outgoing_cltv_value,
6861+
routing,
6862+
skimmed_fee_msat,
6863+
..
6864+
},
6865+
..
6866+
} = payment;
6867+
let (onion_packet, blinded) = match routing {
6868+
PendingHTLCRouting::Forward { ref onion_packet, blinded, .. } => {
6869+
(onion_packet, blinded)
68736870
},
6874-
}) => {
6875-
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
6876-
short_channel_id: prev_short_channel_id,
6877-
user_channel_id: Some(prev_user_channel_id),
6878-
counterparty_node_id: Some(prev_counterparty_node_id),
6879-
channel_id: prev_channel_id,
6880-
outpoint: prev_funding_outpoint,
6881-
htlc_id: prev_htlc_id,
6882-
incoming_packet_shared_secret: incoming_shared_secret,
6883-
// Phantom payments are only PendingHTLCRouting::Receive.
6884-
phantom_shared_secret: None,
6885-
blinded_failure: blinded.map(|b| b.failure),
6886-
cltv_expiry: incoming_cltv_expiry,
6887-
});
6871+
_ => {
6872+
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
6873+
},
6874+
};
68886875
let next_blinding_point = blinded.and_then(|b| {
68896876
b.next_blinding_override.or_else(|| {
68906877
let encrypted_tlvs_ss = self
@@ -6950,7 +6937,7 @@ where
69506937
let logger = WithChannelContext::from(
69516938
&self.logger,
69526939
&optimal_channel.context,
6953-
Some(payment_hash),
6940+
Some(*payment_hash),
69546941
);
69556942
let channel_description =
69566943
if optimal_channel.funding.get_short_channel_id() == Some(short_chan_id) {
@@ -6961,12 +6948,12 @@ where
69616948
log_trace!(logger, "Forwarding HTLC from SCID {} with payment_hash {} and next hop SCID {} over {} channel {} with corresponding peer {}",
69626949
prev_short_channel_id, &payment_hash, short_chan_id, channel_description, optimal_channel.context.channel_id(), &counterparty_node_id);
69636950
if let Err((reason, msg)) = optimal_channel.queue_add_htlc(
6964-
outgoing_amt_msat,
6965-
payment_hash,
6966-
outgoing_cltv_value,
6951+
*outgoing_amt_msat,
6952+
*payment_hash,
6953+
*outgoing_cltv_value,
69676954
htlc_source.clone(),
69686955
onion_packet.clone(),
6969-
skimmed_fee_msat,
6956+
*skimmed_fee_msat,
69706957
next_blinding_point,
69716958
&self.fee_estimator,
69726959
&&logger,
@@ -6991,7 +6978,7 @@ where
69916978
};
69926979
failed_forwards.push((
69936980
htlc_source,
6994-
payment_hash,
6981+
*payment_hash,
69956982
HTLCFailReason::reason(reason, data),
69966983
failure_type,
69976984
));
@@ -7008,9 +6995,6 @@ where
70086995
}
70096996
None
70106997
},
7011-
HTLCForwardInfo::AddHTLC { .. } => {
7012-
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
7013-
},
70146998
HTLCForwardInfo::FailHTLC { htlc_id, ref err_packet } => {
70156999
if let Some(chan) = peer_state
70167000
.channel_by_id
@@ -7094,24 +7078,22 @@ where
70947078
) {
70957079
'next_forwardable_htlc: for forward_info in pending_forwards.drain(..) {
70967080
match forward_info {
7097-
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
7098-
prev_short_channel_id,
7099-
prev_htlc_id,
7100-
prev_channel_id,
7101-
prev_funding_outpoint,
7102-
prev_user_channel_id,
7103-
prev_counterparty_node_id,
7104-
forward_info:
7105-
PendingHTLCInfo {
7106-
routing,
7107-
incoming_shared_secret,
7108-
payment_hash,
7109-
incoming_amt_msat,
7110-
outgoing_amt_msat,
7111-
skimmed_fee_msat,
7112-
..
7113-
},
7114-
}) => {
7081+
HTLCForwardInfo::AddHTLC(payment) => {
7082+
let prev_hop = payment.htlc_previous_hop_data();
7083+
let PendingAddHTLCInfo {
7084+
prev_channel_id,
7085+
prev_funding_outpoint,
7086+
forward_info:
7087+
PendingHTLCInfo {
7088+
routing,
7089+
payment_hash,
7090+
incoming_amt_msat,
7091+
outgoing_amt_msat,
7092+
skimmed_fee_msat,
7093+
..
7094+
},
7095+
..
7096+
} = payment;
71157097
let blinded_failure = routing.blinded_failure();
71167098
let (
71177099
cltv_expiry,
@@ -7183,18 +7165,7 @@ where
71837165
},
71847166
};
71857167
let claimable_htlc = ClaimableHTLC {
7186-
prev_hop: HTLCPreviousHopData {
7187-
short_channel_id: prev_short_channel_id,
7188-
user_channel_id: Some(prev_user_channel_id),
7189-
counterparty_node_id: Some(prev_counterparty_node_id),
7190-
channel_id: prev_channel_id,
7191-
outpoint: prev_funding_outpoint,
7192-
htlc_id: prev_htlc_id,
7193-
incoming_packet_shared_secret: incoming_shared_secret,
7194-
phantom_shared_secret,
7195-
blinded_failure,
7196-
cltv_expiry: Some(cltv_expiry),
7197-
},
7168+
prev_hop,
71987169
// We differentiate the received value from the sender intended value
71997170
// if possible so that we don't prematurely mark MPP payments complete
72007171
// if routing nodes overpay
@@ -13701,19 +13672,7 @@ where
1370113672
let mut intercepted_htlcs = self.pending_intercepted_htlcs.lock().unwrap();
1370213673
intercepted_htlcs.retain(|_, htlc| {
1370313674
if height >= htlc.forward_info.outgoing_cltv_value - HTLC_FAIL_BACK_BUFFER {
13704-
let prev_hop_data = HTLCSource::PreviousHopData(HTLCPreviousHopData {
13705-
short_channel_id: htlc.prev_short_channel_id,
13706-
user_channel_id: Some(htlc.prev_user_channel_id),
13707-
htlc_id: htlc.prev_htlc_id,
13708-
incoming_packet_shared_secret: htlc.forward_info.incoming_shared_secret,
13709-
phantom_shared_secret: None,
13710-
counterparty_node_id: Some(htlc.prev_counterparty_node_id),
13711-
outpoint: htlc.prev_funding_outpoint,
13712-
channel_id: htlc.prev_channel_id,
13713-
blinded_failure: htlc.forward_info.routing.blinded_failure(),
13714-
cltv_expiry: htlc.forward_info.routing.incoming_cltv_expiry(),
13715-
});
13716-
13675+
let prev_hop_data = HTLCSource::PreviousHopData(htlc.htlc_previous_hop_data());
1371713676
let requested_forward_scid /* intercept scid */ = match htlc.forward_info.routing {
1371813677
PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
1371913678
_ => unreachable!(),

0 commit comments

Comments
 (0)