0260aa1fdbd8237eebadb8c88fa4b3d663c6bb09
[rust-lightning] / lightning / src / routing / router.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! The top-level routing/network map tracking logic lives here.
11 //!
12 //! You probably want to create a NetGraphMsgHandler and use that as your RoutingMessageHandler and then
13 //! interrogate it to get routes for your own payments.
14
15 use bitcoin::secp256k1::key::PublicKey;
16
17 use ln::channelmanager::ChannelDetails;
18 use ln::features::{ChannelFeatures, NodeFeatures};
19 use ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
20 use routing::network_graph::{NetworkGraph, RoutingFees};
21 use util::ser::{Writeable, Readable};
22 use util::logger::Logger;
23
24 use std::cmp;
25 use std::collections::{HashMap,BinaryHeap};
26 use std::ops::Deref;
27
28 /// A hop in a route
29 #[derive(Clone, PartialEq)]
30 pub struct RouteHop {
31         /// The node_id of the node at this hop.
32         pub pubkey: PublicKey,
33         /// The node_announcement features of the node at this hop. For the last hop, these may be
34         /// amended to match the features present in the invoice this node generated.
35         pub node_features: NodeFeatures,
36         /// The channel that should be used from the previous hop to reach this node.
37         pub short_channel_id: u64,
38         /// The channel_announcement features of the channel that should be used from the previous hop
39         /// to reach this node.
40         pub channel_features: ChannelFeatures,
41         /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
42         pub fee_msat: u64,
43         /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
44         /// expected at the destination, in excess of the current block height.
45         pub cltv_expiry_delta: u32,
46 }
47
48 impl Writeable for Vec<RouteHop> {
49         fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
50                 (self.len() as u8).write(writer)?;
51                 for hop in self.iter() {
52                         hop.pubkey.write(writer)?;
53                         hop.node_features.write(writer)?;
54                         hop.short_channel_id.write(writer)?;
55                         hop.channel_features.write(writer)?;
56                         hop.fee_msat.write(writer)?;
57                         hop.cltv_expiry_delta.write(writer)?;
58                 }
59                 Ok(())
60         }
61 }
62
63 impl Readable for Vec<RouteHop> {
64         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Vec<RouteHop>, DecodeError> {
65                 let hops_count: u8 = Readable::read(reader)?;
66                 let mut hops = Vec::with_capacity(hops_count as usize);
67                 for _ in 0..hops_count {
68                         hops.push(RouteHop {
69                                 pubkey: Readable::read(reader)?,
70                                 node_features: Readable::read(reader)?,
71                                 short_channel_id: Readable::read(reader)?,
72                                 channel_features: Readable::read(reader)?,
73                                 fee_msat: Readable::read(reader)?,
74                                 cltv_expiry_delta: Readable::read(reader)?,
75                         });
76                 }
77                 Ok(hops)
78         }
79 }
80
81 /// A route directs a payment from the sender (us) to the recipient. If the recipient supports MPP,
82 /// it can take multiple paths. Each path is composed of one or more hops through the network.
83 #[derive(Clone, PartialEq)]
84 pub struct Route {
85         /// The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
86         /// last RouteHop in each path must be the same.
87         /// Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
88         /// destination. Thus, this must always be at least length one. While the maximum length of any
89         /// given path is variable, keeping the length of any path to less than 20 should currently
90         /// ensure it is viable.
91         pub paths: Vec<Vec<RouteHop>>,
92 }
93
94 impl Writeable for Route {
95         fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
96                 (self.paths.len() as u64).write(writer)?;
97                 for hops in self.paths.iter() {
98                         hops.write(writer)?;
99                 }
100                 Ok(())
101         }
102 }
103
104 impl Readable for Route {
105         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Route, DecodeError> {
106                 let path_count: u64 = Readable::read(reader)?;
107                 let mut paths = Vec::with_capacity(cmp::min(path_count, 128) as usize);
108                 for _ in 0..path_count {
109                         paths.push(Readable::read(reader)?);
110                 }
111                 Ok(Route { paths })
112         }
113 }
114
115 /// A channel descriptor which provides a last-hop route to get_route
116 #[derive(Clone)]
117 pub struct RouteHint {
118         /// The node_id of the non-target end of the route
119         pub src_node_id: PublicKey,
120         /// The short_channel_id of this channel
121         pub short_channel_id: u64,
122         /// The fees which must be paid to use this channel
123         pub fees: RoutingFees,
124         /// The difference in CLTV values between this node and the next node.
125         pub cltv_expiry_delta: u16,
126         /// The minimum value, in msat, which must be relayed to the next hop.
127         pub htlc_minimum_msat: Option<u64>,
128         /// The maximum value in msat available for routing with a single HTLC.
129         pub htlc_maximum_msat: Option<u64>,
130 }
131
132 #[derive(Eq, PartialEq)]
133 struct RouteGraphNode {
134         pubkey: PublicKey,
135         lowest_fee_to_peer_through_node: u64,
136         lowest_fee_to_node: u64,
137 }
138
139 impl cmp::Ord for RouteGraphNode {
140         fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
141                 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
142                         .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
143         }
144 }
145
146 impl cmp::PartialOrd for RouteGraphNode {
147         fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
148                 Some(self.cmp(other))
149         }
150 }
151
152 struct DummyDirectionalChannelInfo {
153         cltv_expiry_delta: u32,
154         htlc_minimum_msat: u64,
155         htlc_maximum_msat: Option<u64>,
156         fees: RoutingFees,
157 }
158
159
160 /// Gets a route from us to the given target node.
161 ///
162 /// Extra routing hops between known nodes and the target will be used if they are included in
163 /// last_hops.
164 ///
165 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
166 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
167 /// view of our local channels (from net_graph_msg_handler) will be ignored, and only those in first_hops
168 /// will be used.
169 ///
170 /// Panics if first_hops contains channels without short_channel_ids
171 /// (ChannelManager::list_usable_channels will never include such channels).
172 ///
173 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
174 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
175 /// *is* checked as they may change based on the receiving node.
176 pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, target: &PublicKey, first_hops: Option<&[&ChannelDetails]>,
177         last_hops: &[&RouteHint], final_value_msat: u64, final_cltv: u32, logger: L) -> Result<Route, LightningError> where L::Target: Logger {
178         // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
179         // uptime/success in using a node in the past.
180         if *target == *our_node_id {
181                 return Err(LightningError{err: "Cannot generate a route to ourselves".to_owned(), action: ErrorAction::IgnoreError});
182         }
183
184         if final_value_msat > MAX_VALUE_MSAT {
185                 return Err(LightningError{err: "Cannot generate a route of more value than all existing satoshis".to_owned(), action: ErrorAction::IgnoreError});
186         }
187
188         // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
189         // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
190         // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
191         // to use as the A* heuristic beyond just the cost to get one node further than the current
192         // one.
193
194         let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
195                 cltv_expiry_delta: 0,
196                 htlc_minimum_msat: 0,
197                 htlc_maximum_msat: None,
198                 fees: RoutingFees {
199                         base_msat: 0,
200                         proportional_millionths: 0,
201                 }
202         };
203
204         let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
205         let mut dist = HashMap::with_capacity(network.get_nodes().len());
206
207         let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
208         if let Some(hops) = first_hops {
209                 for chan in hops {
210                         let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
211                         if chan.remote_network_id == *target {
212                                 return Ok(Route {
213                                         paths: vec![vec![RouteHop {
214                                                 pubkey: chan.remote_network_id,
215                                                 node_features: chan.counterparty_features.to_context(),
216                                                 short_channel_id,
217                                                 channel_features: chan.counterparty_features.to_context(),
218                                                 fee_msat: final_value_msat,
219                                                 cltv_expiry_delta: final_cltv,
220                                         }]],
221                                 });
222                         }
223                         first_hop_targets.insert(chan.remote_network_id, (short_channel_id, chan.counterparty_features.clone()));
224                 }
225                 if first_hop_targets.is_empty() {
226                         return Err(LightningError{err: "Cannot route when there are no outbound routes away from us".to_owned(), action: ErrorAction::IgnoreError});
227                 }
228         }
229
230         macro_rules! add_entry {
231                 // Adds entry which goes from $src_node_id to $dest_node_id
232                 // over the channel with id $chan_id with fees described in
233                 // $directional_info.
234                 ( $chan_id: expr, $src_node_id: expr, $dest_node_id: expr, $directional_info: expr, $capacity_sats: expr, $chan_features: expr, $starting_fee_msat: expr ) => {
235                         //TODO: Explore simply adding fee to hit htlc_minimum_msat
236                         if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
237                                 let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fees.proportional_millionths as u64);
238                                 if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
239                                                 ($directional_info.fees.base_msat as u64).checked_add(part / 1000000) })
240                                 {
241                                         let mut total_fee = $starting_fee_msat as u64;
242
243                                         let mut available_msat = $capacity_sats;
244                                         if let Some(htlc_maximum_msat) = $directional_info.htlc_maximum_msat {
245                                                 if let Some(capacity_sats) = $capacity_sats {
246                                                         available_msat = Some(cmp::min(capacity_sats * 1000, htlc_maximum_msat));
247                                                 } else {
248                                                         available_msat = Some(htlc_maximum_msat);
249                                                 }
250                                         }
251
252                                         let hm_entry = dist.entry(&$src_node_id);
253                                         let old_entry = hm_entry.or_insert_with(|| {
254                                                 let mut fee_base_msat = u32::max_value();
255                                                 let mut fee_proportional_millionths = u32::max_value();
256                                                 if let Some(fees) = network.get_nodes().get(&$src_node_id).and_then(|node| node.lowest_inbound_channel_fees) {
257                                                         fee_base_msat = fees.base_msat;
258                                                         fee_proportional_millionths = fees.proportional_millionths;
259                                                 }
260                                                 (u64::max_value(),
261                                                         fee_base_msat,
262                                                         fee_proportional_millionths,
263                                                         RouteHop {
264                                                                 pubkey: $dest_node_id.clone(),
265                                                                 node_features: NodeFeatures::empty(),
266                                                                 short_channel_id: 0,
267                                                                 channel_features: $chan_features.clone(),
268                                                                 fee_msat: 0,
269                                                                 cltv_expiry_delta: 0,
270                                                         },
271                                                         None,
272                                                 )
273                                         });
274                                         if $src_node_id != *our_node_id {
275                                                 // Ignore new_fee for channel-from-us as we assume all channels-from-us
276                                                 // will have the same effective-fee
277                                                 total_fee += new_fee;
278                                                 if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
279                                                         total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
280                                                 } else {
281                                                         // max_value means we'll always fail the old_entry.0 > total_fee check
282                                                         total_fee = u64::max_value();
283                                                 }
284                                         }
285                                         let new_graph_node = RouteGraphNode {
286                                                 pubkey: $src_node_id,
287                                                 lowest_fee_to_peer_through_node: total_fee,
288                                                 lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
289                                         };
290                                         if old_entry.0 > total_fee {
291                                                 targets.push(new_graph_node);
292                                                 old_entry.0 = total_fee;
293                                                 old_entry.3 = RouteHop {
294                                                         pubkey: $dest_node_id.clone(),
295                                                         node_features: NodeFeatures::empty(),
296                                                         short_channel_id: $chan_id.clone(),
297                                                         channel_features: $chan_features.clone(),
298                                                         fee_msat: new_fee, // This field is ignored on the last-hop anyway
299                                                         cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
300                                                 };
301                                                 old_entry.4 = available_msat;
302                                         }
303                                 }
304                         }
305                 };
306         }
307
308         macro_rules! add_entries_to_cheapest_to_target_node {
309                 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
310                         if first_hops.is_some() {
311                                 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&$node_id) {
312                                         add_entry!(first_hop, *our_node_id, $node_id, dummy_directional_info, None::<u64>, features.to_context(), $fee_to_target_msat);
313                                 }
314                         }
315
316                         let features;
317                         if let Some(node_info) = $node.announcement_info.as_ref() {
318                                 features = node_info.features.clone();
319                         } else {
320                                 features = NodeFeatures::empty();
321                         }
322
323                         if !features.requires_unknown_bits() {
324                                 for chan_id in $node.channels.iter() {
325                                         let chan = network.get_channels().get(chan_id).unwrap();
326                                         if !chan.features.requires_unknown_bits() {
327                                                 if chan.node_one == *$node_id {
328                                                         // ie $node is one, ie next hop in A* is two, via the two_to_one channel
329                                                         if first_hops.is_none() || chan.node_two != *our_node_id {
330                                                                 if let Some(two_to_one) = chan.two_to_one.as_ref() {
331                                                                         if two_to_one.enabled {
332                                                                                 add_entry!(chan_id, chan.node_two, chan.node_one, two_to_one, chan.capacity_sats, chan.features, $fee_to_target_msat);
333                                                                         }
334                                                                 }
335                                                         }
336                                                 } else {
337                                                         if first_hops.is_none() || chan.node_one != *our_node_id {
338                                                                 if let Some(one_to_two) = chan.one_to_two.as_ref() {
339                                                                         if one_to_two.enabled {
340                                                                                 add_entry!(chan_id, chan.node_one, chan.node_two, one_to_two, chan.capacity_sats, chan.features, $fee_to_target_msat);
341                                                                         }
342                                                                 }
343
344                                                         }
345                                                 }
346                                         }
347                                 }
348                         }
349                 };
350         }
351
352         match network.get_nodes().get(target) {
353                 None => {},
354                 Some(node) => {
355                         add_entries_to_cheapest_to_target_node!(node, target, 0);
356                 },
357         }
358
359         for hop in last_hops.iter() {
360                 let have_hop_src_in_graph =
361                         if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&hop.src_node_id) {
362                                 // If this hop connects to a node with which we have a direct channel, ignore the
363                                 // network graph and add both the hop and our direct channel to the candidate set:
364                                 //
365                                 // Currently there are no channel-context features defined, so we are a
366                                 // bit lazy here. In the future, we should pull them out via our
367                                 // ChannelManager, but there's no reason to waste the space until we
368                                 // need them.
369                                 add_entry!(first_hop, *our_node_id , hop.src_node_id, dummy_directional_info, None::<u64>, features.to_context(), 0);
370                                 true
371                         } else {
372                                 // In any other case, only add the hop if the source is in the regular network
373                                 // graph:
374                                 network.get_nodes().get(&hop.src_node_id).is_some()
375                         };
376                 if have_hop_src_in_graph {
377                         // BOLT 11 doesn't allow inclusion of features for the last hop hints, which
378                         // really sucks, cause we're gonna need that eventually.
379                         let last_hop_htlc_minimum_msat: u64 = match hop.htlc_minimum_msat {
380                                 Some(htlc_minimum_msat) => htlc_minimum_msat,
381                                 None => 0
382                         };
383                         let directional_info = DummyDirectionalChannelInfo {
384                                 cltv_expiry_delta: hop.cltv_expiry_delta as u32,
385                                 htlc_minimum_msat: last_hop_htlc_minimum_msat,
386                                 htlc_maximum_msat: hop.htlc_maximum_msat,
387                                 fees: hop.fees,
388                         };
389                         add_entry!(hop.short_channel_id, hop.src_node_id, target, directional_info, None::<u64>, ChannelFeatures::empty(), 0);
390                 }
391         }
392
393         while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
394                 if pubkey == *our_node_id {
395                         let mut res = vec!(dist.remove(&our_node_id).unwrap().3);
396                         loop {
397                                 if let Some(&(_, ref features)) = first_hop_targets.get(&res.last().unwrap().pubkey) {
398                                         res.last_mut().unwrap().node_features = features.to_context();
399                                 } else if let Some(node) = network.get_nodes().get(&res.last().unwrap().pubkey) {
400                                         if let Some(node_info) = node.announcement_info.as_ref() {
401                                                 res.last_mut().unwrap().node_features = node_info.features.clone();
402                                         } else {
403                                                 res.last_mut().unwrap().node_features = NodeFeatures::empty();
404                                         }
405                                 } else {
406                                         // We should be able to fill in features for everything except the last
407                                         // hop, if the last hop was provided via a BOLT 11 invoice (though we
408                                         // should be able to extend it further as BOLT 11 does have feature
409                                         // flags for the last hop node itself).
410                                         assert!(res.last().unwrap().pubkey == *target);
411                                 }
412                                 if res.last().unwrap().pubkey == *target {
413                                         break;
414                                 }
415
416                                 let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
417                                         Some(hop) => hop.3,
418                                         None => return Err(LightningError{err: "Failed to find a non-fee-overflowing path to the given destination".to_owned(), action: ErrorAction::IgnoreError}),
419                                 };
420                                 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
421                                 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
422                                 res.push(new_entry);
423                         }
424                         res.last_mut().unwrap().fee_msat = final_value_msat;
425                         res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
426                         let route = Route { paths: vec![res] };
427                         log_trace!(logger, "Got route: {}", log_route!(route));
428                         return Ok(route);
429                 }
430
431                 match network.get_nodes().get(&pubkey) {
432                         None => {},
433                         Some(node) => {
434                                 add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
435                         },
436                 }
437         }
438
439         Err(LightningError{err: "Failed to find a path to the given destination".to_owned(), action: ErrorAction::IgnoreError})
440 }
441
442 #[cfg(test)]
443 mod tests {
444         use routing::router::{get_route, RouteHint, RoutingFees};
445         use routing::network_graph::{NetworkGraph, NetGraphMsgHandler};
446         use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
447         use ln::msgs::{ErrorAction, LightningError, OptionalField, UnsignedChannelAnnouncement, ChannelAnnouncement, RoutingMessageHandler,
448            NodeAnnouncement, UnsignedNodeAnnouncement, ChannelUpdate, UnsignedChannelUpdate};
449         use ln::channelmanager;
450         use util::test_utils;
451         use util::ser::Writeable;
452
453         use bitcoin::hashes::sha256d::Hash as Sha256dHash;
454         use bitcoin::hashes::Hash;
455         use bitcoin::network::constants::Network;
456         use bitcoin::blockdata::constants::genesis_block;
457
458         use hex;
459
460         use bitcoin::secp256k1::key::{PublicKey,SecretKey};
461         use bitcoin::secp256k1::{Secp256k1, All};
462
463         use std::sync::Arc;
464
465         // Using the same keys for LN and BTC ids
466         fn add_channel(net_graph_msg_handler: &NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>, secp_ctx: &Secp256k1<All>, node_1_privkey: &SecretKey,
467            node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64) {
468                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
469                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
470
471                 let unsigned_announcement = UnsignedChannelAnnouncement {
472                         features,
473                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
474                         short_channel_id,
475                         node_id_1,
476                         node_id_2,
477                         bitcoin_key_1: node_id_1,
478                         bitcoin_key_2: node_id_2,
479                         excess_data: Vec::new(),
480                 };
481
482                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
483                 let valid_announcement = ChannelAnnouncement {
484                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
485                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
486                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
487                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
488                         contents: unsigned_announcement.clone(),
489                 };
490                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
491                         Ok(res) => assert!(res),
492                         _ => panic!()
493                 };
494         }
495
496         fn update_channel(net_graph_msg_handler: &NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>, secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, update: UnsignedChannelUpdate) {
497                 let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]);
498                 let valid_channel_update = ChannelUpdate {
499                         signature: secp_ctx.sign(&msghash, node_privkey),
500                         contents: update.clone()
501                 };
502
503                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
504                         Ok(res) => assert!(res),
505                         // Err(_) => panic!()
506                         Err(e) => println!("{:?}", e.err)
507                 };
508         }
509
510
511         fn add_or_update_node(net_graph_msg_handler: &NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>, secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey,
512            features: NodeFeatures, timestamp: u32) {
513                 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
514                 let unsigned_announcement = UnsignedNodeAnnouncement {
515                         features,
516                         timestamp,
517                         node_id,
518                         rgb: [0; 3],
519                         alias: [0; 32],
520                         addresses: Vec::new(),
521                         excess_address_data: Vec::new(),
522                         excess_data: Vec::new(),
523                 };
524                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
525                 let valid_announcement = NodeAnnouncement {
526                         signature: secp_ctx.sign(&msghash, node_privkey),
527                         contents: unsigned_announcement.clone()
528                 };
529
530                 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
531                         Ok(_) => (),
532                         Err(_) => panic!()
533                 };
534         }
535
536         fn get_nodes(secp_ctx: &Secp256k1<All>) -> (SecretKey, PublicKey, Vec<SecretKey>, Vec<PublicKey>) {
537                 let privkeys: Vec<SecretKey> = (2..10).map(|i| {
538                         SecretKey::from_slice(&hex::decode(format!("{:02}", i).repeat(32)).unwrap()[..]).unwrap()
539                 }).collect();
540
541                 let pubkeys = privkeys.iter().map(|secret| PublicKey::from_secret_key(&secp_ctx, secret)).collect();
542
543                 let our_privkey = SecretKey::from_slice(&hex::decode("01".repeat(32)).unwrap()[..]).unwrap();
544                 let our_id = PublicKey::from_secret_key(&secp_ctx, &our_privkey);
545
546                 (our_privkey, our_id, privkeys, pubkeys)
547         }
548
549         fn id_to_feature_flags(id: u8) -> Vec<u8> {
550                 // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can
551                 // test for it later.
552                 let idx = (id - 1) * 2 + 1;
553                 if idx > 8*3 {
554                         vec![1 << (idx - 8*3), 0, 0, 0]
555                 } else if idx > 8*2 {
556                         vec![1 << (idx - 8*2), 0, 0]
557                 } else if idx > 8*1 {
558                         vec![1 << (idx - 8*1), 0]
559                 } else {
560                         vec![1 << idx]
561                 }
562         }
563
564         fn build_graph() -> (Secp256k1<All>, NetGraphMsgHandler<std::sync::Arc<crate::util::test_utils::TestChainSource>, std::sync::Arc<crate::util::test_utils::TestLogger>>, std::sync::Arc<test_utils::TestLogger>) {
565                 let secp_ctx = Secp256k1::new();
566                 let logger = Arc::new(test_utils::TestLogger::new());
567                 let net_graph_msg_handler = NetGraphMsgHandler::new(genesis_block(Network::Testnet).header.block_hash(), None, Arc::clone(&logger));
568                 // Build network from our_id to node7:
569                 //
570                 //        -1(1)2-  node0  -1(3)2-
571                 //       /                       \
572                 // our_id -1(12)2- node7 -1(13)2--- node2
573                 //       \                       /
574                 //        -1(2)2-  node1  -1(4)2-
575                 //
576                 //
577                 // chan1  1-to-2: disabled
578                 // chan1  2-to-1: enabled, 0 fee
579                 //
580                 // chan2  1-to-2: enabled, ignored fee
581                 // chan2  2-to-1: enabled, 0 fee
582                 //
583                 // chan3  1-to-2: enabled, 0 fee
584                 // chan3  2-to-1: enabled, 100 msat fee
585                 //
586                 // chan4  1-to-2: enabled, 100% fee
587                 // chan4  2-to-1: enabled, 0 fee
588                 //
589                 // chan12 1-to-2: enabled, ignored fee
590                 // chan12 2-to-1: enabled, 0 fee
591                 //
592                 // chan13 1-to-2: enabled, 200% fee
593                 // chan13 2-to-1: enabled, 0 fee
594                 //
595                 //
596                 //       -1(5)2- node3 -1(8)2--
597                 //       |         2          |
598                 //       |       (11)         |
599                 //      /          1           \
600                 // node2--1(6)2- node4 -1(9)2--- node6 (not in global route map)
601                 //      \                      /
602                 //       -1(7)2- node5 -1(10)2-
603                 //
604                 // chan5  1-to-2: enabled, 100 msat fee
605                 // chan5  2-to-1: enabled, 0 fee
606                 //
607                 // chan6  1-to-2: enabled, 0 fee
608                 // chan6  2-to-1: enabled, 0 fee
609                 //
610                 // chan7  1-to-2: enabled, 100% fee
611                 // chan7  2-to-1: enabled, 0 fee
612                 //
613                 // chan8  1-to-2: enabled, variable fee (0 then 1000 msat)
614                 // chan8  2-to-1: enabled, 0 fee
615                 //
616                 // chan9  1-to-2: enabled, 1001 msat fee
617                 // chan9  2-to-1: enabled, 0 fee
618                 //
619                 // chan10 1-to-2: enabled, 0 fee
620                 // chan10 2-to-1: enabled, 0 fee
621                 //
622                 // chan11 1-to-2: enabled, 0 fee
623                 // chan11 2-to-1: enabled, 0 fee
624
625                 let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx);
626
627                 add_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, &privkeys[0], ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), 1);
628                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
629                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
630                         short_channel_id: 1,
631                         timestamp: 1,
632                         flags: 1,
633                         cltv_expiry_delta: 0,
634                         htlc_minimum_msat: 0,
635                         htlc_maximum_msat: OptionalField::Absent,
636                         fee_base_msat: 0,
637                         fee_proportional_millionths: 0,
638                         excess_data: Vec::new()
639                 });
640
641                 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[0], NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0);
642
643                 add_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, &privkeys[1], ChannelFeatures::from_le_bytes(id_to_feature_flags(2)), 2);
644                 update_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
645                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
646                         short_channel_id: 2,
647                         timestamp: 1,
648                         flags: 0,
649                         cltv_expiry_delta: u16::max_value(),
650                         htlc_minimum_msat: 0,
651                         htlc_maximum_msat: OptionalField::Absent,
652                         fee_base_msat: u32::max_value(),
653                         fee_proportional_millionths: u32::max_value(),
654                         excess_data: Vec::new()
655                 });
656                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
657                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
658                         short_channel_id: 2,
659                         timestamp: 1,
660                         flags: 1,
661                         cltv_expiry_delta: 0,
662                         htlc_minimum_msat: 0,
663                         htlc_maximum_msat: OptionalField::Absent,
664                         fee_base_msat: 0,
665                         fee_proportional_millionths: 0,
666                         excess_data: Vec::new()
667                 });
668
669                 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[1], NodeFeatures::from_le_bytes(id_to_feature_flags(2)), 0);
670
671                 add_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, &privkeys[7], ChannelFeatures::from_le_bytes(id_to_feature_flags(12)), 12);
672                 update_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
673                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
674                         short_channel_id: 12,
675                         timestamp: 1,
676                         flags: 0,
677                         cltv_expiry_delta: u16::max_value(),
678                         htlc_minimum_msat: 0,
679                         htlc_maximum_msat: OptionalField::Absent,
680                         fee_base_msat: u32::max_value(),
681                         fee_proportional_millionths: u32::max_value(),
682                         excess_data: Vec::new()
683                 });
684                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[7], UnsignedChannelUpdate {
685                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
686                         short_channel_id: 12,
687                         timestamp: 1,
688                         flags: 1,
689                         cltv_expiry_delta: 0,
690                         htlc_minimum_msat: 0,
691                         htlc_maximum_msat: OptionalField::Absent,
692                         fee_base_msat: 0,
693                         fee_proportional_millionths: 0,
694                         excess_data: Vec::new()
695                 });
696
697                 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[7], NodeFeatures::from_le_bytes(id_to_feature_flags(8)), 0);
698
699                 add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[0], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(3)), 3);
700                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
701                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
702                         short_channel_id: 3,
703                         timestamp: 1,
704                         flags: 0,
705                         cltv_expiry_delta: (3 << 8) | 1,
706                         htlc_minimum_msat: 0,
707                         htlc_maximum_msat: OptionalField::Absent,
708                         fee_base_msat: 0,
709                         fee_proportional_millionths: 0,
710                         excess_data: Vec::new()
711                 });
712                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
713                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
714                         short_channel_id: 3,
715                         timestamp: 1,
716                         flags: 1,
717                         cltv_expiry_delta: (3 << 8) | 2,
718                         htlc_minimum_msat: 0,
719                         htlc_maximum_msat: OptionalField::Absent,
720                         fee_base_msat: 100,
721                         fee_proportional_millionths: 0,
722                         excess_data: Vec::new()
723                 });
724
725                 add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[1], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(4)), 4);
726                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
727                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
728                         short_channel_id: 4,
729                         timestamp: 1,
730                         flags: 0,
731                         cltv_expiry_delta: (4 << 8) | 1,
732                         htlc_minimum_msat: 0,
733                         htlc_maximum_msat: OptionalField::Absent,
734                         fee_base_msat: 0,
735                         fee_proportional_millionths: 1000000,
736                         excess_data: Vec::new()
737                 });
738                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
739                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
740                         short_channel_id: 4,
741                         timestamp: 1,
742                         flags: 1,
743                         cltv_expiry_delta: (4 << 8) | 2,
744                         htlc_minimum_msat: 0,
745                         htlc_maximum_msat: OptionalField::Absent,
746                         fee_base_msat: 0,
747                         fee_proportional_millionths: 0,
748                         excess_data: Vec::new()
749                 });
750
751                 add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[7], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(13)), 13);
752                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[7], UnsignedChannelUpdate {
753                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
754                         short_channel_id: 13,
755                         timestamp: 1,
756                         flags: 0,
757                         cltv_expiry_delta: (13 << 8) | 1,
758                         htlc_minimum_msat: 0,
759                         htlc_maximum_msat: OptionalField::Absent,
760                         fee_base_msat: 0,
761                         fee_proportional_millionths: 2000000,
762                         excess_data: Vec::new()
763                 });
764                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
765                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
766                         short_channel_id: 13,
767                         timestamp: 1,
768                         flags: 1,
769                         cltv_expiry_delta: (13 << 8) | 2,
770                         htlc_minimum_msat: 0,
771                         htlc_maximum_msat: OptionalField::Absent,
772                         fee_base_msat: 0,
773                         fee_proportional_millionths: 0,
774                         excess_data: Vec::new()
775                 });
776
777                 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[2], NodeFeatures::from_le_bytes(id_to_feature_flags(3)), 0);
778
779                 add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], &privkeys[4], ChannelFeatures::from_le_bytes(id_to_feature_flags(6)), 6);
780                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
781                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
782                         short_channel_id: 6,
783                         timestamp: 1,
784                         flags: 0,
785                         cltv_expiry_delta: (6 << 8) | 1,
786                         htlc_minimum_msat: 0,
787                         htlc_maximum_msat: OptionalField::Absent,
788                         fee_base_msat: 0,
789                         fee_proportional_millionths: 0,
790                         excess_data: Vec::new()
791                 });
792                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[4], UnsignedChannelUpdate {
793                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
794                         short_channel_id: 6,
795                         timestamp: 1,
796                         flags: 1,
797                         cltv_expiry_delta: (6 << 8) | 2,
798                         htlc_minimum_msat: 0,
799                         htlc_maximum_msat: OptionalField::Absent,
800                         fee_base_msat: 0,
801                         fee_proportional_millionths: 0,
802                         excess_data: Vec::new(),
803                 });
804
805                 add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[4], &privkeys[3], ChannelFeatures::from_le_bytes(id_to_feature_flags(11)), 11);
806                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[4], UnsignedChannelUpdate {
807                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
808                         short_channel_id: 11,
809                         timestamp: 1,
810                         flags: 0,
811                         cltv_expiry_delta: (11 << 8) | 1,
812                         htlc_minimum_msat: 0,
813                         htlc_maximum_msat: OptionalField::Absent,
814                         fee_base_msat: 0,
815                         fee_proportional_millionths: 0,
816                         excess_data: Vec::new()
817                 });
818                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[3], UnsignedChannelUpdate {
819                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
820                         short_channel_id: 11,
821                         timestamp: 1,
822                         flags: 1,
823                         cltv_expiry_delta: (11 << 8) | 2,
824                         htlc_minimum_msat: 0,
825                         htlc_maximum_msat: OptionalField::Absent,
826                         fee_base_msat: 0,
827                         fee_proportional_millionths: 0,
828                         excess_data: Vec::new()
829                 });
830
831                 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[4], NodeFeatures::from_le_bytes(id_to_feature_flags(5)), 0);
832
833                 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[3], NodeFeatures::from_le_bytes(id_to_feature_flags(4)), 0);
834
835                 add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], &privkeys[5], ChannelFeatures::from_le_bytes(id_to_feature_flags(7)), 7);
836                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
837                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
838                         short_channel_id: 7,
839                         timestamp: 1,
840                         flags: 0,
841                         cltv_expiry_delta: (7 << 8) | 1,
842                         htlc_minimum_msat: 0,
843                         htlc_maximum_msat: OptionalField::Absent,
844                         fee_base_msat: 0,
845                         fee_proportional_millionths: 1000000,
846                         excess_data: Vec::new()
847                 });
848                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[5], UnsignedChannelUpdate {
849                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
850                         short_channel_id: 7,
851                         timestamp: 1,
852                         flags: 1,
853                         cltv_expiry_delta: (7 << 8) | 2,
854                         htlc_minimum_msat: 0,
855                         htlc_maximum_msat: OptionalField::Absent,
856                         fee_base_msat: 0,
857                         fee_proportional_millionths: 0,
858                         excess_data: Vec::new()
859                 });
860
861                 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[5], NodeFeatures::from_le_bytes(id_to_feature_flags(6)), 0);
862
863                 (secp_ctx, net_graph_msg_handler, logger)
864         }
865
866         #[test]
867         fn simple_route_test() {
868                 let (secp_ctx, net_graph_msg_handler, logger) = build_graph();
869                 let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
870
871                 // Simple route to 3 via 2
872                 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], None, &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
873                 assert_eq!(route.paths[0].len(), 2);
874
875                 assert_eq!(route.paths[0][0].pubkey, nodes[1]);
876                 assert_eq!(route.paths[0][0].short_channel_id, 2);
877                 assert_eq!(route.paths[0][0].fee_msat, 100);
878                 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
879                 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
880                 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
881
882                 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
883                 assert_eq!(route.paths[0][1].short_channel_id, 4);
884                 assert_eq!(route.paths[0][1].fee_msat, 100);
885                 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
886                 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
887                 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
888         }
889
890         #[test]
891         fn disable_channels_test() {
892                 let (secp_ctx, net_graph_msg_handler, logger) = build_graph();
893                 let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx);
894
895                 // // Disable channels 4 and 12 by flags=2
896                 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
897                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
898                         short_channel_id: 4,
899                         timestamp: 2,
900                         flags: 2, // to disable
901                         cltv_expiry_delta: 0,
902                         htlc_minimum_msat: 0,
903                         htlc_maximum_msat: OptionalField::Absent,
904                         fee_base_msat: 0,
905                         fee_proportional_millionths: 0,
906                         excess_data: Vec::new()
907                 });
908                 update_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
909                         chain_hash: genesis_block(Network::Testnet).header.block_hash(),
910                         short_channel_id: 12,
911                         timestamp: 2,
912                         flags: 2, // to disable
913                         cltv_expiry_delta: 0,
914                         htlc_minimum_msat: 0,
915                         htlc_maximum_msat: OptionalField::Absent,
916                         fee_base_msat: 0,
917                         fee_proportional_millionths: 0,
918                         excess_data: Vec::new()
919                 });
920
921                 // If all the channels require some features we don't understand, route should fail
922                 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], None, &Vec::new(), 100, 42, Arc::clone(&logger)) {
923                         assert_eq!(err, "Failed to find a path to the given destination");
924                 } else { panic!(); }
925
926                 // If we specify a channel to node7, that overrides our local channel view and that gets used
927                 let our_chans = vec![channelmanager::ChannelDetails {
928                         channel_id: [0; 32],
929                         short_channel_id: Some(42),
930                         remote_network_id: nodes[7].clone(),
931                         counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
932                         channel_value_satoshis: 0,
933                         user_id: 0,
934                         outbound_capacity_msat: 0,
935                         inbound_capacity_msat: 0,
936                         is_live: true,
937                 }];
938                 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], Some(&our_chans.iter().collect::<Vec<_>>()),  &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
939                 assert_eq!(route.paths[0].len(), 2);
940
941                 assert_eq!(route.paths[0][0].pubkey, nodes[7]);
942                 assert_eq!(route.paths[0][0].short_channel_id, 42);
943                 assert_eq!(route.paths[0][0].fee_msat, 200);
944                 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
945                 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
946                 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
947
948                 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
949                 assert_eq!(route.paths[0][1].short_channel_id, 13);
950                 assert_eq!(route.paths[0][1].fee_msat, 100);
951                 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
952                 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
953                 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
954         }
955
956         #[test]
957         fn disable_node_test() {
958                 let (secp_ctx, net_graph_msg_handler, logger) = build_graph();
959                 let (_, our_id, privkeys, nodes) = get_nodes(&secp_ctx);
960
961                 // Disable nodes 1, 2, and 8 by requiring unknown feature bits
962                 let mut unknown_features = NodeFeatures::known();
963                 unknown_features.set_required_unknown_bits();
964                 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[0], unknown_features.clone(), 1);
965                 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[1], unknown_features.clone(), 1);
966                 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[7], unknown_features.clone(), 1);
967
968                 // If all nodes require some features we don't understand, route should fail
969                 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], None, &Vec::new(), 100, 42, Arc::clone(&logger)) {
970                         assert_eq!(err, "Failed to find a path to the given destination");
971                 } else { panic!(); }
972
973                 // If we specify a channel to node7, that overrides our local channel view and that gets used
974                 let our_chans = vec![channelmanager::ChannelDetails {
975                         channel_id: [0; 32],
976                         short_channel_id: Some(42),
977                         remote_network_id: nodes[7].clone(),
978                         counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
979                         channel_value_satoshis: 0,
980                         user_id: 0,
981                         outbound_capacity_msat: 0,
982                         inbound_capacity_msat: 0,
983                         is_live: true,
984                 }];
985                 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], Some(&our_chans.iter().collect::<Vec<_>>()), &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
986                 assert_eq!(route.paths[0].len(), 2);
987
988                 assert_eq!(route.paths[0][0].pubkey, nodes[7]);
989                 assert_eq!(route.paths[0][0].short_channel_id, 42);
990                 assert_eq!(route.paths[0][0].fee_msat, 200);
991                 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
992                 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
993                 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
994
995                 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
996                 assert_eq!(route.paths[0][1].short_channel_id, 13);
997                 assert_eq!(route.paths[0][1].fee_msat, 100);
998                 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
999                 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
1000                 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
1001
1002                 // Note that we don't test disabling node 3 and failing to route to it, as we (somewhat
1003                 // naively) assume that the user checked the feature bits on the invoice, which override
1004                 // the node_announcement.
1005         }
1006
1007         #[test]
1008         fn our_chans_test() {
1009                 let (secp_ctx, net_graph_msg_handler, logger) = build_graph();
1010                 let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
1011
1012                 // Route to 1 via 2 and 3 because our channel to 1 is disabled
1013                 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[0], None, &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
1014                 assert_eq!(route.paths[0].len(), 3);
1015
1016                 assert_eq!(route.paths[0][0].pubkey, nodes[1]);
1017                 assert_eq!(route.paths[0][0].short_channel_id, 2);
1018                 assert_eq!(route.paths[0][0].fee_msat, 200);
1019                 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1020                 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
1021                 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
1022
1023                 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
1024                 assert_eq!(route.paths[0][1].short_channel_id, 4);
1025                 assert_eq!(route.paths[0][1].fee_msat, 100);
1026                 assert_eq!(route.paths[0][1].cltv_expiry_delta, (3 << 8) | 2);
1027                 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
1028                 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
1029
1030                 assert_eq!(route.paths[0][2].pubkey, nodes[0]);
1031                 assert_eq!(route.paths[0][2].short_channel_id, 3);
1032                 assert_eq!(route.paths[0][2].fee_msat, 100);
1033                 assert_eq!(route.paths[0][2].cltv_expiry_delta, 42);
1034                 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(1));
1035                 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(3));
1036
1037                 // If we specify a channel to node7, that overrides our local channel view and that gets used
1038                 let our_chans = vec![channelmanager::ChannelDetails {
1039                         channel_id: [0; 32],
1040                         short_channel_id: Some(42),
1041                         remote_network_id: nodes[7].clone(),
1042                         counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1043                         channel_value_satoshis: 0,
1044                         user_id: 0,
1045                         outbound_capacity_msat: 0,
1046                         inbound_capacity_msat: 0,
1047                         is_live: true,
1048                 }];
1049                 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], Some(&our_chans.iter().collect::<Vec<_>>()), &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
1050                 assert_eq!(route.paths[0].len(), 2);
1051
1052                 assert_eq!(route.paths[0][0].pubkey, nodes[7]);
1053                 assert_eq!(route.paths[0][0].short_channel_id, 42);
1054                 assert_eq!(route.paths[0][0].fee_msat, 200);
1055                 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
1056                 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
1057                 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
1058
1059                 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
1060                 assert_eq!(route.paths[0][1].short_channel_id, 13);
1061                 assert_eq!(route.paths[0][1].fee_msat, 100);
1062                 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1063                 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
1064                 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
1065         }
1066
1067         fn last_hops(nodes: &Vec<PublicKey>) -> Vec<RouteHint> {
1068                 let zero_fees = RoutingFees {
1069                         base_msat: 0,
1070                         proportional_millionths: 0,
1071                 };
1072                 vec!(RouteHint {
1073                         src_node_id: nodes[3].clone(),
1074                         short_channel_id: 8,
1075                         fees: zero_fees,
1076                         cltv_expiry_delta: (8 << 8) | 1,
1077                         htlc_minimum_msat: None,
1078                         htlc_maximum_msat: None,
1079                 }, RouteHint {
1080                         src_node_id: nodes[4].clone(),
1081                         short_channel_id: 9,
1082                         fees: RoutingFees {
1083                                 base_msat: 1001,
1084                                 proportional_millionths: 0,
1085                         },
1086                         cltv_expiry_delta: (9 << 8) | 1,
1087                         htlc_minimum_msat: None,
1088                         htlc_maximum_msat: None,
1089                 }, RouteHint {
1090                         src_node_id: nodes[5].clone(),
1091                         short_channel_id: 10,
1092                         fees: zero_fees,
1093                         cltv_expiry_delta: (10 << 8) | 1,
1094                         htlc_minimum_msat: None,
1095                         htlc_maximum_msat: None,
1096                 })
1097         }
1098
1099         #[test]
1100         fn last_hops_test() {
1101                 let (secp_ctx, net_graph_msg_handler, logger) = build_graph();
1102                 let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
1103
1104                 // Simple test across 2, 3, 5, and 4 via a last_hop channel
1105                 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[6], None, &last_hops(&nodes).iter().collect::<Vec<_>>(), 100, 42, Arc::clone(&logger)).unwrap();
1106                 assert_eq!(route.paths[0].len(), 5);
1107
1108                 assert_eq!(route.paths[0][0].pubkey, nodes[1]);
1109                 assert_eq!(route.paths[0][0].short_channel_id, 2);
1110                 assert_eq!(route.paths[0][0].fee_msat, 100);
1111                 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1112                 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
1113                 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
1114
1115                 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
1116                 assert_eq!(route.paths[0][1].short_channel_id, 4);
1117                 assert_eq!(route.paths[0][1].fee_msat, 0);
1118                 assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1);
1119                 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
1120                 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
1121
1122                 assert_eq!(route.paths[0][2].pubkey, nodes[4]);
1123                 assert_eq!(route.paths[0][2].short_channel_id, 6);
1124                 assert_eq!(route.paths[0][2].fee_msat, 0);
1125                 assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1);
1126                 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(5));
1127                 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(6));
1128
1129                 assert_eq!(route.paths[0][3].pubkey, nodes[3]);
1130                 assert_eq!(route.paths[0][3].short_channel_id, 11);
1131                 assert_eq!(route.paths[0][3].fee_msat, 0);
1132                 assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1);
1133                 // If we have a peer in the node map, we'll use their features here since we don't have
1134                 // a way of figuring out their features from the invoice:
1135                 assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags(4));
1136                 assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags(11));
1137
1138                 assert_eq!(route.paths[0][4].pubkey, nodes[6]);
1139                 assert_eq!(route.paths[0][4].short_channel_id, 8);
1140                 assert_eq!(route.paths[0][4].fee_msat, 100);
1141                 assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
1142                 assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1143                 assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1144         }
1145
1146         #[test]
1147         fn our_chans_last_hop_connect_test() {
1148                 let (secp_ctx, net_graph_msg_handler, logger) = build_graph();
1149                 let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
1150
1151                 // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1152                 let our_chans = vec![channelmanager::ChannelDetails {
1153                         channel_id: [0; 32],
1154                         short_channel_id: Some(42),
1155                         remote_network_id: nodes[3].clone(),
1156                         counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1157                         channel_value_satoshis: 0,
1158                         user_id: 0,
1159                         outbound_capacity_msat: 0,
1160                         inbound_capacity_msat: 0,
1161                         is_live: true,
1162                 }];
1163                 let mut last_hops = last_hops(&nodes);
1164                 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[6], Some(&our_chans.iter().collect::<Vec<_>>()), &last_hops.iter().collect::<Vec<_>>(), 100, 42, Arc::clone(&logger)).unwrap();
1165                 assert_eq!(route.paths[0].len(), 2);
1166
1167                 assert_eq!(route.paths[0][0].pubkey, nodes[3]);
1168                 assert_eq!(route.paths[0][0].short_channel_id, 42);
1169                 assert_eq!(route.paths[0][0].fee_msat, 0);
1170                 assert_eq!(route.paths[0][0].cltv_expiry_delta, (8 << 8) | 1);
1171                 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
1172                 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
1173
1174                 assert_eq!(route.paths[0][1].pubkey, nodes[6]);
1175                 assert_eq!(route.paths[0][1].short_channel_id, 8);
1176                 assert_eq!(route.paths[0][1].fee_msat, 100);
1177                 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1178                 assert_eq!(route.paths[0][1].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1179                 assert_eq!(route.paths[0][1].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1180
1181                 last_hops[0].fees.base_msat = 1000;
1182
1183                 // Revert to via 6 as the fee on 8 goes up
1184                 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[6], None, &last_hops.iter().collect::<Vec<_>>(), 100, 42, Arc::clone(&logger)).unwrap();
1185                 assert_eq!(route.paths[0].len(), 4);
1186
1187                 assert_eq!(route.paths[0][0].pubkey, nodes[1]);
1188                 assert_eq!(route.paths[0][0].short_channel_id, 2);
1189                 assert_eq!(route.paths[0][0].fee_msat, 200); // fee increased as its % of value transferred across node
1190                 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1191                 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
1192                 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
1193
1194                 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
1195                 assert_eq!(route.paths[0][1].short_channel_id, 4);
1196                 assert_eq!(route.paths[0][1].fee_msat, 100);
1197                 assert_eq!(route.paths[0][1].cltv_expiry_delta, (7 << 8) | 1);
1198                 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
1199                 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
1200
1201                 assert_eq!(route.paths[0][2].pubkey, nodes[5]);
1202                 assert_eq!(route.paths[0][2].short_channel_id, 7);
1203                 assert_eq!(route.paths[0][2].fee_msat, 0);
1204                 assert_eq!(route.paths[0][2].cltv_expiry_delta, (10 << 8) | 1);
1205                 // If we have a peer in the node map, we'll use their features here since we don't have
1206                 // a way of figuring out their features from the invoice:
1207                 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(6));
1208                 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(7));
1209
1210                 assert_eq!(route.paths[0][3].pubkey, nodes[6]);
1211                 assert_eq!(route.paths[0][3].short_channel_id, 10);
1212                 assert_eq!(route.paths[0][3].fee_msat, 100);
1213                 assert_eq!(route.paths[0][3].cltv_expiry_delta, 42);
1214                 assert_eq!(route.paths[0][3].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1215                 assert_eq!(route.paths[0][3].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1216
1217                 // ...but still use 8 for larger payments as 6 has a variable feerate
1218                 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[6], None, &last_hops.iter().collect::<Vec<_>>(), 2000, 42, Arc::clone(&logger)).unwrap();
1219                 assert_eq!(route.paths[0].len(), 5);
1220
1221                 assert_eq!(route.paths[0][0].pubkey, nodes[1]);
1222                 assert_eq!(route.paths[0][0].short_channel_id, 2);
1223                 assert_eq!(route.paths[0][0].fee_msat, 3000);
1224                 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1225                 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
1226                 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
1227
1228                 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
1229                 assert_eq!(route.paths[0][1].short_channel_id, 4);
1230                 assert_eq!(route.paths[0][1].fee_msat, 0);
1231                 assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1);
1232                 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
1233                 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
1234
1235                 assert_eq!(route.paths[0][2].pubkey, nodes[4]);
1236                 assert_eq!(route.paths[0][2].short_channel_id, 6);
1237                 assert_eq!(route.paths[0][2].fee_msat, 0);
1238                 assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1);
1239                 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(5));
1240                 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(6));
1241
1242                 assert_eq!(route.paths[0][3].pubkey, nodes[3]);
1243                 assert_eq!(route.paths[0][3].short_channel_id, 11);
1244                 assert_eq!(route.paths[0][3].fee_msat, 1000);
1245                 assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1);
1246                 // If we have a peer in the node map, we'll use their features here since we don't have
1247                 // a way of figuring out their features from the invoice:
1248                 assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags(4));
1249                 assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags(11));
1250
1251                 assert_eq!(route.paths[0][4].pubkey, nodes[6]);
1252                 assert_eq!(route.paths[0][4].short_channel_id, 8);
1253                 assert_eq!(route.paths[0][4].fee_msat, 2000);
1254                 assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
1255                 assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1256                 assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1257         }
1258
1259         #[test]
1260         fn unannounced_path_test() {
1261                 // We should be able to send a payment to a destination without any help of a routing graph
1262                 // if we have a channel with a common counterparty that appears in the first and last hop
1263                 // hints.
1264                 let source_node_id = PublicKey::from_secret_key(&Secp256k1::new(), &SecretKey::from_slice(&hex::decode(format!("{:02}", 41).repeat(32)).unwrap()[..]).unwrap());
1265                 let middle_node_id = PublicKey::from_secret_key(&Secp256k1::new(), &SecretKey::from_slice(&hex::decode(format!("{:02}", 42).repeat(32)).unwrap()[..]).unwrap());
1266                 let target_node_id = PublicKey::from_secret_key(&Secp256k1::new(), &SecretKey::from_slice(&hex::decode(format!("{:02}", 43).repeat(32)).unwrap()[..]).unwrap());
1267
1268                 // If we specify a channel to a middle hop, that overrides our local channel view and that gets used
1269                 let last_hops = vec![RouteHint {
1270                         src_node_id: middle_node_id,
1271                         short_channel_id: 8,
1272                         fees: RoutingFees {
1273                                 base_msat: 1000,
1274                                 proportional_millionths: 0,
1275                         },
1276                         cltv_expiry_delta: (8 << 8) | 1,
1277                         htlc_minimum_msat: None,
1278                         htlc_maximum_msat: None,
1279                 }];
1280                 let our_chans = vec![channelmanager::ChannelDetails {
1281                         channel_id: [0; 32],
1282                         short_channel_id: Some(42),
1283                         remote_network_id: middle_node_id,
1284                         counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1285                         channel_value_satoshis: 100000,
1286                         user_id: 0,
1287                         outbound_capacity_msat: 100000,
1288                         inbound_capacity_msat: 100000,
1289                         is_live: true,
1290                 }];
1291                 let route = get_route(&source_node_id, &NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash()), &target_node_id, Some(&our_chans.iter().collect::<Vec<_>>()), &last_hops.iter().collect::<Vec<_>>(), 100, 42, Arc::new(test_utils::TestLogger::new())).unwrap();
1292
1293                 assert_eq!(route.paths[0].len(), 2);
1294
1295                 assert_eq!(route.paths[0][0].pubkey, middle_node_id);
1296                 assert_eq!(route.paths[0][0].short_channel_id, 42);
1297                 assert_eq!(route.paths[0][0].fee_msat, 1000);
1298                 assert_eq!(route.paths[0][0].cltv_expiry_delta, (8 << 8) | 1);
1299                 assert_eq!(route.paths[0][0].node_features.le_flags(), &[0b11]);
1300                 assert_eq!(route.paths[0][0].channel_features.le_flags(), &[0; 0]); // We can't learn any flags from invoices, sadly
1301
1302                 assert_eq!(route.paths[0][1].pubkey, target_node_id);
1303                 assert_eq!(route.paths[0][1].short_channel_id, 8);
1304                 assert_eq!(route.paths[0][1].fee_msat, 100);
1305                 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1306                 assert_eq!(route.paths[0][1].node_features.le_flags(), &[0; 0]); // We dont pass flags in from invoices yet
1307                 assert_eq!(route.paths[0][1].channel_features.le_flags(), &[0; 0]); // We can't learn any flags from invoices, sadly
1308         }
1309 }