+
+ #[test]
+ fn calling_sync_routing_table() {
+ let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
+ let node_privkey_1 = &SecretKey::from_slice(&[42; 32]).unwrap();
+ let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_privkey_1);
+
+ let chain_hash = genesis_block(Network::Testnet).header.block_hash();
+ let first_blocknum = 0;
+ let number_of_blocks = 0xffff_ffff;
+
+ // It should ignore if gossip_queries feature is not enabled
+ {
+ let init_msg = Init { features: InitFeatures::known().clear_gossip_queries() };
+ net_graph_msg_handler.sync_routing_table(&node_id_1, &init_msg);
+ let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
+ assert_eq!(events.len(), 0);
+ }
+
+ // It should send a query_channel_message with the correct information
+ {
+ let init_msg = Init { features: InitFeatures::known() };
+ net_graph_msg_handler.sync_routing_table(&node_id_1, &init_msg);
+ let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
+ assert_eq!(events.len(), 1);
+ match &events[0] {
+ MessageSendEvent::SendChannelRangeQuery{ node_id, msg } => {
+ assert_eq!(node_id, &node_id_1);
+ assert_eq!(msg.chain_hash, chain_hash);
+ assert_eq!(msg.first_blocknum, first_blocknum);
+ assert_eq!(msg.number_of_blocks, number_of_blocks);
+ },
+ _ => panic!("Expected MessageSendEvent::SendChannelRangeQuery")
+ };
+ }
+
+ // It should not enqueue a query when should_request_full_sync return false.
+ // The initial implementation allows syncing with the first 5 peers after
+ // which should_request_full_sync will return false
+ {
+ let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
+ let init_msg = Init { features: InitFeatures::known() };
+ for n in 1..7 {
+ let node_privkey = &SecretKey::from_slice(&[n; 32]).unwrap();
+ let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
+ net_graph_msg_handler.sync_routing_table(&node_id, &init_msg);
+ let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
+ if n <= 5 {
+ assert_eq!(events.len(), 1);
+ } else {
+ assert_eq!(events.len(), 0);
+ }
+
+ }
+ }
+ }
+
+ #[test]
+ fn handling_reply_channel_range() {
+ let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
+ let node_privkey_1 = &SecretKey::from_slice(&[42; 32]).unwrap();
+ let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_privkey_1);
+
+ let chain_hash = genesis_block(Network::Testnet).header.block_hash();
+
+ // Test receipt of a single reply that should enqueue an SCID query
+ // matching the SCIDs in the reply
+ {
+ let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, ReplyChannelRange {
+ chain_hash,
+ sync_complete: true,
+ first_blocknum: 0,
+ number_of_blocks: 2000,
+ short_channel_ids: vec![
+ 0x0003e0_000000_0000, // 992x0x0
+ 0x0003e8_000000_0000, // 1000x0x0
+ 0x0003e9_000000_0000, // 1001x0x0
+ 0x0003f0_000000_0000, // 1008x0x0
+ 0x00044c_000000_0000, // 1100x0x0
+ 0x0006e0_000000_0000, // 1760x0x0
+ ],
+ });
+ assert!(result.is_ok());
+
+ // We expect to emit a query_short_channel_ids message with the received scids
+ let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
+ assert_eq!(events.len(), 1);
+ match &events[0] {
+ MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
+ assert_eq!(node_id, &node_id_1);
+ assert_eq!(msg.chain_hash, chain_hash);
+ assert_eq!(msg.short_channel_ids, vec![
+ 0x0003e0_000000_0000, // 992x0x0
+ 0x0003e8_000000_0000, // 1000x0x0
+ 0x0003e9_000000_0000, // 1001x0x0
+ 0x0003f0_000000_0000, // 1008x0x0
+ 0x00044c_000000_0000, // 1100x0x0
+ 0x0006e0_000000_0000, // 1760x0x0
+ ]);
+ },
+ _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
+ }
+ }
+ }
+
+ #[test]
+ fn handling_reply_short_channel_ids() {
+ let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
+ let node_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
+ let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
+
+ let chain_hash = genesis_block(Network::Testnet).header.block_hash();
+
+ // Test receipt of a successful reply
+ {
+ let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, ReplyShortChannelIdsEnd {
+ chain_hash,
+ full_information: true,
+ });
+ assert!(result.is_ok());
+ }
+
+ // Test receipt of a reply that indicates the peer does not maintain up-to-date information
+ // for the chain_hash requested in the query.
+ {
+ let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, ReplyShortChannelIdsEnd {
+ chain_hash,
+ full_information: false,
+ });
+ assert!(result.is_err());
+ assert_eq!(result.err().unwrap().err, "Received reply_short_channel_ids_end with no information");
+ }
+ }
+
+ #[test]
+ fn handling_query_channel_range() {
+ let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
+
+ let chain_hash = genesis_block(Network::Testnet).header.block_hash();
+ let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
+ let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
+ let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
+ let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
+ let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
+ let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
+ let bitcoin_key_1 = PublicKey::from_secret_key(&secp_ctx, node_1_btckey);
+ let bitcoin_key_2 = PublicKey::from_secret_key(&secp_ctx, node_2_btckey);
+
+ let mut scids: Vec<u64> = vec![
+ scid_from_parts(0xfffffe, 0xffffff, 0xffff).unwrap(), // max
+ scid_from_parts(0xffffff, 0xffffff, 0xffff).unwrap(), // never
+ ];
+
+ // used for testing multipart reply across blocks
+ for block in 100000..=108001 {
+ scids.push(scid_from_parts(block, 0, 0).unwrap());
+ }
+
+ // used for testing resumption on same block
+ scids.push(scid_from_parts(108001, 1, 0).unwrap());
+
+ for scid in scids {
+ let unsigned_announcement = UnsignedChannelAnnouncement {
+ features: ChannelFeatures::known(),
+ chain_hash: chain_hash.clone(),
+ short_channel_id: scid,
+ node_id_1,
+ node_id_2,
+ bitcoin_key_1,
+ bitcoin_key_2,
+ excess_data: Vec::new(),
+ };
+
+ let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
+ let valid_announcement = ChannelAnnouncement {
+ node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
+ node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
+ bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
+ bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
+ contents: unsigned_announcement.clone(),
+ };
+ match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
+ Ok(_) => (),
+ _ => panic!()
+ };
+ }
+
+ // Error when number_of_blocks=0
+ do_handling_query_channel_range(
+ &net_graph_msg_handler,
+ &node_id_2,
+ QueryChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 0,
+ number_of_blocks: 0,
+ },
+ false,
+ vec![ReplyChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 0,
+ number_of_blocks: 0,
+ sync_complete: true,
+ short_channel_ids: vec![]
+ }]
+ );
+
+ // Error when wrong chain
+ do_handling_query_channel_range(
+ &net_graph_msg_handler,
+ &node_id_2,
+ QueryChannelRange {
+ chain_hash: genesis_block(Network::Bitcoin).header.block_hash(),
+ first_blocknum: 0,
+ number_of_blocks: 0xffff_ffff,
+ },
+ false,
+ vec![ReplyChannelRange {
+ chain_hash: genesis_block(Network::Bitcoin).header.block_hash(),
+ first_blocknum: 0,
+ number_of_blocks: 0xffff_ffff,
+ sync_complete: true,
+ short_channel_ids: vec![],
+ }]
+ );
+
+ // Error when first_blocknum > 0xffffff
+ do_handling_query_channel_range(
+ &net_graph_msg_handler,
+ &node_id_2,
+ QueryChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 0x01000000,
+ number_of_blocks: 0xffff_ffff,
+ },
+ false,
+ vec![ReplyChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 0x01000000,
+ number_of_blocks: 0xffff_ffff,
+ sync_complete: true,
+ short_channel_ids: vec![]
+ }]
+ );
+
+ // Empty reply when max valid SCID block num
+ do_handling_query_channel_range(
+ &net_graph_msg_handler,
+ &node_id_2,
+ QueryChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 0xffffff,
+ number_of_blocks: 1,
+ },
+ true,
+ vec![
+ ReplyChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 0xffffff,
+ number_of_blocks: 1,
+ sync_complete: true,
+ short_channel_ids: vec![]
+ },
+ ]
+ );
+
+ // No results in valid query range
+ do_handling_query_channel_range(
+ &net_graph_msg_handler,
+ &node_id_2,
+ QueryChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 1000,
+ number_of_blocks: 1000,
+ },
+ true,
+ vec![
+ ReplyChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 1000,
+ number_of_blocks: 1000,
+ sync_complete: true,
+ short_channel_ids: vec![],
+ }
+ ]
+ );
+
+ // Overflow first_blocknum + number_of_blocks
+ do_handling_query_channel_range(
+ &net_graph_msg_handler,
+ &node_id_2,
+ QueryChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 0xfe0000,
+ number_of_blocks: 0xffffffff,
+ },
+ true,
+ vec![
+ ReplyChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 0xfe0000,
+ number_of_blocks: 0xffffffff - 0xfe0000,
+ sync_complete: true,
+ short_channel_ids: vec![
+ 0xfffffe_ffffff_ffff, // max
+ ]
+ }
+ ]
+ );
+
+ // Single block exactly full
+ do_handling_query_channel_range(
+ &net_graph_msg_handler,
+ &node_id_2,
+ QueryChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 100000,
+ number_of_blocks: 8000,
+ },
+ true,
+ vec![
+ ReplyChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 100000,
+ number_of_blocks: 8000,
+ sync_complete: true,
+ short_channel_ids: (100000..=107999)
+ .map(|block| scid_from_parts(block, 0, 0).unwrap())
+ .collect(),
+ },
+ ]
+ );
+
+ // Multiple split on new block
+ do_handling_query_channel_range(
+ &net_graph_msg_handler,
+ &node_id_2,
+ QueryChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 100000,
+ number_of_blocks: 8001,
+ },
+ true,
+ vec![
+ ReplyChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 100000,
+ number_of_blocks: 7999,
+ sync_complete: false,
+ short_channel_ids: (100000..=107999)
+ .map(|block| scid_from_parts(block, 0, 0).unwrap())
+ .collect(),
+ },
+ ReplyChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 107999,
+ number_of_blocks: 2,
+ sync_complete: true,
+ short_channel_ids: vec![
+ scid_from_parts(108000, 0, 0).unwrap(),
+ ],
+ }
+ ]
+ );
+
+ // Multiple split on same block
+ do_handling_query_channel_range(
+ &net_graph_msg_handler,
+ &node_id_2,
+ QueryChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 100002,
+ number_of_blocks: 8000,
+ },
+ true,
+ vec![
+ ReplyChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 100002,
+ number_of_blocks: 7999,
+ sync_complete: false,
+ short_channel_ids: (100002..=108001)
+ .map(|block| scid_from_parts(block, 0, 0).unwrap())
+ .collect(),
+ },
+ ReplyChannelRange {
+ chain_hash: chain_hash.clone(),
+ first_blocknum: 108001,
+ number_of_blocks: 1,
+ sync_complete: true,
+ short_channel_ids: vec![
+ scid_from_parts(108001, 1, 0).unwrap(),
+ ],
+ }
+ ]
+ );
+ }
+
+ fn do_handling_query_channel_range(
+ net_graph_msg_handler: &NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
+ test_node_id: &PublicKey,
+ msg: QueryChannelRange,
+ expected_ok: bool,
+ expected_replies: Vec<ReplyChannelRange>
+ ) {
+ let mut max_firstblocknum = msg.first_blocknum.saturating_sub(1);
+ let mut c_lightning_0_9_prev_end_blocknum = max_firstblocknum;
+ let query_end_blocknum = msg.end_blocknum();
+ let result = net_graph_msg_handler.handle_query_channel_range(test_node_id, msg);
+
+ if expected_ok {
+ assert!(result.is_ok());
+ } else {
+ assert!(result.is_err());
+ }
+
+ let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
+ assert_eq!(events.len(), expected_replies.len());
+
+ for i in 0..events.len() {
+ let expected_reply = &expected_replies[i];
+ match &events[i] {
+ MessageSendEvent::SendReplyChannelRange { node_id, msg } => {
+ assert_eq!(node_id, test_node_id);
+ assert_eq!(msg.chain_hash, expected_reply.chain_hash);
+ assert_eq!(msg.first_blocknum, expected_reply.first_blocknum);
+ assert_eq!(msg.number_of_blocks, expected_reply.number_of_blocks);
+ assert_eq!(msg.sync_complete, expected_reply.sync_complete);
+ assert_eq!(msg.short_channel_ids, expected_reply.short_channel_ids);
+
+ // Enforce exactly the sequencing requirements present on c-lightning v0.9.3
+ assert!(msg.first_blocknum == c_lightning_0_9_prev_end_blocknum || msg.first_blocknum == c_lightning_0_9_prev_end_blocknum.saturating_add(1));
+ assert!(msg.first_blocknum >= max_firstblocknum);
+ max_firstblocknum = msg.first_blocknum;
+ c_lightning_0_9_prev_end_blocknum = msg.first_blocknum.saturating_add(msg.number_of_blocks);
+
+ // Check that the last block count is >= the query's end_blocknum
+ if i == events.len() - 1 {
+ assert!(msg.first_blocknum.saturating_add(msg.number_of_blocks) >= query_end_blocknum);
+ }
+ },
+ _ => panic!("expected MessageSendEvent::SendReplyChannelRange"),
+ }
+ }
+ }
+
+ #[test]
+ fn handling_query_short_channel_ids() {
+ let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
+ let node_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
+ let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
+
+ let chain_hash = genesis_block(Network::Testnet).header.block_hash();
+
+ let result = net_graph_msg_handler.handle_query_short_channel_ids(&node_id, QueryShortChannelIds {
+ chain_hash,
+ short_channel_ids: vec![0x0003e8_000000_0000],
+ });
+ assert!(result.is_err());
+ }
+}
+
+#[cfg(all(test, feature = "unstable"))]
+mod benches {
+ use super::*;
+
+ use test::Bencher;
+ use std::io::Read;
+
+ #[bench]
+ fn read_network_graph(bench: &mut Bencher) {
+ let mut d = ::routing::router::test_utils::get_route_file().unwrap();
+ let mut v = Vec::new();
+ d.read_to_end(&mut v).unwrap();
+ bench.iter(|| {
+ let _ = NetworkGraph::read(&mut std::io::Cursor::new(&v)).unwrap();
+ });
+ }
+
+ #[bench]
+ fn write_network_graph(bench: &mut Bencher) {
+ let mut d = ::routing::router::test_utils::get_route_file().unwrap();
+ let net_graph = NetworkGraph::read(&mut d).unwrap();
+ bench.iter(|| {
+ let _ = net_graph.encode();
+ });
+ }