Implement gossip_queries sync methods in NetGraphMsgHandler
[rust-lightning] / lightning / src / routing / network_graph.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! The top-level network map tracking logic lives here.
11
12 use bitcoin::secp256k1::key::PublicKey;
13 use bitcoin::secp256k1::Secp256k1;
14 use bitcoin::secp256k1;
15
16 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
17 use bitcoin::hashes::Hash;
18 use bitcoin::blockdata::script::Builder;
19 use bitcoin::blockdata::transaction::TxOut;
20 use bitcoin::blockdata::opcodes;
21 use bitcoin::hash_types::BlockHash;
22
23 use chain;
24 use chain::Access;
25 use ln::features::{ChannelFeatures, NodeFeatures};
26 use ln::msgs::{DecodeError, ErrorAction, LightningError, RoutingMessageHandler, NetAddress, MAX_VALUE_MSAT};
27 use ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, OptionalField};
28 use ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd};
29 use ln::msgs;
30 use util::ser::{Writeable, Readable, Writer};
31 use util::logger::Logger;
32 use util::events;
33
34 use std::{cmp, fmt};
35 use std::sync::{RwLock, RwLockReadGuard};
36 use std::sync::atomic::{AtomicUsize, Ordering};
37 use std::sync::Mutex;
38 use std::collections::BTreeMap;
39 use std::collections::btree_map::Entry as BtreeEntry;
40 use std::collections::HashMap;
41 use std::ops::Deref;
42 use bitcoin::hashes::hex::ToHex;
43
44 /// Maximum number of short_channel_id values that can be encoded in a
45 /// single reply_channel_range or query_short_channel_ids messages when
46 /// using raw encoding. The maximum value ensures that the 8-byte SCIDs
47 /// fit inside the maximum size of the Lightning message, 65535-bytes.
48 const MAX_SHORT_CHANNEL_ID_BATCH_SIZE: usize = 8000;
49
50 /// Maximum number of reply_channel_range messages we will allow in
51 /// reply to a query_channel_range. This value creates an upper-limit 
52 /// on the number of SCIDs we process in reply to a single query.
53 const MAX_REPLY_CHANNEL_RANGE_PER_QUERY: usize = 250;
54
55 /// Represents the network as nodes and channels between them
56 #[derive(PartialEq)]
57 pub struct NetworkGraph {
58         channels: BTreeMap<u64, ChannelInfo>,
59         nodes: BTreeMap<PublicKey, NodeInfo>,
60 }
61
62 /// A simple newtype for RwLockReadGuard<'a, NetworkGraph>.
63 /// This exists only to make accessing a RwLock<NetworkGraph> possible from
64 /// the C bindings, as it can be done directly in Rust code.
65 pub struct LockedNetworkGraph<'a>(pub RwLockReadGuard<'a, NetworkGraph>);
66
67 /// Receives and validates network updates from peers,
68 /// stores authentic and relevant data as a network graph.
69 /// This network graph is then used for routing payments.
70 /// Provides interface to help with initial routing sync by
71 /// serving historical announcements.
72 pub struct NetGraphMsgHandler<C: Deref, L: Deref> where C::Target: chain::Access, L::Target: Logger {
73         secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
74         /// Representation of the payment channel network
75         pub network_graph: RwLock<NetworkGraph>,
76         chain_access: Option<C>,
77         full_syncs_requested: AtomicUsize,
78         pending_events: Mutex<Vec<events::MessageSendEvent>>,
79         chan_range_query_tasks: Mutex<HashMap<PublicKey, ChanRangeQueryTask>>,
80         scid_query_tasks: Mutex<HashMap<PublicKey, ScidQueryTask>>,
81         logger: L,
82 }
83
84 impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: chain::Access, L::Target: Logger {
85         /// Creates a new tracker of the actual state of the network of channels and nodes,
86         /// assuming a fresh network graph.
87         /// Chain monitor is used to make sure announced channels exist on-chain,
88         /// channel data is correct, and that the announcement is signed with
89         /// channel owners' keys.
90         pub fn new(chain_access: Option<C>, logger: L) -> Self {
91                 NetGraphMsgHandler {
92                         secp_ctx: Secp256k1::verification_only(),
93                         network_graph: RwLock::new(NetworkGraph {
94                                 channels: BTreeMap::new(),
95                                 nodes: BTreeMap::new(),
96                         }),
97                         full_syncs_requested: AtomicUsize::new(0),
98                         chain_access,
99                         pending_events: Mutex::new(vec![]),
100                         chan_range_query_tasks: Mutex::new(HashMap::new()),
101                         scid_query_tasks: Mutex::new(HashMap::new()),
102                         logger,
103                 }
104         }
105
106         /// Creates a new tracker of the actual state of the network of channels and nodes,
107         /// assuming an existing Network Graph.
108         pub fn from_net_graph(chain_access: Option<C>, logger: L, network_graph: NetworkGraph) -> Self {
109                 NetGraphMsgHandler {
110                         secp_ctx: Secp256k1::verification_only(),
111                         network_graph: RwLock::new(network_graph),
112                         full_syncs_requested: AtomicUsize::new(0),
113                         chain_access,
114                         pending_events: Mutex::new(vec![]),
115                         chan_range_query_tasks: Mutex::new(HashMap::new()),
116                         scid_query_tasks: Mutex::new(HashMap::new()),
117                         logger,
118                 }
119         }
120
121         /// Take a read lock on the network_graph and return it in the C-bindings
122         /// newtype helper. This is likely only useful when called via the C
123         /// bindings as you can call `self.network_graph.read().unwrap()` in Rust
124         /// yourself.
125         pub fn read_locked_graph<'a>(&'a self) -> LockedNetworkGraph<'a> {
126                 LockedNetworkGraph(self.network_graph.read().unwrap())
127         }
128
129         /// Enqueues a message send event for a batch of short_channel_ids
130         /// in a task.
131         fn finalize_query_short_ids(&self, task: &mut ScidQueryTask) {
132                 let scid_size = std::cmp::min(task.short_channel_ids.len(), MAX_SHORT_CHANNEL_ID_BATCH_SIZE);
133                 let mut short_channel_ids: Vec<u64> = Vec::with_capacity(scid_size);
134                 for scid in task.short_channel_ids.drain(..scid_size) {
135                         short_channel_ids.push(scid);
136                 }
137
138                 log_debug!(self.logger, "Sending query_short_channel_ids peer={}, batch_size={}", log_pubkey!(task.node_id), scid_size);
139
140                 // enqueue the message to the peer
141                 let mut pending_events = self.pending_events.lock().unwrap();
142                 pending_events.push(events::MessageSendEvent::SendShortIdsQuery {
143                         node_id: task.node_id.clone(),
144                         msg: QueryShortChannelIds {
145                                 chain_hash: task.chain_hash.clone(),
146                                 short_channel_ids,
147                         }
148                 });
149         }
150 }
151
152 impl<'a> LockedNetworkGraph<'a> {
153         /// Get a reference to the NetworkGraph which this read-lock contains.
154         pub fn graph(&self) -> &NetworkGraph {
155                 &*self.0
156         }
157 }
158
159
160 macro_rules! secp_verify_sig {
161         ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
162                 match $secp_ctx.verify($msg, $sig, $pubkey) {
163                         Ok(_) => {},
164                         Err(_) => return Err(LightningError{err: "Invalid signature from remote node".to_owned(), action: ErrorAction::IgnoreError}),
165                 }
166         };
167 }
168
169 impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for NetGraphMsgHandler<C, L> where C::Target: chain::Access, L::Target: Logger {
170         fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
171                 self.network_graph.write().unwrap().update_node_from_announcement(msg, &self.secp_ctx)?;
172                 Ok(msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty())
173         }
174
175         fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
176                 self.network_graph.write().unwrap().update_channel_from_announcement(msg, &self.chain_access, &self.secp_ctx)?;
177                 log_trace!(self.logger, "Added channel_announcement for {}{}", msg.contents.short_channel_id, if !msg.contents.excess_data.is_empty() { " with excess uninterpreted data!" } else { "" });
178                 Ok(msg.contents.excess_data.is_empty())
179         }
180
181         fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
182                 match update {
183                         &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
184                                 let _ = self.network_graph.write().unwrap().update_channel(msg, &self.secp_ctx);
185                         },
186                         &msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id, is_permanent } => {
187                                 self.network_graph.write().unwrap().close_channel_from_update(short_channel_id, is_permanent);
188                         },
189                         &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, is_permanent } => {
190                                 self.network_graph.write().unwrap().fail_node(node_id, is_permanent);
191                         },
192                 }
193         }
194
195         fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> {
196                 self.network_graph.write().unwrap().update_channel(msg, &self.secp_ctx)?;
197                 Ok(msg.contents.excess_data.is_empty())
198         }
199
200         fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)> {
201                 let network_graph = self.network_graph.read().unwrap();
202                 let mut result = Vec::with_capacity(batch_amount as usize);
203                 let mut iter = network_graph.get_channels().range(starting_point..);
204                 while result.len() < batch_amount as usize {
205                         if let Some((_, ref chan)) = iter.next() {
206                                 if chan.announcement_message.is_some() {
207                                         let chan_announcement = chan.announcement_message.clone().unwrap();
208                                         let mut one_to_two_announcement: Option<msgs::ChannelUpdate> = None;
209                                         let mut two_to_one_announcement: Option<msgs::ChannelUpdate> = None;
210                                         if let Some(one_to_two) = chan.one_to_two.as_ref() {
211                                                 one_to_two_announcement = one_to_two.last_update_message.clone();
212                                         }
213                                         if let Some(two_to_one) = chan.two_to_one.as_ref() {
214                                                 two_to_one_announcement = two_to_one.last_update_message.clone();
215                                         }
216                                         result.push((chan_announcement, one_to_two_announcement, two_to_one_announcement));
217                                 } else {
218                                         // TODO: We may end up sending un-announced channel_updates if we are sending
219                                         // initial sync data while receiving announce/updates for this channel.
220                                 }
221                         } else {
222                                 return result;
223                         }
224                 }
225                 result
226         }
227
228         fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement> {
229                 let network_graph = self.network_graph.read().unwrap();
230                 let mut result = Vec::with_capacity(batch_amount as usize);
231                 let mut iter = if let Some(pubkey) = starting_point {
232                                 let mut iter = network_graph.get_nodes().range((*pubkey)..);
233                                 iter.next();
234                                 iter
235                         } else {
236                                 network_graph.get_nodes().range(..)
237                         };
238                 while result.len() < batch_amount as usize {
239                         if let Some((_, ref node)) = iter.next() {
240                                 if let Some(node_info) = node.announcement_info.as_ref() {
241                                         if node_info.announcement_message.is_some() {
242                                                 result.push(node_info.announcement_message.clone().unwrap());
243                                         }
244                                 }
245                         } else {
246                                 return result;
247                         }
248                 }
249                 result
250         }
251
252         fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
253                 //TODO: Determine whether to request a full sync based on the network map.
254                 const FULL_SYNCS_TO_REQUEST: usize = 5;
255                 if self.full_syncs_requested.load(Ordering::Acquire) < FULL_SYNCS_TO_REQUEST {
256                         self.full_syncs_requested.fetch_add(1, Ordering::AcqRel);
257                         true
258                 } else {
259                         false
260                 }
261         }
262
263         fn query_channel_range(&self, their_node_id: &PublicKey, chain_hash: BlockHash, first_blocknum: u32, number_of_blocks: u32) -> Result<(), LightningError> {
264                 // We must ensure that we only have a single in-flight query
265                 // to the remote peer. If we already have a query, then we fail
266                 let mut query_range_tasks_lock = self.chan_range_query_tasks.lock().unwrap();
267                 let query_range_tasks = &mut *query_range_tasks_lock;
268                 if query_range_tasks.contains_key(their_node_id) {
269                         return Err(LightningError {
270                                 err: String::from("query_channel_range already in-flight"),
271                                 action: ErrorAction::IgnoreError,
272                         });
273                 }
274
275                 // Construct a new task to keep track of the query until the full
276                 // range query has been completed
277                 let task = ChanRangeQueryTask::new(their_node_id, chain_hash, first_blocknum, number_of_blocks);
278                 query_range_tasks.insert(their_node_id.clone(), task);
279
280                 // Enqueue the message send event
281                 log_debug!(self.logger, "Sending query_channel_range peer={}, first_blocknum={}, number_of_blocks={}", log_pubkey!(their_node_id), first_blocknum, number_of_blocks);
282                 let mut pending_events = self.pending_events.lock().unwrap();
283                 pending_events.push(events::MessageSendEvent::SendChannelRangeQuery {
284                         node_id: their_node_id.clone(),
285                         msg: QueryChannelRange {
286                                 chain_hash,
287                                 first_blocknum,
288                                 number_of_blocks,
289                         },
290                 });
291                 Ok(())
292         }
293
294         /// A query should only request channels referring to unspent outputs.
295         /// This method does not validate this requirement and expects the
296         /// caller to ensure SCIDs are unspent.
297         fn query_short_channel_ids(&self, their_node_id: &PublicKey, chain_hash: BlockHash, short_channel_ids: Vec<u64>) -> Result<(), LightningError> {
298                 // Create a new task or add to the existing task
299                 let mut query_scids_tasks_lock = self.scid_query_tasks.lock().unwrap();
300                 let query_scids_tasks = &mut *query_scids_tasks_lock;
301
302                 // For an existing task we append the short_channel_ids which will be sent when the
303                 // current in-flight batch completes.
304                 if let Some(task) = query_scids_tasks.get_mut(their_node_id) {
305                         task.add(short_channel_ids);
306                         return Ok(());
307                 }
308
309                 // For a new task we create the task with short_channel_ids and send the first
310                 // batch immediately.
311                 query_scids_tasks.insert(their_node_id.clone(), ScidQueryTask::new(
312                         their_node_id,
313                         chain_hash.clone(),
314                         short_channel_ids,
315                 ));
316                 let task = query_scids_tasks.get_mut(their_node_id).unwrap();
317                 self.finalize_query_short_ids(task);
318                 return Ok(());
319         }
320
321         fn handle_reply_channel_range(&self, their_node_id: &PublicKey, msg: &ReplyChannelRange) -> Result<(), LightningError> {
322                 log_debug!(self.logger, "Handling reply_channel_range peer={}, first_blocknum={}, number_of_blocks={}, full_information={}, scids={}", log_pubkey!(their_node_id), msg.first_blocknum, msg.number_of_blocks, msg.full_information, msg.short_channel_ids.len(),);
323
324                 // First we obtain a lock on the task hashmap. In order to avoid borrowing issues
325                 // we will access the task as needed.
326                 let mut query_range_tasks = self.chan_range_query_tasks.lock().unwrap();
327
328                 // If there is no currently executing task then we have received
329                 // an invalid message and will return an error
330                 if query_range_tasks.get(their_node_id).is_none() {
331                         return Err(LightningError {
332                                 err: String::from("Received unknown reply_channel_range message"),
333                                 action: ErrorAction::IgnoreError,
334                         });
335                 }
336
337                 // Now that we know we have a task, we can extract a few values for use
338                 // in validations without having to access the task repeatedly
339                 let (task_chain_hash, task_first_blocknum, task_number_of_blocks, task_received_first_block, task_received_last_block, task_number_of_replies) = {
340                         let task = query_range_tasks.get(their_node_id).unwrap();
341                         (task.chain_hash, task.first_blocknum, task.number_of_blocks, task.received_first_block, task.received_last_block, task.number_of_replies)
342                 };
343
344                 // Validate the chain_hash matches the chain_hash we used in the query.
345                 // If it does not, then the message is malformed and we return an error
346                 if msg.chain_hash != task_chain_hash {
347                         query_range_tasks.remove(their_node_id);
348                         return Err(LightningError {
349                                 err: String::from("Received reply_channel_range with invalid chain_hash"),
350                                 action: ErrorAction::IgnoreError,
351                         });
352                 }
353
354                 // Validate that the remote node maintains up-to-date channel
355                 // information for chain_hash. Some nodes use the full_information
356                 // flag to indicate multi-part messages so we must check whether
357                 // we received information as well.
358                 if !msg.full_information && msg.short_channel_ids.len() == 0 {
359                         query_range_tasks.remove(their_node_id);
360                         return Err(LightningError {
361                                 err: String::from("Received reply_channel_range with no information available"),
362                                 action: ErrorAction::IgnoreError,
363                         });
364                 }
365
366                 // Calculate the last block for the message and the task
367                 let msg_last_block = last_blocknum(msg.first_blocknum, msg.number_of_blocks);
368                 let task_last_block = last_blocknum(task_first_blocknum, task_number_of_blocks);
369
370                 // On the first message...
371                 if task_received_first_block.is_none() {
372                         // The replies can be a superset of the queried block range, but the
373                         // replies must include our requested query range. We check if the
374                         // start of the replies is greater than the start of our query. If
375                         // so, the start of our query is excluded and the message is malformed.
376                         if msg.first_blocknum > task_first_blocknum {
377                                 query_range_tasks.remove(their_node_id);
378                                 return Err(LightningError {
379                                         err: String::from("Failing reply_channel_range with invalid first_blocknum"),
380                                         action: ErrorAction::IgnoreError,
381                                 });
382                         }
383
384                         // Next, we ensure the reply has at least some information matching
385                         // our query. If the received last_blocknum is less than our query's
386                         // first_blocknum then the reply does not encompass the query range
387                         // and the message is malformed.
388                         if msg_last_block < task_first_blocknum {
389                                 query_range_tasks.remove(their_node_id);
390                                 return Err(LightningError {
391                                         err: String::from("Failing reply_channel_range with non-overlapping first reply"),
392                                         action: ErrorAction::IgnoreError,
393                                 });
394                         }
395
396                         // Capture the first block and last block so that subsequent messages
397                         // can be validated.
398                         let task = query_range_tasks.get_mut(their_node_id).unwrap();
399                         task.received_first_block = Some(msg.first_blocknum);
400                         task.received_last_block = Some(msg_last_block);
401                 }
402                 // On subsequent message(s)...
403                 else {
404                         // We need to validate the sequence of the reply message is expected.
405                         // Subsequent messages must set the first_blocknum to the previous
406                         // message's first_blocknum plus number_of_blocks. There is discrepancy
407                         // in implementation where some resume on the last sent block. We will
408                         // loosen the restriction and accept either, and otherwise consider the
409                         // message malformed and return an error.
410                         let task_received_last_block = task_received_last_block.unwrap();
411                         if msg.first_blocknum != task_received_last_block && msg.first_blocknum != task_received_last_block + 1 {
412                                 query_range_tasks.remove(their_node_id);
413                                 return Err(LightningError {
414                                         err: String::from("Failing reply_channel_range with invalid sequence"),
415                                         action: ErrorAction::IgnoreError,
416                                 });
417                         }
418
419                         // Next we check to see that we have received a realistic number of
420                         // reply messages for a query. This caps the allocation exposure
421                         // for short_channel_ids that will be batched and sent in query channels.
422                         if task_number_of_replies + 1 > MAX_REPLY_CHANNEL_RANGE_PER_QUERY {
423                                 query_range_tasks.remove(their_node_id);
424                                 return Err(LightningError {
425                                         err: String::from("Failing reply_channel_range due to excessive messages"),
426                                         action: ErrorAction::IgnoreError,
427                                 });
428                         }
429
430                         // Capture the last_block in our task so that subsequent messages
431                         // can be validated.
432                         let task = query_range_tasks.get_mut(their_node_id).unwrap();
433                         task.number_of_replies += 1;
434                         task.received_last_block = Some(msg_last_block);
435                 }
436
437                 // We filter the short_channel_ids to those inside the query range.
438                 // The most significant 3-bytes of the short_channel_id are the block.
439                 {
440                         let mut filtered_short_channel_ids: Vec<u64> = msg.short_channel_ids.clone().into_iter().filter(|short_channel_id| {
441                                 let block = short_channel_id >> 40;
442                                 return block >= query_range_tasks.get(their_node_id).unwrap().first_blocknum as u64 && block <= task_last_block as u64;
443                         }).collect();
444                         let task = query_range_tasks.get_mut(their_node_id).unwrap();
445                         task.short_channel_ids.append(&mut filtered_short_channel_ids);
446                 }
447
448                 // The final message is indicated by a last_blocknum that is equal to
449                 // or greater than the query's last_blocknum.
450                 if msg_last_block >= task_last_block {
451                         log_debug!(self.logger, "Completed query_channel_range: peer={}, first_blocknum={}, number_of_blocks={}", log_pubkey!(their_node_id), task_first_blocknum, task_number_of_blocks);
452
453                         // We can now fire off a query to obtain routing messages for the
454                         // accumulated short_channel_ids.
455                         {
456                                 let task = query_range_tasks.get_mut(their_node_id).unwrap();
457                                 let mut short_channel_ids = Vec::new();
458                                 std::mem::swap(&mut short_channel_ids, &mut task.short_channel_ids);
459                                 self.query_short_channel_ids(their_node_id, task.chain_hash, short_channel_ids)?;
460                         }
461
462                         // We can remove the query range task now that the query is complete.
463                         query_range_tasks.remove(their_node_id);
464                 }
465                 Ok(())
466         }
467
468         /// When a query is initiated the remote peer will begin streaming
469         /// gossip messages. In the event of a failure, we may have received
470         /// some channel information. Before trying with another peer, the
471         /// caller should update its set of SCIDs that need to be queried.
472         fn handle_reply_short_channel_ids_end(&self, their_node_id: &PublicKey, msg: &ReplyShortChannelIdsEnd) -> Result<(), LightningError> {
473                 log_debug!(self.logger, "Handling reply_short_channel_ids_end peer={}, full_information={}", log_pubkey!(their_node_id), msg.full_information);
474
475                 // First we obtain a lock on the task hashmap. In order to avoid borrowing issues
476                 // we will access the task as needed.
477                 let mut query_short_channel_ids_tasks = self.scid_query_tasks.lock().unwrap();
478
479                 // If there is no existing task then we have received an unknown
480                 // message and should return an error.
481                 if query_short_channel_ids_tasks.get(their_node_id).is_none() {
482                         return Err(LightningError {
483                                 err: String::from("Unknown reply_short_channel_ids_end message"),
484                                 action: ErrorAction::IgnoreError,
485                         });
486                 }
487
488                 // If the reply's chain_hash does not match the task's chain_hash then
489                 // the reply is malformed and we should return an error.
490                 if msg.chain_hash != query_short_channel_ids_tasks.get(their_node_id).unwrap().chain_hash {
491                         query_short_channel_ids_tasks.remove(their_node_id);
492                         return Err(LightningError {
493                                 err: String::from("Received reply_short_channel_ids_end with incorrect chain_hash"),
494                                 action: ErrorAction::IgnoreError
495                         });
496                 }
497
498                 // If the remote node does not have up-to-date information for the
499                 // chain_hash they will set full_information=false. We can fail
500                 // the result and try again with a different peer.
501                 if !msg.full_information {
502                         query_short_channel_ids_tasks.remove(their_node_id);
503                         return Err(LightningError {
504                                 err: String::from("Received reply_short_channel_ids_end with no information"),
505                                 action: ErrorAction::IgnoreError
506                         });
507                 }
508
509                 // If we have more scids to process we send the next batch in the task
510                 {
511                         let task = query_short_channel_ids_tasks.get_mut(their_node_id).unwrap();
512                         if task.short_channel_ids.len() > 0 {
513                                 self.finalize_query_short_ids(task);
514                                 return Ok(());
515                         }
516                 }
517
518                 // Otherwise the task is complete and we can remove it
519                 log_debug!(self.logger, "Completed query_short_channel_ids peer={}", log_pubkey!(their_node_id));
520                 query_short_channel_ids_tasks.remove(their_node_id);
521                 Ok(())
522         }
523
524         /// There are potential DoS vectors when handling inbound queries.
525         /// Handling requests with first_blocknum very far away may trigger repeated
526         /// disk I/O if the NetworkGraph is not fully in-memory.
527         fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: &QueryChannelRange) -> Result<(), LightningError> {
528                 // TODO
529                 Err(LightningError {
530                         err: String::from("Not implemented"),
531                         action: ErrorAction::IgnoreError,
532                 })
533         }
534
535         /// There are potential DoS vectors when handling inbound queries.
536         /// Handling requests with first_blocknum very far away may trigger repeated
537         /// disk I/O if the NetworkGraph is not fully in-memory.
538         fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: &QueryShortChannelIds) -> Result<(), LightningError> {
539                 // TODO
540                 Err(LightningError {
541                         err: String::from("Not implemented"),
542                         action: ErrorAction::IgnoreError,
543                 })
544         }
545 }
546
547 impl<C: Deref, L: Deref> events::MessageSendEventsProvider for NetGraphMsgHandler<C, L>
548 where
549         C::Target: chain::Access,
550         L::Target: Logger,
551 {
552         fn get_and_clear_pending_msg_events(&self) -> Vec<events::MessageSendEvent> {
553                 let mut ret = Vec::new();
554                 let mut pending_events = self.pending_events.lock().unwrap();
555                 std::mem::swap(&mut ret, &mut pending_events);
556                 ret
557         }
558 }
559
560 /// Safely calculates the last_blocknum given a first_blocknum and
561 /// number_of_blocks by returning the u32::MAX-1 if there is an overflow
562 fn last_blocknum(first_blocknum: u32, number_of_blocks: u32) -> u32 {
563         match first_blocknum.checked_add(number_of_blocks) {
564                 Some(val) => val - 1,
565                 None => 0xffff_ffff - 1,
566         }
567 }
568
569 /// Maintains state for a channel range query that we initiated.
570 /// The query may result in one or more reply_channel_range messages
571 /// being received. This struct helps determine the status of the query
572 /// when there are multiple replies. It also collects results for initiating
573 /// SCID queries.
574 ///
575 /// The task is complete and can be cleaned up when a reply meets or
576 /// exceeds the last block in the query. The collected SCIDs in the task
577 /// can be used to generate an ScidQueryTask.
578 ///
579 /// A query may fail if the recipient does not maintain up-to-date
580 /// information for the chain or if the recipient fails to reply within
581 /// a reasonable amount of time. In either event, the query can be
582 /// re-initiated with a different peer.
583 pub struct ChanRangeQueryTask {
584         /// The public key of the node we will be sending queries to
585         pub node_id: PublicKey,
586         /// The genesis hash of the blockchain being queried
587         pub chain_hash: BlockHash,
588         /// The height of the first block for the channel UTXOs being queried
589         pub first_blocknum: u32,
590         /// The number of blocks to include in the query results
591         pub number_of_blocks: u32,
592         /// Tracks the number of reply messages we have received
593         pub number_of_replies: usize,
594         /// The height of the first block received in a reply. This value
595         /// should be less than or equal to the first_blocknum requested in
596         /// the query_channel_range. This allows the range of the replies to
597         /// contain, but not necessarily strictly, the queried range.
598         pub received_first_block: Option<u32>,
599         /// The height of the last block received in a reply. This value
600         /// will get incrementally closer to the target of
601         /// first_blocknum plus number_of_blocks from the query_channel_range.
602         pub received_last_block: Option<u32>,
603         /// Contains short_channel_ids received in one or more reply messages.
604         /// These will be sent in one ore more query_short_channel_ids messages
605         /// when the task is complete.
606         pub short_channel_ids: Vec<u64>,
607 }
608
609 impl ChanRangeQueryTask {
610         /// Constructs a new GossipQueryRangeTask
611         pub fn new(their_node_id: &PublicKey, chain_hash: BlockHash, first_blocknum: u32, number_of_blocks: u32) -> Self {
612                 ChanRangeQueryTask {
613                         node_id: their_node_id.clone(),
614                         chain_hash,
615                         first_blocknum,
616                         number_of_blocks,
617                         number_of_replies: 0,
618                         received_first_block: None,
619                         received_last_block: None,
620                         short_channel_ids: vec![],
621                 }
622         }
623 }
624
625 /// Maintains state when sending one or more short_channel_ids messages
626 /// to a peer. Only a single SCID query can be in-flight with a peer. The
627 /// number of SCIDs per query is limited by the size of a Lightning message
628 /// payload. When querying a large number of SCIDs (results of a large
629 /// channel range query for instance), multiple query_short_channel_ids
630 /// messages need to be sent. This task maintains the list of awaiting
631 /// SCIDs to be queried.
632 ///
633 /// When a successful reply_short_channel_ids_end message is received, the
634 /// next batch of SCIDs can be sent. When no remaining SCIDs exist in the
635 /// task, the task is complete and can be cleaned up.
636 ///
637 /// The recipient may reply indicating that up-to-date information for the
638 /// chain is not maintained. A query may also fail to complete within a
639 /// reasonable amount of time. In either event, the short_channel_ids
640 /// can be queried from a different peer after validating the set of
641 /// SCIDs that still need to be queried.
642 pub struct ScidQueryTask {
643         /// The public key of the node we will be sending queries to
644         pub node_id: PublicKey,
645         /// The genesis hash of the blockchain being queried
646         pub chain_hash: BlockHash,
647         /// A vector of short_channel_ids that we would like routing gossip
648         /// information for. This list will be chunked and sent to the peer
649         /// in one or more query_short_channel_ids messages.
650         pub short_channel_ids: Vec<u64>,
651 }
652
653 impl ScidQueryTask {
654         /// Constructs a new GossipQueryShortChannelIdsTask
655         pub fn new(their_node_id: &PublicKey, chain_hash: BlockHash, short_channel_ids: Vec<u64>) -> Self {
656                 ScidQueryTask {
657                         node_id: their_node_id.clone(),
658                         chain_hash,
659                         short_channel_ids,
660                 }
661         }
662
663         /// Adds short_channel_ids to the pending list of short_channel_ids
664         /// to be sent in the next request. You can add additional values
665         /// while a query is in-flight. These new values will be sent once
666         /// the active query has completed.
667         pub fn add(&mut self, mut short_channel_ids: Vec<u64>) {
668                 self.short_channel_ids.append(&mut short_channel_ids);
669         }
670 }
671
672 #[derive(PartialEq, Debug)]
673 /// Details about one direction of a channel. Received
674 /// within a channel update.
675 pub struct DirectionalChannelInfo {
676         /// When the last update to the channel direction was issued.
677         /// Value is opaque, as set in the announcement.
678         pub last_update: u32,
679         /// Whether the channel can be currently used for payments (in this one direction).
680         pub enabled: bool,
681         /// The difference in CLTV values that you must have when routing through this channel.
682         pub cltv_expiry_delta: u16,
683         /// The minimum value, which must be relayed to the next hop via the channel
684         pub htlc_minimum_msat: u64,
685         /// The maximum value which may be relayed to the next hop via the channel.
686         pub htlc_maximum_msat: Option<u64>,
687         /// Fees charged when the channel is used for routing
688         pub fees: RoutingFees,
689         /// Most recent update for the channel received from the network
690         /// Mostly redundant with the data we store in fields explicitly.
691         /// Everything else is useful only for sending out for initial routing sync.
692         /// Not stored if contains excess data to prevent DoS.
693         pub last_update_message: Option<ChannelUpdate>,
694 }
695
696 impl fmt::Display for DirectionalChannelInfo {
697         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
698                 write!(f, "last_update {}, enabled {}, cltv_expiry_delta {}, htlc_minimum_msat {}, fees {:?}", self.last_update, self.enabled, self.cltv_expiry_delta, self.htlc_minimum_msat, self.fees)?;
699                 Ok(())
700         }
701 }
702
703 impl_writeable!(DirectionalChannelInfo, 0, {
704         last_update,
705         enabled,
706         cltv_expiry_delta,
707         htlc_minimum_msat,
708         htlc_maximum_msat,
709         fees,
710         last_update_message
711 });
712
713 #[derive(PartialEq)]
714 /// Details about a channel (both directions).
715 /// Received within a channel announcement.
716 pub struct ChannelInfo {
717         /// Protocol features of a channel communicated during its announcement
718         pub features: ChannelFeatures,
719         /// Source node of the first direction of a channel
720         pub node_one: PublicKey,
721         /// Details about the first direction of a channel
722         pub one_to_two: Option<DirectionalChannelInfo>,
723         /// Source node of the second direction of a channel
724         pub node_two: PublicKey,
725         /// Details about the second direction of a channel
726         pub two_to_one: Option<DirectionalChannelInfo>,
727         /// The channel capacity as seen on-chain, if chain lookup is available.
728         pub capacity_sats: Option<u64>,
729         /// An initial announcement of the channel
730         /// Mostly redundant with the data we store in fields explicitly.
731         /// Everything else is useful only for sending out for initial routing sync.
732         /// Not stored if contains excess data to prevent DoS.
733         pub announcement_message: Option<ChannelAnnouncement>,
734 }
735
736 impl fmt::Display for ChannelInfo {
737         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
738                 write!(f, "features: {}, node_one: {}, one_to_two: {:?}, node_two: {}, two_to_one: {:?}",
739                    log_bytes!(self.features.encode()), log_pubkey!(self.node_one), self.one_to_two, log_pubkey!(self.node_two), self.two_to_one)?;
740                 Ok(())
741         }
742 }
743
744 impl_writeable!(ChannelInfo, 0, {
745         features,
746         node_one,
747         one_to_two,
748         node_two,
749         two_to_one,
750         capacity_sats,
751         announcement_message
752 });
753
754
755 /// Fees for routing via a given channel or a node
756 #[derive(Eq, PartialEq, Copy, Clone, Debug)]
757 pub struct RoutingFees {
758         /// Flat routing fee in satoshis
759         pub base_msat: u32,
760         /// Liquidity-based routing fee in millionths of a routed amount.
761         /// In other words, 10000 is 1%.
762         pub proportional_millionths: u32,
763 }
764
765 impl Readable for RoutingFees{
766         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<RoutingFees, DecodeError> {
767                 let base_msat: u32 = Readable::read(reader)?;
768                 let proportional_millionths: u32 = Readable::read(reader)?;
769                 Ok(RoutingFees {
770                         base_msat,
771                         proportional_millionths,
772                 })
773         }
774 }
775
776 impl Writeable for RoutingFees {
777         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
778                 self.base_msat.write(writer)?;
779                 self.proportional_millionths.write(writer)?;
780                 Ok(())
781         }
782 }
783
784 #[derive(PartialEq, Debug)]
785 /// Information received in the latest node_announcement from this node.
786 pub struct NodeAnnouncementInfo {
787         /// Protocol features the node announced support for
788         pub features: NodeFeatures,
789         /// When the last known update to the node state was issued.
790         /// Value is opaque, as set in the announcement.
791         pub last_update: u32,
792         /// Color assigned to the node
793         pub rgb: [u8; 3],
794         /// Moniker assigned to the node.
795         /// May be invalid or malicious (eg control chars),
796         /// should not be exposed to the user.
797         pub alias: [u8; 32],
798         /// Internet-level addresses via which one can connect to the node
799         pub addresses: Vec<NetAddress>,
800         /// An initial announcement of the node
801         /// Mostly redundant with the data we store in fields explicitly.
802         /// Everything else is useful only for sending out for initial routing sync.
803         /// Not stored if contains excess data to prevent DoS.
804         pub announcement_message: Option<NodeAnnouncement>
805 }
806
807 impl Writeable for NodeAnnouncementInfo {
808         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
809                 self.features.write(writer)?;
810                 self.last_update.write(writer)?;
811                 self.rgb.write(writer)?;
812                 self.alias.write(writer)?;
813                 (self.addresses.len() as u64).write(writer)?;
814                 for ref addr in &self.addresses {
815                         addr.write(writer)?;
816                 }
817                 self.announcement_message.write(writer)?;
818                 Ok(())
819         }
820 }
821
822 impl Readable for NodeAnnouncementInfo {
823         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeAnnouncementInfo, DecodeError> {
824                 let features = Readable::read(reader)?;
825                 let last_update = Readable::read(reader)?;
826                 let rgb = Readable::read(reader)?;
827                 let alias = Readable::read(reader)?;
828                 let addresses_count: u64 = Readable::read(reader)?;
829                 let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
830                 for _ in 0..addresses_count {
831                         match Readable::read(reader) {
832                                 Ok(Ok(addr)) => { addresses.push(addr); },
833                                 Ok(Err(_)) => return Err(DecodeError::InvalidValue),
834                                 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
835                                 _ => unreachable!(),
836                         }
837                 }
838                 let announcement_message = Readable::read(reader)?;
839                 Ok(NodeAnnouncementInfo {
840                         features,
841                         last_update,
842                         rgb,
843                         alias,
844                         addresses,
845                         announcement_message
846                 })
847         }
848 }
849
850 #[derive(PartialEq)]
851 /// Details about a node in the network, known from the network announcement.
852 pub struct NodeInfo {
853         /// All valid channels a node has announced
854         pub channels: Vec<u64>,
855         /// Lowest fees enabling routing via any of the enabled, known channels to a node.
856         /// The two fields (flat and proportional fee) are independent,
857         /// meaning they don't have to refer to the same channel.
858         pub lowest_inbound_channel_fees: Option<RoutingFees>,
859         /// More information about a node from node_announcement.
860         /// Optional because we store a Node entry after learning about it from
861         /// a channel announcement, but before receiving a node announcement.
862         pub announcement_info: Option<NodeAnnouncementInfo>
863 }
864
865 impl fmt::Display for NodeInfo {
866         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
867                 write!(f, "lowest_inbound_channel_fees: {:?}, channels: {:?}, announcement_info: {:?}",
868                    self.lowest_inbound_channel_fees, &self.channels[..], self.announcement_info)?;
869                 Ok(())
870         }
871 }
872
873 impl Writeable for NodeInfo {
874         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
875                 (self.channels.len() as u64).write(writer)?;
876                 for ref chan in self.channels.iter() {
877                         chan.write(writer)?;
878                 }
879                 self.lowest_inbound_channel_fees.write(writer)?;
880                 self.announcement_info.write(writer)?;
881                 Ok(())
882         }
883 }
884
885 const MAX_ALLOC_SIZE: u64 = 64*1024;
886
887 impl Readable for NodeInfo {
888         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeInfo, DecodeError> {
889                 let channels_count: u64 = Readable::read(reader)?;
890                 let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
891                 for _ in 0..channels_count {
892                         channels.push(Readable::read(reader)?);
893                 }
894                 let lowest_inbound_channel_fees = Readable::read(reader)?;
895                 let announcement_info = Readable::read(reader)?;
896                 Ok(NodeInfo {
897                         channels,
898                         lowest_inbound_channel_fees,
899                         announcement_info,
900                 })
901         }
902 }
903
904 impl Writeable for NetworkGraph {
905         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
906                 (self.channels.len() as u64).write(writer)?;
907                 for (ref chan_id, ref chan_info) in self.channels.iter() {
908                         (*chan_id).write(writer)?;
909                         chan_info.write(writer)?;
910                 }
911                 (self.nodes.len() as u64).write(writer)?;
912                 for (ref node_id, ref node_info) in self.nodes.iter() {
913                         node_id.write(writer)?;
914                         node_info.write(writer)?;
915                 }
916                 Ok(())
917         }
918 }
919
920 impl Readable for NetworkGraph {
921         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NetworkGraph, DecodeError> {
922                 let channels_count: u64 = Readable::read(reader)?;
923                 let mut channels = BTreeMap::new();
924                 for _ in 0..channels_count {
925                         let chan_id: u64 = Readable::read(reader)?;
926                         let chan_info = Readable::read(reader)?;
927                         channels.insert(chan_id, chan_info);
928                 }
929                 let nodes_count: u64 = Readable::read(reader)?;
930                 let mut nodes = BTreeMap::new();
931                 for _ in 0..nodes_count {
932                         let node_id = Readable::read(reader)?;
933                         let node_info = Readable::read(reader)?;
934                         nodes.insert(node_id, node_info);
935                 }
936                 Ok(NetworkGraph {
937                         channels,
938                         nodes,
939                 })
940         }
941 }
942
943 impl fmt::Display for NetworkGraph {
944         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
945                 writeln!(f, "Network map\n[Channels]")?;
946                 for (key, val) in self.channels.iter() {
947                         writeln!(f, " {}: {}", key, val)?;
948                 }
949                 writeln!(f, "[Nodes]")?;
950                 for (key, val) in self.nodes.iter() {
951                         writeln!(f, " {}: {}", log_pubkey!(key), val)?;
952                 }
953                 Ok(())
954         }
955 }
956
957 impl NetworkGraph {
958         /// Returns all known valid channels' short ids along with announced channel info.
959         ///
960         /// (C-not exported) because we have no mapping for `BTreeMap`s
961         pub fn get_channels<'a>(&'a self) -> &'a BTreeMap<u64, ChannelInfo> { &self.channels }
962         /// Returns all known nodes' public keys along with announced node info.
963         ///
964         /// (C-not exported) because we have no mapping for `BTreeMap`s
965         pub fn get_nodes<'a>(&'a self) -> &'a BTreeMap<PublicKey, NodeInfo> { &self.nodes }
966
967         /// Get network addresses by node id.
968         /// Returns None if the requested node is completely unknown,
969         /// or if node announcement for the node was never received.
970         ///
971         /// (C-not exported) as there is no practical way to track lifetimes of returned values.
972         pub fn get_addresses<'a>(&'a self, pubkey: &PublicKey) -> Option<&'a Vec<NetAddress>> {
973                 if let Some(node) = self.nodes.get(pubkey) {
974                         if let Some(node_info) = node.announcement_info.as_ref() {
975                                 return Some(&node_info.addresses)
976                         }
977                 }
978                 None
979         }
980
981         /// Creates a new, empty, network graph.
982         pub fn new() -> NetworkGraph {
983                 Self {
984                         channels: BTreeMap::new(),
985                         nodes: BTreeMap::new(),
986                 }
987         }
988
989         /// For an already known node (from channel announcements), update its stored properties from a
990         /// given node announcement.
991         ///
992         /// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
993         /// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
994         /// routing messages from a source using a protocol other than the lightning P2P protocol.
995         pub fn update_node_from_announcement<T: secp256k1::Verification>(&mut self, msg: &msgs::NodeAnnouncement, secp_ctx: &Secp256k1<T>) -> Result<(), LightningError> {
996                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
997                 secp_verify_sig!(secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
998                 self.update_node_from_announcement_intern(&msg.contents, Some(&msg))
999         }
1000
1001         /// For an already known node (from channel announcements), update its stored properties from a
1002         /// given node announcement without verifying the associated signatures. Because we aren't
1003         /// given the associated signatures here we cannot relay the node announcement to any of our
1004         /// peers.
1005         pub fn update_node_from_unsigned_announcement(&mut self, msg: &msgs::UnsignedNodeAnnouncement) -> Result<(), LightningError> {
1006                 self.update_node_from_announcement_intern(msg, None)
1007         }
1008
1009         fn update_node_from_announcement_intern(&mut self, msg: &msgs::UnsignedNodeAnnouncement, full_msg: Option<&msgs::NodeAnnouncement>) -> Result<(), LightningError> {
1010                 match self.nodes.get_mut(&msg.node_id) {
1011                         None => Err(LightningError{err: "No existing channels for node_announcement".to_owned(), action: ErrorAction::IgnoreError}),
1012                         Some(node) => {
1013                                 if let Some(node_info) = node.announcement_info.as_ref() {
1014                                         if node_info.last_update  >= msg.timestamp {
1015                                                 return Err(LightningError{err: "Update older than last processed update".to_owned(), action: ErrorAction::IgnoreError});
1016                                         }
1017                                 }
1018
1019                                 let should_relay = msg.excess_data.is_empty() && msg.excess_address_data.is_empty();
1020                                 node.announcement_info = Some(NodeAnnouncementInfo {
1021                                         features: msg.features.clone(),
1022                                         last_update: msg.timestamp,
1023                                         rgb: msg.rgb,
1024                                         alias: msg.alias,
1025                                         addresses: msg.addresses.clone(),
1026                                         announcement_message: if should_relay { full_msg.cloned() } else { None },
1027                                 });
1028
1029                                 Ok(())
1030                         }
1031                 }
1032         }
1033
1034         /// Store or update channel info from a channel announcement.
1035         ///
1036         /// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
1037         /// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
1038         /// routing messages from a source using a protocol other than the lightning P2P protocol.
1039         ///
1040         /// If a `chain::Access` object is provided via `chain_access`, it will be called to verify
1041         /// the corresponding UTXO exists on chain and is correctly-formatted.
1042         pub fn update_channel_from_announcement<T: secp256k1::Verification, C: Deref>
1043                         (&mut self, msg: &msgs::ChannelAnnouncement, chain_access: &Option<C>, secp_ctx: &Secp256k1<T>)
1044                         -> Result<(), LightningError>
1045                         where C::Target: chain::Access {
1046                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
1047                 secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
1048                 secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
1049                 secp_verify_sig!(secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
1050                 secp_verify_sig!(secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
1051                 self.update_channel_from_unsigned_announcement_intern(&msg.contents, Some(msg), chain_access)
1052         }
1053
1054         /// Store or update channel info from a channel announcement without verifying the associated
1055         /// signatures. Because we aren't given the associated signatures here we cannot relay the
1056         /// channel announcement to any of our peers.
1057         ///
1058         /// If a `chain::Access` object is provided via `chain_access`, it will be called to verify
1059         /// the corresponding UTXO exists on chain and is correctly-formatted.
1060         pub fn update_channel_from_unsigned_announcement<C: Deref>
1061                         (&mut self, msg: &msgs::UnsignedChannelAnnouncement, chain_access: &Option<C>)
1062                         -> Result<(), LightningError>
1063                         where C::Target: chain::Access {
1064                 self.update_channel_from_unsigned_announcement_intern(msg, None, chain_access)
1065         }
1066
1067         fn update_channel_from_unsigned_announcement_intern<C: Deref>
1068                         (&mut self, msg: &msgs::UnsignedChannelAnnouncement, full_msg: Option<&msgs::ChannelAnnouncement>, chain_access: &Option<C>)
1069                         -> Result<(), LightningError>
1070                         where C::Target: chain::Access {
1071                 if msg.node_id_1 == msg.node_id_2 || msg.bitcoin_key_1 == msg.bitcoin_key_2 {
1072                         return Err(LightningError{err: "Channel announcement node had a channel with itself".to_owned(), action: ErrorAction::IgnoreError});
1073                 }
1074
1075                 let utxo_value = match &chain_access {
1076                         &None => {
1077                                 // Tentatively accept, potentially exposing us to DoS attacks
1078                                 None
1079                         },
1080                         &Some(ref chain_access) => {
1081                                 match chain_access.get_utxo(&msg.chain_hash, msg.short_channel_id) {
1082                                         Ok(TxOut { value, script_pubkey }) => {
1083                                                 let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
1084                                                                                     .push_slice(&msg.bitcoin_key_1.serialize())
1085                                                                                     .push_slice(&msg.bitcoin_key_2.serialize())
1086                                                                                     .push_opcode(opcodes::all::OP_PUSHNUM_2)
1087                                                                                     .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
1088                                                 if script_pubkey != expected_script {
1089                                                         return Err(LightningError{err: format!("Channel announcement key ({}) didn't match on-chain script ({})", script_pubkey.to_hex(), expected_script.to_hex()), action: ErrorAction::IgnoreError});
1090                                                 }
1091                                                 //TODO: Check if value is worth storing, use it to inform routing, and compare it
1092                                                 //to the new HTLC max field in channel_update
1093                                                 Some(value)
1094                                         },
1095                                         Err(chain::AccessError::UnknownChain) => {
1096                                                 return Err(LightningError{err: format!("Channel announced on an unknown chain ({})", msg.chain_hash.encode().to_hex()), action: ErrorAction::IgnoreError});
1097                                         },
1098                                         Err(chain::AccessError::UnknownTx) => {
1099                                                 return Err(LightningError{err: "Channel announced without corresponding UTXO entry".to_owned(), action: ErrorAction::IgnoreError});
1100                                         },
1101                                 }
1102                         },
1103                 };
1104
1105                 let chan_info = ChannelInfo {
1106                                 features: msg.features.clone(),
1107                                 node_one: msg.node_id_1.clone(),
1108                                 one_to_two: None,
1109                                 node_two: msg.node_id_2.clone(),
1110                                 two_to_one: None,
1111                                 capacity_sats: utxo_value,
1112                                 announcement_message: if msg.excess_data.is_empty() { full_msg.cloned() } else { None },
1113                         };
1114
1115                 match self.channels.entry(msg.short_channel_id) {
1116                         BtreeEntry::Occupied(mut entry) => {
1117                                 //TODO: because asking the blockchain if short_channel_id is valid is only optional
1118                                 //in the blockchain API, we need to handle it smartly here, though it's unclear
1119                                 //exactly how...
1120                                 if utxo_value.is_some() {
1121                                         // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
1122                                         // only sometimes returns results. In any case remove the previous entry. Note
1123                                         // that the spec expects us to "blacklist" the node_ids involved, but we can't
1124                                         // do that because
1125                                         // a) we don't *require* a UTXO provider that always returns results.
1126                                         // b) we don't track UTXOs of channels we know about and remove them if they
1127                                         //    get reorg'd out.
1128                                         // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
1129                                         Self::remove_channel_in_nodes(&mut self.nodes, &entry.get(), msg.short_channel_id);
1130                                         *entry.get_mut() = chan_info;
1131                                 } else {
1132                                         return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreError})
1133                                 }
1134                         },
1135                         BtreeEntry::Vacant(entry) => {
1136                                 entry.insert(chan_info);
1137                         }
1138                 };
1139
1140                 macro_rules! add_channel_to_node {
1141                         ( $node_id: expr ) => {
1142                                 match self.nodes.entry($node_id) {
1143                                         BtreeEntry::Occupied(node_entry) => {
1144                                                 node_entry.into_mut().channels.push(msg.short_channel_id);
1145                                         },
1146                                         BtreeEntry::Vacant(node_entry) => {
1147                                                 node_entry.insert(NodeInfo {
1148                                                         channels: vec!(msg.short_channel_id),
1149                                                         lowest_inbound_channel_fees: None,
1150                                                         announcement_info: None,
1151                                                 });
1152                                         }
1153                                 }
1154                         };
1155                 }
1156
1157                 add_channel_to_node!(msg.node_id_1);
1158                 add_channel_to_node!(msg.node_id_2);
1159
1160                 Ok(())
1161         }
1162
1163         /// Close a channel if a corresponding HTLC fail was sent.
1164         /// If permanent, removes a channel from the local storage.
1165         /// May cause the removal of nodes too, if this was their last channel.
1166         /// If not permanent, makes channels unavailable for routing.
1167         pub fn close_channel_from_update(&mut self, short_channel_id: u64, is_permanent: bool) {
1168                 if is_permanent {
1169                         if let Some(chan) = self.channels.remove(&short_channel_id) {
1170                                 Self::remove_channel_in_nodes(&mut self.nodes, &chan, short_channel_id);
1171                         }
1172                 } else {
1173                         if let Some(chan) = self.channels.get_mut(&short_channel_id) {
1174                                 if let Some(one_to_two) = chan.one_to_two.as_mut() {
1175                                         one_to_two.enabled = false;
1176                                 }
1177                                 if let Some(two_to_one) = chan.two_to_one.as_mut() {
1178                                         two_to_one.enabled = false;
1179                                 }
1180                         }
1181                 }
1182         }
1183
1184         fn fail_node(&mut self, _node_id: &PublicKey, is_permanent: bool) {
1185                 if is_permanent {
1186                         // TODO: Wholly remove the node
1187                 } else {
1188                         // TODO: downgrade the node
1189                 }
1190         }
1191
1192         /// For an already known (from announcement) channel, update info about one of the directions
1193         /// of the channel.
1194         ///
1195         /// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
1196         /// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
1197         /// routing messages from a source using a protocol other than the lightning P2P protocol.
1198         pub fn update_channel<T: secp256k1::Verification>(&mut self, msg: &msgs::ChannelUpdate, secp_ctx: &Secp256k1<T>) -> Result<(), LightningError> {
1199                 self.update_channel_intern(&msg.contents, Some(&msg), Some((&msg.signature, secp_ctx)))
1200         }
1201
1202         /// For an already known (from announcement) channel, update info about one of the directions
1203         /// of the channel without verifying the associated signatures. Because we aren't given the
1204         /// associated signatures here we cannot relay the channel update to any of our peers.
1205         pub fn update_channel_unsigned(&mut self, msg: &msgs::UnsignedChannelUpdate) -> Result<(), LightningError> {
1206                 self.update_channel_intern(msg, None, None::<(&secp256k1::Signature, &Secp256k1<secp256k1::VerifyOnly>)>)
1207         }
1208
1209         fn update_channel_intern<T: secp256k1::Verification>(&mut self, msg: &msgs::UnsignedChannelUpdate, full_msg: Option<&msgs::ChannelUpdate>, sig_info: Option<(&secp256k1::Signature, &Secp256k1<T>)>) -> Result<(), LightningError> {
1210                 let dest_node_id;
1211                 let chan_enabled = msg.flags & (1 << 1) != (1 << 1);
1212                 let chan_was_enabled;
1213
1214                 match self.channels.get_mut(&msg.short_channel_id) {
1215                         None => return Err(LightningError{err: "Couldn't find channel for update".to_owned(), action: ErrorAction::IgnoreError}),
1216                         Some(channel) => {
1217                                 if let OptionalField::Present(htlc_maximum_msat) = msg.htlc_maximum_msat {
1218                                         if htlc_maximum_msat > MAX_VALUE_MSAT {
1219                                                 return Err(LightningError{err: "htlc_maximum_msat is larger than maximum possible msats".to_owned(), action: ErrorAction::IgnoreError});
1220                                         }
1221
1222                                         if let Some(capacity_sats) = channel.capacity_sats {
1223                                                 // It's possible channel capacity is available now, although it wasn't available at announcement (so the field is None).
1224                                                 // Don't query UTXO set here to reduce DoS risks.
1225                                                 if capacity_sats > MAX_VALUE_MSAT / 1000 || htlc_maximum_msat > capacity_sats * 1000 {
1226                                                         return Err(LightningError{err: "htlc_maximum_msat is larger than channel capacity or capacity is bogus".to_owned(), action: ErrorAction::IgnoreError});
1227                                                 }
1228                                         }
1229                                 }
1230                                 macro_rules! maybe_update_channel_info {
1231                                         ( $target: expr, $src_node: expr) => {
1232                                                 if let Some(existing_chan_info) = $target.as_ref() {
1233                                                         if existing_chan_info.last_update >= msg.timestamp {
1234                                                                 return Err(LightningError{err: "Update older than last processed update".to_owned(), action: ErrorAction::IgnoreError});
1235                                                         }
1236                                                         chan_was_enabled = existing_chan_info.enabled;
1237                                                 } else {
1238                                                         chan_was_enabled = false;
1239                                                 }
1240
1241                                                 let last_update_message = if msg.excess_data.is_empty() { full_msg.cloned() } else { None };
1242
1243                                                 let updated_channel_dir_info = DirectionalChannelInfo {
1244                                                         enabled: chan_enabled,
1245                                                         last_update: msg.timestamp,
1246                                                         cltv_expiry_delta: msg.cltv_expiry_delta,
1247                                                         htlc_minimum_msat: msg.htlc_minimum_msat,
1248                                                         htlc_maximum_msat: if let OptionalField::Present(max_value) = msg.htlc_maximum_msat { Some(max_value) } else { None },
1249                                                         fees: RoutingFees {
1250                                                                 base_msat: msg.fee_base_msat,
1251                                                                 proportional_millionths: msg.fee_proportional_millionths,
1252                                                         },
1253                                                         last_update_message
1254                                                 };
1255                                                 $target = Some(updated_channel_dir_info);
1256                                         }
1257                                 }
1258
1259                                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
1260                                 if msg.flags & 1 == 1 {
1261                                         dest_node_id = channel.node_one.clone();
1262                                         if let Some((sig, ctx)) = sig_info {
1263                                                 secp_verify_sig!(ctx, &msg_hash, &sig, &channel.node_two);
1264                                         }
1265                                         maybe_update_channel_info!(channel.two_to_one, channel.node_two);
1266                                 } else {
1267                                         dest_node_id = channel.node_two.clone();
1268                                         if let Some((sig, ctx)) = sig_info {
1269                                                 secp_verify_sig!(ctx, &msg_hash, &sig, &channel.node_one);
1270                                         }
1271                                         maybe_update_channel_info!(channel.one_to_two, channel.node_one);
1272                                 }
1273                         }
1274                 }
1275
1276                 if chan_enabled {
1277                         let node = self.nodes.get_mut(&dest_node_id).unwrap();
1278                         let mut base_msat = msg.fee_base_msat;
1279                         let mut proportional_millionths = msg.fee_proportional_millionths;
1280                         if let Some(fees) = node.lowest_inbound_channel_fees {
1281                                 base_msat = cmp::min(base_msat, fees.base_msat);
1282                                 proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
1283                         }
1284                         node.lowest_inbound_channel_fees = Some(RoutingFees {
1285                                 base_msat,
1286                                 proportional_millionths
1287                         });
1288                 } else if chan_was_enabled {
1289                         let node = self.nodes.get_mut(&dest_node_id).unwrap();
1290                         let mut lowest_inbound_channel_fees = None;
1291
1292                         for chan_id in node.channels.iter() {
1293                                 let chan = self.channels.get(chan_id).unwrap();
1294                                 let chan_info_opt;
1295                                 if chan.node_one == dest_node_id {
1296                                         chan_info_opt = chan.two_to_one.as_ref();
1297                                 } else {
1298                                         chan_info_opt = chan.one_to_two.as_ref();
1299                                 }
1300                                 if let Some(chan_info) = chan_info_opt {
1301                                         if chan_info.enabled {
1302                                                 let fees = lowest_inbound_channel_fees.get_or_insert(RoutingFees {
1303                                                         base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
1304                                                 fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
1305                                                 fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
1306                                         }
1307                                 }
1308                         }
1309
1310                         node.lowest_inbound_channel_fees = lowest_inbound_channel_fees;
1311                 }
1312
1313                 Ok(())
1314         }
1315
1316         fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
1317                 macro_rules! remove_from_node {
1318                         ($node_id: expr) => {
1319                                 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
1320                                         entry.get_mut().channels.retain(|chan_id| {
1321                                                 short_channel_id != *chan_id
1322                                         });
1323                                         if entry.get().channels.is_empty() {
1324                                                 entry.remove_entry();
1325                                         }
1326                                 } else {
1327                                         panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
1328                                 }
1329                         }
1330                 }
1331
1332                 remove_from_node!(chan.node_one);
1333                 remove_from_node!(chan.node_two);
1334         }
1335 }
1336
1337 #[cfg(test)]
1338 mod tests {
1339         use chain;
1340         use ln::features::{ChannelFeatures, NodeFeatures};
1341         use routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
1342         use ln::msgs::{OptionalField, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
1343                 UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate,
1344                 ReplyChannelRange, ReplyShortChannelIdsEnd, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
1345         use util::test_utils;
1346         use util::logger::Logger;
1347         use util::ser::{Readable, Writeable};
1348         use util::events::{MessageSendEvent, MessageSendEventsProvider};
1349
1350         use bitcoin::hashes::sha256d::Hash as Sha256dHash;
1351         use bitcoin::hashes::Hash;
1352         use bitcoin::network::constants::Network;
1353         use bitcoin::blockdata::constants::genesis_block;
1354         use bitcoin::blockdata::script::Builder;
1355         use bitcoin::blockdata::transaction::TxOut;
1356         use bitcoin::blockdata::opcodes;
1357
1358         use hex;
1359
1360         use bitcoin::secp256k1::key::{PublicKey, SecretKey};
1361         use bitcoin::secp256k1::{All, Secp256k1};
1362
1363         use std::sync::Arc;
1364
1365         fn create_net_graph_msg_handler() -> (Secp256k1<All>, NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>) {
1366                 let secp_ctx = Secp256k1::new();
1367                 let logger = Arc::new(test_utils::TestLogger::new());
1368                 let net_graph_msg_handler = NetGraphMsgHandler::new(None, Arc::clone(&logger));
1369                 (secp_ctx, net_graph_msg_handler)
1370         }
1371
1372         #[test]
1373         fn request_full_sync_finite_times() {
1374                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1375                 let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
1376
1377                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1378                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1379                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1380                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1381                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1382                 assert!(!net_graph_msg_handler.should_request_full_sync(&node_id));
1383         }
1384
1385         #[test]
1386         fn handling_node_announcements() {
1387                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1388
1389                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1390                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1391                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1392                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1393                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1394                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1395                 let zero_hash = Sha256dHash::hash(&[0; 32]);
1396                 let first_announcement_time = 500;
1397
1398                 let mut unsigned_announcement = UnsignedNodeAnnouncement {
1399                         features: NodeFeatures::known(),
1400                         timestamp: first_announcement_time,
1401                         node_id: node_id_1,
1402                         rgb: [0; 3],
1403                         alias: [0; 32],
1404                         addresses: Vec::new(),
1405                         excess_address_data: Vec::new(),
1406                         excess_data: Vec::new(),
1407                 };
1408                 let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1409                 let valid_announcement = NodeAnnouncement {
1410                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1411                         contents: unsigned_announcement.clone()
1412                 };
1413
1414                 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1415                         Ok(_) => panic!(),
1416                         Err(e) => assert_eq!("No existing channels for node_announcement", e.err)
1417                 };
1418
1419                 {
1420                         // Announce a channel to add a corresponding node.
1421                         let unsigned_announcement = UnsignedChannelAnnouncement {
1422                                 features: ChannelFeatures::known(),
1423                                 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
1424                                 short_channel_id: 0,
1425                                 node_id_1,
1426                                 node_id_2,
1427                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1428                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1429                                 excess_data: Vec::new(),
1430                         };
1431
1432                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1433                         let valid_announcement = ChannelAnnouncement {
1434                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1435                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1436                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1437                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1438                                 contents: unsigned_announcement.clone(),
1439                         };
1440                         match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1441                                 Ok(res) => assert!(res),
1442                                 _ => panic!()
1443                         };
1444                 }
1445
1446                 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1447                         Ok(res) => assert!(res),
1448                         Err(_) => panic!()
1449                 };
1450
1451                 let fake_msghash = hash_to_message!(&zero_hash);
1452                 match net_graph_msg_handler.handle_node_announcement(
1453                         &NodeAnnouncement {
1454                                 signature: secp_ctx.sign(&fake_msghash, node_1_privkey),
1455                                 contents: unsigned_announcement.clone()
1456                 }) {
1457                         Ok(_) => panic!(),
1458                         Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1459                 };
1460
1461                 unsigned_announcement.timestamp += 1000;
1462                 unsigned_announcement.excess_data.push(1);
1463                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1464                 let announcement_with_data = NodeAnnouncement {
1465                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1466                         contents: unsigned_announcement.clone()
1467                 };
1468                 // Return false because contains excess data.
1469                 match net_graph_msg_handler.handle_node_announcement(&announcement_with_data) {
1470                         Ok(res) => assert!(!res),
1471                         Err(_) => panic!()
1472                 };
1473                 unsigned_announcement.excess_data = Vec::new();
1474
1475                 // Even though previous announcement was not relayed further, we still accepted it,
1476                 // so we now won't accept announcements before the previous one.
1477                 unsigned_announcement.timestamp -= 10;
1478                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1479                 let outdated_announcement = NodeAnnouncement {
1480                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1481                         contents: unsigned_announcement.clone()
1482                 };
1483                 match net_graph_msg_handler.handle_node_announcement(&outdated_announcement) {
1484                         Ok(_) => panic!(),
1485                         Err(e) => assert_eq!(e.err, "Update older than last processed update")
1486                 };
1487         }
1488
1489         #[test]
1490         fn handling_channel_announcements() {
1491                 let secp_ctx = Secp256k1::new();
1492                 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1493
1494                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1495                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1496                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1497                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1498                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1499                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1500
1501                 let good_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
1502                    .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_1_btckey).serialize())
1503                    .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_2_btckey).serialize())
1504                    .push_opcode(opcodes::all::OP_PUSHNUM_2)
1505                    .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
1506
1507
1508                 let mut unsigned_announcement = UnsignedChannelAnnouncement {
1509                         features: ChannelFeatures::known(),
1510                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
1511                         short_channel_id: 0,
1512                         node_id_1,
1513                         node_id_2,
1514                         bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1515                         bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1516                         excess_data: Vec::new(),
1517                 };
1518
1519                 let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1520                 let valid_announcement = ChannelAnnouncement {
1521                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1522                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1523                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1524                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1525                         contents: unsigned_announcement.clone(),
1526                 };
1527
1528                 // Test if the UTXO lookups were not supported
1529                 let mut net_graph_msg_handler = NetGraphMsgHandler::new(None, Arc::clone(&logger));
1530                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1531                         Ok(res) => assert!(res),
1532                         _ => panic!()
1533                 };
1534
1535                 {
1536                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1537                         match network.get_channels().get(&unsigned_announcement.short_channel_id) {
1538                                 None => panic!(),
1539                                 Some(_) => ()
1540                         }
1541                 }
1542
1543                 // If we receive announcement for the same channel (with UTXO lookups disabled),
1544                 // drop new one on the floor, since we can't see any changes.
1545                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1546                         Ok(_) => panic!(),
1547                         Err(e) => assert_eq!(e.err, "Already have knowledge of channel")
1548                 };
1549
1550                 // Test if an associated transaction were not on-chain (or not confirmed).
1551                 let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
1552                 *chain_source.utxo_ret.lock().unwrap() = Err(chain::AccessError::UnknownTx);
1553                 net_graph_msg_handler = NetGraphMsgHandler::new(Some(chain_source.clone()), Arc::clone(&logger));
1554                 unsigned_announcement.short_channel_id += 1;
1555
1556                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1557                 let valid_announcement = ChannelAnnouncement {
1558                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1559                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1560                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1561                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1562                         contents: unsigned_announcement.clone(),
1563                 };
1564
1565                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1566                         Ok(_) => panic!(),
1567                         Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry")
1568                 };
1569
1570                 // Now test if the transaction is found in the UTXO set and the script is correct.
1571                 unsigned_announcement.short_channel_id += 1;
1572                 *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script.clone() });
1573
1574                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1575                 let valid_announcement = ChannelAnnouncement {
1576                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1577                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1578                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1579                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1580                         contents: unsigned_announcement.clone(),
1581                 };
1582                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1583                         Ok(res) => assert!(res),
1584                         _ => panic!()
1585                 };
1586
1587                 {
1588                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1589                         match network.get_channels().get(&unsigned_announcement.short_channel_id) {
1590                                 None => panic!(),
1591                                 Some(_) => ()
1592                         }
1593                 }
1594
1595                 // If we receive announcement for the same channel (but TX is not confirmed),
1596                 // drop new one on the floor, since we can't see any changes.
1597                 *chain_source.utxo_ret.lock().unwrap() = Err(chain::AccessError::UnknownTx);
1598                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1599                         Ok(_) => panic!(),
1600                         Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry")
1601                 };
1602
1603                 // But if it is confirmed, replace the channel
1604                 *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script });
1605                 unsigned_announcement.features = ChannelFeatures::empty();
1606                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1607                 let valid_announcement = ChannelAnnouncement {
1608                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1609                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1610                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1611                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1612                         contents: unsigned_announcement.clone(),
1613                 };
1614                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1615                         Ok(res) => assert!(res),
1616                         _ => panic!()
1617                 };
1618                 {
1619                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1620                         match network.get_channels().get(&unsigned_announcement.short_channel_id) {
1621                                 Some(channel_entry) => {
1622                                         assert_eq!(channel_entry.features, ChannelFeatures::empty());
1623                                 },
1624                                 _ => panic!()
1625                         }
1626                 }
1627
1628                 // Don't relay valid channels with excess data
1629                 unsigned_announcement.short_channel_id += 1;
1630                 unsigned_announcement.excess_data.push(1);
1631                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1632                 let valid_announcement = ChannelAnnouncement {
1633                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1634                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1635                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1636                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1637                         contents: unsigned_announcement.clone(),
1638                 };
1639                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1640                         Ok(res) => assert!(!res),
1641                         _ => panic!()
1642                 };
1643
1644                 unsigned_announcement.excess_data = Vec::new();
1645                 let invalid_sig_announcement = ChannelAnnouncement {
1646                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1647                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1648                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1649                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_1_btckey),
1650                         contents: unsigned_announcement.clone(),
1651                 };
1652                 match net_graph_msg_handler.handle_channel_announcement(&invalid_sig_announcement) {
1653                         Ok(_) => panic!(),
1654                         Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1655                 };
1656
1657                 unsigned_announcement.node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1658                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1659                 let channel_to_itself_announcement = ChannelAnnouncement {
1660                         node_signature_1: secp_ctx.sign(&msghash, node_2_privkey),
1661                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1662                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1663                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1664                         contents: unsigned_announcement.clone(),
1665                 };
1666                 match net_graph_msg_handler.handle_channel_announcement(&channel_to_itself_announcement) {
1667                         Ok(_) => panic!(),
1668                         Err(e) => assert_eq!(e.err, "Channel announcement node had a channel with itself")
1669                 };
1670         }
1671
1672         #[test]
1673         fn handling_channel_update() {
1674                 let secp_ctx = Secp256k1::new();
1675                 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1676                 let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
1677                 let net_graph_msg_handler = NetGraphMsgHandler::new(Some(chain_source.clone()), Arc::clone(&logger));
1678
1679                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1680                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1681                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1682                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1683                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1684                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1685
1686                 let zero_hash = Sha256dHash::hash(&[0; 32]);
1687                 let short_channel_id = 0;
1688                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
1689                 let amount_sats = 1000_000;
1690
1691                 {
1692                         // Announce a channel we will update
1693                         let good_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
1694                            .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_1_btckey).serialize())
1695                            .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_2_btckey).serialize())
1696                            .push_opcode(opcodes::all::OP_PUSHNUM_2)
1697                            .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
1698                         *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() });
1699                         let unsigned_announcement = UnsignedChannelAnnouncement {
1700                                 features: ChannelFeatures::empty(),
1701                                 chain_hash,
1702                                 short_channel_id,
1703                                 node_id_1,
1704                                 node_id_2,
1705                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1706                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1707                                 excess_data: Vec::new(),
1708                         };
1709
1710                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1711                         let valid_channel_announcement = ChannelAnnouncement {
1712                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1713                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1714                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1715                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1716                                 contents: unsigned_announcement.clone(),
1717                         };
1718                         match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1719                                 Ok(_) => (),
1720                                 Err(_) => panic!()
1721                         };
1722
1723                 }
1724
1725                 let mut unsigned_channel_update = UnsignedChannelUpdate {
1726                         chain_hash,
1727                         short_channel_id,
1728                         timestamp: 100,
1729                         flags: 0,
1730                         cltv_expiry_delta: 144,
1731                         htlc_minimum_msat: 1000000,
1732                         htlc_maximum_msat: OptionalField::Absent,
1733                         fee_base_msat: 10000,
1734                         fee_proportional_millionths: 20,
1735                         excess_data: Vec::new()
1736                 };
1737                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1738                 let valid_channel_update = ChannelUpdate {
1739                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1740                         contents: unsigned_channel_update.clone()
1741                 };
1742
1743                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1744                         Ok(res) => assert!(res),
1745                         _ => panic!()
1746                 };
1747
1748                 {
1749                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1750                         match network.get_channels().get(&short_channel_id) {
1751                                 None => panic!(),
1752                                 Some(channel_info) => {
1753                                         assert_eq!(channel_info.one_to_two.as_ref().unwrap().cltv_expiry_delta, 144);
1754                                         assert!(channel_info.two_to_one.is_none());
1755                                 }
1756                         }
1757                 }
1758
1759                 unsigned_channel_update.timestamp += 100;
1760                 unsigned_channel_update.excess_data.push(1);
1761                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1762                 let valid_channel_update = ChannelUpdate {
1763                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1764                         contents: unsigned_channel_update.clone()
1765                 };
1766                 // Return false because contains excess data
1767                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1768                         Ok(res) => assert!(!res),
1769                         _ => panic!()
1770                 };
1771                 unsigned_channel_update.timestamp += 10;
1772
1773                 unsigned_channel_update.short_channel_id += 1;
1774                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1775                 let valid_channel_update = ChannelUpdate {
1776                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1777                         contents: unsigned_channel_update.clone()
1778                 };
1779
1780                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1781                         Ok(_) => panic!(),
1782                         Err(e) => assert_eq!(e.err, "Couldn't find channel for update")
1783                 };
1784                 unsigned_channel_update.short_channel_id = short_channel_id;
1785
1786                 unsigned_channel_update.htlc_maximum_msat = OptionalField::Present(MAX_VALUE_MSAT + 1);
1787                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1788                 let valid_channel_update = ChannelUpdate {
1789                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1790                         contents: unsigned_channel_update.clone()
1791                 };
1792
1793                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1794                         Ok(_) => panic!(),
1795                         Err(e) => assert_eq!(e.err, "htlc_maximum_msat is larger than maximum possible msats")
1796                 };
1797                 unsigned_channel_update.htlc_maximum_msat = OptionalField::Absent;
1798
1799                 unsigned_channel_update.htlc_maximum_msat = OptionalField::Present(amount_sats * 1000 + 1);
1800                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1801                 let valid_channel_update = ChannelUpdate {
1802                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1803                         contents: unsigned_channel_update.clone()
1804                 };
1805
1806                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1807                         Ok(_) => panic!(),
1808                         Err(e) => assert_eq!(e.err, "htlc_maximum_msat is larger than channel capacity or capacity is bogus")
1809                 };
1810                 unsigned_channel_update.htlc_maximum_msat = OptionalField::Absent;
1811
1812                 // Even though previous update was not relayed further, we still accepted it,
1813                 // so we now won't accept update before the previous one.
1814                 unsigned_channel_update.timestamp -= 10;
1815                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1816                 let valid_channel_update = ChannelUpdate {
1817                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1818                         contents: unsigned_channel_update.clone()
1819                 };
1820
1821                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1822                         Ok(_) => panic!(),
1823                         Err(e) => assert_eq!(e.err, "Update older than last processed update")
1824                 };
1825                 unsigned_channel_update.timestamp += 500;
1826
1827                 let fake_msghash = hash_to_message!(&zero_hash);
1828                 let invalid_sig_channel_update = ChannelUpdate {
1829                         signature: secp_ctx.sign(&fake_msghash, node_1_privkey),
1830                         contents: unsigned_channel_update.clone()
1831                 };
1832
1833                 match net_graph_msg_handler.handle_channel_update(&invalid_sig_channel_update) {
1834                         Ok(_) => panic!(),
1835                         Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1836                 };
1837
1838         }
1839
1840         #[test]
1841         fn handling_htlc_fail_channel_update() {
1842                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1843                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1844                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1845                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1846                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1847                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1848                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1849
1850                 let short_channel_id = 0;
1851                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
1852
1853                 {
1854                         // There is no nodes in the table at the beginning.
1855                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1856                         assert_eq!(network.get_nodes().len(), 0);
1857                 }
1858
1859                 {
1860                         // Announce a channel we will update
1861                         let unsigned_announcement = UnsignedChannelAnnouncement {
1862                                 features: ChannelFeatures::empty(),
1863                                 chain_hash,
1864                                 short_channel_id,
1865                                 node_id_1,
1866                                 node_id_2,
1867                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1868                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1869                                 excess_data: Vec::new(),
1870                         };
1871
1872                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1873                         let valid_channel_announcement = ChannelAnnouncement {
1874                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1875                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1876                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1877                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1878                                 contents: unsigned_announcement.clone(),
1879                         };
1880                         match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1881                                 Ok(_) => (),
1882                                 Err(_) => panic!()
1883                         };
1884
1885                         let unsigned_channel_update = UnsignedChannelUpdate {
1886                                 chain_hash,
1887                                 short_channel_id,
1888                                 timestamp: 100,
1889                                 flags: 0,
1890                                 cltv_expiry_delta: 144,
1891                                 htlc_minimum_msat: 1000000,
1892                                 htlc_maximum_msat: OptionalField::Absent,
1893                                 fee_base_msat: 10000,
1894                                 fee_proportional_millionths: 20,
1895                                 excess_data: Vec::new()
1896                         };
1897                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1898                         let valid_channel_update = ChannelUpdate {
1899                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
1900                                 contents: unsigned_channel_update.clone()
1901                         };
1902
1903                         match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1904                                 Ok(res) => assert!(res),
1905                                 _ => panic!()
1906                         };
1907                 }
1908
1909                 // Non-permanent closing just disables a channel
1910                 {
1911                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1912                         match network.get_channels().get(&short_channel_id) {
1913                                 None => panic!(),
1914                                 Some(channel_info) => {
1915                                         assert!(channel_info.one_to_two.is_some());
1916                                 }
1917                         }
1918                 }
1919
1920                 let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
1921                         short_channel_id,
1922                         is_permanent: false
1923                 };
1924
1925                 net_graph_msg_handler.handle_htlc_fail_channel_update(&channel_close_msg);
1926
1927                 // Non-permanent closing just disables a channel
1928                 {
1929                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1930                         match network.get_channels().get(&short_channel_id) {
1931                                 None => panic!(),
1932                                 Some(channel_info) => {
1933                                         assert!(!channel_info.one_to_two.as_ref().unwrap().enabled);
1934                                 }
1935                         }
1936                 }
1937
1938                 let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
1939                         short_channel_id,
1940                         is_permanent: true
1941                 };
1942
1943                 net_graph_msg_handler.handle_htlc_fail_channel_update(&channel_close_msg);
1944
1945                 // Permanent closing deletes a channel
1946                 {
1947                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1948                         assert_eq!(network.get_channels().len(), 0);
1949                         // Nodes are also deleted because there are no associated channels anymore
1950                         assert_eq!(network.get_nodes().len(), 0);
1951                 }
1952                 // TODO: Test HTLCFailChannelUpdate::NodeFailure, which is not implemented yet.
1953         }
1954
1955         #[test]
1956         fn getting_next_channel_announcements() {
1957                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1958                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1959                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1960                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1961                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1962                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1963                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1964
1965                 let short_channel_id = 1;
1966                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
1967
1968                 // Channels were not announced yet.
1969                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(0, 1);
1970                 assert_eq!(channels_with_announcements.len(), 0);
1971
1972                 {
1973                         // Announce a channel we will update
1974                         let unsigned_announcement = UnsignedChannelAnnouncement {
1975                                 features: ChannelFeatures::empty(),
1976                                 chain_hash,
1977                                 short_channel_id,
1978                                 node_id_1,
1979                                 node_id_2,
1980                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1981                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1982                                 excess_data: Vec::new(),
1983                         };
1984
1985                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1986                         let valid_channel_announcement = ChannelAnnouncement {
1987                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1988                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1989                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1990                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1991                                 contents: unsigned_announcement.clone(),
1992                         };
1993                         match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1994                                 Ok(_) => (),
1995                                 Err(_) => panic!()
1996                         };
1997                 }
1998
1999                 // Contains initial channel announcement now.
2000                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1);
2001                 assert_eq!(channels_with_announcements.len(), 1);
2002                 if let Some(channel_announcements) = channels_with_announcements.first() {
2003                         let &(_, ref update_1, ref update_2) = channel_announcements;
2004                         assert_eq!(update_1, &None);
2005                         assert_eq!(update_2, &None);
2006                 } else {
2007                         panic!();
2008                 }
2009
2010
2011                 {
2012                         // Valid channel update
2013                         let unsigned_channel_update = UnsignedChannelUpdate {
2014                                 chain_hash,
2015                                 short_channel_id,
2016                                 timestamp: 101,
2017                                 flags: 0,
2018                                 cltv_expiry_delta: 144,
2019                                 htlc_minimum_msat: 1000000,
2020                                 htlc_maximum_msat: OptionalField::Absent,
2021                                 fee_base_msat: 10000,
2022                                 fee_proportional_millionths: 20,
2023                                 excess_data: Vec::new()
2024                         };
2025                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
2026                         let valid_channel_update = ChannelUpdate {
2027                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
2028                                 contents: unsigned_channel_update.clone()
2029                         };
2030                         match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
2031                                 Ok(_) => (),
2032                                 Err(_) => panic!()
2033                         };
2034                 }
2035
2036                 // Now contains an initial announcement and an update.
2037                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1);
2038                 assert_eq!(channels_with_announcements.len(), 1);
2039                 if let Some(channel_announcements) = channels_with_announcements.first() {
2040                         let &(_, ref update_1, ref update_2) = channel_announcements;
2041                         assert_ne!(update_1, &None);
2042                         assert_eq!(update_2, &None);
2043                 } else {
2044                         panic!();
2045                 }
2046
2047
2048                 {
2049                         // Channel update with excess data.
2050                         let unsigned_channel_update = UnsignedChannelUpdate {
2051                                 chain_hash,
2052                                 short_channel_id,
2053                                 timestamp: 102,
2054                                 flags: 0,
2055                                 cltv_expiry_delta: 144,
2056                                 htlc_minimum_msat: 1000000,
2057                                 htlc_maximum_msat: OptionalField::Absent,
2058                                 fee_base_msat: 10000,
2059                                 fee_proportional_millionths: 20,
2060                                 excess_data: [1; 3].to_vec()
2061                         };
2062                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
2063                         let valid_channel_update = ChannelUpdate {
2064                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
2065                                 contents: unsigned_channel_update.clone()
2066                         };
2067                         match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
2068                                 Ok(_) => (),
2069                                 Err(_) => panic!()
2070                         };
2071                 }
2072
2073                 // Test that announcements with excess data won't be returned
2074                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1);
2075                 assert_eq!(channels_with_announcements.len(), 1);
2076                 if let Some(channel_announcements) = channels_with_announcements.first() {
2077                         let &(_, ref update_1, ref update_2) = channel_announcements;
2078                         assert_eq!(update_1, &None);
2079                         assert_eq!(update_2, &None);
2080                 } else {
2081                         panic!();
2082                 }
2083
2084                 // Further starting point have no channels after it
2085                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id + 1000, 1);
2086                 assert_eq!(channels_with_announcements.len(), 0);
2087         }
2088
2089         #[test]
2090         fn getting_next_node_announcements() {
2091                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2092                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
2093                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2094                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2095                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
2096                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
2097                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
2098
2099                 let short_channel_id = 1;
2100                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2101
2102                 // No nodes yet.
2103                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 10);
2104                 assert_eq!(next_announcements.len(), 0);
2105
2106                 {
2107                         // Announce a channel to add 2 nodes
2108                         let unsigned_announcement = UnsignedChannelAnnouncement {
2109                                 features: ChannelFeatures::empty(),
2110                                 chain_hash,
2111                                 short_channel_id,
2112                                 node_id_1,
2113                                 node_id_2,
2114                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
2115                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
2116                                 excess_data: Vec::new(),
2117                         };
2118
2119                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2120                         let valid_channel_announcement = ChannelAnnouncement {
2121                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2122                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2123                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2124                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2125                                 contents: unsigned_announcement.clone(),
2126                         };
2127                         match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
2128                                 Ok(_) => (),
2129                                 Err(_) => panic!()
2130                         };
2131                 }
2132
2133
2134                 // Nodes were never announced
2135                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 3);
2136                 assert_eq!(next_announcements.len(), 0);
2137
2138                 {
2139                         let mut unsigned_announcement = UnsignedNodeAnnouncement {
2140                                 features: NodeFeatures::known(),
2141                                 timestamp: 1000,
2142                                 node_id: node_id_1,
2143                                 rgb: [0; 3],
2144                                 alias: [0; 32],
2145                                 addresses: Vec::new(),
2146                                 excess_address_data: Vec::new(),
2147                                 excess_data: Vec::new(),
2148                         };
2149                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2150                         let valid_announcement = NodeAnnouncement {
2151                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
2152                                 contents: unsigned_announcement.clone()
2153                         };
2154                         match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
2155                                 Ok(_) => (),
2156                                 Err(_) => panic!()
2157                         };
2158
2159                         unsigned_announcement.node_id = node_id_2;
2160                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2161                         let valid_announcement = NodeAnnouncement {
2162                                 signature: secp_ctx.sign(&msghash, node_2_privkey),
2163                                 contents: unsigned_announcement.clone()
2164                         };
2165
2166                         match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
2167                                 Ok(_) => (),
2168                                 Err(_) => panic!()
2169                         };
2170                 }
2171
2172                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 3);
2173                 assert_eq!(next_announcements.len(), 2);
2174
2175                 // Skip the first node.
2176                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(Some(&node_id_1), 2);
2177                 assert_eq!(next_announcements.len(), 1);
2178
2179                 {
2180                         // Later announcement which should not be relayed (excess data) prevent us from sharing a node
2181                         let unsigned_announcement = UnsignedNodeAnnouncement {
2182                                 features: NodeFeatures::known(),
2183                                 timestamp: 1010,
2184                                 node_id: node_id_2,
2185                                 rgb: [0; 3],
2186                                 alias: [0; 32],
2187                                 addresses: Vec::new(),
2188                                 excess_address_data: Vec::new(),
2189                                 excess_data: [1; 3].to_vec(),
2190                         };
2191                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2192                         let valid_announcement = NodeAnnouncement {
2193                                 signature: secp_ctx.sign(&msghash, node_2_privkey),
2194                                 contents: unsigned_announcement.clone()
2195                         };
2196                         match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
2197                                 Ok(res) => assert!(!res),
2198                                 Err(_) => panic!()
2199                         };
2200                 }
2201
2202                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(Some(&node_id_1), 2);
2203                 assert_eq!(next_announcements.len(), 0);
2204         }
2205
2206         #[test]
2207         fn network_graph_serialization() {
2208                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2209
2210                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
2211                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2212                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
2213                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
2214
2215                 // Announce a channel to add a corresponding node.
2216                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2217                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
2218                 let unsigned_announcement = UnsignedChannelAnnouncement {
2219                         features: ChannelFeatures::known(),
2220                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
2221                         short_channel_id: 0,
2222                         node_id_1,
2223                         node_id_2,
2224                         bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
2225                         bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
2226                         excess_data: Vec::new(),
2227                 };
2228
2229                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2230                 let valid_announcement = ChannelAnnouncement {
2231                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2232                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2233                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2234                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2235                         contents: unsigned_announcement.clone(),
2236                 };
2237                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
2238                         Ok(res) => assert!(res),
2239                         _ => panic!()
2240                 };
2241
2242
2243                 let node_id = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2244                 let unsigned_announcement = UnsignedNodeAnnouncement {
2245                         features: NodeFeatures::known(),
2246                         timestamp: 100,
2247                         node_id,
2248                         rgb: [0; 3],
2249                         alias: [0; 32],
2250                         addresses: Vec::new(),
2251                         excess_address_data: Vec::new(),
2252                         excess_data: Vec::new(),
2253                 };
2254                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2255                 let valid_announcement = NodeAnnouncement {
2256                         signature: secp_ctx.sign(&msghash, node_1_privkey),
2257                         contents: unsigned_announcement.clone()
2258                 };
2259
2260                 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
2261                         Ok(_) => (),
2262                         Err(_) => panic!()
2263                 };
2264
2265                 let network = net_graph_msg_handler.network_graph.write().unwrap();
2266                 let mut w = test_utils::TestVecWriter(Vec::new());
2267                 assert!(!network.get_nodes().is_empty());
2268                 assert!(!network.get_channels().is_empty());
2269                 network.write(&mut w).unwrap();
2270                 assert!(<NetworkGraph>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);
2271         }
2272
2273         #[test]
2274         fn sending_query_channel_range() {
2275                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2276                 let node_privkey_1 = &SecretKey::from_slice(&[42; 32]).unwrap();
2277                 let node_privkey_2 = &SecretKey::from_slice(&[41; 32]).unwrap();
2278                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_privkey_1);
2279                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_privkey_2);
2280
2281                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2282                 let first_blocknum = 0;
2283                 let number_of_blocks = 0xffff_ffff;
2284
2285                 // When no active query exists for the node, it should send a query message and generate a task
2286                 {
2287                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, first_blocknum, number_of_blocks);
2288                         assert!(result.is_ok());
2289
2290                         // It should create a task for the query
2291                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().contains_key(&node_id_1));
2292
2293                         // It should send a query_channel_range message with the correct information
2294                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2295                         assert_eq!(events.len(), 1);
2296                         match &events[0] {
2297                                 MessageSendEvent::SendChannelRangeQuery{ node_id, msg } => {
2298                                         assert_eq!(node_id, &node_id_1);
2299                                         assert_eq!(msg.chain_hash, chain_hash);
2300                                         assert_eq!(msg.first_blocknum, first_blocknum);
2301                                         assert_eq!(msg.number_of_blocks, number_of_blocks);
2302                                 },
2303                                 _ => panic!("Expected MessageSendEvent::SendChannelRangeQuery")
2304                         };
2305                 }
2306
2307                 // When an active query exists for the node, when there is a subsequent query request, it
2308                 // should fail to initiate a new query
2309                 {
2310                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, first_blocknum, number_of_blocks);
2311                         assert_eq!(result.is_err(), true);
2312                 }
2313
2314                 // When no active query exists for a different node, it should send a query message
2315                 {
2316                         let result = net_graph_msg_handler.query_channel_range(&node_id_2, chain_hash, first_blocknum, number_of_blocks);
2317                         assert_eq!(result.is_ok(), true);
2318
2319                         // It should create a task for the query
2320                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().contains_key(&node_id_2));
2321
2322                         // It should send a query_channel_message with the correct information
2323                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2324                         assert_eq!(events.len(), 1);
2325                         match &events[0] {
2326                                 MessageSendEvent::SendChannelRangeQuery{ node_id, msg } => {
2327                                         assert_eq!(node_id, &node_id_2);
2328                                         assert_eq!(msg.chain_hash, chain_hash);
2329                                         assert_eq!(msg.first_blocknum, first_blocknum);
2330                                         assert_eq!(msg.number_of_blocks, number_of_blocks);
2331                                 },
2332                                 _ => panic!("Expected MessageSendEvent::SendChannelRangeQuery")
2333                         };
2334                 }
2335         }
2336
2337         #[test]
2338         fn sending_query_short_channel_ids() {
2339                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2340                 let node_privkey_1 = &SecretKey::from_slice(&[42; 32]).unwrap();
2341                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_privkey_1);
2342
2343                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2344
2345                 // The first query should send the batch of scids to the peer
2346                 {
2347                         let short_channel_ids: Vec<u64> = vec![0, 1, 2];
2348                         let result = net_graph_msg_handler.query_short_channel_ids(&node_id_1, chain_hash, short_channel_ids.clone());
2349                         assert!(result.is_ok());
2350
2351                         // Validate that we have enqueued a send message event and that it contains the correct information
2352                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2353                         assert_eq!(events.len(), 1);
2354                         match &events[0] {
2355                                 MessageSendEvent::SendShortIdsQuery{ node_id, msg } => {
2356                                         assert_eq!(node_id, &node_id_1);
2357                                         assert_eq!(msg.chain_hash, chain_hash);
2358                                         assert_eq!(msg.short_channel_ids, short_channel_ids);
2359                                 },
2360                                 _ => panic!("Expected MessageSendEvent::SendShortIdsQuery")
2361                         };
2362                 }
2363
2364                 // Subsequent queries for scids should enqueue them to be sent in the next batch which will
2365                 // be sent when a reply_short_channel_ids_end message is handled.
2366                 {
2367                         let short_channel_ids: Vec<u64> = vec![3, 4, 5];
2368                         let result = net_graph_msg_handler.query_short_channel_ids(&node_id_1, chain_hash, short_channel_ids.clone());
2369                         assert!(result.is_ok());
2370
2371                         // Validate that we have not enqueued another send message event yet
2372                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2373                         assert_eq!(events.len(), 0);
2374
2375                         // Validate the task has the queued scids
2376                         assert_eq!(
2377                                 net_graph_msg_handler.scid_query_tasks.lock().unwrap().get(&node_id_1).unwrap().short_channel_ids,
2378                                 short_channel_ids
2379                         );
2380                 }
2381         }
2382
2383         #[test]
2384         fn handling_reply_channel_range() {
2385                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2386                 let node_privkey_1 = &SecretKey::from_slice(&[42; 32]).unwrap();
2387                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_privkey_1);
2388
2389                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2390
2391                 // Test receipt of an unknown reply message. We expect an error
2392                 {
2393                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2394                                 chain_hash,
2395                                 full_information: true,
2396                                 first_blocknum: 1000,
2397                                 number_of_blocks: 1050,
2398                                 short_channel_ids: vec![
2399                                         0x0003e8_000000_0000, // 1000x0x0
2400                                         0x0003e9_000000_0000, // 1001x0x0
2401                                         0x0003f0_000000_0000  // 1008x0x0
2402                                 ],
2403                         });
2404                         assert!(result.is_err());
2405                 }
2406
2407                 // Test receipt of a single reply_channel_range that exactly matches the queried range.
2408                 // It sends a query_short_channel_ids with the returned scids and removes the pending task
2409                 {
2410                         // Initiate a channel range query to create a query task
2411                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2412                         assert!(result.is_ok());
2413
2414                         // Clear the SendRangeQuery event
2415                         net_graph_msg_handler.get_and_clear_pending_msg_events();
2416
2417                         // Handle a single successful reply that matches the queried channel range
2418                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2419                                 chain_hash,
2420                                 full_information: true,
2421                                 first_blocknum: 1000,
2422                                 number_of_blocks: 100,
2423                                 short_channel_ids: vec![
2424                                         0x0003e8_000000_0000, // 1000x0x0
2425                                         0x0003e9_000000_0000, // 1001x0x0
2426                                         0x0003f0_000000_0000  // 1008x0x0
2427                                 ],
2428                         });
2429                         assert!(result.is_ok());
2430
2431                         // The query is now complete, so we expect the task to be removed
2432                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2433
2434                         // We expect to emit a query_short_channel_ids message with scids in our query range
2435                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2436                         assert_eq!(events.len(), 1);
2437                         match &events[0] {
2438                                 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2439                                         assert_eq!(node_id, &node_id_1);
2440                                         assert_eq!(msg.chain_hash, chain_hash);
2441                                         assert_eq!(msg.short_channel_ids, vec![0x0003e8_000000_0000,0x0003e9_000000_0000,0x0003f0_000000_0000]);
2442                                 },
2443                                 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2444                         }
2445
2446                         // Clean up scid_task
2447                         net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
2448                 }
2449
2450                 // Test receipt of a single reply_channel_range for a query that has a u32 overflow. We expect
2451                 // it sends a query_short_channel_ids with the returned scids and removes the pending task.
2452                 {
2453                         // Initiate a channel range query to create a query task
2454                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 0xffff_ffff);
2455                         assert!(result.is_ok());
2456
2457                         // Clear the SendRangeQuery event
2458                         net_graph_msg_handler.get_and_clear_pending_msg_events();
2459
2460                         // Handle a single successful reply that matches the queried channel range
2461                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2462                                 chain_hash,
2463                                 full_information: true,
2464                                 first_blocknum: 1000,
2465                                 number_of_blocks: 0xffff_ffff,
2466                                 short_channel_ids: vec![
2467                                         0x0003e8_000000_0000, // 1000x0x0
2468                                         0x0003e9_000000_0000, // 1001x0x0
2469                                         0x0003f0_000000_0000  // 1008x0x0
2470                                 ],
2471                         });
2472                         assert!(result.is_ok());
2473
2474                         // The query is now complete, so we expect the task to be removed
2475                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2476
2477                         // We expect to emit a query_short_channel_ids message with scids in our query range
2478                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2479                         assert_eq!(events.len(), 1);
2480                         match &events[0] {
2481                                 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2482                                         assert_eq!(node_id, &node_id_1);
2483                                         assert_eq!(msg.chain_hash, chain_hash);
2484                                         assert_eq!(msg.short_channel_ids, vec![0x0003e8_000000_0000,0x0003e9_000000_0000,0x0003f0_000000_0000]);
2485                                 },
2486                                 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2487                         }
2488
2489                         // Clean up scid_task
2490                         net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
2491                 }
2492
2493                 // Test receipt of a single reply that encompasses the queried channel range. This is allowed
2494                 // since a reply must contain at least part of the query range. Receipt of the reply should
2495                 // send a query_short_channel_ids message with scids filtered to the query range and remove
2496                 // the pending task.
2497                 {
2498                         // Initiate a channel range query to create a query task
2499                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2500                         assert!(result.is_ok());
2501
2502                         // Clear the SendRangeQuery event
2503                         net_graph_msg_handler.get_and_clear_pending_msg_events();
2504
2505                         // Handle a single successful reply that encompasses the queried channel range
2506                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2507                                 chain_hash,
2508                                 full_information: true,
2509                                 first_blocknum: 0,
2510                                 number_of_blocks: 2000,
2511                                 short_channel_ids: vec![
2512                                         0x0003e0_000000_0000, // 992x0x0
2513                                         0x0003e8_000000_0000, // 1000x0x0
2514                                         0x0003e9_000000_0000, // 1001x0x0
2515                                         0x0003f0_000000_0000, // 1008x0x0
2516                                         0x00044c_000000_0000, // 1100x0x0
2517                                         0x0006e0_000000_0000, // 1760x0x0
2518                                 ],
2519                         });
2520                         assert!(result.is_ok());
2521
2522                         // The query is now complete, so we expect the task to be removed
2523                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2524
2525                         // We expect to emit a query_short_channel_ids message with scids filtered to those
2526                         // within the original query range.
2527                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2528                         assert_eq!(events.len(), 1);
2529                         match &events[0] {
2530                                 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2531                                         assert_eq!(node_id, &node_id_1);
2532                                         assert_eq!(msg.chain_hash, chain_hash);
2533                                         assert_eq!(msg.short_channel_ids, vec![0x0003e8_000000_0000,0x0003e9_000000_0000,0x0003f0_000000_0000]);
2534                                 },
2535                                 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2536                         }
2537
2538                         // Clean up scid_task
2539                         net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
2540                 }
2541
2542                 // Test receipt of multiple reply messages for a single query. This happens when the number
2543                 // of scids in the query range exceeds the size limits of a single reply message. We expect
2544                 // to initiate a query_short_channel_ids for the first batch of scids and we enqueue the
2545                 // remaining scids for later processing. We remove the range query task after receipt of all
2546                 // reply messages.
2547                 {
2548                         // Initiate a channel range query to create a query task
2549                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2550                         assert!(result.is_ok());
2551
2552                         // Clear the SendRangeQuery event
2553                         net_graph_msg_handler.get_and_clear_pending_msg_events();
2554
2555                         // Handle the first reply message
2556                         let reply_1_scids =  vec![
2557                                 0x0003e8_000000_0000, // 1000x0x0
2558                                 0x0003e9_000000_0000, // 1001x0x0
2559                                 0x000419_000000_0000, // 1049x0x0
2560                         ];
2561                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2562                                 chain_hash,
2563                                 full_information: true,
2564                                 first_blocknum: 1000,
2565                                 number_of_blocks: 50,
2566                                 short_channel_ids: reply_1_scids.clone(),
2567                         });
2568                         assert!(result.is_ok());
2569
2570                         // Handle the next reply in the sequence, which must start at the previous message's
2571                         // first_blocknum plus number_of_blocks. The scids in this reply will be queued.
2572                         let reply_2_scids = vec![
2573                                 0x00041a_000000_0000, // 1050x0x0
2574                                 0x000432_000000_0000, // 1074x0x0
2575                         ];
2576                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2577                                 chain_hash,
2578                                 full_information: true,
2579                                 first_blocknum: 1050,
2580                                 number_of_blocks: 25,
2581                                 short_channel_ids: reply_2_scids.clone(),
2582                         });
2583                         assert!(result.is_ok());
2584
2585                         // Handle the final reply in the sequence, which must meet or exceed the initial query's
2586                         // first_blocknum plus number_of_blocks. The scids in this reply will be queued.
2587                         let reply_3_scids = vec![
2588                                 0x000433_000000_0000, // 1075x0x0
2589                                 0x00044b_000000_0000, // 1099x0x0
2590                         ];
2591                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2592                                 chain_hash,
2593                                 full_information: true,
2594                                 first_blocknum: 1075,
2595                                 number_of_blocks: 25,
2596                                 short_channel_ids: reply_3_scids.clone(),
2597                         });
2598                         assert!(result.is_ok());
2599
2600                         // After the final reply we expect the query task to be removed
2601                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2602
2603                         // We expect to emit a query_short_channel_ids message with the accumulated scids that
2604                         // match the queried channel range.
2605                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2606                         assert_eq!(events.len(), 1);
2607                         match &events[0] {
2608                                 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2609                                         assert_eq!(node_id, &node_id_1);
2610                                         assert_eq!(msg.chain_hash, chain_hash);
2611                                         assert_eq!(msg.short_channel_ids, [reply_1_scids, reply_2_scids, reply_3_scids].concat());
2612                                 },
2613                                 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2614                         }
2615
2616                         // Clean up scid_task
2617                         net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
2618                 }
2619
2620                 // Test receipt of a sequence of replies with a valid first reply and a second reply that
2621                 // resumes on the same block as the first reply. The spec requires a subsequent
2622                 // first_blocknum to equal the prior first_blocknum plus number_of_blocks, however
2623                 // due to discrepancies in implementation we must loosen this restriction.
2624                 {
2625                         // Initiate a channel range query to create a query task
2626                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2627                         assert!(result.is_ok());
2628
2629                         // Clear the SendRangeQuery event
2630                         net_graph_msg_handler.get_and_clear_pending_msg_events();
2631
2632                         // Handle the first reply message
2633                         let reply_1_scids = vec![
2634                                 0x0003e8_000000_0000, // 1000x0x0
2635                                 0x0003e9_000000_0000, // 1001x0x0
2636                                 0x000419_000000_0000, // 1049x0x0
2637                         ];
2638                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2639                                 chain_hash,
2640                                 full_information: true,
2641                                 first_blocknum: 1000,
2642                                 number_of_blocks: 50,
2643                                 short_channel_ids: reply_1_scids.clone(),
2644                         });
2645                         assert!(result.is_ok());
2646
2647                         // Handle the next reply in the sequence, which is non-spec but resumes on the last block
2648                         // of the first message.
2649                         let reply_2_scids = vec![
2650                                 0x000419_000001_0000, // 1049x1x0
2651                                 0x00041a_000000_0000, // 1050x0x0
2652                                 0x000432_000000_0000, // 1074x0x0
2653                         ];
2654                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2655                                 chain_hash,
2656                                 full_information: true,
2657                                 first_blocknum: 1049,
2658                                 number_of_blocks: 51,
2659                                 short_channel_ids: reply_2_scids.clone(),
2660                         });
2661                         assert!(result.is_ok());
2662
2663                         // After the final reply we expect the query task to be removed
2664                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2665
2666                         // We expect to emit a query_short_channel_ids message with the accumulated scids that
2667                         // match the queried channel range
2668                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2669                         assert_eq!(events.len(), 1);
2670                         match &events[0] {
2671                                 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2672                                         assert_eq!(node_id, &node_id_1);
2673                                         assert_eq!(msg.chain_hash, chain_hash);
2674                                         assert_eq!(msg.short_channel_ids, [reply_1_scids, reply_2_scids].concat());
2675                                 },
2676                                 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2677                         }
2678
2679                         // Clean up scid_task
2680                         net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
2681                 }
2682
2683                 // Test receipt of reply with a chain_hash that does not match the query. We expect to return
2684                 // an error and to remove the query task.
2685                 {
2686                         // Initiate a channel range query to create a query task
2687                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2688                         assert!(result.is_ok());
2689
2690                         // Clear the SendRangeQuery event
2691                         net_graph_msg_handler.get_and_clear_pending_msg_events();
2692
2693                         // Handle the reply with a mismatched chain_hash. We expect IgnoreError result and the
2694                         // task should be removed.
2695                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2696                                 chain_hash: genesis_block(Network::Bitcoin).header.block_hash(),
2697                                 full_information: true,
2698                                 first_blocknum: 1000,
2699                                 number_of_blocks: 1050,
2700                                 short_channel_ids: vec![0x0003e8_000000_0000,0x0003e9_000000_0000,0x0003f0_000000_0000],
2701                         });
2702                         assert!(result.is_err());
2703                         assert_eq!(result.err().unwrap().err, "Received reply_channel_range with invalid chain_hash");
2704                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2705                 }
2706
2707                 // Test receipt of a reply that indicates the remote node does not maintain up-to-date
2708                 // information for the chain_hash. Because of discrepancies in implementation we use
2709                 // full_information=false and short_channel_ids=[] as the signal. We should expect an error
2710                 // and the task should be removed.
2711                 {
2712                         // Initiate a channel range query to create a query task
2713                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2714                         assert!(result.is_ok());
2715
2716                         // Clear the SendRangeQuery event
2717                         net_graph_msg_handler.get_and_clear_pending_msg_events();
2718
2719                         // Handle the reply indicating the peer was unable to fulfill our request.
2720                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2721                                 chain_hash,
2722                                 full_information: false,
2723                                 first_blocknum: 1000,
2724                                 number_of_blocks: 100,
2725                                 short_channel_ids: vec![],
2726                         });
2727                         assert!(result.is_err());
2728                         assert_eq!(result.err().unwrap().err, "Received reply_channel_range with no information available");
2729                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2730                 }
2731
2732                 // Test receipt of a reply that has a first_blocknum that is above the first_blocknum
2733                 // requested in our query. The reply must contain the queried block range. We expect an
2734                 // error result and the task should be removed.
2735                 {
2736                         // Initiate a channel range query to create a query task
2737                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2738                         assert!(result.is_ok());
2739
2740                         // Clear the SendRangeQuery event
2741                         net_graph_msg_handler.get_and_clear_pending_msg_events();
2742
2743                         // Handle the reply that has a first_blocknum above the query's first_blocknum
2744                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2745                                 chain_hash,
2746                                 full_information: true,
2747                                 first_blocknum: 1001,
2748                                 number_of_blocks: 100,
2749                                 short_channel_ids: vec![],
2750                         });
2751                         assert!(result.is_err());
2752                         assert_eq!(result.err().unwrap().err, "Failing reply_channel_range with invalid first_blocknum");
2753                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2754                 }
2755
2756                 // Test receipt of a first reply that does not overlap the query range at all. The first message
2757                 // must have some overlap with the query. We expect an error result and the task should
2758                 // be removed.
2759                 {
2760                         // Initiate a channel range query to create a query task
2761                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2762                         assert!(result.is_ok());
2763
2764                         // Clear the SendRangeQuery event
2765                         net_graph_msg_handler.get_and_clear_pending_msg_events();
2766
2767                         // Handle a reply that contains a block range that precedes the queried block range
2768                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2769                                 chain_hash,
2770                                 full_information: true,
2771                                 first_blocknum: 0,
2772                                 number_of_blocks: 1000,
2773                                 short_channel_ids: vec![],
2774                         });
2775                         assert!(result.is_err());
2776                         assert_eq!(result.err().unwrap().err, "Failing reply_channel_range with non-overlapping first reply");
2777                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2778                 }
2779
2780                 // Test receipt of a sequence of replies with a valid first reply and a second reply that is
2781                 // non-sequential. The spec requires a subsequent first_blocknum to equal the prior
2782                 // first_blocknum plus number_of_blocks. We expect an IgnoreError result and the task should
2783                 // be removed.
2784                 {
2785                         // Initiate a channel range query to create a query task
2786                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2787                         assert!(result.is_ok());
2788
2789                         // Clear the SendRangeQuery event
2790                         net_graph_msg_handler.get_and_clear_pending_msg_events();
2791
2792                         // Handle the first reply
2793                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2794                                 chain_hash,
2795                                 full_information: true,
2796                                 first_blocknum: 1000,
2797                                 number_of_blocks: 50,
2798                                 short_channel_ids: vec![0x0003e8_000000_0000,0x0003e9_000000_0000,0x0003f0_000000_0000],
2799                         });
2800                         assert!(result.is_ok());
2801
2802                         // Handle the second reply which does not start at the proper first_blocknum. We expect
2803                         // to return an error and remove the task.
2804                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2805                                 chain_hash,
2806                                 full_information: true,
2807                                 first_blocknum: 1051,
2808                                 number_of_blocks: 50,
2809                                 short_channel_ids: vec![0x0003f1_000000_0000,0x0003f2_000000_0000],
2810                         });
2811                         assert!(result.is_err());
2812                         assert_eq!(result.err().unwrap().err, "Failing reply_channel_range with invalid sequence");
2813                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2814                 }
2815
2816                 // Test receipt of too many reply messages. We expect an IgnoreError result and the task should
2817                 // be removed.
2818                 {
2819                         // Initiate a channel range query to create a query task
2820                         let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 0xffff_ffff);
2821                         assert!(result.is_ok());
2822
2823                         // Clear the SendRangeQuery event
2824                         net_graph_msg_handler.get_and_clear_pending_msg_events();
2825
2826                         // Handle a sequence of replies that will fail once the max number of reply has been exceeded.
2827                         for block in 1000..=1000 + super::MAX_REPLY_CHANNEL_RANGE_PER_QUERY + 10 {
2828                                 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2829                                         chain_hash,
2830                                         full_information: true,
2831                                         first_blocknum: block as u32,
2832                                         number_of_blocks: 1,
2833                                         short_channel_ids: vec![(block as u64) << 40],
2834                                 });
2835                                 if block <= 1000 + super::MAX_REPLY_CHANNEL_RANGE_PER_QUERY {
2836                                         assert!(result.is_ok());
2837                                 } else if block == 1001 + super::MAX_REPLY_CHANNEL_RANGE_PER_QUERY {
2838                                         assert!(result.is_err());
2839                                         assert_eq!(result.err().unwrap().err, "Failing reply_channel_range due to excessive messages");
2840                                 } else {
2841                                         assert!(result.is_err());
2842                                         assert_eq!(result.err().unwrap().err, "Received unknown reply_channel_range message");
2843                                 }
2844                         }
2845
2846                         // Expect the task to be removed
2847                         assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2848                 }
2849         }
2850
2851         #[test]
2852         fn handling_reply_short_channel_ids() {
2853                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2854                 let node_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2855                 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
2856
2857                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2858
2859                 // Test receipt of a reply when no query exists. We expect an error to be returned
2860                 {
2861                         let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2862                                 chain_hash,
2863                                 full_information: true,
2864                         });
2865                         assert!(result.is_err());
2866                         assert_eq!(result.err().unwrap().err, "Unknown reply_short_channel_ids_end message");
2867                 }
2868
2869                 // Test receipt of a reply that is for a different chain_hash. We expect an error and the task
2870                 // should be removed.
2871                 {
2872                         // Initiate a query to create a pending query task
2873                         let result = net_graph_msg_handler.query_short_channel_ids(&node_id, chain_hash, vec![0x0003e8_000000_0000]);
2874                         assert!(result.is_ok());
2875
2876                         // Process reply with incorrect chain_hash
2877                         let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2878                                 chain_hash: genesis_block(Network::Bitcoin).header.block_hash(),
2879                                 full_information: true,
2880                         });
2881                         assert!(result.is_err());
2882                         assert_eq!(result.err().unwrap().err, "Received reply_short_channel_ids_end with incorrect chain_hash");
2883
2884                         // Expect the task to be removed
2885                         assert!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().is_empty());
2886                 }
2887
2888                 // Test receipt of a reply that indicates the peer does not maintain up-to-date information
2889                 // for the chain_hash requested in the query. We expect an error and task should be removed.
2890                 {
2891                         // Initiate a query to create a pending query task
2892                         let result = net_graph_msg_handler.query_short_channel_ids(&node_id, chain_hash, vec![0x0003e8_000000_0000]);
2893                         assert!(result.is_ok());
2894
2895                         // Process failed reply
2896                         let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2897                                 chain_hash,
2898                                 full_information: false,
2899                         });
2900                         assert!(result.is_err());
2901                         assert_eq!(result.err().unwrap().err, "Received reply_short_channel_ids_end with no information");
2902
2903                         // Expect the task to be removed
2904                         assert!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().is_empty());
2905                 }
2906
2907                 // Test receipt of a successful reply when there are no additional scids to query. We expect
2908                 // the task to be removed.
2909                 {
2910                         // Initiate a query to create a pending query task
2911                         let result = net_graph_msg_handler.query_short_channel_ids(&node_id, chain_hash, vec![0x0003e8_000000_0000]);
2912                         assert!(result.is_ok());
2913
2914                         // Process success reply
2915                         let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2916                                 chain_hash,
2917                                 full_information: true,
2918                         });
2919                         assert!(result.is_ok());
2920
2921                         // Expect the task to be removed
2922                         assert!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().is_empty());
2923                 }
2924
2925                 // Test receipt of a successful reply when there are additional scids to query. We expect
2926                 // additional queries to be sent until the task can be removed.
2927                 {
2928                         // Initiate a query to create a pending query task
2929                         let result = net_graph_msg_handler.query_short_channel_ids(&node_id, chain_hash, vec![0x0003e8_000000_0000]);
2930                         assert!(result.is_ok());
2931
2932                         // Initiate a second query to add pending scids to the task
2933                         let result = net_graph_msg_handler.query_short_channel_ids(&node_id, chain_hash, vec![0x0003e9_000000_0000]);
2934                         assert!(result.is_ok());
2935                         assert_eq!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().get(&node_id).unwrap().short_channel_ids, vec![0x0003e9_000000_0000]);
2936
2937                         // Initiate a third query to add pending scids to the task
2938                         let result = net_graph_msg_handler.query_short_channel_ids(&node_id, chain_hash, vec![0x0003f0_000000_0000]);
2939                         assert!(result.is_ok());
2940                         assert_eq!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().get(&node_id).unwrap().short_channel_ids, vec![0x0003e9_000000_0000, 0x0003f0_000000_0000]);
2941
2942                         // Clear all of the pending send events
2943                         net_graph_msg_handler.get_and_clear_pending_msg_events();
2944
2945                         // Handle the first successful reply, which will send the next batch of scids in a new query
2946                         let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2947                                 chain_hash,
2948                                 full_information: true,
2949                         });
2950                         assert!(result.is_ok());
2951
2952                         // We expect the second batch to be sent in an event
2953                         let expected_node_id = &node_id;
2954                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2955                         assert_eq!(events.len(), 1);
2956                         match &events[0] {
2957                                 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2958                                         assert_eq!(node_id, expected_node_id);
2959                                         assert_eq!(msg.chain_hash, chain_hash);
2960                                         assert_eq!(msg.short_channel_ids, vec![0x0003e9_000000_0000, 0x0003f0_000000_0000]);
2961                                 },
2962                                 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2963                         }
2964
2965                         // We expect the scids to be cleared from the task
2966                         assert_eq!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().get(&node_id).unwrap().short_channel_ids.len(), 0);
2967
2968                         // Handle the second successful reply
2969                         let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2970                                 chain_hash,
2971                                 full_information: true,
2972                         });
2973                         assert!(result.is_ok());
2974
2975                         // We expect the task should be removed
2976                         assert!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().is_empty());
2977                 }
2978         }
2979
2980         #[test]
2981         fn handling_query_channel_range() {
2982                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2983                 let node_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2984                 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
2985
2986                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2987
2988                 let result = net_graph_msg_handler.handle_query_channel_range(&node_id, &QueryChannelRange {
2989                         chain_hash,
2990                         first_blocknum: 0,
2991                         number_of_blocks: 0xffff_ffff,
2992                 });
2993                 assert!(result.is_err());
2994         }
2995
2996         #[test]
2997         fn handling_query_short_channel_ids() {
2998                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2999                 let node_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
3000                 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
3001
3002                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
3003
3004                 let result = net_graph_msg_handler.handle_query_short_channel_ids(&node_id, &QueryShortChannelIds {
3005                         chain_hash,
3006                         short_channel_ids: vec![0x0003e8_000000_0000],
3007                 });
3008                 assert!(result.is_err());
3009         }
3010 }