Use explicit WithoutLength for BOLT 12 features
[rust-lightning] / lightning / src / ln / features.rs
index 4425a78e03f9ad406fe885e9f65d91a00d866115..9d34433161a55e064afcddca798fa6fe7aaacdeb 100644 (file)
 //! [BOLT #9]: https://github.com/lightning/bolts/blob/master/09-features.md
 //! [messages]: crate::ln::msgs
 
-use {io, io_extras};
-use prelude::*;
+use crate::{io, io_extras};
+use crate::prelude::*;
 use core::{cmp, fmt};
 use core::hash::{Hash, Hasher};
 use core::marker::PhantomData;
 
 use bitcoin::bech32;
 use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, u5, WriteBase32};
-use ln::msgs::DecodeError;
-use util::ser::{Readable, Writeable, Writer};
+use crate::ln::msgs::DecodeError;
+use crate::util::ser::{Readable, WithoutLength, Writeable, Writer};
 
 mod sealed {
-       use prelude::*;
-       use ln::features::Features;
+       use crate::prelude::*;
+       use crate::ln::features::Features;
 
        /// The context in which [`Features`] are applicable. Defines which features are known to the
        /// implementation, though specification of them as required or optional is up to the code
@@ -157,6 +157,8 @@ mod sealed {
                // Byte 2
                BasicMPP,
        ]);
+       define_context!(OfferContext, []);
+       define_context!(InvoiceRequestContext, []);
        // This isn't a "real" feature context, and is only used in the channel_type field in an
        // `OpenChannel` message.
        define_context!(ChannelTypeContext, [
@@ -203,6 +205,12 @@ mod sealed {
                                /// [`ODD_BIT`]: #associatedconstant.ODD_BIT
                                const ASSERT_ODD_BIT_PARITY: usize;
 
+                               /// Assertion that the bits are set in the context's [`KNOWN_FEATURE_MASK`].
+                               ///
+                               /// [`KNOWN_FEATURE_MASK`]: Context::KNOWN_FEATURE_MASK
+                               #[cfg(not(test))] // We violate this constraint with `UnknownFeature`
+                               const ASSERT_BITS_IN_MASK: u8;
+
                                /// The byte where the feature is set.
                                const BYTE_OFFSET: usize = Self::EVEN_BIT / 8;
 
@@ -289,6 +297,12 @@ mod sealed {
 
                                        // ODD_BIT % 2 == 1
                                        const ASSERT_ODD_BIT_PARITY: usize = (<Self as $feature>::ODD_BIT % 2) - 1;
+
+                                       // (byte & (REQUIRED_MASK | OPTIONAL_MASK)) >> (EVEN_BIT % 8) == 3
+                                       #[cfg(not(test))] // We violate this constraint with `UnknownFeature`
+                                       const ASSERT_BITS_IN_MASK: u8 =
+                                               ((<$context>::KNOWN_FEATURE_MASK[<Self as $feature>::BYTE_OFFSET] & (<Self as $feature>::REQUIRED_MASK | <Self as $feature>::OPTIONAL_MASK))
+                                                >> (<Self as $feature>::EVEN_BIT % 8)) - 3;
                                }
                        )*
                };
@@ -354,7 +368,8 @@ mod sealed {
                supports_keysend, requires_keysend);
 
        #[cfg(test)]
-       define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext, InvoiceContext],
+       define_feature!(123456789, UnknownFeature,
+               [NodeContext, ChannelContext, InvoiceContext, OfferContext, InvoiceRequestContext],
                "Feature flags for an unknown feature used in testing.", set_unknown_feature_optional,
                set_unknown_feature_required, supports_unknown_test_feature, requires_unknown_test_feature);
 }
@@ -413,6 +428,10 @@ pub type NodeFeatures = Features<sealed::NodeContext>;
 pub type ChannelFeatures = Features<sealed::ChannelContext>;
 /// Features used within an invoice.
 pub type InvoiceFeatures = Features<sealed::InvoiceContext>;
+/// Features used within an `offer`.
+pub type OfferFeatures = Features<sealed::OfferContext>;
+/// Features used within an `invoice_request`.
+pub type InvoiceRequestFeatures = Features<sealed::InvoiceRequestContext>;
 
 /// Features used within the channel_type field in an OpenChannel message.
 ///
@@ -672,6 +691,15 @@ impl<T: sealed::Wumbo> Features<T> {
        }
 }
 
+#[cfg(test)]
+impl<T: sealed::UnknownFeature> Features<T> {
+       pub(crate) fn unknown() -> Self {
+               let mut features = Self::empty();
+               features.set_unknown_feature_required();
+               features
+       }
+}
+
 macro_rules! impl_feature_len_prefixed_write {
        ($features: ident) => {
                impl Writeable for $features {
@@ -692,25 +720,45 @@ impl_feature_len_prefixed_write!(ChannelFeatures);
 impl_feature_len_prefixed_write!(NodeFeatures);
 impl_feature_len_prefixed_write!(InvoiceFeatures);
 
-// Because ChannelTypeFeatures only appears inside of TLVs, it doesn't have a length prefix when
-// serialized. Thus, we can't use `impl_feature_len_prefixed_write`, above, and have to write our
-// own serialization.
-impl Writeable for ChannelTypeFeatures {
+// Some features only appear inside of TLVs, so they don't have a length prefix when serialized.
+macro_rules! impl_feature_tlv_write {
+       ($features: ident) => {
+               impl Writeable for $features {
+                       fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+                               WithoutLength(self).write(w)
+                       }
+               }
+               impl Readable for $features {
+                       fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
+                               Ok(WithoutLength::<Self>::read(r)?.0)
+                       }
+               }
+       }
+}
+
+impl_feature_tlv_write!(ChannelTypeFeatures);
+
+// Some features may appear both in a TLV record and as part of a TLV subtype sequence. The latter
+// requires a length but the former does not.
+
+impl<T: sealed::Context> Writeable for WithoutLength<&Features<T>> {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
-               self.write_be(w)
+               self.0.write_be(w)
        }
 }
-impl Readable for ChannelTypeFeatures {
+
+impl<T: sealed::Context> Readable for WithoutLength<Features<T>> {
        fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
                let v = io_extras::read_to_end(r)?;
-               Ok(Self::from_be_bytes(v))
+               Ok(WithoutLength(Features::<T>::from_be_bytes(v)))
        }
 }
 
 #[cfg(test)]
 mod tests {
-       use super::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, InvoiceFeatures, NodeFeatures, sealed};
+       use super::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, InvoiceFeatures, NodeFeatures, OfferFeatures, sealed};
        use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, u5};
+       use crate::util::ser::{Readable, WithoutLength, Writeable};
 
        #[test]
        fn sanity_test_unknown_bits() {
@@ -804,6 +852,20 @@ mod tests {
                assert!(features.supports_payment_secret());
        }
 
+       #[test]
+       fn encodes_features_without_length() {
+               let features = OfferFeatures::from_le_bytes(vec![1, 2, 3, 4, 5, 42, 100, 101]);
+               assert_eq!(features.flags.len(), 8);
+
+               let mut serialized_features = Vec::new();
+               WithoutLength(&features).write(&mut serialized_features).unwrap();
+               assert_eq!(serialized_features.len(), 8);
+
+               let deserialized_features =
+                       WithoutLength::<OfferFeatures>::read(&mut &serialized_features[..]).unwrap().0;
+               assert_eq!(features, deserialized_features);
+       }
+
        #[test]
        fn invoice_features_encoding() {
                let features_as_u5s = vec![