Add detection of feature_static_remotekey support and print
[rust-lightning] / lightning / src / ln / features.rs
1 //! Feature flag definitions for the Lightning protocol according to [BOLT #9].
2 //!
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.
8 //!
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.
13 //!
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
18
19 use std::{cmp, fmt};
20 use std::result::Result;
21 use std::marker::PhantomData;
22
23 use ln::msgs::DecodeError;
24 use util::ser::{Readable, Writeable, Writer};
25
26 mod sealed {
27         /// The context in which [`Features`] are applicable. Defines which features are required and
28         /// which are optional for the context.
29         ///
30         /// [`Features`]: ../struct.Features.html
31         pub trait Context {
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];
35
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];
39         }
40
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.
44         ///
45         /// [`Context`]: trait.Context.html
46         macro_rules! define_context {
47                 ($context: ident {
48                         required_features: [$( $( $required_feature: ident )|*, )*],
49                         optional_features: [$( $( $optional_feature: ident )|*, )*],
50                 }) => {
51                         pub struct $context {}
52
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:
58                                         //
59                                         // [
60                                         //  `r_0` | `r_1` | ... | `o_0` | `o_1` | ...,
61                                         //  ...,
62                                         // ]
63                                         $(
64                                                 0b00_00_00_00 $(|
65                                                         <Self as $required_feature>::REQUIRED_MASK)*
66                                                 $(|
67                                                         <Self as $optional_feature>::OPTIONAL_MASK)*,
68                                         )*
69                                 ];
70
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.
74                                         $(
75                                                 0b00_00_00_00 $(|
76                                                         <Self as $required_feature>::REQUIRED_MASK |
77                                                         <Self as $required_feature>::OPTIONAL_MASK)*
78                                                 $(|
79                                                         <Self as $optional_feature>::REQUIRED_MASK |
80                                                         <Self as $optional_feature>::OPTIONAL_MASK)*,
81                                         )*
82                                 ];
83                         }
84                 };
85         }
86
87         define_context!(InitContext {
88                 required_features: [
89                         // Byte 0
90                         ,
91                         // Byte 1
92                         ,
93                         // Byte 2
94                         ,
95                 ],
96                 optional_features: [
97                         // Byte 0
98                         DataLossProtect | InitialRoutingSync | UpfrontShutdownScript,
99                         // Byte 1
100                         VariableLengthOnion | PaymentSecret,
101                         // Byte 2
102                         BasicMPP,
103                 ],
104         });
105         define_context!(NodeContext {
106                 required_features: [
107                         // Byte 0
108                         ,
109                         // Byte 1
110                         ,
111                         // Byte 2
112                         ,
113                 ],
114                 optional_features: [
115                         // Byte 0
116                         DataLossProtect | UpfrontShutdownScript,
117                         // Byte 1
118                         VariableLengthOnion | PaymentSecret,
119                         // Byte 2
120                         BasicMPP,
121                 ],
122         });
123         define_context!(ChannelContext {
124                 required_features: [],
125                 optional_features: [],
126         });
127
128         /// Defines a feature with the given bits for the specified [`Context`]s. The generated trait is
129         /// useful for manipulating feature flags.
130         ///
131         /// [`Context`]: trait.Context.html
132         macro_rules! define_feature {
133                 ($odd_bit: expr, $feature: ident, [$($context: ty),+], $doc: expr) => {
134                         #[doc = $doc]
135                         ///
136                         /// See [BOLT #9] for details.
137                         ///
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;
142
143                                 /// The bit used to signify that the feature is optional.
144                                 const ODD_BIT: usize = $odd_bit;
145
146                                 /// Assertion that [`EVEN_BIT`] is actually even.
147                                 ///
148                                 /// [`EVEN_BIT`]: #associatedconstant.EVEN_BIT
149                                 const ASSERT_EVEN_BIT_PARITY: usize;
150
151                                 /// Assertion that [`ODD_BIT`] is actually odd.
152                                 ///
153                                 /// [`ODD_BIT`]: #associatedconstant.ODD_BIT
154                                 const ASSERT_ODD_BIT_PARITY: usize;
155
156                                 /// The byte where the feature is set.
157                                 const BYTE_OFFSET: usize = Self::EVEN_BIT / 8;
158
159                                 /// The bitmask for the feature's required flag relative to the [`BYTE_OFFSET`].
160                                 ///
161                                 /// [`BYTE_OFFSET`]: #associatedconstant.BYTE_OFFSET
162                                 const REQUIRED_MASK: u8 = 1 << (Self::EVEN_BIT - 8 * Self::BYTE_OFFSET);
163
164                                 /// The bitmask for the feature's optional flag relative to the [`BYTE_OFFSET`].
165                                 ///
166                                 /// [`BYTE_OFFSET`]: #associatedconstant.BYTE_OFFSET
167                                 const OPTIONAL_MASK: u8 = 1 << (Self::ODD_BIT - 8 * Self::BYTE_OFFSET);
168
169                                 /// Returns whether the feature is required by the given flags.
170                                 #[inline]
171                                 fn requires_feature(flags: &Vec<u8>) -> bool {
172                                         flags.len() > Self::BYTE_OFFSET &&
173                                                 (flags[Self::BYTE_OFFSET] & Self::REQUIRED_MASK) != 0
174                                 }
175
176                                 /// Returns whether the feature is supported by the given flags.
177                                 #[inline]
178                                 fn supports_feature(flags: &Vec<u8>) -> bool {
179                                         flags.len() > Self::BYTE_OFFSET &&
180                                                 (flags[Self::BYTE_OFFSET] & (Self::REQUIRED_MASK | Self::OPTIONAL_MASK)) != 0
181                                 }
182
183                                 /// Sets the feature's required (even) bit in the given flags.
184                                 #[inline]
185                                 fn set_required_bit(flags: &mut Vec<u8>) {
186                                         if flags.len() <= Self::BYTE_OFFSET {
187                                                 flags.resize(Self::BYTE_OFFSET + 1, 0u8);
188                                         }
189
190                                         flags[Self::BYTE_OFFSET] |= Self::REQUIRED_MASK;
191                                 }
192
193                                 /// Sets the feature's optional (odd) bit in the given flags.
194                                 #[inline]
195                                 fn set_optional_bit(flags: &mut Vec<u8>) {
196                                         if flags.len() <= Self::BYTE_OFFSET {
197                                                 flags.resize(Self::BYTE_OFFSET + 1, 0u8);
198                                         }
199
200                                         flags[Self::BYTE_OFFSET] |= Self::OPTIONAL_MASK;
201                                 }
202
203                                 /// Clears the feature's required (even) and optional (odd) bits from the given
204                                 /// flags.
205                                 #[inline]
206                                 fn clear_bits(flags: &mut Vec<u8>) {
207                                         if flags.len() > Self::BYTE_OFFSET {
208                                                 flags[Self::BYTE_OFFSET] &= !Self::REQUIRED_MASK;
209                                                 flags[Self::BYTE_OFFSET] &= !Self::OPTIONAL_MASK;
210                                         }
211
212                                         let last_non_zero_byte = flags.iter().rposition(|&byte| byte != 0);
213                                         let size = if let Some(offset) = last_non_zero_byte { offset + 1 } else { 0 };
214                                         flags.resize(size, 0u8);
215                                 }
216                         }
217
218                         $(
219                                 impl $feature for $context {
220                                         // EVEN_BIT % 2 == 0
221                                         const ASSERT_EVEN_BIT_PARITY: usize = 0 - (<Self as $feature>::EVEN_BIT % 2);
222
223                                         // ODD_BIT % 2 == 1
224                                         const ASSERT_ODD_BIT_PARITY: usize = (<Self as $feature>::ODD_BIT % 2) - 1;
225                                 }
226                         )*
227                 }
228         }
229
230         define_feature!(1, DataLossProtect, [InitContext, NodeContext],
231                 "Feature flags for `option_data_loss_protect`.");
232         // NOTE: Per Bolt #9, initial_routing_sync has no even bit.
233         define_feature!(3, InitialRoutingSync, [InitContext],
234                 "Feature flags for `initial_routing_sync`.");
235         define_feature!(5, UpfrontShutdownScript, [InitContext, NodeContext],
236                 "Feature flags for `option_upfront_shutdown_script`.");
237         define_feature!(9, VariableLengthOnion, [InitContext, NodeContext],
238                 "Feature flags for `var_onion_optin`.");
239         define_feature!(13, StaticRemoteKey, [InitContext, NodeContext],
240                 "Feature flags for `option_static_remotekey`.");
241         define_feature!(15, PaymentSecret, [InitContext, NodeContext],
242                 "Feature flags for `payment_secret`.");
243         define_feature!(17, BasicMPP, [InitContext, NodeContext],
244                 "Feature flags for `basic_mpp`.");
245
246         #[cfg(test)]
247         define_context!(TestingContext {
248                 required_features: [
249                         // Byte 0
250                         ,
251                         // Byte 1
252                         ,
253                         // Byte 2
254                         UnknownFeature,
255                 ],
256                 optional_features: [
257                         // Byte 0
258                         ,
259                         // Byte 1
260                         ,
261                         // Byte 2
262                         ,
263                 ],
264         });
265
266         #[cfg(test)]
267         define_feature!(23, UnknownFeature, [TestingContext],
268                 "Feature flags for an unknown feature used in testing.");
269 }
270
271 /// Tracks the set of features which a node implements, templated by the context in which it
272 /// appears.
273 pub struct Features<T: sealed::Context> {
274         /// Note that, for convenience, flags is LITTLE endian (despite being big-endian on the wire)
275         flags: Vec<u8>,
276         mark: PhantomData<T>,
277 }
278
279 impl<T: sealed::Context> Clone for Features<T> {
280         fn clone(&self) -> Self {
281                 Self {
282                         flags: self.flags.clone(),
283                         mark: PhantomData,
284                 }
285         }
286 }
287 impl<T: sealed::Context> PartialEq for Features<T> {
288         fn eq(&self, o: &Self) -> bool {
289                 self.flags.eq(&o.flags)
290         }
291 }
292 impl<T: sealed::Context> fmt::Debug for Features<T> {
293         fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
294                 self.flags.fmt(fmt)
295         }
296 }
297
298 /// Features used within an `init` message.
299 pub type InitFeatures = Features<sealed::InitContext>;
300 /// Features used within a `node_announcement` message.
301 pub type NodeFeatures = Features<sealed::NodeContext>;
302 /// Features used within a `channel_announcement` message.
303 pub type ChannelFeatures = Features<sealed::ChannelContext>;
304
305 impl InitFeatures {
306         /// Writes all features present up to, and including, 13.
307         pub(crate) fn write_up_to_13<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
308                 let len = cmp::min(2, self.flags.len());
309                 w.size_hint(len + 2);
310                 (len as u16).write(w)?;
311                 for i in (0..len).rev() {
312                         if i == 0 {
313                                 self.flags[i].write(w)?;
314                         } else {
315                                 // On byte 1, we want up-to-and-including-bit-13, 0-indexed, which is
316                                 // up-to-and-including-bit-5, 0-indexed, on this byte:
317                                 (self.flags[i] & 0b00_11_11_11).write(w)?;
318                         }
319                 }
320                 Ok(())
321         }
322
323         /// or's another InitFeatures into this one.
324         pub(crate) fn or(mut self, o: InitFeatures) -> InitFeatures {
325                 let total_feature_len = cmp::max(self.flags.len(), o.flags.len());
326                 self.flags.resize(total_feature_len, 0u8);
327                 for (byte, o_byte) in self.flags.iter_mut().zip(o.flags.iter()) {
328                         *byte |= *o_byte;
329                 }
330                 self
331         }
332
333         /// Converts `InitFeatures` to `Features<C>`. Only known `InitFeatures` relevant to context `C`
334         /// are included in the result.
335         pub(crate) fn to_context<C: sealed::Context>(&self) -> Features<C> {
336                 self.to_context_internal()
337         }
338 }
339
340 impl<T: sealed::Context> Features<T> {
341         /// Create a blank Features with no features set
342         pub fn empty() -> Features<T> {
343                 Features {
344                         flags: Vec::new(),
345                         mark: PhantomData,
346                 }
347         }
348
349         /// Creates features known by the implementation as defined by [`T::KNOWN_FEATURE_FLAGS`].
350         ///
351         /// [`T::KNOWN_FEATURE_FLAGS`]: sealed/trait.Context.html#associatedconstant.KNOWN_FEATURE_FLAGS
352         pub fn known() -> Features<T> {
353                 Self {
354                         flags: T::KNOWN_FEATURE_FLAGS.to_vec(),
355                         mark: PhantomData,
356                 }
357         }
358
359         /// Converts `Features<T>` to `Features<C>`. Only known `T` features relevant to context `C` are
360         /// included in the result.
361         fn to_context_internal<C: sealed::Context>(&self) -> Features<C> {
362                 let byte_count = C::KNOWN_FEATURE_MASK.len();
363                 let mut flags = Vec::new();
364                 for (i, byte) in self.flags.iter().enumerate() {
365                         if i < byte_count {
366                                 let known_source_features = T::KNOWN_FEATURE_MASK[i];
367                                 let known_target_features = C::KNOWN_FEATURE_MASK[i];
368                                 flags.push(byte & known_source_features & known_target_features);
369                         }
370                 }
371                 Features::<C> { flags, mark: PhantomData, }
372         }
373
374         #[cfg(test)]
375         /// Create a Features given a set of flags, in LE.
376         pub fn from_le_bytes(flags: Vec<u8>) -> Features<T> {
377                 Features {
378                         flags,
379                         mark: PhantomData,
380                 }
381         }
382
383         #[cfg(test)]
384         /// Gets the underlying flags set, in LE.
385         pub fn le_flags(&self) -> &Vec<u8> {
386                 &self.flags
387         }
388
389         pub(crate) fn requires_unknown_bits(&self) -> bool {
390                 // Bitwise AND-ing with all even bits set except for known features will select required
391                 // unknown features.
392                 let byte_count = T::KNOWN_FEATURE_MASK.len();
393                 self.flags.iter().enumerate().any(|(i, &byte)| {
394                         let required_features = 0b01_01_01_01;
395                         let unknown_features = if i < byte_count {
396                                 !T::KNOWN_FEATURE_MASK[i]
397                         } else {
398                                 0b11_11_11_11
399                         };
400                         (byte & (required_features & unknown_features)) != 0
401                 })
402         }
403
404         pub(crate) fn supports_unknown_bits(&self) -> bool {
405                 // Bitwise AND-ing with all even and odd bits set except for known features will select
406                 // both required and optional unknown features.
407                 let byte_count = T::KNOWN_FEATURE_MASK.len();
408                 self.flags.iter().enumerate().any(|(i, &byte)| {
409                         let unknown_features = if i < byte_count {
410                                 !T::KNOWN_FEATURE_MASK[i]
411                         } else {
412                                 0b11_11_11_11
413                         };
414                         (byte & unknown_features) != 0
415                 })
416         }
417
418         /// The number of bytes required to represent the feature flags present. This does not include
419         /// the length bytes which are included in the serialized form.
420         pub(crate) fn byte_count(&self) -> usize {
421                 self.flags.len()
422         }
423
424         #[cfg(test)]
425         pub(crate) fn set_required_unknown_bits(&mut self) {
426                 <sealed::TestingContext as sealed::UnknownFeature>::set_required_bit(&mut self.flags);
427         }
428
429         #[cfg(test)]
430         pub(crate) fn set_optional_unknown_bits(&mut self) {
431                 <sealed::TestingContext as sealed::UnknownFeature>::set_optional_bit(&mut self.flags);
432         }
433
434         #[cfg(test)]
435         pub(crate) fn clear_unknown_bits(&mut self) {
436                 <sealed::TestingContext as sealed::UnknownFeature>::clear_bits(&mut self.flags);
437         }
438 }
439
440 impl<T: sealed::DataLossProtect> Features<T> {
441         #[cfg(test)]
442         pub(crate) fn requires_data_loss_protect(&self) -> bool {
443                 <T as sealed::DataLossProtect>::requires_feature(&self.flags)
444         }
445         pub(crate) fn supports_data_loss_protect(&self) -> bool {
446                 <T as sealed::DataLossProtect>::supports_feature(&self.flags)
447         }
448 }
449
450 impl<T: sealed::UpfrontShutdownScript> Features<T> {
451         #[cfg(test)]
452         pub(crate) fn requires_upfront_shutdown_script(&self) -> bool {
453                 <T as sealed::UpfrontShutdownScript>::requires_feature(&self.flags)
454         }
455         pub(crate) fn supports_upfront_shutdown_script(&self) -> bool {
456                 <T as sealed::UpfrontShutdownScript>::supports_feature(&self.flags)
457         }
458         #[cfg(test)]
459         pub(crate) fn clear_upfront_shutdown_script(mut self) -> Self {
460                 <T as sealed::UpfrontShutdownScript>::clear_bits(&mut self.flags);
461                 self
462         }
463 }
464
465 impl<T: sealed::VariableLengthOnion> Features<T> {
466         #[cfg(test)]
467         pub(crate) fn requires_variable_length_onion(&self) -> bool {
468                 <T as sealed::VariableLengthOnion>::requires_feature(&self.flags)
469         }
470         pub(crate) fn supports_variable_length_onion(&self) -> bool {
471                 <T as sealed::VariableLengthOnion>::supports_feature(&self.flags)
472         }
473 }
474
475 impl<T: sealed::StaticRemoteKey> Features<T> {
476         pub(crate) fn supports_static_remote_key(&self) -> bool {
477                 <T as sealed::StaticRemoteKey>::supports_feature(&self.flags)
478         }
479         #[cfg(test)]
480         pub(crate) fn requires_static_remote_key(&self) -> bool {
481                 <T as sealed::StaticRemoteKey>::requires_feature(&self.flags)
482         }
483 }
484
485 impl<T: sealed::InitialRoutingSync> Features<T> {
486         pub(crate) fn initial_routing_sync(&self) -> bool {
487                 <T as sealed::InitialRoutingSync>::supports_feature(&self.flags)
488         }
489         pub(crate) fn clear_initial_routing_sync(&mut self) {
490                 <T as sealed::InitialRoutingSync>::clear_bits(&mut self.flags)
491         }
492 }
493
494 impl<T: sealed::PaymentSecret> Features<T> {
495         #[cfg(test)]
496         pub(crate) fn requires_payment_secret(&self) -> bool {
497                 <T as sealed::PaymentSecret>::requires_feature(&self.flags)
498         }
499         // Note that we never need to test this since what really matters is the invoice - iff the
500         // invoice provides a payment_secret, we assume that we can use it (ie that the recipient
501         // supports payment_secret).
502         #[allow(dead_code)]
503         pub(crate) fn supports_payment_secret(&self) -> bool {
504                 <T as sealed::PaymentSecret>::supports_feature(&self.flags)
505         }
506 }
507
508 impl<T: sealed::BasicMPP> Features<T> {
509         #[cfg(test)]
510         pub(crate) fn requires_basic_mpp(&self) -> bool {
511                 <T as sealed::BasicMPP>::requires_feature(&self.flags)
512         }
513         // We currently never test for this since we don't actually *generate* multipath routes.
514         #[allow(dead_code)]
515         pub(crate) fn supports_basic_mpp(&self) -> bool {
516                 <T as sealed::BasicMPP>::supports_feature(&self.flags)
517         }
518 }
519
520 impl<T: sealed::Context> Writeable for Features<T> {
521         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
522                 w.size_hint(self.flags.len() + 2);
523                 (self.flags.len() as u16).write(w)?;
524                 for f in self.flags.iter().rev() { // Swap back to big-endian
525                         f.write(w)?;
526                 }
527                 Ok(())
528         }
529 }
530
531 impl<T: sealed::Context> Readable for Features<T> {
532         fn read<R: ::std::io::Read>(r: &mut R) -> Result<Self, DecodeError> {
533                 let mut flags: Vec<u8> = Readable::read(r)?;
534                 flags.reverse(); // Swap to little-endian
535                 Ok(Self {
536                         flags,
537                         mark: PhantomData,
538                 })
539         }
540 }
541
542 #[cfg(test)]
543 mod tests {
544         use super::{ChannelFeatures, InitFeatures, NodeFeatures};
545
546         #[test]
547         fn sanity_test_known_features() {
548                 assert!(!ChannelFeatures::known().requires_unknown_bits());
549                 assert!(!ChannelFeatures::known().supports_unknown_bits());
550                 assert!(!InitFeatures::known().requires_unknown_bits());
551                 assert!(!InitFeatures::known().supports_unknown_bits());
552                 assert!(!NodeFeatures::known().requires_unknown_bits());
553                 assert!(!NodeFeatures::known().supports_unknown_bits());
554
555                 assert!(InitFeatures::known().supports_upfront_shutdown_script());
556                 assert!(NodeFeatures::known().supports_upfront_shutdown_script());
557                 assert!(!InitFeatures::known().requires_upfront_shutdown_script());
558                 assert!(!NodeFeatures::known().requires_upfront_shutdown_script());
559
560                 assert!(InitFeatures::known().supports_data_loss_protect());
561                 assert!(NodeFeatures::known().supports_data_loss_protect());
562                 assert!(!InitFeatures::known().requires_data_loss_protect());
563                 assert!(!NodeFeatures::known().requires_data_loss_protect());
564
565                 assert!(InitFeatures::known().supports_variable_length_onion());
566                 assert!(NodeFeatures::known().supports_variable_length_onion());
567                 assert!(!InitFeatures::known().requires_variable_length_onion());
568                 assert!(!NodeFeatures::known().requires_variable_length_onion());
569
570                 assert!(!InitFeatures::known().supports_static_remote_key());
571                 assert!(!NodeFeatures::known().supports_static_remote_key());
572                 assert!(!InitFeatures::known().requires_static_remote_key());
573                 assert!(!NodeFeatures::known().requires_static_remote_key());
574
575                 assert!(InitFeatures::known().supports_payment_secret());
576                 assert!(NodeFeatures::known().supports_payment_secret());
577                 assert!(!InitFeatures::known().requires_payment_secret());
578                 assert!(!NodeFeatures::known().requires_payment_secret());
579
580                 assert!(InitFeatures::known().supports_basic_mpp());
581                 assert!(NodeFeatures::known().supports_basic_mpp());
582                 assert!(!InitFeatures::known().requires_basic_mpp());
583                 assert!(!NodeFeatures::known().requires_basic_mpp());
584
585                 let mut init_features = InitFeatures::known();
586                 assert!(init_features.initial_routing_sync());
587                 init_features.clear_initial_routing_sync();
588                 assert!(!init_features.initial_routing_sync());
589         }
590
591         #[test]
592         fn sanity_test_unknown_bits() {
593                 let mut features = ChannelFeatures::empty();
594                 assert!(!features.requires_unknown_bits());
595                 assert!(!features.supports_unknown_bits());
596
597                 features.set_required_unknown_bits();
598                 assert!(features.requires_unknown_bits());
599                 assert!(features.supports_unknown_bits());
600
601                 features.clear_unknown_bits();
602                 assert!(!features.requires_unknown_bits());
603                 assert!(!features.supports_unknown_bits());
604
605                 features.set_optional_unknown_bits();
606                 assert!(!features.requires_unknown_bits());
607                 assert!(features.supports_unknown_bits());
608         }
609
610         #[test]
611         fn convert_to_context_with_relevant_flags() {
612                 let init_features = InitFeatures::known().clear_upfront_shutdown_script();
613                 assert!(init_features.initial_routing_sync());
614                 assert!(!init_features.supports_upfront_shutdown_script());
615
616                 let node_features: NodeFeatures = init_features.to_context();
617                 {
618                         // Check that the flags are as expected:
619                         // - option_data_loss_protect
620                         // - var_onion_optin | payment_secret
621                         // - basic_mpp
622                         assert_eq!(node_features.flags.len(), 3);
623                         assert_eq!(node_features.flags[0], 0b00000010);
624                         assert_eq!(node_features.flags[1], 0b10000010);
625                         assert_eq!(node_features.flags[2], 0b00000010);
626                 }
627
628                 // Check that cleared flags are kept blank when converting back:
629                 // - initial_routing_sync was not applicable to NodeContext
630                 // - upfront_shutdown_script was cleared before converting
631                 let features: InitFeatures = node_features.to_context_internal();
632                 assert!(!features.initial_routing_sync());
633                 assert!(!features.supports_upfront_shutdown_script());
634         }
635 }