Skip to content

Commit dbd9400

Browse files
committed
Introduce HopConnector enum
1 parent 960d532 commit dbd9400

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

lightning/src/ln/channelmanager.rs

+25-10
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};
@@ -4261,11 +4261,15 @@ where
42614261
// we don't allow forwards outbound over them.
42624262
return Err(("Refusing to forward to a private channel based on our config.", 0x4000 | 10));
42634263
}
4264-
if chan.context.get_channel_type().supports_scid_privacy() && next_packet.outgoing_scid != chan.context.outbound_scid_alias() {
4265-
// `option_scid_alias` (referred to in LDK as `scid_privacy`) means
4266-
// "refuse to forward unless the SCID alias was used", so we pretend
4267-
// we don't have the channel here.
4268-
return Err(("Refusing to forward over real channel SCID as our counterparty requested.", 0x4000 | 10));
4264+
if let HopConnector::ShortChannelId(outgoing_scid) = next_packet.outgoing_connector {
4265+
if chan.context.get_channel_type().supports_scid_privacy() && outgoing_scid != chan.context.outbound_scid_alias() {
4266+
// `option_scid_alias` (referred to in LDK as `scid_privacy`) means
4267+
// "refuse to forward unless the SCID alias was used", so we pretend
4268+
// we don't have the channel here.
4269+
return Err(("Refusing to forward over real channel SCID as our counterparty requested.", 0x4000 | 10));
4270+
}
4271+
} else {
4272+
return Err(("Cannot forward by Node ID without SCID.", 0x4000 | 10));
42694273
}
42704274

42714275
// Note that we could technically not return an error yet here and just hope
@@ -4317,7 +4321,13 @@ where
43174321
fn can_forward_htlc(
43184322
&self, msg: &msgs::UpdateAddHTLC, next_packet_details: &NextPacketDetails
43194323
) -> Result<(), (&'static str, u16)> {
4320-
match self.do_funded_channel_callback(next_packet_details.outgoing_scid, |chan: &mut FundedChannel<SP>| {
4324+
let outgoing_scid = match next_packet_details.outgoing_connector {
4325+
HopConnector::ShortChannelId(scid) => scid,
4326+
HopConnector::Trampoline { .. } => {
4327+
return Err(("Cannot forward by Node ID without SCID.", 0x4000 | 10));
4328+
}
4329+
};
4330+
match self.do_funded_channel_callback(outgoing_scid, |chan: &mut FundedChannel<SP>| {
43214331
self.can_forward_htlc_to_outgoing_channel(chan, msg, next_packet_details)
43224332
}) {
43234333
Some(Ok(())) => {},
@@ -4326,8 +4336,8 @@ where
43264336
// If we couldn't find the channel info for the scid, it may be a phantom or
43274337
// intercept forward.
43284338
if (self.default_configuration.accept_intercept_htlcs &&
4329-
fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, next_packet_details.outgoing_scid, &self.chain_hash)) ||
4330-
fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, next_packet_details.outgoing_scid, &self.chain_hash)
4339+
fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, outgoing_scid, &self.chain_hash)) ||
4340+
fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, outgoing_scid, &self.chain_hash)
43314341
{} else {
43324342
return Err(("Don't have available channel for forwarding as requested.", 0x4000 | 10));
43334343
}
@@ -5668,7 +5678,12 @@ where
56685678
};
56695679

56705680
let is_intro_node_blinded_forward = next_hop.is_intro_node_blinded_forward();
5671-
let outgoing_scid_opt = next_packet_details_opt.as_ref().map(|d| d.outgoing_scid);
5681+
let outgoing_scid_opt = next_packet_details_opt.as_ref().and_then(|d| {
5682+
match d.outgoing_connector {
5683+
HopConnector::ShortChannelId(scid) => { Some(scid) }
5684+
HopConnector::Trampoline { .. } => { None }
5685+
}
5686+
});
56725687

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

lightning/src/ln/onion_payment.rs

+18-4
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,22 @@ where
334335
})
335336
}
336337

338+
pub(super) enum HopConnector {
339+
// scid-based routing
340+
ShortChannelId(u64),
341+
// Trampoline-based routing
342+
#[allow(unused)]
343+
Trampoline {
344+
// shared secret to derive keys for error decoding
345+
shared_secret: [u8; 32],
346+
// node ID to get to the next Trampoline hop
347+
node_id: NodeId,
348+
},
349+
}
350+
337351
pub(super) struct NextPacketDetails {
338352
pub(super) next_packet_pubkey: Result<PublicKey, secp256k1::Error>,
339-
pub(super) outgoing_scid: u64,
353+
pub(super) outgoing_connector: HopConnector,
340354
pub(super) outgoing_amt_msat: u64,
341355
pub(super) outgoing_cltv_value: u32,
342356
}
@@ -430,7 +444,7 @@ where
430444
let next_packet_pubkey = onion_utils::next_hop_pubkey(secp_ctx,
431445
msg.onion_routing_packet.public_key.unwrap(), &shared_secret);
432446
NextPacketDetails {
433-
next_packet_pubkey, outgoing_scid: short_channel_id,
447+
next_packet_pubkey, outgoing_connector: HopConnector::ShortChannelId(short_channel_id),
434448
outgoing_amt_msat: amt_to_forward, outgoing_cltv_value
435449
}
436450
},
@@ -451,7 +465,7 @@ where
451465
let next_packet_pubkey = onion_utils::next_hop_pubkey(&secp_ctx,
452466
msg.onion_routing_packet.public_key.unwrap(), &shared_secret);
453467
NextPacketDetails {
454-
next_packet_pubkey, outgoing_scid: short_channel_id, outgoing_amt_msat: amt_to_forward,
468+
next_packet_pubkey, outgoing_connector: HopConnector::ShortChannelId(short_channel_id), outgoing_amt_msat: amt_to_forward,
455469
outgoing_cltv_value
456470
}
457471
},

0 commit comments

Comments
 (0)