Add config knob for forwarding intercept payments
authorValentine Wallace <vwallace@protonmail.com>
Mon, 14 Nov 2022 18:36:52 +0000 (13:36 -0500)
committerValentine Wallace <vwallace@protonmail.com>
Wed, 30 Nov 2022 17:52:23 +0000 (12:52 -0500)
lightning/src/ln/channelmanager.rs
lightning/src/ln/payment_tests.rs
lightning/src/util/config.rs
lightning/src/util/events.rs

index c4d90480eecd2baeaeca20fd23299b0de2a00d58..3b1bf7432fbf42b69ba4934993f48d4593c4972e 100644 (file)
@@ -2233,8 +2233,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
                                                None => { // unknown_next_peer
                                                        // Note that this is likely a timing oracle for detecting whether an scid is a
                                                        // phantom or an intercept.
-                                                       if fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, *short_channel_id, &self.genesis_hash) ||
-                                                          fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, *short_channel_id, &self.genesis_hash)
+                                                       if (self.default_configuration.accept_intercept_htlcs &&
+                                                          fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, *short_channel_id, &self.genesis_hash)) ||
+                                                          fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, *short_channel_id, &self.genesis_hash)
                                                        {
                                                                None
                                                        } else {
@@ -3057,14 +3058,16 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
        /// Intercepted HTLCs can be useful for Lightning Service Providers (LSPs) to open a just-in-time
        /// channel to a receiving node if the node lacks sufficient inbound liquidity.
        ///
-       /// To make use of intercepted HTLCs, use [`ChannelManager::get_intercept_scid`] to generate short
-       /// channel id(s) to put in the receiver's invoice route hints. These route hints will signal to
-       /// LDK to generate an [`HTLCIntercepted`] event when it receives the forwarded HTLC, and this
-       /// method or [`ChannelManager::fail_intercepted_htlc`] MUST be called in response to the event.
+       /// To make use of intercepted HTLCs, set [`UserConfig::accept_intercept_htlcs`] and use
+       /// [`ChannelManager::get_intercept_scid`] to generate short channel id(s) to put in the
+       /// receiver's invoice route hints. These route hints will signal to LDK to generate an
+       /// [`HTLCIntercepted`] event when it receives the forwarded HTLC, and this method or
+       /// [`ChannelManager::fail_intercepted_htlc`] MUST be called in response to the event.
        ///
        /// Note that LDK does not enforce fee requirements in `amt_to_forward_msat`, and will not stop
        /// you from forwarding more than you received.
        ///
+       /// [`UserConfig::accept_intercept_htlcs`]: crate::util::config::UserConfig::accept_intercept_htlcs
        /// [`HTLCIntercepted`]: events::Event::HTLCIntercepted
        // TODO: when we move to deciding the best outbound channel at forward time, only take
        // `next_node_id` and not `next_hop_channel_id`
index 59f6909b837597cfec9c6a5bcf4d2dd17803505e..a876b73b88a8f5fb02cc9746200acd824d733347 100644 (file)
@@ -1385,9 +1385,13 @@ fn intercepted_payment() {
 fn do_test_intercepted_payment(fail_intercept: bool) {
        let chanmon_cfgs = create_chanmon_cfgs(3);
        let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
-       let mut chan_config = test_default_channel_config();
-       chan_config.manually_accept_inbound_channels = true;
-       let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, Some(chan_config)]);
+
+       let mut zero_conf_chan_config = test_default_channel_config();
+       zero_conf_chan_config.manually_accept_inbound_channels = true;
+       let mut intercept_forwards_config = test_default_channel_config();
+       intercept_forwards_config.accept_intercept_htlcs = true;
+       let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(intercept_forwards_config), Some(zero_conf_chan_config)]);
+
        let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
        let scorer = test_utils::TestScorer::with_penalty(0);
        let random_seed_bytes = chanmon_cfgs[0].keys_manager.get_secure_random_bytes();
index 26c8da7543615777ec814a88adff9163b147ddd0..c9c76f4e9fa5480e2c8a6be0a642b2192db903ca 100644 (file)
@@ -505,6 +505,17 @@ pub struct UserConfig {
        /// [`msgs::OpenChannel`]: crate::ln::msgs::OpenChannel
        /// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
        pub manually_accept_inbound_channels: bool,
+       ///  If this is set to true, LDK will intercept HTLCs that are attempting to be forwarded over
+       ///  fake short channel ids generated via [`ChannelManager::get_intercept_scid`]. Upon HTLC
+       ///  intercept, LDK will generate an [`Event::HTLCIntercepted`] which MUST be handled by the user.
+       ///
+       ///  Setting this to true may break backwards compatibility with LDK versions < 0.0.113.
+       ///
+       ///  Default value: false.
+       ///
+       /// [`ChannelManager::get_intercept_scid`]: crate::ln::channelmanager::ChannelManager::get_intercept_scid
+       /// [`Event::HTLCIntercepted`]: crate::util::events::Event::HTLCIntercepted
+       pub accept_intercept_htlcs: bool,
 }
 
 impl Default for UserConfig {
@@ -516,6 +527,7 @@ impl Default for UserConfig {
                        accept_forwards_to_priv_channels: false,
                        accept_inbound_channels: true,
                        manually_accept_inbound_channels: false,
+                       accept_intercept_htlcs: false,
                }
        }
 }
index e3e458cb9005c72af363e5eff7c1285f93c34898..18058bc03a8e10505ce0f966729cb56d66a7d147 100644 (file)
@@ -612,13 +612,14 @@ pub enum Event {
        },
        /// Used to indicate that we've intercepted an HTLC forward. This event will only be generated if
        /// you've encoded an intercept scid in the receiver's invoice route hints using
-       /// [`ChannelManager::get_intercept_scid`].
+       /// [`ChannelManager::get_intercept_scid`] and have set [`UserConfig::accept_intercept_htlcs`].
        ///
        /// [`ChannelManager::forward_intercepted_htlc`] or
        /// [`ChannelManager::fail_intercepted_htlc`] MUST be called in response to this event. See
        /// their docs for more information.
        ///
        /// [`ChannelManager::get_intercept_scid`]: crate::ln::channelmanager::ChannelManager::get_intercept_scid
+       /// [`UserConfig::accept_intercept_htlcs`]: crate::util::config::UserConfig::accept_intercept_htlcs
        /// [`ChannelManager::forward_intercepted_htlc`]: crate::ln::channelmanager::ChannelManager::forward_intercepted_htlc
        /// [`ChannelManager::fail_intercepted_htlc`]: crate::ln::channelmanager::ChannelManager::fail_intercepted_htlc
        HTLCIntercepted {