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