Skip to content

Commit

Permalink
Also test decoding of legacy ChannelInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Jul 20, 2022
1 parent 0e66fee commit b449f37
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lightning/src/routing/gossip.rs
Expand Up @@ -784,6 +784,7 @@ impl MaybeReadable for ChannelUpdateInfoDeserWrapper {
match ::util::ser::Readable::read(reader) {
Ok(channel_update_option) => Ok(Some(Self(channel_update_option))),
Err(DecodeError::ShortRead) => Ok(None),
Err(DecodeError::InvalidValue) => Ok(None),
Err(err) => Err(err),
}
}
Expand Down Expand Up @@ -2953,6 +2954,7 @@ mod tests {
let node_chanmgrs = ::ln::functional_test_utils::create_node_chanmgrs(2, &node_cfgs, &[None, None, None, None]);
let nodes = ::ln::functional_test_utils::create_network(2, &node_cfgs, &node_chanmgrs);

// 1. Test encoding/decoding of ChannelUpdateInfo
let chan_update_info = ChannelUpdateInfo {
last_update: 23,
enabled: true,
Expand All @@ -2971,14 +2973,15 @@ mod tests {
assert_eq!(chan_update_info, read_chan_update_info);

// Check the serialization hasn't changed.
let old_chan_update_info_with_some: Vec<u8> = hex::decode("340004000000170201010402002a060800000000000004d2080909000000000000162e0a0d0c00040000000902040000000a0c0100").unwrap();
assert_eq!(encoded_chan_update_info, old_chan_update_info_with_some);
let legacy_chan_update_info_with_some: Vec<u8> = hex::decode("340004000000170201010402002a060800000000000004d2080909000000000000162e0a0d0c00040000000902040000000a0c0100").unwrap();
assert_eq!(encoded_chan_update_info, legacy_chan_update_info_with_some);

// Check we fail if htlc_maximum_msat is not present.
let old_chan_update_info_with_none: Vec<u8> = hex::decode("2c0004000000170201010402002a060800000000000004d20801000a0d0c00040000000902040000000a0c0100").unwrap();
let read_chan_update_info_res: Result<ChannelUpdateInfo, ::ln::msgs::DecodeError> = ::util::ser::Readable::read(&mut old_chan_update_info_with_none.as_slice());
let legacy_chan_update_info_with_none: Vec<u8> = hex::decode("2c0004000000170201010402002a060800000000000004d20801000a0d0c00040000000902040000000a0c0100").unwrap();
let read_chan_update_info_res: Result<ChannelUpdateInfo, ::ln::msgs::DecodeError> = ::util::ser::Readable::read(&mut legacy_chan_update_info_with_none.as_slice());
assert!(read_chan_update_info_res.is_err());

// 2. Test encoding/decoding of ChannelInfo
// Check we can encode/decode ChannelInfo without ChannelUpdateInfo fields present.
let chan_info_none_updates = ChannelInfo {
features: ChannelFeatures::known(),
Expand All @@ -2997,7 +3000,7 @@ mod tests {
let read_chan_info: ChannelInfo = ::util::ser::Readable::read(&mut encoded_chan_info.as_slice()).unwrap();
assert_eq!(chan_info_none_updates, read_chan_info);

// Finally check we can encode/decode ChannelInfo with ChannelUpdateInfo fields present.
// Check we can encode/decode ChannelInfo with ChannelUpdateInfo fields present.
let chan_info_some_updates = ChannelInfo {
features: ChannelFeatures::known(),
node_one: NodeId::from_pubkey(&nodes[0].node.get_our_node_id()),
Expand All @@ -3014,6 +3017,18 @@ mod tests {

let read_chan_info: ChannelInfo = ::util::ser::Readable::read(&mut encoded_chan_info.as_slice()).unwrap();
assert_eq!(chan_info_some_updates, read_chan_info);

// Check the serialization hasn't changed.
let legacy_chan_info_with_some: Vec<u8> = hex::decode("ca00020000010800000000000156660221027f921585f2ac0c7c70e36110adecfd8fd14b8a99bfb3d000a283fcac358fce88043636340004000000170201010402002a060800000000000004d2080909000000000000162e0a0d0c00040000000902040000000a0c010006210355f8d2238a322d16b602bd0ceaad5b01019fb055971eaadcc9b29226a4da6c23083636340004000000170201010402002a060800000000000004d2080909000000000000162e0a0d0c00040000000902040000000a0c01000a01000c0100").unwrap();
assert_eq!(encoded_chan_info, legacy_chan_info_with_some);

// Check we can decode legacy ChannelInfo, even if the `two_to_one`/`one_to_two` fields
// fail to decode.
let legacy_chan_info_with_none: Vec<u8> = hex::decode("ba00020000010800000000000156660221027f921585f2ac0c7c70e36110adecfd8fd14b8a99bfb3d000a283fcac358fce88042e2e2c0004000000170201010402002a060800000000000004d20801000a0d0c00040000000902040000000a0c010006210355f8d2238a322d16b602bd0ceaad5b01019fb055971eaadcc9b29226a4da6c23082e2e2c0004000000170201010402002a060800000000000004d20801000a0d0c00040000000902040000000a0c01000a01000c0100").unwrap();
let read_chan_info: ChannelInfo = ::util::ser::Readable::read(&mut legacy_chan_info_with_none.as_slice()).unwrap();
assert_eq!(read_chan_info.announcement_received_time, 87654);
assert_eq!(read_chan_info.one_to_two, None);
assert_eq!(read_chan_info.two_to_one, None);
}
}

Expand Down

0 comments on commit b449f37

Please sign in to comment.