Skip to content

Commit 9da11c1

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

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3776,14 +3776,24 @@ where
37763776

37773777
#[rustfmt::skip]
37783778
fn create_and_insert_outbound_scid_alias(&self) -> u64 {
3779+
self.create_and_insert_outbound_scid_alias_with_intercept(false)
3780+
}
3781+
3782+
#[rustfmt::skip]
3783+
fn create_and_insert_outbound_scid_alias_with_intercept(&self, use_intercept_namespace: bool) -> u64 {
37793784
let height = self.best_block.read().unwrap().height;
37803785
let mut outbound_scid_alias = 0;
37813786
let mut i = 0;
37823787
loop {
37833788
if cfg!(fuzzing) { // fuzzing chacha20 doesn't use the key at all so we always get the same alias
37843789
outbound_scid_alias += 1;
37853790
} else {
3786-
outbound_scid_alias = fake_scid::Namespace::OutboundAlias.get_fake_scid(height, &self.chain_hash, &self.fake_scid_rand_bytes, &self.entropy_source);
3791+
let namespace = if use_intercept_namespace {
3792+
fake_scid::Namespace::Intercept
3793+
} else {
3794+
fake_scid::Namespace::OutboundAlias
3795+
};
3796+
outbound_scid_alias = namespace.get_fake_scid(height, &self.chain_hash, &self.fake_scid_rand_bytes, &self.entropy_source);
37873797
}
37883798
if outbound_scid_alias != 0 && self.outbound_scid_aliases.lock().unwrap().insert(outbound_scid_alias) {
37893799
break;
@@ -3850,9 +3860,10 @@ where
38503860
}
38513861

38523862
let mut channel = {
3853-
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
3854-
let their_features = &peer_state.latest_features;
38553863
let config = if override_config.is_some() { override_config.as_ref().unwrap() } else { &self.default_configuration };
3864+
let use_intercept_scid = config.channel_handshake_config.intercept_htlcs_on_channel;
3865+
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias_with_intercept(use_intercept_scid);
3866+
let their_features = &peer_state.latest_features;
38563867
match OutboundV1Channel::new(&self.fee_estimator, &self.entropy_source, &self.signer_provider, their_network_key,
38573868
their_features, channel_value_satoshis, push_msat, user_channel_id, config,
38583869
self.best_block.read().unwrap().height, outbound_scid_alias, temporary_channel_id, &*self.logger)
@@ -8191,7 +8202,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
81918202
}
81928203

81938204
// 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();
8205+
let use_intercept_scid = config.channel_handshake_config.intercept_htlcs_on_channel;
8206+
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias_with_intercept(use_intercept_scid);
81958207
channel.context_mut().set_outbound_scid_alias(outbound_scid_alias);
81968208

81978209
if let Some(message_send_event) = message_send_event {
@@ -8407,7 +8419,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84078419
},
84088420
};
84098421

8410-
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
8422+
let use_intercept_scid = self.default_configuration.channel_handshake_config.intercept_htlcs_on_channel;
8423+
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias_with_intercept(use_intercept_scid);
84118424
channel.context_mut().set_outbound_scid_alias(outbound_scid_alias);
84128425

84138426
if let Some(message_send_event) = message_send_event {
@@ -16303,6 +16316,7 @@ mod tests {
1630316316
to_self_delay: Some(200),
1630416317
max_accepted_htlcs: Some(5),
1630516318
channel_reserve_proportional_millionths: Some(20000),
16319+
intercept_htlcs_on_channel: None,
1630616320
}),
1630716321
update_overrides: None,
1630816322
};

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)