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