+ impl alloc::fmt::Display for Features<$context> {
+ fn fmt(&self, fmt: &mut alloc::fmt::Formatter) -> Result<(), alloc::fmt::Error> {
+ $(
+ $(
+ fmt.write_fmt(format_args!("{}: {}, ", stringify!($required_feature),
+ if <$context as $required_feature>::requires_feature(&self.flags) { "required" }
+ else if <$context as $required_feature>::supports_feature(&self.flags) { "supported" }
+ else { "not supported" }))?;
+ )*
+ $(
+ fmt.write_fmt(format_args!("{}: {}, ", stringify!($optional_feature),
+ if <$context as $optional_feature>::requires_feature(&self.flags) { "required" }
+ else if <$context as $optional_feature>::supports_feature(&self.flags) { "supported" }
+ else { "not supported" }))?;
+ )*
+ )*
+ fmt.write_fmt(format_args!("unknown flags: {}",
+ if self.requires_unknown_bits() { "required" }
+ else if self.supports_unknown_bits() { "supported" } else { "none" }))
+ }
+ }
+ };
+ }
+
+ define_context!(InitContext {
+ required_features: [
+ // Byte 0
+ ,
+ // Byte 1
+ VariableLengthOnion | StaticRemoteKey | PaymentSecret,
+ // Byte 2
+ ,
+ // Byte 3
+ ,
+ // Byte 4
+ ,
+ // Byte 5
+ ,
+ ],
+ optional_features: [
+ // Byte 0
+ DataLossProtect | InitialRoutingSync | UpfrontShutdownScript | GossipQueries,
+ // Byte 1
+ ,
+ // Byte 2
+ BasicMPP,
+ // Byte 3
+ ShutdownAnySegwit,
+ // Byte 4
+ ,
+ // Byte 5
+ ChannelType,
+ ],
+ });
+ define_context!(NodeContext {
+ required_features: [
+ // Byte 0
+ ,
+ // Byte 1
+ VariableLengthOnion | StaticRemoteKey | PaymentSecret,
+ // Byte 2
+ ,
+ // Byte 3
+ ,
+ // Byte 4
+ ,
+ // Byte 5
+ ,
+ // Byte 6
+ ,
+ ],
+ optional_features: [
+ // Byte 0
+ DataLossProtect | UpfrontShutdownScript | GossipQueries,
+ // Byte 1
+ ,
+ // Byte 2
+ BasicMPP,
+ // Byte 3
+ ShutdownAnySegwit,
+ // Byte 4
+ ,
+ // Byte 5
+ ChannelType,
+ // Byte 6
+ Keysend,
+ ],
+ });
+ define_context!(ChannelContext {
+ required_features: [],
+ optional_features: [],
+ });
+ define_context!(InvoiceContext {
+ required_features: [
+ // Byte 0
+ ,
+ // Byte 1
+ VariableLengthOnion | PaymentSecret,
+ // Byte 2
+ ,
+ ],
+ optional_features: [
+ // Byte 0
+ ,
+ // Byte 1
+ ,
+ // Byte 2
+ BasicMPP,
+ ],
+ });
+ // This isn't a "real" feature context, and is only used in the channel_type field in an
+ // `OpenChannel` message.
+ define_context!(ChannelTypeContext {
+ required_features: [
+ // Byte 0
+ ,
+ // Byte 1
+ StaticRemoteKey,
+ // Byte 2
+ ,
+ // Byte 3
+ ,
+ ],
+ optional_features: [
+ // Byte 0
+ ,
+ // Byte 1
+ ,
+ // Byte 2
+ ,
+ // Byte 3
+ ,
+ ],
+ });
+
+ /// Defines a feature with the given bits for the specified [`Context`]s. The generated trait is
+ /// useful for manipulating feature flags.
+ macro_rules! define_feature {
+ ($odd_bit: expr, $feature: ident, [$($context: ty),+], $doc: expr, $optional_setter: ident,
+ $required_setter: ident, $supported_getter: ident) => {
+ #[doc = $doc]
+ ///
+ /// See [BOLT #9] for details.
+ ///
+ /// [BOLT #9]: https://github.com/lightningnetwork/lightning-rfc/blob/master/09-features.md
+ pub trait $feature: Context {
+ /// The bit used to signify that the feature is required.
+ const EVEN_BIT: usize = $odd_bit - 1;
+
+ /// The bit used to signify that the feature is optional.
+ const ODD_BIT: usize = $odd_bit;
+
+ /// Assertion that [`EVEN_BIT`] is actually even.
+ ///
+ /// [`EVEN_BIT`]: #associatedconstant.EVEN_BIT
+ const ASSERT_EVEN_BIT_PARITY: usize;
+
+ /// Assertion that [`ODD_BIT`] is actually odd.
+ ///
+ /// [`ODD_BIT`]: #associatedconstant.ODD_BIT
+ const ASSERT_ODD_BIT_PARITY: usize;
+
+ /// The byte where the feature is set.
+ const BYTE_OFFSET: usize = Self::EVEN_BIT / 8;
+
+ /// The bitmask for the feature's required flag relative to the [`BYTE_OFFSET`].
+ ///
+ /// [`BYTE_OFFSET`]: #associatedconstant.BYTE_OFFSET
+ const REQUIRED_MASK: u8 = 1 << (Self::EVEN_BIT - 8 * Self::BYTE_OFFSET);
+
+ /// The bitmask for the feature's optional flag relative to the [`BYTE_OFFSET`].
+ ///
+ /// [`BYTE_OFFSET`]: #associatedconstant.BYTE_OFFSET
+ const OPTIONAL_MASK: u8 = 1 << (Self::ODD_BIT - 8 * Self::BYTE_OFFSET);
+
+ /// Returns whether the feature is required by the given flags.
+ #[inline]
+ fn requires_feature(flags: &Vec<u8>) -> bool {
+ flags.len() > Self::BYTE_OFFSET &&
+ (flags[Self::BYTE_OFFSET] & Self::REQUIRED_MASK) != 0
+ }
+
+ /// Returns whether the feature is supported by the given flags.
+ #[inline]
+ fn supports_feature(flags: &Vec<u8>) -> bool {
+ flags.len() > Self::BYTE_OFFSET &&
+ (flags[Self::BYTE_OFFSET] & (Self::REQUIRED_MASK | Self::OPTIONAL_MASK)) != 0
+ }
+
+ /// Sets the feature's required (even) bit in the given flags.
+ #[inline]
+ fn set_required_bit(flags: &mut Vec<u8>) {
+ if flags.len() <= Self::BYTE_OFFSET {
+ flags.resize(Self::BYTE_OFFSET + 1, 0u8);
+ }
+
+ flags[Self::BYTE_OFFSET] |= Self::REQUIRED_MASK;
+ }
+
+ /// Sets the feature's optional (odd) bit in the given flags.
+ #[inline]
+ fn set_optional_bit(flags: &mut Vec<u8>) {
+ if flags.len() <= Self::BYTE_OFFSET {
+ flags.resize(Self::BYTE_OFFSET + 1, 0u8);
+ }
+
+ flags[Self::BYTE_OFFSET] |= Self::OPTIONAL_MASK;
+ }
+
+ /// Clears the feature's required (even) and optional (odd) bits from the given
+ /// flags.
+ #[inline]
+ fn clear_bits(flags: &mut Vec<u8>) {
+ if flags.len() > Self::BYTE_OFFSET {
+ flags[Self::BYTE_OFFSET] &= !Self::REQUIRED_MASK;
+ flags[Self::BYTE_OFFSET] &= !Self::OPTIONAL_MASK;
+ }
+
+ let last_non_zero_byte = flags.iter().rposition(|&byte| byte != 0);
+ let size = if let Some(offset) = last_non_zero_byte { offset + 1 } else { 0 };
+ flags.resize(size, 0u8);
+ }
+ }