1 //! The top-level routing/network map tracking logic lives here.
3 //! You probably want to create a NetGraphMsgHandler and use that as your RoutingMessageHandler and then
4 //! interrogate it to get routes for your own payments.
6 use bitcoin::secp256k1::key::PublicKey;
8 use ln::channelmanager;
9 use ln::features::{ChannelFeatures, NodeFeatures};
10 use ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
11 use routing::network_graph::{NetworkGraph, RoutingFees};
12 use util::ser::{Writeable, Readable};
13 use util::logger::Logger;
16 use std::collections::{HashMap,BinaryHeap};
20 #[derive(Clone, PartialEq)]
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.
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,
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)?;
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 {
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)?,
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)]
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>>,
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() {
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)?);
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,
120 #[derive(Eq, PartialEq)]
121 struct RouteGraphNode {
123 lowest_fee_to_peer_through_node: u64,
124 lowest_fee_to_node: u64,
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()))
134 impl cmp::PartialOrd for RouteGraphNode {
135 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
136 Some(self.cmp(other))
140 struct DummyDirectionalChannelInfo {
141 cltv_expiry_delta: u32,
142 htlc_minimum_msat: u64,
147 /// Gets a route from us to the given target node.
149 /// Extra routing hops between known nodes and the target will be used if they are included in
152 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
153 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
154 /// view of our local channels (from net_graph_msg_handler) will be ignored, and only those in first_hops
157 /// Panics if first_hops contains channels without short_channel_ids
158 /// (ChannelManager::list_usable_channels will never include such channels).
160 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
161 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
162 /// *is* checked as they may change based on the receiving node.
163 pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, target: &PublicKey, first_hops: Option<&[channelmanager::ChannelDetails]>,
164 last_hops: &[RouteHint], final_value_msat: u64, final_cltv: u32, logger: L) -> Result<Route, LightningError> where L::Target: Logger {
165 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
166 // uptime/success in using a node in the past.
167 if *target == *our_node_id {
168 return Err(LightningError{err: "Cannot generate a route to ourselves".to_owned(), action: ErrorAction::IgnoreError});
171 if final_value_msat > MAX_VALUE_MSAT {
172 return Err(LightningError{err: "Cannot generate a route of more value than all existing satoshis".to_owned(), action: ErrorAction::IgnoreError});
175 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
176 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
177 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
178 // to use as the A* heuristic beyond just the cost to get one node further than the current
181 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
182 cltv_expiry_delta: 0,
183 htlc_minimum_msat: 0,
186 proportional_millionths: 0,
190 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
191 let mut dist = HashMap::with_capacity(network.get_nodes().len());
193 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
194 if let Some(hops) = first_hops {
196 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
197 if chan.remote_network_id == *target {
199 paths: vec![vec![RouteHop {
200 pubkey: chan.remote_network_id,
201 node_features: chan.counterparty_features.to_context(),
203 channel_features: chan.counterparty_features.to_context(),
204 fee_msat: final_value_msat,
205 cltv_expiry_delta: final_cltv,
209 first_hop_targets.insert(chan.remote_network_id, (short_channel_id, chan.counterparty_features.clone()));
211 if first_hop_targets.is_empty() {
212 return Err(LightningError{err: "Cannot route when there are no outbound routes away from us".to_owned(), action: ErrorAction::IgnoreError});
216 macro_rules! add_entry {
217 // Adds entry which goes from $src_node_id to $dest_node_id
218 // over the channel with id $chan_id with fees described in
219 // $directional_info.
220 ( $chan_id: expr, $src_node_id: expr, $dest_node_id: expr, $directional_info: expr, $chan_features: expr, $starting_fee_msat: expr ) => {
221 //TODO: Explore simply adding fee to hit htlc_minimum_msat
222 if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
223 let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fees.proportional_millionths as u64);
224 if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
225 ($directional_info.fees.base_msat as u64).checked_add(part / 1000000) })
227 let mut total_fee = $starting_fee_msat as u64;
228 let hm_entry = dist.entry(&$src_node_id);
229 let old_entry = hm_entry.or_insert_with(|| {
230 let node = network.get_nodes().get(&$src_node_id).unwrap();
231 let mut fee_base_msat = u32::max_value();
232 let mut fee_proportional_millionths = u32::max_value();
233 if let Some(fees) = node.lowest_inbound_channel_fees {
234 fee_base_msat = fees.base_msat;
235 fee_proportional_millionths = fees.proportional_millionths;
239 fee_proportional_millionths,
241 pubkey: $dest_node_id.clone(),
242 node_features: NodeFeatures::empty(),
244 channel_features: $chan_features.clone(),
246 cltv_expiry_delta: 0,
250 if $src_node_id != *our_node_id {
251 // Ignore new_fee for channel-from-us as we assume all channels-from-us
252 // will have the same effective-fee
253 total_fee += new_fee;
254 if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
255 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
257 // max_value means we'll always fail the old_entry.0 > total_fee check
258 total_fee = u64::max_value();
261 let new_graph_node = RouteGraphNode {
262 pubkey: $src_node_id,
263 lowest_fee_to_peer_through_node: total_fee,
264 lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
266 if old_entry.0 > total_fee {
267 targets.push(new_graph_node);
268 old_entry.0 = total_fee;
269 old_entry.3 = RouteHop {
270 pubkey: $dest_node_id.clone(),
271 node_features: NodeFeatures::empty(),
272 short_channel_id: $chan_id.clone(),
273 channel_features: $chan_features.clone(),
274 fee_msat: new_fee, // This field is ignored on the last-hop anyway
275 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
283 macro_rules! add_entries_to_cheapest_to_target_node {
284 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
285 if first_hops.is_some() {
286 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&$node_id) {
287 add_entry!(first_hop, *our_node_id, $node_id, dummy_directional_info, features.to_context(), $fee_to_target_msat);
292 if let Some(node_info) = $node.announcement_info.as_ref() {
293 features = node_info.features.clone();
295 features = NodeFeatures::empty();
298 if !features.requires_unknown_bits() {
299 for chan_id in $node.channels.iter() {
300 let chan = network.get_channels().get(chan_id).unwrap();
301 if !chan.features.requires_unknown_bits() {
302 if chan.node_one == *$node_id {
303 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
304 if first_hops.is_none() || chan.node_two != *our_node_id {
305 if let Some(two_to_one) = chan.two_to_one.as_ref() {
306 if two_to_one.enabled {
307 add_entry!(chan_id, chan.node_two, chan.node_one, two_to_one, chan.features, $fee_to_target_msat);
312 if first_hops.is_none() || chan.node_one != *our_node_id {
313 if let Some(one_to_two) = chan.one_to_two.as_ref() {
314 if one_to_two.enabled {
315 add_entry!(chan_id, chan.node_one, chan.node_two, one_to_two, chan.features, $fee_to_target_msat);
327 match network.get_nodes().get(target) {
330 add_entries_to_cheapest_to_target_node!(node, target, 0);
334 for hop in last_hops.iter() {
335 if first_hops.is_none() || hop.src_node_id != *our_node_id { // first_hop overrules last_hops
336 if network.get_nodes().get(&hop.src_node_id).is_some() {
337 if first_hops.is_some() {
338 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&hop.src_node_id) {
339 // Currently there are no channel-context features defined, so we are a
340 // bit lazy here. In the future, we should pull them out via our
341 // ChannelManager, but there's no reason to waste the space until we
343 add_entry!(first_hop, *our_node_id , hop.src_node_id, dummy_directional_info, features.to_context(), 0);
346 // BOLT 11 doesn't allow inclusion of features for the last hop hints, which
347 // really sucks, cause we're gonna need that eventually.
348 add_entry!(hop.short_channel_id, hop.src_node_id, target, hop, ChannelFeatures::empty(), 0);
353 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
354 if pubkey == *our_node_id {
355 let mut res = vec!(dist.remove(&our_node_id).unwrap().3);
357 if let Some(&(_, ref features)) = first_hop_targets.get(&res.last().unwrap().pubkey) {
358 res.last_mut().unwrap().node_features = features.to_context();
359 } else if let Some(node) = network.get_nodes().get(&res.last().unwrap().pubkey) {
360 if let Some(node_info) = node.announcement_info.as_ref() {
361 res.last_mut().unwrap().node_features = node_info.features.clone();
363 res.last_mut().unwrap().node_features = NodeFeatures::empty();
366 // We should be able to fill in features for everything except the last
367 // hop, if the last hop was provided via a BOLT 11 invoice (though we
368 // should be able to extend it further as BOLT 11 does have feature
369 // flags for the last hop node itself).
370 assert!(res.last().unwrap().pubkey == *target);
372 if res.last().unwrap().pubkey == *target {
376 let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
378 None => return Err(LightningError{err: "Failed to find a non-fee-overflowing path to the given destination".to_owned(), action: ErrorAction::IgnoreError}),
380 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
381 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
384 res.last_mut().unwrap().fee_msat = final_value_msat;
385 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
386 let route = Route { paths: vec![res] };
387 log_trace!(logger, "Got route: {}", log_route!(route));
391 match network.get_nodes().get(&pubkey) {
394 add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
399 Err(LightningError{err: "Failed to find a path to the given destination".to_owned(), action: ErrorAction::IgnoreError})
404 use chain::chaininterface;
405 use routing::router::{get_route, RouteHint, RoutingFees};
406 use routing::network_graph::NetGraphMsgHandler;
407 use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
408 use ln::msgs::{ErrorAction, LightningError, UnsignedChannelAnnouncement, ChannelAnnouncement, RoutingMessageHandler,
409 NodeAnnouncement, UnsignedNodeAnnouncement, ChannelUpdate, UnsignedChannelUpdate};
410 use ln::channelmanager;
411 use util::test_utils;
412 use util::ser::Writeable;
414 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
415 use bitcoin::hashes::Hash;
416 use bitcoin::network::constants::Network;
417 use bitcoin::blockdata::constants::genesis_block;
418 use bitcoin::util::hash::BitcoinHash;
422 use bitcoin::secp256k1::key::{PublicKey,SecretKey};
423 use bitcoin::secp256k1::{Secp256k1, All};
427 // Using the same keys for LN and BTC ids
428 fn add_channel(net_graph_msg_handler: &NetGraphMsgHandler<Arc<chaininterface::ChainWatchInterfaceUtil>, Arc<test_utils::TestLogger>>, secp_ctx: &Secp256k1<All>, node_1_privkey: &SecretKey,
429 node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64) {
430 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
431 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
433 let unsigned_announcement = UnsignedChannelAnnouncement {
435 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
439 bitcoin_key_1: node_id_1,
440 bitcoin_key_2: node_id_2,
441 excess_data: Vec::new(),
444 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
445 let valid_announcement = ChannelAnnouncement {
446 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
447 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
448 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
449 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
450 contents: unsigned_announcement.clone(),
452 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
453 Ok(res) => assert!(res),
458 fn update_channel(net_graph_msg_handler: &NetGraphMsgHandler<Arc<chaininterface::ChainWatchInterfaceUtil>, Arc<test_utils::TestLogger>>, secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, update: UnsignedChannelUpdate) {
459 let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]);
460 let valid_channel_update = ChannelUpdate {
461 signature: secp_ctx.sign(&msghash, node_privkey),
462 contents: update.clone()
465 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
466 Ok(res) => assert!(res),
467 // Err(_) => panic!()
468 Err(e) => println!("{:?}", e.err)
473 fn add_or_update_node(net_graph_msg_handler: &NetGraphMsgHandler<Arc<chaininterface::ChainWatchInterfaceUtil>, Arc<test_utils::TestLogger>>, secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey,
474 features: NodeFeatures, timestamp: u32) {
475 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
476 let unsigned_announcement = UnsignedNodeAnnouncement {
482 addresses: Vec::new(),
483 excess_address_data: Vec::new(),
484 excess_data: Vec::new(),
486 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
487 let valid_announcement = NodeAnnouncement {
488 signature: secp_ctx.sign(&msghash, node_privkey),
489 contents: unsigned_announcement.clone()
492 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
500 let secp_ctx = Secp256k1::new();
501 let our_privkey = &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
502 let our_id = PublicKey::from_secret_key(&secp_ctx, our_privkey);
503 let logger = Arc::new(test_utils::TestLogger::new());
504 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet));
505 let net_graph_msg_handler = NetGraphMsgHandler::new(chain_monitor, Arc::clone(&logger));
506 // Build network from our_id to node8:
508 // -1(1)2- node1 -1(3)2-
510 // our_id -1(12)2- node8 -1(13)2--- node3
512 // -1(2)2- node2 -1(4)2-
515 // chan1 1-to-2: disabled
516 // chan1 2-to-1: enabled, 0 fee
518 // chan2 1-to-2: enabled, ignored fee
519 // chan2 2-to-1: enabled, 0 fee
521 // chan3 1-to-2: enabled, 0 fee
522 // chan3 2-to-1: enabled, 100 msat fee
524 // chan4 1-to-2: enabled, 100% fee
525 // chan4 2-to-1: enabled, 0 fee
527 // chan12 1-to-2: enabled, ignored fee
528 // chan12 2-to-1: enabled, 0 fee
530 // chan13 1-to-2: enabled, 200% fee
531 // chan13 2-to-1: enabled, 0 fee
534 // -1(5)2- node4 -1(8)2--
538 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
540 // -1(7)2- node6 -1(10)2-
542 // chan5 1-to-2: enabled, 100 msat fee
543 // chan5 2-to-1: enabled, 0 fee
545 // chan6 1-to-2: enabled, 0 fee
546 // chan6 2-to-1: enabled, 0 fee
548 // chan7 1-to-2: enabled, 100% fee
549 // chan7 2-to-1: enabled, 0 fee
551 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
552 // chan8 2-to-1: enabled, 0 fee
554 // chan9 1-to-2: enabled, 1001 msat fee
555 // chan9 2-to-1: enabled, 0 fee
557 // chan10 1-to-2: enabled, 0 fee
558 // chan10 2-to-1: enabled, 0 fee
560 // chan11 1-to-2: enabled, 0 fee
561 // chan11 2-to-1: enabled, 0 fee
563 let node1_privkey = &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap();
564 let node2_privkey = &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap();
565 let node3_privkey = &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap();
566 let node4_privkey = &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap();
567 let node5_privkey = &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap();
568 let node6_privkey = &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap();
569 let node7_privkey = &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap();
570 let node8_privkey = &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap();
573 let node1 = PublicKey::from_secret_key(&secp_ctx, node1_privkey);
574 let node2 = PublicKey::from_secret_key(&secp_ctx, node2_privkey);
575 let node3 = PublicKey::from_secret_key(&secp_ctx, node3_privkey);
576 let node4 = PublicKey::from_secret_key(&secp_ctx, node4_privkey);
577 let node5 = PublicKey::from_secret_key(&secp_ctx, node5_privkey);
578 let node6 = PublicKey::from_secret_key(&secp_ctx, node6_privkey);
579 let node7 = PublicKey::from_secret_key(&secp_ctx, node7_privkey);
580 let node8 = PublicKey::from_secret_key(&secp_ctx, node8_privkey);
582 macro_rules! id_to_feature_flags {
583 // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can
584 // test for it later.
586 let idx = ($id - 1) * 2 + 1;
588 vec![1 << (idx - 8*3), 0, 0, 0]
589 } else if idx > 8*2 {
590 vec![1 << (idx - 8*2), 0, 0]
591 } else if idx > 8*1 {
592 vec![1 << (idx - 8*1), 0]
599 add_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, node1_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(1)), 1);
600 update_channel(&net_graph_msg_handler, &secp_ctx, node1_privkey, UnsignedChannelUpdate {
601 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
605 cltv_expiry_delta: 0,
606 htlc_minimum_msat: 0,
608 fee_proportional_millionths: 0,
609 excess_data: Vec::new()
611 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node1_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(1)), 0);
613 add_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, node2_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(2)), 2);
614 update_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, UnsignedChannelUpdate {
615 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
619 cltv_expiry_delta: u16::max_value(),
620 htlc_minimum_msat: 0,
621 fee_base_msat: u32::max_value(),
622 fee_proportional_millionths: u32::max_value(),
623 excess_data: Vec::new()
625 update_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, UnsignedChannelUpdate {
626 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
630 cltv_expiry_delta: 0,
631 htlc_minimum_msat: 0,
633 fee_proportional_millionths: 0,
634 excess_data: Vec::new()
637 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node2_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(2)), 0);
639 add_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, node8_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(12)), 12);
640 update_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, UnsignedChannelUpdate {
641 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
642 short_channel_id: 12,
645 cltv_expiry_delta: u16::max_value(),
646 htlc_minimum_msat: 0,
647 fee_base_msat: u32::max_value(),
648 fee_proportional_millionths: u32::max_value(),
649 excess_data: Vec::new()
651 update_channel(&net_graph_msg_handler, &secp_ctx, node8_privkey, UnsignedChannelUpdate {
652 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
653 short_channel_id: 12,
656 cltv_expiry_delta: 0,
657 htlc_minimum_msat: 0,
659 fee_proportional_millionths: 0,
660 excess_data: Vec::new()
664 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node8_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(8)), 0);
666 add_channel(&net_graph_msg_handler, &secp_ctx, node1_privkey, node3_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(3)), 3);
667 update_channel(&net_graph_msg_handler, &secp_ctx, node1_privkey, UnsignedChannelUpdate {
668 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
672 cltv_expiry_delta: (3 << 8) | 1,
673 htlc_minimum_msat: 0,
675 fee_proportional_millionths: 0,
676 excess_data: Vec::new()
678 update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate {
679 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
683 cltv_expiry_delta: (3 << 8) | 2,
684 htlc_minimum_msat: 0,
686 fee_proportional_millionths: 0,
687 excess_data: Vec::new()
691 add_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, node3_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(4)), 4);
692 update_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, UnsignedChannelUpdate {
693 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
697 cltv_expiry_delta: (4 << 8) | 1,
698 htlc_minimum_msat: 0,
700 fee_proportional_millionths: 1000000,
701 excess_data: Vec::new()
703 update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate {
704 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
708 cltv_expiry_delta: (4 << 8) | 2,
709 htlc_minimum_msat: 0,
711 fee_proportional_millionths: 0,
712 excess_data: Vec::new()
715 add_channel(&net_graph_msg_handler, &secp_ctx, node8_privkey, node3_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(13)), 13);
716 update_channel(&net_graph_msg_handler, &secp_ctx, node8_privkey, UnsignedChannelUpdate {
717 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
718 short_channel_id: 13,
721 cltv_expiry_delta: (13 << 8) | 1,
722 htlc_minimum_msat: 0,
724 fee_proportional_millionths: 2000000,
725 excess_data: Vec::new()
727 update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate {
728 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
729 short_channel_id: 13,
732 cltv_expiry_delta: (13 << 8) | 2,
733 htlc_minimum_msat: 0,
735 fee_proportional_millionths: 0,
736 excess_data: Vec::new()
738 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node3_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(3)), 0);
739 add_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, node5_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(6)), 6);
741 update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate {
742 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
746 cltv_expiry_delta: (6 << 8) | 1,
747 htlc_minimum_msat: 0,
749 fee_proportional_millionths: 0,
750 excess_data: Vec::new()
752 update_channel(&net_graph_msg_handler, &secp_ctx, node5_privkey, UnsignedChannelUpdate {
753 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
757 cltv_expiry_delta: (6 << 8) | 2,
758 htlc_minimum_msat: 0,
760 fee_proportional_millionths: 0,
761 excess_data: Vec::new()
764 add_channel(&net_graph_msg_handler, &secp_ctx, node5_privkey, node4_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(11)), 11);
765 update_channel(&net_graph_msg_handler, &secp_ctx, node5_privkey, UnsignedChannelUpdate {
766 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
767 short_channel_id: 11,
770 cltv_expiry_delta: (11 << 8) | 1,
771 htlc_minimum_msat: 0,
773 fee_proportional_millionths: 0,
774 excess_data: Vec::new()
776 update_channel(&net_graph_msg_handler, &secp_ctx, node4_privkey, UnsignedChannelUpdate {
777 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
778 short_channel_id: 11,
781 cltv_expiry_delta: (11 << 8) | 2,
782 htlc_minimum_msat: 0,
784 fee_proportional_millionths: 0,
785 excess_data: Vec::new()
787 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node5_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(5)), 0);
788 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node4_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(4)), 0);
790 add_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, node6_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(7)), 7);
792 update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate {
793 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
797 cltv_expiry_delta: (7 << 8) | 1,
798 htlc_minimum_msat: 0,
800 fee_proportional_millionths: 1000000,
801 excess_data: Vec::new()
803 update_channel(&net_graph_msg_handler, &secp_ctx, node6_privkey, UnsignedChannelUpdate {
804 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
808 cltv_expiry_delta: (7 << 8) | 2,
809 htlc_minimum_msat: 0,
811 fee_proportional_millionths: 0,
812 excess_data: Vec::new()
815 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node6_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(6)), 0);
817 // Simple route to 3 via 2
818 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node3, None, &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
819 assert_eq!(route.paths[0].len(), 2);
821 assert_eq!(route.paths[0][0].pubkey, node2);
822 assert_eq!(route.paths[0][0].short_channel_id, 2);
823 assert_eq!(route.paths[0][0].fee_msat, 100);
824 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
825 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
826 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
828 assert_eq!(route.paths[0][1].pubkey, node3);
829 assert_eq!(route.paths[0][1].short_channel_id, 4);
830 assert_eq!(route.paths[0][1].fee_msat, 100);
831 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
832 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
833 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
836 // // Disable channels 4 and 12 by flags=2
837 update_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, UnsignedChannelUpdate {
838 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
841 flags: 2, // to disable
842 cltv_expiry_delta: 0,
843 htlc_minimum_msat: 0,
845 fee_proportional_millionths: 0,
846 excess_data: Vec::new()
848 update_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, UnsignedChannelUpdate {
849 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
850 short_channel_id: 12,
852 flags: 2, // to disable
853 cltv_expiry_delta: 0,
854 htlc_minimum_msat: 0,
856 fee_proportional_millionths: 0,
857 excess_data: Vec::new()
860 // If all the channels require some features we don't understand, route should fail
861 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)) {
862 assert_eq!(err, "Failed to find a path to the given destination");
865 // If we specify a channel to node8, that overrides our local channel view and that gets used
866 let our_chans = vec![channelmanager::ChannelDetails {
868 short_channel_id: Some(42),
869 remote_network_id: node8.clone(),
870 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
871 channel_value_satoshis: 0,
873 outbound_capacity_msat: 0,
874 inbound_capacity_msat: 0,
877 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();
878 assert_eq!(route.paths[0].len(), 2);
880 assert_eq!(route.paths[0][0].pubkey, node8);
881 assert_eq!(route.paths[0][0].short_channel_id, 42);
882 assert_eq!(route.paths[0][0].fee_msat, 200);
883 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
884 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
885 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
887 assert_eq!(route.paths[0][1].pubkey, node3);
888 assert_eq!(route.paths[0][1].short_channel_id, 13);
889 assert_eq!(route.paths[0][1].fee_msat, 100);
890 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
891 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
892 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13));
894 // Re-enable channels 4 and 12
895 update_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, UnsignedChannelUpdate {
896 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
899 flags: 0, // to enable
900 cltv_expiry_delta: (4 << 8) | 1,
901 htlc_minimum_msat: 0,
903 fee_proportional_millionths: 1000000,
904 excess_data: Vec::new()
906 update_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, UnsignedChannelUpdate {
907 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
908 short_channel_id: 12,
910 flags: 0, // to enable
911 cltv_expiry_delta: u16::max_value(),
912 htlc_minimum_msat: 0,
913 fee_base_msat: u32::max_value(),
914 fee_proportional_millionths: u32::max_value(),
915 excess_data: Vec::new()
917 // Disable nodes 1, 2, and 8 by requiring unknown feature bits
918 let mut unknown_features = NodeFeatures::known();
919 unknown_features.set_required_unknown_bits();
920 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node1_privkey, unknown_features.clone(), 1);
921 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node2_privkey, unknown_features.clone(), 1);
922 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node8_privkey, unknown_features.clone(), 1);
924 // // If all nodes require some features we don't understand, route should fail
925 // 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)) {
926 // assert_eq!(err, "Failed to find a path to the given destination");
927 // } else { panic!(); }
929 // If we specify a channel to node8, that overrides our local channel view and that gets used
930 let our_chans = vec![channelmanager::ChannelDetails {
932 short_channel_id: Some(42),
933 remote_network_id: node8.clone(),
934 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
935 channel_value_satoshis: 0,
937 outbound_capacity_msat: 0,
938 inbound_capacity_msat: 0,
941 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();
942 assert_eq!(route.paths[0].len(), 2);
944 assert_eq!(route.paths[0][0].pubkey, node8);
945 assert_eq!(route.paths[0][0].short_channel_id, 42);
946 assert_eq!(route.paths[0][0].fee_msat, 200);
947 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
948 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
949 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
951 assert_eq!(route.paths[0][1].pubkey, node3);
952 assert_eq!(route.paths[0][1].short_channel_id, 13);
953 assert_eq!(route.paths[0][1].fee_msat, 100);
954 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
955 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
956 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13));
958 // Re-enable nodes 1, 2, and 8
959 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node1_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(1)), 2);
960 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node2_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(2)), 2);
961 add_or_update_node(&net_graph_msg_handler, &secp_ctx, node8_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(8)), 2);
963 // Note that we don't test disabling node 3 and failing to route to it, as we (somewhat
964 // naively) assume that the user checked the feature bits on the invoice, which override
965 // the node_announcement.
967 // Route to 1 via 2 and 3 because our channel to 1 is disabled
968 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node1, None, &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
969 assert_eq!(route.paths[0].len(), 3);
971 assert_eq!(route.paths[0][0].pubkey, node2);
972 assert_eq!(route.paths[0][0].short_channel_id, 2);
973 assert_eq!(route.paths[0][0].fee_msat, 200);
974 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
975 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
976 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
978 assert_eq!(route.paths[0][1].pubkey, node3);
979 assert_eq!(route.paths[0][1].short_channel_id, 4);
980 assert_eq!(route.paths[0][1].fee_msat, 100);
981 assert_eq!(route.paths[0][1].cltv_expiry_delta, (3 << 8) | 2);
982 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
983 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
985 assert_eq!(route.paths[0][2].pubkey, node1);
986 assert_eq!(route.paths[0][2].short_channel_id, 3);
987 assert_eq!(route.paths[0][2].fee_msat, 100);
988 assert_eq!(route.paths[0][2].cltv_expiry_delta, 42);
989 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(1));
990 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(3));
992 // If we specify a channel to node8, that overrides our local channel view and that gets used
993 let our_chans = vec![channelmanager::ChannelDetails {
995 short_channel_id: Some(42),
996 remote_network_id: node8.clone(),
997 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
998 channel_value_satoshis: 0,
1000 outbound_capacity_msat: 0,
1001 inbound_capacity_msat: 0,
1004 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();
1005 assert_eq!(route.paths[0].len(), 2);
1007 assert_eq!(route.paths[0][0].pubkey, node8);
1008 assert_eq!(route.paths[0][0].short_channel_id, 42);
1009 assert_eq!(route.paths[0][0].fee_msat, 200);
1010 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
1011 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
1012 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
1014 assert_eq!(route.paths[0][1].pubkey, node3);
1015 assert_eq!(route.paths[0][1].short_channel_id, 13);
1016 assert_eq!(route.paths[0][1].fee_msat, 100);
1017 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1018 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1019 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13));
1021 let zero_fees = RoutingFees {
1023 proportional_millionths: 0,
1025 let mut last_hops = vec!(RouteHint {
1026 src_node_id: node4.clone(),
1027 short_channel_id: 8,
1029 cltv_expiry_delta: (8 << 8) | 1,
1030 htlc_minimum_msat: 0,
1032 src_node_id: node5.clone(),
1033 short_channel_id: 9,
1036 proportional_millionths: 0,
1038 cltv_expiry_delta: (9 << 8) | 1,
1039 htlc_minimum_msat: 0,
1041 src_node_id: node6.clone(),
1042 short_channel_id: 10,
1044 cltv_expiry_delta: (10 << 8) | 1,
1045 htlc_minimum_msat: 0,
1048 // Simple test across 2, 3, 5, and 4 via a last_hop channel
1049 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node7, None, &last_hops, 100, 42, Arc::clone(&logger)).unwrap();
1050 assert_eq!(route.paths[0].len(), 5);
1052 assert_eq!(route.paths[0][0].pubkey, node2);
1053 assert_eq!(route.paths[0][0].short_channel_id, 2);
1054 assert_eq!(route.paths[0][0].fee_msat, 100);
1055 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1056 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1057 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1059 assert_eq!(route.paths[0][1].pubkey, node3);
1060 assert_eq!(route.paths[0][1].short_channel_id, 4);
1061 assert_eq!(route.paths[0][1].fee_msat, 0);
1062 assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1);
1063 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1064 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1066 assert_eq!(route.paths[0][2].pubkey, node5);
1067 assert_eq!(route.paths[0][2].short_channel_id, 6);
1068 assert_eq!(route.paths[0][2].fee_msat, 0);
1069 assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1);
1070 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(5));
1071 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(6));
1073 assert_eq!(route.paths[0][3].pubkey, node4);
1074 assert_eq!(route.paths[0][3].short_channel_id, 11);
1075 assert_eq!(route.paths[0][3].fee_msat, 0);
1076 assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1);
1077 // If we have a peer in the node map, we'll use their features here since we don't have
1078 // a way of figuring out their features from the invoice:
1079 assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags!(4));
1080 assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags!(11));
1082 assert_eq!(route.paths[0][4].pubkey, node7);
1083 assert_eq!(route.paths[0][4].short_channel_id, 8);
1084 assert_eq!(route.paths[0][4].fee_msat, 100);
1085 assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
1086 assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1087 assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1089 // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1090 let our_chans = vec![channelmanager::ChannelDetails {
1091 channel_id: [0; 32],
1092 short_channel_id: Some(42),
1093 remote_network_id: node4.clone(),
1094 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1095 channel_value_satoshis: 0,
1097 outbound_capacity_msat: 0,
1098 inbound_capacity_msat: 0,
1101 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();
1102 assert_eq!(route.paths[0].len(), 2);
1104 assert_eq!(route.paths[0][0].pubkey, node4);
1105 assert_eq!(route.paths[0][0].short_channel_id, 42);
1106 assert_eq!(route.paths[0][0].fee_msat, 0);
1107 assert_eq!(route.paths[0][0].cltv_expiry_delta, (8 << 8) | 1);
1108 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
1109 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
1111 assert_eq!(route.paths[0][1].pubkey, node7);
1112 assert_eq!(route.paths[0][1].short_channel_id, 8);
1113 assert_eq!(route.paths[0][1].fee_msat, 100);
1114 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1115 assert_eq!(route.paths[0][1].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1116 assert_eq!(route.paths[0][1].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1118 last_hops[0].fees.base_msat = 1000;
1120 // Revert to via 6 as the fee on 8 goes up
1121 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node7, None, &last_hops, 100, 42, Arc::clone(&logger)).unwrap();
1122 assert_eq!(route.paths[0].len(), 4);
1124 assert_eq!(route.paths[0][0].pubkey, node2);
1125 assert_eq!(route.paths[0][0].short_channel_id, 2);
1126 assert_eq!(route.paths[0][0].fee_msat, 200); // fee increased as its % of value transferred across node
1127 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1128 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1129 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1131 assert_eq!(route.paths[0][1].pubkey, node3);
1132 assert_eq!(route.paths[0][1].short_channel_id, 4);
1133 assert_eq!(route.paths[0][1].fee_msat, 100);
1134 assert_eq!(route.paths[0][1].cltv_expiry_delta, (7 << 8) | 1);
1135 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1136 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1138 assert_eq!(route.paths[0][2].pubkey, node6);
1139 assert_eq!(route.paths[0][2].short_channel_id, 7);
1140 assert_eq!(route.paths[0][2].fee_msat, 0);
1141 assert_eq!(route.paths[0][2].cltv_expiry_delta, (10 << 8) | 1);
1142 // If we have a peer in the node map, we'll use their features here since we don't have
1143 // a way of figuring out their features from the invoice:
1144 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(6));
1145 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(7));
1147 assert_eq!(route.paths[0][3].pubkey, node7);
1148 assert_eq!(route.paths[0][3].short_channel_id, 10);
1149 assert_eq!(route.paths[0][3].fee_msat, 100);
1150 assert_eq!(route.paths[0][3].cltv_expiry_delta, 42);
1151 assert_eq!(route.paths[0][3].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1152 assert_eq!(route.paths[0][3].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1154 // ...but still use 8 for larger payments as 6 has a variable feerate
1155 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &node7, None, &last_hops, 2000, 42, Arc::clone(&logger)).unwrap();
1156 assert_eq!(route.paths[0].len(), 5);
1158 assert_eq!(route.paths[0][0].pubkey, node2);
1159 assert_eq!(route.paths[0][0].short_channel_id, 2);
1160 assert_eq!(route.paths[0][0].fee_msat, 3000);
1161 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1162 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1163 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1165 assert_eq!(route.paths[0][1].pubkey, node3);
1166 assert_eq!(route.paths[0][1].short_channel_id, 4);
1167 assert_eq!(route.paths[0][1].fee_msat, 0);
1168 assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1);
1169 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1170 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1172 assert_eq!(route.paths[0][2].pubkey, node5);
1173 assert_eq!(route.paths[0][2].short_channel_id, 6);
1174 assert_eq!(route.paths[0][2].fee_msat, 0);
1175 assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1);
1176 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(5));
1177 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(6));
1179 assert_eq!(route.paths[0][3].pubkey, node4);
1180 assert_eq!(route.paths[0][3].short_channel_id, 11);
1181 assert_eq!(route.paths[0][3].fee_msat, 1000);
1182 assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1);
1183 // If we have a peer in the node map, we'll use their features here since we don't have
1184 // a way of figuring out their features from the invoice:
1185 assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags!(4));
1186 assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags!(11));
1188 assert_eq!(route.paths[0][4].pubkey, node7);
1189 assert_eq!(route.paths[0][4].short_channel_id, 8);
1190 assert_eq!(route.paths[0][4].fee_msat, 2000);
1191 assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
1192 assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1193 assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly