1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! The top-level routing/network map tracking logic lives here.
12 //! You probably want to create a NetGraphMsgHandler and use that as your RoutingMessageHandler and then
13 //! interrogate it to get routes for your own payments.
15 use bitcoin::secp256k1::key::PublicKey;
17 use ln::channelmanager;
18 use ln::features::{ChannelFeatures, NodeFeatures};
19 use ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
20 use routing::network_graph::{NetworkGraph, RoutingFees};
21 use util::ser::{Writeable, Readable};
22 use util::logger::Logger;
25 use std::collections::{HashMap,BinaryHeap};
29 #[derive(Clone, PartialEq)]
31 /// The node_id of the node at this hop.
32 pub pubkey: PublicKey,
33 /// The node_announcement features of the node at this hop. For the last hop, these may be
34 /// amended to match the features present in the invoice this node generated.
35 pub node_features: NodeFeatures,
36 /// The channel that should be used from the previous hop to reach this node.
37 pub short_channel_id: u64,
38 /// The channel_announcement features of the channel that should be used from the previous hop
39 /// to reach this node.
40 pub channel_features: ChannelFeatures,
41 /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
43 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
44 /// expected at the destination, in excess of the current block height.
45 pub cltv_expiry_delta: u32,
48 impl Writeable for Vec<RouteHop> {
49 fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
50 (self.len() as u8).write(writer)?;
51 for hop in self.iter() {
52 hop.pubkey.write(writer)?;
53 hop.node_features.write(writer)?;
54 hop.short_channel_id.write(writer)?;
55 hop.channel_features.write(writer)?;
56 hop.fee_msat.write(writer)?;
57 hop.cltv_expiry_delta.write(writer)?;
63 impl Readable for Vec<RouteHop> {
64 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Vec<RouteHop>, DecodeError> {
65 let hops_count: u8 = Readable::read(reader)?;
66 let mut hops = Vec::with_capacity(hops_count as usize);
67 for _ in 0..hops_count {
69 pubkey: Readable::read(reader)?,
70 node_features: Readable::read(reader)?,
71 short_channel_id: Readable::read(reader)?,
72 channel_features: Readable::read(reader)?,
73 fee_msat: Readable::read(reader)?,
74 cltv_expiry_delta: Readable::read(reader)?,
81 /// A route directs a payment from the sender (us) to the recipient. If the recipient supports MPP,
82 /// it can take multiple paths. Each path is composed of one or more hops through the network.
83 #[derive(Clone, PartialEq)]
85 /// The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
86 /// last RouteHop in each path must be the same.
87 /// Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
88 /// destination. Thus, this must always be at least length one. While the maximum length of any
89 /// given path is variable, keeping the length of any path to less than 20 should currently
90 /// ensure it is viable.
91 pub paths: Vec<Vec<RouteHop>>,
94 impl Writeable for Route {
95 fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
96 (self.paths.len() as u64).write(writer)?;
97 for hops in self.paths.iter() {
104 impl Readable for Route {
105 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Route, DecodeError> {
106 let path_count: u64 = Readable::read(reader)?;
107 let mut paths = Vec::with_capacity(cmp::min(path_count, 128) as usize);
108 for _ in 0..path_count {
109 paths.push(Readable::read(reader)?);
115 /// A channel descriptor which provides a last-hop route to get_route
116 pub struct RouteHint {
117 /// The node_id of the non-target end of the route
118 pub src_node_id: PublicKey,
119 /// The short_channel_id of this channel
120 pub short_channel_id: u64,
121 /// The fees which must be paid to use this channel
122 pub fees: RoutingFees,
123 /// The difference in CLTV values between this node and the next node.
124 pub cltv_expiry_delta: u16,
125 /// The minimum value, in msat, which must be relayed to the next hop.
126 pub htlc_minimum_msat: u64,
129 #[derive(Eq, PartialEq)]
130 struct RouteGraphNode {
132 lowest_fee_to_peer_through_node: u64,
133 lowest_fee_to_node: u64,
136 impl cmp::Ord for RouteGraphNode {
137 fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
138 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
139 .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
143 impl cmp::PartialOrd for RouteGraphNode {
144 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
145 Some(self.cmp(other))
149 struct DummyDirectionalChannelInfo {
150 cltv_expiry_delta: u32,
151 htlc_minimum_msat: u64,
156 /// Gets a route from us to the given target node.
158 /// Extra routing hops between known nodes and the target will be used if they are included in
161 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
162 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
163 /// view of our local channels (from net_graph_msg_handler) will be ignored, and only those in first_hops
166 /// Panics if first_hops contains channels without short_channel_ids
167 /// (ChannelManager::list_usable_channels will never include such channels).
169 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
170 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
171 /// *is* checked as they may change based on the receiving node.
172 pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, target: &PublicKey, first_hops: Option<&[channelmanager::ChannelDetails]>,
173 last_hops: &[RouteHint], final_value_msat: u64, final_cltv: u32, logger: L) -> Result<Route, LightningError> where L::Target: Logger {
174 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
175 // uptime/success in using a node in the past.
176 if *target == *our_node_id {
177 return Err(LightningError{err: "Cannot generate a route to ourselves".to_owned(), action: ErrorAction::IgnoreError});
180 if final_value_msat > MAX_VALUE_MSAT {
181 return Err(LightningError{err: "Cannot generate a route of more value than all existing satoshis".to_owned(), action: ErrorAction::IgnoreError});
184 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
185 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
186 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
187 // to use as the A* heuristic beyond just the cost to get one node further than the current
190 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
191 cltv_expiry_delta: 0,
192 htlc_minimum_msat: 0,
195 proportional_millionths: 0,
199 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
200 let mut dist = HashMap::with_capacity(network.get_nodes().len());
202 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
203 if let Some(hops) = first_hops {
205 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
206 if chan.remote_network_id == *target {
208 paths: vec![vec![RouteHop {
209 pubkey: chan.remote_network_id,
210 node_features: chan.counterparty_features.to_context(),
212 channel_features: chan.counterparty_features.to_context(),
213 fee_msat: final_value_msat,
214 cltv_expiry_delta: final_cltv,
218 first_hop_targets.insert(chan.remote_network_id, (short_channel_id, chan.counterparty_features.clone()));
220 if first_hop_targets.is_empty() {
221 return Err(LightningError{err: "Cannot route when there are no outbound routes away from us".to_owned(), action: ErrorAction::IgnoreError});
225 macro_rules! add_entry {
226 // Adds entry which goes from $src_node_id to $dest_node_id
227 // over the channel with id $chan_id with fees described in
228 // $directional_info.
229 ( $chan_id: expr, $src_node_id: expr, $dest_node_id: expr, $directional_info: expr, $chan_features: expr, $starting_fee_msat: expr ) => {
230 //TODO: Explore simply adding fee to hit htlc_minimum_msat
231 if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
232 let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fees.proportional_millionths as u64);
233 if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
234 ($directional_info.fees.base_msat as u64).checked_add(part / 1000000) })
236 let mut total_fee = $starting_fee_msat as u64;
237 let hm_entry = dist.entry(&$src_node_id);
238 let old_entry = hm_entry.or_insert_with(|| {
239 let node = network.get_nodes().get(&$src_node_id).unwrap();
240 let mut fee_base_msat = u32::max_value();
241 let mut fee_proportional_millionths = u32::max_value();
242 if let Some(fees) = node.lowest_inbound_channel_fees {
243 fee_base_msat = fees.base_msat;
244 fee_proportional_millionths = fees.proportional_millionths;
248 fee_proportional_millionths,
250 pubkey: $dest_node_id.clone(),
251 node_features: NodeFeatures::empty(),
253 channel_features: $chan_features.clone(),
255 cltv_expiry_delta: 0,
259 if $src_node_id != *our_node_id {
260 // Ignore new_fee for channel-from-us as we assume all channels-from-us
261 // will have the same effective-fee
262 total_fee += new_fee;
263 if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
264 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
266 // max_value means we'll always fail the old_entry.0 > total_fee check
267 total_fee = u64::max_value();
270 let new_graph_node = RouteGraphNode {
271 pubkey: $src_node_id,
272 lowest_fee_to_peer_through_node: total_fee,
273 lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
275 if old_entry.0 > total_fee {
276 targets.push(new_graph_node);
277 old_entry.0 = total_fee;
278 old_entry.3 = RouteHop {
279 pubkey: $dest_node_id.clone(),
280 node_features: NodeFeatures::empty(),
281 short_channel_id: $chan_id.clone(),
282 channel_features: $chan_features.clone(),
283 fee_msat: new_fee, // This field is ignored on the last-hop anyway
284 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
292 macro_rules! add_entries_to_cheapest_to_target_node {
293 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
294 if first_hops.is_some() {
295 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&$node_id) {
296 add_entry!(first_hop, *our_node_id, $node_id, dummy_directional_info, features.to_context(), $fee_to_target_msat);
301 if let Some(node_info) = $node.announcement_info.as_ref() {
302 features = node_info.features.clone();
304 features = NodeFeatures::empty();
307 if !features.requires_unknown_bits() {
308 for chan_id in $node.channels.iter() {
309 let chan = network.get_channels().get(chan_id).unwrap();
310 if !chan.features.requires_unknown_bits() {
311 if chan.node_one == *$node_id {
312 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
313 if first_hops.is_none() || chan.node_two != *our_node_id {
314 if let Some(two_to_one) = chan.two_to_one.as_ref() {
315 if two_to_one.enabled {
316 add_entry!(chan_id, chan.node_two, chan.node_one, two_to_one, chan.features, $fee_to_target_msat);
321 if first_hops.is_none() || chan.node_one != *our_node_id {
322 if let Some(one_to_two) = chan.one_to_two.as_ref() {
323 if one_to_two.enabled {
324 add_entry!(chan_id, chan.node_one, chan.node_two, one_to_two, chan.features, $fee_to_target_msat);
336 match network.get_nodes().get(target) {
339 add_entries_to_cheapest_to_target_node!(node, target, 0);
343 for hop in last_hops.iter() {
344 if first_hops.is_none() || hop.src_node_id != *our_node_id { // first_hop overrules last_hops
345 if network.get_nodes().get(&hop.src_node_id).is_some() {
346 if first_hops.is_some() {
347 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&hop.src_node_id) {
348 // Currently there are no channel-context features defined, so we are a
349 // bit lazy here. In the future, we should pull them out via our
350 // ChannelManager, but there's no reason to waste the space until we
352 add_entry!(first_hop, *our_node_id , hop.src_node_id, dummy_directional_info, features.to_context(), 0);
355 // BOLT 11 doesn't allow inclusion of features for the last hop hints, which
356 // really sucks, cause we're gonna need that eventually.
357 add_entry!(hop.short_channel_id, hop.src_node_id, target, hop, ChannelFeatures::empty(), 0);
362 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
363 if pubkey == *our_node_id {
364 let mut res = vec!(dist.remove(&our_node_id).unwrap().3);
366 if let Some(&(_, ref features)) = first_hop_targets.get(&res.last().unwrap().pubkey) {
367 res.last_mut().unwrap().node_features = features.to_context();
368 } else if let Some(node) = network.get_nodes().get(&res.last().unwrap().pubkey) {
369 if let Some(node_info) = node.announcement_info.as_ref() {
370 res.last_mut().unwrap().node_features = node_info.features.clone();
372 res.last_mut().unwrap().node_features = NodeFeatures::empty();
375 // We should be able to fill in features for everything except the last
376 // hop, if the last hop was provided via a BOLT 11 invoice (though we
377 // should be able to extend it further as BOLT 11 does have feature
378 // flags for the last hop node itself).
379 assert!(res.last().unwrap().pubkey == *target);
381 if res.last().unwrap().pubkey == *target {
385 let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
387 None => return Err(LightningError{err: "Failed to find a non-fee-overflowing path to the given destination".to_owned(), action: ErrorAction::IgnoreError}),
389 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
390 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
393 res.last_mut().unwrap().fee_msat = final_value_msat;
394 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
395 let route = Route { paths: vec![res] };
396 log_trace!(logger, "Got route: {}", log_route!(route));
400 match network.get_nodes().get(&pubkey) {
403 add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
408 Err(LightningError{err: "Failed to find a path to the given destination".to_owned(), action: ErrorAction::IgnoreError})
413 use chain::chaininterface;
414 use routing::router::{get_route, RouteHint, RoutingFees};
415 use routing::network_graph::NetGraphMsgHandler;
416 use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
417 use ln::msgs::{ErrorAction, LightningError, OptionalField, UnsignedChannelAnnouncement, ChannelAnnouncement, RoutingMessageHandler,
418 NodeAnnouncement, UnsignedNodeAnnouncement, ChannelUpdate, UnsignedChannelUpdate};
419 use ln::channelmanager;
420 use util::test_utils;
421 use util::ser::Writeable;
423 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
424 use bitcoin::hashes::Hash;
425 use bitcoin::network::constants::Network;
426 use bitcoin::blockdata::constants::genesis_block;
427 use bitcoin::util::hash::BitcoinHash;
431 use bitcoin::secp256k1::key::{PublicKey,SecretKey};
432 use bitcoin::secp256k1::{Secp256k1, All};
436 // Using the same keys for LN and BTC ids
437 fn add_channel(net_graph_msg_handler: &NetGraphMsgHandler<Arc<chaininterface::ChainWatchInterfaceUtil>, Arc<test_utils::TestLogger>>, secp_ctx: &Secp256k1<All>, node_1_privkey: &SecretKey,
438 node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64) {
439 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
440 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
442 let unsigned_announcement = UnsignedChannelAnnouncement {
444 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
448 bitcoin_key_1: node_id_1,
449 bitcoin_key_2: node_id_2,
450 excess_data: Vec::new(),
453 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
454 let valid_announcement = ChannelAnnouncement {
455 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
456 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
457 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
458 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
459 contents: unsigned_announcement.clone(),
461 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
462 Ok(res) => assert!(res),
467 fn update_channel(net_graph_msg_handler: &NetGraphMsgHandler<Arc<chaininterface::ChainWatchInterfaceUtil>, Arc<test_utils::TestLogger>>, secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, update: UnsignedChannelUpdate) {
468 let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]);
469 let valid_channel_update = ChannelUpdate {
470 signature: secp_ctx.sign(&msghash, node_privkey),
471 contents: update.clone()
474 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
475 Ok(res) => assert!(res),
476 // Err(_) => panic!()
477 Err(e) => println!("{:?}", e.err)
482 fn add_or_update_node(net_graph_msg_handler: &NetGraphMsgHandler<Arc<chaininterface::ChainWatchInterfaceUtil>, Arc<test_utils::TestLogger>>, secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey,
483 features: NodeFeatures, timestamp: u32) {
484 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
485 let unsigned_announcement = UnsignedNodeAnnouncement {
491 addresses: Vec::new(),
492 excess_address_data: Vec::new(),
493 excess_data: Vec::new(),
495 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
496 let valid_announcement = NodeAnnouncement {
497 signature: secp_ctx.sign(&msghash, node_privkey),
498 contents: unsigned_announcement.clone()
501 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
509 let secp_ctx = Secp256k1::new();
510 let our_privkey = &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
511 let our_id = PublicKey::from_secret_key(&secp_ctx, our_privkey);
512 let logger = Arc::new(test_utils::TestLogger::new());
513 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet));
514 let net_graph_msg_handler = NetGraphMsgHandler::new(chain_monitor, Arc::clone(&logger));
515 // Build network from our_id to node8:
517 // -1(1)2- node1 -1(3)2-
519 // our_id -1(12)2- node8 -1(13)2--- node3
521 // -1(2)2- node2 -1(4)2-
524 // chan1 1-to-2: disabled
525 // chan1 2-to-1: enabled, 0 fee
527 // chan2 1-to-2: enabled, ignored fee
528 // chan2 2-to-1: enabled, 0 fee
530 // chan3 1-to-2: enabled, 0 fee
531 // chan3 2-to-1: enabled, 100 msat fee
533 // chan4 1-to-2: enabled, 100% fee
534 // chan4 2-to-1: enabled, 0 fee
536 // chan12 1-to-2: enabled, ignored fee
537 // chan12 2-to-1: enabled, 0 fee
539 // chan13 1-to-2: enabled, 200% fee
540 // chan13 2-to-1: enabled, 0 fee
543 // -1(5)2- node4 -1(8)2--
547 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
549 // -1(7)2- node6 -1(10)2-
551 // chan5 1-to-2: enabled, 100 msat fee
552 // chan5 2-to-1: enabled, 0 fee
554 // chan6 1-to-2: enabled, 0 fee
555 // chan6 2-to-1: enabled, 0 fee
557 // chan7 1-to-2: enabled, 100% fee
558 // chan7 2-to-1: enabled, 0 fee
560 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
561 // chan8 2-to-1: enabled, 0 fee
563 // chan9 1-to-2: enabled, 1001 msat fee
564 // chan9 2-to-1: enabled, 0 fee
566 // chan10 1-to-2: enabled, 0 fee
567 // chan10 2-to-1: enabled, 0 fee
569 // chan11 1-to-2: enabled, 0 fee
570 // chan11 2-to-1: enabled, 0 fee
572 let node1_privkey = &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap();
573 let node2_privkey = &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap();
574 let node3_privkey = &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap();
575 let node4_privkey = &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap();
576 let node5_privkey = &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap();
577 let node6_privkey = &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap();
578 let node7_privkey = &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap();
579 let node8_privkey = &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap();
582 let node1 = PublicKey::from_secret_key(&secp_ctx, node1_privkey);
583 let node2 = PublicKey::from_secret_key(&secp_ctx, node2_privkey);
584 let node3 = PublicKey::from_secret_key(&secp_ctx, node3_privkey);
585 let node4 = PublicKey::from_secret_key(&secp_ctx, node4_privkey);
586 let node5 = PublicKey::from_secret_key(&secp_ctx, node5_privkey);
587 let node6 = PublicKey::from_secret_key(&secp_ctx, node6_privkey);
588 let node7 = PublicKey::from_secret_key(&secp_ctx, node7_privkey);
589 let node8 = PublicKey::from_secret_key(&secp_ctx, node8_privkey);
591 macro_rules! id_to_feature_flags {
592 // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can
593 // test for it later.
595 let idx = ($id - 1) * 2 + 1;
597 vec![1 << (idx - 8*3), 0, 0, 0]
598 } else if idx > 8*2 {
599 vec![1 << (idx - 8*2), 0, 0]
600 } else if idx > 8*1 {
601 vec![1 << (idx - 8*1), 0]
608 add_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, node1_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(1)), 1);
609 update_channel(&net_graph_msg_handler, &secp_ctx, node1_privkey, UnsignedChannelUpdate {
610 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
614 cltv_expiry_delta: 0,
615 htlc_minimum_msat: 0,
616 htlc_maximum_msat: OptionalField::Absent,
618 fee_proportional_millionths: 0,
619 excess_data: Vec::new()
621 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node1_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(1)), 0);
623 add_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, node2_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(2)), 2);
624 update_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, UnsignedChannelUpdate {
625 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
629 cltv_expiry_delta: u16::max_value(),
630 htlc_minimum_msat: 0,
631 htlc_maximum_msat: OptionalField::Absent,
632 fee_base_msat: u32::max_value(),
633 fee_proportional_millionths: u32::max_value(),
634 excess_data: Vec::new()
636 update_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, UnsignedChannelUpdate {
637 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
641 cltv_expiry_delta: 0,
642 htlc_minimum_msat: 0,
643 htlc_maximum_msat: OptionalField::Absent,
645 fee_proportional_millionths: 0,
646 excess_data: Vec::new()
649 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node2_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(2)), 0);
651 add_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, node8_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(12)), 12);
652 update_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, UnsignedChannelUpdate {
653 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
654 short_channel_id: 12,
657 cltv_expiry_delta: u16::max_value(),
658 htlc_minimum_msat: 0,
659 htlc_maximum_msat: OptionalField::Absent,
660 fee_base_msat: u32::max_value(),
661 fee_proportional_millionths: u32::max_value(),
662 excess_data: Vec::new()
664 update_channel(&net_graph_msg_handler, &secp_ctx, node8_privkey, UnsignedChannelUpdate {
665 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
666 short_channel_id: 12,
669 cltv_expiry_delta: 0,
670 htlc_minimum_msat: 0,
671 htlc_maximum_msat: OptionalField::Absent,
673 fee_proportional_millionths: 0,
674 excess_data: Vec::new()
678 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node8_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(8)), 0);
680 add_channel(&net_graph_msg_handler, &secp_ctx, node1_privkey, node3_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(3)), 3);
681 update_channel(&net_graph_msg_handler, &secp_ctx, node1_privkey, UnsignedChannelUpdate {
682 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
686 cltv_expiry_delta: (3 << 8) | 1,
687 htlc_minimum_msat: 0,
688 htlc_maximum_msat: OptionalField::Absent,
690 fee_proportional_millionths: 0,
691 excess_data: Vec::new()
693 update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate {
694 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
698 cltv_expiry_delta: (3 << 8) | 2,
699 htlc_minimum_msat: 0,
700 htlc_maximum_msat: OptionalField::Absent,
702 fee_proportional_millionths: 0,
703 excess_data: Vec::new()
707 add_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, node3_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(4)), 4);
708 update_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, UnsignedChannelUpdate {
709 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
713 cltv_expiry_delta: (4 << 8) | 1,
714 htlc_minimum_msat: 0,
715 htlc_maximum_msat: OptionalField::Absent,
717 fee_proportional_millionths: 1000000,
718 excess_data: Vec::new()
720 update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate {
721 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
725 cltv_expiry_delta: (4 << 8) | 2,
726 htlc_minimum_msat: 0,
727 htlc_maximum_msat: OptionalField::Absent,
729 fee_proportional_millionths: 0,
730 excess_data: Vec::new()
733 add_channel(&net_graph_msg_handler, &secp_ctx, node8_privkey, node3_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(13)), 13);
734 update_channel(&net_graph_msg_handler, &secp_ctx, node8_privkey, UnsignedChannelUpdate {
735 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
736 short_channel_id: 13,
739 cltv_expiry_delta: (13 << 8) | 1,
740 htlc_minimum_msat: 0,
741 htlc_maximum_msat: OptionalField::Absent,
743 fee_proportional_millionths: 2000000,
744 excess_data: Vec::new()
746 update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate {
747 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
748 short_channel_id: 13,
751 cltv_expiry_delta: (13 << 8) | 2,
752 htlc_minimum_msat: 0,
753 htlc_maximum_msat: OptionalField::Absent,
755 fee_proportional_millionths: 0,
756 excess_data: Vec::new()
758 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node3_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(3)), 0);
759 add_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, node5_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(6)), 6);
761 update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate {
762 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
766 cltv_expiry_delta: (6 << 8) | 1,
767 htlc_minimum_msat: 0,
768 htlc_maximum_msat: OptionalField::Absent,
770 fee_proportional_millionths: 0,
771 excess_data: Vec::new()
773 update_channel(&net_graph_msg_handler, &secp_ctx, node5_privkey, UnsignedChannelUpdate {
774 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
778 cltv_expiry_delta: (6 << 8) | 2,
779 htlc_minimum_msat: 0,
780 htlc_maximum_msat: OptionalField::Absent,
782 fee_proportional_millionths: 0,
783 excess_data: Vec::new()
786 add_channel(&net_graph_msg_handler, &secp_ctx, node5_privkey, node4_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(11)), 11);
787 update_channel(&net_graph_msg_handler, &secp_ctx, node5_privkey, UnsignedChannelUpdate {
788 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
789 short_channel_id: 11,
792 cltv_expiry_delta: (11 << 8) | 1,
793 htlc_minimum_msat: 0,
794 htlc_maximum_msat: OptionalField::Absent,
796 fee_proportional_millionths: 0,
797 excess_data: Vec::new()
799 update_channel(&net_graph_msg_handler, &secp_ctx, node4_privkey, UnsignedChannelUpdate {
800 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
801 short_channel_id: 11,
804 cltv_expiry_delta: (11 << 8) | 2,
805 htlc_minimum_msat: 0,
806 htlc_maximum_msat: OptionalField::Absent,
808 fee_proportional_millionths: 0,
809 excess_data: Vec::new()
811 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node5_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(5)), 0);
812 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node4_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(4)), 0);
814 add_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, node6_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(7)), 7);
816 update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate {
817 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
821 cltv_expiry_delta: (7 << 8) | 1,
822 htlc_minimum_msat: 0,
823 htlc_maximum_msat: OptionalField::Absent,
825 fee_proportional_millionths: 1000000,
826 excess_data: Vec::new()
828 update_channel(&net_graph_msg_handler, &secp_ctx, node6_privkey, UnsignedChannelUpdate {
829 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
833 cltv_expiry_delta: (7 << 8) | 2,
834 htlc_minimum_msat: 0,
835 htlc_maximum_msat: OptionalField::Absent,
837 fee_proportional_millionths: 0,
838 excess_data: Vec::new()
841 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node6_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(6)), 0);
843 // Simple route to 3 via 2
844 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node3, None, &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
845 assert_eq!(route.paths[0].len(), 2);
847 assert_eq!(route.paths[0][0].pubkey, node2);
848 assert_eq!(route.paths[0][0].short_channel_id, 2);
849 assert_eq!(route.paths[0][0].fee_msat, 100);
850 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
851 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
852 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
854 assert_eq!(route.paths[0][1].pubkey, node3);
855 assert_eq!(route.paths[0][1].short_channel_id, 4);
856 assert_eq!(route.paths[0][1].fee_msat, 100);
857 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
858 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
859 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
862 // // Disable channels 4 and 12 by flags=2
863 update_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, UnsignedChannelUpdate {
864 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
867 flags: 2, // to disable
868 cltv_expiry_delta: 0,
869 htlc_minimum_msat: 0,
870 htlc_maximum_msat: OptionalField::Absent,
872 fee_proportional_millionths: 0,
873 excess_data: Vec::new()
875 update_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, UnsignedChannelUpdate {
876 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
877 short_channel_id: 12,
879 flags: 2, // to disable
880 cltv_expiry_delta: 0,
881 htlc_minimum_msat: 0,
882 htlc_maximum_msat: OptionalField::Absent,
884 fee_proportional_millionths: 0,
885 excess_data: Vec::new()
888 // If all the channels require some features we don't understand, route should fail
889 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node3, None, &Vec::new(), 100, 42, Arc::clone(&logger)) {
890 assert_eq!(err, "Failed to find a path to the given destination");
893 // If we specify a channel to node8, that overrides our local channel view and that gets used
894 let our_chans = vec![channelmanager::ChannelDetails {
896 short_channel_id: Some(42),
897 remote_network_id: node8.clone(),
898 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
899 channel_value_satoshis: 0,
901 outbound_capacity_msat: 0,
902 inbound_capacity_msat: 0,
905 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node3, Some(&our_chans), &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
906 assert_eq!(route.paths[0].len(), 2);
908 assert_eq!(route.paths[0][0].pubkey, node8);
909 assert_eq!(route.paths[0][0].short_channel_id, 42);
910 assert_eq!(route.paths[0][0].fee_msat, 200);
911 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
912 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
913 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
915 assert_eq!(route.paths[0][1].pubkey, node3);
916 assert_eq!(route.paths[0][1].short_channel_id, 13);
917 assert_eq!(route.paths[0][1].fee_msat, 100);
918 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
919 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
920 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13));
922 // Re-enable channels 4 and 12
923 update_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, UnsignedChannelUpdate {
924 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
927 flags: 0, // to enable
928 cltv_expiry_delta: (4 << 8) | 1,
929 htlc_minimum_msat: 0,
930 htlc_maximum_msat: OptionalField::Absent,
932 fee_proportional_millionths: 1000000,
933 excess_data: Vec::new()
935 update_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, UnsignedChannelUpdate {
936 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
937 short_channel_id: 12,
939 flags: 0, // to enable
940 cltv_expiry_delta: u16::max_value(),
941 htlc_minimum_msat: 0,
942 htlc_maximum_msat: OptionalField::Absent,
943 fee_base_msat: u32::max_value(),
944 fee_proportional_millionths: u32::max_value(),
945 excess_data: Vec::new()
947 // Disable nodes 1, 2, and 8 by requiring unknown feature bits
948 let mut unknown_features = NodeFeatures::known();
949 unknown_features.set_required_unknown_bits();
950 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node1_privkey, unknown_features.clone(), 1);
951 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node2_privkey, unknown_features.clone(), 1);
952 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node8_privkey, unknown_features.clone(), 1);
954 // // If all nodes require some features we don't understand, route should fail
955 // 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)) {
956 // assert_eq!(err, "Failed to find a path to the given destination");
957 // } else { panic!(); }
959 // If we specify a channel to node8, that overrides our local channel view and that gets used
960 let our_chans = vec![channelmanager::ChannelDetails {
962 short_channel_id: Some(42),
963 remote_network_id: node8.clone(),
964 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
965 channel_value_satoshis: 0,
967 outbound_capacity_msat: 0,
968 inbound_capacity_msat: 0,
971 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node3, Some(&our_chans), &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
972 assert_eq!(route.paths[0].len(), 2);
974 assert_eq!(route.paths[0][0].pubkey, node8);
975 assert_eq!(route.paths[0][0].short_channel_id, 42);
976 assert_eq!(route.paths[0][0].fee_msat, 200);
977 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
978 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
979 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
981 assert_eq!(route.paths[0][1].pubkey, node3);
982 assert_eq!(route.paths[0][1].short_channel_id, 13);
983 assert_eq!(route.paths[0][1].fee_msat, 100);
984 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
985 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
986 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13));
988 // Re-enable nodes 1, 2, and 8
989 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node1_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(1)), 2);
990 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node2_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(2)), 2);
991 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node8_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(8)), 2);
993 // Note that we don't test disabling node 3 and failing to route to it, as we (somewhat
994 // naively) assume that the user checked the feature bits on the invoice, which override
995 // the node_announcement.
997 // Route to 1 via 2 and 3 because our channel to 1 is disabled
998 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node1, None, &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
999 assert_eq!(route.paths[0].len(), 3);
1001 assert_eq!(route.paths[0][0].pubkey, node2);
1002 assert_eq!(route.paths[0][0].short_channel_id, 2);
1003 assert_eq!(route.paths[0][0].fee_msat, 200);
1004 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1005 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1006 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1008 assert_eq!(route.paths[0][1].pubkey, node3);
1009 assert_eq!(route.paths[0][1].short_channel_id, 4);
1010 assert_eq!(route.paths[0][1].fee_msat, 100);
1011 assert_eq!(route.paths[0][1].cltv_expiry_delta, (3 << 8) | 2);
1012 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1013 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1015 assert_eq!(route.paths[0][2].pubkey, node1);
1016 assert_eq!(route.paths[0][2].short_channel_id, 3);
1017 assert_eq!(route.paths[0][2].fee_msat, 100);
1018 assert_eq!(route.paths[0][2].cltv_expiry_delta, 42);
1019 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(1));
1020 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(3));
1022 // If we specify a channel to node8, that overrides our local channel view and that gets used
1023 let our_chans = vec![channelmanager::ChannelDetails {
1024 channel_id: [0; 32],
1025 short_channel_id: Some(42),
1026 remote_network_id: node8.clone(),
1027 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1028 channel_value_satoshis: 0,
1030 outbound_capacity_msat: 0,
1031 inbound_capacity_msat: 0,
1034 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node3, Some(&our_chans), &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
1035 assert_eq!(route.paths[0].len(), 2);
1037 assert_eq!(route.paths[0][0].pubkey, node8);
1038 assert_eq!(route.paths[0][0].short_channel_id, 42);
1039 assert_eq!(route.paths[0][0].fee_msat, 200);
1040 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
1041 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
1042 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
1044 assert_eq!(route.paths[0][1].pubkey, node3);
1045 assert_eq!(route.paths[0][1].short_channel_id, 13);
1046 assert_eq!(route.paths[0][1].fee_msat, 100);
1047 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1048 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1049 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13));
1051 let zero_fees = RoutingFees {
1053 proportional_millionths: 0,
1055 let mut last_hops = vec!(RouteHint {
1056 src_node_id: node4.clone(),
1057 short_channel_id: 8,
1059 cltv_expiry_delta: (8 << 8) | 1,
1060 htlc_minimum_msat: 0,
1062 src_node_id: node5.clone(),
1063 short_channel_id: 9,
1066 proportional_millionths: 0,
1068 cltv_expiry_delta: (9 << 8) | 1,
1069 htlc_minimum_msat: 0,
1071 src_node_id: node6.clone(),
1072 short_channel_id: 10,
1074 cltv_expiry_delta: (10 << 8) | 1,
1075 htlc_minimum_msat: 0,
1078 // Simple test across 2, 3, 5, and 4 via a last_hop channel
1079 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node7, None, &last_hops, 100, 42, Arc::clone(&logger)).unwrap();
1080 assert_eq!(route.paths[0].len(), 5);
1082 assert_eq!(route.paths[0][0].pubkey, node2);
1083 assert_eq!(route.paths[0][0].short_channel_id, 2);
1084 assert_eq!(route.paths[0][0].fee_msat, 100);
1085 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1086 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1087 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1089 assert_eq!(route.paths[0][1].pubkey, node3);
1090 assert_eq!(route.paths[0][1].short_channel_id, 4);
1091 assert_eq!(route.paths[0][1].fee_msat, 0);
1092 assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1);
1093 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1094 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1096 assert_eq!(route.paths[0][2].pubkey, node5);
1097 assert_eq!(route.paths[0][2].short_channel_id, 6);
1098 assert_eq!(route.paths[0][2].fee_msat, 0);
1099 assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1);
1100 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(5));
1101 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(6));
1103 assert_eq!(route.paths[0][3].pubkey, node4);
1104 assert_eq!(route.paths[0][3].short_channel_id, 11);
1105 assert_eq!(route.paths[0][3].fee_msat, 0);
1106 assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1);
1107 // If we have a peer in the node map, we'll use their features here since we don't have
1108 // a way of figuring out their features from the invoice:
1109 assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags!(4));
1110 assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags!(11));
1112 assert_eq!(route.paths[0][4].pubkey, node7);
1113 assert_eq!(route.paths[0][4].short_channel_id, 8);
1114 assert_eq!(route.paths[0][4].fee_msat, 100);
1115 assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
1116 assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1117 assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1119 // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1120 let our_chans = vec![channelmanager::ChannelDetails {
1121 channel_id: [0; 32],
1122 short_channel_id: Some(42),
1123 remote_network_id: node4.clone(),
1124 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1125 channel_value_satoshis: 0,
1127 outbound_capacity_msat: 0,
1128 inbound_capacity_msat: 0,
1131 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node7, Some(&our_chans), &last_hops, 100, 42, Arc::clone(&logger)).unwrap();
1132 assert_eq!(route.paths[0].len(), 2);
1134 assert_eq!(route.paths[0][0].pubkey, node4);
1135 assert_eq!(route.paths[0][0].short_channel_id, 42);
1136 assert_eq!(route.paths[0][0].fee_msat, 0);
1137 assert_eq!(route.paths[0][0].cltv_expiry_delta, (8 << 8) | 1);
1138 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
1139 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
1141 assert_eq!(route.paths[0][1].pubkey, node7);
1142 assert_eq!(route.paths[0][1].short_channel_id, 8);
1143 assert_eq!(route.paths[0][1].fee_msat, 100);
1144 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1145 assert_eq!(route.paths[0][1].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1146 assert_eq!(route.paths[0][1].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1148 last_hops[0].fees.base_msat = 1000;
1150 // Revert to via 6 as the fee on 8 goes up
1151 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node7, None, &last_hops, 100, 42, Arc::clone(&logger)).unwrap();
1152 assert_eq!(route.paths[0].len(), 4);
1154 assert_eq!(route.paths[0][0].pubkey, node2);
1155 assert_eq!(route.paths[0][0].short_channel_id, 2);
1156 assert_eq!(route.paths[0][0].fee_msat, 200); // fee increased as its % of value transferred across node
1157 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1158 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1159 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1161 assert_eq!(route.paths[0][1].pubkey, node3);
1162 assert_eq!(route.paths[0][1].short_channel_id, 4);
1163 assert_eq!(route.paths[0][1].fee_msat, 100);
1164 assert_eq!(route.paths[0][1].cltv_expiry_delta, (7 << 8) | 1);
1165 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1166 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1168 assert_eq!(route.paths[0][2].pubkey, node6);
1169 assert_eq!(route.paths[0][2].short_channel_id, 7);
1170 assert_eq!(route.paths[0][2].fee_msat, 0);
1171 assert_eq!(route.paths[0][2].cltv_expiry_delta, (10 << 8) | 1);
1172 // If we have a peer in the node map, we'll use their features here since we don't have
1173 // a way of figuring out their features from the invoice:
1174 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(6));
1175 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(7));
1177 assert_eq!(route.paths[0][3].pubkey, node7);
1178 assert_eq!(route.paths[0][3].short_channel_id, 10);
1179 assert_eq!(route.paths[0][3].fee_msat, 100);
1180 assert_eq!(route.paths[0][3].cltv_expiry_delta, 42);
1181 assert_eq!(route.paths[0][3].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1182 assert_eq!(route.paths[0][3].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1184 // ...but still use 8 for larger payments as 6 has a variable feerate
1185 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node7, None, &last_hops, 2000, 42, Arc::clone(&logger)).unwrap();
1186 assert_eq!(route.paths[0].len(), 5);
1188 assert_eq!(route.paths[0][0].pubkey, node2);
1189 assert_eq!(route.paths[0][0].short_channel_id, 2);
1190 assert_eq!(route.paths[0][0].fee_msat, 3000);
1191 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1192 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1193 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1195 assert_eq!(route.paths[0][1].pubkey, node3);
1196 assert_eq!(route.paths[0][1].short_channel_id, 4);
1197 assert_eq!(route.paths[0][1].fee_msat, 0);
1198 assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1);
1199 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1200 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1202 assert_eq!(route.paths[0][2].pubkey, node5);
1203 assert_eq!(route.paths[0][2].short_channel_id, 6);
1204 assert_eq!(route.paths[0][2].fee_msat, 0);
1205 assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1);
1206 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(5));
1207 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(6));
1209 assert_eq!(route.paths[0][3].pubkey, node4);
1210 assert_eq!(route.paths[0][3].short_channel_id, 11);
1211 assert_eq!(route.paths[0][3].fee_msat, 1000);
1212 assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1);
1213 // If we have a peer in the node map, we'll use their features here since we don't have
1214 // a way of figuring out their features from the invoice:
1215 assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags!(4));
1216 assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags!(11));
1218 assert_eq!(route.paths[0][4].pubkey, node7);
1219 assert_eq!(route.paths[0][4].short_channel_id, 8);
1220 assert_eq!(route.paths[0][4].fee_msat, 2000);
1221 assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
1222 assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1223 assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly