+
+ #[test]
+ fn encoding_final_onion_hop_data_with_secret() {
+ let expected_payment_secret = PaymentSecret([0x42u8; 32]);
+ let mut msg = msgs::OnionHopData {
+ format: OnionHopDataFormat::FinalNode {
+ payment_data: Some(FinalOnionHopData {
+ payment_secret: expected_payment_secret,
+ total_msat: 0x1badca1f
+ }),
+ },
+ amt_to_forward: 0x0badf00d01020304,
+ outgoing_cltv_value: 0xffffffff,
+ };
+ let encoded_value = msg.encode();
+ let target_value = hex::decode("3602080badf00d010203040404ffffffff082442424242424242424242424242424242424242424242424242424242424242421badca1f").unwrap();
+ assert_eq!(encoded_value, target_value);
+ msg = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
+ if let OnionHopDataFormat::FinalNode {
+ payment_data: Some(FinalOnionHopData {
+ payment_secret,
+ total_msat: 0x1badca1f
+ })
+ } = msg.format {
+ assert_eq!(payment_secret, expected_payment_secret);
+ } else { panic!(); }
+ assert_eq!(msg.amt_to_forward, 0x0badf00d01020304);
+ assert_eq!(msg.outgoing_cltv_value, 0xffffffff);
+ }
+
+ #[test]
+ fn encoding_query_channel_range() {
+ let mut query_channel_range = msgs::QueryChannelRange {
+ chain_hash: BlockHash::from_hex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f").unwrap(),
+ first_blocknum: 100000,
+ number_of_blocks: 1500,
+ };
+ let encoded_value = query_channel_range.encode();
+ let target_value = hex::decode("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206000186a0000005dc").unwrap();
+ assert_eq!(encoded_value, target_value);
+
+ query_channel_range = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
+ assert_eq!(query_channel_range.first_blocknum, 100000);
+ assert_eq!(query_channel_range.number_of_blocks, 1500);
+ }
+
+ #[test]
+ fn encoding_reply_channel_range() {
+ do_encoding_reply_channel_range(0);
+ do_encoding_reply_channel_range(1);
+ }
+
+ fn do_encoding_reply_channel_range(encoding_type: u8) {
+ let mut target_value = hex::decode("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206000b8a06000005dc01").unwrap();
+ let expected_chain_hash = BlockHash::from_hex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f").unwrap();
+ let mut reply_channel_range = msgs::ReplyChannelRange {
+ chain_hash: expected_chain_hash,
+ first_blocknum: 756230,
+ number_of_blocks: 1500,
+ full_information: true,
+ short_channel_ids: vec![0x000000000000008e, 0x0000000000003c69, 0x000000000045a6c4],
+ };
+
+ if encoding_type == 0 {
+ target_value.append(&mut hex::decode("001900000000000000008e0000000000003c69000000000045a6c4").unwrap());
+ let encoded_value = reply_channel_range.encode();
+ assert_eq!(encoded_value, target_value);
+
+ reply_channel_range = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
+ assert_eq!(reply_channel_range.chain_hash, expected_chain_hash);
+ assert_eq!(reply_channel_range.first_blocknum, 756230);
+ assert_eq!(reply_channel_range.number_of_blocks, 1500);
+ assert_eq!(reply_channel_range.full_information, true);
+ assert_eq!(reply_channel_range.short_channel_ids[0], 0x000000000000008e);
+ assert_eq!(reply_channel_range.short_channel_ids[1], 0x0000000000003c69);
+ assert_eq!(reply_channel_range.short_channel_ids[2], 0x000000000045a6c4);
+ } else {
+ target_value.append(&mut hex::decode("001601789c636000833e08659309a65878be010010a9023a").unwrap());
+ let result: Result<msgs::ReplyChannelRange, msgs::DecodeError> = Readable::read(&mut Cursor::new(&target_value[..]));
+ assert!(result.is_err(), "Expected decode failure with unsupported zlib encoding");
+ }
+ }
+
+ #[test]
+ fn encoding_query_short_channel_ids() {
+ do_encoding_query_short_channel_ids(0);
+ do_encoding_query_short_channel_ids(1);
+ }
+
+ fn do_encoding_query_short_channel_ids(encoding_type: u8) {
+ let mut target_value = hex::decode("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206").unwrap();
+ let expected_chain_hash = BlockHash::from_hex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f").unwrap();
+ let mut query_short_channel_ids = msgs::QueryShortChannelIds {
+ chain_hash: expected_chain_hash,
+ short_channel_ids: vec![0x0000000000008e, 0x0000000000003c69, 0x000000000045a6c4],
+ };
+
+ if encoding_type == 0 {
+ target_value.append(&mut hex::decode("001900000000000000008e0000000000003c69000000000045a6c4").unwrap());
+ let encoded_value = query_short_channel_ids.encode();
+ assert_eq!(encoded_value, target_value);
+
+ query_short_channel_ids = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
+ assert_eq!(query_short_channel_ids.chain_hash, expected_chain_hash);
+ assert_eq!(query_short_channel_ids.short_channel_ids[0], 0x000000000000008e);
+ assert_eq!(query_short_channel_ids.short_channel_ids[1], 0x0000000000003c69);
+ assert_eq!(query_short_channel_ids.short_channel_ids[2], 0x000000000045a6c4);
+ } else {
+ target_value.append(&mut hex::decode("001601789c636000833e08659309a65878be010010a9023a").unwrap());
+ let result: Result<msgs::QueryShortChannelIds, msgs::DecodeError> = Readable::read(&mut Cursor::new(&target_value[..]));
+ assert!(result.is_err(), "Expected decode failure with unsupported zlib encoding");
+ }
+ }
+
+ #[test]
+ fn encoding_reply_short_channel_ids_end() {
+ let expected_chain_hash = BlockHash::from_hex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f").unwrap();
+ let mut reply_short_channel_ids_end = msgs::ReplyShortChannelIdsEnd {
+ chain_hash: expected_chain_hash,
+ full_information: true,
+ };
+ let encoded_value = reply_short_channel_ids_end.encode();
+ let target_value = hex::decode("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e220601").unwrap();
+ assert_eq!(encoded_value, target_value);
+
+ reply_short_channel_ids_end = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
+ assert_eq!(reply_short_channel_ids_end.chain_hash, expected_chain_hash);
+ assert_eq!(reply_short_channel_ids_end.full_information, true);
+ }
+
+ #[test]
+ fn encoding_gossip_timestamp_filter(){
+ let expected_chain_hash = BlockHash::from_hex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f").unwrap();
+ let mut gossip_timestamp_filter = msgs::GossipTimestampFilter {
+ chain_hash: expected_chain_hash,
+ first_timestamp: 1590000000,
+ timestamp_range: 0xffff_ffff,
+ };
+ let encoded_value = gossip_timestamp_filter.encode();
+ let target_value = hex::decode("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e22065ec57980ffffffff").unwrap();
+ assert_eq!(encoded_value, target_value);
+
+ gossip_timestamp_filter = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
+ assert_eq!(gossip_timestamp_filter.chain_hash, expected_chain_hash);
+ assert_eq!(gossip_timestamp_filter.first_timestamp, 1590000000);
+ assert_eq!(gossip_timestamp_filter.timestamp_range, 0xffff_ffff);
+ }