Skip to content

Commit 2b5db73

Browse files
committed
f Define each feature only in terms of an odd bit
Feature flags are generally assigned in pairs -- an even bit and an odd bit. Remove the need to specify both when defining a feature. Instead, define it in terms of an odd bit and infer the even bit. Remove the special case where initial_routing_sync has no even bit. Now, it (bit 2) is considered known by the implementation.
1 parent 7d93fe0 commit 2b5db73

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

lightning/src/ln/features.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,29 @@ mod sealed { // You should just use the type aliases instead.
2525
///
2626
/// [`Context`]: trait.Context.html
2727
macro_rules! define_feature {
28-
($even_bit: expr, $odd_bit: expr, $feature: ident, [$($context: ty),+], $doc: expr) => {
28+
($odd_bit: expr, $feature: ident, [$($context: ty),+], $doc: expr) => {
2929
#[doc = $doc]
3030
///
3131
/// See [BOLT #9] for details.
3232
///
3333
/// [BOLT #9]: https://github.com/lightningnetwork/lightning-rfc/blob/master/09-features.md
3434
pub trait $feature: Context {
3535
/// The bit used to signify that the feature is required.
36-
const EVEN_BIT: usize = $even_bit;
36+
const EVEN_BIT: usize = $odd_bit - 1;
3737

3838
/// The bit used to signify that the feature is optional.
3939
const ODD_BIT: usize = $odd_bit;
4040

41+
/// Assertion that [`EVEN_BIT`] is actually even.
42+
///
43+
/// [`EVEN_BIT`]: #associatedconstant.EVEN_BIT
44+
const ASSERT_EVEN_BIT_PARITY: usize;
45+
46+
/// Assertion that [`ODD_BIT`] is actually odd.
47+
///
48+
/// [`ODD_BIT`]: #associatedconstant.ODD_BIT
49+
const ASSERT_ODD_BIT_PARITY: usize;
50+
4151
/// The byte where the feature is set.
4252
const BYTE_OFFSET: usize = Self::EVEN_BIT / 8;
4353

@@ -78,23 +88,29 @@ mod sealed { // You should just use the type aliases instead.
7888
}
7989

8090
$(
81-
impl $feature for $context {}
91+
impl $feature for $context {
92+
// EVEN_BIT % 2 == 0
93+
const ASSERT_EVEN_BIT_PARITY: usize = 0 - (<Self as $feature>::EVEN_BIT % 2);
94+
95+
// ODD_BIT % 2 == 1
96+
const ASSERT_ODD_BIT_PARITY: usize = (<Self as $feature>::ODD_BIT % 2) - 1;
97+
}
8298
)*
8399
}
84100
}
85101

86-
define_feature!(0, 1, DataLossProtect, [InitContext, NodeContext],
102+
define_feature!(1, DataLossProtect, [InitContext, NodeContext],
87103
"Feature flags for `option_data_loss_protect`.");
88104
// NOTE: Per Bolt #9, initial_routing_sync has no even bit.
89-
define_feature!(3, 3, InitialRoutingSync, [InitContext],
105+
define_feature!(3, InitialRoutingSync, [InitContext],
90106
"Feature flags for `initial_routing_sync`.");
91-
define_feature!(4, 5, UpfrontShutdownScript, [InitContext, NodeContext],
107+
define_feature!(5, UpfrontShutdownScript, [InitContext, NodeContext],
92108
"Feature flags for `option_upfront_shutdown_script`.");
93-
define_feature!(8, 9, VariableLengthOnion, [InitContext, NodeContext],
109+
define_feature!(9, VariableLengthOnion, [InitContext, NodeContext],
94110
"Feature flags for `var_onion_optin`.");
95-
define_feature!(14, 15, PaymentSecret, [InitContext, NodeContext],
111+
define_feature!(15, PaymentSecret, [InitContext, NodeContext],
96112
"Feature flags for `payment_secret`.");
97-
define_feature!(16, 17, BasicMPP, [InitContext, NodeContext],
113+
define_feature!(17, BasicMPP, [InitContext, NodeContext],
98114
"Feature flags for `basic_mpp`.");
99115

100116
/// Generates a feature flag byte with the given features set as optional. Useful for initializing

0 commit comments

Comments
 (0)