Skip to content

Commit cb299eb

Browse files
committed
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 94fff25 commit cb299eb

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
@@ -115,19 +115,29 @@ mod sealed {
115115
///
116116
/// [`Context`]: trait.Context.html
117117
macro_rules! define_feature {
118-
($even_bit: expr, $odd_bit: expr, $feature: ident, [$($context: ty),+], $doc: expr) => {
118+
($odd_bit: expr, $feature: ident, [$($context: ty),+], $doc: expr) => {
119119
#[doc = $doc]
120120
///
121121
/// See [BOLT #9] for details.
122122
///
123123
/// [BOLT #9]: https://github.com/lightningnetwork/lightning-rfc/blob/master/09-features.md
124124
pub trait $feature: Context {
125125
/// The bit used to signify that the feature is required.
126-
const EVEN_BIT: usize = $even_bit;
126+
const EVEN_BIT: usize = $odd_bit - 1;
127127

128128
/// The bit used to signify that the feature is optional.
129129
const ODD_BIT: usize = $odd_bit;
130130

131+
/// Assertion that [`EVEN_BIT`] is actually even.
132+
///
133+
/// [`EVEN_BIT`]: #associatedconstant.EVEN_BIT
134+
const ASSERT_EVEN_BIT_PARITY: usize;
135+
136+
/// Assertion that [`ODD_BIT`] is actually odd.
137+
///
138+
/// [`ODD_BIT`]: #associatedconstant.ODD_BIT
139+
const ASSERT_ODD_BIT_PARITY: usize;
140+
131141
/// The byte where the feature is set.
132142
const BYTE_OFFSET: usize = Self::EVEN_BIT / 8;
133143

@@ -170,23 +180,29 @@ mod sealed {
170180
}
171181

172182
$(
173-
impl $feature for $context {}
183+
impl $feature for $context {
184+
// EVEN_BIT % 2 == 0
185+
const ASSERT_EVEN_BIT_PARITY: usize = 0 - (<Self as $feature>::EVEN_BIT % 2);
186+
187+
// ODD_BIT % 2 == 1
188+
const ASSERT_ODD_BIT_PARITY: usize = (<Self as $feature>::ODD_BIT % 2) - 1;
189+
}
174190
)*
175191
}
176192
}
177193

178-
define_feature!(0, 1, DataLossProtect, [InitContext, NodeContext],
194+
define_feature!(1, DataLossProtect, [InitContext, NodeContext],
179195
"Feature flags for `option_data_loss_protect`.");
180196
// NOTE: Per Bolt #9, initial_routing_sync has no even bit.
181-
define_feature!(3, 3, InitialRoutingSync, [InitContext],
197+
define_feature!(3, InitialRoutingSync, [InitContext],
182198
"Feature flags for `initial_routing_sync`.");
183-
define_feature!(4, 5, UpfrontShutdownScript, [InitContext, NodeContext],
199+
define_feature!(5, UpfrontShutdownScript, [InitContext, NodeContext],
184200
"Feature flags for `option_upfront_shutdown_script`.");
185-
define_feature!(8, 9, VariableLengthOnion, [InitContext, NodeContext],
201+
define_feature!(9, VariableLengthOnion, [InitContext, NodeContext],
186202
"Feature flags for `var_onion_optin`.");
187-
define_feature!(14, 15, PaymentSecret, [InitContext, NodeContext],
203+
define_feature!(15, PaymentSecret, [InitContext, NodeContext],
188204
"Feature flags for `payment_secret`.");
189-
define_feature!(16, 17, BasicMPP, [InitContext, NodeContext],
205+
define_feature!(17, BasicMPP, [InitContext, NodeContext],
190206
"Feature flags for `basic_mpp`.");
191207
}
192208

0 commit comments

Comments
 (0)