Skip to content

Commit 8b86ed7

Browse files
committed
Test serialization of ChannelInfo and NodeInfo
1 parent 8f4c951 commit 8b86ed7

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

lightning/src/routing/gossip.rs

+136
Original file line numberDiff line numberDiff line change
@@ -2952,6 +2952,142 @@ mod tests {
29522952
assert_eq!(format_bytes_alias(b"\xFFI <heart>\0LDK!"), "\u{FFFD}I <heart>");
29532953
assert_eq!(format_bytes_alias(b"\xFFI <heart>\tLDK!"), "\u{FFFD}I <heart>\u{FFFD}LDK!");
29542954
}
2955+
2956+
#[test]
2957+
fn channel_info_is_readable() {
2958+
let chanmon_cfgs = ::ln::functional_test_utils::create_chanmon_cfgs(2);
2959+
let node_cfgs = ::ln::functional_test_utils::create_node_cfgs(2, &chanmon_cfgs);
2960+
let node_chanmgrs = ::ln::functional_test_utils::create_node_chanmgrs(2, &node_cfgs, &[None, None, None, None]);
2961+
let nodes = ::ln::functional_test_utils::create_network(2, &node_cfgs, &node_chanmgrs);
2962+
2963+
// 1. Test encoding/decoding of ChannelUpdateInfo
2964+
let chan_update_info = ChannelUpdateInfo {
2965+
last_update: 23,
2966+
enabled: true,
2967+
cltv_expiry_delta: 42,
2968+
htlc_minimum_msat: 1234,
2969+
htlc_maximum_msat: 5678,
2970+
fees: RoutingFees { base_msat: 9, proportional_millionths: 10 },
2971+
last_update_message: None,
2972+
};
2973+
2974+
let mut encoded_chan_update_info: Vec<u8> = Vec::new();
2975+
assert!(chan_update_info.write(&mut encoded_chan_update_info).is_ok());
2976+
2977+
// First make sure we can read ChannelUpdateInfos we just wrote
2978+
let read_chan_update_info: ChannelUpdateInfo = ::util::ser::Readable::read(&mut encoded_chan_update_info.as_slice()).unwrap();
2979+
assert_eq!(chan_update_info, read_chan_update_info);
2980+
2981+
// Check the serialization hasn't changed.
2982+
let legacy_chan_update_info_with_some: Vec<u8> = hex::decode("340004000000170201010402002a060800000000000004d2080909000000000000162e0a0d0c00040000000902040000000a0c0100").unwrap();
2983+
assert_eq!(encoded_chan_update_info, legacy_chan_update_info_with_some);
2984+
2985+
// Check we fail if htlc_maximum_msat is not present in either the ChannelUpdateInfo itself
2986+
// or the ChannelUpdate enclosed with `last_update_message`.
2987+
let legacy_chan_update_info_with_some_and_fail_update: Vec<u8> = hex::decode("b40004000000170201010402002a060800000000000004d2080909000000000000162e0a0d0c00040000000902040000000a0c8181d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f00083a840000034d013413a70000009000000000000f42400000271000000014").unwrap();
2988+
let read_chan_update_info_res: Result<ChannelUpdateInfo, ::ln::msgs::DecodeError> = ::util::ser::Readable::read(&mut legacy_chan_update_info_with_some_and_fail_update.as_slice());
2989+
assert!(read_chan_update_info_res.is_err());
2990+
2991+
let legacy_chan_update_info_with_none: Vec<u8> = hex::decode("2c0004000000170201010402002a060800000000000004d20801000a0d0c00040000000902040000000a0c0100").unwrap();
2992+
let read_chan_update_info_res: Result<ChannelUpdateInfo, ::ln::msgs::DecodeError> = ::util::ser::Readable::read(&mut legacy_chan_update_info_with_none.as_slice());
2993+
assert!(read_chan_update_info_res.is_err());
2994+
2995+
// 2. Test encoding/decoding of ChannelInfo
2996+
// Check we can encode/decode ChannelInfo without ChannelUpdateInfo fields present.
2997+
let chan_info_none_updates = ChannelInfo {
2998+
features: ChannelFeatures::known(),
2999+
node_one: NodeId::from_pubkey(&nodes[0].node.get_our_node_id()),
3000+
one_to_two: None,
3001+
node_two: NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
3002+
two_to_one: None,
3003+
capacity_sats: None,
3004+
announcement_message: None,
3005+
announcement_received_time: 87654,
3006+
};
3007+
3008+
let mut encoded_chan_info: Vec<u8> = Vec::new();
3009+
assert!(chan_info_none_updates.write(&mut encoded_chan_info).is_ok());
3010+
3011+
let read_chan_info: ChannelInfo = ::util::ser::Readable::read(&mut encoded_chan_info.as_slice()).unwrap();
3012+
assert_eq!(chan_info_none_updates, read_chan_info);
3013+
3014+
// Check we can encode/decode ChannelInfo with ChannelUpdateInfo fields present.
3015+
let chan_info_some_updates = ChannelInfo {
3016+
features: ChannelFeatures::known(),
3017+
node_one: NodeId::from_pubkey(&nodes[0].node.get_our_node_id()),
3018+
one_to_two: Some(chan_update_info.clone()),
3019+
node_two: NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
3020+
two_to_one: Some(chan_update_info.clone()),
3021+
capacity_sats: None,
3022+
announcement_message: None,
3023+
announcement_received_time: 87654,
3024+
};
3025+
3026+
let mut encoded_chan_info: Vec<u8> = Vec::new();
3027+
assert!(chan_info_some_updates.write(&mut encoded_chan_info).is_ok());
3028+
3029+
let read_chan_info: ChannelInfo = ::util::ser::Readable::read(&mut encoded_chan_info.as_slice()).unwrap();
3030+
assert_eq!(chan_info_some_updates, read_chan_info);
3031+
3032+
// Check the serialization hasn't changed.
3033+
let legacy_chan_info_with_some: Vec<u8> = hex::decode("ca00020000010800000000000156660221027f921585f2ac0c7c70e36110adecfd8fd14b8a99bfb3d000a283fcac358fce88043636340004000000170201010402002a060800000000000004d2080909000000000000162e0a0d0c00040000000902040000000a0c010006210355f8d2238a322d16b602bd0ceaad5b01019fb055971eaadcc9b29226a4da6c23083636340004000000170201010402002a060800000000000004d2080909000000000000162e0a0d0c00040000000902040000000a0c01000a01000c0100").unwrap();
3034+
assert_eq!(encoded_chan_info, legacy_chan_info_with_some);
3035+
3036+
// Check we can decode legacy ChannelInfo, even if the `two_to_one` / `one_to_two` /
3037+
// `last_update_message` fields fail to decode due to missing htlc_maximum_msat.
3038+
let legacy_chan_info_with_some_and_fail_update = hex::decode("fd01ca00020000010800000000000156660221027f921585f2ac0c7c70e36110adecfd8fd14b8a99bfb3d000a283fcac358fce8804b6b6b40004000000170201010402002a060800000000000004d2080909000000000000162e0a0d0c00040000000902040000000a0c8181d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f00083a840000034d013413a70000009000000000000f4240000027100000001406210355f8d2238a322d16b602bd0ceaad5b01019fb055971eaadcc9b29226a4da6c2308b6b6b40004000000170201010402002a060800000000000004d2080909000000000000162e0a0d0c00040000000902040000000a0c8181d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f00083a840000034d013413a70000009000000000000f424000002710000000140a01000c0100").unwrap();
3039+
let read_chan_info: ChannelInfo = ::util::ser::Readable::read(&mut legacy_chan_info_with_some_and_fail_update.as_slice()).unwrap();
3040+
assert_eq!(read_chan_info.announcement_received_time, 87654);
3041+
assert_eq!(read_chan_info.one_to_two, None);
3042+
assert_eq!(read_chan_info.two_to_one, None);
3043+
3044+
let legacy_chan_info_with_none: Vec<u8> = hex::decode("ba00020000010800000000000156660221027f921585f2ac0c7c70e36110adecfd8fd14b8a99bfb3d000a283fcac358fce88042e2e2c0004000000170201010402002a060800000000000004d20801000a0d0c00040000000902040000000a0c010006210355f8d2238a322d16b602bd0ceaad5b01019fb055971eaadcc9b29226a4da6c23082e2e2c0004000000170201010402002a060800000000000004d20801000a0d0c00040000000902040000000a0c01000a01000c0100").unwrap();
3045+
let read_chan_info: ChannelInfo = ::util::ser::Readable::read(&mut legacy_chan_info_with_none.as_slice()).unwrap();
3046+
assert_eq!(read_chan_info.announcement_received_time, 87654);
3047+
assert_eq!(read_chan_info.one_to_two, None);
3048+
assert_eq!(read_chan_info.two_to_one, None);
3049+
}
3050+
3051+
#[test]
3052+
fn node_info_is_readable() {
3053+
use std::convert::TryFrom;
3054+
3055+
// 1. Check we can read a valid NodeAnnouncementInfo and fail on an invalid one
3056+
let valid_netaddr = ::ln::msgs::NetAddress::Hostname { hostname: ::util::ser::Hostname::try_from("A".to_string()).unwrap(), port: 1234 };
3057+
let valid_node_ann_info = NodeAnnouncementInfo {
3058+
features: NodeFeatures::known(),
3059+
last_update: 0,
3060+
rgb: [0u8; 3],
3061+
alias: NodeAlias([0u8; 32]),
3062+
addresses: vec![valid_netaddr],
3063+
announcement_message: None,
3064+
};
3065+
3066+
let mut encoded_valid_node_ann_info = Vec::new();
3067+
assert!(valid_node_ann_info.write(&mut encoded_valid_node_ann_info).is_ok());
3068+
let read_valid_node_ann_info: NodeAnnouncementInfo = ::util::ser::Readable::read(&mut encoded_valid_node_ann_info.as_slice()).unwrap();
3069+
assert_eq!(read_valid_node_ann_info, valid_node_ann_info);
3070+
3071+
let encoded_invalid_node_ann_info = hex::decode("3f0009000788a000080a51a20204000000000403000000062000000000000000000000000000000000000000000000000000000000000000000a0505014004d2").unwrap();
3072+
let read_invalid_node_ann_info_res: Result<NodeAnnouncementInfo, ::ln::msgs::DecodeError> = ::util::ser::Readable::read(&mut encoded_invalid_node_ann_info.as_slice());
3073+
assert!(read_invalid_node_ann_info_res.is_err());
3074+
3075+
// 2. Check we can read a NodeInfo anyways, but set the NodeAnnouncementInfo to None if invalid
3076+
let valid_node_info = NodeInfo {
3077+
channels: Vec::new(),
3078+
lowest_inbound_channel_fees: None,
3079+
announcement_info: Some(valid_node_ann_info),
3080+
};
3081+
3082+
let mut encoded_valid_node_info = Vec::new();
3083+
assert!(valid_node_info.write(&mut encoded_valid_node_info).is_ok());
3084+
let read_valid_node_info: NodeInfo = ::util::ser::Readable::read(&mut encoded_valid_node_info.as_slice()).unwrap();
3085+
assert_eq!(read_valid_node_info, valid_node_info);
3086+
3087+
let encoded_invalid_node_info_hex = hex::decode("4402403f0009000788a000080a51a20204000000000403000000062000000000000000000000000000000000000000000000000000000000000000000a0505014004d20400").unwrap();
3088+
let read_invalid_node_info: NodeInfo = ::util::ser::Readable::read(&mut encoded_invalid_node_info_hex.as_slice()).unwrap();
3089+
assert_eq!(read_invalid_node_info.announcement_info, None);
3090+
}
29553091
}
29563092

29573093
#[cfg(all(test, feature = "_bench_unstable"))]

0 commit comments

Comments
 (0)