Skip to content

Commit 57b12a1

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 and UNKNOWN_FEATURE_MASK are defined on Context instead of hardcoded in each method specialization. Thus, such methods are no longer required.
1 parent 1d677c7 commit 57b12a1

File tree

1 file changed

+18
-35
lines changed

1 file changed

+18
-35
lines changed

lightning/src/ln/features.rs

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -260,33 +260,6 @@ impl InitFeatures {
260260
}
261261
}
262262

263-
impl ChannelFeatures {
264-
/// Takes the flags that we know how to interpret in an init-context features that are also
265-
/// relevant in a channel-context features and creates a channel-context features from them.
266-
pub(crate) fn with_known_relevant_init_flags(_init_ctx: &InitFeatures) -> Self {
267-
// There are currently no channel flags defined that we understand.
268-
Self { flags: Vec::new(), mark: PhantomData, }
269-
}
270-
}
271-
272-
impl NodeFeatures {
273-
/// Takes the flags that we know how to interpret in an init-context features that are also
274-
/// relevant in a node-context features and creates a node-context features from them.
275-
/// Be sure to blank out features that are unknown to us.
276-
pub(crate) fn with_known_relevant_init_flags(init_ctx: &InitFeatures) -> Self {
277-
use ln::features::sealed::Context;
278-
let byte_count = sealed::NodeContext::KNOWN_FEATURE_MASK.len();
279-
280-
let mut flags = Vec::new();
281-
for (i, feature_byte) in init_ctx.flags.iter().enumerate() {
282-
if i < byte_count {
283-
flags.push(feature_byte & sealed::NodeContext::KNOWN_FEATURE_MASK[i]);
284-
}
285-
}
286-
Self { flags, mark: PhantomData, }
287-
}
288-
}
289-
290263
impl<T: sealed::Context> Features<T> {
291264
/// Create a blank Features with no features set
292265
pub fn empty() -> Features<T> {
@@ -304,6 +277,20 @@ impl<T: sealed::Context> Features<T> {
304277
}
305278
}
306279

280+
/// Takes the flags that we know how to interpret in an init-context features that are also
281+
/// relevant in a node-context features and creates a node-context features from them.
282+
/// Be sure to blank out features that are unknown to us.
283+
pub(crate) fn with_known_relevant_init_flags(init_ctx: &InitFeatures) -> Self {
284+
let byte_count = T::KNOWN_FEATURE_MASK.len();
285+
let mut flags = Vec::new();
286+
for (i, feature_byte) in init_ctx.flags.iter().enumerate() {
287+
if i < byte_count {
288+
flags.push(feature_byte & T::KNOWN_FEATURE_MASK[i]);
289+
}
290+
}
291+
Self { flags, mark: PhantomData, }
292+
}
293+
307294
#[cfg(test)]
308295
/// Create a Features given a set of flags, in LE.
309296
pub fn from_le_bytes(flags: Vec<u8>) -> Features<T> {
@@ -320,15 +307,13 @@ impl<T: sealed::Context> Features<T> {
320307
}
321308

322309
pub(crate) fn requires_unknown_bits(&self) -> bool {
323-
use ln::features::sealed::Context;
324-
let byte_count = sealed::InitContext::UNKNOWN_FEATURE_MASK.len();
325-
326310
// Bitwise AND-ing with all even bits set except for known features will select unknown
327311
// required features.
312+
let byte_count = T::UNKNOWN_FEATURE_MASK.len();
328313
self.flags.iter().enumerate().any(|(i, &byte)| {
329314
let required_features = 0b01_01_01_01;
330315
let unknown_features = if i < byte_count {
331-
sealed::InitContext::UNKNOWN_FEATURE_MASK[i]
316+
T::UNKNOWN_FEATURE_MASK[i]
332317
} else {
333318
0b11_11_11_11
334319
};
@@ -337,14 +322,12 @@ impl<T: sealed::Context> Features<T> {
337322
}
338323

339324
pub(crate) fn supports_unknown_bits(&self) -> bool {
340-
use ln::features::sealed::Context;
341-
let byte_count = sealed::InitContext::UNKNOWN_FEATURE_MASK.len();
342-
343325
// Bitwise AND-ing with all even and odd bits set except for known features will select
344326
// unknown features.
327+
let byte_count = T::UNKNOWN_FEATURE_MASK.len();
345328
self.flags.iter().enumerate().any(|(i, &byte)| {
346329
let unknown_features = if i < byte_count {
347-
sealed::InitContext::UNKNOWN_FEATURE_MASK[i]
330+
T::UNKNOWN_FEATURE_MASK[i]
348331
} else {
349332
0b11_11_11_11
350333
};

0 commit comments

Comments
 (0)