Skip to content

Commit 363cf07

Browse files
committed
Add option to use scid alias from intercept namespace
1 parent 0848e7a commit 363cf07

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3771,19 +3771,21 @@ where
37713771

37723772
#[cfg(test)]
37733773
pub fn create_and_insert_outbound_scid_alias_for_test(&self) -> u64 {
3774-
self.create_and_insert_outbound_scid_alias()
3774+
self.create_and_insert_outbound_scid_alias_with_namespace(
3775+
fake_scid::Namespace::OutboundAlias
3776+
)
37753777
}
37763778

37773779
#[rustfmt::skip]
3778-
fn create_and_insert_outbound_scid_alias(&self) -> u64 {
3780+
fn create_and_insert_outbound_scid_alias_with_namespace(&self, namespace: fake_scid::Namespace) -> u64 {
37793781
let height = self.best_block.read().unwrap().height;
37803782
let mut outbound_scid_alias = 0;
37813783
let mut i = 0;
37823784
loop {
37833785
if cfg!(fuzzing) { // fuzzing chacha20 doesn't use the key at all so we always get the same alias
37843786
outbound_scid_alias += 1;
37853787
} else {
3786-
outbound_scid_alias = fake_scid::Namespace::OutboundAlias.get_fake_scid(height, &self.chain_hash, &self.fake_scid_rand_bytes, &self.entropy_source);
3788+
outbound_scid_alias = namespace.get_fake_scid(height, &self.chain_hash, &self.fake_scid_rand_bytes, &self.entropy_source);
37873789
}
37883790
if outbound_scid_alias != 0 && self.outbound_scid_aliases.lock().unwrap().insert(outbound_scid_alias) {
37893791
break;
@@ -3794,6 +3796,15 @@ where
37943796
outbound_scid_alias
37953797
}
37963798

3799+
/// Determines the appropriate outbound SCID namespace based on the intercept_htlcs_on_channel configuration.
3800+
fn get_outbound_scid_namespace(intercept_htlcs_on_channel: bool) -> fake_scid::Namespace {
3801+
if intercept_htlcs_on_channel {
3802+
fake_scid::Namespace::Intercept
3803+
} else {
3804+
fake_scid::Namespace::OutboundAlias
3805+
}
3806+
}
3807+
37973808
/// Creates a new outbound channel to the given remote node and with the given value.
37983809
///
37993810
/// `user_channel_id` will be provided back as in
@@ -3850,9 +3861,10 @@ where
38503861
}
38513862

38523863
let mut channel = {
3853-
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
3854-
let their_features = &peer_state.latest_features;
38553864
let config = if override_config.is_some() { override_config.as_ref().unwrap() } else { &self.default_configuration };
3865+
let outbound_scid_namespace = Self::get_outbound_scid_namespace(config.channel_handshake_config.intercept_htlcs_on_channel);
3866+
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias_with_namespace(outbound_scid_namespace);
3867+
let their_features = &peer_state.latest_features;
38563868
match OutboundV1Channel::new(&self.fee_estimator, &self.entropy_source, &self.signer_provider, their_network_key,
38573869
their_features, channel_value_satoshis, push_msat, user_channel_id, config,
38583870
self.best_block.read().unwrap().height, outbound_scid_alias, temporary_channel_id, &*self.logger)
@@ -8191,7 +8203,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
81918203
}
81928204

81938205
// Now that we know we have a channel, assign an outbound SCID alias.
8194-
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
8206+
let outbound_scid_namespace = Self::get_outbound_scid_namespace(config.channel_handshake_config.intercept_htlcs_on_channel);
8207+
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias_with_namespace(outbound_scid_namespace);
81958208
channel.context_mut().set_outbound_scid_alias(outbound_scid_alias);
81968209

81978210
if let Some(message_send_event) = message_send_event {
@@ -8407,7 +8420,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84078420
},
84088421
};
84098422

8410-
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
8423+
let outbound_scid_namespace = Self::get_outbound_scid_namespace(self.default_configuration.channel_handshake_config.intercept_htlcs_on_channel);
8424+
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias_with_namespace(outbound_scid_namespace);
84118425
channel.context_mut().set_outbound_scid_alias(outbound_scid_alias);
84128426

84138427
if let Some(message_send_event) = message_send_event {
@@ -16303,6 +16317,7 @@ mod tests {
1630316317
to_self_delay: Some(200),
1630416318
max_accepted_htlcs: Some(5),
1630516319
channel_reserve_proportional_millionths: Some(20000),
16320+
intercept_htlcs_on_channel: None,
1630616321
}),
1630716322
update_overrides: None,
1630816323
};

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7474,6 +7474,7 @@ pub fn test_manually_accept_inbound_channel_request() {
74747474
to_self_delay: None,
74757475
max_accepted_htlcs: Some(3),
74767476
channel_reserve_proportional_millionths: None,
7477+
intercept_htlcs_on_channel: None,
74777478
}),
74787479
update_overrides: Some(ChannelConfigUpdate {
74797480
forwarding_fee_proportional_millionths: None,

lightning/src/util/config.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,18 @@ pub struct ChannelHandshakeConfig {
233233
///
234234
/// [`max_htlcs`]: crate::ln::chan_utils::max_htlcs
235235
pub our_max_accepted_htlcs: u16,
236+
/// If set, this channel's SCID alias will be generated in the intercept namespace, causing
237+
/// all HTLCs sent to this channel to trigger [`Event::HTLCIntercepted`] events instead of
238+
/// being automatically forwarded. This enables manual control over all payments to the channel.
239+
///
240+
/// Requires [`UserConfig::accept_intercept_htlcs`] to be `true`. Useful for scenarios like
241+
/// fee-taking or conditional forwarding.
242+
///
243+
/// Default value: `false`
244+
///
245+
/// [`Event::HTLCIntercepted`]: crate::events::Event::HTLCIntercepted
246+
/// [`UserConfig::accept_intercept_htlcs`]: crate::util::config::UserConfig::accept_intercept_htlcs
247+
pub intercept_htlcs_on_channel: bool,
236248
}
237249

238250
impl Default for ChannelHandshakeConfig {
@@ -250,6 +262,7 @@ impl Default for ChannelHandshakeConfig {
250262
#[cfg(test)]
251263
negotiate_anchor_zero_fee_commitments: false,
252264
our_max_accepted_htlcs: 50,
265+
intercept_htlcs_on_channel: false,
253266
}
254267
}
255268
}
@@ -273,6 +286,7 @@ impl Readable for ChannelHandshakeConfig {
273286
#[cfg(test)]
274287
negotiate_anchor_zero_fee_commitments: Readable::read(reader)?,
275288
our_max_accepted_htlcs: Readable::read(reader)?,
289+
intercept_htlcs_on_channel: Readable::read(reader)?,
276290
})
277291
}
278292
}
@@ -1009,6 +1023,9 @@ pub struct ChannelHandshakeConfigUpdate {
10091023
/// The Proportion of the channel value to configure as counterparty's channel reserve. See
10101024
/// [`ChannelHandshakeConfig::their_channel_reserve_proportional_millionths`].
10111025
pub channel_reserve_proportional_millionths: Option<u32>,
1026+
/// If set, allows this channel's SCID alias to be generated in the intercept namespace. See
1027+
/// [`ChannelHandshakeConfig::intercept_htlcs_on_channel`].
1028+
pub intercept_htlcs_on_channel: Option<bool>,
10121029
}
10131030

10141031
impl ChannelHandshakeConfig {
@@ -1039,5 +1056,9 @@ impl ChannelHandshakeConfig {
10391056
if let Some(channel_reserve) = config.channel_reserve_proportional_millionths {
10401057
self.their_channel_reserve_proportional_millionths = channel_reserve;
10411058
}
1059+
1060+
if let Some(intercept_htlcs) = config.intercept_htlcs_on_channel {
1061+
self.intercept_htlcs_on_channel = intercept_htlcs;
1062+
}
10421063
}
10431064
}

0 commit comments

Comments
 (0)