From 446c007939089d2e9bfd3e055149c85204d841b4 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 9 Apr 2024 17:07:06 +0200 Subject: [PATCH] DRY up `unknown_features` calculation --- lightning/src/ln/features.rs | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/lightning/src/ln/features.rs b/lightning/src/ln/features.rs index 9dcac5788..6b4834edf 100644 --- a/lightning/src/ln/features.rs +++ b/lightning/src/ln/features.rs @@ -771,15 +771,7 @@ impl Features { // unknown features. self.flags.iter().enumerate().any(|(i, &byte)| { const REQUIRED_FEATURES: u8 = 0b01_01_01_01; - const OPTIONAL_FEATURES: u8 = 0b10_10_10_10; - let unknown_features = if i < other.flags.len() { - // Form a mask similar to !T::KNOWN_FEATURE_MASK only for `other` - !(other.flags[i] - | ((other.flags[i] >> 1) & REQUIRED_FEATURES) - | ((other.flags[i] << 1) & OPTIONAL_FEATURES)) - } else { - 0b11_11_11_11 - }; + let unknown_features = unset_features_mask_at_position(other, i); (byte & (REQUIRED_FEATURES & unknown_features)) != 0 }) } @@ -790,17 +782,7 @@ impl Features { // Bitwise AND-ing with all even bits set except for known features will select required // unknown features. self.flags.iter().enumerate().for_each(|(i, &byte)| { - const REQUIRED_FEATURES: u8 = 0b01_01_01_01; - const OPTIONAL_FEATURES: u8 = 0b10_10_10_10; - let unknown_features = if i < other.flags.len() { - // Form a mask similar to !T::KNOWN_FEATURE_MASK only for `other` - !(other.flags[i] - | ((other.flags[i] >> 1) & REQUIRED_FEATURES) - | ((other.flags[i] << 1) & OPTIONAL_FEATURES)) - } else { - 0b11_11_11_11 - }; - + let unknown_features = unset_features_mask_at_position(other, i); if byte & unknown_features != 0 { for bit in (0..8).step_by(2) { if ((byte & unknown_features) >> bit) & 1 == 1 { @@ -1047,6 +1029,19 @@ impl Readable for WithoutLength> { } } +pub(crate) fn unset_features_mask_at_position(other: &Features, index: usize) -> u8 { + const REQUIRED_FEATURES: u8 = 0b01_01_01_01; + const OPTIONAL_FEATURES: u8 = 0b10_10_10_10; + if index < other.flags.len() { + // Form a mask similar to !T::KNOWN_FEATURE_MASK only for `other` + !(other.flags[index] + | ((other.flags[index] >> 1) & REQUIRED_FEATURES) + | ((other.flags[index] << 1) & OPTIONAL_FEATURES)) + } else { + 0b11_11_11_11 + } +} + #[cfg(test)] mod tests { use super::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, Bolt11InvoiceFeatures, NodeFeatures, OfferFeatures, sealed}; -- 2.39.5