1 //! Feature flag definitions for the Lightning protocol according to [BOLT #9].
3 //! Lightning nodes advertise a supported set of operation through feature flags. Features are
4 //! applicable for a specific context as indicated in some [messages]. [`Features`] encapsulates
5 //! behavior for specifying and checking feature flags for a particular context. Each feature is
6 //! defined internally by a trait specifying the corresponding flags (i.e., even and odd bits). A
7 //! [`Context`] is used to parameterize [`Features`] and defines which features it can support.
9 //! Whether a feature is considered "known" or "unknown" is relative to the implementation, whereas
10 //! the term "supports" is used in reference to a particular set of [`Features`]. That is, a node
11 //! supports a feature if it advertises the feature (as either required or optional) to its peers.
12 //! And the implementation can interpret a feature if the feature is known to it.
14 //! [BOLT #9]: https://github.com/lightningnetwork/lightning-rfc/blob/master/09-features.md
15 //! [messages]: ../msgs/index.html
16 //! [`Features`]: struct.Features.html
17 //! [`Context`]: sealed/trait.Context.html
20 use std::result::Result;
21 use std::marker::PhantomData;
23 use ln::msgs::DecodeError;
24 use util::ser::{Readable, Writeable, Writer};
27 /// The context in which [`Features`] are applicable. Defines which features are required and
28 /// which are optional for the context.
30 /// [`Features`]: ../struct.Features.html
32 /// Features that are known to the implementation, where a required feature is indicated by
33 /// its even bit and an optional feature is indicated by its odd bit.
34 const KNOWN_FEATURE_FLAGS: &'static [u8];
36 /// Bitmask for selecting features that are known to the implementation, regardless of
37 /// whether each feature is required or optional.
38 const KNOWN_FEATURE_MASK: &'static [u8];
41 /// Defines a [`Context`] by stating which features it requires and which are optional. Features
42 /// are specified as a comma-separated list of bytes where each byte is a pipe-delimited list of
43 /// feature identifiers.
45 /// [`Context`]: trait.Context.html
46 macro_rules! define_context {
48 required_features: [$( $( $required_feature: ident )|*, )*],
49 optional_features: [$( $( $optional_feature: ident )|*, )*],
51 pub struct $context {}
53 impl Context for $context {
54 const KNOWN_FEATURE_FLAGS: &'static [u8] = &[
55 // For each byte, use bitwise-OR to compute the applicable flags for known
56 // required features `r_i` and optional features `o_j` for all `i` and `j` such
57 // that the following slice is formed:
60 // `r_0` | `r_1` | ... | `o_0` | `o_1` | ...,
65 <Self as $required_feature>::REQUIRED_MASK)*
67 <Self as $optional_feature>::OPTIONAL_MASK)*,
71 const KNOWN_FEATURE_MASK: &'static [u8] = &[
72 // Similar as above, but set both flags for each feature regardless of whether
73 // the feature is required or optional.
76 <Self as $required_feature>::REQUIRED_MASK |
77 <Self as $required_feature>::OPTIONAL_MASK)*
79 <Self as $optional_feature>::REQUIRED_MASK |
80 <Self as $optional_feature>::OPTIONAL_MASK)*,
87 define_context!(InitContext {
98 DataLossProtect | InitialRoutingSync | UpfrontShutdownScript,
100 VariableLengthOnion | PaymentSecret,
105 define_context!(NodeContext {
116 DataLossProtect | UpfrontShutdownScript,
118 VariableLengthOnion | PaymentSecret,
123 define_context!(ChannelContext {
124 required_features: [],
125 optional_features: [],
128 /// Defines a feature with the given bits for the specified [`Context`]s. The generated trait is
129 /// useful for manipulating feature flags.
131 /// [`Context`]: trait.Context.html
132 macro_rules! define_feature {
133 ($odd_bit: expr, $feature: ident, [$($context: ty),+], $doc: expr) => {
136 /// See [BOLT #9] for details.
138 /// [BOLT #9]: https://github.com/lightningnetwork/lightning-rfc/blob/master/09-features.md
139 pub trait $feature: Context {
140 /// The bit used to signify that the feature is required.
141 const EVEN_BIT: usize = $odd_bit - 1;
143 /// The bit used to signify that the feature is optional.
144 const ODD_BIT: usize = $odd_bit;
146 /// Assertion that [`EVEN_BIT`] is actually even.
148 /// [`EVEN_BIT`]: #associatedconstant.EVEN_BIT
149 const ASSERT_EVEN_BIT_PARITY: usize;
151 /// Assertion that [`ODD_BIT`] is actually odd.
153 /// [`ODD_BIT`]: #associatedconstant.ODD_BIT
154 const ASSERT_ODD_BIT_PARITY: usize;
156 /// The byte where the feature is set.
157 const BYTE_OFFSET: usize = Self::EVEN_BIT / 8;
159 /// The bitmask for the feature's required flag relative to the [`BYTE_OFFSET`].
161 /// [`BYTE_OFFSET`]: #associatedconstant.BYTE_OFFSET
162 const REQUIRED_MASK: u8 = 1 << (Self::EVEN_BIT - 8 * Self::BYTE_OFFSET);
164 /// The bitmask for the feature's optional flag relative to the [`BYTE_OFFSET`].
166 /// [`BYTE_OFFSET`]: #associatedconstant.BYTE_OFFSET
167 const OPTIONAL_MASK: u8 = 1 << (Self::ODD_BIT - 8 * Self::BYTE_OFFSET);
169 /// Returns whether the feature is supported by the given flags.
171 fn supports_feature(flags: &Vec<u8>) -> bool {
172 flags.len() > Self::BYTE_OFFSET &&
173 (flags[Self::BYTE_OFFSET] & (Self::REQUIRED_MASK | Self::OPTIONAL_MASK)) != 0
176 /// Sets the feature's required (even) bit in the given flags.
178 fn set_required_bit(flags: &mut Vec<u8>) {
179 if flags.len() <= Self::BYTE_OFFSET {
180 flags.resize(Self::BYTE_OFFSET + 1, 0u8);
183 flags[Self::BYTE_OFFSET] |= Self::REQUIRED_MASK;
186 /// Sets the feature's optional (odd) bit in the given flags.
188 fn set_optional_bit(flags: &mut Vec<u8>) {
189 if flags.len() <= Self::BYTE_OFFSET {
190 flags.resize(Self::BYTE_OFFSET + 1, 0u8);
193 flags[Self::BYTE_OFFSET] |= Self::OPTIONAL_MASK;
196 /// Clears the feature's required (even) and optional (odd) bits from the given
199 fn clear_bits(flags: &mut Vec<u8>) {
200 if flags.len() > Self::BYTE_OFFSET {
201 flags[Self::BYTE_OFFSET] &= !Self::REQUIRED_MASK;
202 flags[Self::BYTE_OFFSET] &= !Self::OPTIONAL_MASK;
205 let last_non_zero_byte = flags.iter().rposition(|&byte| byte != 0);
206 let size = if let Some(offset) = last_non_zero_byte { offset + 1 } else { 0 };
207 flags.resize(size, 0u8);
212 impl $feature for $context {
214 const ASSERT_EVEN_BIT_PARITY: usize = 0 - (<Self as $feature>::EVEN_BIT % 2);
217 const ASSERT_ODD_BIT_PARITY: usize = (<Self as $feature>::ODD_BIT % 2) - 1;
223 define_feature!(1, DataLossProtect, [InitContext, NodeContext],
224 "Feature flags for `option_data_loss_protect`.");
225 // NOTE: Per Bolt #9, initial_routing_sync has no even bit.
226 define_feature!(3, InitialRoutingSync, [InitContext],
227 "Feature flags for `initial_routing_sync`.");
228 define_feature!(5, UpfrontShutdownScript, [InitContext, NodeContext],
229 "Feature flags for `option_upfront_shutdown_script`.");
230 define_feature!(9, VariableLengthOnion, [InitContext, NodeContext],
231 "Feature flags for `var_onion_optin`.");
232 define_feature!(15, PaymentSecret, [InitContext, NodeContext],
233 "Feature flags for `payment_secret`.");
234 define_feature!(17, BasicMPP, [InitContext, NodeContext],
235 "Feature flags for `basic_mpp`.");
238 define_context!(TestingContext {
258 define_feature!(23, UnknownFeature, [TestingContext],
259 "Feature flags for an unknown feature used in testing.");
262 /// Tracks the set of features which a node implements, templated by the context in which it
264 pub struct Features<T: sealed::Context> {
265 /// Note that, for convenience, flags is LITTLE endian (despite being big-endian on the wire)
267 mark: PhantomData<T>,
270 impl<T: sealed::Context> Clone for Features<T> {
271 fn clone(&self) -> Self {
273 flags: self.flags.clone(),
278 impl<T: sealed::Context> PartialEq for Features<T> {
279 fn eq(&self, o: &Self) -> bool {
280 self.flags.eq(&o.flags)
283 impl<T: sealed::Context> fmt::Debug for Features<T> {
284 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
289 /// Features used within an `init` message.
290 pub type InitFeatures = Features<sealed::InitContext>;
291 /// Features used within a `node_announcement` message.
292 pub type NodeFeatures = Features<sealed::NodeContext>;
293 /// Features used within a `channel_announcement` message.
294 pub type ChannelFeatures = Features<sealed::ChannelContext>;
297 /// Writes all features present up to, and including, 13.
298 pub(crate) fn write_up_to_13<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
299 let len = cmp::min(2, self.flags.len());
300 w.size_hint(len + 2);
301 (len as u16).write(w)?;
302 for i in (0..len).rev() {
304 self.flags[i].write(w)?;
306 // On byte 1, we want up-to-and-including-bit-13, 0-indexed, which is
307 // up-to-and-including-bit-5, 0-indexed, on this byte:
308 (self.flags[i] & 0b00_11_11_11).write(w)?;
314 /// or's another InitFeatures into this one.
315 pub(crate) fn or(mut self, o: InitFeatures) -> InitFeatures {
316 let total_feature_len = cmp::max(self.flags.len(), o.flags.len());
317 self.flags.resize(total_feature_len, 0u8);
318 for (byte, o_byte) in self.flags.iter_mut().zip(o.flags.iter()) {
324 /// Converts `InitFeatures` to `Features<C>`. Only known `InitFeatures` relevant to context `C`
325 /// are included in the result.
326 pub(crate) fn to_context<C: sealed::Context>(&self) -> Features<C> {
327 self.to_context_internal()
331 impl<T: sealed::Context> Features<T> {
332 /// Create a blank Features with no features set
333 pub fn empty() -> Features<T> {
340 /// Creates features known by the implementation as defined by [`T::KNOWN_FEATURE_FLAGS`].
342 /// [`T::KNOWN_FEATURE_FLAGS`]: sealed/trait.Context.html#associatedconstant.KNOWN_FEATURE_FLAGS
343 pub fn known() -> Features<T> {
345 flags: T::KNOWN_FEATURE_FLAGS.to_vec(),
350 /// Converts `Features<T>` to `Features<C>`. Only known `T` features relevant to context `C` are
351 /// included in the result.
352 fn to_context_internal<C: sealed::Context>(&self) -> Features<C> {
353 let byte_count = C::KNOWN_FEATURE_MASK.len();
354 let mut flags = Vec::new();
355 for (i, byte) in self.flags.iter().enumerate() {
357 let known_source_features = T::KNOWN_FEATURE_MASK[i];
358 let known_target_features = C::KNOWN_FEATURE_MASK[i];
359 flags.push(byte & known_source_features & known_target_features);
362 Features::<C> { flags, mark: PhantomData, }
366 /// Create a Features given a set of flags, in LE.
367 pub fn from_le_bytes(flags: Vec<u8>) -> Features<T> {
375 /// Gets the underlying flags set, in LE.
376 pub fn le_flags(&self) -> &Vec<u8> {
380 pub(crate) fn requires_unknown_bits(&self) -> bool {
381 // Bitwise AND-ing with all even bits set except for known features will select required
383 let byte_count = T::KNOWN_FEATURE_MASK.len();
384 self.flags.iter().enumerate().any(|(i, &byte)| {
385 let required_features = 0b01_01_01_01;
386 let unknown_features = if i < byte_count {
387 !T::KNOWN_FEATURE_MASK[i]
391 (byte & (required_features & unknown_features)) != 0
395 pub(crate) fn supports_unknown_bits(&self) -> bool {
396 // Bitwise AND-ing with all even and odd bits set except for known features will select
397 // both required and optional unknown features.
398 let byte_count = T::KNOWN_FEATURE_MASK.len();
399 self.flags.iter().enumerate().any(|(i, &byte)| {
400 let unknown_features = if i < byte_count {
401 !T::KNOWN_FEATURE_MASK[i]
405 (byte & unknown_features) != 0
409 /// The number of bytes required to represent the feature flags present. This does not include
410 /// the length bytes which are included in the serialized form.
411 pub(crate) fn byte_count(&self) -> usize {
416 pub(crate) fn set_required_unknown_bits(&mut self) {
417 <sealed::TestingContext as sealed::UnknownFeature>::set_required_bit(&mut self.flags);
421 pub(crate) fn set_optional_unknown_bits(&mut self) {
422 <sealed::TestingContext as sealed::UnknownFeature>::set_optional_bit(&mut self.flags);
426 pub(crate) fn clear_unknown_bits(&mut self) {
427 <sealed::TestingContext as sealed::UnknownFeature>::clear_bits(&mut self.flags);
431 impl<T: sealed::DataLossProtect> Features<T> {
432 pub(crate) fn supports_data_loss_protect(&self) -> bool {
433 <T as sealed::DataLossProtect>::supports_feature(&self.flags)
437 impl<T: sealed::UpfrontShutdownScript> Features<T> {
438 pub(crate) fn supports_upfront_shutdown_script(&self) -> bool {
439 <T as sealed::UpfrontShutdownScript>::supports_feature(&self.flags)
442 pub(crate) fn clear_upfront_shutdown_script(mut self) -> Self {
443 <T as sealed::UpfrontShutdownScript>::clear_bits(&mut self.flags);
448 impl<T: sealed::VariableLengthOnion> Features<T> {
449 pub(crate) fn supports_variable_length_onion(&self) -> bool {
450 <T as sealed::VariableLengthOnion>::supports_feature(&self.flags)
454 impl<T: sealed::InitialRoutingSync> Features<T> {
455 pub(crate) fn initial_routing_sync(&self) -> bool {
456 <T as sealed::InitialRoutingSync>::supports_feature(&self.flags)
458 pub(crate) fn clear_initial_routing_sync(&mut self) {
459 <T as sealed::InitialRoutingSync>::clear_bits(&mut self.flags)
463 impl<T: sealed::PaymentSecret> Features<T> {
465 // Note that we never need to test this since what really matters is the invoice - iff the
466 // invoice provides a payment_secret, we assume that we can use it (ie that the recipient
467 // supports payment_secret).
468 pub(crate) fn supports_payment_secret(&self) -> bool {
469 <T as sealed::PaymentSecret>::supports_feature(&self.flags)
473 impl<T: sealed::BasicMPP> Features<T> {
474 // We currently never test for this since we don't actually *generate* multipath routes.
476 pub(crate) fn supports_basic_mpp(&self) -> bool {
477 <T as sealed::BasicMPP>::supports_feature(&self.flags)
481 impl<T: sealed::Context> Writeable for Features<T> {
482 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
483 w.size_hint(self.flags.len() + 2);
484 (self.flags.len() as u16).write(w)?;
485 for f in self.flags.iter().rev() { // Swap back to big-endian
492 impl<T: sealed::Context> Readable for Features<T> {
493 fn read<R: ::std::io::Read>(r: &mut R) -> Result<Self, DecodeError> {
494 let mut flags: Vec<u8> = Readable::read(r)?;
495 flags.reverse(); // Swap to little-endian
505 use super::{ChannelFeatures, InitFeatures, NodeFeatures};
508 fn sanity_test_our_features() {
509 assert!(!ChannelFeatures::known().requires_unknown_bits());
510 assert!(!ChannelFeatures::known().supports_unknown_bits());
511 assert!(!InitFeatures::known().requires_unknown_bits());
512 assert!(!InitFeatures::known().supports_unknown_bits());
513 assert!(!NodeFeatures::known().requires_unknown_bits());
514 assert!(!NodeFeatures::known().supports_unknown_bits());
516 assert!(InitFeatures::known().supports_upfront_shutdown_script());
517 assert!(NodeFeatures::known().supports_upfront_shutdown_script());
519 assert!(InitFeatures::known().supports_data_loss_protect());
520 assert!(NodeFeatures::known().supports_data_loss_protect());
522 assert!(InitFeatures::known().supports_variable_length_onion());
523 assert!(NodeFeatures::known().supports_variable_length_onion());
525 assert!(InitFeatures::known().supports_payment_secret());
526 assert!(NodeFeatures::known().supports_payment_secret());
528 assert!(InitFeatures::known().supports_basic_mpp());
529 assert!(NodeFeatures::known().supports_basic_mpp());
531 let mut init_features = InitFeatures::known();
532 assert!(init_features.initial_routing_sync());
533 init_features.clear_initial_routing_sync();
534 assert!(!init_features.initial_routing_sync());
538 fn sanity_test_unknown_bits() {
539 let mut features = ChannelFeatures::empty();
540 assert!(!features.requires_unknown_bits());
541 assert!(!features.supports_unknown_bits());
543 features.set_required_unknown_bits();
544 assert!(features.requires_unknown_bits());
545 assert!(features.supports_unknown_bits());
547 features.clear_unknown_bits();
548 assert!(!features.requires_unknown_bits());
549 assert!(!features.supports_unknown_bits());
551 features.set_optional_unknown_bits();
552 assert!(!features.requires_unknown_bits());
553 assert!(features.supports_unknown_bits());
557 fn convert_to_context_with_relevant_flags() {
558 let init_features = InitFeatures::known().clear_upfront_shutdown_script();
559 assert!(init_features.initial_routing_sync());
560 assert!(!init_features.supports_upfront_shutdown_script());
562 let node_features: NodeFeatures = init_features.to_context();
564 // Check that the flags are as expected:
565 // - option_data_loss_protect
566 // - var_onion_optin | payment_secret
568 assert_eq!(node_features.flags.len(), 3);
569 assert_eq!(node_features.flags[0], 0b00000010);
570 assert_eq!(node_features.flags[1], 0b10000010);
571 assert_eq!(node_features.flags[2], 0b00000010);
574 // Check that cleared flags are kept blank when converting back:
575 // - initial_routing_sync was not applicable to NodeContext
576 // - upfront_shutdown_script was cleared before converting
577 let features: InitFeatures = node_features.to_context_internal();
578 assert!(!features.initial_routing_sync());
579 assert!(!features.supports_upfront_shutdown_script());