Skip to content

Commit c472b30

Browse files
committed
DRY up unknown_features calculation
1 parent a62dbfd commit c472b30

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

lightning/src/ln/features.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -771,15 +771,7 @@ impl<T: sealed::Context> Features<T> {
771771
// unknown features.
772772
self.flags.iter().enumerate().any(|(i, &byte)| {
773773
const REQUIRED_FEATURES: u8 = 0b01_01_01_01;
774-
const OPTIONAL_FEATURES: u8 = 0b10_10_10_10;
775-
let unknown_features = if i < other.flags.len() {
776-
// Form a mask similar to !T::KNOWN_FEATURE_MASK only for `other`
777-
!(other.flags[i]
778-
| ((other.flags[i] >> 1) & REQUIRED_FEATURES)
779-
| ((other.flags[i] << 1) & OPTIONAL_FEATURES))
780-
} else {
781-
0b11_11_11_11
782-
};
774+
let unknown_features = self.unknown_features_at_position(other, i);
783775
(byte & (REQUIRED_FEATURES & unknown_features)) != 0
784776
})
785777
}
@@ -790,17 +782,7 @@ impl<T: sealed::Context> Features<T> {
790782
// Bitwise AND-ing with all even bits set except for known features will select required
791783
// unknown features.
792784
self.flags.iter().enumerate().for_each(|(i, &byte)| {
793-
const REQUIRED_FEATURES: u8 = 0b01_01_01_01;
794-
const OPTIONAL_FEATURES: u8 = 0b10_10_10_10;
795-
let unknown_features = if i < other.flags.len() {
796-
// Form a mask similar to !T::KNOWN_FEATURE_MASK only for `other`
797-
!(other.flags[i]
798-
| ((other.flags[i] >> 1) & REQUIRED_FEATURES)
799-
| ((other.flags[i] << 1) & OPTIONAL_FEATURES))
800-
} else {
801-
0b11_11_11_11
802-
};
803-
785+
let unknown_features = self.unknown_features_at_position(other, i);
804786
if byte & unknown_features != 0 {
805787
for bit in (0..8).step_by(2) {
806788
if byte >> bit & 1 == 1 {
@@ -813,6 +795,19 @@ impl<T: sealed::Context> Features<T> {
813795
unknown_bits
814796
}
815797

798+
pub(crate) fn unknown_features_at_position(&self, other: &Self, index: usize) -> u8 {
799+
const REQUIRED_FEATURES: u8 = 0b01_01_01_01;
800+
const OPTIONAL_FEATURES: u8 = 0b10_10_10_10;
801+
if index < other.flags.len() {
802+
// Form a mask similar to !T::KNOWN_FEATURE_MASK only for `other`
803+
!(other.flags[index]
804+
| ((other.flags[index] >> 1) & REQUIRED_FEATURES)
805+
| ((other.flags[index] << 1) & OPTIONAL_FEATURES))
806+
} else {
807+
0b11_11_11_11
808+
}
809+
}
810+
816811
/// Returns true if this `Features` object contains unknown feature flags which are set as
817812
/// "required".
818813
pub fn requires_unknown_bits(&self) -> bool {

0 commit comments

Comments
 (0)