Skip to content

Commit eee9467

Browse files
authored
Merge pull request #541 from valentinewallace/fix-node-features
Fix node `with_relevant_init_flags`
2 parents 83c9eb4 + 40a1aef commit eee9467

File tree

2 files changed

+93
-4
lines changed

2 files changed

+93
-4
lines changed

lightning/src/ln/features.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,18 @@ impl NodeFeatures {
150150

151151
/// Takes the flags that we know how to interpret in an init-context features that are also
152152
/// relevant in a node-context features and creates a node-context features from them.
153+
/// Be sure to blank out features that are unknown to us.
153154
pub(crate) fn with_known_relevant_init_flags(init_ctx: &InitFeatures) -> Self {
154155
let mut flags = Vec::new();
155-
if init_ctx.flags.len() > 0 {
156-
// Pull out data_loss_protect and upfront_shutdown_script (bits 0, 1, 4, and 5)
157-
flags.push(init_ctx.flags.last().unwrap() & 0b00110011);
156+
for (i, feature_byte)in init_ctx.flags.iter().enumerate() {
157+
match i {
158+
// Blank out initial_routing_sync (feature bits 2/3), gossip_queries (6/7),
159+
// gossip_queries_ex (10/11), option_static_remotekey (12/13), and
160+
// payment_secret (14/15)
161+
0 => flags.push(feature_byte & 0b00110011),
162+
1 => flags.push(feature_byte & 0b00000011),
163+
_ => (),
164+
}
158165
}
159166
Self { flags, mark: PhantomData, }
160167
}
@@ -296,7 +303,7 @@ impl<T: sealed::Context> Readable for Features<T> {
296303

297304
#[cfg(test)]
298305
mod tests {
299-
use super::{ChannelFeatures, InitFeatures, NodeFeatures};
306+
use super::{ChannelFeatures, InitFeatures, NodeFeatures, Features};
300307

301308
#[test]
302309
fn sanity_test_our_features() {
@@ -330,4 +337,26 @@ mod tests {
330337
features.clear_require_unknown_bits();
331338
assert!(!features.requires_unknown_bits());
332339
}
340+
341+
#[test]
342+
fn test_node_with_known_relevant_init_flags() {
343+
// Create an InitFeatures with initial_routing_sync supported.
344+
let mut init_features = InitFeatures::supported();
345+
init_features.set_initial_routing_sync();
346+
347+
// Attempt to pull out non-node-context feature flags from these InitFeatures.
348+
let res = NodeFeatures::with_known_relevant_init_flags(&init_features);
349+
350+
{
351+
// Check that the flags are as expected: optional_data_loss_protect,
352+
// option_upfront_shutdown_script, and var_onion_optin set.
353+
assert_eq!(res.flags[0], 0b00100010);
354+
assert_eq!(res.flags[1], 0b00000010);
355+
assert_eq!(res.flags.len(), 2);
356+
}
357+
358+
// Check that the initial_routing_sync feature was correctly blanked out.
359+
let new_features: InitFeatures = Features::from_le_bytes(res.flags);
360+
assert!(!new_features.initial_routing_sync());
361+
}
333362
}

lightning/src/ln/wire.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,64 @@ mod tests {
401401
let message = Message::Unknown(MessageType(43));
402402
assert!(!message.type_id().is_even());
403403
}
404+
405+
#[test]
406+
fn read_lnd_init_msg() {
407+
// Taken from lnd v0.9.0-beta.
408+
let buffer = vec![0, 16, 0, 2, 34, 0, 0, 3, 2, 162, 161];
409+
check_init_msg(buffer);
410+
}
411+
412+
#[test]
413+
fn read_clightning_init_msg() {
414+
// Taken from c-lightning v0.8.0.
415+
let buffer = vec![0, 16, 0, 2, 34, 0, 0, 3, 2, 170, 162, 1, 32, 6, 34, 110, 70, 17, 26, 11, 89, 202, 175, 18, 96, 67, 235, 91, 191, 40, 195, 79, 58, 94, 51, 42, 31, 199, 178, 183, 60, 241, 136, 145, 15];
416+
check_init_msg(buffer);
417+
}
418+
419+
fn check_init_msg(buffer: Vec<u8>) {
420+
let mut reader = ::std::io::Cursor::new(buffer);
421+
let decoded_msg = read(&mut reader).unwrap();
422+
match decoded_msg {
423+
Message::Init(msgs::Init { features }) => {
424+
assert!(features.supports_variable_length_onion());
425+
assert!(features.supports_upfront_shutdown_script());
426+
assert!(features.supports_unknown_bits());
427+
assert!(!features.requires_unknown_bits());
428+
assert!(!features.initial_routing_sync());
429+
},
430+
_ => panic!("Expected init message, found message type: {}", decoded_msg.type_id())
431+
}
432+
}
433+
434+
#[test]
435+
fn read_lnd_node_announcement() {
436+
// Taken from lnd v0.9.0-beta.
437+
let buffer = vec![1, 1, 91, 164, 146, 213, 213, 165, 21, 227, 102, 33, 105, 179, 214, 21, 221, 175, 228, 93, 57, 177, 191, 127, 107, 229, 31, 50, 21, 81, 179, 71, 39, 18, 35, 2, 89, 224, 110, 123, 66, 39, 148, 246, 177, 85, 12, 19, 70, 226, 173, 132, 156, 26, 122, 146, 71, 213, 247, 48, 93, 190, 185, 177, 12, 172, 0, 3, 2, 162, 161, 94, 103, 195, 37, 2, 37, 242, 97, 140, 2, 111, 69, 85, 39, 118, 30, 221, 99, 254, 120, 49, 103, 22, 170, 227, 111, 172, 164, 160, 49, 68, 138, 116, 16, 22, 206, 107, 51, 153, 255, 97, 108, 105, 99, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 172, 21, 0, 2, 38, 7];
438+
let mut reader = ::std::io::Cursor::new(buffer);
439+
let decoded_msg = read(&mut reader).unwrap();
440+
match decoded_msg {
441+
Message::NodeAnnouncement(msgs::NodeAnnouncement { contents: msgs::UnsignedNodeAnnouncement { features, ..}, ..}) => {
442+
assert!(features.supports_variable_length_onion());
443+
assert!(features.supports_upfront_shutdown_script());
444+
assert!(features.supports_unknown_bits());
445+
assert!(!features.requires_unknown_bits());
446+
},
447+
_ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id())
448+
}
449+
}
450+
451+
#[test]
452+
fn read_lnd_chan_announcement() {
453+
// Taken from lnd v0.9.0-beta.
454+
let buffer = vec![1, 0, 82, 238, 153, 33, 128, 87, 215, 2, 28, 241, 140, 250, 98, 255, 56, 5, 79, 240, 214, 231, 172, 35, 240, 171, 44, 9, 78, 91, 8, 193, 102, 5, 17, 178, 142, 106, 180, 183, 46, 38, 217, 212, 25, 236, 69, 47, 92, 217, 181, 221, 161, 205, 121, 201, 99, 38, 158, 216, 186, 193, 230, 86, 222, 6, 206, 67, 22, 255, 137, 212, 141, 161, 62, 134, 76, 48, 241, 54, 50, 167, 187, 247, 73, 27, 74, 1, 129, 185, 197, 153, 38, 90, 255, 138, 39, 161, 102, 172, 213, 74, 107, 88, 150, 90, 0, 49, 104, 7, 182, 184, 194, 219, 181, 172, 8, 245, 65, 226, 19, 228, 101, 145, 25, 159, 52, 31, 58, 93, 53, 59, 218, 91, 37, 84, 103, 17, 74, 133, 33, 35, 2, 203, 101, 73, 19, 94, 175, 122, 46, 224, 47, 168, 128, 128, 25, 26, 25, 214, 52, 247, 43, 241, 117, 52, 206, 94, 135, 156, 52, 164, 143, 234, 58, 185, 50, 185, 140, 198, 174, 71, 65, 18, 105, 70, 131, 172, 137, 0, 164, 51, 215, 143, 117, 119, 217, 241, 197, 177, 227, 227, 170, 199, 114, 7, 218, 12, 107, 30, 191, 236, 203, 21, 61, 242, 48, 192, 90, 233, 200, 199, 111, 162, 68, 234, 54, 219, 1, 233, 66, 5, 82, 74, 84, 211, 95, 199, 245, 202, 89, 223, 102, 124, 62, 166, 253, 253, 90, 180, 118, 21, 61, 110, 37, 5, 96, 167, 0, 0, 6, 34, 110, 70, 17, 26, 11, 89, 202, 175, 18, 96, 67, 235, 91, 191, 40, 195, 79, 58, 94, 51, 42, 31, 199, 178, 183, 60, 241, 136, 145, 15, 0, 2, 65, 0, 0, 1, 0, 0, 2, 37, 242, 97, 140, 2, 111, 69, 85, 39, 118, 30, 221, 99, 254, 120, 49, 103, 22, 170, 227, 111, 172, 164, 160, 49, 68, 138, 116, 16, 22, 206, 107, 3, 54, 61, 144, 88, 171, 247, 136, 208, 99, 9, 135, 37, 201, 178, 253, 136, 0, 185, 235, 68, 160, 106, 110, 12, 46, 21, 125, 204, 18, 75, 234, 16, 3, 42, 171, 28, 52, 224, 11, 30, 30, 253, 156, 148, 175, 203, 121, 250, 111, 122, 195, 84, 122, 77, 183, 56, 135, 101, 88, 41, 60, 191, 99, 232, 85, 2, 36, 17, 156, 11, 8, 12, 189, 177, 68, 88, 28, 15, 207, 21, 179, 151, 56, 226, 158, 148, 3, 120, 113, 177, 243, 184, 17, 173, 37, 46, 222, 16];
455+
let mut reader = ::std::io::Cursor::new(buffer);
456+
let decoded_msg = read(&mut reader).unwrap();
457+
match decoded_msg {
458+
Message::ChannelAnnouncement(msgs::ChannelAnnouncement { contents: msgs::UnsignedChannelAnnouncement { features, ..}, ..}) => {
459+
assert!(!features.requires_unknown_bits());
460+
},
461+
_ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id())
462+
}
463+
}
404464
}

0 commit comments

Comments
 (0)