Skip to content

Commit cd9cd47

Browse files
authored
Merge pull request #1205 from TheBlueMatt/2021-12-new-feature-bit
Support the channel_type feature bit.
2 parents 3ec529d + 8f4a22f commit cd9cd47

File tree

1 file changed

+56
-88
lines changed

1 file changed

+56
-88
lines changed

lightning/src/ln/features.rs

Lines changed: 56 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ mod sealed {
126126
,
127127
// Byte 3
128128
,
129+
// Byte 4
130+
,
131+
// Byte 5
132+
,
129133
],
130134
optional_features: [
131135
// Byte 0
@@ -136,6 +140,10 @@ mod sealed {
136140
BasicMPP,
137141
// Byte 3
138142
ShutdownAnySegwit,
143+
// Byte 4
144+
,
145+
// Byte 5
146+
ChannelType,
139147
],
140148
});
141149
define_context!(NodeContext {
@@ -167,7 +175,7 @@ mod sealed {
167175
// Byte 4
168176
,
169177
// Byte 5
170-
,
178+
ChannelType,
171179
// Byte 6
172180
Keysend,
173181
],
@@ -223,7 +231,7 @@ mod sealed {
223231
/// useful for manipulating feature flags.
224232
macro_rules! define_feature {
225233
($odd_bit: expr, $feature: ident, [$($context: ty),+], $doc: expr, $optional_setter: ident,
226-
$required_setter: ident) => {
234+
$required_setter: ident, $supported_getter: ident) => {
227235
#[doc = $doc]
228236
///
229237
/// See [BOLT #9] for details.
@@ -320,6 +328,11 @@ mod sealed {
320328
<T as $feature>::set_required_bit(&mut self.flags);
321329
self
322330
}
331+
332+
/// Checks if this feature is supported.
333+
pub fn $supported_getter(&self) -> bool {
334+
<T as $feature>::supports_feature(&self.flags)
335+
}
323336
}
324337

325338
$(
@@ -331,41 +344,60 @@ mod sealed {
331344
const ASSERT_ODD_BIT_PARITY: usize = (<Self as $feature>::ODD_BIT % 2) - 1;
332345
}
333346
)*
334-
347+
};
348+
($odd_bit: expr, $feature: ident, [$($context: ty),+], $doc: expr, $optional_setter: ident,
349+
$required_setter: ident, $supported_getter: ident, $required_getter: ident) => {
350+
define_feature!($odd_bit, $feature, [$($context),+], $doc, $optional_setter, $required_setter, $supported_getter);
351+
impl <T: $feature> Features<T> {
352+
/// Checks if this feature is required.
353+
pub fn $required_getter(&self) -> bool {
354+
<T as $feature>::requires_feature(&self.flags)
355+
}
356+
}
335357
}
336358
}
337359

338360
define_feature!(1, DataLossProtect, [InitContext, NodeContext],
339361
"Feature flags for `option_data_loss_protect`.", set_data_loss_protect_optional,
340-
set_data_loss_protect_required);
362+
set_data_loss_protect_required, supports_data_loss_protect, requires_data_loss_protect);
341363
// NOTE: Per Bolt #9, initial_routing_sync has no even bit.
342364
define_feature!(3, InitialRoutingSync, [InitContext], "Feature flags for `initial_routing_sync`.",
343-
set_initial_routing_sync_optional, set_initial_routing_sync_required);
365+
set_initial_routing_sync_optional, set_initial_routing_sync_required,
366+
initial_routing_sync);
344367
define_feature!(5, UpfrontShutdownScript, [InitContext, NodeContext],
345368
"Feature flags for `option_upfront_shutdown_script`.", set_upfront_shutdown_script_optional,
346-
set_upfront_shutdown_script_required);
369+
set_upfront_shutdown_script_required, supports_upfront_shutdown_script,
370+
requires_upfront_shutdown_script);
347371
define_feature!(7, GossipQueries, [InitContext, NodeContext],
348-
"Feature flags for `gossip_queries`.", set_gossip_queries_optional, set_gossip_queries_required);
372+
"Feature flags for `gossip_queries`.", set_gossip_queries_optional, set_gossip_queries_required,
373+
supports_gossip_queries, requires_gossip_queries);
349374
define_feature!(9, VariableLengthOnion, [InitContext, NodeContext, InvoiceContext],
350375
"Feature flags for `var_onion_optin`.", set_variable_length_onion_optional,
351-
set_variable_length_onion_required);
376+
set_variable_length_onion_required, supports_variable_length_onion,
377+
requires_variable_length_onion);
352378
define_feature!(13, StaticRemoteKey, [InitContext, NodeContext, ChannelTypeContext],
353379
"Feature flags for `option_static_remotekey`.", set_static_remote_key_optional,
354-
set_static_remote_key_required);
380+
set_static_remote_key_required, supports_static_remote_key, requires_static_remote_key);
355381
define_feature!(15, PaymentSecret, [InitContext, NodeContext, InvoiceContext],
356-
"Feature flags for `payment_secret`.", set_payment_secret_optional, set_payment_secret_required);
382+
"Feature flags for `payment_secret`.", set_payment_secret_optional, set_payment_secret_required,
383+
supports_payment_secret, requires_payment_secret);
357384
define_feature!(17, BasicMPP, [InitContext, NodeContext, InvoiceContext],
358-
"Feature flags for `basic_mpp`.", set_basic_mpp_optional, set_basic_mpp_required);
385+
"Feature flags for `basic_mpp`.", set_basic_mpp_optional, set_basic_mpp_required,
386+
supports_basic_mpp, requires_basic_mpp);
359387
define_feature!(27, ShutdownAnySegwit, [InitContext, NodeContext],
360388
"Feature flags for `opt_shutdown_anysegwit`.", set_shutdown_any_segwit_optional,
361-
set_shutdown_any_segwit_required);
389+
set_shutdown_any_segwit_required, supports_shutdown_anysegwit, requires_shutdown_anysegwit);
390+
define_feature!(45, ChannelType, [InitContext, NodeContext],
391+
"Feature flags for `option_channel_type`.", set_channel_type_optional,
392+
set_channel_type_required, supports_channel_type, requires_channel_type);
362393
define_feature!(55, Keysend, [NodeContext],
363-
"Feature flags for keysend payments.", set_keysend_optional, set_keysend_required);
394+
"Feature flags for keysend payments.", set_keysend_optional, set_keysend_required,
395+
supports_keysend, requires_keysend);
364396

365397
#[cfg(test)]
366398
define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext, InvoiceContext],
367399
"Feature flags for an unknown feature used in testing.", set_unknown_feature_optional,
368-
set_unknown_feature_required);
400+
set_unknown_feature_required, supports_unknown_test_feature, requires_unknown_test_feature);
369401
}
370402

371403
/// Tracks the set of features which a node implements, templated by the context in which it
@@ -662,25 +694,7 @@ impl<T: sealed::Context> Features<T> {
662694
}
663695
}
664696

665-
impl<T: sealed::DataLossProtect> Features<T> {
666-
#[cfg(test)]
667-
pub(crate) fn requires_data_loss_protect(&self) -> bool {
668-
<T as sealed::DataLossProtect>::requires_feature(&self.flags)
669-
}
670-
#[cfg(test)]
671-
pub(crate) fn supports_data_loss_protect(&self) -> bool {
672-
<T as sealed::DataLossProtect>::supports_feature(&self.flags)
673-
}
674-
}
675-
676697
impl<T: sealed::UpfrontShutdownScript> Features<T> {
677-
#[cfg(test)]
678-
pub(crate) fn requires_upfront_shutdown_script(&self) -> bool {
679-
<T as sealed::UpfrontShutdownScript>::requires_feature(&self.flags)
680-
}
681-
pub(crate) fn supports_upfront_shutdown_script(&self) -> bool {
682-
<T as sealed::UpfrontShutdownScript>::supports_feature(&self.flags)
683-
}
684698
#[cfg(test)]
685699
pub(crate) fn clear_upfront_shutdown_script(mut self) -> Self {
686700
<T as sealed::UpfrontShutdownScript>::clear_bits(&mut self.flags);
@@ -690,44 +704,14 @@ impl<T: sealed::UpfrontShutdownScript> Features<T> {
690704

691705

692706
impl<T: sealed::GossipQueries> Features<T> {
693-
#[cfg(test)]
694-
pub(crate) fn requires_gossip_queries(&self) -> bool {
695-
<T as sealed::GossipQueries>::requires_feature(&self.flags)
696-
}
697-
pub(crate) fn supports_gossip_queries(&self) -> bool {
698-
<T as sealed::GossipQueries>::supports_feature(&self.flags)
699-
}
700707
#[cfg(test)]
701708
pub(crate) fn clear_gossip_queries(mut self) -> Self {
702709
<T as sealed::GossipQueries>::clear_bits(&mut self.flags);
703710
self
704711
}
705712
}
706713

707-
impl<T: sealed::VariableLengthOnion> Features<T> {
708-
#[cfg(test)]
709-
pub(crate) fn requires_variable_length_onion(&self) -> bool {
710-
<T as sealed::VariableLengthOnion>::requires_feature(&self.flags)
711-
}
712-
pub(crate) fn supports_variable_length_onion(&self) -> bool {
713-
<T as sealed::VariableLengthOnion>::supports_feature(&self.flags)
714-
}
715-
}
716-
717-
impl<T: sealed::StaticRemoteKey> Features<T> {
718-
pub(crate) fn supports_static_remote_key(&self) -> bool {
719-
<T as sealed::StaticRemoteKey>::supports_feature(&self.flags)
720-
}
721-
#[cfg(test)]
722-
pub(crate) fn requires_static_remote_key(&self) -> bool {
723-
<T as sealed::StaticRemoteKey>::requires_feature(&self.flags)
724-
}
725-
}
726-
727714
impl<T: sealed::InitialRoutingSync> Features<T> {
728-
pub(crate) fn initial_routing_sync(&self) -> bool {
729-
<T as sealed::InitialRoutingSync>::supports_feature(&self.flags)
730-
}
731715
// We are no longer setting initial_routing_sync now that gossip_queries
732716
// is enabled. This feature is ignored by a peer when gossip_queries has
733717
// been negotiated.
@@ -737,32 +721,7 @@ impl<T: sealed::InitialRoutingSync> Features<T> {
737721
}
738722
}
739723

740-
impl<T: sealed::PaymentSecret> Features<T> {
741-
#[cfg(test)]
742-
pub(crate) fn requires_payment_secret(&self) -> bool {
743-
<T as sealed::PaymentSecret>::requires_feature(&self.flags)
744-
}
745-
/// Returns whether the `payment_secret` feature is supported.
746-
pub fn supports_payment_secret(&self) -> bool {
747-
<T as sealed::PaymentSecret>::supports_feature(&self.flags)
748-
}
749-
}
750-
751-
impl<T: sealed::BasicMPP> Features<T> {
752-
#[cfg(test)]
753-
pub(crate) fn requires_basic_mpp(&self) -> bool {
754-
<T as sealed::BasicMPP>::requires_feature(&self.flags)
755-
}
756-
// We currently never test for this since we don't actually *generate* multipath routes.
757-
pub(crate) fn supports_basic_mpp(&self) -> bool {
758-
<T as sealed::BasicMPP>::supports_feature(&self.flags)
759-
}
760-
}
761-
762724
impl<T: sealed::ShutdownAnySegwit> Features<T> {
763-
pub(crate) fn supports_shutdown_anysegwit(&self) -> bool {
764-
<T as sealed::ShutdownAnySegwit>::supports_feature(&self.flags)
765-
}
766725
#[cfg(test)]
767726
pub(crate) fn clear_shutdown_anysegwit(mut self) -> Self {
768727
<T as sealed::ShutdownAnySegwit>::clear_bits(&mut self.flags);
@@ -859,6 +818,11 @@ mod tests {
859818
assert!(!NodeFeatures::known().requires_basic_mpp());
860819
assert!(!InvoiceFeatures::known().requires_basic_mpp());
861820

821+
assert!(InitFeatures::known().supports_channel_type());
822+
assert!(NodeFeatures::known().supports_channel_type());
823+
assert!(!InitFeatures::known().requires_channel_type());
824+
assert!(!NodeFeatures::known().requires_channel_type());
825+
862826
assert!(InitFeatures::known().supports_shutdown_anysegwit());
863827
assert!(NodeFeatures::known().supports_shutdown_anysegwit());
864828

@@ -897,11 +861,15 @@ mod tests {
897861
// - var_onion_optin (req) | static_remote_key (req) | payment_secret(req)
898862
// - basic_mpp
899863
// - opt_shutdown_anysegwit
900-
assert_eq!(node_features.flags.len(), 4);
864+
// -
865+
// - option_channel_type
866+
assert_eq!(node_features.flags.len(), 6);
901867
assert_eq!(node_features.flags[0], 0b00000010);
902868
assert_eq!(node_features.flags[1], 0b01010001);
903869
assert_eq!(node_features.flags[2], 0b00000010);
904870
assert_eq!(node_features.flags[3], 0b00001000);
871+
assert_eq!(node_features.flags[4], 0b00000000);
872+
assert_eq!(node_features.flags[5], 0b00100000);
905873
}
906874

907875
// Check that cleared flags are kept blank when converting back:

0 commit comments

Comments
 (0)