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