Skip to content

Commit 9ee73df

Browse files
committed
Move {REQUIRED,OPTIONAL}_FEATURES masks to module-level consts
.. which allows to DRY up their usage in several places.
1 parent 04fca74 commit 9ee73df

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

lightning/src/ln/features.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ mod sealed {
443443
set_unknown_feature_required, supports_unknown_test_feature, requires_unknown_test_feature);
444444
}
445445

446+
const ANY_REQUIRED_FEATURES_MASK: u8 = 0b01_01_01_01;
447+
const ANY_OPTIONAL_FEATURES_MASK: u8 = 0b10_10_10_10;
448+
446449
/// Tracks the set of features which a node implements, templated by the context in which it
447450
/// appears.
448451
///
@@ -616,8 +619,8 @@ impl ChannelTypeFeatures {
616619
// ChannelTypeFeatures must only contain required bits, so we OR the required forms of all
617620
// optional bits and then AND out the optional ones.
618621
for byte in ret.flags.iter_mut() {
619-
*byte |= (*byte & 0b10_10_10_10) >> 1;
620-
*byte &= 0b01_01_01_01;
622+
*byte |= (*byte & ANY_OPTIONAL_FEATURES_MASK) >> 1;
623+
*byte &= ANY_REQUIRED_FEATURES_MASK;
621624
}
622625
ret
623626
}
@@ -762,17 +765,16 @@ impl<T: sealed::Context> Features<T> {
762765
}
763766

764767
pub(crate) fn supports_any_optional_bits(&self) -> bool {
765-
self.flags.iter().any(|&byte| (byte & 0b10_10_10_10) != 0)
768+
self.flags.iter().any(|&byte| (byte & ANY_OPTIONAL_FEATURES_MASK) != 0)
766769
}
767770

768771
/// Returns true if this `Features` object contains required features unknown by `other`.
769772
pub fn requires_unknown_bits_from(&self, other: &Self) -> bool {
770773
// Bitwise AND-ing with all even bits set except for known features will select required
771774
// unknown features.
772775
self.flags.iter().enumerate().any(|(i, &byte)| {
773-
const REQUIRED_FEATURES: u8 = 0b01_01_01_01;
774776
let unknown_features = unset_features_mask_at_position(other, i);
775-
(byte & (REQUIRED_FEATURES & unknown_features)) != 0
777+
(byte & (ANY_REQUIRED_FEATURES_MASK & unknown_features)) != 0
776778
})
777779
}
778780

@@ -802,13 +804,12 @@ impl<T: sealed::Context> Features<T> {
802804
// unknown features.
803805
let byte_count = T::KNOWN_FEATURE_MASK.len();
804806
self.flags.iter().enumerate().any(|(i, &byte)| {
805-
let required_features = 0b01_01_01_01;
806807
let unknown_features = if i < byte_count {
807808
!T::KNOWN_FEATURE_MASK[i]
808809
} else {
809810
0b11_11_11_11
810811
};
811-
(byte & (required_features & unknown_features)) != 0
812+
(byte & (ANY_REQUIRED_FEATURES_MASK & unknown_features)) != 0
812813
})
813814
}
814815

@@ -1030,13 +1031,11 @@ impl<T: sealed::Context> Readable for WithoutLength<Features<T>> {
10301031
}
10311032

10321033
pub(crate) fn unset_features_mask_at_position<T: sealed::Context>(other: &Features<T>, index: usize) -> u8 {
1033-
const REQUIRED_FEATURES: u8 = 0b01_01_01_01;
1034-
const OPTIONAL_FEATURES: u8 = 0b10_10_10_10;
10351034
if index < other.flags.len() {
10361035
// Form a mask similar to !T::KNOWN_FEATURE_MASK only for `other`
10371036
!(other.flags[index]
1038-
| ((other.flags[index] >> 1) & REQUIRED_FEATURES)
1039-
| ((other.flags[index] << 1) & OPTIONAL_FEATURES))
1037+
| ((other.flags[index] >> 1) & ANY_REQUIRED_FEATURES_MASK)
1038+
| ((other.flags[index] << 1) & ANY_OPTIONAL_FEATURES_MASK))
10401039
} else {
10411040
0b11_11_11_11
10421041
}

0 commit comments

Comments
 (0)