-
- // Clean up scid_task
- net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
- }
-
- // Test receipt of multiple reply messages for a single query. This happens when the number
- // of scids in the query range exceeds the size limits of a single reply message. We expect
- // to initiate a query_short_channel_ids for the first batch of scids and we enqueue the
- // remaining scids for later processing. We remove the range query task after receipt of all
- // reply messages.
- {
- // Initiate a channel range query to create a query task
- let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
- assert!(result.is_ok());
-
- // Clear the SendRangeQuery event
- net_graph_msg_handler.get_and_clear_pending_msg_events();
-
- // Handle the first reply message
- let reply_1_scids = vec![
- 0x0003e8_000000_0000, // 1000x0x0
- 0x0003e9_000000_0000, // 1001x0x0
- 0x000419_000000_0000, // 1049x0x0
- ];
- let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
- chain_hash,
- full_information: true,
- first_blocknum: 1000,
- number_of_blocks: 50,
- short_channel_ids: reply_1_scids.clone(),
- });
- assert!(result.is_ok());
-
- // Handle the next reply in the sequence, which must start at the previous message's
- // first_blocknum plus number_of_blocks. The scids in this reply will be queued.
- let reply_2_scids = vec![
- 0x00041a_000000_0000, // 1050x0x0
- 0x000432_000000_0000, // 1074x0x0
- ];
- let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
- chain_hash,
- full_information: true,
- first_blocknum: 1050,
- number_of_blocks: 25,
- short_channel_ids: reply_2_scids.clone(),
- });
- assert!(result.is_ok());
-
- // Handle the final reply in the sequence, which must meet or exceed the initial query's
- // first_blocknum plus number_of_blocks. The scids in this reply will be queued.
- let reply_3_scids = vec![
- 0x000433_000000_0000, // 1075x0x0
- 0x00044b_000000_0000, // 1099x0x0
- ];
- let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
- chain_hash,
- full_information: true,
- first_blocknum: 1075,
- number_of_blocks: 25,
- short_channel_ids: reply_3_scids.clone(),
- });
- assert!(result.is_ok());
-
- // After the final reply we expect the query task to be removed
- assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
-
- // We expect to emit a query_short_channel_ids message with the accumulated scids that
- // match the queried channel range.
- 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, [reply_1_scids, reply_2_scids, reply_3_scids].concat());
- },
- _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
- }
-
- // Clean up scid_task
- net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
- }
-
- // Test receipt of a sequence of replies with a valid first reply and a second reply that
- // resumes on the same block as the first reply. The spec requires a subsequent
- // first_blocknum to equal the prior first_blocknum plus number_of_blocks, however
- // due to discrepancies in implementation we must loosen this restriction.
- {
- // Initiate a channel range query to create a query task
- let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
- assert!(result.is_ok());
-
- // Clear the SendRangeQuery event
- net_graph_msg_handler.get_and_clear_pending_msg_events();
-
- // Handle the first reply message
- let reply_1_scids = vec![
- 0x0003e8_000000_0000, // 1000x0x0
- 0x0003e9_000000_0000, // 1001x0x0
- 0x000419_000000_0000, // 1049x0x0
- ];
- let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
- chain_hash,
- full_information: true,
- first_blocknum: 1000,
- number_of_blocks: 50,
- short_channel_ids: reply_1_scids.clone(),
- });
- assert!(result.is_ok());
-
- // Handle the next reply in the sequence, which is non-spec but resumes on the last block
- // of the first message.
- let reply_2_scids = vec![
- 0x000419_000001_0000, // 1049x1x0
- 0x00041a_000000_0000, // 1050x0x0
- 0x000432_000000_0000, // 1074x0x0
- ];
- let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
- chain_hash,
- full_information: true,
- first_blocknum: 1049,
- number_of_blocks: 51,
- short_channel_ids: reply_2_scids.clone(),
- });
- assert!(result.is_ok());
-
- // After the final reply we expect the query task to be removed
- assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
-
- // We expect to emit a query_short_channel_ids message with the accumulated scids that
- // match the queried channel range
- 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, [reply_1_scids, reply_2_scids].concat());
- },
- _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
- }
-
- // Clean up scid_task
- net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
- }
-
- // Test receipt of reply with a chain_hash that does not match the query. We expect to return
- // an error and to remove the query task.
- {
- // Initiate a channel range query to create a query task
- let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
- assert!(result.is_ok());
-
- // Clear the SendRangeQuery event
- net_graph_msg_handler.get_and_clear_pending_msg_events();
-
- // Handle the reply with a mismatched chain_hash. We expect IgnoreError result and the
- // task should be removed.
- let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
- chain_hash: genesis_block(Network::Bitcoin).header.block_hash(),
- full_information: true,
- first_blocknum: 1000,
- number_of_blocks: 1050,
- short_channel_ids: vec![0x0003e8_000000_0000,0x0003e9_000000_0000,0x0003f0_000000_0000],
- });
- assert!(result.is_err());
- assert_eq!(result.err().unwrap().err, "Received reply_channel_range with invalid chain_hash");
- assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());