@@ -32,6 +32,7 @@ use ln::msgs::DecodeError;
32
32
use util:: ser:: { Readable , Writeable , Writer } ;
33
33
34
34
mod sealed {
35
+ use ln:: features:: Features ;
35
36
/// The context in which [`Features`] are applicable. Defines which features are required and
36
37
/// which are optional for the context.
37
38
///
@@ -157,7 +158,8 @@ mod sealed {
157
158
///
158
159
/// [`Context`]: trait.Context.html
159
160
macro_rules! define_feature {
160
- ( $odd_bit: expr, $feature: ident, [ $( $context: ty) ,+] , $doc: expr) => {
161
+ ( $odd_bit: expr, $feature: ident, [ $( $context: ty) ,+] , $doc: expr, $optional_setter: ident,
162
+ $required_setter: ident) => {
161
163
#[ doc = $doc]
162
164
///
163
165
/// See [BOLT #9] for details.
@@ -242,6 +244,20 @@ mod sealed {
242
244
}
243
245
}
244
246
247
+ impl <T : $feature> Features <T > {
248
+ /// Set this feature as optional.
249
+ pub fn $optional_setter( mut self ) -> Self {
250
+ <T as $feature>:: set_optional_bit( & mut self . flags) ;
251
+ self
252
+ }
253
+
254
+ /// Set this feature as required.
255
+ pub fn $required_setter( mut self ) -> Self {
256
+ <T as $feature>:: set_required_bit( & mut self . flags) ;
257
+ self
258
+ }
259
+ }
260
+
245
261
$(
246
262
impl $feature for $context {
247
263
// EVEN_BIT % 2 == 0
@@ -251,28 +267,34 @@ mod sealed {
251
267
const ASSERT_ODD_BIT_PARITY : usize = ( <Self as $feature>:: ODD_BIT % 2 ) - 1 ;
252
268
}
253
269
) *
270
+
254
271
}
255
272
}
256
273
257
274
define_feature ! ( 1 , DataLossProtect , [ InitContext , NodeContext ] ,
258
- "Feature flags for `option_data_loss_protect`." ) ;
275
+ "Feature flags for `option_data_loss_protect`." , set_data_loss_protect_optional,
276
+ set_data_loss_protect_required) ;
259
277
// NOTE: Per Bolt #9, initial_routing_sync has no even bit.
260
- define_feature ! ( 3 , InitialRoutingSync , [ InitContext ] ,
261
- "Feature flags for `initial_routing_sync`." ) ;
278
+ define_feature ! ( 3 , InitialRoutingSync , [ InitContext ] , "Feature flags for `initial_routing_sync`." ,
279
+ set_initial_routing_sync_optional , set_initial_routing_sync_required ) ;
262
280
define_feature ! ( 5 , UpfrontShutdownScript , [ InitContext , NodeContext ] ,
263
- "Feature flags for `option_upfront_shutdown_script`." ) ;
281
+ "Feature flags for `option_upfront_shutdown_script`." , set_upfront_shutdown_script_optional,
282
+ set_upfront_shutdown_script_required) ;
264
283
define_feature ! ( 7 , GossipQueries , [ InitContext , NodeContext ] ,
265
- "Feature flags for `gossip_queries`." ) ;
284
+ "Feature flags for `gossip_queries`." , set_gossip_queries_optional , set_gossip_queries_required ) ;
266
285
define_feature ! ( 9 , VariableLengthOnion , [ InitContext , NodeContext , InvoiceContext ] ,
267
- "Feature flags for `var_onion_optin`." ) ;
286
+ "Feature flags for `var_onion_optin`." , set_variable_length_onion_optional,
287
+ set_variable_length_onion_required) ;
268
288
define_feature ! ( 13 , StaticRemoteKey , [ InitContext , NodeContext ] ,
269
- "Feature flags for `option_static_remotekey`." ) ;
289
+ "Feature flags for `option_static_remotekey`." , set_static_remote_key_optional,
290
+ set_static_remote_key_required) ;
270
291
define_feature ! ( 15 , PaymentSecret , [ InitContext , NodeContext , InvoiceContext ] ,
271
- "Feature flags for `payment_secret`." ) ;
292
+ "Feature flags for `payment_secret`." , set_payment_secret_optional , set_payment_secret_required ) ;
272
293
define_feature ! ( 17 , BasicMPP , [ InitContext , NodeContext , InvoiceContext ] ,
273
- "Feature flags for `basic_mpp`." ) ;
294
+ "Feature flags for `basic_mpp`." , set_basic_mpp_optional , set_basic_mpp_required ) ;
274
295
define_feature ! ( 27 , ShutdownAnySegwit , [ InitContext , NodeContext ] ,
275
- "Feature flags for `opt_shutdown_anysegwit`." ) ;
296
+ "Feature flags for `opt_shutdown_anysegwit`." , set_shutdown_any_segwit_optional,
297
+ set_shutdown_any_segwit_required) ;
276
298
277
299
#[ cfg( test) ]
278
300
define_context ! ( TestingContext {
@@ -296,7 +318,8 @@ mod sealed {
296
318
297
319
#[ cfg( test) ]
298
320
define_feature ! ( 23 , UnknownFeature , [ TestingContext ] ,
299
- "Feature flags for an unknown feature used in testing." ) ;
321
+ "Feature flags for an unknown feature used in testing." , set_unknown_feature_optional,
322
+ set_unknown_feature_required) ;
300
323
}
301
324
302
325
/// Tracks the set of features which a node implements, templated by the context in which it
@@ -614,7 +637,7 @@ impl<T: sealed::Context> Readable for Features<T> {
614
637
615
638
#[ cfg( test) ]
616
639
mod tests {
617
- use super :: { ChannelFeatures , InitFeatures , NodeFeatures } ;
640
+ use super :: { ChannelFeatures , InitFeatures , InvoiceFeatures , NodeFeatures } ;
618
641
619
642
#[ test]
620
643
fn sanity_test_known_features ( ) {
@@ -718,4 +741,13 @@ mod tests {
718
741
assert ! ( !features. supports_upfront_shutdown_script( ) ) ;
719
742
assert ! ( !init_features. supports_gossip_queries( ) ) ;
720
743
}
744
+
745
+ #[ test]
746
+ fn set_feature_bits ( ) {
747
+ let features = InvoiceFeatures :: empty ( )
748
+ . set_basic_mpp_optional ( )
749
+ . set_payment_secret_required ( ) ;
750
+ assert ! ( features. supports_basic_mpp( ) ) ;
751
+ assert ! ( features. requires_payment_secret( ) ) ;
752
+ }
721
753
}
0 commit comments