Skip to content

Commit 446c007

Browse files
committed
DRY up unknown_features calculation
1 parent 158ef6f commit 446c007

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 = unset_features_mask_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 = unset_features_mask_at_position(other, i);
804786
if byte & unknown_features != 0 {
805787
for bit in (0..8).step_by(2) {
806788
if ((byte & unknown_features) >> bit) & 1 == 1 {
@@ -1047,6 +1029,19 @@ impl<T: sealed::Context> Readable for WithoutLength<Features<T>> {
10471029
}
10481030
}
10491031

1032+
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;
1035+
if index < other.flags.len() {
1036+
// Form a mask similar to !T::KNOWN_FEATURE_MASK only for `other`
1037+
!(other.flags[index]
1038+
| ((other.flags[index] >> 1) & REQUIRED_FEATURES)
1039+
| ((other.flags[index] << 1) & OPTIONAL_FEATURES))
1040+
} else {
1041+
0b11_11_11_11
1042+
}
1043+
}
1044+
10501045
#[cfg(test)]
10511046
mod tests {
10521047
use super::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, Bolt11InvoiceFeatures, NodeFeatures, OfferFeatures, sealed};

0 commit comments

Comments
 (0)