Actual no_std support
[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, Init, 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, Level};
32 use util::events::{MessageSendEvent, MessageSendEventsProvider};
33 use util::scid_utils::{block_from_scid, scid_from_parts, MAX_SCID_BLOCK};
34
35 use io;
36 use prelude::*;
37 use alloc::collections::{BTreeMap, btree_map::Entry as BtreeEntry};
38 use core::{cmp, fmt};
39 use sync::{RwLock, RwLockReadGuard};
40 use core::sync::atomic::{AtomicUsize, Ordering};
41 use sync::Mutex;
42 use core::ops::Deref;
43 use bitcoin::hashes::hex::ToHex;
44
45 /// The maximum number of extra bytes which we do not understand in a gossip message before we will
46 /// refuse to relay the message.
47 const MAX_EXCESS_BYTES_FOR_RELAY: usize = 1024;
48
49 /// Maximum number of short_channel_ids that will be encoded in one gossip reply message.
50 /// This value ensures a reply fits within the 65k payload limit and is consistent with other implementations.
51 const MAX_SCIDS_PER_REPLY: usize = 8000;
52
53 /// Represents the network as nodes and channels between them
54 #[derive(Clone, PartialEq)]
55 pub struct NetworkGraph {
56         genesis_hash: BlockHash,
57         channels: BTreeMap<u64, ChannelInfo>,
58         nodes: BTreeMap<PublicKey, NodeInfo>,
59 }
60
61 /// A simple newtype for RwLockReadGuard<'a, NetworkGraph>.
62 /// This exists only to make accessing a RwLock<NetworkGraph> possible from
63 /// the C bindings, as it can be done directly in Rust code.
64 pub struct LockedNetworkGraph<'a>(pub RwLockReadGuard<'a, NetworkGraph>);
65
66 /// Receives and validates network updates from peers,
67 /// stores authentic and relevant data as a network graph.
68 /// This network graph is then used for routing payments.
69 /// Provides interface to help with initial routing sync by
70 /// serving historical announcements.
71 pub struct NetGraphMsgHandler<C: Deref, L: Deref> where C::Target: chain::Access, L::Target: Logger {
72         secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
73         /// Representation of the payment channel network
74         pub network_graph: RwLock<NetworkGraph>,
75         chain_access: Option<C>,
76         full_syncs_requested: AtomicUsize,
77         pending_events: Mutex<Vec<MessageSendEvent>>,
78         logger: L,
79 }
80
81 impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: chain::Access, L::Target: Logger {
82         /// Creates a new tracker of the actual state of the network of channels and nodes,
83         /// assuming a fresh network graph.
84         /// Chain monitor is used to make sure announced channels exist on-chain,
85         /// channel data is correct, and that the announcement is signed with
86         /// channel owners' keys.
87         pub fn new(genesis_hash: BlockHash, chain_access: Option<C>, logger: L) -> Self {
88                 NetGraphMsgHandler {
89                         secp_ctx: Secp256k1::verification_only(),
90                         network_graph: RwLock::new(NetworkGraph::new(genesis_hash)),
91                         full_syncs_requested: AtomicUsize::new(0),
92                         chain_access,
93                         pending_events: Mutex::new(vec![]),
94                         logger,
95                 }
96         }
97
98         /// Creates a new tracker of the actual state of the network of channels and nodes,
99         /// assuming an existing Network Graph.
100         pub fn from_net_graph(chain_access: Option<C>, logger: L, network_graph: NetworkGraph) -> Self {
101                 NetGraphMsgHandler {
102                         secp_ctx: Secp256k1::verification_only(),
103                         network_graph: RwLock::new(network_graph),
104                         full_syncs_requested: AtomicUsize::new(0),
105                         chain_access,
106                         pending_events: Mutex::new(vec![]),
107                         logger,
108                 }
109         }
110
111         /// Adds a provider used to check new announcements. Does not affect
112         /// existing announcements unless they are updated.
113         /// Add, update or remove the provider would replace the current one.
114         pub fn add_chain_access(&mut self, chain_access: Option<C>) {
115                 self.chain_access = chain_access;
116         }
117
118         /// Take a read lock on the network_graph and return it in the C-bindings
119         /// newtype helper. This is likely only useful when called via the C
120         /// bindings as you can call `self.network_graph.read().unwrap()` in Rust
121         /// yourself.
122         pub fn read_locked_graph<'a>(&'a self) -> LockedNetworkGraph<'a> {
123                 LockedNetworkGraph(self.network_graph.read().unwrap())
124         }
125
126         /// Returns true when a full routing table sync should be performed with a peer.
127         fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
128                 //TODO: Determine whether to request a full sync based on the network map.
129                 const FULL_SYNCS_TO_REQUEST: usize = 5;
130                 if self.full_syncs_requested.load(Ordering::Acquire) < FULL_SYNCS_TO_REQUEST {
131                         self.full_syncs_requested.fetch_add(1, Ordering::AcqRel);
132                         true
133                 } else {
134                         false
135                 }
136         }
137 }
138
139 impl<'a> LockedNetworkGraph<'a> {
140         /// Get a reference to the NetworkGraph which this read-lock contains.
141         pub fn graph(&self) -> &NetworkGraph {
142                 &*self.0
143         }
144 }
145
146
147 macro_rules! secp_verify_sig {
148         ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
149                 match $secp_ctx.verify($msg, $sig, $pubkey) {
150                         Ok(_) => {},
151                         Err(_) => return Err(LightningError{err: "Invalid signature from remote node".to_owned(), action: ErrorAction::IgnoreError}),
152                 }
153         };
154 }
155
156 impl<C: Deref , L: Deref > RoutingMessageHandler for NetGraphMsgHandler<C, L> where C::Target: chain::Access, L::Target: Logger {
157         fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
158                 self.network_graph.write().unwrap().update_node_from_announcement(msg, &self.secp_ctx)?;
159                 Ok(msg.contents.excess_data.len() <=  MAX_EXCESS_BYTES_FOR_RELAY &&
160                    msg.contents.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY &&
161                    msg.contents.excess_data.len() + msg.contents.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY)
162         }
163
164         fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
165                 self.network_graph.write().unwrap().update_channel_from_announcement(msg, &self.chain_access, &self.secp_ctx)?;
166                 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 { "" });
167                 Ok(msg.contents.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY)
168         }
169
170         fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
171                 match update {
172                         &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
173                                 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
174                                 log_debug!(self.logger, "Updating channel with channel_update from a payment failure. Channel {} is {}abled.", msg.contents.short_channel_id, if chan_enabled { "en" } else { "dis" });
175                                 let _ = self.network_graph.write().unwrap().update_channel(msg, &self.secp_ctx);
176                         },
177                         &msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id, is_permanent } => {
178                                 log_debug!(self.logger, "{} channel graph entry for {} due to a payment failure.", if is_permanent { "Removing" } else { "Disabling" }, short_channel_id);
179                                 self.network_graph.write().unwrap().close_channel_from_update(short_channel_id, is_permanent);
180                         },
181                         &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, is_permanent } => {
182                                 log_debug!(self.logger, "{} node graph entry for {} due to a payment failure.", if is_permanent { "Removing" } else { "Disabling" }, node_id);
183                                 self.network_graph.write().unwrap().fail_node(node_id, is_permanent);
184                         },
185                 }
186         }
187
188         fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> {
189                 self.network_graph.write().unwrap().update_channel(msg, &self.secp_ctx)?;
190                 Ok(msg.contents.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY)
191         }
192
193         fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)> {
194                 let network_graph = self.network_graph.read().unwrap();
195                 let mut result = Vec::with_capacity(batch_amount as usize);
196                 let mut iter = network_graph.get_channels().range(starting_point..);
197                 while result.len() < batch_amount as usize {
198                         if let Some((_, ref chan)) = iter.next() {
199                                 if chan.announcement_message.is_some() {
200                                         let chan_announcement = chan.announcement_message.clone().unwrap();
201                                         let mut one_to_two_announcement: Option<msgs::ChannelUpdate> = None;
202                                         let mut two_to_one_announcement: Option<msgs::ChannelUpdate> = None;
203                                         if let Some(one_to_two) = chan.one_to_two.as_ref() {
204                                                 one_to_two_announcement = one_to_two.last_update_message.clone();
205                                         }
206                                         if let Some(two_to_one) = chan.two_to_one.as_ref() {
207                                                 two_to_one_announcement = two_to_one.last_update_message.clone();
208                                         }
209                                         result.push((chan_announcement, one_to_two_announcement, two_to_one_announcement));
210                                 } else {
211                                         // TODO: We may end up sending un-announced channel_updates if we are sending
212                                         // initial sync data while receiving announce/updates for this channel.
213                                 }
214                         } else {
215                                 return result;
216                         }
217                 }
218                 result
219         }
220
221         fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement> {
222                 let network_graph = self.network_graph.read().unwrap();
223                 let mut result = Vec::with_capacity(batch_amount as usize);
224                 let mut iter = if let Some(pubkey) = starting_point {
225                                 let mut iter = network_graph.get_nodes().range((*pubkey)..);
226                                 iter.next();
227                                 iter
228                         } else {
229                                 network_graph.get_nodes().range(..)
230                         };
231                 while result.len() < batch_amount as usize {
232                         if let Some((_, ref node)) = iter.next() {
233                                 if let Some(node_info) = node.announcement_info.as_ref() {
234                                         if node_info.announcement_message.is_some() {
235                                                 result.push(node_info.announcement_message.clone().unwrap());
236                                         }
237                                 }
238                         } else {
239                                 return result;
240                         }
241                 }
242                 result
243         }
244
245         /// Initiates a stateless sync of routing gossip information with a peer
246         /// using gossip_queries. The default strategy used by this implementation
247         /// is to sync the full block range with several peers.
248         ///
249         /// We should expect one or more reply_channel_range messages in response
250         /// to our query_channel_range. Each reply will enqueue a query_scid message
251         /// to request gossip messages for each channel. The sync is considered complete
252         /// when the final reply_scids_end message is received, though we are not
253         /// tracking this directly.
254         fn sync_routing_table(&self, their_node_id: &PublicKey, init_msg: &Init) {
255
256                 // We will only perform a sync with peers that support gossip_queries.
257                 if !init_msg.features.supports_gossip_queries() {
258                         return ();
259                 }
260
261                 // Check if we need to perform a full synchronization with this peer
262                 if !self.should_request_full_sync(their_node_id) {
263                         return ();
264                 }
265
266                 let first_blocknum = 0;
267                 let number_of_blocks = 0xffffffff;
268                 log_debug!(self.logger, "Sending query_channel_range peer={}, first_blocknum={}, number_of_blocks={}", log_pubkey!(their_node_id), first_blocknum, number_of_blocks);
269                 let mut pending_events = self.pending_events.lock().unwrap();
270                 pending_events.push(MessageSendEvent::SendChannelRangeQuery {
271                         node_id: their_node_id.clone(),
272                         msg: QueryChannelRange {
273                                 chain_hash: self.network_graph.read().unwrap().genesis_hash,
274                                 first_blocknum,
275                                 number_of_blocks,
276                         },
277                 });
278         }
279
280         /// Statelessly processes a reply to a channel range query by immediately
281         /// sending an SCID query with SCIDs in the reply. To keep this handler
282         /// stateless, it does not validate the sequencing of replies for multi-
283         /// reply ranges. It does not validate whether the reply(ies) cover the
284         /// queried range. It also does not filter SCIDs to only those in the
285         /// original query range. We also do not validate that the chain_hash
286         /// matches the chain_hash of the NetworkGraph. Any chan_ann message that
287         /// does not match our chain_hash will be rejected when the announcement is
288         /// processed.
289         fn handle_reply_channel_range(&self, their_node_id: &PublicKey, msg: ReplyChannelRange) -> Result<(), LightningError> {
290                 log_debug!(self.logger, "Handling reply_channel_range peer={}, first_blocknum={}, number_of_blocks={}, sync_complete={}, scids={}", log_pubkey!(their_node_id), msg.first_blocknum, msg.number_of_blocks, msg.sync_complete, msg.short_channel_ids.len(),);
291
292                 log_debug!(self.logger, "Sending query_short_channel_ids peer={}, batch_size={}", log_pubkey!(their_node_id), msg.short_channel_ids.len());
293                 let mut pending_events = self.pending_events.lock().unwrap();
294                 pending_events.push(MessageSendEvent::SendShortIdsQuery {
295                         node_id: their_node_id.clone(),
296                         msg: QueryShortChannelIds {
297                                 chain_hash: msg.chain_hash,
298                                 short_channel_ids: msg.short_channel_ids,
299                         }
300                 });
301
302                 Ok(())
303         }
304
305         /// When an SCID query is initiated the remote peer will begin streaming
306         /// gossip messages. In the event of a failure, we may have received
307         /// some channel information. Before trying with another peer, the
308         /// caller should update its set of SCIDs that need to be queried.
309         fn handle_reply_short_channel_ids_end(&self, their_node_id: &PublicKey, msg: ReplyShortChannelIdsEnd) -> Result<(), LightningError> {
310                 log_debug!(self.logger, "Handling reply_short_channel_ids_end peer={}, full_information={}", log_pubkey!(their_node_id), msg.full_information);
311
312                 // If the remote node does not have up-to-date information for the
313                 // chain_hash they will set full_information=false. We can fail
314                 // the result and try again with a different peer.
315                 if !msg.full_information {
316                         return Err(LightningError {
317                                 err: String::from("Received reply_short_channel_ids_end with no information"),
318                                 action: ErrorAction::IgnoreError
319                         });
320                 }
321
322                 Ok(())
323         }
324
325         /// Processes a query from a peer by finding announced/public channels whose funding UTXOs
326         /// are in the specified block range. Due to message size limits, large range
327         /// queries may result in several reply messages. This implementation enqueues
328         /// all reply messages into pending events. Each message will allocate just under 65KiB. A full
329         /// sync of the public routing table with 128k channels will generated 16 messages and allocate ~1MB.
330         /// Logic can be changed to reduce allocation if/when a full sync of the routing table impacts
331         /// memory constrained systems.
332         fn handle_query_channel_range(&self, their_node_id: &PublicKey, msg: QueryChannelRange) -> Result<(), LightningError> {
333                 log_debug!(self.logger, "Handling query_channel_range peer={}, first_blocknum={}, number_of_blocks={}", log_pubkey!(their_node_id), msg.first_blocknum, msg.number_of_blocks);
334
335                 let network_graph = self.network_graph.read().unwrap();
336
337                 let inclusive_start_scid = scid_from_parts(msg.first_blocknum as u64, 0, 0);
338
339                 // We might receive valid queries with end_blocknum that would overflow SCID conversion.
340                 // If so, we manually cap the ending block to avoid this overflow.
341                 let exclusive_end_scid = scid_from_parts(cmp::min(msg.end_blocknum() as u64, MAX_SCID_BLOCK), 0, 0);
342
343                 // Per spec, we must reply to a query. Send an empty message when things are invalid.
344                 if msg.chain_hash != network_graph.genesis_hash || inclusive_start_scid.is_err() || exclusive_end_scid.is_err() || msg.number_of_blocks == 0 {
345                         let mut pending_events = self.pending_events.lock().unwrap();
346                         pending_events.push(MessageSendEvent::SendReplyChannelRange {
347                                 node_id: their_node_id.clone(),
348                                 msg: ReplyChannelRange {
349                                         chain_hash: msg.chain_hash.clone(),
350                                         first_blocknum: msg.first_blocknum,
351                                         number_of_blocks: msg.number_of_blocks,
352                                         sync_complete: true,
353                                         short_channel_ids: vec![],
354                                 }
355                         });
356                         return Err(LightningError {
357                                 err: String::from("query_channel_range could not be processed"),
358                                 action: ErrorAction::IgnoreError,
359                         });
360                 }
361
362                 // Creates channel batches. We are not checking if the channel is routable
363                 // (has at least one update). A peer may still want to know the channel
364                 // exists even if its not yet routable.
365                 let mut batches: Vec<Vec<u64>> = vec![Vec::with_capacity(MAX_SCIDS_PER_REPLY)];
366                 for (_, ref chan) in network_graph.get_channels().range(inclusive_start_scid.unwrap()..exclusive_end_scid.unwrap()) {
367                         if let Some(chan_announcement) = &chan.announcement_message {
368                                 // Construct a new batch if last one is full
369                                 if batches.last().unwrap().len() == batches.last().unwrap().capacity() {
370                                         batches.push(Vec::with_capacity(MAX_SCIDS_PER_REPLY));
371                                 }
372
373                                 let batch = batches.last_mut().unwrap();
374                                 batch.push(chan_announcement.contents.short_channel_id);
375                         }
376                 }
377                 drop(network_graph);
378
379                 let mut pending_events = self.pending_events.lock().unwrap();
380                 let batch_count = batches.len();
381                 let mut prev_batch_endblock = msg.first_blocknum;
382                 for (batch_index, batch) in batches.into_iter().enumerate() {
383                         // Per spec, the initial `first_blocknum` needs to be <= the query's `first_blocknum`
384                         // and subsequent `first_blocknum`s must be >= the prior reply's `first_blocknum`.
385                         //
386                         // Additionally, c-lightning versions < 0.10 require that the `first_blocknum` of each
387                         // reply is >= the previous reply's `first_blocknum` and either exactly the previous
388                         // reply's `first_blocknum + number_of_blocks` or exactly one greater. This is a
389                         // significant diversion from the requirements set by the spec, and, in case of blocks
390                         // with no channel opens (e.g. empty blocks), requires that we use the previous value
391                         // and *not* derive the first_blocknum from the actual first block of the reply.
392                         let first_blocknum = prev_batch_endblock;
393
394                         // Each message carries the number of blocks (from the `first_blocknum`) its contents
395                         // fit in. Though there is no requirement that we use exactly the number of blocks its
396                         // contents are from, except for the bogus requirements c-lightning enforces, above.
397                         //
398                         // Per spec, the last end block (ie `first_blocknum + number_of_blocks`) needs to be
399                         // >= the query's end block. Thus, for the last reply, we calculate the difference
400                         // between the query's end block and the start of the reply.
401                         //
402                         // Overflow safe since end_blocknum=msg.first_block_num+msg.number_of_blocks and
403                         // first_blocknum will be either msg.first_blocknum or a higher block height.
404                         let (sync_complete, number_of_blocks) = if batch_index == batch_count-1 {
405                                 (true, msg.end_blocknum() - first_blocknum)
406                         }
407                         // Prior replies should use the number of blocks that fit into the reply. Overflow
408                         // safe since first_blocknum is always <= last SCID's block.
409                         else {
410                                 (false, block_from_scid(batch.last().unwrap()) - first_blocknum)
411                         };
412
413                         prev_batch_endblock = first_blocknum + number_of_blocks;
414
415                         pending_events.push(MessageSendEvent::SendReplyChannelRange {
416                                 node_id: their_node_id.clone(),
417                                 msg: ReplyChannelRange {
418                                         chain_hash: msg.chain_hash.clone(),
419                                         first_blocknum,
420                                         number_of_blocks,
421                                         sync_complete,
422                                         short_channel_ids: batch,
423                                 }
424                         });
425                 }
426
427                 Ok(())
428         }
429
430         fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: QueryShortChannelIds) -> Result<(), LightningError> {
431                 // TODO
432                 Err(LightningError {
433                         err: String::from("Not implemented"),
434                         action: ErrorAction::IgnoreError,
435                 })
436         }
437 }
438
439 impl<C: Deref, L: Deref> MessageSendEventsProvider for NetGraphMsgHandler<C, L>
440 where
441         C::Target: chain::Access,
442         L::Target: Logger,
443 {
444         fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
445                 let mut ret = Vec::new();
446                 let mut pending_events = self.pending_events.lock().unwrap();
447                 core::mem::swap(&mut ret, &mut pending_events);
448                 ret
449         }
450 }
451
452 #[derive(Clone, Debug, PartialEq)]
453 /// Details about one direction of a channel. Received
454 /// within a channel update.
455 pub struct DirectionalChannelInfo {
456         /// When the last update to the channel direction was issued.
457         /// Value is opaque, as set in the announcement.
458         pub last_update: u32,
459         /// Whether the channel can be currently used for payments (in this one direction).
460         pub enabled: bool,
461         /// The difference in CLTV values that you must have when routing through this channel.
462         pub cltv_expiry_delta: u16,
463         /// The minimum value, which must be relayed to the next hop via the channel
464         pub htlc_minimum_msat: u64,
465         /// The maximum value which may be relayed to the next hop via the channel.
466         pub htlc_maximum_msat: Option<u64>,
467         /// Fees charged when the channel is used for routing
468         pub fees: RoutingFees,
469         /// Most recent update for the channel received from the network
470         /// Mostly redundant with the data we store in fields explicitly.
471         /// Everything else is useful only for sending out for initial routing sync.
472         /// Not stored if contains excess data to prevent DoS.
473         pub last_update_message: Option<ChannelUpdate>,
474 }
475
476 impl fmt::Display for DirectionalChannelInfo {
477         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
478                 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)?;
479                 Ok(())
480         }
481 }
482
483 impl_writeable_tlv_based!(DirectionalChannelInfo, {
484         (0, last_update, required),
485         (2, enabled, required),
486         (4, cltv_expiry_delta, required),
487         (6, htlc_minimum_msat, required),
488         (8, htlc_maximum_msat, required),
489         (10, fees, required),
490         (12, last_update_message, required),
491 });
492
493 #[derive(Clone, Debug, PartialEq)]
494 /// Details about a channel (both directions).
495 /// Received within a channel announcement.
496 pub struct ChannelInfo {
497         /// Protocol features of a channel communicated during its announcement
498         pub features: ChannelFeatures,
499         /// Source node of the first direction of a channel
500         pub node_one: PublicKey,
501         /// Details about the first direction of a channel
502         pub one_to_two: Option<DirectionalChannelInfo>,
503         /// Source node of the second direction of a channel
504         pub node_two: PublicKey,
505         /// Details about the second direction of a channel
506         pub two_to_one: Option<DirectionalChannelInfo>,
507         /// The channel capacity as seen on-chain, if chain lookup is available.
508         pub capacity_sats: Option<u64>,
509         /// An initial announcement of the channel
510         /// Mostly redundant with the data we store in fields explicitly.
511         /// Everything else is useful only for sending out for initial routing sync.
512         /// Not stored if contains excess data to prevent DoS.
513         pub announcement_message: Option<ChannelAnnouncement>,
514 }
515
516 impl fmt::Display for ChannelInfo {
517         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
518                 write!(f, "features: {}, node_one: {}, one_to_two: {:?}, node_two: {}, two_to_one: {:?}",
519                    log_bytes!(self.features.encode()), log_pubkey!(self.node_one), self.one_to_two, log_pubkey!(self.node_two), self.two_to_one)?;
520                 Ok(())
521         }
522 }
523
524 impl_writeable_tlv_based!(ChannelInfo, {
525         (0, features, required),
526         (2, node_one, required),
527         (4, one_to_two, required),
528         (6, node_two, required),
529         (8, two_to_one, required),
530         (10, capacity_sats, required),
531         (12, announcement_message, required),
532 });
533
534
535 /// Fees for routing via a given channel or a node
536 #[derive(Eq, PartialEq, Copy, Clone, Debug)]
537 pub struct RoutingFees {
538         /// Flat routing fee in satoshis
539         pub base_msat: u32,
540         /// Liquidity-based routing fee in millionths of a routed amount.
541         /// In other words, 10000 is 1%.
542         pub proportional_millionths: u32,
543 }
544
545 impl_writeable_tlv_based!(RoutingFees, {
546         (0, base_msat, required),
547         (2, proportional_millionths, required)
548 });
549
550 #[derive(Clone, Debug, PartialEq)]
551 /// Information received in the latest node_announcement from this node.
552 pub struct NodeAnnouncementInfo {
553         /// Protocol features the node announced support for
554         pub features: NodeFeatures,
555         /// When the last known update to the node state was issued.
556         /// Value is opaque, as set in the announcement.
557         pub last_update: u32,
558         /// Color assigned to the node
559         pub rgb: [u8; 3],
560         /// Moniker assigned to the node.
561         /// May be invalid or malicious (eg control chars),
562         /// should not be exposed to the user.
563         pub alias: [u8; 32],
564         /// Internet-level addresses via which one can connect to the node
565         pub addresses: Vec<NetAddress>,
566         /// An initial announcement of the node
567         /// Mostly redundant with the data we store in fields explicitly.
568         /// Everything else is useful only for sending out for initial routing sync.
569         /// Not stored if contains excess data to prevent DoS.
570         pub announcement_message: Option<NodeAnnouncement>
571 }
572
573 impl_writeable_tlv_based!(NodeAnnouncementInfo, {
574         (0, features, required),
575         (2, last_update, required),
576         (4, rgb, required),
577         (6, alias, required),
578         (8, announcement_message, option),
579         (10, addresses, vec_type),
580 });
581
582 #[derive(Clone, Debug, PartialEq)]
583 /// Details about a node in the network, known from the network announcement.
584 pub struct NodeInfo {
585         /// All valid channels a node has announced
586         pub channels: Vec<u64>,
587         /// Lowest fees enabling routing via any of the enabled, known channels to a node.
588         /// The two fields (flat and proportional fee) are independent,
589         /// meaning they don't have to refer to the same channel.
590         pub lowest_inbound_channel_fees: Option<RoutingFees>,
591         /// More information about a node from node_announcement.
592         /// Optional because we store a Node entry after learning about it from
593         /// a channel announcement, but before receiving a node announcement.
594         pub announcement_info: Option<NodeAnnouncementInfo>
595 }
596
597 impl fmt::Display for NodeInfo {
598         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
599                 write!(f, "lowest_inbound_channel_fees: {:?}, channels: {:?}, announcement_info: {:?}",
600                    self.lowest_inbound_channel_fees, &self.channels[..], self.announcement_info)?;
601                 Ok(())
602         }
603 }
604
605 impl_writeable_tlv_based!(NodeInfo, {
606         (0, lowest_inbound_channel_fees, option),
607         (2, announcement_info, option),
608         (4, channels, vec_type),
609 });
610
611 const SERIALIZATION_VERSION: u8 = 1;
612 const MIN_SERIALIZATION_VERSION: u8 = 1;
613
614 impl Writeable for NetworkGraph {
615         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
616                 write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
617
618                 self.genesis_hash.write(writer)?;
619                 (self.channels.len() as u64).write(writer)?;
620                 for (ref chan_id, ref chan_info) in self.channels.iter() {
621                         (*chan_id).write(writer)?;
622                         chan_info.write(writer)?;
623                 }
624                 (self.nodes.len() as u64).write(writer)?;
625                 for (ref node_id, ref node_info) in self.nodes.iter() {
626                         node_id.write(writer)?;
627                         node_info.write(writer)?;
628                 }
629
630                 write_tlv_fields!(writer, {});
631                 Ok(())
632         }
633 }
634
635 impl Readable for NetworkGraph {
636         fn read<R: io::Read>(reader: &mut R) -> Result<NetworkGraph, DecodeError> {
637                 let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
638
639                 let genesis_hash: BlockHash = Readable::read(reader)?;
640                 let channels_count: u64 = Readable::read(reader)?;
641                 let mut channels = BTreeMap::new();
642                 for _ in 0..channels_count {
643                         let chan_id: u64 = Readable::read(reader)?;
644                         let chan_info = Readable::read(reader)?;
645                         channels.insert(chan_id, chan_info);
646                 }
647                 let nodes_count: u64 = Readable::read(reader)?;
648                 let mut nodes = BTreeMap::new();
649                 for _ in 0..nodes_count {
650                         let node_id = Readable::read(reader)?;
651                         let node_info = Readable::read(reader)?;
652                         nodes.insert(node_id, node_info);
653                 }
654                 read_tlv_fields!(reader, {});
655
656                 Ok(NetworkGraph {
657                         genesis_hash,
658                         channels,
659                         nodes,
660                 })
661         }
662 }
663
664 impl fmt::Display for NetworkGraph {
665         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
666                 writeln!(f, "Network map\n[Channels]")?;
667                 for (key, val) in self.channels.iter() {
668                         writeln!(f, " {}: {}", key, val)?;
669                 }
670                 writeln!(f, "[Nodes]")?;
671                 for (key, val) in self.nodes.iter() {
672                         writeln!(f, " {}: {}", log_pubkey!(key), val)?;
673                 }
674                 Ok(())
675         }
676 }
677
678 impl NetworkGraph {
679         /// Returns all known valid channels' short ids along with announced channel info.
680         ///
681         /// (C-not exported) because we have no mapping for `BTreeMap`s
682         pub fn get_channels<'a>(&'a self) -> &'a BTreeMap<u64, ChannelInfo> { &self.channels }
683         /// Returns all known nodes' public keys along with announced node info.
684         ///
685         /// (C-not exported) because we have no mapping for `BTreeMap`s
686         pub fn get_nodes<'a>(&'a self) -> &'a BTreeMap<PublicKey, NodeInfo> { &self.nodes }
687
688         /// Get network addresses by node id.
689         /// Returns None if the requested node is completely unknown,
690         /// or if node announcement for the node was never received.
691         ///
692         /// (C-not exported) as there is no practical way to track lifetimes of returned values.
693         pub fn get_addresses<'a>(&'a self, pubkey: &PublicKey) -> Option<&'a Vec<NetAddress>> {
694                 if let Some(node) = self.nodes.get(pubkey) {
695                         if let Some(node_info) = node.announcement_info.as_ref() {
696                                 return Some(&node_info.addresses)
697                         }
698                 }
699                 None
700         }
701
702         /// Creates a new, empty, network graph.
703         pub fn new(genesis_hash: BlockHash) -> NetworkGraph {
704                 Self {
705                         genesis_hash,
706                         channels: BTreeMap::new(),
707                         nodes: BTreeMap::new(),
708                 }
709         }
710
711         /// For an already known node (from channel announcements), update its stored properties from a
712         /// given node announcement.
713         ///
714         /// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
715         /// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
716         /// routing messages from a source using a protocol other than the lightning P2P protocol.
717         pub fn update_node_from_announcement<T: secp256k1::Verification>(&mut self, msg: &msgs::NodeAnnouncement, secp_ctx: &Secp256k1<T>) -> Result<(), LightningError> {
718                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
719                 secp_verify_sig!(secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
720                 self.update_node_from_announcement_intern(&msg.contents, Some(&msg))
721         }
722
723         /// For an already known node (from channel announcements), update its stored properties from a
724         /// given node announcement without verifying the associated signatures. Because we aren't
725         /// given the associated signatures here we cannot relay the node announcement to any of our
726         /// peers.
727         pub fn update_node_from_unsigned_announcement(&mut self, msg: &msgs::UnsignedNodeAnnouncement) -> Result<(), LightningError> {
728                 self.update_node_from_announcement_intern(msg, None)
729         }
730
731         fn update_node_from_announcement_intern(&mut self, msg: &msgs::UnsignedNodeAnnouncement, full_msg: Option<&msgs::NodeAnnouncement>) -> Result<(), LightningError> {
732                 match self.nodes.get_mut(&msg.node_id) {
733                         None => Err(LightningError{err: "No existing channels for node_announcement".to_owned(), action: ErrorAction::IgnoreError}),
734                         Some(node) => {
735                                 if let Some(node_info) = node.announcement_info.as_ref() {
736                                         if node_info.last_update  >= msg.timestamp {
737                                                 return Err(LightningError{err: "Update older than last processed update".to_owned(), action: ErrorAction::IgnoreAndLog(Level::Trace)});
738                                         }
739                                 }
740
741                                 let should_relay =
742                                         msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY &&
743                                         msg.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY &&
744                                         msg.excess_data.len() + msg.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY;
745                                 node.announcement_info = Some(NodeAnnouncementInfo {
746                                         features: msg.features.clone(),
747                                         last_update: msg.timestamp,
748                                         rgb: msg.rgb,
749                                         alias: msg.alias,
750                                         addresses: msg.addresses.clone(),
751                                         announcement_message: if should_relay { full_msg.cloned() } else { None },
752                                 });
753
754                                 Ok(())
755                         }
756                 }
757         }
758
759         /// Store or update channel info from a channel announcement.
760         ///
761         /// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
762         /// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
763         /// routing messages from a source using a protocol other than the lightning P2P protocol.
764         ///
765         /// If a `chain::Access` object is provided via `chain_access`, it will be called to verify
766         /// the corresponding UTXO exists on chain and is correctly-formatted.
767         pub fn update_channel_from_announcement<T: secp256k1::Verification, C: Deref>
768                         (&mut self, msg: &msgs::ChannelAnnouncement, chain_access: &Option<C>, secp_ctx: &Secp256k1<T>)
769                         -> Result<(), LightningError>
770                         where C::Target: chain::Access {
771                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
772                 secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
773                 secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
774                 secp_verify_sig!(secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
775                 secp_verify_sig!(secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
776                 self.update_channel_from_unsigned_announcement_intern(&msg.contents, Some(msg), chain_access)
777         }
778
779         /// Store or update channel info from a channel announcement without verifying the associated
780         /// signatures. Because we aren't given the associated signatures here we cannot relay the
781         /// channel announcement to any of our peers.
782         ///
783         /// If a `chain::Access` object is provided via `chain_access`, it will be called to verify
784         /// the corresponding UTXO exists on chain and is correctly-formatted.
785         pub fn update_channel_from_unsigned_announcement<C: Deref>
786                         (&mut self, msg: &msgs::UnsignedChannelAnnouncement, chain_access: &Option<C>)
787                         -> Result<(), LightningError>
788                         where C::Target: chain::Access {
789                 self.update_channel_from_unsigned_announcement_intern(msg, None, chain_access)
790         }
791
792         fn update_channel_from_unsigned_announcement_intern<C: Deref>
793                         (&mut self, msg: &msgs::UnsignedChannelAnnouncement, full_msg: Option<&msgs::ChannelAnnouncement>, chain_access: &Option<C>)
794                         -> Result<(), LightningError>
795                         where C::Target: chain::Access {
796                 if msg.node_id_1 == msg.node_id_2 || msg.bitcoin_key_1 == msg.bitcoin_key_2 {
797                         return Err(LightningError{err: "Channel announcement node had a channel with itself".to_owned(), action: ErrorAction::IgnoreError});
798                 }
799
800                 let utxo_value = match &chain_access {
801                         &None => {
802                                 // Tentatively accept, potentially exposing us to DoS attacks
803                                 None
804                         },
805                         &Some(ref chain_access) => {
806                                 match chain_access.get_utxo(&msg.chain_hash, msg.short_channel_id) {
807                                         Ok(TxOut { value, script_pubkey }) => {
808                                                 let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
809                                                                                     .push_slice(&msg.bitcoin_key_1.serialize())
810                                                                                     .push_slice(&msg.bitcoin_key_2.serialize())
811                                                                                     .push_opcode(opcodes::all::OP_PUSHNUM_2)
812                                                                                     .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
813                                                 if script_pubkey != expected_script {
814                                                         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});
815                                                 }
816                                                 //TODO: Check if value is worth storing, use it to inform routing, and compare it
817                                                 //to the new HTLC max field in channel_update
818                                                 Some(value)
819                                         },
820                                         Err(chain::AccessError::UnknownChain) => {
821                                                 return Err(LightningError{err: format!("Channel announced on an unknown chain ({})", msg.chain_hash.encode().to_hex()), action: ErrorAction::IgnoreError});
822                                         },
823                                         Err(chain::AccessError::UnknownTx) => {
824                                                 return Err(LightningError{err: "Channel announced without corresponding UTXO entry".to_owned(), action: ErrorAction::IgnoreError});
825                                         },
826                                 }
827                         },
828                 };
829
830                 let chan_info = ChannelInfo {
831                                 features: msg.features.clone(),
832                                 node_one: msg.node_id_1.clone(),
833                                 one_to_two: None,
834                                 node_two: msg.node_id_2.clone(),
835                                 two_to_one: None,
836                                 capacity_sats: utxo_value,
837                                 announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
838                                         { full_msg.cloned() } else { None },
839                         };
840
841                 match self.channels.entry(msg.short_channel_id) {
842                         BtreeEntry::Occupied(mut entry) => {
843                                 //TODO: because asking the blockchain if short_channel_id is valid is only optional
844                                 //in the blockchain API, we need to handle it smartly here, though it's unclear
845                                 //exactly how...
846                                 if utxo_value.is_some() {
847                                         // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
848                                         // only sometimes returns results. In any case remove the previous entry. Note
849                                         // that the spec expects us to "blacklist" the node_ids involved, but we can't
850                                         // do that because
851                                         // a) we don't *require* a UTXO provider that always returns results.
852                                         // b) we don't track UTXOs of channels we know about and remove them if they
853                                         //    get reorg'd out.
854                                         // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
855                                         Self::remove_channel_in_nodes(&mut self.nodes, &entry.get(), msg.short_channel_id);
856                                         *entry.get_mut() = chan_info;
857                                 } else {
858                                         return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreAndLog(Level::Trace)})
859                                 }
860                         },
861                         BtreeEntry::Vacant(entry) => {
862                                 entry.insert(chan_info);
863                         }
864                 };
865
866                 macro_rules! add_channel_to_node {
867                         ( $node_id: expr ) => {
868                                 match self.nodes.entry($node_id) {
869                                         BtreeEntry::Occupied(node_entry) => {
870                                                 node_entry.into_mut().channels.push(msg.short_channel_id);
871                                         },
872                                         BtreeEntry::Vacant(node_entry) => {
873                                                 node_entry.insert(NodeInfo {
874                                                         channels: vec!(msg.short_channel_id),
875                                                         lowest_inbound_channel_fees: None,
876                                                         announcement_info: None,
877                                                 });
878                                         }
879                                 }
880                         };
881                 }
882
883                 add_channel_to_node!(msg.node_id_1);
884                 add_channel_to_node!(msg.node_id_2);
885
886                 Ok(())
887         }
888
889         /// Close a channel if a corresponding HTLC fail was sent.
890         /// If permanent, removes a channel from the local storage.
891         /// May cause the removal of nodes too, if this was their last channel.
892         /// If not permanent, makes channels unavailable for routing.
893         pub fn close_channel_from_update(&mut self, short_channel_id: u64, is_permanent: bool) {
894                 if is_permanent {
895                         if let Some(chan) = self.channels.remove(&short_channel_id) {
896                                 Self::remove_channel_in_nodes(&mut self.nodes, &chan, short_channel_id);
897                         }
898                 } else {
899                         if let Some(chan) = self.channels.get_mut(&short_channel_id) {
900                                 if let Some(one_to_two) = chan.one_to_two.as_mut() {
901                                         one_to_two.enabled = false;
902                                 }
903                                 if let Some(two_to_one) = chan.two_to_one.as_mut() {
904                                         two_to_one.enabled = false;
905                                 }
906                         }
907                 }
908         }
909
910         fn fail_node(&mut self, _node_id: &PublicKey, is_permanent: bool) {
911                 if is_permanent {
912                         // TODO: Wholly remove the node
913                 } else {
914                         // TODO: downgrade the node
915                 }
916         }
917
918         /// For an already known (from announcement) channel, update info about one of the directions
919         /// of the channel.
920         ///
921         /// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
922         /// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
923         /// routing messages from a source using a protocol other than the lightning P2P protocol.
924         pub fn update_channel<T: secp256k1::Verification>(&mut self, msg: &msgs::ChannelUpdate, secp_ctx: &Secp256k1<T>) -> Result<(), LightningError> {
925                 self.update_channel_intern(&msg.contents, Some(&msg), Some((&msg.signature, secp_ctx)))
926         }
927
928         /// For an already known (from announcement) channel, update info about one of the directions
929         /// of the channel without verifying the associated signatures. Because we aren't given the
930         /// associated signatures here we cannot relay the channel update to any of our peers.
931         pub fn update_channel_unsigned(&mut self, msg: &msgs::UnsignedChannelUpdate) -> Result<(), LightningError> {
932                 self.update_channel_intern(msg, None, None::<(&secp256k1::Signature, &Secp256k1<secp256k1::VerifyOnly>)>)
933         }
934
935         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> {
936                 let dest_node_id;
937                 let chan_enabled = msg.flags & (1 << 1) != (1 << 1);
938                 let chan_was_enabled;
939
940                 match self.channels.get_mut(&msg.short_channel_id) {
941                         None => return Err(LightningError{err: "Couldn't find channel for update".to_owned(), action: ErrorAction::IgnoreError}),
942                         Some(channel) => {
943                                 if let OptionalField::Present(htlc_maximum_msat) = msg.htlc_maximum_msat {
944                                         if htlc_maximum_msat > MAX_VALUE_MSAT {
945                                                 return Err(LightningError{err: "htlc_maximum_msat is larger than maximum possible msats".to_owned(), action: ErrorAction::IgnoreError});
946                                         }
947
948                                         if let Some(capacity_sats) = channel.capacity_sats {
949                                                 // It's possible channel capacity is available now, although it wasn't available at announcement (so the field is None).
950                                                 // Don't query UTXO set here to reduce DoS risks.
951                                                 if capacity_sats > MAX_VALUE_MSAT / 1000 || htlc_maximum_msat > capacity_sats * 1000 {
952                                                         return Err(LightningError{err: "htlc_maximum_msat is larger than channel capacity or capacity is bogus".to_owned(), action: ErrorAction::IgnoreError});
953                                                 }
954                                         }
955                                 }
956                                 macro_rules! maybe_update_channel_info {
957                                         ( $target: expr, $src_node: expr) => {
958                                                 if let Some(existing_chan_info) = $target.as_ref() {
959                                                         if existing_chan_info.last_update >= msg.timestamp {
960                                                                 return Err(LightningError{err: "Update older than last processed update".to_owned(), action: ErrorAction::IgnoreAndLog(Level::Trace)});
961                                                         }
962                                                         chan_was_enabled = existing_chan_info.enabled;
963                                                 } else {
964                                                         chan_was_enabled = false;
965                                                 }
966
967                                                 let last_update_message = if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
968                                                         { full_msg.cloned() } else { None };
969
970                                                 let updated_channel_dir_info = DirectionalChannelInfo {
971                                                         enabled: chan_enabled,
972                                                         last_update: msg.timestamp,
973                                                         cltv_expiry_delta: msg.cltv_expiry_delta,
974                                                         htlc_minimum_msat: msg.htlc_minimum_msat,
975                                                         htlc_maximum_msat: if let OptionalField::Present(max_value) = msg.htlc_maximum_msat { Some(max_value) } else { None },
976                                                         fees: RoutingFees {
977                                                                 base_msat: msg.fee_base_msat,
978                                                                 proportional_millionths: msg.fee_proportional_millionths,
979                                                         },
980                                                         last_update_message
981                                                 };
982                                                 $target = Some(updated_channel_dir_info);
983                                         }
984                                 }
985
986                                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
987                                 if msg.flags & 1 == 1 {
988                                         dest_node_id = channel.node_one.clone();
989                                         if let Some((sig, ctx)) = sig_info {
990                                                 secp_verify_sig!(ctx, &msg_hash, &sig, &channel.node_two);
991                                         }
992                                         maybe_update_channel_info!(channel.two_to_one, channel.node_two);
993                                 } else {
994                                         dest_node_id = channel.node_two.clone();
995                                         if let Some((sig, ctx)) = sig_info {
996                                                 secp_verify_sig!(ctx, &msg_hash, &sig, &channel.node_one);
997                                         }
998                                         maybe_update_channel_info!(channel.one_to_two, channel.node_one);
999                                 }
1000                         }
1001                 }
1002
1003                 if chan_enabled {
1004                         let node = self.nodes.get_mut(&dest_node_id).unwrap();
1005                         let mut base_msat = msg.fee_base_msat;
1006                         let mut proportional_millionths = msg.fee_proportional_millionths;
1007                         if let Some(fees) = node.lowest_inbound_channel_fees {
1008                                 base_msat = cmp::min(base_msat, fees.base_msat);
1009                                 proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
1010                         }
1011                         node.lowest_inbound_channel_fees = Some(RoutingFees {
1012                                 base_msat,
1013                                 proportional_millionths
1014                         });
1015                 } else if chan_was_enabled {
1016                         let node = self.nodes.get_mut(&dest_node_id).unwrap();
1017                         let mut lowest_inbound_channel_fees = None;
1018
1019                         for chan_id in node.channels.iter() {
1020                                 let chan = self.channels.get(chan_id).unwrap();
1021                                 let chan_info_opt;
1022                                 if chan.node_one == dest_node_id {
1023                                         chan_info_opt = chan.two_to_one.as_ref();
1024                                 } else {
1025                                         chan_info_opt = chan.one_to_two.as_ref();
1026                                 }
1027                                 if let Some(chan_info) = chan_info_opt {
1028                                         if chan_info.enabled {
1029                                                 let fees = lowest_inbound_channel_fees.get_or_insert(RoutingFees {
1030                                                         base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
1031                                                 fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
1032                                                 fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
1033                                         }
1034                                 }
1035                         }
1036
1037                         node.lowest_inbound_channel_fees = lowest_inbound_channel_fees;
1038                 }
1039
1040                 Ok(())
1041         }
1042
1043         fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
1044                 macro_rules! remove_from_node {
1045                         ($node_id: expr) => {
1046                                 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
1047                                         entry.get_mut().channels.retain(|chan_id| {
1048                                                 short_channel_id != *chan_id
1049                                         });
1050                                         if entry.get().channels.is_empty() {
1051                                                 entry.remove_entry();
1052                                         }
1053                                 } else {
1054                                         panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
1055                                 }
1056                         }
1057                 }
1058
1059                 remove_from_node!(chan.node_one);
1060                 remove_from_node!(chan.node_two);
1061         }
1062 }
1063
1064 #[cfg(test)]
1065 mod tests {
1066         use chain;
1067         use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
1068         use routing::network_graph::{NetGraphMsgHandler, NetworkGraph, MAX_EXCESS_BYTES_FOR_RELAY};
1069         use ln::msgs::{Init, OptionalField, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
1070                 UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate,
1071                 ReplyChannelRange, ReplyShortChannelIdsEnd, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
1072         use util::test_utils;
1073         use util::logger::Logger;
1074         use util::ser::{Readable, Writeable};
1075         use util::events::{MessageSendEvent, MessageSendEventsProvider};
1076         use util::scid_utils::scid_from_parts;
1077
1078         use bitcoin::hashes::sha256d::Hash as Sha256dHash;
1079         use bitcoin::hashes::Hash;
1080         use bitcoin::network::constants::Network;
1081         use bitcoin::blockdata::constants::genesis_block;
1082         use bitcoin::blockdata::script::Builder;
1083         use bitcoin::blockdata::transaction::TxOut;
1084         use bitcoin::blockdata::opcodes;
1085
1086         use hex;
1087
1088         use bitcoin::secp256k1::key::{PublicKey, SecretKey};
1089         use bitcoin::secp256k1::{All, Secp256k1};
1090
1091         use io;
1092         use prelude::*;
1093         use sync::Arc;
1094
1095         fn create_net_graph_msg_handler() -> (Secp256k1<All>, NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>) {
1096                 let secp_ctx = Secp256k1::new();
1097                 let logger = Arc::new(test_utils::TestLogger::new());
1098                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1099                 let net_graph_msg_handler = NetGraphMsgHandler::new(genesis_hash, None, Arc::clone(&logger));
1100                 (secp_ctx, net_graph_msg_handler)
1101         }
1102
1103         #[test]
1104         fn request_full_sync_finite_times() {
1105                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1106                 let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
1107
1108                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1109                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1110                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1111                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1112                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1113                 assert!(!net_graph_msg_handler.should_request_full_sync(&node_id));
1114         }
1115
1116         #[test]
1117         fn handling_node_announcements() {
1118                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1119
1120                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1121                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1122                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1123                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1124                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1125                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1126                 let zero_hash = Sha256dHash::hash(&[0; 32]);
1127                 let first_announcement_time = 500;
1128
1129                 let mut unsigned_announcement = UnsignedNodeAnnouncement {
1130                         features: NodeFeatures::known(),
1131                         timestamp: first_announcement_time,
1132                         node_id: node_id_1,
1133                         rgb: [0; 3],
1134                         alias: [0; 32],
1135                         addresses: Vec::new(),
1136                         excess_address_data: Vec::new(),
1137                         excess_data: Vec::new(),
1138                 };
1139                 let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1140                 let valid_announcement = NodeAnnouncement {
1141                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1142                         contents: unsigned_announcement.clone()
1143                 };
1144
1145                 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1146                         Ok(_) => panic!(),
1147                         Err(e) => assert_eq!("No existing channels for node_announcement", e.err)
1148                 };
1149
1150                 {
1151                         // Announce a channel to add a corresponding node.
1152                         let unsigned_announcement = UnsignedChannelAnnouncement {
1153                                 features: ChannelFeatures::known(),
1154                                 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
1155                                 short_channel_id: 0,
1156                                 node_id_1,
1157                                 node_id_2,
1158                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1159                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1160                                 excess_data: Vec::new(),
1161                         };
1162
1163                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1164                         let valid_announcement = ChannelAnnouncement {
1165                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1166                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1167                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1168                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1169                                 contents: unsigned_announcement.clone(),
1170                         };
1171                         match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1172                                 Ok(res) => assert!(res),
1173                                 _ => panic!()
1174                         };
1175                 }
1176
1177                 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1178                         Ok(res) => assert!(res),
1179                         Err(_) => panic!()
1180                 };
1181
1182                 let fake_msghash = hash_to_message!(&zero_hash);
1183                 match net_graph_msg_handler.handle_node_announcement(
1184                         &NodeAnnouncement {
1185                                 signature: secp_ctx.sign(&fake_msghash, node_1_privkey),
1186                                 contents: unsigned_announcement.clone()
1187                 }) {
1188                         Ok(_) => panic!(),
1189                         Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1190                 };
1191
1192                 unsigned_announcement.timestamp += 1000;
1193                 unsigned_announcement.excess_data.resize(MAX_EXCESS_BYTES_FOR_RELAY + 1, 0);
1194                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1195                 let announcement_with_data = NodeAnnouncement {
1196                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1197                         contents: unsigned_announcement.clone()
1198                 };
1199                 // Return false because contains excess data.
1200                 match net_graph_msg_handler.handle_node_announcement(&announcement_with_data) {
1201                         Ok(res) => assert!(!res),
1202                         Err(_) => panic!()
1203                 };
1204                 unsigned_announcement.excess_data = Vec::new();
1205
1206                 // Even though previous announcement was not relayed further, we still accepted it,
1207                 // so we now won't accept announcements before the previous one.
1208                 unsigned_announcement.timestamp -= 10;
1209                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1210                 let outdated_announcement = NodeAnnouncement {
1211                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1212                         contents: unsigned_announcement.clone()
1213                 };
1214                 match net_graph_msg_handler.handle_node_announcement(&outdated_announcement) {
1215                         Ok(_) => panic!(),
1216                         Err(e) => assert_eq!(e.err, "Update older than last processed update")
1217                 };
1218         }
1219
1220         #[test]
1221         fn handling_channel_announcements() {
1222                 let secp_ctx = Secp256k1::new();
1223                 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1224
1225                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1226                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1227                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1228                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1229                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1230                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1231
1232                 let good_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
1233                    .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_1_btckey).serialize())
1234                    .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_2_btckey).serialize())
1235                    .push_opcode(opcodes::all::OP_PUSHNUM_2)
1236                    .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
1237
1238
1239                 let mut unsigned_announcement = UnsignedChannelAnnouncement {
1240                         features: ChannelFeatures::known(),
1241                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
1242                         short_channel_id: 0,
1243                         node_id_1,
1244                         node_id_2,
1245                         bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1246                         bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1247                         excess_data: Vec::new(),
1248                 };
1249
1250                 let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1251                 let valid_announcement = ChannelAnnouncement {
1252                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1253                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1254                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1255                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1256                         contents: unsigned_announcement.clone(),
1257                 };
1258
1259                 // Test if the UTXO lookups were not supported
1260                 let mut net_graph_msg_handler = NetGraphMsgHandler::new(genesis_block(Network::Testnet).header.block_hash(), None, Arc::clone(&logger));
1261                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1262                         Ok(res) => assert!(res),
1263                         _ => panic!()
1264                 };
1265
1266                 {
1267                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1268                         match network.get_channels().get(&unsigned_announcement.short_channel_id) {
1269                                 None => panic!(),
1270                                 Some(_) => ()
1271                         }
1272                 }
1273
1274                 // If we receive announcement for the same channel (with UTXO lookups disabled),
1275                 // drop new one on the floor, since we can't see any changes.
1276                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1277                         Ok(_) => panic!(),
1278                         Err(e) => assert_eq!(e.err, "Already have knowledge of channel")
1279                 };
1280
1281                 // Test if an associated transaction were not on-chain (or not confirmed).
1282                 let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
1283                 *chain_source.utxo_ret.lock().unwrap() = Err(chain::AccessError::UnknownTx);
1284                 net_graph_msg_handler = NetGraphMsgHandler::new(chain_source.clone().genesis_hash, Some(chain_source.clone()), Arc::clone(&logger));
1285                 unsigned_announcement.short_channel_id += 1;
1286
1287                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1288                 let valid_announcement = ChannelAnnouncement {
1289                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1290                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1291                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1292                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1293                         contents: unsigned_announcement.clone(),
1294                 };
1295
1296                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1297                         Ok(_) => panic!(),
1298                         Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry")
1299                 };
1300
1301                 // Now test if the transaction is found in the UTXO set and the script is correct.
1302                 unsigned_announcement.short_channel_id += 1;
1303                 *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script.clone() });
1304
1305                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1306                 let valid_announcement = ChannelAnnouncement {
1307                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1308                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1309                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1310                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1311                         contents: unsigned_announcement.clone(),
1312                 };
1313                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1314                         Ok(res) => assert!(res),
1315                         _ => panic!()
1316                 };
1317
1318                 {
1319                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1320                         match network.get_channels().get(&unsigned_announcement.short_channel_id) {
1321                                 None => panic!(),
1322                                 Some(_) => ()
1323                         }
1324                 }
1325
1326                 // If we receive announcement for the same channel (but TX is not confirmed),
1327                 // drop new one on the floor, since we can't see any changes.
1328                 *chain_source.utxo_ret.lock().unwrap() = Err(chain::AccessError::UnknownTx);
1329                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1330                         Ok(_) => panic!(),
1331                         Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry")
1332                 };
1333
1334                 // But if it is confirmed, replace the channel
1335                 *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script });
1336                 unsigned_announcement.features = ChannelFeatures::empty();
1337                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1338                 let valid_announcement = ChannelAnnouncement {
1339                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1340                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1341                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1342                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1343                         contents: unsigned_announcement.clone(),
1344                 };
1345                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1346                         Ok(res) => assert!(res),
1347                         _ => panic!()
1348                 };
1349                 {
1350                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1351                         match network.get_channels().get(&unsigned_announcement.short_channel_id) {
1352                                 Some(channel_entry) => {
1353                                         assert_eq!(channel_entry.features, ChannelFeatures::empty());
1354                                 },
1355                                 _ => panic!()
1356                         }
1357                 }
1358
1359                 // Don't relay valid channels with excess data
1360                 unsigned_announcement.short_channel_id += 1;
1361                 unsigned_announcement.excess_data.resize(MAX_EXCESS_BYTES_FOR_RELAY + 1, 0);
1362                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1363                 let valid_announcement = ChannelAnnouncement {
1364                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1365                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1366                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1367                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1368                         contents: unsigned_announcement.clone(),
1369                 };
1370                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1371                         Ok(res) => assert!(!res),
1372                         _ => panic!()
1373                 };
1374
1375                 unsigned_announcement.excess_data = Vec::new();
1376                 let invalid_sig_announcement = ChannelAnnouncement {
1377                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1378                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1379                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1380                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_1_btckey),
1381                         contents: unsigned_announcement.clone(),
1382                 };
1383                 match net_graph_msg_handler.handle_channel_announcement(&invalid_sig_announcement) {
1384                         Ok(_) => panic!(),
1385                         Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1386                 };
1387
1388                 unsigned_announcement.node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1389                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1390                 let channel_to_itself_announcement = ChannelAnnouncement {
1391                         node_signature_1: secp_ctx.sign(&msghash, node_2_privkey),
1392                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1393                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1394                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1395                         contents: unsigned_announcement.clone(),
1396                 };
1397                 match net_graph_msg_handler.handle_channel_announcement(&channel_to_itself_announcement) {
1398                         Ok(_) => panic!(),
1399                         Err(e) => assert_eq!(e.err, "Channel announcement node had a channel with itself")
1400                 };
1401         }
1402
1403         #[test]
1404         fn handling_channel_update() {
1405                 let secp_ctx = Secp256k1::new();
1406                 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1407                 let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
1408                 let net_graph_msg_handler = NetGraphMsgHandler::new(genesis_block(Network::Testnet).header.block_hash(), Some(chain_source.clone()), Arc::clone(&logger));
1409
1410                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1411                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1412                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1413                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1414                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1415                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1416
1417                 let zero_hash = Sha256dHash::hash(&[0; 32]);
1418                 let short_channel_id = 0;
1419                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
1420                 let amount_sats = 1000_000;
1421
1422                 {
1423                         // Announce a channel we will update
1424                         let good_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
1425                            .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_1_btckey).serialize())
1426                            .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_2_btckey).serialize())
1427                            .push_opcode(opcodes::all::OP_PUSHNUM_2)
1428                            .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
1429                         *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() });
1430                         let unsigned_announcement = UnsignedChannelAnnouncement {
1431                                 features: ChannelFeatures::empty(),
1432                                 chain_hash,
1433                                 short_channel_id,
1434                                 node_id_1,
1435                                 node_id_2,
1436                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1437                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1438                                 excess_data: Vec::new(),
1439                         };
1440
1441                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1442                         let valid_channel_announcement = ChannelAnnouncement {
1443                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1444                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1445                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1446                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1447                                 contents: unsigned_announcement.clone(),
1448                         };
1449                         match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1450                                 Ok(_) => (),
1451                                 Err(_) => panic!()
1452                         };
1453
1454                 }
1455
1456                 let mut unsigned_channel_update = UnsignedChannelUpdate {
1457                         chain_hash,
1458                         short_channel_id,
1459                         timestamp: 100,
1460                         flags: 0,
1461                         cltv_expiry_delta: 144,
1462                         htlc_minimum_msat: 1000000,
1463                         htlc_maximum_msat: OptionalField::Absent,
1464                         fee_base_msat: 10000,
1465                         fee_proportional_millionths: 20,
1466                         excess_data: Vec::new()
1467                 };
1468                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1469                 let valid_channel_update = ChannelUpdate {
1470                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1471                         contents: unsigned_channel_update.clone()
1472                 };
1473
1474                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1475                         Ok(res) => assert!(res),
1476                         _ => panic!()
1477                 };
1478
1479                 {
1480                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1481                         match network.get_channels().get(&short_channel_id) {
1482                                 None => panic!(),
1483                                 Some(channel_info) => {
1484                                         assert_eq!(channel_info.one_to_two.as_ref().unwrap().cltv_expiry_delta, 144);
1485                                         assert!(channel_info.two_to_one.is_none());
1486                                 }
1487                         }
1488                 }
1489
1490                 unsigned_channel_update.timestamp += 100;
1491                 unsigned_channel_update.excess_data.resize(MAX_EXCESS_BYTES_FOR_RELAY + 1, 0);
1492                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1493                 let valid_channel_update = ChannelUpdate {
1494                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1495                         contents: unsigned_channel_update.clone()
1496                 };
1497                 // Return false because contains excess data
1498                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1499                         Ok(res) => assert!(!res),
1500                         _ => panic!()
1501                 };
1502                 unsigned_channel_update.timestamp += 10;
1503
1504                 unsigned_channel_update.short_channel_id += 1;
1505                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1506                 let valid_channel_update = ChannelUpdate {
1507                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1508                         contents: unsigned_channel_update.clone()
1509                 };
1510
1511                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1512                         Ok(_) => panic!(),
1513                         Err(e) => assert_eq!(e.err, "Couldn't find channel for update")
1514                 };
1515                 unsigned_channel_update.short_channel_id = short_channel_id;
1516
1517                 unsigned_channel_update.htlc_maximum_msat = OptionalField::Present(MAX_VALUE_MSAT + 1);
1518                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1519                 let valid_channel_update = ChannelUpdate {
1520                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1521                         contents: unsigned_channel_update.clone()
1522                 };
1523
1524                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1525                         Ok(_) => panic!(),
1526                         Err(e) => assert_eq!(e.err, "htlc_maximum_msat is larger than maximum possible msats")
1527                 };
1528                 unsigned_channel_update.htlc_maximum_msat = OptionalField::Absent;
1529
1530                 unsigned_channel_update.htlc_maximum_msat = OptionalField::Present(amount_sats * 1000 + 1);
1531                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1532                 let valid_channel_update = ChannelUpdate {
1533                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1534                         contents: unsigned_channel_update.clone()
1535                 };
1536
1537                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1538                         Ok(_) => panic!(),
1539                         Err(e) => assert_eq!(e.err, "htlc_maximum_msat is larger than channel capacity or capacity is bogus")
1540                 };
1541                 unsigned_channel_update.htlc_maximum_msat = OptionalField::Absent;
1542
1543                 // Even though previous update was not relayed further, we still accepted it,
1544                 // so we now won't accept update before the previous one.
1545                 unsigned_channel_update.timestamp -= 10;
1546                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1547                 let valid_channel_update = ChannelUpdate {
1548                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1549                         contents: unsigned_channel_update.clone()
1550                 };
1551
1552                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1553                         Ok(_) => panic!(),
1554                         Err(e) => assert_eq!(e.err, "Update older than last processed update")
1555                 };
1556                 unsigned_channel_update.timestamp += 500;
1557
1558                 let fake_msghash = hash_to_message!(&zero_hash);
1559                 let invalid_sig_channel_update = ChannelUpdate {
1560                         signature: secp_ctx.sign(&fake_msghash, node_1_privkey),
1561                         contents: unsigned_channel_update.clone()
1562                 };
1563
1564                 match net_graph_msg_handler.handle_channel_update(&invalid_sig_channel_update) {
1565                         Ok(_) => panic!(),
1566                         Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1567                 };
1568
1569         }
1570
1571         #[test]
1572         fn handling_htlc_fail_channel_update() {
1573                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1574                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1575                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1576                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1577                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1578                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1579                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1580
1581                 let short_channel_id = 0;
1582                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
1583
1584                 {
1585                         // There is no nodes in the table at the beginning.
1586                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1587                         assert_eq!(network.get_nodes().len(), 0);
1588                 }
1589
1590                 {
1591                         // Announce a channel we will update
1592                         let unsigned_announcement = UnsignedChannelAnnouncement {
1593                                 features: ChannelFeatures::empty(),
1594                                 chain_hash,
1595                                 short_channel_id,
1596                                 node_id_1,
1597                                 node_id_2,
1598                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1599                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1600                                 excess_data: Vec::new(),
1601                         };
1602
1603                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1604                         let valid_channel_announcement = ChannelAnnouncement {
1605                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1606                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1607                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1608                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1609                                 contents: unsigned_announcement.clone(),
1610                         };
1611                         match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1612                                 Ok(_) => (),
1613                                 Err(_) => panic!()
1614                         };
1615
1616                         let unsigned_channel_update = UnsignedChannelUpdate {
1617                                 chain_hash,
1618                                 short_channel_id,
1619                                 timestamp: 100,
1620                                 flags: 0,
1621                                 cltv_expiry_delta: 144,
1622                                 htlc_minimum_msat: 1000000,
1623                                 htlc_maximum_msat: OptionalField::Absent,
1624                                 fee_base_msat: 10000,
1625                                 fee_proportional_millionths: 20,
1626                                 excess_data: Vec::new()
1627                         };
1628                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1629                         let valid_channel_update = ChannelUpdate {
1630                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
1631                                 contents: unsigned_channel_update.clone()
1632                         };
1633
1634                         match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1635                                 Ok(res) => assert!(res),
1636                                 _ => panic!()
1637                         };
1638                 }
1639
1640                 // Non-permanent closing just disables a channel
1641                 {
1642                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1643                         match network.get_channels().get(&short_channel_id) {
1644                                 None => panic!(),
1645                                 Some(channel_info) => {
1646                                         assert!(channel_info.one_to_two.is_some());
1647                                 }
1648                         }
1649                 }
1650
1651                 let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
1652                         short_channel_id,
1653                         is_permanent: false
1654                 };
1655
1656                 net_graph_msg_handler.handle_htlc_fail_channel_update(&channel_close_msg);
1657
1658                 // Non-permanent closing just disables a channel
1659                 {
1660                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1661                         match network.get_channels().get(&short_channel_id) {
1662                                 None => panic!(),
1663                                 Some(channel_info) => {
1664                                         assert!(!channel_info.one_to_two.as_ref().unwrap().enabled);
1665                                 }
1666                         }
1667                 }
1668
1669                 let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
1670                         short_channel_id,
1671                         is_permanent: true
1672                 };
1673
1674                 net_graph_msg_handler.handle_htlc_fail_channel_update(&channel_close_msg);
1675
1676                 // Permanent closing deletes a channel
1677                 {
1678                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1679                         assert_eq!(network.get_channels().len(), 0);
1680                         // Nodes are also deleted because there are no associated channels anymore
1681                         assert_eq!(network.get_nodes().len(), 0);
1682                 }
1683                 // TODO: Test HTLCFailChannelUpdate::NodeFailure, which is not implemented yet.
1684         }
1685
1686         #[test]
1687         fn getting_next_channel_announcements() {
1688                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1689                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1690                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1691                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1692                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1693                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1694                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1695
1696                 let short_channel_id = 1;
1697                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
1698
1699                 // Channels were not announced yet.
1700                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(0, 1);
1701                 assert_eq!(channels_with_announcements.len(), 0);
1702
1703                 {
1704                         // Announce a channel we will update
1705                         let unsigned_announcement = UnsignedChannelAnnouncement {
1706                                 features: ChannelFeatures::empty(),
1707                                 chain_hash,
1708                                 short_channel_id,
1709                                 node_id_1,
1710                                 node_id_2,
1711                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1712                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1713                                 excess_data: Vec::new(),
1714                         };
1715
1716                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1717                         let valid_channel_announcement = ChannelAnnouncement {
1718                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1719                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1720                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1721                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1722                                 contents: unsigned_announcement.clone(),
1723                         };
1724                         match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1725                                 Ok(_) => (),
1726                                 Err(_) => panic!()
1727                         };
1728                 }
1729
1730                 // Contains initial channel announcement now.
1731                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1);
1732                 assert_eq!(channels_with_announcements.len(), 1);
1733                 if let Some(channel_announcements) = channels_with_announcements.first() {
1734                         let &(_, ref update_1, ref update_2) = channel_announcements;
1735                         assert_eq!(update_1, &None);
1736                         assert_eq!(update_2, &None);
1737                 } else {
1738                         panic!();
1739                 }
1740
1741
1742                 {
1743                         // Valid channel update
1744                         let unsigned_channel_update = UnsignedChannelUpdate {
1745                                 chain_hash,
1746                                 short_channel_id,
1747                                 timestamp: 101,
1748                                 flags: 0,
1749                                 cltv_expiry_delta: 144,
1750                                 htlc_minimum_msat: 1000000,
1751                                 htlc_maximum_msat: OptionalField::Absent,
1752                                 fee_base_msat: 10000,
1753                                 fee_proportional_millionths: 20,
1754                                 excess_data: Vec::new()
1755                         };
1756                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1757                         let valid_channel_update = ChannelUpdate {
1758                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
1759                                 contents: unsigned_channel_update.clone()
1760                         };
1761                         match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1762                                 Ok(_) => (),
1763                                 Err(_) => panic!()
1764                         };
1765                 }
1766
1767                 // Now contains an initial announcement and an update.
1768                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1);
1769                 assert_eq!(channels_with_announcements.len(), 1);
1770                 if let Some(channel_announcements) = channels_with_announcements.first() {
1771                         let &(_, ref update_1, ref update_2) = channel_announcements;
1772                         assert_ne!(update_1, &None);
1773                         assert_eq!(update_2, &None);
1774                 } else {
1775                         panic!();
1776                 }
1777
1778
1779                 {
1780                         // Channel update with excess data.
1781                         let unsigned_channel_update = UnsignedChannelUpdate {
1782                                 chain_hash,
1783                                 short_channel_id,
1784                                 timestamp: 102,
1785                                 flags: 0,
1786                                 cltv_expiry_delta: 144,
1787                                 htlc_minimum_msat: 1000000,
1788                                 htlc_maximum_msat: OptionalField::Absent,
1789                                 fee_base_msat: 10000,
1790                                 fee_proportional_millionths: 20,
1791                                 excess_data: [1; MAX_EXCESS_BYTES_FOR_RELAY + 1].to_vec()
1792                         };
1793                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1794                         let valid_channel_update = ChannelUpdate {
1795                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
1796                                 contents: unsigned_channel_update.clone()
1797                         };
1798                         match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1799                                 Ok(_) => (),
1800                                 Err(_) => panic!()
1801                         };
1802                 }
1803
1804                 // Test that announcements with excess data won't be returned
1805                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1);
1806                 assert_eq!(channels_with_announcements.len(), 1);
1807                 if let Some(channel_announcements) = channels_with_announcements.first() {
1808                         let &(_, ref update_1, ref update_2) = channel_announcements;
1809                         assert_eq!(update_1, &None);
1810                         assert_eq!(update_2, &None);
1811                 } else {
1812                         panic!();
1813                 }
1814
1815                 // Further starting point have no channels after it
1816                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id + 1000, 1);
1817                 assert_eq!(channels_with_announcements.len(), 0);
1818         }
1819
1820         #[test]
1821         fn getting_next_node_announcements() {
1822                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1823                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1824                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1825                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1826                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1827                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1828                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1829
1830                 let short_channel_id = 1;
1831                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
1832
1833                 // No nodes yet.
1834                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 10);
1835                 assert_eq!(next_announcements.len(), 0);
1836
1837                 {
1838                         // Announce a channel to add 2 nodes
1839                         let unsigned_announcement = UnsignedChannelAnnouncement {
1840                                 features: ChannelFeatures::empty(),
1841                                 chain_hash,
1842                                 short_channel_id,
1843                                 node_id_1,
1844                                 node_id_2,
1845                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1846                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1847                                 excess_data: Vec::new(),
1848                         };
1849
1850                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1851                         let valid_channel_announcement = ChannelAnnouncement {
1852                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1853                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1854                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1855                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1856                                 contents: unsigned_announcement.clone(),
1857                         };
1858                         match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1859                                 Ok(_) => (),
1860                                 Err(_) => panic!()
1861                         };
1862                 }
1863
1864
1865                 // Nodes were never announced
1866                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 3);
1867                 assert_eq!(next_announcements.len(), 0);
1868
1869                 {
1870                         let mut unsigned_announcement = UnsignedNodeAnnouncement {
1871                                 features: NodeFeatures::known(),
1872                                 timestamp: 1000,
1873                                 node_id: node_id_1,
1874                                 rgb: [0; 3],
1875                                 alias: [0; 32],
1876                                 addresses: Vec::new(),
1877                                 excess_address_data: Vec::new(),
1878                                 excess_data: Vec::new(),
1879                         };
1880                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1881                         let valid_announcement = NodeAnnouncement {
1882                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
1883                                 contents: unsigned_announcement.clone()
1884                         };
1885                         match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1886                                 Ok(_) => (),
1887                                 Err(_) => panic!()
1888                         };
1889
1890                         unsigned_announcement.node_id = node_id_2;
1891                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1892                         let valid_announcement = NodeAnnouncement {
1893                                 signature: secp_ctx.sign(&msghash, node_2_privkey),
1894                                 contents: unsigned_announcement.clone()
1895                         };
1896
1897                         match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1898                                 Ok(_) => (),
1899                                 Err(_) => panic!()
1900                         };
1901                 }
1902
1903                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 3);
1904                 assert_eq!(next_announcements.len(), 2);
1905
1906                 // Skip the first node.
1907                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(Some(&node_id_1), 2);
1908                 assert_eq!(next_announcements.len(), 1);
1909
1910                 {
1911                         // Later announcement which should not be relayed (excess data) prevent us from sharing a node
1912                         let unsigned_announcement = UnsignedNodeAnnouncement {
1913                                 features: NodeFeatures::known(),
1914                                 timestamp: 1010,
1915                                 node_id: node_id_2,
1916                                 rgb: [0; 3],
1917                                 alias: [0; 32],
1918                                 addresses: Vec::new(),
1919                                 excess_address_data: Vec::new(),
1920                                 excess_data: [1; MAX_EXCESS_BYTES_FOR_RELAY + 1].to_vec(),
1921                         };
1922                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1923                         let valid_announcement = NodeAnnouncement {
1924                                 signature: secp_ctx.sign(&msghash, node_2_privkey),
1925                                 contents: unsigned_announcement.clone()
1926                         };
1927                         match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1928                                 Ok(res) => assert!(!res),
1929                                 Err(_) => panic!()
1930                         };
1931                 }
1932
1933                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(Some(&node_id_1), 2);
1934                 assert_eq!(next_announcements.len(), 0);
1935         }
1936
1937         #[test]
1938         fn network_graph_serialization() {
1939                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1940
1941                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1942                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1943                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1944                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1945
1946                 // Announce a channel to add a corresponding node.
1947                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1948                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1949                 let unsigned_announcement = UnsignedChannelAnnouncement {
1950                         features: ChannelFeatures::known(),
1951                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
1952                         short_channel_id: 0,
1953                         node_id_1,
1954                         node_id_2,
1955                         bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1956                         bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1957                         excess_data: Vec::new(),
1958                 };
1959
1960                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1961                 let valid_announcement = ChannelAnnouncement {
1962                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1963                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1964                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1965                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1966                         contents: unsigned_announcement.clone(),
1967                 };
1968                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1969                         Ok(res) => assert!(res),
1970                         _ => panic!()
1971                 };
1972
1973
1974                 let node_id = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1975                 let unsigned_announcement = UnsignedNodeAnnouncement {
1976                         features: NodeFeatures::known(),
1977                         timestamp: 100,
1978                         node_id,
1979                         rgb: [0; 3],
1980                         alias: [0; 32],
1981                         addresses: Vec::new(),
1982                         excess_address_data: Vec::new(),
1983                         excess_data: Vec::new(),
1984                 };
1985                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1986                 let valid_announcement = NodeAnnouncement {
1987                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1988                         contents: unsigned_announcement.clone()
1989                 };
1990
1991                 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1992                         Ok(_) => (),
1993                         Err(_) => panic!()
1994                 };
1995
1996                 let network = net_graph_msg_handler.network_graph.write().unwrap();
1997                 let mut w = test_utils::TestVecWriter(Vec::new());
1998                 assert!(!network.get_nodes().is_empty());
1999                 assert!(!network.get_channels().is_empty());
2000                 network.write(&mut w).unwrap();
2001                 assert!(<NetworkGraph>::read(&mut io::Cursor::new(&w.0)).unwrap() == *network);
2002         }
2003
2004         #[test]
2005         fn calling_sync_routing_table() {
2006                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2007                 let node_privkey_1 = &SecretKey::from_slice(&[42; 32]).unwrap();
2008                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_privkey_1);
2009
2010                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2011                 let first_blocknum = 0;
2012                 let number_of_blocks = 0xffff_ffff;
2013
2014                 // It should ignore if gossip_queries feature is not enabled
2015                 {
2016                         let init_msg = Init { features: InitFeatures::known().clear_gossip_queries() };
2017                         net_graph_msg_handler.sync_routing_table(&node_id_1, &init_msg);
2018                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2019                         assert_eq!(events.len(), 0);
2020                 }
2021
2022                 // It should send a query_channel_message with the correct information
2023                 {
2024                         let init_msg = Init { features: InitFeatures::known() };
2025                         net_graph_msg_handler.sync_routing_table(&node_id_1, &init_msg);
2026                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2027                         assert_eq!(events.len(), 1);
2028                         match &events[0] {
2029                                 MessageSendEvent::SendChannelRangeQuery{ node_id, msg } => {
2030                                         assert_eq!(node_id, &node_id_1);
2031                                         assert_eq!(msg.chain_hash, chain_hash);
2032                                         assert_eq!(msg.first_blocknum, first_blocknum);
2033                                         assert_eq!(msg.number_of_blocks, number_of_blocks);
2034                                 },
2035                                 _ => panic!("Expected MessageSendEvent::SendChannelRangeQuery")
2036                         };
2037                 }
2038
2039                 // It should not enqueue a query when should_request_full_sync return false.
2040                 // The initial implementation allows syncing with the first 5 peers after
2041                 // which should_request_full_sync will return false
2042                 {
2043                         let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2044                         let init_msg = Init { features: InitFeatures::known() };
2045                         for n in 1..7 {
2046                                 let node_privkey = &SecretKey::from_slice(&[n; 32]).unwrap();
2047                                 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
2048                                 net_graph_msg_handler.sync_routing_table(&node_id, &init_msg);
2049                                 let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2050                                 if n <= 5 {
2051                                         assert_eq!(events.len(), 1);
2052                                 } else {
2053                                         assert_eq!(events.len(), 0);
2054                                 }
2055
2056                         }
2057                 }
2058         }
2059
2060         #[test]
2061         fn handling_reply_channel_range() {
2062                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2063                 let node_privkey_1 = &SecretKey::from_slice(&[42; 32]).unwrap();
2064                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_privkey_1);
2065
2066                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2067
2068                 // Test receipt of a single reply that should enqueue an SCID query
2069                 // matching the SCIDs in the reply
2070                 {
2071                         let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, ReplyChannelRange {
2072                                 chain_hash,
2073                                 sync_complete: true,
2074                                 first_blocknum: 0,
2075                                 number_of_blocks: 2000,
2076                                 short_channel_ids: vec![
2077                                         0x0003e0_000000_0000, // 992x0x0
2078                                         0x0003e8_000000_0000, // 1000x0x0
2079                                         0x0003e9_000000_0000, // 1001x0x0
2080                                         0x0003f0_000000_0000, // 1008x0x0
2081                                         0x00044c_000000_0000, // 1100x0x0
2082                                         0x0006e0_000000_0000, // 1760x0x0
2083                                 ],
2084                         });
2085                         assert!(result.is_ok());
2086
2087                         // We expect to emit a query_short_channel_ids message with the received scids
2088                         let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2089                         assert_eq!(events.len(), 1);
2090                         match &events[0] {
2091                                 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2092                                         assert_eq!(node_id, &node_id_1);
2093                                         assert_eq!(msg.chain_hash, chain_hash);
2094                                         assert_eq!(msg.short_channel_ids, vec![
2095                                                 0x0003e0_000000_0000, // 992x0x0
2096                                                 0x0003e8_000000_0000, // 1000x0x0
2097                                                 0x0003e9_000000_0000, // 1001x0x0
2098                                                 0x0003f0_000000_0000, // 1008x0x0
2099                                                 0x00044c_000000_0000, // 1100x0x0
2100                                                 0x0006e0_000000_0000, // 1760x0x0
2101                                         ]);
2102                                 },
2103                                 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2104                         }
2105                 }
2106         }
2107
2108         #[test]
2109         fn handling_reply_short_channel_ids() {
2110                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2111                 let node_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2112                 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
2113
2114                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2115
2116                 // Test receipt of a successful reply
2117                 {
2118                         let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, ReplyShortChannelIdsEnd {
2119                                 chain_hash,
2120                                 full_information: true,
2121                         });
2122                         assert!(result.is_ok());
2123                 }
2124
2125                 // Test receipt of a reply that indicates the peer does not maintain up-to-date information
2126                 // for the chain_hash requested in the query.
2127                 {
2128                         let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, ReplyShortChannelIdsEnd {
2129                                 chain_hash,
2130                                 full_information: false,
2131                         });
2132                         assert!(result.is_err());
2133                         assert_eq!(result.err().unwrap().err, "Received reply_short_channel_ids_end with no information");
2134                 }
2135         }
2136
2137         #[test]
2138         fn handling_query_channel_range() {
2139                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2140
2141                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2142                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
2143                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2144                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
2145                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
2146                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2147                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
2148                 let bitcoin_key_1 = PublicKey::from_secret_key(&secp_ctx, node_1_btckey);
2149                 let bitcoin_key_2 = PublicKey::from_secret_key(&secp_ctx, node_2_btckey);
2150
2151                 let mut scids: Vec<u64> = vec![
2152                         scid_from_parts(0xfffffe, 0xffffff, 0xffff).unwrap(), // max
2153                         scid_from_parts(0xffffff, 0xffffff, 0xffff).unwrap(), // never
2154                 ];
2155
2156                 // used for testing multipart reply across blocks
2157                 for block in 100000..=108001 {
2158                         scids.push(scid_from_parts(block, 0, 0).unwrap());
2159                 }
2160
2161                 // used for testing resumption on same block
2162                 scids.push(scid_from_parts(108001, 1, 0).unwrap());
2163
2164                 for scid in scids {
2165                         let unsigned_announcement = UnsignedChannelAnnouncement {
2166                                 features: ChannelFeatures::known(),
2167                                 chain_hash: chain_hash.clone(),
2168                                 short_channel_id: scid,
2169                                 node_id_1,
2170                                 node_id_2,
2171                                 bitcoin_key_1,
2172                                 bitcoin_key_2,
2173                                 excess_data: Vec::new(),
2174                         };
2175
2176                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2177                         let valid_announcement = ChannelAnnouncement {
2178                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2179                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2180                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2181                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2182                                 contents: unsigned_announcement.clone(),
2183                         };
2184                         match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
2185                                 Ok(_) => (),
2186                                 _ => panic!()
2187                         };
2188                 }
2189
2190                 // Error when number_of_blocks=0
2191                 do_handling_query_channel_range(
2192                         &net_graph_msg_handler,
2193                         &node_id_2,
2194                         QueryChannelRange {
2195                                 chain_hash: chain_hash.clone(),
2196                                 first_blocknum: 0,
2197                                 number_of_blocks: 0,
2198                         },
2199                         false,
2200                         vec![ReplyChannelRange {
2201                                 chain_hash: chain_hash.clone(),
2202                                 first_blocknum: 0,
2203                                 number_of_blocks: 0,
2204                                 sync_complete: true,
2205                                 short_channel_ids: vec![]
2206                         }]
2207                 );
2208
2209                 // Error when wrong chain
2210                 do_handling_query_channel_range(
2211                         &net_graph_msg_handler,
2212                         &node_id_2,
2213                         QueryChannelRange {
2214                                 chain_hash: genesis_block(Network::Bitcoin).header.block_hash(),
2215                                 first_blocknum: 0,
2216                                 number_of_blocks: 0xffff_ffff,
2217                         },
2218                         false,
2219                         vec![ReplyChannelRange {
2220                                 chain_hash: genesis_block(Network::Bitcoin).header.block_hash(),
2221                                 first_blocknum: 0,
2222                                 number_of_blocks: 0xffff_ffff,
2223                                 sync_complete: true,
2224                                 short_channel_ids: vec![],
2225                         }]
2226                 );
2227
2228                 // Error when first_blocknum > 0xffffff
2229                 do_handling_query_channel_range(
2230                         &net_graph_msg_handler,
2231                         &node_id_2,
2232                         QueryChannelRange {
2233                                 chain_hash: chain_hash.clone(),
2234                                 first_blocknum: 0x01000000,
2235                                 number_of_blocks: 0xffff_ffff,
2236                         },
2237                         false,
2238                         vec![ReplyChannelRange {
2239                                 chain_hash: chain_hash.clone(),
2240                                 first_blocknum: 0x01000000,
2241                                 number_of_blocks: 0xffff_ffff,
2242                                 sync_complete: true,
2243                                 short_channel_ids: vec![]
2244                         }]
2245                 );
2246
2247                 // Empty reply when max valid SCID block num
2248                 do_handling_query_channel_range(
2249                         &net_graph_msg_handler,
2250                         &node_id_2,
2251                         QueryChannelRange {
2252                                 chain_hash: chain_hash.clone(),
2253                                 first_blocknum: 0xffffff,
2254                                 number_of_blocks: 1,
2255                         },
2256                         true,
2257                         vec![
2258                                 ReplyChannelRange {
2259                                         chain_hash: chain_hash.clone(),
2260                                         first_blocknum: 0xffffff,
2261                                         number_of_blocks: 1,
2262                                         sync_complete: true,
2263                                         short_channel_ids: vec![]
2264                                 },
2265                         ]
2266                 );
2267
2268                 // No results in valid query range
2269                 do_handling_query_channel_range(
2270                         &net_graph_msg_handler,
2271                         &node_id_2,
2272                         QueryChannelRange {
2273                                 chain_hash: chain_hash.clone(),
2274                                 first_blocknum: 1000,
2275                                 number_of_blocks: 1000,
2276                         },
2277                         true,
2278                         vec![
2279                                 ReplyChannelRange {
2280                                         chain_hash: chain_hash.clone(),
2281                                         first_blocknum: 1000,
2282                                         number_of_blocks: 1000,
2283                                         sync_complete: true,
2284                                         short_channel_ids: vec![],
2285                                 }
2286                         ]
2287                 );
2288
2289                 // Overflow first_blocknum + number_of_blocks
2290                 do_handling_query_channel_range(
2291                         &net_graph_msg_handler,
2292                         &node_id_2,
2293                         QueryChannelRange {
2294                                 chain_hash: chain_hash.clone(),
2295                                 first_blocknum: 0xfe0000,
2296                                 number_of_blocks: 0xffffffff,
2297                         },
2298                         true,
2299                         vec![
2300                                 ReplyChannelRange {
2301                                         chain_hash: chain_hash.clone(),
2302                                         first_blocknum: 0xfe0000,
2303                                         number_of_blocks: 0xffffffff - 0xfe0000,
2304                                         sync_complete: true,
2305                                         short_channel_ids: vec![
2306                                                 0xfffffe_ffffff_ffff, // max
2307                                         ]
2308                                 }
2309                         ]
2310                 );
2311
2312                 // Single block exactly full
2313                 do_handling_query_channel_range(
2314                         &net_graph_msg_handler,
2315                         &node_id_2,
2316                         QueryChannelRange {
2317                                 chain_hash: chain_hash.clone(),
2318                                 first_blocknum: 100000,
2319                                 number_of_blocks: 8000,
2320                         },
2321                         true,
2322                         vec![
2323                                 ReplyChannelRange {
2324                                         chain_hash: chain_hash.clone(),
2325                                         first_blocknum: 100000,
2326                                         number_of_blocks: 8000,
2327                                         sync_complete: true,
2328                                         short_channel_ids: (100000..=107999)
2329                                                 .map(|block| scid_from_parts(block, 0, 0).unwrap())
2330                                                 .collect(),
2331                                 },
2332                         ]
2333                 );
2334
2335                 // Multiple split on new block
2336                 do_handling_query_channel_range(
2337                         &net_graph_msg_handler,
2338                         &node_id_2,
2339                         QueryChannelRange {
2340                                 chain_hash: chain_hash.clone(),
2341                                 first_blocknum: 100000,
2342                                 number_of_blocks: 8001,
2343                         },
2344                         true,
2345                         vec![
2346                                 ReplyChannelRange {
2347                                         chain_hash: chain_hash.clone(),
2348                                         first_blocknum: 100000,
2349                                         number_of_blocks: 7999,
2350                                         sync_complete: false,
2351                                         short_channel_ids: (100000..=107999)
2352                                                 .map(|block| scid_from_parts(block, 0, 0).unwrap())
2353                                                 .collect(),
2354                                 },
2355                                 ReplyChannelRange {
2356                                         chain_hash: chain_hash.clone(),
2357                                         first_blocknum: 107999,
2358                                         number_of_blocks: 2,
2359                                         sync_complete: true,
2360                                         short_channel_ids: vec![
2361                                                 scid_from_parts(108000, 0, 0).unwrap(),
2362                                         ],
2363                                 }
2364                         ]
2365                 );
2366
2367                 // Multiple split on same block
2368                 do_handling_query_channel_range(
2369                         &net_graph_msg_handler,
2370                         &node_id_2,
2371                         QueryChannelRange {
2372                                 chain_hash: chain_hash.clone(),
2373                                 first_blocknum: 100002,
2374                                 number_of_blocks: 8000,
2375                         },
2376                         true,
2377                         vec![
2378                                 ReplyChannelRange {
2379                                         chain_hash: chain_hash.clone(),
2380                                         first_blocknum: 100002,
2381                                         number_of_blocks: 7999,
2382                                         sync_complete: false,
2383                                         short_channel_ids: (100002..=108001)
2384                                                 .map(|block| scid_from_parts(block, 0, 0).unwrap())
2385                                                 .collect(),
2386                                 },
2387                                 ReplyChannelRange {
2388                                         chain_hash: chain_hash.clone(),
2389                                         first_blocknum: 108001,
2390                                         number_of_blocks: 1,
2391                                         sync_complete: true,
2392                                         short_channel_ids: vec![
2393                                                 scid_from_parts(108001, 1, 0).unwrap(),
2394                                         ],
2395                                 }
2396                         ]
2397                 );
2398         }
2399
2400         fn do_handling_query_channel_range(
2401                 net_graph_msg_handler: &NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
2402                 test_node_id: &PublicKey,
2403                 msg: QueryChannelRange,
2404                 expected_ok: bool,
2405                 expected_replies: Vec<ReplyChannelRange>
2406         ) {
2407                 let mut max_firstblocknum = msg.first_blocknum.saturating_sub(1);
2408                 let mut c_lightning_0_9_prev_end_blocknum = max_firstblocknum;
2409                 let query_end_blocknum = msg.end_blocknum();
2410                 let result = net_graph_msg_handler.handle_query_channel_range(test_node_id, msg);
2411
2412                 if expected_ok {
2413                         assert!(result.is_ok());
2414                 } else {
2415                         assert!(result.is_err());
2416                 }
2417
2418                 let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2419                 assert_eq!(events.len(), expected_replies.len());
2420
2421                 for i in 0..events.len() {
2422                         let expected_reply = &expected_replies[i];
2423                         match &events[i] {
2424                                 MessageSendEvent::SendReplyChannelRange { node_id, msg } => {
2425                                         assert_eq!(node_id, test_node_id);
2426                                         assert_eq!(msg.chain_hash, expected_reply.chain_hash);
2427                                         assert_eq!(msg.first_blocknum, expected_reply.first_blocknum);
2428                                         assert_eq!(msg.number_of_blocks, expected_reply.number_of_blocks);
2429                                         assert_eq!(msg.sync_complete, expected_reply.sync_complete);
2430                                         assert_eq!(msg.short_channel_ids, expected_reply.short_channel_ids);
2431
2432                                         // Enforce exactly the sequencing requirements present on c-lightning v0.9.3
2433                                         assert!(msg.first_blocknum == c_lightning_0_9_prev_end_blocknum || msg.first_blocknum == c_lightning_0_9_prev_end_blocknum.saturating_add(1));
2434                                         assert!(msg.first_blocknum >= max_firstblocknum);
2435                                         max_firstblocknum = msg.first_blocknum;
2436                                         c_lightning_0_9_prev_end_blocknum = msg.first_blocknum.saturating_add(msg.number_of_blocks);
2437
2438                                         // Check that the last block count is >= the query's end_blocknum
2439                                         if i == events.len() - 1 {
2440                                                 assert!(msg.first_blocknum.saturating_add(msg.number_of_blocks) >= query_end_blocknum);
2441                                         }
2442                                 },
2443                                 _ => panic!("expected MessageSendEvent::SendReplyChannelRange"),
2444                         }
2445                 }
2446         }
2447
2448         #[test]
2449         fn handling_query_short_channel_ids() {
2450                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2451                 let node_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2452                 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
2453
2454                 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2455
2456                 let result = net_graph_msg_handler.handle_query_short_channel_ids(&node_id, QueryShortChannelIds {
2457                         chain_hash,
2458                         short_channel_ids: vec![0x0003e8_000000_0000],
2459                 });
2460                 assert!(result.is_err());
2461         }
2462 }
2463
2464 #[cfg(all(test, feature = "unstable"))]
2465 mod benches {
2466         use super::*;
2467
2468         use test::Bencher;
2469         use std::io::Read;
2470
2471         #[bench]
2472         fn read_network_graph(bench: &mut Bencher) {
2473                 let mut d = ::routing::router::test_utils::get_route_file().unwrap();
2474                 let mut v = Vec::new();
2475                 d.read_to_end(&mut v).unwrap();
2476                 bench.iter(|| {
2477                         let _ = NetworkGraph::read(&mut std::io::Cursor::new(&v)).unwrap();
2478                 });
2479         }
2480
2481         #[bench]
2482         fn write_network_graph(bench: &mut Bencher) {
2483                 let mut d = ::routing::router::test_utils::get_route_file().unwrap();
2484                 let net_graph = NetworkGraph::read(&mut d).unwrap();
2485                 bench.iter(|| {
2486                         let _ = net_graph.encode();
2487                 });
2488         }
2489 }