Skip to content

Commit

Permalink
Assert that all defined features are in the known features set
Browse files Browse the repository at this point in the history
Now that the features contexts track the full set of all known
features, rather than the set of supported features, all defined
features should be listed in the context definition macro.

This adds a compile-time assertion to check that all bits for known
features are set in the context known set.
  • Loading branch information
TheBlueMatt committed Sep 14, 2022
1 parent 1895f5d commit 9f74638
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lightning/src/ln/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,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;

Expand Down Expand Up @@ -291,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;
}
)*
};
Expand Down

0 comments on commit 9f74638

Please sign in to comment.