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