Use ChainWatchInterfaceUtil directly in tests instead of a wrapper
[rust-lightning] / src / ln / router.rs
1 use secp256k1::key::PublicKey;
2 use secp256k1::{Secp256k1,Message};
3
4 use bitcoin::util::hash::Sha256dHash;
5
6 use ln::msgs::{HandleError,RoutingMessageHandler,MsgEncodable,NetAddress,GlobalFeatures};
7 use ln::msgs;
8
9 use std::cmp;
10 use std::sync::RwLock;
11 use std::collections::{HashMap,BinaryHeap};
12 use std::collections::hash_map::Entry;
13
14 /// A hop in a route
15 pub struct RouteHop {
16         pub pubkey: PublicKey,
17         /// The channel that should be used from the previous hop to reach this node.
18         pub short_channel_id: u64,
19         /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
20         pub fee_msat: u64,
21         /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
22         /// expected at the destination, NOT a delta.
23         pub cltv_expiry_delta: u32,
24 }
25
26 /// A route from us through the network to a destination
27 pub struct Route {
28         /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
29         /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
30         /// length.
31         pub hops: Vec<RouteHop>,
32 }
33
34 struct DirectionalChannelInfo {
35         src_node_id: PublicKey,
36         last_update: u32,
37         enabled: bool,
38         cltv_expiry_delta: u16,
39         htlc_minimum_msat: u64,
40         fee_base_msat: u32,
41         fee_proportional_millionths: u32,
42 }
43
44 struct ChannelInfo {
45         features: GlobalFeatures,
46         one_to_two: DirectionalChannelInfo,
47         two_to_one: DirectionalChannelInfo,
48 }
49
50 struct NodeInfo {
51         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
52     channels: Vec<(u64, Sha256dHash)>,
53         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
54     channels: Vec<u64>,
55
56         lowest_inbound_channel_fee_base_msat: u32,
57         lowest_inbound_channel_fee_proportional_millionths: u32,
58
59         features: GlobalFeatures,
60         last_update: u32,
61         rgb: [u8; 3],
62         alias: [u8; 32],
63         addresses: Vec<NetAddress>,
64 }
65
66 struct NetworkMap {
67         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
68         channels: HashMap<(u64, Sha256dHash), ChannelInfo>,
69         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
70         channels: HashMap<u64, ChannelInfo>,
71
72         our_node_id: PublicKey,
73         nodes: HashMap<PublicKey, NodeInfo>,
74 }
75
76 impl NetworkMap {
77         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
78         #[inline]
79         fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
80                 (short_channel_id, chain_hash)
81         }
82
83         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
84         #[inline]
85         fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
86                 short_channel_id
87         }
88 }
89
90 /// A channel descriptor which provides a last-hop route to get_route
91 pub struct RouteHint {
92         pub src_node_id: PublicKey,
93         pub short_channel_id: u64,
94         pub fee_base_msat: u64,
95         pub fee_proportional_millionths: u32,
96         pub cltv_expiry_delta: u16,
97         pub htlc_minimum_msat: u64,
98 }
99
100 /// Tracks a view of the network, receiving updates from peers and generating Routes to
101 /// payment destinations.
102 pub struct Router {
103         secp_ctx: Secp256k1,
104         network_map: RwLock<NetworkMap>,
105 }
106
107 macro_rules! secp_verify_sig {
108         ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
109                 match $secp_ctx.verify($msg, $sig, $pubkey) {
110                         Ok(_) => {},
111                         Err(_) => return Err(HandleError{err: "Invalid signature from remote node", msg: None}),
112                 }
113         };
114 }
115
116 impl RoutingMessageHandler for Router {
117         fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<(), HandleError> {
118                 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
119                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
120
121                 let mut network = self.network_map.write().unwrap();
122                 match network.nodes.get_mut(&msg.contents.node_id) {
123                         None => Err(HandleError{err: "No existing channels for node_announcement", msg: None}),
124                         Some(node) => {
125                                 if node.last_update >= msg.contents.timestamp {
126                                         return Err(HandleError{err: "Update older than last processed update", msg: None});
127                                 }
128
129                                 node.features = msg.contents.features.clone();
130                                 node.last_update = msg.contents.timestamp;
131                                 node.rgb = msg.contents.rgb;
132                                 node.alias = msg.contents.alias;
133                                 node.addresses = msg.contents.addresses.clone();
134                                 Ok(())
135                         }
136                 }
137         }
138
139         fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
140                 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
141                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
142                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
143                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
144                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
145
146                 //TODO: Call blockchain thing to ask if the short_channel_id is valid
147                 //TODO: Only allow bitcoin chain_hash
148
149                 if msg.contents.features.requires_unknown_bits() {
150                         return Err(HandleError{err: "Channel announcement required unknown feature flags", msg: None});
151                 }
152
153                 let mut network = self.network_map.write().unwrap();
154
155                 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
156                         Entry::Occupied(_) => {
157                                 //TODO: because asking the blockchain if short_channel_id is valid is only optional
158                                 //in the blockchain API, we need to handle it smartly here, though its unclear
159                                 //exactly how...
160                                 return Err(HandleError{err: "Already have knowledge of channel", msg: None})
161                         },
162                         Entry::Vacant(entry) => {
163                                 entry.insert(ChannelInfo {
164                                         features: msg.contents.features.clone(),
165                                         one_to_two: DirectionalChannelInfo {
166                                                 src_node_id: msg.contents.node_id_1.clone(),
167                                                 last_update: 0,
168                                                 enabled: false,
169                                                 cltv_expiry_delta: u16::max_value(),
170                                                 htlc_minimum_msat: u64::max_value(),
171                                                 fee_base_msat: u32::max_value(),
172                                                 fee_proportional_millionths: u32::max_value(),
173                                         },
174                                         two_to_one: DirectionalChannelInfo {
175                                                 src_node_id: msg.contents.node_id_2.clone(),
176                                                 last_update: 0,
177                                                 enabled: false,
178                                                 cltv_expiry_delta: u16::max_value(),
179                                                 htlc_minimum_msat: u64::max_value(),
180                                                 fee_base_msat: u32::max_value(),
181                                                 fee_proportional_millionths: u32::max_value(),
182                                         }
183                                 });
184                         }
185                 };
186
187                 macro_rules! add_channel_to_node {
188                         ( $node_id: expr ) => {
189                                 match network.nodes.entry($node_id) {
190                                         Entry::Occupied(node_entry) => {
191                                                 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
192                                         },
193                                         Entry::Vacant(node_entry) => {
194                                                 node_entry.insert(NodeInfo {
195                                                         channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
196                                                         lowest_inbound_channel_fee_base_msat: u32::max_value(),
197                                                         lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
198                                                         features: GlobalFeatures::new(),
199                                                         last_update: 0,
200                                                         rgb: [0; 3],
201                                                         alias: [0; 32],
202                                                         addresses: Vec::new(),
203                                                 });
204                                         }
205                                 }
206                         };
207                 }
208
209                 add_channel_to_node!(msg.contents.node_id_1);
210                 add_channel_to_node!(msg.contents.node_id_2);
211
212                 Ok(!msg.contents.features.supports_unknown_bits())
213         }
214
215         fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<(), HandleError> {
216                 let mut network = self.network_map.write().unwrap();
217                 let dest_node_id;
218                 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
219                 let chan_was_enabled;
220
221                 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
222                         None => return Err(HandleError{err: "Couldn't find channel for update", msg: None}),
223                         Some(channel) => {
224                                 macro_rules! maybe_update_channel_info {
225                                         ( $target: expr) => {
226                                                 if $target.last_update >= msg.contents.timestamp {
227                                                         return Err(HandleError{err: "Update older than last processed update", msg: None});
228                                                 }
229                                                 chan_was_enabled = $target.enabled;
230                                                 $target.last_update = msg.contents.timestamp;
231                                                 $target.enabled = chan_enabled;
232                                                 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
233                                                 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
234                                                 $target.fee_base_msat = msg.contents.fee_base_msat;
235                                                 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
236                                         }
237                                 }
238
239                                 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
240                                 if msg.contents.flags & 1 == 1 {
241                                         dest_node_id = channel.one_to_two.src_node_id.clone();
242                                         secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
243                                         maybe_update_channel_info!(channel.two_to_one);
244                                 } else {
245                                         dest_node_id = channel.two_to_one.src_node_id.clone();
246                                         secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
247                                         maybe_update_channel_info!(channel.one_to_two);
248                                 }
249                         }
250                 }
251
252                 if chan_enabled {
253                         let node = network.nodes.get_mut(&dest_node_id).unwrap();
254                         node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
255                         node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
256                 } else if chan_was_enabled {
257                         let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
258                         let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
259
260                         {
261                                 let node = network.nodes.get(&dest_node_id).unwrap();
262
263                                 for chan_id in node.channels.iter() {
264                                         let chan = network.channels.get(chan_id).unwrap();
265                                         if chan.one_to_two.src_node_id == dest_node_id {
266                                                 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
267                                                 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
268                                         } else {
269                                                 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
270                                                 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
271                                         }
272                                 }
273                         }
274
275                         //TODO: satisfy the borrow-checker without a double-map-lookup :(
276                         let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
277                         mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
278                         mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
279                 }
280
281                 Ok(())
282         }
283 }
284
285 #[derive(Eq, PartialEq)]
286 struct RouteGraphNode {
287         pubkey: PublicKey,
288         lowest_fee_to_peer_through_node: u64,
289 }
290
291 impl cmp::Ord for RouteGraphNode {
292         fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
293                 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
294                         .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
295         }
296 }
297
298 impl cmp::PartialOrd for RouteGraphNode {
299         fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
300                 Some(self.cmp(other))
301         }
302 }
303
304 impl Router {
305         pub fn new(our_pubkey: PublicKey) -> Router {
306                 let mut nodes = HashMap::new();
307                 nodes.insert(our_pubkey.clone(), NodeInfo {
308                         channels: Vec::new(),
309                         lowest_inbound_channel_fee_base_msat: u32::max_value(),
310                         lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
311                         features: GlobalFeatures::new(),
312                         last_update: 0,
313                         rgb: [0; 3],
314                         alias: [0; 32],
315                         addresses: Vec::new(),
316                 });
317                 Router {
318                         secp_ctx: Secp256k1::new(),
319                         network_map: RwLock::new(NetworkMap {
320                                 channels: HashMap::new(),
321                                 our_node_id: our_pubkey,
322                                 nodes: nodes,
323                         }),
324                 }
325         }
326
327         /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
328         /// with an expotnential decay in node "badness". Note that there is deliberately no
329         /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
330         /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
331         /// boolean will reduce the penalty, returning the node to usability faster. If the node is
332         /// behaving correctly, it will disable the failing channel and we will use it again next time.
333         pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
334                 unimplemented!();
335         }
336
337         /// Gets a route from us to the given target node.
338         /// Extra routing hops between known nodes and the target will be used if they are included in
339         /// last_hops.
340         /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
341         /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
342         /// *is* checked as they may change based on the receiving node.
343         pub fn get_route(&self, target: &PublicKey, last_hops: &Vec<RouteHint>, final_value_msat: u64, final_cltv: u32) -> Result<Route, HandleError> {
344                 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
345                 // uptime/success in using a node in the past.
346                 let network = self.network_map.read().unwrap();
347
348                 if *target == network.our_node_id {
349                         return Err(HandleError{err: "Cannot generate a route to ourselves", msg: None});
350                 }
351
352                 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
353                 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
354                 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
355                 // to use as the A* heuristic beyond just the cost to get one node further than the current
356                 // one.
357
358                 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
359                 let mut dist = HashMap::with_capacity(network.nodes.len());
360                 for (key, node) in network.nodes.iter() {
361                         dist.insert(key.clone(), (u64::max_value(),
362                                 node.lowest_inbound_channel_fee_base_msat as u64,
363                                 node.lowest_inbound_channel_fee_proportional_millionths as u64,
364                                 RouteHop {
365                                         pubkey: PublicKey::new(),
366                                         short_channel_id: 0,
367                                         fee_msat: 0,
368                                         cltv_expiry_delta: 0,
369                         }));
370                 }
371
372                 macro_rules! add_entry {
373                         // Adds entry which goes from the node pointed to by $directional_info to
374                         // $dest_node_id over the channel with id $chan_id with fees described in
375                         // $directional_info.
376                         ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $starting_fee_msat: expr ) => {
377                                 //TODO: Explore simply adding fee to hit htlc_minimum_msat
378                                 if $starting_fee_msat as u64 + final_value_msat > $directional_info.htlc_minimum_msat {
379                                         let new_fee = $directional_info.fee_base_msat as u64 + ($starting_fee_msat + final_value_msat) * ($directional_info.fee_proportional_millionths as u64) / 1000000;
380                                         let mut total_fee = $starting_fee_msat as u64;
381                                         let old_entry = dist.get_mut(&$directional_info.src_node_id).unwrap();
382                                         if $directional_info.src_node_id != network.our_node_id {
383                                                 // Ignore new_fee for channel-from-us as we assume all channels-from-us
384                                                 // will have the same effective-fee
385                                                 total_fee += new_fee;
386                                                 total_fee += old_entry.2 * (final_value_msat + total_fee) / 1000000 + old_entry.1;
387                                         }
388                                         let new_graph_node = RouteGraphNode {
389                                                 pubkey: $directional_info.src_node_id,
390                                                 lowest_fee_to_peer_through_node: total_fee,
391                                         };
392                                         if old_entry.0 > total_fee {
393                                                 targets.push(new_graph_node);
394                                                 old_entry.0 = total_fee;
395                                                 old_entry.3 = RouteHop {
396                                                         pubkey: $dest_node_id.clone(),
397                                                         short_channel_id: $chan_id.clone(),
398                                                         fee_msat: new_fee, // This field is ignored on the last-hop anyway
399                                                         cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
400                                                 }
401                                         }
402                                 }
403                         };
404                 }
405
406                 macro_rules! add_entries_to_cheapest_to_target_node {
407                         ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
408                                 for chan_id in $node.channels.iter() {
409                                         let chan = network.channels.get(chan_id).unwrap();
410                                         if chan.one_to_two.src_node_id == *$node_id {
411                                                 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
412                                                 if chan.two_to_one.enabled {
413                                                         add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
414                                                 }
415                                         } else {
416                                                 if chan.one_to_two.enabled {
417                                                         add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
418                                                 }
419                                         }
420                                 }
421                         };
422                 }
423
424                 match network.nodes.get(target) {
425                         None => {},
426                         Some(node) => {
427                                 add_entries_to_cheapest_to_target_node!(node, target, 0);
428                         },
429                 }
430
431                 for hop in last_hops.iter() {
432                         if network.nodes.get(&hop.src_node_id).is_some() {
433                                 add_entry!(hop.short_channel_id, target, hop, 0);
434                         }
435                 }
436
437                 while let Some(RouteGraphNode { pubkey, lowest_fee_to_peer_through_node }) = targets.pop() {
438                         if pubkey == network.our_node_id {
439                                 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
440                                 while res.last().unwrap().pubkey != *target {
441                                         let new_entry = dist.remove(&res.last().unwrap().pubkey).unwrap().3;
442                                         res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
443                                         res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
444                                         res.push(new_entry);
445                                 }
446                                 res.last_mut().unwrap().fee_msat = final_value_msat;
447                                 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
448                                 return Ok(Route {
449                                         hops: res
450                                 });
451                         }
452
453                         match network.nodes.get(&pubkey) {
454                                 None => {},
455                                 Some(node) => {
456                                         let mut fee = lowest_fee_to_peer_through_node - node.lowest_inbound_channel_fee_base_msat as u64;
457                                         fee -= node.lowest_inbound_channel_fee_proportional_millionths as u64 * (fee + final_value_msat) / 1000000;
458                                         add_entries_to_cheapest_to_target_node!(node, &pubkey, fee);
459                                 },
460                         }
461                 }
462
463                 Err(HandleError{err: "Failed to find a path to the given destination", msg: None})
464         }
465 }
466
467 #[cfg(test)]
468 mod tests {
469         use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
470         use ln::msgs::GlobalFeatures;
471
472         use bitcoin::util::misc::hex_bytes;
473         use bitcoin::util::hash::Sha256dHash;
474
475         use secp256k1::key::{PublicKey,SecretKey};
476         use secp256k1::Secp256k1;
477
478         #[test]
479         fn route_test() {
480                 let secp_ctx = Secp256k1::new();
481                 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap()).unwrap();
482                 let router = Router::new(our_id);
483
484                 // Build network from our_id to node8:
485                 //
486                 //        -1(1)2- node1 -1(3)2-
487                 //       /                     \
488                 // our_id                       - node3
489                 //       \                     /
490                 //        -1(2)2- node2 -1(4)2-
491                 //
492                 //
493                 // chan1 1-to-2: disabled
494                 // chan1 2-to-1: enabled, 0 fee
495                 //
496                 // chan2 1-to-2: enabled, ignored fee
497                 // chan2 2-to-1: enabled, 0 fee
498                 //
499                 // chan3 1-to-2: enabled, 0 fee
500                 // chan3 2-to-1: enabled, 100 msat fee
501                 //
502                 // chan4 1-to-2: enabled, 100% fee
503                 // chan4 2-to-1: enabled, 0 fee
504                 //
505                 //
506                 //
507                 //       -1(5)2- node4 -1(8)2--
508                 //       |         2          |
509                 //       |       (11)         |
510                 //      /          1           \
511                 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
512                 //      \                      /
513                 //       -1(7)2- node6 -1(10)2-
514                 //
515                 // chan5  1-to-2: enabled, 100 msat fee
516                 // chan5  2-to-1: enabled, 0 fee
517                 //
518                 // chan6  1-to-2: enabled, 0 fee
519                 // chan6  2-to-1: enabled, 0 fee
520                 //
521                 // chan7  1-to-2: enabled, 100% fee
522                 // chan7  2-to-1: enabled, 0 fee
523                 //
524                 // chan8  1-to-2: enabled, variable fee (0 then 1000 msat)
525                 // chan8  2-to-1: enabled, 0 fee
526                 //
527                 // chan9  1-to-2: enabled, 1001 msat fee
528                 // chan9  2-to-1: enabled, 0 fee
529                 //
530                 // chan10 1-to-2: enabled, 0 fee
531                 // chan10 2-to-1: enabled, 0 fee
532                 //
533                 // chan11 1-to-2: enabled, 0 fee
534                 // chan11 2-to-1: enabled, 0 fee
535
536                 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap()).unwrap();
537                 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap()).unwrap();
538                 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap()).unwrap();
539                 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap()).unwrap();
540                 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap()).unwrap();
541                 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap()).unwrap();
542                 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap()).unwrap();
543
544                 let zero_hash = Sha256dHash::from_data(&[0; 32]);
545
546                 {
547                         let mut network = router.network_map.write().unwrap();
548
549                         network.nodes.insert(node1.clone(), NodeInfo {
550                                 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
551                                 lowest_inbound_channel_fee_base_msat: 100,
552                                 lowest_inbound_channel_fee_proportional_millionths: 0,
553                                 features: GlobalFeatures::new(),
554                                 last_update: 1,
555                                 rgb: [0; 3],
556                                 alias: [0; 32],
557                                 addresses: Vec::new(),
558                         });
559                         network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
560                                 features: GlobalFeatures::new(),
561                                 one_to_two: DirectionalChannelInfo {
562                                         src_node_id: our_id.clone(),
563                                         last_update: 0,
564                                         enabled: false,
565                                         cltv_expiry_delta: u16::max_value(), // This value should be ignored
566                                         htlc_minimum_msat: 0,
567                                         fee_base_msat: u32::max_value(), // This value should be ignored
568                                         fee_proportional_millionths: u32::max_value(), // This value should be ignored
569                                 }, two_to_one: DirectionalChannelInfo {
570                                         src_node_id: node1.clone(),
571                                         last_update: 0,
572                                         enabled: true,
573                                         cltv_expiry_delta: 0,
574                                         htlc_minimum_msat: 0,
575                                         fee_base_msat: 0,
576                                         fee_proportional_millionths: 0,
577                                 },
578                         });
579                         network.nodes.insert(node2.clone(), NodeInfo {
580                                 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
581                                 lowest_inbound_channel_fee_base_msat: 0,
582                                 lowest_inbound_channel_fee_proportional_millionths: 0,
583                                 features: GlobalFeatures::new(),
584                                 last_update: 1,
585                                 rgb: [0; 3],
586                                 alias: [0; 32],
587                                 addresses: Vec::new(),
588                         });
589                         network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
590                                 features: GlobalFeatures::new(),
591                                 one_to_two: DirectionalChannelInfo {
592                                         src_node_id: our_id.clone(),
593                                         last_update: 0,
594                                         enabled: true,
595                                         cltv_expiry_delta: u16::max_value(), // This value should be ignored
596                                         htlc_minimum_msat: 0,
597                                         fee_base_msat: u32::max_value(), // This value should be ignored
598                                         fee_proportional_millionths: u32::max_value(), // This value should be ignored
599                                 }, two_to_one: DirectionalChannelInfo {
600                                         src_node_id: node2.clone(),
601                                         last_update: 0,
602                                         enabled: true,
603                                         cltv_expiry_delta: 0,
604                                         htlc_minimum_msat: 0,
605                                         fee_base_msat: 0,
606                                         fee_proportional_millionths: 0,
607                                 },
608                         });
609                         network.nodes.insert(node3.clone(), NodeInfo {
610                                 channels: vec!(
611                                         NetworkMap::get_key(3, zero_hash.clone()),
612                                         NetworkMap::get_key(4, zero_hash.clone()),
613                                         NetworkMap::get_key(5, zero_hash.clone()),
614                                         NetworkMap::get_key(6, zero_hash.clone()),
615                                         NetworkMap::get_key(7, zero_hash.clone())),
616                                 lowest_inbound_channel_fee_base_msat: 0,
617                                 lowest_inbound_channel_fee_proportional_millionths: 0,
618                                 features: GlobalFeatures::new(),
619                                 last_update: 1,
620                                 rgb: [0; 3],
621                                 alias: [0; 32],
622                                 addresses: Vec::new(),
623                         });
624                         network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
625                                 features: GlobalFeatures::new(),
626                                 one_to_two: DirectionalChannelInfo {
627                                         src_node_id: node1.clone(),
628                                         last_update: 0,
629                                         enabled: true,
630                                         cltv_expiry_delta: (3 << 8) | 1,
631                                         htlc_minimum_msat: 0,
632                                         fee_base_msat: 0,
633                                         fee_proportional_millionths: 0,
634                                 }, two_to_one: DirectionalChannelInfo {
635                                         src_node_id: node3.clone(),
636                                         last_update: 0,
637                                         enabled: true,
638                                         cltv_expiry_delta: (3 << 8) | 2,
639                                         htlc_minimum_msat: 0,
640                                         fee_base_msat: 100,
641                                         fee_proportional_millionths: 0,
642                                 },
643                         });
644                         network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
645                                 features: GlobalFeatures::new(),
646                                 one_to_two: DirectionalChannelInfo {
647                                         src_node_id: node2.clone(),
648                                         last_update: 0,
649                                         enabled: true,
650                                         cltv_expiry_delta: (4 << 8) | 1,
651                                         htlc_minimum_msat: 0,
652                                         fee_base_msat: 0,
653                                         fee_proportional_millionths: 1000000,
654                                 }, two_to_one: DirectionalChannelInfo {
655                                         src_node_id: node3.clone(),
656                                         last_update: 0,
657                                         enabled: true,
658                                         cltv_expiry_delta: (4 << 8) | 2,
659                                         htlc_minimum_msat: 0,
660                                         fee_base_msat: 0,
661                                         fee_proportional_millionths: 0,
662                                 },
663                         });
664                         network.nodes.insert(node4.clone(), NodeInfo {
665                                 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
666                                 lowest_inbound_channel_fee_base_msat: 0,
667                                 lowest_inbound_channel_fee_proportional_millionths: 0,
668                                 features: GlobalFeatures::new(),
669                                 last_update: 1,
670                                 rgb: [0; 3],
671                                 alias: [0; 32],
672                                 addresses: Vec::new(),
673                         });
674                         network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
675                                 features: GlobalFeatures::new(),
676                                 one_to_two: DirectionalChannelInfo {
677                                         src_node_id: node3.clone(),
678                                         last_update: 0,
679                                         enabled: true,
680                                         cltv_expiry_delta: (5 << 8) | 1,
681                                         htlc_minimum_msat: 0,
682                                         fee_base_msat: 100,
683                                         fee_proportional_millionths: 0,
684                                 }, two_to_one: DirectionalChannelInfo {
685                                         src_node_id: node4.clone(),
686                                         last_update: 0,
687                                         enabled: true,
688                                         cltv_expiry_delta: (5 << 8) | 2,
689                                         htlc_minimum_msat: 0,
690                                         fee_base_msat: 0,
691                                         fee_proportional_millionths: 0,
692                                 },
693                         });
694                         network.nodes.insert(node5.clone(), NodeInfo {
695                                 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
696                                 lowest_inbound_channel_fee_base_msat: 0,
697                                 lowest_inbound_channel_fee_proportional_millionths: 0,
698                                 features: GlobalFeatures::new(),
699                                 last_update: 1,
700                                 rgb: [0; 3],
701                                 alias: [0; 32],
702                                 addresses: Vec::new(),
703                         });
704                         network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
705                                 features: GlobalFeatures::new(),
706                                 one_to_two: DirectionalChannelInfo {
707                                         src_node_id: node3.clone(),
708                                         last_update: 0,
709                                         enabled: true,
710                                         cltv_expiry_delta: (6 << 8) | 1,
711                                         htlc_minimum_msat: 0,
712                                         fee_base_msat: 0,
713                                         fee_proportional_millionths: 0,
714                                 }, two_to_one: DirectionalChannelInfo {
715                                         src_node_id: node5.clone(),
716                                         last_update: 0,
717                                         enabled: true,
718                                         cltv_expiry_delta: (6 << 8) | 2,
719                                         htlc_minimum_msat: 0,
720                                         fee_base_msat: 0,
721                                         fee_proportional_millionths: 0,
722                                 },
723                         });
724                         network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
725                                 features: GlobalFeatures::new(),
726                                 one_to_two: DirectionalChannelInfo {
727                                         src_node_id: node5.clone(),
728                                         last_update: 0,
729                                         enabled: true,
730                                         cltv_expiry_delta: (11 << 8) | 1,
731                                         htlc_minimum_msat: 0,
732                                         fee_base_msat: 0,
733                                         fee_proportional_millionths: 0,
734                                 }, two_to_one: DirectionalChannelInfo {
735                                         src_node_id: node4.clone(),
736                                         last_update: 0,
737                                         enabled: true,
738                                         cltv_expiry_delta: (11 << 8) | 2,
739                                         htlc_minimum_msat: 0,
740                                         fee_base_msat: 0,
741                                         fee_proportional_millionths: 0,
742                                 },
743                         });
744                         network.nodes.insert(node6.clone(), NodeInfo {
745                                 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
746                                 lowest_inbound_channel_fee_base_msat: 0,
747                                 lowest_inbound_channel_fee_proportional_millionths: 0,
748                                 features: GlobalFeatures::new(),
749                                 last_update: 1,
750                                 rgb: [0; 3],
751                                 alias: [0; 32],
752                                 addresses: Vec::new(),
753                         });
754                         network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
755                                 features: GlobalFeatures::new(),
756                                 one_to_two: DirectionalChannelInfo {
757                                         src_node_id: node3.clone(),
758                                         last_update: 0,
759                                         enabled: true,
760                                         cltv_expiry_delta: (7 << 8) | 1,
761                                         htlc_minimum_msat: 0,
762                                         fee_base_msat: 0,
763                                         fee_proportional_millionths: 1000000,
764                                 }, two_to_one: DirectionalChannelInfo {
765                                         src_node_id: node6.clone(),
766                                         last_update: 0,
767                                         enabled: true,
768                                         cltv_expiry_delta: (7 << 8) | 2,
769                                         htlc_minimum_msat: 0,
770                                         fee_base_msat: 0,
771                                         fee_proportional_millionths: 0,
772                                 },
773                         });
774                 }
775
776                 { // Simple route to 3 via 2
777                         let route = router.get_route(&node3, &Vec::new(), 100, 42).unwrap();
778                         assert_eq!(route.hops.len(), 2);
779
780                         assert_eq!(route.hops[0].pubkey, node2);
781                         assert_eq!(route.hops[0].short_channel_id, 2);
782                         assert_eq!(route.hops[0].fee_msat, 100);
783                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
784
785                         assert_eq!(route.hops[1].pubkey, node3);
786                         assert_eq!(route.hops[1].short_channel_id, 4);
787                         assert_eq!(route.hops[1].fee_msat, 100);
788                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
789                 }
790
791                 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
792                         let route = router.get_route(&node1, &Vec::new(), 100, 42).unwrap();
793                         assert_eq!(route.hops.len(), 3);
794
795                         assert_eq!(route.hops[0].pubkey, node2);
796                         assert_eq!(route.hops[0].short_channel_id, 2);
797                         assert_eq!(route.hops[0].fee_msat, 200);
798                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
799
800                         assert_eq!(route.hops[1].pubkey, node3);
801                         assert_eq!(route.hops[1].short_channel_id, 4);
802                         assert_eq!(route.hops[1].fee_msat, 100);
803                         assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
804
805                         assert_eq!(route.hops[2].pubkey, node1);
806                         assert_eq!(route.hops[2].short_channel_id, 3);
807                         assert_eq!(route.hops[2].fee_msat, 100);
808                         assert_eq!(route.hops[2].cltv_expiry_delta, 42);
809                 }
810
811                 let mut last_hops = vec!(RouteHint {
812                                 src_node_id: node4.clone(),
813                                 short_channel_id: 8,
814                                 fee_base_msat: 0,
815                                 fee_proportional_millionths: 0,
816                                 cltv_expiry_delta: (8 << 8) | 1,
817                                 htlc_minimum_msat: 0,
818                         }, RouteHint {
819                                 src_node_id: node5.clone(),
820                                 short_channel_id: 9,
821                                 fee_base_msat: 1001,
822                                 fee_proportional_millionths: 0,
823                                 cltv_expiry_delta: (9 << 8) | 1,
824                                 htlc_minimum_msat: 0,
825                         }, RouteHint {
826                                 src_node_id: node6.clone(),
827                                 short_channel_id: 10,
828                                 fee_base_msat: 0,
829                                 fee_proportional_millionths: 0,
830                                 cltv_expiry_delta: (10 << 8) | 1,
831                                 htlc_minimum_msat: 0,
832                         });
833
834                 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
835                         let route = router.get_route(&node7, &last_hops, 100, 42).unwrap();
836                         assert_eq!(route.hops.len(), 5);
837
838                         assert_eq!(route.hops[0].pubkey, node2);
839                         assert_eq!(route.hops[0].short_channel_id, 2);
840                         assert_eq!(route.hops[0].fee_msat, 100);
841                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
842
843                         assert_eq!(route.hops[1].pubkey, node3);
844                         assert_eq!(route.hops[1].short_channel_id, 4);
845                         assert_eq!(route.hops[1].fee_msat, 0);
846                         assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
847
848                         assert_eq!(route.hops[2].pubkey, node5);
849                         assert_eq!(route.hops[2].short_channel_id, 6);
850                         assert_eq!(route.hops[2].fee_msat, 0);
851                         assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
852
853                         assert_eq!(route.hops[3].pubkey, node4);
854                         assert_eq!(route.hops[3].short_channel_id, 11);
855                         assert_eq!(route.hops[3].fee_msat, 0);
856                         assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
857
858                         assert_eq!(route.hops[4].pubkey, node7);
859                         assert_eq!(route.hops[4].short_channel_id, 8);
860                         assert_eq!(route.hops[4].fee_msat, 100);
861                         assert_eq!(route.hops[4].cltv_expiry_delta, 42);
862                 }
863
864                 last_hops[0].fee_base_msat = 1000;
865
866                 { // Revert to via 6 as the fee on 8 goes up
867                         let route = router.get_route(&node7, &last_hops, 100, 42).unwrap();
868                         assert_eq!(route.hops.len(), 4);
869
870                         assert_eq!(route.hops[0].pubkey, node2);
871                         assert_eq!(route.hops[0].short_channel_id, 2);
872                         assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
873                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
874
875                         assert_eq!(route.hops[1].pubkey, node3);
876                         assert_eq!(route.hops[1].short_channel_id, 4);
877                         assert_eq!(route.hops[1].fee_msat, 100);
878                         assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
879
880                         assert_eq!(route.hops[2].pubkey, node6);
881                         assert_eq!(route.hops[2].short_channel_id, 7);
882                         assert_eq!(route.hops[2].fee_msat, 0);
883                         assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
884
885                         assert_eq!(route.hops[3].pubkey, node7);
886                         assert_eq!(route.hops[3].short_channel_id, 10);
887                         assert_eq!(route.hops[3].fee_msat, 100);
888                         assert_eq!(route.hops[3].cltv_expiry_delta, 42);
889                 }
890
891                 { // ...but still use 8 for larger payments as 6 has a variable feerate
892                         let route = router.get_route(&node7, &last_hops, 2000, 42).unwrap();
893                         assert_eq!(route.hops.len(), 5);
894
895                         assert_eq!(route.hops[0].pubkey, node2);
896                         assert_eq!(route.hops[0].short_channel_id, 2);
897                         assert_eq!(route.hops[0].fee_msat, 3000);
898                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
899
900                         assert_eq!(route.hops[1].pubkey, node3);
901                         assert_eq!(route.hops[1].short_channel_id, 4);
902                         assert_eq!(route.hops[1].fee_msat, 0);
903                         assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
904
905                         assert_eq!(route.hops[2].pubkey, node5);
906                         assert_eq!(route.hops[2].short_channel_id, 6);
907                         assert_eq!(route.hops[2].fee_msat, 0);
908                         assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
909
910                         assert_eq!(route.hops[3].pubkey, node4);
911                         assert_eq!(route.hops[3].short_channel_id, 11);
912                         assert_eq!(route.hops[3].fee_msat, 1000);
913                         assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
914
915                         assert_eq!(route.hops[4].pubkey, node7);
916                         assert_eq!(route.hops[4].short_channel_id, 8);
917                         assert_eq!(route.hops[4].fee_msat, 2000);
918                         assert_eq!(route.hops[4].cltv_expiry_delta, 42);
919                 }
920         }
921 }