Add tests for getting node announcements
[rust-lightning] / lightning / src / ln / router.rs
1 //! The top-level routing/network map tracking logic lives here.
2 //!
3 //! You probably want to create a Router and use that as your RoutingMessageHandler and then
4 //! interrogate it to get routes for your own payments.
5
6 use secp256k1::key::PublicKey;
7 use secp256k1::Secp256k1;
8 use secp256k1;
9
10 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
11 use bitcoin_hashes::Hash;
12 use bitcoin::blockdata::script::Builder;
13 use bitcoin::blockdata::opcodes;
14
15 use chain::chaininterface::{ChainError, ChainWatchInterface};
16 use ln::channelmanager;
17 use ln::features::{ChannelFeatures, NodeFeatures};
18 use ln::msgs::{DecodeError,ErrorAction,LightningError,RoutingMessageHandler,NetAddress};
19 use ln::msgs;
20 use util::ser::{Writeable, Readable, Writer, ReadableArgs};
21 use util::logger::Logger;
22
23 use std::cmp;
24 use std::sync::{RwLock,Arc};
25 use std::sync::atomic::{AtomicUsize, Ordering};
26 use std::collections::{HashMap,BinaryHeap,BTreeMap};
27 use std::collections::btree_map::Entry as BtreeEntry;
28 use std;
29
30 /// A hop in a route
31 #[derive(Clone, PartialEq)]
32 pub struct RouteHop {
33         /// The node_id of the node at this hop.
34         pub pubkey: PublicKey,
35         /// The node_announcement features of the node at this hop. For the last hop, these may be
36         /// amended to match the features present in the invoice this node generated.
37         pub node_features: NodeFeatures,
38         /// The channel that should be used from the previous hop to reach this node.
39         pub short_channel_id: u64,
40         /// The channel_announcement features of the channel that should be used from the previous hop
41         /// to reach this node.
42         pub channel_features: ChannelFeatures,
43         /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
44         pub fee_msat: u64,
45         /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
46         /// expected at the destination, in excess of the current block height.
47         pub cltv_expiry_delta: u32,
48 }
49
50 /// A route from us through the network to a destination
51 #[derive(Clone, PartialEq)]
52 pub struct Route {
53         /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
54         /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
55         /// length.
56         pub hops: Vec<RouteHop>,
57 }
58
59 impl Writeable for Route {
60         fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
61                 (self.hops.len() as u8).write(writer)?;
62                 for hop in self.hops.iter() {
63                         hop.pubkey.write(writer)?;
64                         hop.node_features.write(writer)?;
65                         hop.short_channel_id.write(writer)?;
66                         hop.channel_features.write(writer)?;
67                         hop.fee_msat.write(writer)?;
68                         hop.cltv_expiry_delta.write(writer)?;
69                 }
70                 Ok(())
71         }
72 }
73
74 impl Readable for Route {
75         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Route, DecodeError> {
76                 let hops_count: u8 = Readable::read(reader)?;
77                 let mut hops = Vec::with_capacity(hops_count as usize);
78                 for _ in 0..hops_count {
79                         hops.push(RouteHop {
80                                 pubkey: Readable::read(reader)?,
81                                 node_features: Readable::read(reader)?,
82                                 short_channel_id: Readable::read(reader)?,
83                                 channel_features: Readable::read(reader)?,
84                                 fee_msat: Readable::read(reader)?,
85                                 cltv_expiry_delta: Readable::read(reader)?,
86                         });
87                 }
88                 Ok(Route {
89                         hops
90                 })
91         }
92 }
93
94 #[derive(PartialEq)]
95 struct DirectionalChannelInfo {
96         src_node_id: PublicKey,
97         last_update: u32,
98         enabled: bool,
99         cltv_expiry_delta: u16,
100         htlc_minimum_msat: u64,
101         fee_base_msat: u32,
102         fee_proportional_millionths: u32,
103         last_update_message: Option<msgs::ChannelUpdate>,
104 }
105
106 impl std::fmt::Display for DirectionalChannelInfo {
107         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
108                 write!(f, "src_node_id {}, last_update {}, enabled {}, cltv_expiry_delta {}, htlc_minimum_msat {}, fee_base_msat {}, fee_proportional_millionths {}", log_pubkey!(self.src_node_id), self.last_update, self.enabled, self.cltv_expiry_delta, self.htlc_minimum_msat, self.fee_base_msat, self.fee_proportional_millionths)?;
109                 Ok(())
110         }
111 }
112
113 impl_writeable!(DirectionalChannelInfo, 0, {
114         src_node_id,
115         last_update,
116         enabled,
117         cltv_expiry_delta,
118         htlc_minimum_msat,
119         fee_base_msat,
120         fee_proportional_millionths,
121         last_update_message
122 });
123
124 #[derive(PartialEq)]
125 struct ChannelInfo {
126         features: ChannelFeatures,
127         one_to_two: DirectionalChannelInfo,
128         two_to_one: DirectionalChannelInfo,
129         //this is cached here so we can send out it later if required by route_init_sync
130         //keep an eye on this to see if the extra memory is a problem
131         announcement_message: Option<msgs::ChannelAnnouncement>,
132 }
133
134 impl std::fmt::Display for ChannelInfo {
135         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
136                 write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?;
137                 Ok(())
138         }
139 }
140
141 impl_writeable!(ChannelInfo, 0, {
142         features,
143         one_to_two,
144         two_to_one,
145         announcement_message
146 });
147
148 #[derive(PartialEq)]
149 struct NodeInfo {
150         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
151         channels: Vec<(u64, Sha256dHash)>,
152         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
153         channels: Vec<u64>,
154
155         lowest_inbound_channel_fee_base_msat: u32,
156         lowest_inbound_channel_fee_proportional_millionths: u32,
157
158         features: NodeFeatures,
159         /// Unlike for channels, we may have a NodeInfo entry before having received a node_update.
160         /// Thus, we have to be able to capture "no update has been received", which we do with an
161         /// Option here.
162         last_update: Option<u32>,
163         rgb: [u8; 3],
164         alias: [u8; 32],
165         addresses: Vec<NetAddress>,
166         //this is cached here so we can send out it later if required by route_init_sync
167         //keep an eye on this to see if the extra memory is a problem
168         announcement_message: Option<msgs::NodeAnnouncement>,
169 }
170
171 impl std::fmt::Display for NodeInfo {
172         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
173                 write!(f, "features: {}, last_update: {:?}, lowest_inbound_channel_fee_base_msat: {}, lowest_inbound_channel_fee_proportional_millionths: {}, channels: {:?}", log_bytes!(self.features.encode()), self.last_update, self.lowest_inbound_channel_fee_base_msat, self.lowest_inbound_channel_fee_proportional_millionths, &self.channels[..])?;
174                 Ok(())
175         }
176 }
177
178 impl Writeable for NodeInfo {
179         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
180                 (self.channels.len() as u64).write(writer)?;
181                 for ref chan in self.channels.iter() {
182                         chan.write(writer)?;
183                 }
184                 self.lowest_inbound_channel_fee_base_msat.write(writer)?;
185                 self.lowest_inbound_channel_fee_proportional_millionths.write(writer)?;
186                 self.features.write(writer)?;
187                 self.last_update.write(writer)?;
188                 self.rgb.write(writer)?;
189                 self.alias.write(writer)?;
190                 (self.addresses.len() as u64).write(writer)?;
191                 for ref addr in &self.addresses {
192                         addr.write(writer)?;
193                 }
194                 self.announcement_message.write(writer)?;
195                 Ok(())
196         }
197 }
198
199 const MAX_ALLOC_SIZE: u64 = 64*1024;
200
201 impl Readable for NodeInfo {
202         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeInfo, DecodeError> {
203                 let channels_count: u64 = Readable::read(reader)?;
204                 let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
205                 for _ in 0..channels_count {
206                         channels.push(Readable::read(reader)?);
207                 }
208                 let lowest_inbound_channel_fee_base_msat = Readable::read(reader)?;
209                 let lowest_inbound_channel_fee_proportional_millionths = Readable::read(reader)?;
210                 let features = Readable::read(reader)?;
211                 let last_update = Readable::read(reader)?;
212                 let rgb = Readable::read(reader)?;
213                 let alias = Readable::read(reader)?;
214                 let addresses_count: u64 = Readable::read(reader)?;
215                 let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
216                 for _ in 0..addresses_count {
217                         match Readable::read(reader) {
218                                 Ok(Ok(addr)) => { addresses.push(addr); },
219                                 Ok(Err(_)) => return Err(DecodeError::InvalidValue),
220                                 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
221                                 _ => unreachable!(),
222                         }
223                 }
224                 let announcement_message = Readable::read(reader)?;
225                 Ok(NodeInfo {
226                         channels,
227                         lowest_inbound_channel_fee_base_msat,
228                         lowest_inbound_channel_fee_proportional_millionths,
229                         features,
230                         last_update,
231                         rgb,
232                         alias,
233                         addresses,
234                         announcement_message
235                 })
236         }
237 }
238
239 #[derive(PartialEq)]
240 struct NetworkMap {
241         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
242         channels: BTreeMap<(u64, Sha256dHash), ChannelInfo>,
243         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
244         channels: BTreeMap<u64, ChannelInfo>,
245
246         our_node_id: PublicKey,
247         nodes: BTreeMap<PublicKey, NodeInfo>,
248 }
249
250 impl Writeable for NetworkMap {
251         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
252                 (self.channels.len() as u64).write(writer)?;
253                 for (ref chan_id, ref chan_info) in self.channels.iter() {
254                         (*chan_id).write(writer)?;
255                         chan_info.write(writer)?;
256                 }
257                 self.our_node_id.write(writer)?;
258                 (self.nodes.len() as u64).write(writer)?;
259                 for (ref node_id, ref node_info) in self.nodes.iter() {
260                         node_id.write(writer)?;
261                         node_info.write(writer)?;
262                 }
263                 Ok(())
264         }
265 }
266
267 impl Readable for NetworkMap {
268         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NetworkMap, DecodeError> {
269                 let channels_count: u64 = Readable::read(reader)?;
270                 let mut channels = BTreeMap::new();
271                 for _ in 0..channels_count {
272                         let chan_id: u64 = Readable::read(reader)?;
273                         let chan_info = Readable::read(reader)?;
274                         channels.insert(chan_id, chan_info);
275                 }
276                 let our_node_id = Readable::read(reader)?;
277                 let nodes_count: u64 = Readable::read(reader)?;
278                 let mut nodes = BTreeMap::new();
279                 for _ in 0..nodes_count {
280                         let node_id = Readable::read(reader)?;
281                         let node_info = Readable::read(reader)?;
282                         nodes.insert(node_id, node_info);
283                 }
284                 Ok(NetworkMap {
285                         channels,
286                         our_node_id,
287                         nodes,
288                 })
289         }
290 }
291
292 impl std::fmt::Display for NetworkMap {
293         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
294                 write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
295                 for (key, val) in self.channels.iter() {
296                         write!(f, " {}: {}\n", key, val)?;
297                 }
298                 write!(f, "[Nodes]\n")?;
299                 for (key, val) in self.nodes.iter() {
300                         write!(f, " {}: {}\n", log_pubkey!(key), val)?;
301                 }
302                 Ok(())
303         }
304 }
305
306 impl NetworkMap {
307         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
308         #[inline]
309         fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
310                 (short_channel_id, chain_hash)
311         }
312
313         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
314         #[inline]
315         fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
316                 short_channel_id
317         }
318
319         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
320         #[inline]
321         fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
322                 &id.0
323         }
324
325         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
326         #[inline]
327         fn get_short_id(id: &u64) -> &u64 {
328                 id
329         }
330 }
331
332 /// A channel descriptor which provides a last-hop route to get_route
333 pub struct RouteHint {
334         /// The node_id of the non-target end of the route
335         pub src_node_id: PublicKey,
336         /// The short_channel_id of this channel
337         pub short_channel_id: u64,
338         /// The static msat-denominated fee which must be paid to use this channel
339         pub fee_base_msat: u32,
340         /// The dynamic proportional fee which must be paid to use this channel, denominated in
341         /// millionths of the value being forwarded to the next hop.
342         pub fee_proportional_millionths: u32,
343         /// The difference in CLTV values between this node and the next node.
344         pub cltv_expiry_delta: u16,
345         /// The minimum value, in msat, which must be relayed to the next hop.
346         pub htlc_minimum_msat: u64,
347 }
348
349 /// Tracks a view of the network, receiving updates from peers and generating Routes to
350 /// payment destinations.
351 pub struct Router {
352         secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
353         network_map: RwLock<NetworkMap>,
354         full_syncs_requested: AtomicUsize,
355         chain_monitor: Arc<ChainWatchInterface>,
356         logger: Arc<Logger>,
357 }
358
359 const SERIALIZATION_VERSION: u8 = 1;
360 const MIN_SERIALIZATION_VERSION: u8 = 1;
361
362 impl Writeable for Router {
363         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
364                 writer.write_all(&[SERIALIZATION_VERSION; 1])?;
365                 writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
366
367                 let network = self.network_map.read().unwrap();
368                 network.write(writer)?;
369                 Ok(())
370         }
371 }
372
373 /// Arguments for the creation of a Router that are not deserialized.
374 /// At a high-level, the process for deserializing a Router and resuming normal operation is:
375 /// 1) Deserialize the Router by filling in this struct and calling <Router>::read(reaser, args).
376 /// 2) Register the new Router with your ChainWatchInterface
377 pub struct RouterReadArgs {
378         /// The ChainWatchInterface for use in the Router in the future.
379         ///
380         /// No calls to the ChainWatchInterface will be made during deserialization.
381         pub chain_monitor: Arc<ChainWatchInterface>,
382         /// The Logger for use in the ChannelManager and which may be used to log information during
383         /// deserialization.
384         pub logger: Arc<Logger>,
385 }
386
387 impl ReadableArgs<RouterReadArgs> for Router {
388         fn read<R: ::std::io::Read>(reader: &mut R, args: RouterReadArgs) -> Result<Router, DecodeError> {
389                 let _ver: u8 = Readable::read(reader)?;
390                 let min_ver: u8 = Readable::read(reader)?;
391                 if min_ver > SERIALIZATION_VERSION {
392                         return Err(DecodeError::UnknownVersion);
393                 }
394                 let network_map = Readable::read(reader)?;
395                 Ok(Router {
396                         secp_ctx: Secp256k1::verification_only(),
397                         network_map: RwLock::new(network_map),
398                         full_syncs_requested: AtomicUsize::new(0),
399                         chain_monitor: args.chain_monitor,
400                         logger: args.logger,
401                 })
402         }
403 }
404
405 macro_rules! secp_verify_sig {
406         ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
407                 match $secp_ctx.verify($msg, $sig, $pubkey) {
408                         Ok(_) => {},
409                         Err(_) => return Err(LightningError{err: "Invalid signature from remote node", action: ErrorAction::IgnoreError}),
410                 }
411         };
412 }
413
414 impl RoutingMessageHandler for Router {
415
416         fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
417                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
418                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
419
420                 let mut network = self.network_map.write().unwrap();
421                 match network.nodes.get_mut(&msg.contents.node_id) {
422                         None => Err(LightningError{err: "No existing channels for node_announcement", action: ErrorAction::IgnoreError}),
423                         Some(node) => {
424                                 match node.last_update {
425                                         Some(last_update) => if last_update >= msg.contents.timestamp {
426                                                 return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
427                                         },
428                                         None => {},
429                                 }
430
431                                 node.features = msg.contents.features.clone();
432                                 node.last_update = Some(msg.contents.timestamp);
433                                 node.rgb = msg.contents.rgb;
434                                 node.alias = msg.contents.alias;
435                                 node.addresses = msg.contents.addresses.clone();
436
437                                 let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty();
438                                 node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
439                                 Ok(should_relay)
440                         }
441                 }
442         }
443
444         fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
445                 if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
446                         return Err(LightningError{err: "Channel announcement node had a channel with itself", action: ErrorAction::IgnoreError});
447                 }
448
449                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
450                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
451                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
452                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
453                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
454
455                 let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
456                         Ok((script_pubkey, _value)) => {
457                                 let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
458                                                                     .push_slice(&msg.contents.bitcoin_key_1.serialize())
459                                                                     .push_slice(&msg.contents.bitcoin_key_2.serialize())
460                                                                     .push_opcode(opcodes::all::OP_PUSHNUM_2)
461                                                                     .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
462                                 if script_pubkey != expected_script {
463                                         return Err(LightningError{err: "Channel announcement keys didn't match on-chain script", action: ErrorAction::IgnoreError});
464                                 }
465                                 //TODO: Check if value is worth storing, use it to inform routing, and compare it
466                                 //to the new HTLC max field in channel_update
467                                 true
468                         },
469                         Err(ChainError::NotSupported) => {
470                                 // Tentatively accept, potentially exposing us to DoS attacks
471                                 false
472                         },
473                         Err(ChainError::NotWatched) => {
474                                 return Err(LightningError{err: "Channel announced on an unknown chain", action: ErrorAction::IgnoreError});
475                         },
476                         Err(ChainError::UnknownTx) => {
477                                 return Err(LightningError{err: "Channel announced without corresponding UTXO entry", action: ErrorAction::IgnoreError});
478                         },
479                 };
480
481                 let mut network_lock = self.network_map.write().unwrap();
482                 let network = &mut *network_lock;
483
484                 let should_relay = msg.contents.excess_data.is_empty();
485
486                 let chan_info = ChannelInfo {
487                                 features: msg.contents.features.clone(),
488                                 one_to_two: DirectionalChannelInfo {
489                                         src_node_id: msg.contents.node_id_1.clone(),
490                                         last_update: 0,
491                                         enabled: false,
492                                         cltv_expiry_delta: u16::max_value(),
493                                         htlc_minimum_msat: u64::max_value(),
494                                         fee_base_msat: u32::max_value(),
495                                         fee_proportional_millionths: u32::max_value(),
496                                         last_update_message: None,
497                                 },
498                                 two_to_one: DirectionalChannelInfo {
499                                         src_node_id: msg.contents.node_id_2.clone(),
500                                         last_update: 0,
501                                         enabled: false,
502                                         cltv_expiry_delta: u16::max_value(),
503                                         htlc_minimum_msat: u64::max_value(),
504                                         fee_base_msat: u32::max_value(),
505                                         fee_proportional_millionths: u32::max_value(),
506                                         last_update_message: None,
507                                 },
508                                 announcement_message: if should_relay { Some(msg.clone()) } else { None },
509                         };
510
511                 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
512                         BtreeEntry::Occupied(mut entry) => {
513                                 //TODO: because asking the blockchain if short_channel_id is valid is only optional
514                                 //in the blockchain API, we need to handle it smartly here, though it's unclear
515                                 //exactly how...
516                                 if checked_utxo {
517                                         // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
518                                         // only sometimes returns results. In any case remove the previous entry. Note
519                                         // that the spec expects us to "blacklist" the node_ids involved, but we can't
520                                         // do that because
521                                         // a) we don't *require* a UTXO provider that always returns results.
522                                         // b) we don't track UTXOs of channels we know about and remove them if they
523                                         //    get reorg'd out.
524                                         // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
525                                         Self::remove_channel_in_nodes(&mut network.nodes, &entry.get(), msg.contents.short_channel_id);
526                                         *entry.get_mut() = chan_info;
527                                 } else {
528                                         return Err(LightningError{err: "Already have knowledge of channel", action: ErrorAction::IgnoreError})
529                                 }
530                         },
531                         BtreeEntry::Vacant(entry) => {
532                                 entry.insert(chan_info);
533                         }
534                 };
535
536                 macro_rules! add_channel_to_node {
537                         ( $node_id: expr ) => {
538                                 match network.nodes.entry($node_id) {
539                                         BtreeEntry::Occupied(node_entry) => {
540                                                 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
541                                         },
542                                         BtreeEntry::Vacant(node_entry) => {
543                                                 node_entry.insert(NodeInfo {
544                                                         channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
545                                                         lowest_inbound_channel_fee_base_msat: u32::max_value(),
546                                                         lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
547                                                         features: NodeFeatures::empty(),
548                                                         last_update: None,
549                                                         rgb: [0; 3],
550                                                         alias: [0; 32],
551                                                         addresses: Vec::new(),
552                                                         announcement_message: None,
553                                                 });
554                                         }
555                                 }
556                         };
557                 }
558
559                 add_channel_to_node!(msg.contents.node_id_1);
560                 add_channel_to_node!(msg.contents.node_id_2);
561
562                 log_trace!(self, "Added channel_announcement for {}{}", msg.contents.short_channel_id, if !should_relay { " with excess uninterpreted data!" } else { "" });
563                 Ok(should_relay)
564         }
565
566         fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
567                 match update {
568                         &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
569                                 let _ = self.handle_channel_update(msg);
570                         },
571                         &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
572                                 let mut network = self.network_map.write().unwrap();
573                                 if *is_permanent {
574                                         if let Some(chan) = network.channels.remove(short_channel_id) {
575                                                 Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
576                                         }
577                                 } else {
578                                         if let Some(chan) = network.channels.get_mut(short_channel_id) {
579                                                 chan.one_to_two.enabled = false;
580                                                 chan.two_to_one.enabled = false;
581                                         }
582                                 }
583                         },
584                         &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
585                                 if *is_permanent {
586                                         //TODO: Wholly remove the node
587                                 } else {
588                                         self.mark_node_bad(node_id, false);
589                                 }
590                         },
591                 }
592         }
593
594         fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> {
595                 let mut network = self.network_map.write().unwrap();
596                 let dest_node_id;
597                 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
598                 let chan_was_enabled;
599
600                 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
601                         None => return Err(LightningError{err: "Couldn't find channel for update", action: ErrorAction::IgnoreError}),
602                         Some(channel) => {
603                                 macro_rules! maybe_update_channel_info {
604                                         ( $target: expr) => {
605                                                 if $target.last_update >= msg.contents.timestamp {
606                                                         return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
607                                                 }
608                                                 chan_was_enabled = $target.enabled;
609                                                 $target.last_update = msg.contents.timestamp;
610                                                 $target.enabled = chan_enabled;
611                                                 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
612                                                 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
613                                                 $target.fee_base_msat = msg.contents.fee_base_msat;
614                                                 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
615                                                 $target.last_update_message = if msg.contents.excess_data.is_empty() {
616                                                         Some(msg.clone())
617                                                 } else {
618                                                         None
619                                                 };
620                                         }
621                                 }
622                                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
623                                 if msg.contents.flags & 1 == 1 {
624                                         dest_node_id = channel.one_to_two.src_node_id.clone();
625                                         secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
626                                         maybe_update_channel_info!(channel.two_to_one);
627                                 } else {
628                                         dest_node_id = channel.two_to_one.src_node_id.clone();
629                                         secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
630                                         maybe_update_channel_info!(channel.one_to_two);
631                                 }
632                         }
633                 }
634
635                 if chan_enabled {
636                         let node = network.nodes.get_mut(&dest_node_id).unwrap();
637                         node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
638                         node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
639                 } else if chan_was_enabled {
640                         let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
641                         let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
642
643                         {
644                                 let node = network.nodes.get(&dest_node_id).unwrap();
645
646                                 for chan_id in node.channels.iter() {
647                                         let chan = network.channels.get(chan_id).unwrap();
648                                         if chan.one_to_two.src_node_id == dest_node_id {
649                                                 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
650                                                 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
651                                         } else {
652                                                 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
653                                                 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
654                                         }
655                                 }
656                         }
657
658                         //TODO: satisfy the borrow-checker without a double-map-lookup :(
659                         let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
660                         mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
661                         mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
662                 }
663
664                 Ok(msg.contents.excess_data.is_empty())
665         }
666
667         fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, Option<msgs::ChannelUpdate>, Option<msgs::ChannelUpdate>)> {
668                 let mut result = Vec::with_capacity(batch_amount as usize);
669                 let network = self.network_map.read().unwrap();
670                 let mut iter = network.channels.range(starting_point..);
671                 while result.len() < batch_amount as usize {
672                         if let Some((_, ref chan)) = iter.next() {
673                                 if chan.announcement_message.is_some() {
674                                         result.push((chan.announcement_message.clone().unwrap(),
675                                                 chan.one_to_two.last_update_message.clone(),
676                                                 chan.two_to_one.last_update_message.clone()));
677                                 } else {
678                                         // TODO: We may end up sending un-announced channel_updates if we are sending
679                                         // initial sync data while receiving announce/updates for this channel.
680                                 }
681                         } else {
682                                 return result;
683                         }
684                 }
685                 result
686         }
687
688         fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
689                 let mut result = Vec::with_capacity(batch_amount as usize);
690                 let network = self.network_map.read().unwrap();
691                 let mut iter = if let Some(pubkey) = starting_point {
692                                 let mut iter = network.nodes.range((*pubkey)..);
693                                 iter.next();
694                                 iter
695                         } else {
696                                 network.nodes.range(..)
697                         };
698                 while result.len() < batch_amount as usize {
699                         if let Some((_, ref node)) = iter.next() {
700                                 if node.announcement_message.is_some() {
701                                         result.push(node.announcement_message.clone().unwrap());
702                                 }
703                         } else {
704                                 return result;
705                         }
706                 }
707                 result
708         }
709
710         fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
711                 //TODO: Determine whether to request a full sync based on the network map.
712                 const FULL_SYNCS_TO_REQUEST: usize = 5;
713                 if self.full_syncs_requested.load(Ordering::Acquire) < FULL_SYNCS_TO_REQUEST {
714                         self.full_syncs_requested.fetch_add(1, Ordering::AcqRel);
715                         true
716                 } else {
717                         false
718                 }
719         }
720 }
721
722 #[derive(Eq, PartialEq)]
723 struct RouteGraphNode {
724         pubkey: PublicKey,
725         lowest_fee_to_peer_through_node: u64,
726         lowest_fee_to_node: u64,
727 }
728
729 impl cmp::Ord for RouteGraphNode {
730         fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
731                 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
732                         .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
733         }
734 }
735
736 impl cmp::PartialOrd for RouteGraphNode {
737         fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
738                 Some(self.cmp(other))
739         }
740 }
741
742 struct DummyDirectionalChannelInfo {
743         src_node_id: PublicKey,
744         cltv_expiry_delta: u32,
745         htlc_minimum_msat: u64,
746         fee_base_msat: u32,
747         fee_proportional_millionths: u32,
748 }
749
750 impl Router {
751         /// Creates a new router with the given node_id to be used as the source for get_route()
752         pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
753                 let mut nodes = BTreeMap::new();
754                 nodes.insert(our_pubkey.clone(), NodeInfo {
755                         channels: Vec::new(),
756                         lowest_inbound_channel_fee_base_msat: u32::max_value(),
757                         lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
758                         features: NodeFeatures::empty(),
759                         last_update: None,
760                         rgb: [0; 3],
761                         alias: [0; 32],
762                         addresses: Vec::new(),
763                         announcement_message: None,
764                 });
765                 Router {
766                         secp_ctx: Secp256k1::verification_only(),
767                         network_map: RwLock::new(NetworkMap {
768                                 channels: BTreeMap::new(),
769                                 our_node_id: our_pubkey,
770                                 nodes: nodes,
771                         }),
772                         full_syncs_requested: AtomicUsize::new(0),
773                         chain_monitor,
774                         logger,
775                 }
776         }
777
778         /// Dumps the entire network view of this Router to the logger provided in the constructor at
779         /// level Trace
780         pub fn trace_state(&self) {
781                 log_trace!(self, "{}", self.network_map.read().unwrap());
782         }
783
784         /// Get network addresses by node id
785         pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
786                 let network = self.network_map.read().unwrap();
787                 network.nodes.get(pubkey).map(|n| n.addresses.clone())
788         }
789
790         /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
791         /// with an exponential decay in node "badness". Note that there is deliberately no
792         /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
793         /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
794         /// boolean will reduce the penalty, returning the node to usability faster. If the node is
795         /// behaving correctly, it will disable the failing channel and we will use it again next time.
796         pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
797                 unimplemented!();
798         }
799
800         fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
801                 macro_rules! remove_from_node {
802                         ($node_id: expr) => {
803                                 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
804                                         entry.get_mut().channels.retain(|chan_id| {
805                                                 short_channel_id != *NetworkMap::get_short_id(chan_id)
806                                         });
807                                         if entry.get().channels.is_empty() {
808                                                 entry.remove_entry();
809                                         }
810                                 } else {
811                                         panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
812                                 }
813                         }
814                 }
815                 remove_from_node!(chan.one_to_two.src_node_id);
816                 remove_from_node!(chan.two_to_one.src_node_id);
817         }
818
819         /// Gets a route from us to the given target node.
820         ///
821         /// Extra routing hops between known nodes and the target will be used if they are included in
822         /// last_hops.
823         ///
824         /// If some channels aren't announced, it may be useful to fill in a first_hops with the
825         /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
826         /// (this Router's) view of our local channels will be ignored, and only those in first_hops
827         /// will be used.
828         ///
829         /// Panics if first_hops contains channels without short_channel_ids
830         /// (ChannelManager::list_usable_channels will never include such channels).
831         ///
832         /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
833         /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
834         /// *is* checked as they may change based on the receiving node.
835         pub fn get_route(&self, target: &PublicKey, first_hops: Option<&[channelmanager::ChannelDetails]>, last_hops: &[RouteHint], final_value_msat: u64, final_cltv: u32) -> Result<Route, LightningError> {
836                 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
837                 // uptime/success in using a node in the past.
838                 let network = self.network_map.read().unwrap();
839
840                 if *target == network.our_node_id {
841                         return Err(LightningError{err: "Cannot generate a route to ourselves", action: ErrorAction::IgnoreError});
842                 }
843
844                 if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
845                         return Err(LightningError{err: "Cannot generate a route of more value than all existing satoshis", action: ErrorAction::IgnoreError});
846                 }
847
848                 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
849                 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
850                 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
851                 // to use as the A* heuristic beyond just the cost to get one node further than the current
852                 // one.
853
854                 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
855                         src_node_id: network.our_node_id.clone(),
856                         cltv_expiry_delta: 0,
857                         htlc_minimum_msat: 0,
858                         fee_base_msat: 0,
859                         fee_proportional_millionths: 0,
860                 };
861
862                 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
863                 let mut dist = HashMap::with_capacity(network.nodes.len());
864
865                 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
866                 if let Some(hops) = first_hops {
867                         for chan in hops {
868                                 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
869                                 if chan.remote_network_id == *target {
870                                         return Ok(Route {
871                                                 hops: vec![RouteHop {
872                                                         pubkey: chan.remote_network_id,
873                                                         node_features: NodeFeatures::with_known_relevant_init_flags(&chan.counterparty_features),
874                                                         short_channel_id,
875                                                         channel_features: ChannelFeatures::with_known_relevant_init_flags(&chan.counterparty_features),
876                                                         fee_msat: final_value_msat,
877                                                         cltv_expiry_delta: final_cltv,
878                                                 }],
879                                         });
880                                 }
881                                 first_hop_targets.insert(chan.remote_network_id, (short_channel_id, chan.counterparty_features.clone()));
882                         }
883                         if first_hop_targets.is_empty() {
884                                 return Err(LightningError{err: "Cannot route when there are no outbound routes away from us", action: ErrorAction::IgnoreError});
885                         }
886                 }
887
888                 macro_rules! add_entry {
889                         // Adds entry which goes from the node pointed to by $directional_info to
890                         // $dest_node_id over the channel with id $chan_id with fees described in
891                         // $directional_info.
892                         ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $chan_features: expr, $starting_fee_msat: expr ) => {
893                                 //TODO: Explore simply adding fee to hit htlc_minimum_msat
894                                 if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
895                                         let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
896                                         if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
897                                                         ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
898                                         {
899                                                 let mut total_fee = $starting_fee_msat as u64;
900                                                 let hm_entry = dist.entry(&$directional_info.src_node_id);
901                                                 let old_entry = hm_entry.or_insert_with(|| {
902                                                         let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
903                                                         (u64::max_value(),
904                                                                 node.lowest_inbound_channel_fee_base_msat,
905                                                                 node.lowest_inbound_channel_fee_proportional_millionths,
906                                                                 RouteHop {
907                                                                         pubkey: $dest_node_id.clone(),
908                                                                         node_features: NodeFeatures::empty(),
909                                                                         short_channel_id: 0,
910                                                                         channel_features: $chan_features.clone(),
911                                                                         fee_msat: 0,
912                                                                         cltv_expiry_delta: 0,
913                                                         })
914                                                 });
915                                                 if $directional_info.src_node_id != network.our_node_id {
916                                                         // Ignore new_fee for channel-from-us as we assume all channels-from-us
917                                                         // will have the same effective-fee
918                                                         total_fee += new_fee;
919                                                         if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
920                                                                 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
921                                                         } else {
922                                                                 // max_value means we'll always fail the old_entry.0 > total_fee check
923                                                                 total_fee = u64::max_value();
924                                                         }
925                                                 }
926                                                 let new_graph_node = RouteGraphNode {
927                                                         pubkey: $directional_info.src_node_id,
928                                                         lowest_fee_to_peer_through_node: total_fee,
929                                                         lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
930                                                 };
931                                                 if old_entry.0 > total_fee {
932                                                         targets.push(new_graph_node);
933                                                         old_entry.0 = total_fee;
934                                                         old_entry.3 = RouteHop {
935                                                                 pubkey: $dest_node_id.clone(),
936                                                                 node_features: NodeFeatures::empty(),
937                                                                 short_channel_id: $chan_id.clone(),
938                                                                 channel_features: $chan_features.clone(),
939                                                                 fee_msat: new_fee, // This field is ignored on the last-hop anyway
940                                                                 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
941                                                         }
942                                                 }
943                                         }
944                                 }
945                         };
946                 }
947
948                 macro_rules! add_entries_to_cheapest_to_target_node {
949                         ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
950                                 if first_hops.is_some() {
951                                         if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&$node_id) {
952                                                 add_entry!(first_hop, $node_id, dummy_directional_info, ChannelFeatures::with_known_relevant_init_flags(&features), $fee_to_target_msat);
953                                         }
954                                 }
955
956                                 if !$node.features.requires_unknown_bits() {
957                                         for chan_id in $node.channels.iter() {
958                                                 let chan = network.channels.get(chan_id).unwrap();
959                                                 if !chan.features.requires_unknown_bits() {
960                                                         if chan.one_to_two.src_node_id == *$node_id {
961                                                                 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
962                                                                 if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
963                                                                         if chan.two_to_one.enabled {
964                                                                                 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, chan.features, $fee_to_target_msat);
965                                                                         }
966                                                                 }
967                                                         } else {
968                                                                 if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
969                                                                         if chan.one_to_two.enabled {
970                                                                                 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, chan.features, $fee_to_target_msat);
971                                                                         }
972                                                                 }
973                                                         }
974                                                 }
975                                         }
976                                 }
977                         };
978                 }
979
980                 match network.nodes.get(target) {
981                         None => {},
982                         Some(node) => {
983                                 add_entries_to_cheapest_to_target_node!(node, target, 0);
984                         },
985                 }
986
987                 for hop in last_hops.iter() {
988                         if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
989                                 if network.nodes.get(&hop.src_node_id).is_some() {
990                                         if first_hops.is_some() {
991                                                 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&hop.src_node_id) {
992                                                         // Currently there are no channel-context features defined, so we are a
993                                                         // bit lazy here. In the future, we should pull them out via our
994                                                         // ChannelManager, but there's no reason to waste the space until we
995                                                         // need them.
996                                                         add_entry!(first_hop, hop.src_node_id, dummy_directional_info, ChannelFeatures::with_known_relevant_init_flags(&features), 0);
997                                                 }
998                                         }
999                                         // BOLT 11 doesn't allow inclusion of features for the last hop hints, which
1000                                         // really sucks, cause we're gonna need that eventually.
1001                                         add_entry!(hop.short_channel_id, target, hop, ChannelFeatures::empty(), 0);
1002                                 }
1003                         }
1004                 }
1005
1006                 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
1007                         if pubkey == network.our_node_id {
1008                                 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
1009                                 loop {
1010                                         if let Some(&(_, ref features)) = first_hop_targets.get(&res.last().unwrap().pubkey) {
1011                                                 res.last_mut().unwrap().node_features = NodeFeatures::with_known_relevant_init_flags(&features);
1012                                         } else if let Some(node) = network.nodes.get(&res.last().unwrap().pubkey) {
1013                                                 res.last_mut().unwrap().node_features = node.features.clone();
1014                                         } else {
1015                                                 // We should be able to fill in features for everything except the last
1016                                                 // hop, if the last hop was provided via a BOLT 11 invoice (though we
1017                                                 // should be able to extend it further as BOLT 11 does have feature
1018                                                 // flags for the last hop node itself).
1019                                                 assert!(res.last().unwrap().pubkey == *target);
1020                                         }
1021                                         if res.last().unwrap().pubkey == *target {
1022                                                 break;
1023                                         }
1024
1025                                         let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
1026                                                 Some(hop) => hop.3,
1027                                                 None => return Err(LightningError{err: "Failed to find a non-fee-overflowing path to the given destination", action: ErrorAction::IgnoreError}),
1028                                         };
1029                                         res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
1030                                         res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
1031                                         res.push(new_entry);
1032                                 }
1033                                 res.last_mut().unwrap().fee_msat = final_value_msat;
1034                                 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
1035                                 let route = Route { hops: res };
1036                                 log_trace!(self, "Got route: {}", log_route!(route));
1037                                 return Ok(route);
1038                         }
1039
1040                         match network.nodes.get(&pubkey) {
1041                                 None => {},
1042                                 Some(node) => {
1043                                         add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
1044                                 },
1045                         }
1046                 }
1047
1048                 Err(LightningError{err: "Failed to find a path to the given destination", action: ErrorAction::IgnoreError})
1049         }
1050 }
1051
1052 #[cfg(test)]
1053 mod tests {
1054         use chain::chaininterface;
1055         use ln::channelmanager;
1056         use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
1057         use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
1058         use ln::msgs::{ErrorAction, LightningError, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
1059            UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate};
1060         use util::test_utils;
1061         use util::test_utils::TestVecWriter;
1062         use util::logger::Logger;
1063         use util::ser::{Writeable, Readable};
1064
1065         use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1066         use bitcoin_hashes::Hash;
1067         use bitcoin::network::constants::Network;
1068         use bitcoin::blockdata::constants::genesis_block;
1069         use bitcoin::blockdata::script::Builder;
1070         use bitcoin::blockdata::opcodes;
1071         use bitcoin::util::hash::BitcoinHash;
1072
1073         use hex;
1074
1075         use secp256k1::key::{PublicKey,SecretKey};
1076         use secp256k1::All;
1077         use secp256k1::Secp256k1;
1078
1079         use std::sync::Arc;
1080         use std::collections::btree_map::Entry as BtreeEntry;
1081
1082         fn create_router() -> (Secp256k1<All>, PublicKey, Router) {
1083                 let secp_ctx = Secp256k1::new();
1084                 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
1085                 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1086                 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
1087                 let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
1088                 (secp_ctx, our_id, router)
1089         }
1090
1091         #[test]
1092         fn route_test() {
1093                 let (secp_ctx, our_id, router) = create_router();
1094
1095                 // Build network from our_id to node8:
1096                 //
1097                 //        -1(1)2-  node1  -1(3)2-
1098                 //       /                       \
1099                 // our_id -1(12)2- node8 -1(13)2--- node3
1100                 //       \                       /
1101                 //        -1(2)2-  node2  -1(4)2-
1102                 //
1103                 //
1104                 // chan1  1-to-2: disabled
1105                 // chan1  2-to-1: enabled, 0 fee
1106                 //
1107                 // chan2  1-to-2: enabled, ignored fee
1108                 // chan2  2-to-1: enabled, 0 fee
1109                 //
1110                 // chan3  1-to-2: enabled, 0 fee
1111                 // chan3  2-to-1: enabled, 100 msat fee
1112                 //
1113                 // chan4  1-to-2: enabled, 100% fee
1114                 // chan4  2-to-1: enabled, 0 fee
1115                 //
1116                 // chan12 1-to-2: enabled, ignored fee
1117                 // chan12 2-to-1: enabled, 0 fee
1118                 //
1119                 // chan13 1-to-2: enabled, 200% fee
1120                 // chan13 2-to-1: enabled, 0 fee
1121                 //
1122                 //
1123                 //       -1(5)2- node4 -1(8)2--
1124                 //       |         2          |
1125                 //       |       (11)         |
1126                 //      /          1           \
1127                 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
1128                 //      \                      /
1129                 //       -1(7)2- node6 -1(10)2-
1130                 //
1131                 // chan5  1-to-2: enabled, 100 msat fee
1132                 // chan5  2-to-1: enabled, 0 fee
1133                 //
1134                 // chan6  1-to-2: enabled, 0 fee
1135                 // chan6  2-to-1: enabled, 0 fee
1136                 //
1137                 // chan7  1-to-2: enabled, 100% fee
1138                 // chan7  2-to-1: enabled, 0 fee
1139                 //
1140                 // chan8  1-to-2: enabled, variable fee (0 then 1000 msat)
1141                 // chan8  2-to-1: enabled, 0 fee
1142                 //
1143                 // chan9  1-to-2: enabled, 1001 msat fee
1144                 // chan9  2-to-1: enabled, 0 fee
1145                 //
1146                 // chan10 1-to-2: enabled, 0 fee
1147                 // chan10 2-to-1: enabled, 0 fee
1148                 //
1149                 // chan11 1-to-2: enabled, 0 fee
1150                 // chan11 2-to-1: enabled, 0 fee
1151
1152                 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
1153                 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
1154                 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
1155                 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
1156                 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
1157                 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
1158                 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
1159                 let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
1160
1161                 let zero_hash = Sha256dHash::hash(&[0; 32]);
1162
1163                 macro_rules! id_to_feature_flags {
1164                         // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can
1165                         // test for it later.
1166                         ($id: expr) => { {
1167                                 let idx = ($id - 1) * 2 + 1;
1168                                 if idx > 8*3 {
1169                                         vec![1 << (idx - 8*3), 0, 0, 0]
1170                                 } else if idx > 8*2 {
1171                                         vec![1 << (idx - 8*2), 0, 0]
1172                                 } else if idx > 8*1 {
1173                                         vec![1 << (idx - 8*1), 0]
1174                                 } else {
1175                                         vec![1 << idx]
1176                                 }
1177                         } }
1178                 }
1179
1180                 {
1181                         let mut network = router.network_map.write().unwrap();
1182
1183                         network.nodes.insert(node1.clone(), NodeInfo {
1184                                 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
1185                                 lowest_inbound_channel_fee_base_msat: 100,
1186                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1187                                 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(1)),
1188                                 last_update: Some(1),
1189                                 rgb: [0; 3],
1190                                 alias: [0; 32],
1191                                 addresses: Vec::new(),
1192                                 announcement_message: None,
1193                         });
1194                         network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
1195                                 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(1)),
1196                                 one_to_two: DirectionalChannelInfo {
1197                                         src_node_id: our_id.clone(),
1198                                         last_update: 0,
1199                                         enabled: false,
1200                                         cltv_expiry_delta: u16::max_value(), // This value should be ignored
1201                                         htlc_minimum_msat: 0,
1202                                         fee_base_msat: u32::max_value(), // This value should be ignored
1203                                         fee_proportional_millionths: u32::max_value(), // This value should be ignored
1204                                         last_update_message: None,
1205                                 }, two_to_one: DirectionalChannelInfo {
1206                                         src_node_id: node1.clone(),
1207                                         last_update: 0,
1208                                         enabled: true,
1209                                         cltv_expiry_delta: 0,
1210                                         htlc_minimum_msat: 0,
1211                                         fee_base_msat: 0,
1212                                         fee_proportional_millionths: 0,
1213                                         last_update_message: None,
1214                                 },
1215                                 announcement_message: None,
1216                         });
1217                         network.nodes.insert(node2.clone(), NodeInfo {
1218                                 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
1219                                 lowest_inbound_channel_fee_base_msat: 0,
1220                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1221                                 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(2)),
1222                                 last_update: Some(1),
1223                                 rgb: [0; 3],
1224                                 alias: [0; 32],
1225                                 addresses: Vec::new(),
1226                                 announcement_message: None,
1227                         });
1228                         network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
1229                                 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(2)),
1230                                 one_to_two: DirectionalChannelInfo {
1231                                         src_node_id: our_id.clone(),
1232                                         last_update: 0,
1233                                         enabled: true,
1234                                         cltv_expiry_delta: u16::max_value(), // This value should be ignored
1235                                         htlc_minimum_msat: 0,
1236                                         fee_base_msat: u32::max_value(), // This value should be ignored
1237                                         fee_proportional_millionths: u32::max_value(), // This value should be ignored
1238                                         last_update_message: None,
1239                                 }, two_to_one: DirectionalChannelInfo {
1240                                         src_node_id: node2.clone(),
1241                                         last_update: 0,
1242                                         enabled: true,
1243                                         cltv_expiry_delta: 0,
1244                                         htlc_minimum_msat: 0,
1245                                         fee_base_msat: 0,
1246                                         fee_proportional_millionths: 0,
1247                                         last_update_message: None,
1248                                 },
1249                                 announcement_message: None,
1250                         });
1251                         network.nodes.insert(node8.clone(), NodeInfo {
1252                                 channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
1253                                 lowest_inbound_channel_fee_base_msat: 0,
1254                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1255                                 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(8)),
1256                                 last_update: Some(1),
1257                                 rgb: [0; 3],
1258                                 alias: [0; 32],
1259                                 addresses: Vec::new(),
1260                                 announcement_message: None,
1261                         });
1262                         network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
1263                                 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(12)),
1264                                 one_to_two: DirectionalChannelInfo {
1265                                         src_node_id: our_id.clone(),
1266                                         last_update: 0,
1267                                         enabled: true,
1268                                         cltv_expiry_delta: u16::max_value(), // This value should be ignored
1269                                         htlc_minimum_msat: 0,
1270                                         fee_base_msat: u32::max_value(), // This value should be ignored
1271                                         fee_proportional_millionths: u32::max_value(), // This value should be ignored
1272                                         last_update_message: None,
1273                                 }, two_to_one: DirectionalChannelInfo {
1274                                         src_node_id: node8.clone(),
1275                                         last_update: 0,
1276                                         enabled: true,
1277                                         cltv_expiry_delta: 0,
1278                                         htlc_minimum_msat: 0,
1279                                         fee_base_msat: 0,
1280                                         fee_proportional_millionths: 0,
1281                                         last_update_message: None,
1282                                 },
1283                                 announcement_message: None,
1284                         });
1285                         network.nodes.insert(node3.clone(), NodeInfo {
1286                                 channels: vec!(
1287                                         NetworkMap::get_key(3, zero_hash.clone()),
1288                                         NetworkMap::get_key(4, zero_hash.clone()),
1289                                         NetworkMap::get_key(13, zero_hash.clone()),
1290                                         NetworkMap::get_key(5, zero_hash.clone()),
1291                                         NetworkMap::get_key(6, zero_hash.clone()),
1292                                         NetworkMap::get_key(7, zero_hash.clone())),
1293                                 lowest_inbound_channel_fee_base_msat: 0,
1294                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1295                                 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(3)),
1296                                 last_update: Some(1),
1297                                 rgb: [0; 3],
1298                                 alias: [0; 32],
1299                                 addresses: Vec::new(),
1300                                 announcement_message: None,
1301                         });
1302                         network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
1303                                 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(3)),
1304                                 one_to_two: DirectionalChannelInfo {
1305                                         src_node_id: node1.clone(),
1306                                         last_update: 0,
1307                                         enabled: true,
1308                                         cltv_expiry_delta: (3 << 8) | 1,
1309                                         htlc_minimum_msat: 0,
1310                                         fee_base_msat: 0,
1311                                         fee_proportional_millionths: 0,
1312                                         last_update_message: None,
1313                                 }, two_to_one: DirectionalChannelInfo {
1314                                         src_node_id: node3.clone(),
1315                                         last_update: 0,
1316                                         enabled: true,
1317                                         cltv_expiry_delta: (3 << 8) | 2,
1318                                         htlc_minimum_msat: 0,
1319                                         fee_base_msat: 100,
1320                                         fee_proportional_millionths: 0,
1321                                         last_update_message: None,
1322                                 },
1323                                 announcement_message: None,
1324                         });
1325                         network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
1326                                 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(4)),
1327                                 one_to_two: DirectionalChannelInfo {
1328                                         src_node_id: node2.clone(),
1329                                         last_update: 0,
1330                                         enabled: true,
1331                                         cltv_expiry_delta: (4 << 8) | 1,
1332                                         htlc_minimum_msat: 0,
1333                                         fee_base_msat: 0,
1334                                         fee_proportional_millionths: 1000000,
1335                                         last_update_message: None,
1336                                 }, two_to_one: DirectionalChannelInfo {
1337                                         src_node_id: node3.clone(),
1338                                         last_update: 0,
1339                                         enabled: true,
1340                                         cltv_expiry_delta: (4 << 8) | 2,
1341                                         htlc_minimum_msat: 0,
1342                                         fee_base_msat: 0,
1343                                         fee_proportional_millionths: 0,
1344                                         last_update_message: None,
1345                                 },
1346                                 announcement_message: None,
1347                         });
1348                         network.channels.insert(NetworkMap::get_key(13, zero_hash.clone()), ChannelInfo {
1349                                 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(13)),
1350                                 one_to_two: DirectionalChannelInfo {
1351                                         src_node_id: node8.clone(),
1352                                         last_update: 0,
1353                                         enabled: true,
1354                                         cltv_expiry_delta: (13 << 8) | 1,
1355                                         htlc_minimum_msat: 0,
1356                                         fee_base_msat: 0,
1357                                         fee_proportional_millionths: 2000000,
1358                                         last_update_message: None,
1359                                 }, two_to_one: DirectionalChannelInfo {
1360                                         src_node_id: node3.clone(),
1361                                         last_update: 0,
1362                                         enabled: true,
1363                                         cltv_expiry_delta: (13 << 8) | 2,
1364                                         htlc_minimum_msat: 0,
1365                                         fee_base_msat: 0,
1366                                         fee_proportional_millionths: 0,
1367                                         last_update_message: None,
1368                                 },
1369                                 announcement_message: None,
1370                         });
1371                         network.nodes.insert(node4.clone(), NodeInfo {
1372                                 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1373                                 lowest_inbound_channel_fee_base_msat: 0,
1374                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1375                                 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(4)),
1376                                 last_update: Some(1),
1377                                 rgb: [0; 3],
1378                                 alias: [0; 32],
1379                                 addresses: Vec::new(),
1380                                 announcement_message: None,
1381                         });
1382                         network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
1383                                 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(5)),
1384                                 one_to_two: DirectionalChannelInfo {
1385                                         src_node_id: node3.clone(),
1386                                         last_update: 0,
1387                                         enabled: true,
1388                                         cltv_expiry_delta: (5 << 8) | 1,
1389                                         htlc_minimum_msat: 0,
1390                                         fee_base_msat: 100,
1391                                         fee_proportional_millionths: 0,
1392                                         last_update_message: None,
1393                                 }, two_to_one: DirectionalChannelInfo {
1394                                         src_node_id: node4.clone(),
1395                                         last_update: 0,
1396                                         enabled: true,
1397                                         cltv_expiry_delta: (5 << 8) | 2,
1398                                         htlc_minimum_msat: 0,
1399                                         fee_base_msat: 0,
1400                                         fee_proportional_millionths: 0,
1401                                         last_update_message: None,
1402                                 },
1403                                 announcement_message: None,
1404                         });
1405                         network.nodes.insert(node5.clone(), NodeInfo {
1406                                 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1407                                 lowest_inbound_channel_fee_base_msat: 0,
1408                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1409                                 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(5)),
1410                                 last_update: Some(1),
1411                                 rgb: [0; 3],
1412                                 alias: [0; 32],
1413                                 addresses: Vec::new(),
1414                                 announcement_message: None,
1415                         });
1416                         network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
1417                                 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(6)),
1418                                 one_to_two: DirectionalChannelInfo {
1419                                         src_node_id: node3.clone(),
1420                                         last_update: 0,
1421                                         enabled: true,
1422                                         cltv_expiry_delta: (6 << 8) | 1,
1423                                         htlc_minimum_msat: 0,
1424                                         fee_base_msat: 0,
1425                                         fee_proportional_millionths: 0,
1426                                         last_update_message: None,
1427                                 }, two_to_one: DirectionalChannelInfo {
1428                                         src_node_id: node5.clone(),
1429                                         last_update: 0,
1430                                         enabled: true,
1431                                         cltv_expiry_delta: (6 << 8) | 2,
1432                                         htlc_minimum_msat: 0,
1433                                         fee_base_msat: 0,
1434                                         fee_proportional_millionths: 0,
1435                                         last_update_message: None,
1436                                 },
1437                                 announcement_message: None,
1438                         });
1439                         network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
1440                                 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(11)),
1441                                 one_to_two: DirectionalChannelInfo {
1442                                         src_node_id: node5.clone(),
1443                                         last_update: 0,
1444                                         enabled: true,
1445                                         cltv_expiry_delta: (11 << 8) | 1,
1446                                         htlc_minimum_msat: 0,
1447                                         fee_base_msat: 0,
1448                                         fee_proportional_millionths: 0,
1449                                         last_update_message: None,
1450                                 }, two_to_one: DirectionalChannelInfo {
1451                                         src_node_id: node4.clone(),
1452                                         last_update: 0,
1453                                         enabled: true,
1454                                         cltv_expiry_delta: (11 << 8) | 2,
1455                                         htlc_minimum_msat: 0,
1456                                         fee_base_msat: 0,
1457                                         fee_proportional_millionths: 0,
1458                                         last_update_message: None,
1459                                 },
1460                                 announcement_message: None,
1461                         });
1462                         network.nodes.insert(node6.clone(), NodeInfo {
1463                                 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
1464                                 lowest_inbound_channel_fee_base_msat: 0,
1465                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1466                                 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(6)),
1467                                 last_update: Some(1),
1468                                 rgb: [0; 3],
1469                                 alias: [0; 32],
1470                                 addresses: Vec::new(),
1471                                 announcement_message: None,
1472                         });
1473                         network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
1474                                 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(7)),
1475                                 one_to_two: DirectionalChannelInfo {
1476                                         src_node_id: node3.clone(),
1477                                         last_update: 0,
1478                                         enabled: true,
1479                                         cltv_expiry_delta: (7 << 8) | 1,
1480                                         htlc_minimum_msat: 0,
1481                                         fee_base_msat: 0,
1482                                         fee_proportional_millionths: 1000000,
1483                                         last_update_message: None,
1484                                 }, two_to_one: DirectionalChannelInfo {
1485                                         src_node_id: node6.clone(),
1486                                         last_update: 0,
1487                                         enabled: true,
1488                                         cltv_expiry_delta: (7 << 8) | 2,
1489                                         htlc_minimum_msat: 0,
1490                                         fee_base_msat: 0,
1491                                         fee_proportional_millionths: 0,
1492                                         last_update_message: None,
1493                                 },
1494                                 announcement_message: None,
1495                         });
1496                 }
1497
1498                 { // Simple route to 3 via 2
1499                         let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
1500                         assert_eq!(route.hops.len(), 2);
1501
1502                         assert_eq!(route.hops[0].pubkey, node2);
1503                         assert_eq!(route.hops[0].short_channel_id, 2);
1504                         assert_eq!(route.hops[0].fee_msat, 100);
1505                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1506                         assert_eq!(route.hops[0].node_features.le_flags(), &id_to_feature_flags!(2));
1507                         assert_eq!(route.hops[0].channel_features.le_flags(), &id_to_feature_flags!(2));
1508
1509                         assert_eq!(route.hops[1].pubkey, node3);
1510                         assert_eq!(route.hops[1].short_channel_id, 4);
1511                         assert_eq!(route.hops[1].fee_msat, 100);
1512                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1513                         assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1514                         assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(4));
1515                 }
1516
1517                 { // Disable channels 4 and 12 by requiring unknown feature bits
1518                         let mut network = router.network_map.write().unwrap();
1519                         network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1520                         network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1521                 }
1522
1523                 { // If all the channels require some features we don't understand, route should fail
1524                         if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1525                                 assert_eq!(err, "Failed to find a path to the given destination");
1526                         } else { panic!(); }
1527                 }
1528
1529                 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1530                         let our_chans = vec![channelmanager::ChannelDetails {
1531                                 channel_id: [0; 32],
1532                                 short_channel_id: Some(42),
1533                                 remote_network_id: node8.clone(),
1534                                 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1535                                 channel_value_satoshis: 0,
1536                                 user_id: 0,
1537                                 outbound_capacity_msat: 0,
1538                                 inbound_capacity_msat: 0,
1539                                 is_live: true,
1540                         }];
1541                         let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1542                         assert_eq!(route.hops.len(), 2);
1543
1544                         assert_eq!(route.hops[0].pubkey, node8);
1545                         assert_eq!(route.hops[0].short_channel_id, 42);
1546                         assert_eq!(route.hops[0].fee_msat, 200);
1547                         assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1548                         assert_eq!(route.hops[0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
1549                         assert_eq!(route.hops[0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion
1550
1551                         assert_eq!(route.hops[1].pubkey, node3);
1552                         assert_eq!(route.hops[1].short_channel_id, 13);
1553                         assert_eq!(route.hops[1].fee_msat, 100);
1554                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1555                         assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1556                         assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(13));
1557                 }
1558
1559                 { // Re-enable channels 4 and 12 by wiping the unknown feature bits
1560                         let mut network = router.network_map.write().unwrap();
1561                         network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1562                         network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1563                 }
1564
1565                 { // Disable nodes 1, 2, and 8 by requiring unknown feature bits
1566                         let mut network = router.network_map.write().unwrap();
1567                         network.nodes.get_mut(&node1).unwrap().features.set_require_unknown_bits();
1568                         network.nodes.get_mut(&node2).unwrap().features.set_require_unknown_bits();
1569                         network.nodes.get_mut(&node8).unwrap().features.set_require_unknown_bits();
1570                 }
1571
1572                 { // If all nodes require some features we don't understand, route should fail
1573                         if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1574                                 assert_eq!(err, "Failed to find a path to the given destination");
1575                         } else { panic!(); }
1576                 }
1577
1578                 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1579                         let our_chans = vec![channelmanager::ChannelDetails {
1580                                 channel_id: [0; 32],
1581                                 short_channel_id: Some(42),
1582                                 remote_network_id: node8.clone(),
1583                                 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1584                                 channel_value_satoshis: 0,
1585                                 user_id: 0,
1586                                 outbound_capacity_msat: 0,
1587                                 inbound_capacity_msat: 0,
1588                                 is_live: true,
1589                         }];
1590                         let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1591                         assert_eq!(route.hops.len(), 2);
1592
1593                         assert_eq!(route.hops[0].pubkey, node8);
1594                         assert_eq!(route.hops[0].short_channel_id, 42);
1595                         assert_eq!(route.hops[0].fee_msat, 200);
1596                         assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1597                         assert_eq!(route.hops[0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
1598                         assert_eq!(route.hops[0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion
1599
1600                         assert_eq!(route.hops[1].pubkey, node3);
1601                         assert_eq!(route.hops[1].short_channel_id, 13);
1602                         assert_eq!(route.hops[1].fee_msat, 100);
1603                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1604                         assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1605                         assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(13));
1606                 }
1607
1608                 { // Re-enable nodes 1, 2, and 8
1609                         let mut network = router.network_map.write().unwrap();
1610                         network.nodes.get_mut(&node1).unwrap().features.clear_require_unknown_bits();
1611                         network.nodes.get_mut(&node2).unwrap().features.clear_require_unknown_bits();
1612                         network.nodes.get_mut(&node8).unwrap().features.clear_require_unknown_bits();
1613                 }
1614
1615                 // Note that we don't test disabling node 3 and failing to route to it, as we (somewhat
1616                 // naively) assume that the user checked the feature bits on the invoice, which override
1617                 // the node_announcement.
1618
1619                 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
1620                         let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
1621                         assert_eq!(route.hops.len(), 3);
1622
1623                         assert_eq!(route.hops[0].pubkey, node2);
1624                         assert_eq!(route.hops[0].short_channel_id, 2);
1625                         assert_eq!(route.hops[0].fee_msat, 200);
1626                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1627                         assert_eq!(route.hops[0].node_features.le_flags(), &id_to_feature_flags!(2));
1628                         assert_eq!(route.hops[0].channel_features.le_flags(), &id_to_feature_flags!(2));
1629
1630                         assert_eq!(route.hops[1].pubkey, node3);
1631                         assert_eq!(route.hops[1].short_channel_id, 4);
1632                         assert_eq!(route.hops[1].fee_msat, 100);
1633                         assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
1634                         assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1635                         assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(4));
1636
1637                         assert_eq!(route.hops[2].pubkey, node1);
1638                         assert_eq!(route.hops[2].short_channel_id, 3);
1639                         assert_eq!(route.hops[2].fee_msat, 100);
1640                         assert_eq!(route.hops[2].cltv_expiry_delta, 42);
1641                         assert_eq!(route.hops[2].node_features.le_flags(), &id_to_feature_flags!(1));
1642                         assert_eq!(route.hops[2].channel_features.le_flags(), &id_to_feature_flags!(3));
1643                 }
1644
1645                 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1646                         let our_chans = vec![channelmanager::ChannelDetails {
1647                                 channel_id: [0; 32],
1648                                 short_channel_id: Some(42),
1649                                 remote_network_id: node8.clone(),
1650                                 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1651                                 channel_value_satoshis: 0,
1652                                 user_id: 0,
1653                                 outbound_capacity_msat: 0,
1654                                 inbound_capacity_msat: 0,
1655                                 is_live: true,
1656                         }];
1657                         let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1658                         assert_eq!(route.hops.len(), 2);
1659
1660                         assert_eq!(route.hops[0].pubkey, node8);
1661                         assert_eq!(route.hops[0].short_channel_id, 42);
1662                         assert_eq!(route.hops[0].fee_msat, 200);
1663                         assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1664                         assert_eq!(route.hops[0].node_features.le_flags(), &vec![0b11]);
1665                         assert_eq!(route.hops[0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion
1666
1667                         assert_eq!(route.hops[1].pubkey, node3);
1668                         assert_eq!(route.hops[1].short_channel_id, 13);
1669                         assert_eq!(route.hops[1].fee_msat, 100);
1670                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1671                         assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1672                         assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(13));
1673                 }
1674
1675                 let mut last_hops = vec!(RouteHint {
1676                                 src_node_id: node4.clone(),
1677                                 short_channel_id: 8,
1678                                 fee_base_msat: 0,
1679                                 fee_proportional_millionths: 0,
1680                                 cltv_expiry_delta: (8 << 8) | 1,
1681                                 htlc_minimum_msat: 0,
1682                         }, RouteHint {
1683                                 src_node_id: node5.clone(),
1684                                 short_channel_id: 9,
1685                                 fee_base_msat: 1001,
1686                                 fee_proportional_millionths: 0,
1687                                 cltv_expiry_delta: (9 << 8) | 1,
1688                                 htlc_minimum_msat: 0,
1689                         }, RouteHint {
1690                                 src_node_id: node6.clone(),
1691                                 short_channel_id: 10,
1692                                 fee_base_msat: 0,
1693                                 fee_proportional_millionths: 0,
1694                                 cltv_expiry_delta: (10 << 8) | 1,
1695                                 htlc_minimum_msat: 0,
1696                         });
1697
1698                 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
1699                         let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1700                         assert_eq!(route.hops.len(), 5);
1701
1702                         assert_eq!(route.hops[0].pubkey, node2);
1703                         assert_eq!(route.hops[0].short_channel_id, 2);
1704                         assert_eq!(route.hops[0].fee_msat, 100);
1705                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1706                         assert_eq!(route.hops[0].node_features.le_flags(), &id_to_feature_flags!(2));
1707                         assert_eq!(route.hops[0].channel_features.le_flags(), &id_to_feature_flags!(2));
1708
1709                         assert_eq!(route.hops[1].pubkey, node3);
1710                         assert_eq!(route.hops[1].short_channel_id, 4);
1711                         assert_eq!(route.hops[1].fee_msat, 0);
1712                         assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1713                         assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1714                         assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(4));
1715
1716                         assert_eq!(route.hops[2].pubkey, node5);
1717                         assert_eq!(route.hops[2].short_channel_id, 6);
1718                         assert_eq!(route.hops[2].fee_msat, 0);
1719                         assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1720                         assert_eq!(route.hops[2].node_features.le_flags(), &id_to_feature_flags!(5));
1721                         assert_eq!(route.hops[2].channel_features.le_flags(), &id_to_feature_flags!(6));
1722
1723                         assert_eq!(route.hops[3].pubkey, node4);
1724                         assert_eq!(route.hops[3].short_channel_id, 11);
1725                         assert_eq!(route.hops[3].fee_msat, 0);
1726                         assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1727                         // If we have a peer in the node map, we'll use their features here since we don't have
1728                         // a way of figuring out their features from the invoice:
1729                         assert_eq!(route.hops[3].node_features.le_flags(), &id_to_feature_flags!(4));
1730                         assert_eq!(route.hops[3].channel_features.le_flags(), &id_to_feature_flags!(11));
1731
1732                         assert_eq!(route.hops[4].pubkey, node7);
1733                         assert_eq!(route.hops[4].short_channel_id, 8);
1734                         assert_eq!(route.hops[4].fee_msat, 100);
1735                         assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1736                         assert_eq!(route.hops[4].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet
1737                         assert_eq!(route.hops[4].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly
1738                 }
1739
1740                 { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1741                         let our_chans = vec![channelmanager::ChannelDetails {
1742                                 channel_id: [0; 32],
1743                                 short_channel_id: Some(42),
1744                                 remote_network_id: node4.clone(),
1745                                 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1746                                 channel_value_satoshis: 0,
1747                                 user_id: 0,
1748                                 outbound_capacity_msat: 0,
1749                                 inbound_capacity_msat: 0,
1750                                 is_live: true,
1751                         }];
1752                         let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
1753                         assert_eq!(route.hops.len(), 2);
1754
1755                         assert_eq!(route.hops[0].pubkey, node4);
1756                         assert_eq!(route.hops[0].short_channel_id, 42);
1757                         assert_eq!(route.hops[0].fee_msat, 0);
1758                         assert_eq!(route.hops[0].cltv_expiry_delta, (8 << 8) | 1);
1759                         assert_eq!(route.hops[0].node_features.le_flags(), &vec![0b11]);
1760                         assert_eq!(route.hops[0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion
1761
1762                         assert_eq!(route.hops[1].pubkey, node7);
1763                         assert_eq!(route.hops[1].short_channel_id, 8);
1764                         assert_eq!(route.hops[1].fee_msat, 100);
1765                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1766                         assert_eq!(route.hops[1].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet
1767                         assert_eq!(route.hops[1].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly
1768                 }
1769
1770                 last_hops[0].fee_base_msat = 1000;
1771
1772                 { // Revert to via 6 as the fee on 8 goes up
1773                         let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1774                         assert_eq!(route.hops.len(), 4);
1775
1776                         assert_eq!(route.hops[0].pubkey, node2);
1777                         assert_eq!(route.hops[0].short_channel_id, 2);
1778                         assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
1779                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1780                         assert_eq!(route.hops[0].node_features.le_flags(), &id_to_feature_flags!(2));
1781                         assert_eq!(route.hops[0].channel_features.le_flags(), &id_to_feature_flags!(2));
1782
1783                         assert_eq!(route.hops[1].pubkey, node3);
1784                         assert_eq!(route.hops[1].short_channel_id, 4);
1785                         assert_eq!(route.hops[1].fee_msat, 100);
1786                         assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
1787                         assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1788                         assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(4));
1789
1790                         assert_eq!(route.hops[2].pubkey, node6);
1791                         assert_eq!(route.hops[2].short_channel_id, 7);
1792                         assert_eq!(route.hops[2].fee_msat, 0);
1793                         assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
1794                         // If we have a peer in the node map, we'll use their features here since we don't have
1795                         // a way of figuring out their features from the invoice:
1796                         assert_eq!(route.hops[2].node_features.le_flags(), &id_to_feature_flags!(6));
1797                         assert_eq!(route.hops[2].channel_features.le_flags(), &id_to_feature_flags!(7));
1798
1799                         assert_eq!(route.hops[3].pubkey, node7);
1800                         assert_eq!(route.hops[3].short_channel_id, 10);
1801                         assert_eq!(route.hops[3].fee_msat, 100);
1802                         assert_eq!(route.hops[3].cltv_expiry_delta, 42);
1803                         assert_eq!(route.hops[3].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet
1804                         assert_eq!(route.hops[3].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly
1805                 }
1806
1807                 { // ...but still use 8 for larger payments as 6 has a variable feerate
1808                         let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
1809                         assert_eq!(route.hops.len(), 5);
1810
1811                         assert_eq!(route.hops[0].pubkey, node2);
1812                         assert_eq!(route.hops[0].short_channel_id, 2);
1813                         assert_eq!(route.hops[0].fee_msat, 3000);
1814                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1815                         assert_eq!(route.hops[0].node_features.le_flags(), &id_to_feature_flags!(2));
1816                         assert_eq!(route.hops[0].channel_features.le_flags(), &id_to_feature_flags!(2));
1817
1818                         assert_eq!(route.hops[1].pubkey, node3);
1819                         assert_eq!(route.hops[1].short_channel_id, 4);
1820                         assert_eq!(route.hops[1].fee_msat, 0);
1821                         assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1822                         assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1823                         assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(4));
1824
1825                         assert_eq!(route.hops[2].pubkey, node5);
1826                         assert_eq!(route.hops[2].short_channel_id, 6);
1827                         assert_eq!(route.hops[2].fee_msat, 0);
1828                         assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1829                         assert_eq!(route.hops[2].node_features.le_flags(), &id_to_feature_flags!(5));
1830                         assert_eq!(route.hops[2].channel_features.le_flags(), &id_to_feature_flags!(6));
1831
1832                         assert_eq!(route.hops[3].pubkey, node4);
1833                         assert_eq!(route.hops[3].short_channel_id, 11);
1834                         assert_eq!(route.hops[3].fee_msat, 1000);
1835                         assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1836                         // If we have a peer in the node map, we'll use their features here since we don't have
1837                         // a way of figuring out their features from the invoice:
1838                         assert_eq!(route.hops[3].node_features.le_flags(), &id_to_feature_flags!(4));
1839                         assert_eq!(route.hops[3].channel_features.le_flags(), &id_to_feature_flags!(11));
1840
1841                         assert_eq!(route.hops[4].pubkey, node7);
1842                         assert_eq!(route.hops[4].short_channel_id, 8);
1843                         assert_eq!(route.hops[4].fee_msat, 2000);
1844                         assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1845                         assert_eq!(route.hops[4].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet
1846                         assert_eq!(route.hops[4].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly
1847                 }
1848
1849                 { // Test Router serialization/deserialization
1850                         let mut w = TestVecWriter(Vec::new());
1851                         let network = router.network_map.read().unwrap();
1852                         assert!(!network.channels.is_empty());
1853                         assert!(!network.nodes.is_empty());
1854                         network.write(&mut w).unwrap();
1855                         assert!(<NetworkMap>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);
1856                 }
1857         }
1858
1859         #[test]
1860         fn request_full_sync_finite_times() {
1861                 let (secp_ctx, _, router) = create_router();
1862                 let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
1863
1864                 assert!(router.should_request_full_sync(&node_id));
1865                 assert!(router.should_request_full_sync(&node_id));
1866                 assert!(router.should_request_full_sync(&node_id));
1867                 assert!(router.should_request_full_sync(&node_id));
1868                 assert!(router.should_request_full_sync(&node_id));
1869                 assert!(!router.should_request_full_sync(&node_id));
1870         }
1871
1872         #[test]
1873         fn handling_node_announcements() {
1874                 let (secp_ctx, _, router) = create_router();
1875
1876                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1877                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1878                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1879                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1880                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1881                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1882                 let zero_hash = Sha256dHash::hash(&[0; 32]);
1883                 let first_announcement_time = 500;
1884
1885                 let mut unsigned_announcement = UnsignedNodeAnnouncement {
1886                         features: NodeFeatures::supported(),
1887                         timestamp: first_announcement_time,
1888                         node_id: node_id_1,
1889                         rgb: [0; 3],
1890                         alias: [0; 32],
1891                         addresses: Vec::new(),
1892                         excess_address_data: Vec::new(),
1893                         excess_data: Vec::new(),
1894                 };
1895                 let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1896                 let valid_announcement = NodeAnnouncement {
1897                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1898                         contents: unsigned_announcement.clone()
1899                 };
1900
1901                 match router.handle_node_announcement(&valid_announcement) {
1902                         Ok(_) => panic!(),
1903                         Err(e) => assert_eq!("No existing channels for node_announcement", e.err)
1904                 };
1905
1906                 {
1907                         // Announce a channel to add a corresponding node.
1908                         let unsigned_announcement = UnsignedChannelAnnouncement {
1909                                 features: ChannelFeatures::supported(),
1910                                 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
1911                                 short_channel_id: 0,
1912                                 node_id_1,
1913                                 node_id_2,
1914                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1915                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1916                                 excess_data: Vec::new(),
1917                         };
1918
1919                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1920                         let valid_announcement = ChannelAnnouncement {
1921                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1922                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1923                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1924                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1925                                 contents: unsigned_announcement.clone(),
1926                         };
1927                         match router.handle_channel_announcement(&valid_announcement) {
1928                                 Ok(res) => assert!(res),
1929                                 _ => panic!()
1930                         };
1931                 }
1932
1933                 match router.handle_node_announcement(&valid_announcement) {
1934                         Ok(res) => assert!(res),
1935                         Err(_) => panic!()
1936                 };
1937
1938                 let fake_msghash = hash_to_message!(&zero_hash);
1939                 match router.handle_node_announcement(
1940                         &NodeAnnouncement {
1941                                 signature: secp_ctx.sign(&fake_msghash, node_1_privkey),
1942                                 contents: unsigned_announcement.clone()
1943                 }) {
1944                         Ok(_) => panic!(),
1945                         Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1946                 };
1947
1948                 unsigned_announcement.timestamp += 1000;
1949                 unsigned_announcement.excess_data.push(1);
1950                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1951                 let announcement_with_data = NodeAnnouncement {
1952                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1953                         contents: unsigned_announcement.clone()
1954                 };
1955                 // Return false because contains excess data.
1956                 match router.handle_node_announcement(&announcement_with_data) {
1957                         Ok(res) => assert!(!res),
1958                         Err(_) => panic!()
1959                 };
1960                 unsigned_announcement.excess_data = Vec::new();
1961
1962                 // Even though previous announcement was not relayed further, we still accepted it,
1963                 // so we now won't accept announcements before the previous one.
1964                 unsigned_announcement.timestamp -= 10;
1965                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1966                 let outdated_announcement = NodeAnnouncement {
1967                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1968                         contents: unsigned_announcement.clone()
1969                 };
1970                 match router.handle_node_announcement(&outdated_announcement) {
1971                         Ok(_) => panic!(),
1972                         Err(e) => assert_eq!(e.err, "Update older than last processed update")
1973                 };
1974         }
1975
1976         #[test]
1977         fn handling_channel_announcements() {
1978                 let secp_ctx = Secp256k1::new();
1979                 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(
1980                    &hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
1981                 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1982                 let chain_monitor = Arc::new(test_utils::TestChainWatcher::new());
1983                 let router = Router::new(our_id, chain_monitor.clone(), Arc::clone(&logger));
1984
1985                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1986                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1987                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1988                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1989                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1990                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1991
1992                 let good_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
1993                    .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_1_btckey).serialize())
1994                    .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_2_btckey).serialize())
1995                    .push_opcode(opcodes::all::OP_PUSHNUM_2)
1996                    .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
1997
1998
1999                 let mut unsigned_announcement = UnsignedChannelAnnouncement {
2000                         features: ChannelFeatures::supported(),
2001                         chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
2002                         short_channel_id: 0,
2003                         node_id_1,
2004                         node_id_2,
2005                         bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
2006                         bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
2007                         excess_data: Vec::new(),
2008                 };
2009
2010                 let channel_key = NetworkMap::get_key(unsigned_announcement.short_channel_id,
2011                                                     unsigned_announcement.chain_hash);
2012
2013                 let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2014                 let valid_announcement = ChannelAnnouncement {
2015                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2016                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2017                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2018                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2019                         contents: unsigned_announcement.clone(),
2020                 };
2021
2022                 // Test if the UTXO lookups were not supported
2023                 *chain_monitor.utxo_ret.lock().unwrap() = Err(chaininterface::ChainError::NotSupported);
2024
2025                 match router.handle_channel_announcement(&valid_announcement) {
2026                         Ok(res) => assert!(res),
2027                         _ => panic!()
2028                 };
2029                 {
2030                         let network = router.network_map.write().unwrap();
2031                         match network.channels.get(&channel_key) {
2032                                 None => panic!(),
2033                                 Some(_) => ()
2034                         }
2035                 }
2036
2037                 // If we receive announcement for the same channel (with UTXO lookups disabled),
2038                 // drop new one on the floor, since we can't see any changes.
2039                 match router.handle_channel_announcement(&valid_announcement) {
2040                         Ok(_) => panic!(),
2041                         Err(e) => assert_eq!(e.err, "Already have knowledge of channel")
2042                 };
2043
2044
2045                 // Test if an associated transaction were not on-chain (or not confirmed).
2046                 *chain_monitor.utxo_ret.lock().unwrap() = Err(chaininterface::ChainError::UnknownTx);
2047                 unsigned_announcement.short_channel_id += 1;
2048
2049                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2050                 let valid_announcement = ChannelAnnouncement {
2051                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2052                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2053                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2054                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2055                         contents: unsigned_announcement.clone(),
2056                 };
2057
2058                 match router.handle_channel_announcement(&valid_announcement) {
2059                         Ok(_) => panic!(),
2060                         Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry")
2061                 };
2062
2063
2064                 // Now test if the transaction is found in the UTXO set and the script is correct.
2065                 unsigned_announcement.short_channel_id += 1;
2066                 *chain_monitor.utxo_ret.lock().unwrap() = Ok((good_script.clone(), 0));
2067                 let channel_key = NetworkMap::get_key(unsigned_announcement.short_channel_id,
2068                                                    unsigned_announcement.chain_hash);
2069
2070                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2071                 let valid_announcement = ChannelAnnouncement {
2072                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2073                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2074                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2075                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2076                         contents: unsigned_announcement.clone(),
2077                 };
2078                 match router.handle_channel_announcement(&valid_announcement) {
2079                         Ok(res) => assert!(res),
2080                         _ => panic!()
2081                 };
2082                 {
2083                         let network = router.network_map.write().unwrap();
2084                         match network.channels.get(&channel_key) {
2085                                 None => panic!(),
2086                                 Some(_) => ()
2087                         }
2088                 }
2089
2090                 // If we receive announcement for the same channel (but TX is not confirmed),
2091                 // drop new one on the floor, since we can't see any changes.
2092                 *chain_monitor.utxo_ret.lock().unwrap() = Err(chaininterface::ChainError::UnknownTx);
2093                 match router.handle_channel_announcement(&valid_announcement) {
2094                         Ok(_) => panic!(),
2095                         Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry")
2096                 };
2097
2098                 // But if it is confirmed, replace the channel
2099                 *chain_monitor.utxo_ret.lock().unwrap() = Ok((good_script, 0));
2100                 unsigned_announcement.features = ChannelFeatures::empty();
2101                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2102                 let valid_announcement = ChannelAnnouncement {
2103                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2104                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2105                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2106                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2107                         contents: unsigned_announcement.clone(),
2108                 };
2109                 match router.handle_channel_announcement(&valid_announcement) {
2110                         Ok(res) => assert!(res),
2111                         _ => panic!()
2112                 };
2113                 {
2114                         let mut network = router.network_map.write().unwrap();
2115                         match network.channels.entry(channel_key) {
2116                                 BtreeEntry::Occupied(channel_entry) => {
2117                                         assert_eq!(channel_entry.get().features, ChannelFeatures::empty());
2118                                 },
2119                                 _ => panic!()
2120                         }
2121                 }
2122
2123                 // Don't relay valid channels with excess data
2124                 unsigned_announcement.short_channel_id += 1;
2125                 unsigned_announcement.excess_data.push(1);
2126                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2127                 let valid_announcement = ChannelAnnouncement {
2128                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2129                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2130                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2131                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2132                         contents: unsigned_announcement.clone(),
2133                 };
2134                 match router.handle_channel_announcement(&valid_announcement) {
2135                         Ok(res) => assert!(!res),
2136                         _ => panic!()
2137                 };
2138
2139                 unsigned_announcement.excess_data = Vec::new();
2140                 let invalid_sig_announcement = ChannelAnnouncement {
2141                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2142                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2143                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2144                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_1_btckey),
2145                         contents: unsigned_announcement.clone(),
2146                 };
2147                 match router.handle_channel_announcement(&invalid_sig_announcement) {
2148                         Ok(_) => panic!(),
2149                         Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
2150                 };
2151
2152                 unsigned_announcement.node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
2153                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2154                 let channel_to_itself_announcement = ChannelAnnouncement {
2155                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2156                         node_signature_2: secp_ctx.sign(&msghash, node_1_privkey),
2157                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2158                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2159                         contents: unsigned_announcement.clone(),
2160                 };
2161                 match router.handle_channel_announcement(&channel_to_itself_announcement) {
2162                         Ok(_) => panic!(),
2163                         Err(e) => assert_eq!(e.err, "Channel announcement node had a channel with itself")
2164                 };
2165         }
2166
2167         #[test]
2168         fn handling_channel_update() {
2169                 let (secp_ctx, _, router) = create_router();
2170                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
2171                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2172                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2173                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
2174                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
2175                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
2176
2177                 let zero_hash = Sha256dHash::hash(&[0; 32]);
2178                 let short_channel_id = 0;
2179                 let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
2180                 let channel_key = NetworkMap::get_key(short_channel_id, chain_hash);
2181
2182
2183                 {
2184                         // Announce a channel we will update
2185                         let unsigned_announcement = UnsignedChannelAnnouncement {
2186                                 features: ChannelFeatures::empty(),
2187                                 chain_hash,
2188                                 short_channel_id,
2189                                 node_id_1,
2190                                 node_id_2,
2191                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
2192                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
2193                                 excess_data: Vec::new(),
2194                         };
2195
2196                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2197                         let valid_channel_announcement = ChannelAnnouncement {
2198                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2199                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2200                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2201                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2202                                 contents: unsigned_announcement.clone(),
2203                         };
2204                         match router.handle_channel_announcement(&valid_channel_announcement) {
2205                                 Ok(_) => (),
2206                                 Err(_) => panic!()
2207                         };
2208
2209                 }
2210
2211                 let mut unsigned_channel_update = UnsignedChannelUpdate {
2212                         chain_hash,
2213                         short_channel_id,
2214                         timestamp: 100,
2215                         flags: 0,
2216                         cltv_expiry_delta: 144,
2217                         htlc_minimum_msat: 1000000,
2218                         fee_base_msat: 10000,
2219                         fee_proportional_millionths: 20,
2220                         excess_data: Vec::new()
2221                 };
2222                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
2223                 let valid_channel_update = ChannelUpdate {
2224                         signature: secp_ctx.sign(&msghash, node_1_privkey),
2225                         contents: unsigned_channel_update.clone()
2226                 };
2227
2228                 match router.handle_channel_update(&valid_channel_update) {
2229                         Ok(res) => assert!(res),
2230                         _ => panic!()
2231                 };
2232
2233                 {
2234                         let network = router.network_map.write().unwrap();
2235                         match network.channels.get(&channel_key) {
2236                                 None => panic!(),
2237                                 Some(channel_info) => {
2238                                         assert_eq!(channel_info.one_to_two.cltv_expiry_delta, 144);
2239                                         assert_eq!(channel_info.two_to_one.cltv_expiry_delta, u16::max_value());
2240                                 }
2241                         }
2242                 }
2243
2244                 unsigned_channel_update.timestamp += 100;
2245                 unsigned_channel_update.excess_data.push(1);
2246                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
2247                 let valid_channel_update = ChannelUpdate {
2248                         signature: secp_ctx.sign(&msghash, node_1_privkey),
2249                         contents: unsigned_channel_update.clone()
2250                 };
2251                 // Return false because contains excess data
2252                 match router.handle_channel_update(&valid_channel_update) {
2253                         Ok(res) => assert!(!res),
2254                         _ => panic!()
2255                 };
2256
2257                 unsigned_channel_update.short_channel_id += 1;
2258                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
2259                 let valid_channel_update = ChannelUpdate {
2260                         signature: secp_ctx.sign(&msghash, node_1_privkey),
2261                         contents: unsigned_channel_update.clone()
2262                 };
2263
2264                 match router.handle_channel_update(&valid_channel_update) {
2265                         Ok(_) => panic!(),
2266                         Err(e) => assert_eq!(e.err, "Couldn't find channel for update")
2267                 };
2268                 unsigned_channel_update.short_channel_id = short_channel_id;
2269
2270
2271                 // Even though previous update was not relayed further, we still accepted it,
2272                 // so we now won't accept update before the previous one.
2273                 unsigned_channel_update.timestamp -= 10;
2274                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
2275                 let valid_channel_update = ChannelUpdate {
2276                         signature: secp_ctx.sign(&msghash, node_1_privkey),
2277                         contents: unsigned_channel_update.clone()
2278                 };
2279
2280                 match router.handle_channel_update(&valid_channel_update) {
2281                         Ok(_) => panic!(),
2282                         Err(e) => assert_eq!(e.err, "Update older than last processed update")
2283                 };
2284                 unsigned_channel_update.timestamp += 500;
2285
2286                 let fake_msghash = hash_to_message!(&zero_hash);
2287                 let invalid_sig_channel_update = ChannelUpdate {
2288                         signature: secp_ctx.sign(&fake_msghash, node_1_privkey),
2289                         contents: unsigned_channel_update.clone()
2290                 };
2291
2292                 match router.handle_channel_update(&invalid_sig_channel_update) {
2293                         Ok(_) => panic!(),
2294                         Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
2295                 };
2296
2297         }
2298
2299         #[test]
2300         fn handling_htlc_fail_channel_update() {
2301                 let (secp_ctx, our_id, router) = create_router();
2302                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
2303                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2304                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2305                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
2306                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
2307                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
2308
2309                 let short_channel_id = 0;
2310                 let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
2311                 let channel_key = NetworkMap::get_key(short_channel_id, chain_hash);
2312
2313                 {
2314                         // There is only local node in the table at the beginning.
2315                         let network = router.network_map.read().unwrap();
2316                         assert_eq!(network.nodes.len(), 1);
2317                         assert_eq!(network.nodes.contains_key(&our_id), true);
2318                 }
2319
2320                 {
2321                         // Announce a channel we will update
2322                         let unsigned_announcement = UnsignedChannelAnnouncement {
2323                                 features: ChannelFeatures::empty(),
2324                                 chain_hash,
2325                                 short_channel_id,
2326                                 node_id_1,
2327                                 node_id_2,
2328                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
2329                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
2330                                 excess_data: Vec::new(),
2331                         };
2332
2333                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2334                         let valid_channel_announcement = ChannelAnnouncement {
2335                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2336                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2337                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2338                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2339                                 contents: unsigned_announcement.clone(),
2340                         };
2341                         match router.handle_channel_announcement(&valid_channel_announcement) {
2342                                 Ok(_) => (),
2343                                 Err(_) => panic!()
2344                         };
2345
2346                 }
2347
2348                 let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
2349                         short_channel_id,
2350                         is_permanent: false
2351                 };
2352
2353                 router.handle_htlc_fail_channel_update(&channel_close_msg);
2354
2355                 {
2356                         // Non-permanent closing just disables a channel
2357                         let network = router.network_map.write().unwrap();
2358                         match network.channels.get(&channel_key) {
2359                                 None => panic!(),
2360                                 Some(channel_info) => {
2361                                         assert!(!channel_info.one_to_two.enabled);
2362                                         assert!(!channel_info.two_to_one.enabled);
2363                                 }
2364                         }
2365                 }
2366
2367                 let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
2368                         short_channel_id,
2369                         is_permanent: true
2370                 };
2371
2372                 router.handle_htlc_fail_channel_update(&channel_close_msg);
2373
2374                 {
2375                         // Permanent closing deletes a channel
2376                         let network = router.network_map.read().unwrap();
2377                         assert_eq!(network.channels.len(), 0);
2378                         // Nodes are also deleted because there are no associated channels anymore
2379                         // Only the local node remains in the table.
2380                         assert_eq!(network.nodes.len(), 1);
2381                         assert_eq!(network.nodes.contains_key(&our_id), true);
2382                 }
2383
2384                 // TODO: Test HTLCFailChannelUpdate::NodeFailure, which is not implemented yet.
2385         }
2386
2387         #[test]
2388         fn getting_next_channel_announcements() {
2389                 let (secp_ctx, _, router) = create_router();
2390                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
2391                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2392                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2393                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
2394                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
2395                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
2396
2397                 let short_channel_id = 1;
2398                 let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
2399                 let channel_key = NetworkMap::get_key(short_channel_id, chain_hash);
2400
2401                 // Channels were not announced yet.
2402                 let channels_with_announcements = router.get_next_channel_announcements(0, 1);
2403                 assert_eq!(channels_with_announcements.len(), 0);
2404
2405                 {
2406                         // Announce a channel we will update
2407                         let unsigned_announcement = UnsignedChannelAnnouncement {
2408                                 features: ChannelFeatures::empty(),
2409                                 chain_hash,
2410                                 short_channel_id,
2411                                 node_id_1,
2412                                 node_id_2,
2413                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
2414                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
2415                                 excess_data: Vec::new(),
2416                         };
2417
2418                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2419                         let valid_channel_announcement = ChannelAnnouncement {
2420                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2421                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2422                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2423                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2424                                 contents: unsigned_announcement.clone(),
2425                         };
2426                         match router.handle_channel_announcement(&valid_channel_announcement) {
2427                                 Ok(_) => (),
2428                                 Err(_) => panic!()
2429                         };
2430                 }
2431
2432                 // Contains initial channel announcement now.
2433                 let channels_with_announcements = router.get_next_channel_announcements(channel_key, 1);
2434                 assert_eq!(channels_with_announcements.len(), 1);
2435                 if let Some(channel_announcements) = channels_with_announcements.first() {
2436                         let &(_, ref update_1, ref update_2) = channel_announcements;
2437                         assert_eq!(update_1, &None);
2438                         assert_eq!(update_2, &None);
2439                 } else {
2440                         panic!();
2441                 }
2442
2443
2444                 {
2445                         // Valid channel update
2446                         let unsigned_channel_update = UnsignedChannelUpdate {
2447                                 chain_hash,
2448                                 short_channel_id,
2449                                 timestamp: 101,
2450                                 flags: 0,
2451                                 cltv_expiry_delta: 144,
2452                                 htlc_minimum_msat: 1000000,
2453                                 fee_base_msat: 10000,
2454                                 fee_proportional_millionths: 20,
2455                                 excess_data: Vec::new()
2456                         };
2457                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
2458                         let valid_channel_update = ChannelUpdate {
2459                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
2460                                 contents: unsigned_channel_update.clone()
2461                         };
2462                         match router.handle_channel_update(&valid_channel_update) {
2463                                 Ok(_) => (),
2464                                 Err(_) => panic!()
2465                         };
2466                 }
2467
2468                 // Now contains an initial announcement and an update.
2469                 let channels_with_announcements = router.get_next_channel_announcements(channel_key, 1);
2470                 assert_eq!(channels_with_announcements.len(), 1);
2471                 if let Some(channel_announcements) = channels_with_announcements.first() {
2472                         let &(_, ref update_1, ref update_2) = channel_announcements;
2473                         assert_ne!(update_1, &None);
2474                         assert_eq!(update_2, &None);
2475                 } else {
2476                         panic!();
2477                 }
2478
2479
2480                 {
2481                         // Channel update with excess data.
2482                         let unsigned_channel_update = UnsignedChannelUpdate {
2483                                 chain_hash,
2484                                 short_channel_id,
2485                                 timestamp: 102,
2486                                 flags: 0,
2487                                 cltv_expiry_delta: 144,
2488                                 htlc_minimum_msat: 1000000,
2489                                 fee_base_msat: 10000,
2490                                 fee_proportional_millionths: 20,
2491                                 excess_data: [1; 3].to_vec()
2492                         };
2493                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
2494                         let valid_channel_update = ChannelUpdate {
2495                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
2496                                 contents: unsigned_channel_update.clone()
2497                         };
2498                         match router.handle_channel_update(&valid_channel_update) {
2499                                 Ok(_) => (),
2500                                 Err(_) => panic!()
2501                         };
2502                 }
2503
2504                 // Test that announcements with excess data won't be returned
2505                 let channels_with_announcements = router.get_next_channel_announcements(channel_key, 1);
2506                 assert_eq!(channels_with_announcements.len(), 1);
2507                 if let Some(channel_announcements) = channels_with_announcements.first() {
2508                         let &(_, ref update_1, ref update_2) = channel_announcements;
2509                         assert_eq!(update_1, &None);
2510                         assert_eq!(update_2, &None);
2511                 } else {
2512                         panic!();
2513                 }
2514
2515                 // Further starting point have no channels after it
2516                 let channels_with_announcements = router.get_next_channel_announcements(channel_key + 1000, 1);
2517                 assert_eq!(channels_with_announcements.len(), 0);
2518         }
2519
2520         #[test]
2521         fn getting_next_node_announcements() {
2522                 let (secp_ctx, _, router) = create_router();
2523                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
2524                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2525                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2526                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
2527                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
2528                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
2529
2530                 let short_channel_id = 1;
2531                 let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
2532
2533                 // No nodes yet.
2534                 let next_announcements = router.get_next_node_announcements(None, 10);
2535                 assert_eq!(next_announcements.len(), 0);
2536
2537                 {
2538                         // Announce a channel to add 2 nodes
2539                         let unsigned_announcement = UnsignedChannelAnnouncement {
2540                                 features: ChannelFeatures::empty(),
2541                                 chain_hash,
2542                                 short_channel_id,
2543                                 node_id_1,
2544                                 node_id_2,
2545                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
2546                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
2547                                 excess_data: Vec::new(),
2548                         };
2549
2550                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2551                         let valid_channel_announcement = ChannelAnnouncement {
2552                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2553                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2554                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2555                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2556                                 contents: unsigned_announcement.clone(),
2557                         };
2558                         match router.handle_channel_announcement(&valid_channel_announcement) {
2559                                 Ok(_) => (),
2560                                 Err(_) => panic!()
2561                         };
2562                 }
2563
2564
2565                 // Nodes were never announced
2566                 let next_announcements = router.get_next_node_announcements(None, 3);
2567                 assert_eq!(next_announcements.len(), 0);
2568
2569                 {
2570                         let mut unsigned_announcement = UnsignedNodeAnnouncement {
2571                                 features: NodeFeatures::supported(),
2572                                 timestamp: 1000,
2573                                 node_id: node_id_1,
2574                                 rgb: [0; 3],
2575                                 alias: [0; 32],
2576                                 addresses: Vec::new(),
2577                                 excess_address_data: Vec::new(),
2578                                 excess_data: Vec::new(),
2579                         };
2580                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2581                         let valid_announcement = NodeAnnouncement {
2582                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
2583                                 contents: unsigned_announcement.clone()
2584                         };
2585                         match router.handle_node_announcement(&valid_announcement) {
2586                                 Ok(_) => (),
2587                                 Err(_) => panic!()
2588                         };
2589
2590                         unsigned_announcement.node_id = node_id_2;
2591                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2592                         let valid_announcement = NodeAnnouncement {
2593                                 signature: secp_ctx.sign(&msghash, node_2_privkey),
2594                                 contents: unsigned_announcement.clone()
2595                         };
2596
2597                         match router.handle_node_announcement(&valid_announcement) {
2598                                 Ok(_) => (),
2599                                 Err(_) => panic!()
2600                         };
2601                 }
2602
2603                 let next_announcements = router.get_next_node_announcements(None, 3);
2604                 assert_eq!(next_announcements.len(), 2);
2605
2606                 // Skip the first node.
2607                 let next_announcements = router.get_next_node_announcements(Some(&node_id_1), 2);
2608                 assert_eq!(next_announcements.len(), 1);
2609
2610                 {
2611                         // Later announcement which should not be relayed (excess data) prevent us from sharing a node
2612                         let unsigned_announcement = UnsignedNodeAnnouncement {
2613                                 features: NodeFeatures::supported(),
2614                                 timestamp: 1010,
2615                                 node_id: node_id_2,
2616                                 rgb: [0; 3],
2617                                 alias: [0; 32],
2618                                 addresses: Vec::new(),
2619                                 excess_address_data: Vec::new(),
2620                                 excess_data: [1; 3].to_vec(),
2621                         };
2622                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2623                         let valid_announcement = NodeAnnouncement {
2624                                 signature: secp_ctx.sign(&msghash, node_2_privkey),
2625                                 contents: unsigned_announcement.clone()
2626                         };
2627                         match router.handle_node_announcement(&valid_announcement) {
2628                                 Ok(res) => assert!(!res),
2629                                 Err(_) => panic!()
2630                         };
2631                 }
2632
2633                 let next_announcements = router.get_next_node_announcements(Some(&node_id_1), 2);
2634                 assert_eq!(next_announcements.len(), 0);
2635
2636         }
2637 }