From: Jeffrey Czyz Date: Thu, 16 Apr 2020 05:44:43 +0000 (-0700) Subject: Generalize feature methods to work in any context X-Git-Tag: v0.0.12~69^2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=65ff1bf0beaeb33397c5024d278a65a780fc8ffd;p=rust-lightning Generalize feature methods to work in any context Refactoring the features module allowed for making code specific to certain contexts generalizable. Specifically, KNOWN_FEATURE_MASK is defined on Context instead of hardcoded in each method specialization. Thus, such methods are no longer required. --- diff --git a/lightning/src/ln/features.rs b/lightning/src/ln/features.rs index fc2526fe5..0dd5fa304 100644 --- a/lightning/src/ln/features.rs +++ b/lightning/src/ln/features.rs @@ -284,33 +284,6 @@ impl InitFeatures { } } -impl ChannelFeatures { - /// Takes the flags that we know how to interpret in an init-context features that are also - /// relevant in a channel-context features and creates a channel-context features from them. - pub(crate) fn with_known_relevant_init_flags(_init_ctx: &InitFeatures) -> Self { - // There are currently no channel flags defined that we understand. - Self { flags: Vec::new(), mark: PhantomData, } - } -} - -impl NodeFeatures { - /// Takes the flags that we know how to interpret in an init-context features that are also - /// relevant in a node-context features and creates a node-context features from them. - /// Be sure to blank out features that are unknown to us. - pub(crate) fn with_known_relevant_init_flags(init_ctx: &InitFeatures) -> Self { - use ln::features::sealed::Context; - let byte_count = sealed::NodeContext::KNOWN_FEATURE_MASK.len(); - - let mut flags = Vec::new(); - for (i, feature_byte) in init_ctx.flags.iter().enumerate() { - if i < byte_count { - flags.push(feature_byte & sealed::NodeContext::KNOWN_FEATURE_MASK[i]); - } - } - Self { flags, mark: PhantomData, } - } -} - impl Features { /// Create a blank Features with no features set pub fn empty() -> Features { @@ -330,6 +303,20 @@ impl Features { } } + /// Takes the flags that we know how to interpret in an init-context features that are also + /// relevant in a node-context features and creates a node-context features from them. + /// Be sure to blank out features that are unknown to us. + pub(crate) fn with_known_relevant_init_flags(init_ctx: &InitFeatures) -> Self { + let byte_count = T::KNOWN_FEATURE_MASK.len(); + let mut flags = Vec::new(); + for (i, feature_byte) in init_ctx.flags.iter().enumerate() { + if i < byte_count { + flags.push(feature_byte & T::KNOWN_FEATURE_MASK[i]); + } + } + Self { flags, mark: PhantomData, } + } + #[cfg(test)] /// Create a Features given a set of flags, in LE. pub fn from_le_bytes(flags: Vec) -> Features { @@ -346,15 +333,13 @@ impl Features { } pub(crate) fn requires_unknown_bits(&self) -> bool { - use ln::features::sealed::Context; - let byte_count = sealed::InitContext::KNOWN_FEATURE_MASK.len(); - - // Bitwise AND-ing with all even bits set except for known features will select unknown - // required features. + // Bitwise AND-ing with all even bits set except for known features will select required + // unknown features. + let byte_count = T::KNOWN_FEATURE_MASK.len(); self.flags.iter().enumerate().any(|(i, &byte)| { let required_features = 0b01_01_01_01; let unknown_features = if i < byte_count { - !sealed::InitContext::KNOWN_FEATURE_MASK[i] + !T::KNOWN_FEATURE_MASK[i] } else { 0b11_11_11_11 }; @@ -363,14 +348,12 @@ impl Features { } pub(crate) fn supports_unknown_bits(&self) -> bool { - use ln::features::sealed::Context; - let byte_count = sealed::InitContext::KNOWN_FEATURE_MASK.len(); - // Bitwise AND-ing with all even and odd bits set except for known features will select - // unknown features. + // both required and optional unknown features. + let byte_count = T::KNOWN_FEATURE_MASK.len(); self.flags.iter().enumerate().any(|(i, &byte)| { let unknown_features = if i < byte_count { - !sealed::InitContext::KNOWN_FEATURE_MASK[i] + !T::KNOWN_FEATURE_MASK[i] } else { 0b11_11_11_11 };