Add/announce features for payment_secret and basic_mpp
authorMatt Corallo <git@bluematt.me>
Wed, 15 Jan 2020 04:31:21 +0000 (23:31 -0500)
committerMatt Corallo <git@bluematt.me>
Sun, 1 Mar 2020 04:26:16 +0000 (23:26 -0500)
lightning/src/ln/features.rs

index 990e0478c4d7b70a77d436af67f0888428a70e5d..2b91158c1fe9dfb3c779f352cdc060567a35eb9d 100644 (file)
@@ -33,6 +33,14 @@ mod sealed { // You should just use the type aliases instead.
        pub trait VariableLengthOnion: Context {}
        impl VariableLengthOnion for InitContext {}
        impl VariableLengthOnion for NodeContext {}
+
+       pub trait PaymentSecret: Context {}
+       impl PaymentSecret for InitContext {}
+       impl PaymentSecret for NodeContext {}
+
+       pub trait BasicMPP: Context {}
+       impl BasicMPP for InitContext {}
+       impl BasicMPP for NodeContext {}
 }
 
 /// Tracks the set of features which a node implements, templated by the context in which it
@@ -73,7 +81,7 @@ impl InitFeatures {
        /// Create a Features with the features we support
        pub fn supported() -> InitFeatures {
                InitFeatures {
-                       flags: vec![2 | 1 << 5, 1 << (9-8)],
+                       flags: vec![2 | 1 << 5, 1 << (9-8) | 1 << (15 - 8), 1 << (17 - 8*2)],
                        mark: PhantomData,
                }
        }
@@ -136,14 +144,14 @@ impl NodeFeatures {
        #[cfg(not(feature = "fuzztarget"))]
        pub(crate) fn supported() -> NodeFeatures {
                NodeFeatures {
-                       flags: vec![2 | 1 << 5, 1 << (9-8)],
+                       flags: vec![2 | 1 << 5, 1 << (9-8) | 1 << (15 - 8), 1 << (17 - 8*2)],
                        mark: PhantomData,
                }
        }
        #[cfg(feature = "fuzztarget")]
        pub fn supported() -> NodeFeatures {
                NodeFeatures {
-                       flags: vec![2 | 1 << 5, 1 << (9-8)],
+                       flags: vec![2 | 1 << 5, 1 << (9-8) | 1 << (15 - 8), 1 << (17 - 8*2)],
                        mark: PhantomData,
                }
        }
@@ -192,8 +200,10 @@ impl<T: sealed::Context> Features<T> {
                                // unknown, upfront_shutdown_script, unknown (actually initial_routing_sync, but it
                                // is only valid as an optional feature), and data_loss_protect:
                                0 => (byte & 0b01000100),
-                               // unknown, unknown, unknown, var_onion_optin:
-                               1 => (byte & 0b01010100),
+                               // payment_secret, unknown, unknown, var_onion_optin:
+                               1 => (byte & 0b00010100),
+                               // unknown, unknown, unknown, basic_mpp:
+                               2 => (byte & 0b01010100),
                                // fallback, all even bits set:
                                _ => (byte & 0b01010101),
                        }) != 0
@@ -206,8 +216,10 @@ impl<T: sealed::Context> Features<T> {
                                // unknown, upfront_shutdown_script, initial_routing_sync (is only valid as an
                                // optional feature), and data_loss_protect:
                                0 => (byte & 0b11000100),
-                               // unknown, unknown, unknown, var_onion_optin:
-                               1 => (byte & 0b11111100),
+                               // payment_secret, unknown, unknown, var_onion_optin:
+                               1 => (byte & 0b00111100),
+                               // unknown, unknown, unknown, basic_mpp:
+                               2 => (byte & 0b11111100),
                                _ => byte,
                        }) != 0
                })
@@ -221,16 +233,19 @@ impl<T: sealed::Context> Features<T> {
 
        #[cfg(test)]
        pub(crate) fn set_require_unknown_bits(&mut self) {
-               let newlen = cmp::max(2, self.flags.len());
+               let newlen = cmp::max(3, self.flags.len());
                self.flags.resize(newlen, 0u8);
-               self.flags[1] |= 0x40;
+               self.flags[2] |= 0x40;
        }
 
        #[cfg(test)]
        pub(crate) fn clear_require_unknown_bits(&mut self) {
-               let newlen = cmp::max(2, self.flags.len());
+               let newlen = cmp::max(3, self.flags.len());
                self.flags.resize(newlen, 0u8);
-               self.flags[1] &= !0x40;
+               self.flags[2] &= !0x40;
+               if self.flags.len() == 3 && self.flags[2] == 0 {
+                       self.flags.resize(2, 0u8);
+               }
                if self.flags.len() == 2 && self.flags[1] == 0 {
                        self.flags.resize(1, 0u8);
                }
@@ -272,6 +287,23 @@ impl<T: sealed::InitialRoutingSync> Features<T> {
        }
 }
 
+impl<T: sealed::PaymentSecret> Features<T> {
+       #[allow(dead_code)]
+       // Note that we never need to test this since what really matters is the invoice - iff the
+       // invoice provides a payment_secret, we assume all the way through that we can do MPP.
+       pub(crate) fn payment_secret(&self) -> bool {
+               self.flags.len() > 1 && (self.flags[1] & (3 << (12-8))) != 0
+       }
+}
+
+impl<T: sealed::BasicMPP> Features<T> {
+       // We currently never test for this since we don't actually *generate* multipath routes.
+       #[allow(dead_code)]
+       pub(crate) fn basic_mpp(&self) -> bool {
+               self.flags.len() > 2 && (self.flags[2] & (3 << (16-8*2))) != 0
+       }
+}
+
 impl<T: sealed::Context> Writeable for Features<T> {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
                w.size_hint(self.flags.len() + 2);