Skip to content

Commit d3b636c

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 d3b636c

File tree

1 file changed

+87
-135
lines changed

1 file changed

+87
-135
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 87 additions & 135 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,8 @@ 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+
prev_hop.phantom_shared_secret = phantom_ss;
66736671
let failure_type = if next_hop_unknown {
66746672
HTLCHandlingFailureType::InvalidForward {
66756673
requested_forward_scid: short_chan_id,
@@ -6679,7 +6677,7 @@ where
66796677
};
66806678

66816679
failed_forwards.push((
6682-
htlc_source,
6680+
HTLCSource::PreviousHopData(prev_hop),
66836681
payment_hash,
66846682
HTLCFailReason::reason(reason, err_data),
66856683
failure_type,
@@ -6848,43 +6846,25 @@ where
68486846
let mut draining_pending_forwards = pending_forwards.drain(..);
68496847
while let Some(forward_info) = draining_pending_forwards.next() {
68506848
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-
..
6873-
},
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-
});
6849+
HTLCForwardInfo::AddHTLC(ref payment) => {
6850+
let htlc_source = HTLCSource::PreviousHopData(payment.htlc_previous_hop_data());
6851+
let PendingAddHTLCInfo {
6852+
prev_short_channel_id,
6853+
forward_info:
6854+
PendingHTLCInfo {
6855+
payment_hash,
6856+
outgoing_amt_msat,
6857+
outgoing_cltv_value,
6858+
routing:
6859+
PendingHTLCRouting::Forward { ref onion_packet, blinded, .. },
6860+
skimmed_fee_msat,
6861+
..
6862+
},
6863+
..
6864+
} = payment
6865+
else {
6866+
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
6867+
};
68886868
let next_blinding_point = blinded.and_then(|b| {
68896869
b.next_blinding_override.or_else(|| {
68906870
let encrypted_tlvs_ss = self
@@ -6950,7 +6930,7 @@ where
69506930
let logger = WithChannelContext::from(
69516931
&self.logger,
69526932
&optimal_channel.context,
6953-
Some(payment_hash),
6933+
Some(*payment_hash),
69546934
);
69556935
let channel_description =
69566936
if optimal_channel.funding.get_short_channel_id() == Some(short_chan_id) {
@@ -6961,12 +6941,12 @@ where
69616941
log_trace!(logger, "Forwarding HTLC from SCID {} with payment_hash {} and next hop SCID {} over {} channel {} with corresponding peer {}",
69626942
prev_short_channel_id, &payment_hash, short_chan_id, channel_description, optimal_channel.context.channel_id(), &counterparty_node_id);
69636943
if let Err((reason, msg)) = optimal_channel.queue_add_htlc(
6964-
outgoing_amt_msat,
6965-
payment_hash,
6966-
outgoing_cltv_value,
6944+
*outgoing_amt_msat,
6945+
*payment_hash,
6946+
*outgoing_cltv_value,
69676947
htlc_source.clone(),
69686948
onion_packet.clone(),
6969-
skimmed_fee_msat,
6949+
*skimmed_fee_msat,
69706950
next_blinding_point,
69716951
&self.fee_estimator,
69726952
&&logger,
@@ -6991,7 +6971,7 @@ where
69916971
};
69926972
failed_forwards.push((
69936973
htlc_source,
6994-
payment_hash,
6974+
*payment_hash,
69956975
HTLCFailReason::reason(reason, data),
69966976
failure_type,
69976977
));
@@ -7008,9 +6988,6 @@ where
70086988
}
70096989
None
70106990
},
7011-
HTLCForwardInfo::AddHTLC { .. } => {
7012-
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
7013-
},
70146991
HTLCForwardInfo::FailHTLC { htlc_id, ref err_packet } => {
70156992
if let Some(chan) = peer_state
70166993
.channel_by_id
@@ -7094,24 +7071,22 @@ where
70947071
) {
70957072
'next_forwardable_htlc: for forward_info in pending_forwards.drain(..) {
70967073
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-
}) => {
7074+
HTLCForwardInfo::AddHTLC(payment) => {
7075+
let prev_hop = payment.htlc_previous_hop_data();
7076+
let PendingAddHTLCInfo {
7077+
prev_channel_id,
7078+
prev_funding_outpoint,
7079+
forward_info:
7080+
PendingHTLCInfo {
7081+
routing,
7082+
payment_hash,
7083+
incoming_amt_msat,
7084+
outgoing_amt_msat,
7085+
skimmed_fee_msat,
7086+
..
7087+
},
7088+
..
7089+
} = payment;
71157090
let blinded_failure = routing.blinded_failure();
71167091
let (
71177092
cltv_expiry,
@@ -7183,18 +7158,7 @@ where
71837158
},
71847159
};
71857160
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-
},
7161+
prev_hop,
71987162
// We differentiate the received value from the sender intended value
71997163
// if possible so that we don't prematurely mark MPP payments complete
72007164
// if routing nodes overpay
@@ -13701,19 +13665,7 @@ where
1370113665
let mut intercepted_htlcs = self.pending_intercepted_htlcs.lock().unwrap();
1370213666
intercepted_htlcs.retain(|_, htlc| {
1370313667
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-
13668+
let prev_hop_data = HTLCSource::PreviousHopData(htlc.htlc_previous_hop_data());
1371713669
let requested_forward_scid /* intercept scid */ = match htlc.forward_info.routing {
1371813670
PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
1371913671
_ => unreachable!(),

0 commit comments

Comments
 (0)