Merge pull request #294 from TheBlueMatt/2019-01-deps-bump
[rust-lightning] / src / ln / router.rs
1 //! The top-level routing/network map tracking logic lives here.
2 //!
3 //! You probably want to create a Router and use that as your RoutingMessageHandler and then
4 //! interrogate it to get routes for your own payments.
5
6 use secp256k1::key::PublicKey;
7 use secp256k1::Secp256k1;
8 use secp256k1;
9
10 use bitcoin::util::hash::Sha256dHash;
11 use bitcoin::blockdata::script::Builder;
12 use bitcoin::blockdata::opcodes;
13
14 use chain::chaininterface::{ChainError, ChainWatchInterface};
15 use ln::channelmanager;
16 use ln::msgs::{DecodeError,ErrorAction,HandleError,RoutingMessageHandler,NetAddress,GlobalFeatures};
17 use ln::msgs;
18 use util::ser::{Writeable, Readable};
19 use util::logger::Logger;
20
21 use std::cmp;
22 use std::sync::{RwLock,Arc};
23 use std::collections::{HashMap,BinaryHeap,BTreeMap};
24 use std::collections::btree_map::Entry as BtreeEntry;
25 use std;
26
27 /// A hop in a route
28 #[derive(Clone, PartialEq)]
29 pub struct RouteHop {
30         /// The node_id of the node at this hop.
31         pub pubkey: PublicKey,
32         /// The channel that should be used from the previous hop to reach this node.
33         pub short_channel_id: u64,
34         /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
35         pub fee_msat: u64,
36         /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
37         /// expected at the destination, in excess of the current block height.
38         pub cltv_expiry_delta: u32,
39 }
40
41 /// A route from us through the network to a destination
42 #[derive(Clone, PartialEq)]
43 pub struct Route {
44         /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
45         /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
46         /// length.
47         pub hops: Vec<RouteHop>,
48 }
49
50 impl Writeable for Route {
51         fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
52                 (self.hops.len() as u8).write(writer)?;
53                 for hop in self.hops.iter() {
54                         hop.pubkey.write(writer)?;
55                         hop.short_channel_id.write(writer)?;
56                         hop.fee_msat.write(writer)?;
57                         hop.cltv_expiry_delta.write(writer)?;
58                 }
59                 Ok(())
60         }
61 }
62
63 impl<R: ::std::io::Read> Readable<R> for Route {
64         fn read(reader: &mut R) -> Result<Route, DecodeError> {
65                 let hops_count: u8 = Readable::read(reader)?;
66                 let mut hops = Vec::with_capacity(hops_count as usize);
67                 for _ in 0..hops_count {
68                         hops.push(RouteHop {
69                                 pubkey: Readable::read(reader)?,
70                                 short_channel_id: Readable::read(reader)?,
71                                 fee_msat: Readable::read(reader)?,
72                                 cltv_expiry_delta: Readable::read(reader)?,
73                         });
74                 }
75                 Ok(Route {
76                         hops
77                 })
78         }
79 }
80
81 struct DirectionalChannelInfo {
82         src_node_id: PublicKey,
83         last_update: u32,
84         enabled: bool,
85         cltv_expiry_delta: u16,
86         htlc_minimum_msat: u64,
87         fee_base_msat: u32,
88         fee_proportional_millionths: u32,
89         last_update_message: Option<msgs::ChannelUpdate>,
90 }
91
92 impl std::fmt::Display for DirectionalChannelInfo {
93         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
94                 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)?;
95                 Ok(())
96         }
97 }
98
99 struct ChannelInfo {
100         features: GlobalFeatures,
101         one_to_two: DirectionalChannelInfo,
102         two_to_one: DirectionalChannelInfo,
103         //this is cached here so we can send out it later if required by route_init_sync
104         //keep an eye on this to see if the extra memory is a problem
105         announcement_message: Option<msgs::ChannelAnnouncement>,
106 }
107
108 impl std::fmt::Display for ChannelInfo {
109         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
110                 write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?;
111                 Ok(())
112         }
113 }
114
115 struct NodeInfo {
116         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
117         channels: Vec<(u64, Sha256dHash)>,
118         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
119         channels: Vec<u64>,
120
121         lowest_inbound_channel_fee_base_msat: u32,
122         lowest_inbound_channel_fee_proportional_millionths: u32,
123
124         features: GlobalFeatures,
125         last_update: u32,
126         rgb: [u8; 3],
127         alias: [u8; 32],
128         addresses: Vec<NetAddress>,
129         //this is cached here so we can send out it later if required by route_init_sync
130         //keep an eye on this to see if the extra memory is a problem
131         announcement_message: Option<msgs::NodeAnnouncement>,
132 }
133
134 impl std::fmt::Display for NodeInfo {
135         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
136                 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[..])?;
137                 Ok(())
138         }
139 }
140
141 struct NetworkMap {
142         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
143         channels: BTreeMap<(u64, Sha256dHash), ChannelInfo>,
144         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
145         channels: BTreeMap<u64, ChannelInfo>,
146
147         our_node_id: PublicKey,
148         nodes: BTreeMap<PublicKey, NodeInfo>,
149 }
150 struct MutNetworkMap<'a> {
151         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
152         channels: &'a mut BTreeMap<(u64, Sha256dHash), ChannelInfo>,
153         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
154         channels: &'a mut BTreeMap<u64, ChannelInfo>,
155         nodes: &'a mut BTreeMap<PublicKey, NodeInfo>,
156 }
157 impl NetworkMap {
158         fn borrow_parts(&mut self) -> MutNetworkMap {
159                 MutNetworkMap {
160                         channels: &mut self.channels,
161                         nodes: &mut self.nodes,
162                 }
163         }
164 }
165 impl std::fmt::Display for NetworkMap {
166         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
167                 write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
168                 for (key, val) in self.channels.iter() {
169                         write!(f, " {}: {}\n", key, val)?;
170                 }
171                 write!(f, "[Nodes]\n")?;
172                 for (key, val) in self.nodes.iter() {
173                         write!(f, " {}: {}\n", log_pubkey!(key), val)?;
174                 }
175                 Ok(())
176         }
177 }
178
179 impl NetworkMap {
180         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
181         #[inline]
182         fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
183                 (short_channel_id, chain_hash)
184         }
185
186         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
187         #[inline]
188         fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
189                 short_channel_id
190         }
191
192         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
193         #[inline]
194         fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
195                 &id.0
196         }
197
198         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
199         #[inline]
200         fn get_short_id(id: &u64) -> &u64 {
201                 id
202         }
203 }
204
205 /// A channel descriptor which provides a last-hop route to get_route
206 pub struct RouteHint {
207         /// The node_id of the non-target end of the route
208         pub src_node_id: PublicKey,
209         /// The short_channel_id of this channel
210         pub short_channel_id: u64,
211         /// The static msat-denominated fee which must be paid to use this channel
212         pub fee_base_msat: u32,
213         /// The dynamic proportional fee which must be paid to use this channel, denominated in
214         /// millionths of the value being forwarded to the next hop.
215         pub fee_proportional_millionths: u32,
216         /// The difference in CLTV values between this node and the next node.
217         pub cltv_expiry_delta: u16,
218         /// The minimum value, in msat, which must be relayed to the next hop.
219         pub htlc_minimum_msat: u64,
220 }
221
222 /// Tracks a view of the network, receiving updates from peers and generating Routes to
223 /// payment destinations.
224 pub struct Router {
225         secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
226         network_map: RwLock<NetworkMap>,
227         chain_monitor: Arc<ChainWatchInterface>,
228         logger: Arc<Logger>,
229 }
230
231 macro_rules! secp_verify_sig {
232         ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
233                 match $secp_ctx.verify($msg, $sig, $pubkey) {
234                         Ok(_) => {},
235                         Err(_) => return Err(HandleError{err: "Invalid signature from remote node", action: None}),
236                 }
237         };
238 }
239
240 impl RoutingMessageHandler for Router {
241         fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
242                 let msg_hash = hash_to_message!(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]);
243                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
244
245                 if msg.contents.features.requires_unknown_bits() {
246                         panic!("Unknown-required-features NodeAnnouncements should never deserialize!");
247                 }
248
249                 let mut network = self.network_map.write().unwrap();
250                 match network.nodes.get_mut(&msg.contents.node_id) {
251                         None => Err(HandleError{err: "No existing channels for node_announcement", action: Some(ErrorAction::IgnoreError)}),
252                         Some(node) => {
253                                 if node.last_update >= msg.contents.timestamp {
254                                         return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
255                                 }
256
257                                 node.features = msg.contents.features.clone();
258                                 node.last_update = msg.contents.timestamp;
259                                 node.rgb = msg.contents.rgb;
260                                 node.alias = msg.contents.alias;
261                                 node.addresses = msg.contents.addresses.clone();
262
263                                 let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty() && !msg.contents.features.supports_unknown_bits();
264                                 node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
265                                 Ok(should_relay)
266                         }
267                 }
268         }
269
270         fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
271                 if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
272                         return Err(HandleError{err: "Channel announcement node had a channel with itself", action: Some(ErrorAction::IgnoreError)});
273                 }
274
275                 let msg_hash = hash_to_message!(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]);
276                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
277                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
278                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
279                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
280
281                 if msg.contents.features.requires_unknown_bits() {
282                         panic!("Unknown-required-features ChannelAnnouncements should never deserialize!");
283                 }
284
285                 let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
286                         Ok((script_pubkey, _value)) => {
287                                 let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
288                                                                     .push_slice(&msg.contents.bitcoin_key_1.serialize())
289                                                                     .push_slice(&msg.contents.bitcoin_key_2.serialize())
290                                                                     .push_opcode(opcodes::all::OP_PUSHNUM_2)
291                                                                     .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
292                                 if script_pubkey != expected_script {
293                                         return Err(HandleError{err: "Channel announcement keys didn't match on-chain script", action: Some(ErrorAction::IgnoreError)});
294                                 }
295                                 //TODO: Check if value is worth storing, use it to inform routing, and compare it
296                                 //to the new HTLC max field in channel_update
297                                 true
298                         },
299                         Err(ChainError::NotSupported) => {
300                                 // Tentatively accept, potentially exposing us to DoS attacks
301                                 false
302                         },
303                         Err(ChainError::NotWatched) => {
304                                 return Err(HandleError{err: "Channel announced on an unknown chain", action: Some(ErrorAction::IgnoreError)});
305                         },
306                         Err(ChainError::UnknownTx) => {
307                                 return Err(HandleError{err: "Channel announced without corresponding UTXO entry", action: Some(ErrorAction::IgnoreError)});
308                         },
309                 };
310
311                 let mut network_lock = self.network_map.write().unwrap();
312                 let network = network_lock.borrow_parts();
313
314                 let should_relay = msg.contents.excess_data.is_empty() && !msg.contents.features.supports_unknown_bits();
315
316                 let chan_info = ChannelInfo {
317                                 features: msg.contents.features.clone(),
318                                 one_to_two: DirectionalChannelInfo {
319                                         src_node_id: msg.contents.node_id_1.clone(),
320                                         last_update: 0,
321                                         enabled: false,
322                                         cltv_expiry_delta: u16::max_value(),
323                                         htlc_minimum_msat: u64::max_value(),
324                                         fee_base_msat: u32::max_value(),
325                                         fee_proportional_millionths: u32::max_value(),
326                                         last_update_message: None,
327                                 },
328                                 two_to_one: DirectionalChannelInfo {
329                                         src_node_id: msg.contents.node_id_2.clone(),
330                                         last_update: 0,
331                                         enabled: false,
332                                         cltv_expiry_delta: u16::max_value(),
333                                         htlc_minimum_msat: u64::max_value(),
334                                         fee_base_msat: u32::max_value(),
335                                         fee_proportional_millionths: u32::max_value(),
336                                         last_update_message: None,
337                                 },
338                                 announcement_message: if should_relay { Some(msg.clone()) } else { None },
339                         };
340
341                 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
342                         BtreeEntry::Occupied(mut entry) => {
343                                 //TODO: because asking the blockchain if short_channel_id is valid is only optional
344                                 //in the blockchain API, we need to handle it smartly here, though its unclear
345                                 //exactly how...
346                                 if checked_utxo {
347                                         // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
348                                         // only sometimes returns results. In any case remove the previous entry. Note
349                                         // that the spec expects us to "blacklist" the node_ids involved, but we can't
350                                         // do that because
351                                         // a) we don't *require* a UTXO provider that always returns results.
352                                         // b) we don't track UTXOs of channels we know about and remove them if they
353                                         //    get reorg'd out.
354                                         // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
355                                         Self::remove_channel_in_nodes(network.nodes, &entry.get(), msg.contents.short_channel_id);
356                                         *entry.get_mut() = chan_info;
357                                 } else {
358                                         return Err(HandleError{err: "Already have knowledge of channel", action: Some(ErrorAction::IgnoreError)})
359                                 }
360                         },
361                         BtreeEntry::Vacant(entry) => {
362                                 entry.insert(chan_info);
363                         }
364                 };
365
366                 macro_rules! add_channel_to_node {
367                         ( $node_id: expr ) => {
368                                 match network.nodes.entry($node_id) {
369                                         BtreeEntry::Occupied(node_entry) => {
370                                                 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
371                                         },
372                                         BtreeEntry::Vacant(node_entry) => {
373                                                 node_entry.insert(NodeInfo {
374                                                         channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
375                                                         lowest_inbound_channel_fee_base_msat: u32::max_value(),
376                                                         lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
377                                                         features: GlobalFeatures::new(),
378                                                         last_update: 0,
379                                                         rgb: [0; 3],
380                                                         alias: [0; 32],
381                                                         addresses: Vec::new(),
382                                                         announcement_message: None,
383                                                 });
384                                         }
385                                 }
386                         };
387                 }
388
389                 add_channel_to_node!(msg.contents.node_id_1);
390                 add_channel_to_node!(msg.contents.node_id_2);
391
392                 Ok(should_relay)
393         }
394
395         fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
396                 match update {
397                         &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
398                                 let _ = self.handle_channel_update(msg);
399                         },
400                         &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
401                                 let mut network = self.network_map.write().unwrap();
402                                 if *is_permanent {
403                                         if let Some(chan) = network.channels.remove(short_channel_id) {
404                                                 Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
405                                         }
406                                 } else {
407                                         if let Some(chan) = network.channels.get_mut(short_channel_id) {
408                                                 chan.one_to_two.enabled = false;
409                                                 chan.two_to_one.enabled = false;
410                                         }
411                                 }
412                         },
413                         &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
414                                 if *is_permanent {
415                                         //TODO: Wholly remove the node
416                                 } else {
417                                         self.mark_node_bad(node_id, false);
418                                 }
419                         },
420                 }
421         }
422
423         fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
424                 let mut network = self.network_map.write().unwrap();
425                 let dest_node_id;
426                 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
427                 let chan_was_enabled;
428
429                 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
430                         None => return Err(HandleError{err: "Couldn't find channel for update", action: Some(ErrorAction::IgnoreError)}),
431                         Some(channel) => {
432                                 macro_rules! maybe_update_channel_info {
433                                         ( $target: expr) => {
434                                                 if $target.last_update >= msg.contents.timestamp {
435                                                         return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
436                                                 }
437                                                 chan_was_enabled = $target.enabled;
438                                                 $target.last_update = msg.contents.timestamp;
439                                                 $target.enabled = chan_enabled;
440                                                 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
441                                                 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
442                                                 $target.fee_base_msat = msg.contents.fee_base_msat;
443                                                 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
444                                                 $target.last_update_message = if msg.contents.excess_data.is_empty() {
445                                                         Some(msg.clone())
446                                                 } else {
447                                                         None
448                                                 };
449                                         }
450                                 }
451                                 let msg_hash = hash_to_message!(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]);
452                                 if msg.contents.flags & 1 == 1 {
453                                         dest_node_id = channel.one_to_two.src_node_id.clone();
454                                         secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
455                                         maybe_update_channel_info!(channel.two_to_one);
456                                 } else {
457                                         dest_node_id = channel.two_to_one.src_node_id.clone();
458                                         secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
459                                         maybe_update_channel_info!(channel.one_to_two);
460                                 }
461                         }
462                 }
463
464                 if chan_enabled {
465                         let node = network.nodes.get_mut(&dest_node_id).unwrap();
466                         node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
467                         node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
468                 } else if chan_was_enabled {
469                         let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
470                         let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
471
472                         {
473                                 let node = network.nodes.get(&dest_node_id).unwrap();
474
475                                 for chan_id in node.channels.iter() {
476                                         let chan = network.channels.get(chan_id).unwrap();
477                                         if chan.one_to_two.src_node_id == dest_node_id {
478                                                 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
479                                                 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
480                                         } else {
481                                                 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
482                                                 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
483                                         }
484                                 }
485                         }
486
487                         //TODO: satisfy the borrow-checker without a double-map-lookup :(
488                         let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
489                         mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
490                         mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
491                 }
492
493                 Ok(msg.contents.excess_data.is_empty())
494         }
495
496
497         fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
498                 let mut result = Vec::with_capacity(batch_amount as usize);
499                 let network = self.network_map.read().unwrap();
500                 let mut iter = network.channels.range(starting_point..);
501                 while result.len() < batch_amount as usize {
502                         if let Some((_, ref chan)) = iter.next() {
503                                 if chan.announcement_message.is_some() &&
504                                                 chan.one_to_two.last_update_message.is_some() &&
505                                                 chan.two_to_one.last_update_message.is_some() {
506                                         result.push((chan.announcement_message.clone().unwrap(),
507                                                 chan.one_to_two.last_update_message.clone().unwrap(),
508                                                 chan.two_to_one.last_update_message.clone().unwrap()));
509                                 } else {
510                                         // TODO: We may end up sending un-announced channel_updates if we are sending
511                                         // initial sync data while receiving announce/updates for this channel.
512                                 }
513                         } else {
514                                 return result;
515                         }
516                 }
517                 result
518         }
519
520         fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
521                 let mut result = Vec::with_capacity(batch_amount as usize);
522                 let network = self.network_map.read().unwrap();
523                 let mut iter = if let Some(pubkey) = starting_point {
524                                 let mut iter = network.nodes.range((*pubkey)..);
525                                 iter.next();
526                                 iter
527                         } else {
528                                 network.nodes.range(..)
529                         };
530                 while result.len() < batch_amount as usize {
531                         if let Some((_, ref node)) = iter.next() {
532                                 if node.announcement_message.is_some() {
533                                         result.push(node.announcement_message.clone().unwrap());
534                                 }
535                         } else {
536                                 return result;
537                         }
538                 }
539                 result
540         }
541 }
542
543 #[derive(Eq, PartialEq)]
544 struct RouteGraphNode {
545         pubkey: PublicKey,
546         lowest_fee_to_peer_through_node: u64,
547         lowest_fee_to_node: u64,
548 }
549
550 impl cmp::Ord for RouteGraphNode {
551         fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
552                 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
553                         .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
554         }
555 }
556
557 impl cmp::PartialOrd for RouteGraphNode {
558         fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
559                 Some(self.cmp(other))
560         }
561 }
562
563 struct DummyDirectionalChannelInfo {
564         src_node_id: PublicKey,
565         cltv_expiry_delta: u32,
566         htlc_minimum_msat: u64,
567         fee_base_msat: u32,
568         fee_proportional_millionths: u32,
569 }
570
571 impl Router {
572         /// Creates a new router with the given node_id to be used as the source for get_route()
573         pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
574                 let mut nodes = BTreeMap::new();
575                 nodes.insert(our_pubkey.clone(), NodeInfo {
576                         channels: Vec::new(),
577                         lowest_inbound_channel_fee_base_msat: u32::max_value(),
578                         lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
579                         features: GlobalFeatures::new(),
580                         last_update: 0,
581                         rgb: [0; 3],
582                         alias: [0; 32],
583                         addresses: Vec::new(),
584                         announcement_message: None,
585                 });
586                 Router {
587                         secp_ctx: Secp256k1::verification_only(),
588                         network_map: RwLock::new(NetworkMap {
589                                 channels: BTreeMap::new(),
590                                 our_node_id: our_pubkey,
591                                 nodes: nodes,
592                         }),
593                         chain_monitor,
594                         logger,
595                 }
596         }
597
598         /// Dumps the entire network view of this Router to the logger provided in the constructor at
599         /// level Trace
600         pub fn trace_state(&self) {
601                 log_trace!(self, "{}", self.network_map.read().unwrap());
602         }
603
604         /// Get network addresses by node id
605         pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
606                 let network = self.network_map.read().unwrap();
607                 network.nodes.get(pubkey).map(|n| n.addresses.clone())
608         }
609
610         /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
611         /// with an expotnential decay in node "badness". Note that there is deliberately no
612         /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
613         /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
614         /// boolean will reduce the penalty, returning the node to usability faster. If the node is
615         /// behaving correctly, it will disable the failing channel and we will use it again next time.
616         pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
617                 unimplemented!();
618         }
619
620         fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
621                 macro_rules! remove_from_node {
622                         ($node_id: expr) => {
623                                 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
624                                         entry.get_mut().channels.retain(|chan_id| {
625                                                 short_channel_id != *NetworkMap::get_short_id(chan_id)
626                                         });
627                                         if entry.get().channels.is_empty() {
628                                                 entry.remove_entry();
629                                         }
630                                 } else {
631                                         panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
632                                 }
633                         }
634                 }
635                 remove_from_node!(chan.one_to_two.src_node_id);
636                 remove_from_node!(chan.two_to_one.src_node_id);
637         }
638
639         /// Gets a route from us to the given target node.
640         ///
641         /// Extra routing hops between known nodes and the target will be used if they are included in
642         /// last_hops.
643         ///
644         /// If some channels aren't announced, it may be useful to fill in a first_hops with the
645         /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
646         /// (this Router's) view of our local channels will be ignored, and only those in first_hops
647         /// will be used.
648         ///
649         /// Panics if first_hops contains channels without short_channel_ids
650         /// (ChannelManager::list_usable_channels will never include such channels).
651         ///
652         /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
653         /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
654         /// *is* checked as they may change based on the receiving node.
655         pub fn get_route(&self, target: &PublicKey, first_hops: Option<&[channelmanager::ChannelDetails]>, last_hops: &[RouteHint], final_value_msat: u64, final_cltv: u32) -> Result<Route, HandleError> {
656                 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
657                 // uptime/success in using a node in the past.
658                 let network = self.network_map.read().unwrap();
659
660                 if *target == network.our_node_id {
661                         return Err(HandleError{err: "Cannot generate a route to ourselves", action: None});
662                 }
663
664                 if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
665                         return Err(HandleError{err: "Cannot generate a route of more value than all existing satoshis", action: None});
666                 }
667
668                 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
669                 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
670                 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
671                 // to use as the A* heuristic beyond just the cost to get one node further than the current
672                 // one.
673
674                 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
675                         src_node_id: network.our_node_id.clone(),
676                         cltv_expiry_delta: 0,
677                         htlc_minimum_msat: 0,
678                         fee_base_msat: 0,
679                         fee_proportional_millionths: 0,
680                 };
681
682                 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
683                 let mut dist = HashMap::with_capacity(network.nodes.len());
684
685                 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
686                 if let Some(hops) = first_hops {
687                         for chan in hops {
688                                 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
689                                 if chan.remote_network_id == *target {
690                                         return Ok(Route {
691                                                 hops: vec![RouteHop {
692                                                         pubkey: chan.remote_network_id,
693                                                         short_channel_id,
694                                                         fee_msat: final_value_msat,
695                                                         cltv_expiry_delta: final_cltv,
696                                                 }],
697                                         });
698                                 }
699                                 first_hop_targets.insert(chan.remote_network_id, short_channel_id);
700                         }
701                         if first_hop_targets.is_empty() {
702                                 return Err(HandleError{err: "Cannot route when there are no outbound routes away from us", action: None});
703                         }
704                 }
705
706                 macro_rules! add_entry {
707                         // Adds entry which goes from the node pointed to by $directional_info to
708                         // $dest_node_id over the channel with id $chan_id with fees described in
709                         // $directional_info.
710                         ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $starting_fee_msat: expr ) => {
711                                 //TODO: Explore simply adding fee to hit htlc_minimum_msat
712                                 if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
713                                         let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
714                                         if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
715                                                         ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
716                                         {
717                                                 let mut total_fee = $starting_fee_msat as u64;
718                                                 let hm_entry = dist.entry(&$directional_info.src_node_id);
719                                                 let old_entry = hm_entry.or_insert_with(|| {
720                                                         let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
721                                                         (u64::max_value(),
722                                                                 node.lowest_inbound_channel_fee_base_msat,
723                                                                 node.lowest_inbound_channel_fee_proportional_millionths,
724                                                                 RouteHop {
725                                                                         pubkey: $dest_node_id.clone(),
726                                                                         short_channel_id: 0,
727                                                                         fee_msat: 0,
728                                                                         cltv_expiry_delta: 0,
729                                                         })
730                                                 });
731                                                 if $directional_info.src_node_id != network.our_node_id {
732                                                         // Ignore new_fee for channel-from-us as we assume all channels-from-us
733                                                         // will have the same effective-fee
734                                                         total_fee += new_fee;
735                                                         if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
736                                                                 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
737                                                         } else {
738                                                                 // max_value means we'll always fail the old_entry.0 > total_fee check
739                                                                 total_fee = u64::max_value();
740                                                         }
741                                                 }
742                                                 let new_graph_node = RouteGraphNode {
743                                                         pubkey: $directional_info.src_node_id,
744                                                         lowest_fee_to_peer_through_node: total_fee,
745                                                         lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
746                                                 };
747                                                 if old_entry.0 > total_fee {
748                                                         targets.push(new_graph_node);
749                                                         old_entry.0 = total_fee;
750                                                         old_entry.3 = RouteHop {
751                                                                 pubkey: $dest_node_id.clone(),
752                                                                 short_channel_id: $chan_id.clone(),
753                                                                 fee_msat: new_fee, // This field is ignored on the last-hop anyway
754                                                                 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
755                                                         }
756                                                 }
757                                         }
758                                 }
759                         };
760                 }
761
762                 macro_rules! add_entries_to_cheapest_to_target_node {
763                         ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
764                                 if first_hops.is_some() {
765                                         if let Some(first_hop) = first_hop_targets.get(&$node_id) {
766                                                 add_entry!(first_hop, $node_id, dummy_directional_info, $fee_to_target_msat);
767                                         }
768                                 }
769
770                                 for chan_id in $node.channels.iter() {
771                                         let chan = network.channels.get(chan_id).unwrap();
772                                         if chan.one_to_two.src_node_id == *$node_id {
773                                                 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
774                                                 if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
775                                                         if chan.two_to_one.enabled {
776                                                                 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
777                                                         }
778                                                 }
779                                         } else {
780                                                 if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
781                                                         if chan.one_to_two.enabled {
782                                                                 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
783                                                         }
784                                                 }
785                                         }
786                                 }
787                         };
788                 }
789
790                 match network.nodes.get(target) {
791                         None => {},
792                         Some(node) => {
793                                 add_entries_to_cheapest_to_target_node!(node, target, 0);
794                         },
795                 }
796
797                 for hop in last_hops.iter() {
798                         if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
799                                 if network.nodes.get(&hop.src_node_id).is_some() {
800                                         if first_hops.is_some() {
801                                                 if let Some(first_hop) = first_hop_targets.get(&hop.src_node_id) {
802                                                         add_entry!(first_hop, hop.src_node_id, dummy_directional_info, 0);
803                                                 }
804                                         }
805                                         add_entry!(hop.short_channel_id, target, hop, 0);
806                                 }
807                         }
808                 }
809
810                 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
811                         if pubkey == network.our_node_id {
812                                 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
813                                 while res.last().unwrap().pubkey != *target {
814                                         let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
815                                                 Some(hop) => hop.3,
816                                                 None => return Err(HandleError{err: "Failed to find a non-fee-overflowing path to the given destination", action: None}),
817                                         };
818                                         res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
819                                         res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
820                                         res.push(new_entry);
821                                 }
822                                 res.last_mut().unwrap().fee_msat = final_value_msat;
823                                 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
824                                 let route = Route { hops: res };
825                                 log_trace!(self, "Got route: {}", log_route!(route));
826                                 return Ok(route);
827                         }
828
829                         match network.nodes.get(&pubkey) {
830                                 None => {},
831                                 Some(node) => {
832                                         add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
833                                 },
834                         }
835                 }
836
837                 Err(HandleError{err: "Failed to find a path to the given destination", action: None})
838         }
839 }
840
841 #[cfg(test)]
842 mod tests {
843         use chain::chaininterface;
844         use ln::channelmanager;
845         use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
846         use ln::msgs::GlobalFeatures;
847         use util::test_utils;
848         use util::logger::Logger;
849
850         use bitcoin::util::hash::Sha256dHash;
851         use bitcoin::network::constants::Network;
852
853         use hex;
854
855         use secp256k1::key::{PublicKey,SecretKey};
856         use secp256k1::Secp256k1;
857
858         use std::sync::Arc;
859
860         #[test]
861         fn route_test() {
862                 let secp_ctx = Secp256k1::new();
863                 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
864                 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
865                 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
866                 let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
867
868                 // Build network from our_id to node8:
869                 //
870                 //        -1(1)2-  node1  -1(3)2-
871                 //       /                       \
872                 // our_id -1(12)2- node8 -1(13)2--- node3
873                 //       \                       /
874                 //        -1(2)2-  node2  -1(4)2-
875                 //
876                 //
877                 // chan1  1-to-2: disabled
878                 // chan1  2-to-1: enabled, 0 fee
879                 //
880                 // chan2  1-to-2: enabled, ignored fee
881                 // chan2  2-to-1: enabled, 0 fee
882                 //
883                 // chan3  1-to-2: enabled, 0 fee
884                 // chan3  2-to-1: enabled, 100 msat fee
885                 //
886                 // chan4  1-to-2: enabled, 100% fee
887                 // chan4  2-to-1: enabled, 0 fee
888                 //
889                 // chan12 1-to-2: enabled, ignored fee
890                 // chan12 2-to-1: enabled, 0 fee
891                 //
892                 // chan13 1-to-2: enabled, 200% fee
893                 // chan13 2-to-1: enabled, 0 fee
894                 //
895                 //
896                 //       -1(5)2- node4 -1(8)2--
897                 //       |         2          |
898                 //       |       (11)         |
899                 //      /          1           \
900                 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
901                 //      \                      /
902                 //       -1(7)2- node6 -1(10)2-
903                 //
904                 // chan5  1-to-2: enabled, 100 msat fee
905                 // chan5  2-to-1: enabled, 0 fee
906                 //
907                 // chan6  1-to-2: enabled, 0 fee
908                 // chan6  2-to-1: enabled, 0 fee
909                 //
910                 // chan7  1-to-2: enabled, 100% fee
911                 // chan7  2-to-1: enabled, 0 fee
912                 //
913                 // chan8  1-to-2: enabled, variable fee (0 then 1000 msat)
914                 // chan8  2-to-1: enabled, 0 fee
915                 //
916                 // chan9  1-to-2: enabled, 1001 msat fee
917                 // chan9  2-to-1: enabled, 0 fee
918                 //
919                 // chan10 1-to-2: enabled, 0 fee
920                 // chan10 2-to-1: enabled, 0 fee
921                 //
922                 // chan11 1-to-2: enabled, 0 fee
923                 // chan11 2-to-1: enabled, 0 fee
924
925                 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
926                 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
927                 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
928                 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
929                 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
930                 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
931                 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
932                 let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
933
934                 let zero_hash = Sha256dHash::from_data(&[0; 32]);
935
936                 {
937                         let mut network = router.network_map.write().unwrap();
938
939                         network.nodes.insert(node1.clone(), NodeInfo {
940                                 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
941                                 lowest_inbound_channel_fee_base_msat: 100,
942                                 lowest_inbound_channel_fee_proportional_millionths: 0,
943                                 features: GlobalFeatures::new(),
944                                 last_update: 1,
945                                 rgb: [0; 3],
946                                 alias: [0; 32],
947                                 addresses: Vec::new(),
948                                 announcement_message: None,
949                         });
950                         network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
951                                 features: GlobalFeatures::new(),
952                                 one_to_two: DirectionalChannelInfo {
953                                         src_node_id: our_id.clone(),
954                                         last_update: 0,
955                                         enabled: false,
956                                         cltv_expiry_delta: u16::max_value(), // This value should be ignored
957                                         htlc_minimum_msat: 0,
958                                         fee_base_msat: u32::max_value(), // This value should be ignored
959                                         fee_proportional_millionths: u32::max_value(), // This value should be ignored
960                                         last_update_message: None,
961                                 }, two_to_one: DirectionalChannelInfo {
962                                         src_node_id: node1.clone(),
963                                         last_update: 0,
964                                         enabled: true,
965                                         cltv_expiry_delta: 0,
966                                         htlc_minimum_msat: 0,
967                                         fee_base_msat: 0,
968                                         fee_proportional_millionths: 0,
969                                         last_update_message: None,
970                                 },
971                                 announcement_message: None,
972                         });
973                         network.nodes.insert(node2.clone(), NodeInfo {
974                                 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
975                                 lowest_inbound_channel_fee_base_msat: 0,
976                                 lowest_inbound_channel_fee_proportional_millionths: 0,
977                                 features: GlobalFeatures::new(),
978                                 last_update: 1,
979                                 rgb: [0; 3],
980                                 alias: [0; 32],
981                                 addresses: Vec::new(),
982                                 announcement_message: None,
983                         });
984                         network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
985                                 features: GlobalFeatures::new(),
986                                 one_to_two: DirectionalChannelInfo {
987                                         src_node_id: our_id.clone(),
988                                         last_update: 0,
989                                         enabled: true,
990                                         cltv_expiry_delta: u16::max_value(), // This value should be ignored
991                                         htlc_minimum_msat: 0,
992                                         fee_base_msat: u32::max_value(), // This value should be ignored
993                                         fee_proportional_millionths: u32::max_value(), // This value should be ignored
994                                         last_update_message: None,
995                                 }, two_to_one: DirectionalChannelInfo {
996                                         src_node_id: node2.clone(),
997                                         last_update: 0,
998                                         enabled: true,
999                                         cltv_expiry_delta: 0,
1000                                         htlc_minimum_msat: 0,
1001                                         fee_base_msat: 0,
1002                                         fee_proportional_millionths: 0,
1003                                         last_update_message: None,
1004                                 },
1005                                 announcement_message: None,
1006                         });
1007                         network.nodes.insert(node8.clone(), NodeInfo {
1008                                 channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
1009                                 lowest_inbound_channel_fee_base_msat: 0,
1010                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1011                                 features: GlobalFeatures::new(),
1012                                 last_update: 1,
1013                                 rgb: [0; 3],
1014                                 alias: [0; 32],
1015                                 addresses: Vec::new(),
1016                                 announcement_message: None,
1017                         });
1018                         network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
1019                                 features: GlobalFeatures::new(),
1020                                 one_to_two: DirectionalChannelInfo {
1021                                         src_node_id: our_id.clone(),
1022                                         last_update: 0,
1023                                         enabled: true,
1024                                         cltv_expiry_delta: u16::max_value(), // This value should be ignored
1025                                         htlc_minimum_msat: 0,
1026                                         fee_base_msat: u32::max_value(), // This value should be ignored
1027                                         fee_proportional_millionths: u32::max_value(), // This value should be ignored
1028                                         last_update_message: None,
1029                                 }, two_to_one: DirectionalChannelInfo {
1030                                         src_node_id: node8.clone(),
1031                                         last_update: 0,
1032                                         enabled: true,
1033                                         cltv_expiry_delta: 0,
1034                                         htlc_minimum_msat: 0,
1035                                         fee_base_msat: 0,
1036                                         fee_proportional_millionths: 0,
1037                                         last_update_message: None,
1038                                 },
1039                                 announcement_message: None,
1040                         });
1041                         network.nodes.insert(node3.clone(), NodeInfo {
1042                                 channels: vec!(
1043                                         NetworkMap::get_key(3, zero_hash.clone()),
1044                                         NetworkMap::get_key(4, zero_hash.clone()),
1045                                         NetworkMap::get_key(13, zero_hash.clone()),
1046                                         NetworkMap::get_key(5, zero_hash.clone()),
1047                                         NetworkMap::get_key(6, zero_hash.clone()),
1048                                         NetworkMap::get_key(7, zero_hash.clone())),
1049                                 lowest_inbound_channel_fee_base_msat: 0,
1050                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1051                                 features: GlobalFeatures::new(),
1052                                 last_update: 1,
1053                                 rgb: [0; 3],
1054                                 alias: [0; 32],
1055                                 addresses: Vec::new(),
1056                                 announcement_message: None,
1057                         });
1058                         network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
1059                                 features: GlobalFeatures::new(),
1060                                 one_to_two: DirectionalChannelInfo {
1061                                         src_node_id: node1.clone(),
1062                                         last_update: 0,
1063                                         enabled: true,
1064                                         cltv_expiry_delta: (3 << 8) | 1,
1065                                         htlc_minimum_msat: 0,
1066                                         fee_base_msat: 0,
1067                                         fee_proportional_millionths: 0,
1068                                         last_update_message: None,
1069                                 }, two_to_one: DirectionalChannelInfo {
1070                                         src_node_id: node3.clone(),
1071                                         last_update: 0,
1072                                         enabled: true,
1073                                         cltv_expiry_delta: (3 << 8) | 2,
1074                                         htlc_minimum_msat: 0,
1075                                         fee_base_msat: 100,
1076                                         fee_proportional_millionths: 0,
1077                                         last_update_message: None,
1078                                 },
1079                                 announcement_message: None,
1080                         });
1081                         network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
1082                                 features: GlobalFeatures::new(),
1083                                 one_to_two: DirectionalChannelInfo {
1084                                         src_node_id: node2.clone(),
1085                                         last_update: 0,
1086                                         enabled: true,
1087                                         cltv_expiry_delta: (4 << 8) | 1,
1088                                         htlc_minimum_msat: 0,
1089                                         fee_base_msat: 0,
1090                                         fee_proportional_millionths: 1000000,
1091                                         last_update_message: None,
1092                                 }, two_to_one: DirectionalChannelInfo {
1093                                         src_node_id: node3.clone(),
1094                                         last_update: 0,
1095                                         enabled: true,
1096                                         cltv_expiry_delta: (4 << 8) | 2,
1097                                         htlc_minimum_msat: 0,
1098                                         fee_base_msat: 0,
1099                                         fee_proportional_millionths: 0,
1100                                         last_update_message: None,
1101                                 },
1102                                 announcement_message: None,
1103                         });
1104                         network.channels.insert(NetworkMap::get_key(13, zero_hash.clone()), ChannelInfo {
1105                                 features: GlobalFeatures::new(),
1106                                 one_to_two: DirectionalChannelInfo {
1107                                         src_node_id: node8.clone(),
1108                                         last_update: 0,
1109                                         enabled: true,
1110                                         cltv_expiry_delta: (13 << 8) | 1,
1111                                         htlc_minimum_msat: 0,
1112                                         fee_base_msat: 0,
1113                                         fee_proportional_millionths: 2000000,
1114                                         last_update_message: None,
1115                                 }, two_to_one: DirectionalChannelInfo {
1116                                         src_node_id: node3.clone(),
1117                                         last_update: 0,
1118                                         enabled: true,
1119                                         cltv_expiry_delta: (13 << 8) | 2,
1120                                         htlc_minimum_msat: 0,
1121                                         fee_base_msat: 0,
1122                                         fee_proportional_millionths: 0,
1123                                         last_update_message: None,
1124                                 },
1125                                 announcement_message: None,
1126                         });
1127                         network.nodes.insert(node4.clone(), NodeInfo {
1128                                 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1129                                 lowest_inbound_channel_fee_base_msat: 0,
1130                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1131                                 features: GlobalFeatures::new(),
1132                                 last_update: 1,
1133                                 rgb: [0; 3],
1134                                 alias: [0; 32],
1135                                 addresses: Vec::new(),
1136                                 announcement_message: None,
1137                         });
1138                         network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
1139                                 features: GlobalFeatures::new(),
1140                                 one_to_two: DirectionalChannelInfo {
1141                                         src_node_id: node3.clone(),
1142                                         last_update: 0,
1143                                         enabled: true,
1144                                         cltv_expiry_delta: (5 << 8) | 1,
1145                                         htlc_minimum_msat: 0,
1146                                         fee_base_msat: 100,
1147                                         fee_proportional_millionths: 0,
1148                                         last_update_message: None,
1149                                 }, two_to_one: DirectionalChannelInfo {
1150                                         src_node_id: node4.clone(),
1151                                         last_update: 0,
1152                                         enabled: true,
1153                                         cltv_expiry_delta: (5 << 8) | 2,
1154                                         htlc_minimum_msat: 0,
1155                                         fee_base_msat: 0,
1156                                         fee_proportional_millionths: 0,
1157                                         last_update_message: None,
1158                                 },
1159                                 announcement_message: None,
1160                         });
1161                         network.nodes.insert(node5.clone(), NodeInfo {
1162                                 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1163                                 lowest_inbound_channel_fee_base_msat: 0,
1164                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1165                                 features: GlobalFeatures::new(),
1166                                 last_update: 1,
1167                                 rgb: [0; 3],
1168                                 alias: [0; 32],
1169                                 addresses: Vec::new(),
1170                                 announcement_message: None,
1171                         });
1172                         network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
1173                                 features: GlobalFeatures::new(),
1174                                 one_to_two: DirectionalChannelInfo {
1175                                         src_node_id: node3.clone(),
1176                                         last_update: 0,
1177                                         enabled: true,
1178                                         cltv_expiry_delta: (6 << 8) | 1,
1179                                         htlc_minimum_msat: 0,
1180                                         fee_base_msat: 0,
1181                                         fee_proportional_millionths: 0,
1182                                         last_update_message: None,
1183                                 }, two_to_one: DirectionalChannelInfo {
1184                                         src_node_id: node5.clone(),
1185                                         last_update: 0,
1186                                         enabled: true,
1187                                         cltv_expiry_delta: (6 << 8) | 2,
1188                                         htlc_minimum_msat: 0,
1189                                         fee_base_msat: 0,
1190                                         fee_proportional_millionths: 0,
1191                                         last_update_message: None,
1192                                 },
1193                                 announcement_message: None,
1194                         });
1195                         network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
1196                                 features: GlobalFeatures::new(),
1197                                 one_to_two: DirectionalChannelInfo {
1198                                         src_node_id: node5.clone(),
1199                                         last_update: 0,
1200                                         enabled: true,
1201                                         cltv_expiry_delta: (11 << 8) | 1,
1202                                         htlc_minimum_msat: 0,
1203                                         fee_base_msat: 0,
1204                                         fee_proportional_millionths: 0,
1205                                         last_update_message: None,
1206                                 }, two_to_one: DirectionalChannelInfo {
1207                                         src_node_id: node4.clone(),
1208                                         last_update: 0,
1209                                         enabled: true,
1210                                         cltv_expiry_delta: (11 << 8) | 2,
1211                                         htlc_minimum_msat: 0,
1212                                         fee_base_msat: 0,
1213                                         fee_proportional_millionths: 0,
1214                                         last_update_message: None,
1215                                 },
1216                                 announcement_message: None,
1217                         });
1218                         network.nodes.insert(node6.clone(), NodeInfo {
1219                                 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
1220                                 lowest_inbound_channel_fee_base_msat: 0,
1221                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1222                                 features: GlobalFeatures::new(),
1223                                 last_update: 1,
1224                                 rgb: [0; 3],
1225                                 alias: [0; 32],
1226                                 addresses: Vec::new(),
1227                                 announcement_message: None,
1228                         });
1229                         network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
1230                                 features: GlobalFeatures::new(),
1231                                 one_to_two: DirectionalChannelInfo {
1232                                         src_node_id: node3.clone(),
1233                                         last_update: 0,
1234                                         enabled: true,
1235                                         cltv_expiry_delta: (7 << 8) | 1,
1236                                         htlc_minimum_msat: 0,
1237                                         fee_base_msat: 0,
1238                                         fee_proportional_millionths: 1000000,
1239                                         last_update_message: None,
1240                                 }, two_to_one: DirectionalChannelInfo {
1241                                         src_node_id: node6.clone(),
1242                                         last_update: 0,
1243                                         enabled: true,
1244                                         cltv_expiry_delta: (7 << 8) | 2,
1245                                         htlc_minimum_msat: 0,
1246                                         fee_base_msat: 0,
1247                                         fee_proportional_millionths: 0,
1248                                         last_update_message: None,
1249                                 },
1250                                 announcement_message: None,
1251                         });
1252                 }
1253
1254                 { // Simple route to 3 via 2
1255                         let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
1256                         assert_eq!(route.hops.len(), 2);
1257
1258                         assert_eq!(route.hops[0].pubkey, node2);
1259                         assert_eq!(route.hops[0].short_channel_id, 2);
1260                         assert_eq!(route.hops[0].fee_msat, 100);
1261                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1262
1263                         assert_eq!(route.hops[1].pubkey, node3);
1264                         assert_eq!(route.hops[1].short_channel_id, 4);
1265                         assert_eq!(route.hops[1].fee_msat, 100);
1266                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1267                 }
1268
1269                 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
1270                         let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
1271                         assert_eq!(route.hops.len(), 3);
1272
1273                         assert_eq!(route.hops[0].pubkey, node2);
1274                         assert_eq!(route.hops[0].short_channel_id, 2);
1275                         assert_eq!(route.hops[0].fee_msat, 200);
1276                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1277
1278                         assert_eq!(route.hops[1].pubkey, node3);
1279                         assert_eq!(route.hops[1].short_channel_id, 4);
1280                         assert_eq!(route.hops[1].fee_msat, 100);
1281                         assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
1282
1283                         assert_eq!(route.hops[2].pubkey, node1);
1284                         assert_eq!(route.hops[2].short_channel_id, 3);
1285                         assert_eq!(route.hops[2].fee_msat, 100);
1286                         assert_eq!(route.hops[2].cltv_expiry_delta, 42);
1287                 }
1288
1289                 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1290                         let our_chans = vec![channelmanager::ChannelDetails {
1291                                 channel_id: [0; 32],
1292                                 short_channel_id: Some(42),
1293                                 remote_network_id: node8.clone(),
1294                                 channel_value_satoshis: 0,
1295                                 user_id: 0,
1296                         }];
1297                         let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1298                         assert_eq!(route.hops.len(), 2);
1299
1300                         assert_eq!(route.hops[0].pubkey, node8);
1301                         assert_eq!(route.hops[0].short_channel_id, 42);
1302                         assert_eq!(route.hops[0].fee_msat, 200);
1303                         assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1304
1305                         assert_eq!(route.hops[1].pubkey, node3);
1306                         assert_eq!(route.hops[1].short_channel_id, 13);
1307                         assert_eq!(route.hops[1].fee_msat, 100);
1308                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1309                 }
1310
1311                 let mut last_hops = vec!(RouteHint {
1312                                 src_node_id: node4.clone(),
1313                                 short_channel_id: 8,
1314                                 fee_base_msat: 0,
1315                                 fee_proportional_millionths: 0,
1316                                 cltv_expiry_delta: (8 << 8) | 1,
1317                                 htlc_minimum_msat: 0,
1318                         }, RouteHint {
1319                                 src_node_id: node5.clone(),
1320                                 short_channel_id: 9,
1321                                 fee_base_msat: 1001,
1322                                 fee_proportional_millionths: 0,
1323                                 cltv_expiry_delta: (9 << 8) | 1,
1324                                 htlc_minimum_msat: 0,
1325                         }, RouteHint {
1326                                 src_node_id: node6.clone(),
1327                                 short_channel_id: 10,
1328                                 fee_base_msat: 0,
1329                                 fee_proportional_millionths: 0,
1330                                 cltv_expiry_delta: (10 << 8) | 1,
1331                                 htlc_minimum_msat: 0,
1332                         });
1333
1334                 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
1335                         let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1336                         assert_eq!(route.hops.len(), 5);
1337
1338                         assert_eq!(route.hops[0].pubkey, node2);
1339                         assert_eq!(route.hops[0].short_channel_id, 2);
1340                         assert_eq!(route.hops[0].fee_msat, 100);
1341                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1342
1343                         assert_eq!(route.hops[1].pubkey, node3);
1344                         assert_eq!(route.hops[1].short_channel_id, 4);
1345                         assert_eq!(route.hops[1].fee_msat, 0);
1346                         assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1347
1348                         assert_eq!(route.hops[2].pubkey, node5);
1349                         assert_eq!(route.hops[2].short_channel_id, 6);
1350                         assert_eq!(route.hops[2].fee_msat, 0);
1351                         assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1352
1353                         assert_eq!(route.hops[3].pubkey, node4);
1354                         assert_eq!(route.hops[3].short_channel_id, 11);
1355                         assert_eq!(route.hops[3].fee_msat, 0);
1356                         assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1357
1358                         assert_eq!(route.hops[4].pubkey, node7);
1359                         assert_eq!(route.hops[4].short_channel_id, 8);
1360                         assert_eq!(route.hops[4].fee_msat, 100);
1361                         assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1362                 }
1363
1364                 { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1365                         let our_chans = vec![channelmanager::ChannelDetails {
1366                                 channel_id: [0; 32],
1367                                 short_channel_id: Some(42),
1368                                 remote_network_id: node4.clone(),
1369                                 channel_value_satoshis: 0,
1370                                 user_id: 0,
1371                         }];
1372                         let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
1373                         assert_eq!(route.hops.len(), 2);
1374
1375                         assert_eq!(route.hops[0].pubkey, node4);
1376                         assert_eq!(route.hops[0].short_channel_id, 42);
1377                         assert_eq!(route.hops[0].fee_msat, 0);
1378                         assert_eq!(route.hops[0].cltv_expiry_delta, (8 << 8) | 1);
1379
1380                         assert_eq!(route.hops[1].pubkey, node7);
1381                         assert_eq!(route.hops[1].short_channel_id, 8);
1382                         assert_eq!(route.hops[1].fee_msat, 100);
1383                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1384                 }
1385
1386                 last_hops[0].fee_base_msat = 1000;
1387
1388                 { // Revert to via 6 as the fee on 8 goes up
1389                         let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1390                         assert_eq!(route.hops.len(), 4);
1391
1392                         assert_eq!(route.hops[0].pubkey, node2);
1393                         assert_eq!(route.hops[0].short_channel_id, 2);
1394                         assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
1395                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1396
1397                         assert_eq!(route.hops[1].pubkey, node3);
1398                         assert_eq!(route.hops[1].short_channel_id, 4);
1399                         assert_eq!(route.hops[1].fee_msat, 100);
1400                         assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
1401
1402                         assert_eq!(route.hops[2].pubkey, node6);
1403                         assert_eq!(route.hops[2].short_channel_id, 7);
1404                         assert_eq!(route.hops[2].fee_msat, 0);
1405                         assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
1406
1407                         assert_eq!(route.hops[3].pubkey, node7);
1408                         assert_eq!(route.hops[3].short_channel_id, 10);
1409                         assert_eq!(route.hops[3].fee_msat, 100);
1410                         assert_eq!(route.hops[3].cltv_expiry_delta, 42);
1411                 }
1412
1413                 { // ...but still use 8 for larger payments as 6 has a variable feerate
1414                         let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
1415                         assert_eq!(route.hops.len(), 5);
1416
1417                         assert_eq!(route.hops[0].pubkey, node2);
1418                         assert_eq!(route.hops[0].short_channel_id, 2);
1419                         assert_eq!(route.hops[0].fee_msat, 3000);
1420                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1421
1422                         assert_eq!(route.hops[1].pubkey, node3);
1423                         assert_eq!(route.hops[1].short_channel_id, 4);
1424                         assert_eq!(route.hops[1].fee_msat, 0);
1425                         assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1426
1427                         assert_eq!(route.hops[2].pubkey, node5);
1428                         assert_eq!(route.hops[2].short_channel_id, 6);
1429                         assert_eq!(route.hops[2].fee_msat, 0);
1430                         assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1431
1432                         assert_eq!(route.hops[3].pubkey, node4);
1433                         assert_eq!(route.hops[3].short_channel_id, 11);
1434                         assert_eq!(route.hops[3].fee_msat, 1000);
1435                         assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1436
1437                         assert_eq!(route.hops[4].pubkey, node7);
1438                         assert_eq!(route.hops[4].short_channel_id, 8);
1439                         assert_eq!(route.hops[4].fee_msat, 2000);
1440                         assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1441                 }
1442         }
1443 }