@@ -272,19 +272,19 @@ impl<T: sealed::Context> Features<T> {
272272 }
273273 }
274274
275- /// Takes the flags that we know how to interpret in an init-context features that are also
276- /// relevant in a node-context features and creates a node-context features from them.
277- /// Be sure to blank out features that are unknown to us.
278- pub ( crate ) fn with_known_relevant_init_flags ( init_ctx : & InitFeatures ) -> Self {
279- let byte_count = T :: KNOWN_FEATURE_MASK . len ( ) ;
275+ /// Converts `Features<T>` to `Features<C>`. Only known `T` features relevant to `C` are
276+ /// included in the result.
277+ pub ( crate ) fn to_context < C : sealed:: Context > ( & self ) -> Features < C > {
278+ let byte_count = C :: KNOWN_FEATURE_MASK . len ( ) ;
280279 let mut flags = Vec :: new ( ) ;
281- for ( i, feature_byte ) in init_ctx . flags . iter ( ) . enumerate ( ) {
280+ for ( i, byte ) in self . flags . iter ( ) . enumerate ( ) {
282281 if i < byte_count {
283- let known_features = T :: KNOWN_FEATURE_MASK [ i] ;
284- flags. push ( feature_byte & known_features) ;
282+ let known_source_features = T :: KNOWN_FEATURE_MASK [ i] ;
283+ let known_target_features = C :: KNOWN_FEATURE_MASK [ i] ;
284+ flags. push ( byte & known_source_features & known_target_features) ;
285285 }
286286 }
287- Self { flags, mark : PhantomData , }
287+ Features :: < C > { flags, mark : PhantomData , }
288288 }
289289
290290 #[ cfg( test) ]
@@ -369,8 +369,9 @@ impl<T: sealed::UpfrontShutdownScript> Features<T> {
369369 <T as sealed:: UpfrontShutdownScript >:: supports_feature ( & self . flags )
370370 }
371371 #[ cfg( test) ]
372- pub ( crate ) fn unset_upfront_shutdown_script ( & mut self ) {
373- <T as sealed:: UpfrontShutdownScript >:: clear_bits ( & mut self . flags )
372+ pub ( crate ) fn clear_upfront_shutdown_script ( mut self ) -> Self {
373+ <T as sealed:: UpfrontShutdownScript >:: clear_bits ( & mut self . flags ) ;
374+ self
374375 }
375376}
376377
@@ -431,7 +432,7 @@ impl<T: sealed::Context> Readable for Features<T> {
431432
432433#[ cfg( test) ]
433434mod tests {
434- use super :: { ChannelFeatures , InitFeatures , NodeFeatures , Features } ;
435+ use super :: { ChannelFeatures , InitFeatures , NodeFeatures } ;
435436
436437 #[ test]
437438 fn sanity_test_our_features ( ) {
@@ -470,26 +471,26 @@ mod tests {
470471 }
471472
472473 #[ test]
473- fn test_node_with_known_relevant_init_flags ( ) {
474- // Create an InitFeatures with initial_routing_sync supported.
475- let init_features = InitFeatures :: known ( ) ;
474+ fn convert_to_context_with_relevant_flags ( ) {
475+ let init_features = InitFeatures :: known ( ) . clear_upfront_shutdown_script ( ) ;
476476 assert ! ( init_features. initial_routing_sync( ) ) ;
477+ assert ! ( !init_features. supports_upfront_shutdown_script( ) ) ;
477478
478- // Attempt to pull out non-node-context feature flags from these InitFeatures.
479- let res = NodeFeatures :: with_known_relevant_init_flags ( & init_features) ;
480-
479+ let node_features: NodeFeatures = init_features. to_context ( ) ;
481480 {
482- // Check that the flags are as expected: optional_data_loss_protect,
483- // option_upfront_shutdown_script, var_onion_optin, payment_secret, and
484- // basic_mpp.
485- assert_eq ! ( res. flags. len( ) , 3 ) ;
486- assert_eq ! ( res. flags[ 0 ] , 0b00100010 ) ;
487- assert_eq ! ( res. flags[ 1 ] , 0b10000010 ) ;
488- assert_eq ! ( res. flags[ 2 ] , 0b00000010 ) ;
481+ // Check that the flags are as expected:
482+ // - option_data_loss_protect
483+ // - var_onion_optin | payment_secret
484+ // - basic_mpp
485+ assert_eq ! ( node_features. flags. len( ) , 3 ) ;
486+ assert_eq ! ( node_features. flags[ 0 ] , 0b00000010 ) ;
487+ assert_eq ! ( node_features. flags[ 1 ] , 0b10000010 ) ;
488+ assert_eq ! ( node_features. flags[ 2 ] , 0b00000010 ) ;
489489 }
490490
491- // Check that the initial_routing_sync feature was correctly blanked out.
492- let new_features: InitFeatures = Features :: from_le_bytes ( res. flags ) ;
493- assert ! ( !new_features. initial_routing_sync( ) ) ;
491+ // Check that cleared flags are kept blank when converting back.
492+ let features: InitFeatures = node_features. to_context ( ) ;
493+ assert ! ( !features. initial_routing_sync( ) ) ;
494+ assert ! ( !features. supports_upfront_shutdown_script( ) ) ;
494495 }
495496}
0 commit comments