Macroize feature printing to ensure we don't miss new flags
[rust-lightning] / lightning / src / ln / features.rs
index a9f5a10ee2dad14395e18eda253cbaba2943106c..b90748aa87b7e5cd55bf0461244fd2c654feb7eb 100644 (file)
@@ -89,6 +89,28 @@ mod sealed {
                                        )*
                                ];
                        }
+
+                       impl alloc::fmt::Display for Features<$context> {
+                               fn fmt(&self, fmt: &mut alloc::fmt::Formatter) -> Result<(), alloc::fmt::Error> {
+                                       $(
+                                               $(
+                                                       fmt.write_fmt(format_args!("{}: {}, ", stringify!($required_feature),
+                                                               if <$context as $required_feature>::requires_feature(&self.flags) { "required" }
+                                                               else if <$context as $required_feature>::supports_feature(&self.flags) { "supported" }
+                                                               else { "not supported" }))?;
+                                               )*
+                                               $(
+                                                       fmt.write_fmt(format_args!("{}: {}, ", stringify!($optional_feature),
+                                                               if <$context as $optional_feature>::requires_feature(&self.flags) { "required" }
+                                                               else if <$context as $optional_feature>::supports_feature(&self.flags) { "supported" }
+                                                               else { "not supported" }))?;
+                                               )*
+                                       )*
+                                       fmt.write_fmt(format_args!("unknown flags: {}",
+                                               if self.requires_unknown_bits() { "required" }
+                                               else if self.supports_unknown_bits() { "supported" } else { "none" }))
+                               }
+                       }
                };
        }
 
@@ -124,6 +146,12 @@ mod sealed {
                        ,
                        // Byte 3
                        ,
+                       // Byte 4
+                       ,
+                       // Byte 5
+                       ,
+                       // Byte 6
+                       ,
                ],
                optional_features: [
                        // Byte 0
@@ -134,6 +162,12 @@ mod sealed {
                        BasicMPP,
                        // Byte 3
                        ShutdownAnySegwit,
+                       // Byte 4
+                       ,
+                       // Byte 5
+                       ,
+                       // Byte 6
+                       Keysend,
                ],
        });
        define_context!(ChannelContext {
@@ -299,9 +333,11 @@ mod sealed {
        define_feature!(27, ShutdownAnySegwit, [InitContext, NodeContext],
                "Feature flags for `opt_shutdown_anysegwit`.", set_shutdown_any_segwit_optional,
                set_shutdown_any_segwit_required);
+       define_feature!(55, Keysend, [NodeContext],
+               "Feature flags for keysend payments.", set_keysend_optional, set_keysend_required);
 
        #[cfg(test)]
-       define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext],
+       define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext, InvoiceContext],
                "Feature flags for an unknown feature used in testing.", set_unknown_feature_optional,
                set_unknown_feature_required);
 }
@@ -386,6 +422,18 @@ impl InvoiceFeatures {
        pub(crate) fn to_context<C: sealed::Context>(&self) -> Features<C> {
                self.to_context_internal()
        }
+
+       /// Getting a route for a keysend payment to a private node requires providing the payee's
+       /// features (since they were not announced in a node announcement). However, keysend payments
+       /// don't have an invoice to pull the payee's features from, so this method is provided for use in
+       /// [`get_keysend_route`], thus omitting the need for payers to manually construct an
+       /// `InvoiceFeatures` for [`get_route`].
+       ///
+       /// [`get_keysend_route`]: crate::routing::router::get_keysend_route
+       /// [`get_route`]: crate::routing::router::get_route
+       pub(crate) fn for_keysend() -> InvoiceFeatures {
+               InvoiceFeatures::empty().set_variable_length_onion_optional()
+       }
 }
 
 impl ToBase32 for InvoiceFeatures {
@@ -540,6 +588,7 @@ impl<T: sealed::DataLossProtect> Features<T> {
        pub(crate) fn requires_data_loss_protect(&self) -> bool {
                <T as sealed::DataLossProtect>::requires_feature(&self.flags)
        }
+       #[cfg(test)]
        pub(crate) fn supports_data_loss_protect(&self) -> bool {
                <T as sealed::DataLossProtect>::supports_feature(&self.flags)
        }
@@ -774,6 +823,16 @@ mod tests {
                assert!(!init_features.supports_gossip_queries());
        }
 
+       #[test]
+       fn convert_to_context_with_unknown_flags() {
+               // Ensure the `from` context has fewer known feature bytes than the `to` context.
+               assert!(InvoiceFeatures::known().byte_count() < NodeFeatures::known().byte_count());
+               let invoice_features = InvoiceFeatures::known().set_unknown_feature_optional();
+               assert!(invoice_features.supports_unknown_bits());
+               let node_features: NodeFeatures = invoice_features.to_context();
+               assert!(!node_features.supports_unknown_bits());
+       }
+
        #[test]
        fn set_feature_bits() {
                let features = InvoiceFeatures::empty()