Skip to content

Commit 3372a4d

Browse files
committed
Introduce HopConnector enum
1 parent 8c3e777 commit 3372a4d

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

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

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

56825692
let is_intro_node_blinded_forward = next_hop.is_intro_node_blinded_forward();
5683-
let outgoing_scid_opt = next_packet_details_opt.as_ref().map(|d| d.outgoing_scid);
5693+
let outgoing_scid_opt = next_packet_details_opt.as_ref().and_then(|d| {
5694+
match d.outgoing_connector {
5695+
HopConnector::ShortChannelId(scid) => { Some(scid) }
5696+
HopConnector::Trampoline { .. } => { None }
5697+
}
5698+
});
56845699

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

Diff for: 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

@@ -298,7 +299,7 @@ where
298299
Ok(match hop {
299300
onion_utils::Hop::Forward { next_hop_data, next_hop_hmac, new_packet_bytes } => {
300301
let NextPacketDetails {
301-
next_packet_pubkey, outgoing_amt_msat: _, outgoing_scid: _, outgoing_cltv_value
302+
next_packet_pubkey, outgoing_amt_msat: _, outgoing_connector: _, outgoing_cltv_value
302303
} = match next_packet_details_opt {
303304
Some(next_packet_details) => next_packet_details,
304305
// Forward should always include the next hop details
@@ -336,9 +337,22 @@ where
336337
})
337338
}
338339

340+
pub(super) enum HopConnector {
341+
// scid-based routing
342+
ShortChannelId(u64),
343+
// Trampoline-based routing
344+
#[allow(unused)]
345+
Trampoline {
346+
// shared secret to derive keys for error decoding
347+
shared_secret: [u8; 32],
348+
// node ID to get to the next Trampoline hop
349+
node_id: NodeId,
350+
},
351+
}
352+
339353
pub(super) struct NextPacketDetails {
340354
pub(super) next_packet_pubkey: Result<PublicKey, secp256k1::Error>,
341-
pub(super) outgoing_scid: u64,
355+
pub(super) outgoing_connector: HopConnector,
342356
pub(super) outgoing_amt_msat: u64,
343357
pub(super) outgoing_cltv_value: u32,
344358
}
@@ -432,7 +446,7 @@ where
432446
let next_packet_pubkey = onion_utils::next_hop_pubkey(secp_ctx,
433447
msg.onion_routing_packet.public_key.unwrap(), &shared_secret);
434448
NextPacketDetails {
435-
next_packet_pubkey, outgoing_scid: short_channel_id,
449+
next_packet_pubkey, outgoing_connector: HopConnector::ShortChannelId(short_channel_id),
436450
outgoing_amt_msat: amt_to_forward, outgoing_cltv_value
437451
}
438452
},
@@ -453,7 +467,7 @@ where
453467
let next_packet_pubkey = onion_utils::next_hop_pubkey(&secp_ctx,
454468
msg.onion_routing_packet.public_key.unwrap(), &shared_secret);
455469
NextPacketDetails {
456-
next_packet_pubkey, outgoing_scid: short_channel_id, outgoing_amt_msat: amt_to_forward,
470+
next_packet_pubkey, outgoing_connector: HopConnector::ShortChannelId(short_channel_id), outgoing_amt_msat: amt_to_forward,
457471
outgoing_cltv_value
458472
}
459473
},

0 commit comments

Comments
 (0)