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