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