Skip to content

Commit 65ff1bf

Browse files
committed
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.
1 parent 9dd2be1 commit 65ff1bf

File tree

1 file changed

+21
-38
lines changed

1 file changed

+21
-38
lines changed

lightning/src/ln/features.rs

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -284,33 +284,6 @@ impl InitFeatures {
284284
}
285285
}
286286

287-
impl ChannelFeatures {
288-
/// Takes the flags that we know how to interpret in an init-context features that are also
289-
/// relevant in a channel-context features and creates a channel-context features from them.
290-
pub(crate) fn with_known_relevant_init_flags(_init_ctx: &InitFeatures) -> Self {
291-
// There are currently no channel flags defined that we understand.
292-
Self { flags: Vec::new(), mark: PhantomData, }
293-
}
294-
}
295-
296-
impl NodeFeatures {
297-
/// Takes the flags that we know how to interpret in an init-context features that are also
298-
/// relevant in a node-context features and creates a node-context features from them.
299-
/// Be sure to blank out features that are unknown to us.
300-
pub(crate) fn with_known_relevant_init_flags(init_ctx: &InitFeatures) -> Self {
301-
use ln::features::sealed::Context;
302-
let byte_count = sealed::NodeContext::KNOWN_FEATURE_MASK.len();
303-
304-
let mut flags = Vec::new();
305-
for (i, feature_byte) in init_ctx.flags.iter().enumerate() {
306-
if i < byte_count {
307-
flags.push(feature_byte & sealed::NodeContext::KNOWN_FEATURE_MASK[i]);
308-
}
309-
}
310-
Self { flags, mark: PhantomData, }
311-
}
312-
}
313-
314287
impl<T: sealed::Context> Features<T> {
315288
/// Create a blank Features with no features set
316289
pub fn empty() -> Features<T> {
@@ -330,6 +303,20 @@ impl<T: sealed::Context> Features<T> {
330303
}
331304
}
332305

306+
/// Takes the flags that we know how to interpret in an init-context features that are also
307+
/// relevant in a node-context features and creates a node-context features from them.
308+
/// Be sure to blank out features that are unknown to us.
309+
pub(crate) fn with_known_relevant_init_flags(init_ctx: &InitFeatures) -> Self {
310+
let byte_count = T::KNOWN_FEATURE_MASK.len();
311+
let mut flags = Vec::new();
312+
for (i, feature_byte) in init_ctx.flags.iter().enumerate() {
313+
if i < byte_count {
314+
flags.push(feature_byte & T::KNOWN_FEATURE_MASK[i]);
315+
}
316+
}
317+
Self { flags, mark: PhantomData, }
318+
}
319+
333320
#[cfg(test)]
334321
/// Create a Features given a set of flags, in LE.
335322
pub fn from_le_bytes(flags: Vec<u8>) -> Features<T> {
@@ -346,15 +333,13 @@ impl<T: sealed::Context> Features<T> {
346333
}
347334

348335
pub(crate) fn requires_unknown_bits(&self) -> bool {
349-
use ln::features::sealed::Context;
350-
let byte_count = sealed::InitContext::KNOWN_FEATURE_MASK.len();
351-
352-
// Bitwise AND-ing with all even bits set except for known features will select unknown
353-
// required features.
336+
// Bitwise AND-ing with all even bits set except for known features will select required
337+
// unknown features.
338+
let byte_count = T::KNOWN_FEATURE_MASK.len();
354339
self.flags.iter().enumerate().any(|(i, &byte)| {
355340
let required_features = 0b01_01_01_01;
356341
let unknown_features = if i < byte_count {
357-
!sealed::InitContext::KNOWN_FEATURE_MASK[i]
342+
!T::KNOWN_FEATURE_MASK[i]
358343
} else {
359344
0b11_11_11_11
360345
};
@@ -363,14 +348,12 @@ impl<T: sealed::Context> Features<T> {
363348
}
364349

365350
pub(crate) fn supports_unknown_bits(&self) -> bool {
366-
use ln::features::sealed::Context;
367-
let byte_count = sealed::InitContext::KNOWN_FEATURE_MASK.len();
368-
369351
// Bitwise AND-ing with all even and odd bits set except for known features will select
370-
// unknown features.
352+
// both required and optional unknown features.
353+
let byte_count = T::KNOWN_FEATURE_MASK.len();
371354
self.flags.iter().enumerate().any(|(i, &byte)| {
372355
let unknown_features = if i < byte_count {
373-
!sealed::InitContext::KNOWN_FEATURE_MASK[i]
356+
!T::KNOWN_FEATURE_MASK[i]
374357
} else {
375358
0b11_11_11_11
376359
};

0 commit comments

Comments
 (0)