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