+mod sealed {
+ /// The context in which [`Features`] are applicable. Defines which features are required and
+ /// which are optional for the context.
+ ///
+ /// [`Features`]: ../struct.Features.html
+ pub trait Context {
+ /// Features that are known to the implementation, where a required feature is indicated by
+ /// its even bit and an optional feature is indicated by its odd bit.
+ const KNOWN_FEATURE_FLAGS: &'static [u8];
+
+ /// Bitmask for selecting features that are known to the implementation, regardless of
+ /// whether each feature is required or optional.
+ const KNOWN_FEATURE_MASK: &'static [u8];
+ }
+
+ /// Defines a [`Context`] by stating which features it requires and which are optional. Features
+ /// are specified as a comma-separated list of bytes where each byte is a pipe-delimited list of
+ /// feature identifiers.
+ ///
+ /// [`Context`]: trait.Context.html
+ macro_rules! define_context {
+ ($context: ident {
+ required_features: [$( $( $required_feature: ident )|*, )*],
+ optional_features: [$( $( $optional_feature: ident )|*, )*],
+ }) => {
+ pub struct $context {}
+
+ impl Context for $context {
+ const KNOWN_FEATURE_FLAGS: &'static [u8] = &[
+ // For each byte, use bitwise-OR to compute the applicable flags for known
+ // required features `r_i` and optional features `o_j` for all `i` and `j` such
+ // that the following slice is formed:
+ //
+ // [
+ // `r_0` | `r_1` | ... | `o_0` | `o_1` | ...,
+ // ...,
+ // ]
+ $(
+ 0b00_00_00_00 $(|
+ <Self as $required_feature>::REQUIRED_MASK)*
+ $(|
+ <Self as $optional_feature>::OPTIONAL_MASK)*,
+ )*
+ ];
+
+ const KNOWN_FEATURE_MASK: &'static [u8] = &[
+ // Similar as above, but set both flags for each feature regardless of whether
+ // the feature is required or optional.
+ $(
+ 0b00_00_00_00 $(|
+ <Self as $required_feature>::REQUIRED_MASK |
+ <Self as $required_feature>::OPTIONAL_MASK)*
+ $(|
+ <Self as $optional_feature>::REQUIRED_MASK |
+ <Self as $optional_feature>::OPTIONAL_MASK)*,
+ )*
+ ];
+ }
+ };
+ }
+
+ define_context!(InitContext {
+ required_features: [
+ // Byte 0
+ ,
+ // Byte 1
+ StaticRemoteKey,
+ // Byte 2
+ ,
+ ],
+ optional_features: [
+ // Byte 0
+ DataLossProtect | InitialRoutingSync | UpfrontShutdownScript,
+ // Byte 1
+ VariableLengthOnion | PaymentSecret,
+ // Byte 2
+ BasicMPP,
+ ],
+ });
+ define_context!(NodeContext {
+ required_features: [
+ // Byte 0
+ ,
+ // Byte 1
+ StaticRemoteKey,
+ // Byte 2
+ ,
+ ],
+ optional_features: [
+ // Byte 0
+ DataLossProtect | UpfrontShutdownScript,
+ // Byte 1
+ VariableLengthOnion | PaymentSecret,
+ // Byte 2
+ BasicMPP,
+ ],
+ });
+ define_context!(ChannelContext {
+ required_features: [],
+ optional_features: [],
+ });
+
+ /// Defines a feature with the given bits for the specified [`Context`]s. The generated trait is
+ /// useful for manipulating feature flags.
+ ///
+ /// [`Context`]: trait.Context.html
+ macro_rules! define_feature {
+ ($odd_bit: expr, $feature: ident, [$($context: ty),+], $doc: expr) => {
+ #[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);
+ }
+ }