Skip to content

Commit 9e091c2

Browse files
committed
Introduce ForwardInfo for NextPacketDetails
1 parent 78f5d3f commit 9e091c2

File tree

3 files changed

+67
-32
lines changed

3 files changed

+67
-32
lines changed

lightning/src/ln/blinded_payment_tests.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,8 +1665,9 @@ fn route_blinding_spec_test_vector() {
16651665
hop_data: carol_packet_bytes,
16661666
hmac: carol_hmac,
16671667
};
1668+
let carol_forward_info = carol_packet_details.forward_info.unwrap();
16681669
let carol_update_add = update_add_msg(
1669-
carol_packet_details.outgoing_amt_msat, carol_packet_details.outgoing_cltv_value,
1670+
carol_forward_info.outgoing_amt_msat, carol_forward_info.outgoing_cltv_value,
16701671
Some(pubkey_from_hex("034e09f450a80c3d252b258aba0a61215bf60dda3b0dc78ffb0736ea1259dfd8a0")),
16711672
carol_onion
16721673
);
@@ -1699,8 +1700,9 @@ fn route_blinding_spec_test_vector() {
16991700
hop_data: dave_packet_bytes,
17001701
hmac: dave_hmac,
17011702
};
1703+
let dave_forward_info = dave_packet_details.forward_info.unwrap();
17021704
let dave_update_add = update_add_msg(
1703-
dave_packet_details.outgoing_amt_msat, dave_packet_details.outgoing_cltv_value,
1705+
dave_forward_info.outgoing_amt_msat, dave_forward_info.outgoing_cltv_value,
17041706
Some(pubkey_from_hex("031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f")),
17051707
dave_onion
17061708
);
@@ -1733,8 +1735,9 @@ fn route_blinding_spec_test_vector() {
17331735
hop_data: eve_packet_bytes,
17341736
hmac: eve_hmac,
17351737
};
1738+
let eve_forward_info = eve_packet_details.forward_info.unwrap();
17361739
let eve_update_add = update_add_msg(
1737-
eve_packet_details.outgoing_amt_msat, eve_packet_details.outgoing_cltv_value,
1740+
eve_forward_info.outgoing_amt_msat, eve_forward_info.outgoing_cltv_value,
17381741
Some(pubkey_from_hex("03e09038ee76e50f444b19abf0a555e8697e035f62937168b80adf0931b31ce52a")),
17391742
eve_onion
17401743
);
@@ -1965,7 +1968,8 @@ fn test_trampoline_inbound_payment_decoding() {
19651968
hop_data: carol_packet_bytes,
19661969
hmac: carol_hmac,
19671970
};
1968-
let carol_update_add = update_add_msg(carol_packet_details.outgoing_amt_msat, carol_packet_details.outgoing_cltv_value, None, carol_onion);
1971+
let carol_forward_info = carol_packet_details.forward_info.unwrap();
1972+
let carol_update_add = update_add_msg(carol_forward_info.outgoing_amt_msat, carol_forward_info.outgoing_cltv_value, None, carol_onion);
19691973

19701974
let carol_node_signer = TestEcdhSigner { node_secret: carol_secret };
19711975
let (carol_peeled_onion, _) = onion_payment::decode_incoming_update_add_htlc_onion(

lightning/src/ln/channelmanager.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ use crate::ln::msgs::{
7272
};
7373
use crate::ln::onion_payment::{
7474
check_incoming_htlc_cltv, create_fwd_pending_htlc_info, create_recv_pending_htlc_info,
75-
decode_incoming_update_add_htlc_onion, invalid_payment_err_data, HopConnector, InboundHTLCErr,
76-
NextPacketDetails,
75+
decode_incoming_update_add_htlc_onion, invalid_payment_err_data, ForwardInfo, HopConnector,
76+
InboundHTLCErr, NextPacketDetails,
7777
};
7878
use crate::ln::onion_utils::{self};
7979
use crate::ln::onion_utils::{
@@ -4685,7 +4685,7 @@ where
46854685

46864686
#[rustfmt::skip]
46874687
fn can_forward_htlc_to_outgoing_channel(
4688-
&self, chan: &mut FundedChannel<SP>, msg: &msgs::UpdateAddHTLC, next_packet: &NextPacketDetails
4688+
&self, chan: &mut FundedChannel<SP>, msg: &msgs::UpdateAddHTLC, forward_info: &ForwardInfo
46894689
) -> Result<(), LocalHTLCFailureReason> {
46904690
if !chan.context.should_announce()
46914691
&& !self.config.read().unwrap().accept_forwards_to_priv_channels
@@ -4695,7 +4695,8 @@ where
46954695
// we don't allow forwards outbound over them.
46964696
return Err(LocalHTLCFailureReason::PrivateChannelForward);
46974697
}
4698-
if let HopConnector::ShortChannelId(outgoing_scid) = next_packet.outgoing_connector {
4698+
4699+
if let HopConnector::ShortChannelId(outgoing_scid) = forward_info.outgoing_connector {
46994700
if chan.funding.get_channel_type().supports_scid_privacy() && outgoing_scid != chan.context.outbound_scid_alias() {
47004701
// `option_scid_alias` (referred to in LDK as `scid_privacy`) means
47014702
// "refuse to forward unless the SCID alias was used", so we pretend
@@ -4718,10 +4719,10 @@ where
47184719
return Err(LocalHTLCFailureReason::ChannelNotReady);
47194720
}
47204721
}
4721-
if next_packet.outgoing_amt_msat < chan.context.get_counterparty_htlc_minimum_msat() {
4722+
if forward_info.outgoing_amt_msat < chan.context.get_counterparty_htlc_minimum_msat() {
47224723
return Err(LocalHTLCFailureReason::AmountBelowMinimum);
47234724
}
4724-
chan.htlc_satisfies_config(msg, next_packet.outgoing_amt_msat, next_packet.outgoing_cltv_value)?;
4725+
chan.htlc_satisfies_config(msg, forward_info.outgoing_amt_msat, forward_info.outgoing_cltv_value)?;
47254726

47264727
Ok(())
47274728
}
@@ -4753,14 +4754,20 @@ where
47534754
fn can_forward_htlc(
47544755
&self, msg: &msgs::UpdateAddHTLC, next_packet_details: &NextPacketDetails
47554756
) -> Result<(), LocalHTLCFailureReason> {
4756-
let outgoing_scid = match next_packet_details.outgoing_connector {
4757+
let forward_info = next_packet_details
4758+
.forward_info
4759+
.as_ref()
4760+
.ok_or(LocalHTLCFailureReason::InvalidOnionPayload)?;
4761+
4762+
let outgoing_scid = match forward_info.outgoing_connector {
47574763
HopConnector::ShortChannelId(scid) => scid,
47584764
HopConnector::Trampoline(_) => {
47594765
return Err(LocalHTLCFailureReason::InvalidTrampolineForward);
47604766
}
47614767
};
4768+
47624769
match self.do_funded_channel_callback(outgoing_scid, |chan: &mut FundedChannel<SP>| {
4763-
self.can_forward_htlc_to_outgoing_channel(chan, msg, next_packet_details)
4770+
self.can_forward_htlc_to_outgoing_channel(chan, msg, forward_info)
47644771
}) {
47654772
Some(Ok(())) => {},
47664773
Some(Err(e)) => return Err(e),
@@ -4777,7 +4784,7 @@ where
47774784
}
47784785

47794786
let cur_height = self.best_block.read().unwrap().height + 1;
4780-
check_incoming_htlc_cltv(cur_height, next_packet_details.outgoing_cltv_value, msg.cltv_expiry)?;
4787+
check_incoming_htlc_cltv(cur_height, forward_info.outgoing_cltv_value, msg.cltv_expiry)?;
47814788

47824789
Ok(())
47834790
}
@@ -6616,11 +6623,12 @@ where
66166623
};
66176624

66186625
let is_intro_node_blinded_forward = next_hop.is_intro_node_blinded_forward();
6619-
let outgoing_scid_opt =
6620-
next_packet_details_opt.as_ref().and_then(|d| match d.outgoing_connector {
6626+
let outgoing_scid_opt = next_packet_details_opt.as_ref().and_then(|d| {
6627+
d.forward_info.as_ref().and_then(|f| match f.outgoing_connector {
66216628
HopConnector::ShortChannelId(scid) => Some(scid),
66226629
HopConnector::Trampoline(_) => None,
6623-
});
6630+
})
6631+
});
66246632
let shared_secret = next_hop.shared_secret().secret_bytes();
66256633

66266634
// Nodes shouldn't expect us to hold HTLCs for them if we don't advertise htlc_hold feature

lightning/src/ln/onion_payment.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -485,16 +485,19 @@ where
485485
Ok(match hop {
486486
onion_utils::Hop::Forward { shared_secret, .. } |
487487
onion_utils::Hop::BlindedForward { shared_secret, .. } => {
488-
let NextPacketDetails {
489-
next_packet_pubkey, outgoing_amt_msat: _, outgoing_connector: _, outgoing_cltv_value
490-
} = match next_packet_details_opt {
491-
Some(next_packet_details) => next_packet_details,
488+
let (next_packet_pubkey, outgoing_cltv_value) = match next_packet_details_opt {
489+
Some(NextPacketDetails {
490+
next_packet_pubkey,
491+
forward_info: Some(ForwardInfo { outgoing_cltv_value, .. }),
492+
}) => (next_packet_pubkey, outgoing_cltv_value),
492493
// Forward should always include the next hop details
493-
None => return Err(InboundHTLCErr {
494-
msg: "Failed to decode update add htlc onion",
495-
reason: LocalHTLCFailureReason::InvalidOnionPayload,
496-
err_data: Vec::new(),
497-
}),
494+
_ => {
495+
return Err(InboundHTLCErr {
496+
msg: "Failed to decode update add htlc onion",
497+
reason: LocalHTLCFailureReason::InvalidOnionPayload,
498+
err_data: Vec::new(),
499+
});
500+
}
498501
};
499502

500503
if let Err(reason) = check_incoming_htlc_cltv(
@@ -532,6 +535,10 @@ pub(super) enum HopConnector {
532535

533536
pub(super) struct NextPacketDetails {
534537
pub(super) next_packet_pubkey: Result<PublicKey, secp256k1::Error>,
538+
pub(super) forward_info: Option<ForwardInfo>,
539+
}
540+
541+
pub(super) struct ForwardInfo {
535542
pub(super) outgoing_connector: HopConnector,
536543
pub(super) outgoing_amt_msat: u64,
537544
pub(super) outgoing_cltv_value: u32,
@@ -608,8 +615,12 @@ where
608615
let next_packet_pubkey = onion_utils::next_hop_pubkey(secp_ctx,
609616
msg.onion_routing_packet.public_key.unwrap(), &shared_secret.secret_bytes());
610617
Some(NextPacketDetails {
611-
next_packet_pubkey, outgoing_connector: HopConnector::ShortChannelId(short_channel_id),
612-
outgoing_amt_msat: amt_to_forward, outgoing_cltv_value
618+
next_packet_pubkey,
619+
forward_info: Some(ForwardInfo {
620+
outgoing_connector: HopConnector::ShortChannelId(short_channel_id),
621+
outgoing_amt_msat: amt_to_forward,
622+
outgoing_cltv_value,
623+
}),
613624
})
614625
}
615626
onion_utils::Hop::BlindedForward { next_hop_data: msgs::InboundOnionBlindedForwardPayload { short_channel_id, ref payment_relay, ref payment_constraints, ref features, .. }, shared_secret, .. } => {
@@ -625,19 +636,31 @@ where
625636
let next_packet_pubkey = onion_utils::next_hop_pubkey(&secp_ctx,
626637
msg.onion_routing_packet.public_key.unwrap(), &shared_secret.secret_bytes());
627638
Some(NextPacketDetails {
628-
next_packet_pubkey, outgoing_connector: HopConnector::ShortChannelId(short_channel_id), outgoing_amt_msat: amt_to_forward,
629-
outgoing_cltv_value
639+
next_packet_pubkey,
640+
forward_info: Some(ForwardInfo {
641+
outgoing_connector: HopConnector::ShortChannelId(short_channel_id),
642+
outgoing_amt_msat: amt_to_forward,
643+
outgoing_cltv_value,
644+
}),
630645
})
631646
}
632647
onion_utils::Hop::TrampolineForward { next_trampoline_hop_data: msgs::InboundTrampolineForwardPayload { amt_to_forward, outgoing_cltv_value, next_trampoline }, trampoline_shared_secret, incoming_trampoline_public_key, .. } => {
633648
let next_trampoline_packet_pubkey = onion_utils::next_hop_pubkey(secp_ctx,
634649
incoming_trampoline_public_key, &trampoline_shared_secret.secret_bytes());
635650
Some(NextPacketDetails {
636651
next_packet_pubkey: next_trampoline_packet_pubkey,
637-
outgoing_connector: HopConnector::Trampoline(next_trampoline),
638-
outgoing_amt_msat: amt_to_forward,
639-
outgoing_cltv_value,
652+
forward_info: Some(ForwardInfo {
653+
outgoing_connector: HopConnector::Trampoline(next_trampoline),
654+
outgoing_amt_msat: amt_to_forward,
655+
outgoing_cltv_value,
656+
}),
640657
})
658+
},
659+
onion_utils::Hop::Dummy { shared_secret, .. } => {
660+
let next_packet_pubkey = onion_utils::next_hop_pubkey(secp_ctx,
661+
msg.onion_routing_packet.public_key.unwrap(), &shared_secret.secret_bytes());
662+
663+
Some(NextPacketDetails { next_packet_pubkey, forward_info: None })
641664
}
642665
_ => None
643666
};

0 commit comments

Comments
 (0)