Skip to content

Commit 86ae53b

Browse files
committed
Introduce HopConnector enum
1 parent 0c77c1b commit 86ae53b

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use crate::types::features::{Bolt12InvoiceFeatures, ChannelFeatures, ChannelType
5656
#[cfg(any(feature = "_test_utils", test))]
5757
use crate::types::features::Bolt11InvoiceFeatures;
5858
use crate::routing::router::{BlindedTail, FixedRouter, InFlightHtlcs, Path, Payee, PaymentParameters, Route, RouteParameters, Router};
59-
use crate::ln::onion_payment::{check_incoming_htlc_cltv, create_recv_pending_htlc_info, create_fwd_pending_htlc_info, decode_incoming_update_add_htlc_onion, InboundHTLCErr, NextPacketDetails};
59+
use crate::ln::onion_payment::{check_incoming_htlc_cltv, create_recv_pending_htlc_info, create_fwd_pending_htlc_info, decode_incoming_update_add_htlc_onion, InboundHTLCErr, NextPacketDetails, HopConnector};
6060
use crate::ln::msgs;
6161
use crate::ln::onion_utils;
6262
use crate::ln::onion_utils::{HTLCFailReason, INVALID_ONION_BLINDING};
@@ -4240,11 +4240,15 @@ where
42404240
// we don't allow forwards outbound over them.
42414241
return Err(("Refusing to forward to a private channel based on our config.", 0x4000 | 10));
42424242
}
4243-
if chan.context.get_channel_type().supports_scid_privacy() && next_packet.outgoing_scid != chan.context.outbound_scid_alias() {
4244-
// `option_scid_alias` (referred to in LDK as `scid_privacy`) means
4245-
// "refuse to forward unless the SCID alias was used", so we pretend
4246-
// we don't have the channel here.
4247-
return Err(("Refusing to forward over real channel SCID as our counterparty requested.", 0x4000 | 10));
4243+
if let HopConnector::ShortChannelId(outgoing_scid) = next_packet.outgoing_connector {
4244+
if chan.context.get_channel_type().supports_scid_privacy() && outgoing_scid != chan.context.outbound_scid_alias() {
4245+
// `option_scid_alias` (referred to in LDK as `scid_privacy`) means
4246+
// "refuse to forward unless the SCID alias was used", so we pretend
4247+
// we don't have the channel here.
4248+
return Err(("Refusing to forward over real channel SCID as our counterparty requested.", 0x4000 | 10));
4249+
}
4250+
} else {
4251+
return Err(("Cannot forward by Node ID without SCID.", 0x4000 | 10));
42484252
}
42494253

42504254
// Note that we could technically not return an error yet here and just hope
@@ -4296,7 +4300,13 @@ where
42964300
fn can_forward_htlc(
42974301
&self, msg: &msgs::UpdateAddHTLC, next_packet_details: &NextPacketDetails
42984302
) -> Result<(), (&'static str, u16)> {
4299-
match self.do_funded_channel_callback(next_packet_details.outgoing_scid, |chan: &mut FundedChannel<SP>| {
4303+
let outgoing_scid = match next_packet_details.outgoing_connector {
4304+
HopConnector::ShortChannelId(scid) => scid,
4305+
HopConnector::NodeId(_) => {
4306+
return Err(("Cannot forward by Node ID without SCID.", 0x4000 | 10));
4307+
}
4308+
};
4309+
match self.do_funded_channel_callback(outgoing_scid, |chan: &mut FundedChannel<SP>| {
43004310
self.can_forward_htlc_to_outgoing_channel(chan, msg, next_packet_details)
43014311
}) {
43024312
Some(Ok(())) => {},
@@ -4305,8 +4315,8 @@ where
43054315
// If we couldn't find the channel info for the scid, it may be a phantom or
43064316
// intercept forward.
43074317
if (self.default_configuration.accept_intercept_htlcs &&
4308-
fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, next_packet_details.outgoing_scid, &self.chain_hash)) ||
4309-
fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, next_packet_details.outgoing_scid, &self.chain_hash)
4318+
fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, outgoing_scid, &self.chain_hash)) ||
4319+
fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, outgoing_scid, &self.chain_hash)
43104320
{} else {
43114321
return Err(("Don't have available channel for forwarding as requested.", 0x4000 | 10));
43124322
}
@@ -5647,7 +5657,12 @@ where
56475657
};
56485658

56495659
let is_intro_node_blinded_forward = next_hop.is_intro_node_blinded_forward();
5650-
let outgoing_scid_opt = next_packet_details_opt.as_ref().map(|d| d.outgoing_scid);
5660+
let outgoing_scid_opt = next_packet_details_opt.as_ref().and_then(|d| {
5661+
match d.outgoing_connector {
5662+
HopConnector::ShortChannelId(scid) => { Some(scid) }
5663+
HopConnector::NodeId(_) => { None }
5664+
}
5665+
});
56515666

56525667
// Process the HTLC on the incoming channel.
56535668
match self.do_funded_channel_callback(incoming_scid, |chan: &mut FundedChannel<SP>| {

lightning/src/ln/onion_payment.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::types::features::BlindedHopFeatures;
1717
use crate::ln::msgs;
1818
use crate::ln::onion_utils;
1919
use crate::ln::onion_utils::{HTLCFailReason, INVALID_ONION_BLINDING};
20+
use crate::routing::gossip::NodeId;
2021
use crate::sign::{NodeSigner, Recipient};
2122
use crate::util::logger::Logger;
2223

@@ -296,7 +297,7 @@ where
296297
Ok(match hop {
297298
onion_utils::Hop::Forward { next_hop_data, next_hop_hmac, new_packet_bytes } => {
298299
let NextPacketDetails {
299-
next_packet_pubkey, outgoing_amt_msat: _, outgoing_scid: _, outgoing_cltv_value
300+
next_packet_pubkey, outgoing_amt_msat: _, outgoing_connector: _, outgoing_cltv_value
300301
} = match next_packet_details_opt {
301302
Some(next_packet_details) => next_packet_details,
302303
// Forward should always include the next hop details
@@ -334,9 +335,16 @@ where
334335
})
335336
}
336337

338+
pub(super) enum HopConnector {
339+
// scid-based routing
340+
ShortChannelId(u64),
341+
// Trampoline-based routing
342+
NodeId(NodeId),
343+
}
344+
337345
pub(super) struct NextPacketDetails {
338346
pub(super) next_packet_pubkey: Result<PublicKey, secp256k1::Error>,
339-
pub(super) outgoing_scid: u64,
347+
pub(super) outgoing_connector: HopConnector,
340348
pub(super) outgoing_amt_msat: u64,
341349
pub(super) outgoing_cltv_value: u32,
342350
}
@@ -430,7 +438,7 @@ where
430438
let next_packet_pubkey = onion_utils::next_hop_pubkey(secp_ctx,
431439
msg.onion_routing_packet.public_key.unwrap(), &shared_secret);
432440
NextPacketDetails {
433-
next_packet_pubkey, outgoing_scid: short_channel_id,
441+
next_packet_pubkey, outgoing_connector: HopConnector::ShortChannelId(short_channel_id),
434442
outgoing_amt_msat: amt_to_forward, outgoing_cltv_value
435443
}
436444
},
@@ -451,7 +459,7 @@ where
451459
let next_packet_pubkey = onion_utils::next_hop_pubkey(&secp_ctx,
452460
msg.onion_routing_packet.public_key.unwrap(), &shared_secret);
453461
NextPacketDetails {
454-
next_packet_pubkey, outgoing_scid: short_channel_id, outgoing_amt_msat: amt_to_forward,
462+
next_packet_pubkey, outgoing_connector: HopConnector::ShortChannelId(short_channel_id), outgoing_amt_msat: amt_to_forward,
455463
outgoing_cltv_value
456464
}
457465
},

0 commit comments

Comments
 (0)