Skip to content

Commit be512c2

Browse files
Add methods to set features in Features objects.
1 parent 52edab7 commit be512c2

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

lightning/src/ln/features.rs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use ln::msgs::DecodeError;
3232
use util::ser::{Readable, Writeable, Writer};
3333

3434
mod sealed {
35+
use ln::features::Features;
3536
/// The context in which [`Features`] are applicable. Defines which features are required and
3637
/// which are optional for the context.
3738
///
@@ -157,7 +158,8 @@ mod sealed {
157158
///
158159
/// [`Context`]: trait.Context.html
159160
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) => {
161163
#[doc = $doc]
162164
///
163165
/// See [BOLT #9] for details.
@@ -242,6 +244,18 @@ mod sealed {
242244
}
243245
}
244246

247+
impl <T: $feature> Features<T> {
248+
/// Set this feature as supported.
249+
pub fn $optional_setter(&mut self) {
250+
<T as $feature>::set_optional_bit(&mut self.flags);
251+
}
252+
253+
/// Set this feature as required.
254+
pub fn $required_setter(&mut self) {
255+
<T as $feature>::set_required_bit(&mut self.flags);
256+
}
257+
}
258+
245259
$(
246260
impl $feature for $context {
247261
// EVEN_BIT % 2 == 0
@@ -251,28 +265,34 @@ mod sealed {
251265
const ASSERT_ODD_BIT_PARITY: usize = (<Self as $feature>::ODD_BIT % 2) - 1;
252266
}
253267
)*
268+
254269
}
255270
}
256271

257272
define_feature!(1, DataLossProtect, [InitContext, NodeContext],
258-
"Feature flags for `option_data_loss_protect`.");
273+
"Feature flags for `option_data_loss_protect`.", set_data_loss_protect_optional,
274+
set_data_loss_protect_required);
259275
// NOTE: Per Bolt #9, initial_routing_sync has no even bit.
260-
define_feature!(3, InitialRoutingSync, [InitContext],
261-
"Feature flags for `initial_routing_sync`.");
276+
define_feature!(3, InitialRoutingSync, [InitContext], "Feature flags for `initial_routing_sync`.",
277+
set_initial_routing_sync_optional, set_initial_routing_sync_required);
262278
define_feature!(5, UpfrontShutdownScript, [InitContext, NodeContext],
263-
"Feature flags for `option_upfront_shutdown_script`.");
279+
"Feature flags for `option_upfront_shutdown_script`.", set_upfront_shutdown_script_optional,
280+
set_upfront_shutdown_script_required);
264281
define_feature!(7, GossipQueries, [InitContext, NodeContext],
265-
"Feature flags for `gossip_queries`.");
282+
"Feature flags for `gossip_queries`.", set_gossip_queries_optional, set_gossip_queries_required);
266283
define_feature!(9, VariableLengthOnion, [InitContext, NodeContext, InvoiceContext],
267-
"Feature flags for `var_onion_optin`.");
284+
"Feature flags for `var_onion_optin`.", set_variable_length_onion_optional,
285+
set_variable_length_onion_required);
268286
define_feature!(13, StaticRemoteKey, [InitContext, NodeContext],
269-
"Feature flags for `option_static_remotekey`.");
287+
"Feature flags for `option_static_remotekey`.", set_static_remote_key_optional,
288+
set_static_remote_key_required);
270289
define_feature!(15, PaymentSecret, [InitContext, NodeContext, InvoiceContext],
271-
"Feature flags for `payment_secret`.");
290+
"Feature flags for `payment_secret`.", set_payment_secret_optional, set_payment_secret_required);
272291
define_feature!(17, BasicMPP, [InitContext, NodeContext, InvoiceContext],
273-
"Feature flags for `basic_mpp`.");
292+
"Feature flags for `basic_mpp`.", set_basic_mpp_optional, set_basic_mpp_required);
274293
define_feature!(27, ShutdownAnySegwit, [InitContext, NodeContext],
275-
"Feature flags for `opt_shutdown_anysegwit`.");
294+
"Feature flags for `opt_shutdown_anysegwit`.", set_shutdown_any_segwit_optional,
295+
set_shutdown_any_segwit_required);
276296

277297
#[cfg(test)]
278298
define_context!(TestingContext {
@@ -296,7 +316,8 @@ mod sealed {
296316

297317
#[cfg(test)]
298318
define_feature!(23, UnknownFeature, [TestingContext],
299-
"Feature flags for an unknown feature used in testing.");
319+
"Feature flags for an unknown feature used in testing.", set_unknown_feature_optional,
320+
set_unknown_feature_required);
300321
}
301322

302323
/// Tracks the set of features which a node implements, templated by the context in which it
@@ -614,7 +635,7 @@ impl<T: sealed::Context> Readable for Features<T> {
614635

615636
#[cfg(test)]
616637
mod tests {
617-
use super::{ChannelFeatures, InitFeatures, NodeFeatures};
638+
use super::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
618639

619640
#[test]
620641
fn sanity_test_known_features() {
@@ -718,4 +739,13 @@ mod tests {
718739
assert!(!features.supports_upfront_shutdown_script());
719740
assert!(!init_features.supports_gossip_queries());
720741
}
742+
743+
#[test]
744+
fn set_feature_bits() {
745+
let mut features = InvoiceFeatures::empty();
746+
features.set_basic_mpp_optional();
747+
assert!(features.supports_basic_mpp());
748+
features.set_payment_secret_required();
749+
assert!(features.requires_payment_secret());
750+
}
721751
}

0 commit comments

Comments
 (0)