Skip to content

Commit bb53e9d

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 bb53e9d

File tree

1 file changed

+90
-134
lines changed

1 file changed

+90
-134
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 90 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,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,29 @@ 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-
..
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+
skimmed_fee_msat,
6860+
..
6861+
},
6862+
..
6863+
} = payment;
6864+
let (onion_packet, blinded) = match routing {
6865+
PendingHTLCRouting::Forward { ref onion_packet, blinded, .. } => {
6866+
(onion_packet, blinded)
68736867
},
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-
});
6868+
_ => {
6869+
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
6870+
},
6871+
};
68886872
let next_blinding_point = blinded.and_then(|b| {
68896873
b.next_blinding_override.or_else(|| {
68906874
let encrypted_tlvs_ss = self
@@ -6950,7 +6934,7 @@ where
69506934
let logger = WithChannelContext::from(
69516935
&self.logger,
69526936
&optimal_channel.context,
6953-
Some(payment_hash),
6937+
Some(*payment_hash),
69546938
);
69556939
let channel_description =
69566940
if optimal_channel.funding.get_short_channel_id() == Some(short_chan_id) {
@@ -6961,12 +6945,12 @@ where
69616945
log_trace!(logger, "Forwarding HTLC from SCID {} with payment_hash {} and next hop SCID {} over {} channel {} with corresponding peer {}",
69626946
prev_short_channel_id, &payment_hash, short_chan_id, channel_description, optimal_channel.context.channel_id(), &counterparty_node_id);
69636947
if let Err((reason, msg)) = optimal_channel.queue_add_htlc(
6964-
outgoing_amt_msat,
6965-
payment_hash,
6966-
outgoing_cltv_value,
6948+
*outgoing_amt_msat,
6949+
*payment_hash,
6950+
*outgoing_cltv_value,
69676951
htlc_source.clone(),
69686952
onion_packet.clone(),
6969-
skimmed_fee_msat,
6953+
*skimmed_fee_msat,
69706954
next_blinding_point,
69716955
&self.fee_estimator,
69726956
&&logger,
@@ -6991,7 +6975,7 @@ where
69916975
};
69926976
failed_forwards.push((
69936977
htlc_source,
6994-
payment_hash,
6978+
*payment_hash,
69956979
HTLCFailReason::reason(reason, data),
69966980
failure_type,
69976981
));
@@ -7008,9 +6992,6 @@ where
70086992
}
70096993
None
70106994
},
7011-
HTLCForwardInfo::AddHTLC { .. } => {
7012-
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
7013-
},
70146995
HTLCForwardInfo::FailHTLC { htlc_id, ref err_packet } => {
70156996
if let Some(chan) = peer_state
70166997
.channel_by_id
@@ -7094,24 +7075,22 @@ where
70947075
) {
70957076
'next_forwardable_htlc: for forward_info in pending_forwards.drain(..) {
70967077
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-
}) => {
7078+
HTLCForwardInfo::AddHTLC(payment) => {
7079+
let prev_hop = payment.htlc_previous_hop_data();
7080+
let PendingAddHTLCInfo {
7081+
prev_channel_id,
7082+
prev_funding_outpoint,
7083+
forward_info:
7084+
PendingHTLCInfo {
7085+
routing,
7086+
payment_hash,
7087+
incoming_amt_msat,
7088+
outgoing_amt_msat,
7089+
skimmed_fee_msat,
7090+
..
7091+
},
7092+
..
7093+
} = payment;
71157094
let blinded_failure = routing.blinded_failure();
71167095
let (
71177096
cltv_expiry,
@@ -7183,18 +7162,7 @@ where
71837162
},
71847163
};
71857164
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-
},
7165+
prev_hop,
71987166
// We differentiate the received value from the sender intended value
71997167
// if possible so that we don't prematurely mark MPP payments complete
72007168
// if routing nodes overpay
@@ -13701,19 +13669,7 @@ where
1370113669
let mut intercepted_htlcs = self.pending_intercepted_htlcs.lock().unwrap();
1370213670
intercepted_htlcs.retain(|_, htlc| {
1370313671
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-
13672+
let prev_hop_data = HTLCSource::PreviousHopData(htlc.htlc_previous_hop_data());
1371713673
let requested_forward_scid /* intercept scid */ = match htlc.forward_info.routing {
1371813674
PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
1371913675
_ => unreachable!(),

0 commit comments

Comments
 (0)