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::ChannelDetails;
18 use ln::features::{ChannelFeatures, NodeFeatures};
19 use ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
20 use routing::network_graph::{NetworkGraph, RoutingFees};
21 use util::ser::{Writeable, Readable};
22 use util::logger::Logger;
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,
49 impl Writeable for Vec<RouteHop> {
50 fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
51 (self.len() as u8).write(writer)?;
52 for hop in self.iter() {
53 hop.pubkey.write(writer)?;
54 hop.node_features.write(writer)?;
55 hop.short_channel_id.write(writer)?;
56 hop.channel_features.write(writer)?;
57 hop.fee_msat.write(writer)?;
58 hop.cltv_expiry_delta.write(writer)?;
65 impl Readable for Vec<RouteHop> {
66 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Vec<RouteHop>, DecodeError> {
67 let hops_count: u8 = Readable::read(reader)?;
68 let mut hops = Vec::with_capacity(hops_count as usize);
69 for _ in 0..hops_count {
71 pubkey: Readable::read(reader)?,
72 node_features: Readable::read(reader)?,
73 short_channel_id: Readable::read(reader)?,
74 channel_features: Readable::read(reader)?,
75 fee_msat: Readable::read(reader)?,
76 cltv_expiry_delta: Readable::read(reader)?,
83 /// A route directs a payment from the sender (us) to the recipient. If the recipient supports MPP,
84 /// it can take multiple paths. Each path is composed of one or more hops through the network.
85 #[derive(Clone, PartialEq)]
87 /// The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
88 /// last RouteHop in each path must be the same.
89 /// Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
90 /// destination. Thus, this must always be at least length one. While the maximum length of any
91 /// given path is variable, keeping the length of any path to less than 20 should currently
92 /// ensure it is viable.
93 pub paths: Vec<Vec<RouteHop>>,
96 impl Writeable for Route {
97 fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
98 (self.paths.len() as u64).write(writer)?;
99 for hops in self.paths.iter() {
106 impl Readable for Route {
107 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Route, DecodeError> {
108 let path_count: u64 = Readable::read(reader)?;
109 let mut paths = Vec::with_capacity(cmp::min(path_count, 128) as usize);
110 for _ in 0..path_count {
111 paths.push(Readable::read(reader)?);
117 /// A channel descriptor which provides a last-hop route to get_route
119 pub struct RouteHint {
120 /// The node_id of the non-target end of the route
121 pub src_node_id: PublicKey,
122 /// The short_channel_id of this channel
123 pub short_channel_id: u64,
124 /// The fees which must be paid to use this channel
125 pub fees: RoutingFees,
126 /// The difference in CLTV values between this node and the next node.
127 pub cltv_expiry_delta: u16,
128 /// The minimum value, in msat, which must be relayed to the next hop.
129 pub htlc_minimum_msat: u64,
132 #[derive(Eq, PartialEq)]
133 struct RouteGraphNode {
135 lowest_fee_to_peer_through_node: u64,
136 lowest_fee_to_node: u64,
139 impl cmp::Ord for RouteGraphNode {
140 fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
141 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
142 .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
146 impl cmp::PartialOrd for RouteGraphNode {
147 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
148 Some(self.cmp(other))
152 struct DummyDirectionalChannelInfo {
153 cltv_expiry_delta: u32,
154 htlc_minimum_msat: u64,
159 /// Gets a route from us to the given target node.
161 /// Extra routing hops between known nodes and the target will be used if they are included in
164 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
165 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
166 /// view of our local channels (from net_graph_msg_handler) will be ignored, and only those in first_hops
169 /// Panics if first_hops contains channels without short_channel_ids
170 /// (ChannelManager::list_usable_channels will never include such channels).
172 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
173 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
174 /// *is* checked as they may change based on the receiving node.
175 pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, target: &PublicKey, first_hops: Option<&[&ChannelDetails]>,
176 last_hops: &[&RouteHint], final_value_msat: u64, final_cltv: u32, logger: L) -> Result<Route, LightningError> where L::Target: Logger {
177 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
178 // uptime/success in using a node in the past.
179 if *target == *our_node_id {
180 return Err(LightningError{err: "Cannot generate a route to ourselves".to_owned(), action: ErrorAction::IgnoreError});
183 if final_value_msat > MAX_VALUE_MSAT {
184 return Err(LightningError{err: "Cannot generate a route of more value than all existing satoshis".to_owned(), action: ErrorAction::IgnoreError});
187 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
188 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
189 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
190 // to use as the A* heuristic beyond just the cost to get one node further than the current
193 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
194 cltv_expiry_delta: 0,
195 htlc_minimum_msat: 0,
198 proportional_millionths: 0,
202 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
203 let mut dist = HashMap::with_capacity(network.get_nodes().len());
205 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
206 if let Some(hops) = first_hops {
208 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
209 if chan.remote_network_id == *target {
211 paths: vec![vec![RouteHop {
212 pubkey: chan.remote_network_id,
213 node_features: chan.counterparty_features.to_context(),
215 channel_features: chan.counterparty_features.to_context(),
216 fee_msat: final_value_msat,
217 cltv_expiry_delta: final_cltv,
221 first_hop_targets.insert(chan.remote_network_id, (short_channel_id, chan.counterparty_features.clone()));
223 if first_hop_targets.is_empty() {
224 return Err(LightningError{err: "Cannot route when there are no outbound routes away from us".to_owned(), action: ErrorAction::IgnoreError});
228 macro_rules! add_entry {
229 // Adds entry which goes from $src_node_id to $dest_node_id
230 // over the channel with id $chan_id with fees described in
231 // $directional_info.
232 ( $chan_id: expr, $src_node_id: expr, $dest_node_id: expr, $directional_info: expr, $chan_features: expr, $starting_fee_msat: expr ) => {
233 //TODO: Explore simply adding fee to hit htlc_minimum_msat
234 if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
235 let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fees.proportional_millionths as u64);
236 if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
237 ($directional_info.fees.base_msat as u64).checked_add(part / 1000000) })
239 let mut total_fee = $starting_fee_msat as u64;
240 let hm_entry = dist.entry(&$src_node_id);
241 let old_entry = hm_entry.or_insert_with(|| {
242 let mut fee_base_msat = u32::max_value();
243 let mut fee_proportional_millionths = u32::max_value();
244 if let Some(fees) = network.get_nodes().get(&$src_node_id).and_then(|node| node.lowest_inbound_channel_fees) {
245 fee_base_msat = fees.base_msat;
246 fee_proportional_millionths = fees.proportional_millionths;
250 fee_proportional_millionths,
252 pubkey: $dest_node_id.clone(),
253 node_features: NodeFeatures::empty(),
255 channel_features: $chan_features.clone(),
257 cltv_expiry_delta: 0,
261 if $src_node_id != *our_node_id {
262 // Ignore new_fee for channel-from-us as we assume all channels-from-us
263 // will have the same effective-fee
264 total_fee += new_fee;
265 if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
266 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
268 // max_value means we'll always fail the old_entry.0 > total_fee check
269 total_fee = u64::max_value();
272 let new_graph_node = RouteGraphNode {
273 pubkey: $src_node_id,
274 lowest_fee_to_peer_through_node: total_fee,
275 lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
277 if old_entry.0 > total_fee {
278 targets.push(new_graph_node);
279 old_entry.0 = total_fee;
280 old_entry.3 = RouteHop {
281 pubkey: $dest_node_id.clone(),
282 node_features: NodeFeatures::empty(),
283 short_channel_id: $chan_id.clone(),
284 channel_features: $chan_features.clone(),
285 fee_msat: new_fee, // This field is ignored on the last-hop anyway
286 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
294 macro_rules! add_entries_to_cheapest_to_target_node {
295 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
296 if first_hops.is_some() {
297 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&$node_id) {
298 add_entry!(first_hop, *our_node_id, $node_id, dummy_directional_info, features.to_context(), $fee_to_target_msat);
303 if let Some(node_info) = $node.announcement_info.as_ref() {
304 features = node_info.features.clone();
306 features = NodeFeatures::empty();
309 if !features.requires_unknown_bits() {
310 for chan_id in $node.channels.iter() {
311 let chan = network.get_channels().get(chan_id).unwrap();
312 if !chan.features.requires_unknown_bits() {
313 if chan.node_one == *$node_id {
314 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
315 if first_hops.is_none() || chan.node_two != *our_node_id {
316 if let Some(two_to_one) = chan.two_to_one.as_ref() {
317 if two_to_one.enabled {
318 add_entry!(chan_id, chan.node_two, chan.node_one, two_to_one, chan.features, $fee_to_target_msat);
323 if first_hops.is_none() || chan.node_one != *our_node_id {
324 if let Some(one_to_two) = chan.one_to_two.as_ref() {
325 if one_to_two.enabled {
326 add_entry!(chan_id, chan.node_one, chan.node_two, one_to_two, chan.features, $fee_to_target_msat);
338 match network.get_nodes().get(target) {
341 add_entries_to_cheapest_to_target_node!(node, target, 0);
345 for hop in last_hops.iter() {
346 let have_hop_src_in_graph =
347 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&hop.src_node_id) {
348 // If this hop connects to a node with which we have a direct channel, ignore the
349 // network graph and add both the hop and our direct channel to the candidate set:
351 // Currently there are no channel-context features defined, so we are a
352 // bit lazy here. In the future, we should pull them out via our
353 // ChannelManager, but there's no reason to waste the space until we
355 add_entry!(first_hop, *our_node_id , hop.src_node_id, dummy_directional_info, features.to_context(), 0);
358 // In any other case, only add the hop if the source is in the regular network
360 network.get_nodes().get(&hop.src_node_id).is_some()
362 if have_hop_src_in_graph {
363 // BOLT 11 doesn't allow inclusion of features for the last hop hints, which
364 // really sucks, cause we're gonna need that eventually.
365 add_entry!(hop.short_channel_id, hop.src_node_id, target, hop, ChannelFeatures::empty(), 0);
369 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
370 if pubkey == *our_node_id {
371 let mut res = vec!(dist.remove(&our_node_id).unwrap().3);
373 if let Some(&(_, ref features)) = first_hop_targets.get(&res.last().unwrap().pubkey) {
374 res.last_mut().unwrap().node_features = features.to_context();
375 } else if let Some(node) = network.get_nodes().get(&res.last().unwrap().pubkey) {
376 if let Some(node_info) = node.announcement_info.as_ref() {
377 res.last_mut().unwrap().node_features = node_info.features.clone();
379 res.last_mut().unwrap().node_features = NodeFeatures::empty();
382 // We should be able to fill in features for everything except the last
383 // hop, if the last hop was provided via a BOLT 11 invoice (though we
384 // should be able to extend it further as BOLT 11 does have feature
385 // flags for the last hop node itself).
386 assert!(res.last().unwrap().pubkey == *target);
388 if res.last().unwrap().pubkey == *target {
392 let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
394 None => return Err(LightningError{err: "Failed to find a non-fee-overflowing path to the given destination".to_owned(), action: ErrorAction::IgnoreError}),
396 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
397 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
400 res.last_mut().unwrap().fee_msat = final_value_msat;
401 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
402 let route = Route { paths: vec![res] };
403 log_trace!(logger, "Got route: {}", log_route!(route));
407 match network.get_nodes().get(&pubkey) {
410 add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
415 Err(LightningError{err: "Failed to find a path to the given destination".to_owned(), action: ErrorAction::IgnoreError})
420 use routing::router::{get_route, RouteHint, RoutingFees};
421 use routing::network_graph::{NetworkGraph, NetGraphMsgHandler};
422 use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
423 use ln::msgs::{ErrorAction, LightningError, OptionalField, UnsignedChannelAnnouncement, ChannelAnnouncement, RoutingMessageHandler,
424 NodeAnnouncement, UnsignedNodeAnnouncement, ChannelUpdate, UnsignedChannelUpdate};
425 use ln::channelmanager;
426 use util::test_utils;
427 use util::ser::Writeable;
429 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
430 use bitcoin::hashes::Hash;
431 use bitcoin::network::constants::Network;
432 use bitcoin::blockdata::constants::genesis_block;
436 use bitcoin::secp256k1::key::{PublicKey,SecretKey};
437 use bitcoin::secp256k1::{Secp256k1, All};
441 // Using the same keys for LN and BTC ids
442 fn add_channel(net_graph_msg_handler: &NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>, secp_ctx: &Secp256k1<All>, node_1_privkey: &SecretKey,
443 node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64) {
444 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
445 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
447 let unsigned_announcement = UnsignedChannelAnnouncement {
449 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
453 bitcoin_key_1: node_id_1,
454 bitcoin_key_2: node_id_2,
455 excess_data: Vec::new(),
458 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
459 let valid_announcement = ChannelAnnouncement {
460 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
461 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
462 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
463 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
464 contents: unsigned_announcement.clone(),
466 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
467 Ok(res) => assert!(res),
472 fn update_channel(net_graph_msg_handler: &NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>, secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, update: UnsignedChannelUpdate) {
473 let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]);
474 let valid_channel_update = ChannelUpdate {
475 signature: secp_ctx.sign(&msghash, node_privkey),
476 contents: update.clone()
479 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
480 Ok(res) => assert!(res),
481 // Err(_) => panic!()
482 Err(e) => println!("{:?}", e.err)
487 fn add_or_update_node(net_graph_msg_handler: &NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>, secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey,
488 features: NodeFeatures, timestamp: u32) {
489 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
490 let unsigned_announcement = UnsignedNodeAnnouncement {
496 addresses: Vec::new(),
497 excess_address_data: Vec::new(),
498 excess_data: Vec::new(),
500 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
501 let valid_announcement = NodeAnnouncement {
502 signature: secp_ctx.sign(&msghash, node_privkey),
503 contents: unsigned_announcement.clone()
506 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
512 fn get_nodes(secp_ctx: &Secp256k1<All>) -> (SecretKey, PublicKey, Vec<SecretKey>, Vec<PublicKey>) {
513 let privkeys: Vec<SecretKey> = (2..10).map(|i| {
514 SecretKey::from_slice(&hex::decode(format!("{:02}", i).repeat(32)).unwrap()[..]).unwrap()
517 let pubkeys = privkeys.iter().map(|secret| PublicKey::from_secret_key(&secp_ctx, secret)).collect();
519 let our_privkey = SecretKey::from_slice(&hex::decode("01".repeat(32)).unwrap()[..]).unwrap();
520 let our_id = PublicKey::from_secret_key(&secp_ctx, &our_privkey);
522 (our_privkey, our_id, privkeys, pubkeys)
525 fn id_to_feature_flags(id: u8) -> Vec<u8> {
526 // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can
527 // test for it later.
528 let idx = (id - 1) * 2 + 1;
530 vec![1 << (idx - 8*3), 0, 0, 0]
531 } else if idx > 8*2 {
532 vec![1 << (idx - 8*2), 0, 0]
533 } else if idx > 8*1 {
534 vec![1 << (idx - 8*1), 0]
540 fn build_graph() -> (Secp256k1<All>, NetGraphMsgHandler<std::sync::Arc<crate::util::test_utils::TestChainSource>, std::sync::Arc<crate::util::test_utils::TestLogger>>, std::sync::Arc<test_utils::TestLogger>) {
541 let secp_ctx = Secp256k1::new();
542 let logger = Arc::new(test_utils::TestLogger::new());
543 let net_graph_msg_handler = NetGraphMsgHandler::new(genesis_block(Network::Testnet).header.block_hash(), None, Arc::clone(&logger));
544 // Build network from our_id to node7:
546 // -1(1)2- node0 -1(3)2-
548 // our_id -1(12)2- node7 -1(13)2--- node2
550 // -1(2)2- node1 -1(4)2-
553 // chan1 1-to-2: disabled
554 // chan1 2-to-1: enabled, 0 fee
556 // chan2 1-to-2: enabled, ignored fee
557 // chan2 2-to-1: enabled, 0 fee
559 // chan3 1-to-2: enabled, 0 fee
560 // chan3 2-to-1: enabled, 100 msat fee
562 // chan4 1-to-2: enabled, 100% fee
563 // chan4 2-to-1: enabled, 0 fee
565 // chan12 1-to-2: enabled, ignored fee
566 // chan12 2-to-1: enabled, 0 fee
568 // chan13 1-to-2: enabled, 200% fee
569 // chan13 2-to-1: enabled, 0 fee
572 // -1(5)2- node3 -1(8)2--
576 // node2--1(6)2- node4 -1(9)2--- node6 (not in global route map)
578 // -1(7)2- node5 -1(10)2-
580 // chan5 1-to-2: enabled, 100 msat fee
581 // chan5 2-to-1: enabled, 0 fee
583 // chan6 1-to-2: enabled, 0 fee
584 // chan6 2-to-1: enabled, 0 fee
586 // chan7 1-to-2: enabled, 100% fee
587 // chan7 2-to-1: enabled, 0 fee
589 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
590 // chan8 2-to-1: enabled, 0 fee
592 // chan9 1-to-2: enabled, 1001 msat fee
593 // chan9 2-to-1: enabled, 0 fee
595 // chan10 1-to-2: enabled, 0 fee
596 // chan10 2-to-1: enabled, 0 fee
598 // chan11 1-to-2: enabled, 0 fee
599 // chan11 2-to-1: enabled, 0 fee
601 let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx);
603 add_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, &privkeys[0], ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), 1);
604 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
605 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
609 cltv_expiry_delta: 0,
610 htlc_minimum_msat: 0,
611 htlc_maximum_msat: OptionalField::Absent,
613 fee_proportional_millionths: 0,
614 excess_data: Vec::new()
617 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[0], NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0);
619 add_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, &privkeys[1], ChannelFeatures::from_le_bytes(id_to_feature_flags(2)), 2);
620 update_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
621 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
625 cltv_expiry_delta: u16::max_value(),
626 htlc_minimum_msat: 0,
627 htlc_maximum_msat: OptionalField::Absent,
628 fee_base_msat: u32::max_value(),
629 fee_proportional_millionths: u32::max_value(),
630 excess_data: Vec::new()
632 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
633 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
637 cltv_expiry_delta: 0,
638 htlc_minimum_msat: 0,
639 htlc_maximum_msat: OptionalField::Absent,
641 fee_proportional_millionths: 0,
642 excess_data: Vec::new()
645 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[1], NodeFeatures::from_le_bytes(id_to_feature_flags(2)), 0);
647 add_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, &privkeys[7], ChannelFeatures::from_le_bytes(id_to_feature_flags(12)), 12);
648 update_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
649 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
650 short_channel_id: 12,
653 cltv_expiry_delta: u16::max_value(),
654 htlc_minimum_msat: 0,
655 htlc_maximum_msat: OptionalField::Absent,
656 fee_base_msat: u32::max_value(),
657 fee_proportional_millionths: u32::max_value(),
658 excess_data: Vec::new()
660 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[7], UnsignedChannelUpdate {
661 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
662 short_channel_id: 12,
665 cltv_expiry_delta: 0,
666 htlc_minimum_msat: 0,
667 htlc_maximum_msat: OptionalField::Absent,
669 fee_proportional_millionths: 0,
670 excess_data: Vec::new()
673 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[7], NodeFeatures::from_le_bytes(id_to_feature_flags(8)), 0);
675 add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[0], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(3)), 3);
676 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
677 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
681 cltv_expiry_delta: (3 << 8) | 1,
682 htlc_minimum_msat: 0,
683 htlc_maximum_msat: OptionalField::Absent,
685 fee_proportional_millionths: 0,
686 excess_data: Vec::new()
688 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
689 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
693 cltv_expiry_delta: (3 << 8) | 2,
694 htlc_minimum_msat: 0,
695 htlc_maximum_msat: OptionalField::Absent,
697 fee_proportional_millionths: 0,
698 excess_data: Vec::new()
701 add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[1], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(4)), 4);
702 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
703 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
707 cltv_expiry_delta: (4 << 8) | 1,
708 htlc_minimum_msat: 0,
709 htlc_maximum_msat: OptionalField::Absent,
711 fee_proportional_millionths: 1000000,
712 excess_data: Vec::new()
714 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
715 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
719 cltv_expiry_delta: (4 << 8) | 2,
720 htlc_minimum_msat: 0,
721 htlc_maximum_msat: OptionalField::Absent,
723 fee_proportional_millionths: 0,
724 excess_data: Vec::new()
727 add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[7], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(13)), 13);
728 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[7], UnsignedChannelUpdate {
729 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
730 short_channel_id: 13,
733 cltv_expiry_delta: (13 << 8) | 1,
734 htlc_minimum_msat: 0,
735 htlc_maximum_msat: OptionalField::Absent,
737 fee_proportional_millionths: 2000000,
738 excess_data: Vec::new()
740 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
741 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
742 short_channel_id: 13,
745 cltv_expiry_delta: (13 << 8) | 2,
746 htlc_minimum_msat: 0,
747 htlc_maximum_msat: OptionalField::Absent,
749 fee_proportional_millionths: 0,
750 excess_data: Vec::new()
753 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[2], NodeFeatures::from_le_bytes(id_to_feature_flags(3)), 0);
755 add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], &privkeys[4], ChannelFeatures::from_le_bytes(id_to_feature_flags(6)), 6);
756 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
757 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
761 cltv_expiry_delta: (6 << 8) | 1,
762 htlc_minimum_msat: 0,
763 htlc_maximum_msat: OptionalField::Absent,
765 fee_proportional_millionths: 0,
766 excess_data: Vec::new()
768 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[4], UnsignedChannelUpdate {
769 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
773 cltv_expiry_delta: (6 << 8) | 2,
774 htlc_minimum_msat: 0,
775 htlc_maximum_msat: OptionalField::Absent,
777 fee_proportional_millionths: 0,
778 excess_data: Vec::new(),
781 add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[4], &privkeys[3], ChannelFeatures::from_le_bytes(id_to_feature_flags(11)), 11);
782 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[4], UnsignedChannelUpdate {
783 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
784 short_channel_id: 11,
787 cltv_expiry_delta: (11 << 8) | 1,
788 htlc_minimum_msat: 0,
789 htlc_maximum_msat: OptionalField::Absent,
791 fee_proportional_millionths: 0,
792 excess_data: Vec::new()
794 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[3], UnsignedChannelUpdate {
795 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
796 short_channel_id: 11,
799 cltv_expiry_delta: (11 << 8) | 2,
800 htlc_minimum_msat: 0,
801 htlc_maximum_msat: OptionalField::Absent,
803 fee_proportional_millionths: 0,
804 excess_data: Vec::new()
807 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[4], NodeFeatures::from_le_bytes(id_to_feature_flags(5)), 0);
809 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[3], NodeFeatures::from_le_bytes(id_to_feature_flags(4)), 0);
811 add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], &privkeys[5], ChannelFeatures::from_le_bytes(id_to_feature_flags(7)), 7);
812 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
813 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
817 cltv_expiry_delta: (7 << 8) | 1,
818 htlc_minimum_msat: 0,
819 htlc_maximum_msat: OptionalField::Absent,
821 fee_proportional_millionths: 1000000,
822 excess_data: Vec::new()
824 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[5], UnsignedChannelUpdate {
825 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
829 cltv_expiry_delta: (7 << 8) | 2,
830 htlc_minimum_msat: 0,
831 htlc_maximum_msat: OptionalField::Absent,
833 fee_proportional_millionths: 0,
834 excess_data: Vec::new()
837 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[5], NodeFeatures::from_le_bytes(id_to_feature_flags(6)), 0);
839 (secp_ctx, net_graph_msg_handler, logger)
843 fn simple_route_test() {
844 let (secp_ctx, net_graph_msg_handler, logger) = build_graph();
845 let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
847 // Simple route to 3 via 2
848 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], None, &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
849 assert_eq!(route.paths[0].len(), 2);
851 assert_eq!(route.paths[0][0].pubkey, nodes[1]);
852 assert_eq!(route.paths[0][0].short_channel_id, 2);
853 assert_eq!(route.paths[0][0].fee_msat, 100);
854 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
855 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
856 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
858 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
859 assert_eq!(route.paths[0][1].short_channel_id, 4);
860 assert_eq!(route.paths[0][1].fee_msat, 100);
861 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
862 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
863 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
867 fn disable_channels_test() {
868 let (secp_ctx, net_graph_msg_handler, logger) = build_graph();
869 let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx);
871 // // Disable channels 4 and 12 by flags=2
872 update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
873 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
876 flags: 2, // to disable
877 cltv_expiry_delta: 0,
878 htlc_minimum_msat: 0,
879 htlc_maximum_msat: OptionalField::Absent,
881 fee_proportional_millionths: 0,
882 excess_data: Vec::new()
884 update_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
885 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
886 short_channel_id: 12,
888 flags: 2, // to disable
889 cltv_expiry_delta: 0,
890 htlc_minimum_msat: 0,
891 htlc_maximum_msat: OptionalField::Absent,
893 fee_proportional_millionths: 0,
894 excess_data: Vec::new()
897 // If all the channels require some features we don't understand, route should fail
898 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], None, &Vec::new(), 100, 42, Arc::clone(&logger)) {
899 assert_eq!(err, "Failed to find a path to the given destination");
902 // If we specify a channel to node7, that overrides our local channel view and that gets used
903 let our_chans = vec![channelmanager::ChannelDetails {
905 short_channel_id: Some(42),
906 remote_network_id: nodes[7].clone(),
907 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
908 channel_value_satoshis: 0,
910 outbound_capacity_msat: 0,
911 inbound_capacity_msat: 0,
914 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], Some(&our_chans.iter().collect::<Vec<_>>()), &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
915 assert_eq!(route.paths[0].len(), 2);
917 assert_eq!(route.paths[0][0].pubkey, nodes[7]);
918 assert_eq!(route.paths[0][0].short_channel_id, 42);
919 assert_eq!(route.paths[0][0].fee_msat, 200);
920 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
921 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
922 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
924 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
925 assert_eq!(route.paths[0][1].short_channel_id, 13);
926 assert_eq!(route.paths[0][1].fee_msat, 100);
927 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
928 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
929 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
933 fn disable_node_test() {
934 let (secp_ctx, net_graph_msg_handler, logger) = build_graph();
935 let (_, our_id, privkeys, nodes) = get_nodes(&secp_ctx);
937 // Disable nodes 1, 2, and 8 by requiring unknown feature bits
938 let mut unknown_features = NodeFeatures::known();
939 unknown_features.set_required_unknown_bits();
940 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[0], unknown_features.clone(), 1);
941 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[1], unknown_features.clone(), 1);
942 add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[7], unknown_features.clone(), 1);
944 // If all nodes require some features we don't understand, route should fail
945 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], None, &Vec::new(), 100, 42, Arc::clone(&logger)) {
946 assert_eq!(err, "Failed to find a path to the given destination");
949 // If we specify a channel to node7, that overrides our local channel view and that gets used
950 let our_chans = vec![channelmanager::ChannelDetails {
952 short_channel_id: Some(42),
953 remote_network_id: nodes[7].clone(),
954 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
955 channel_value_satoshis: 0,
957 outbound_capacity_msat: 0,
958 inbound_capacity_msat: 0,
961 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], Some(&our_chans.iter().collect::<Vec<_>>()), &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
962 assert_eq!(route.paths[0].len(), 2);
964 assert_eq!(route.paths[0][0].pubkey, nodes[7]);
965 assert_eq!(route.paths[0][0].short_channel_id, 42);
966 assert_eq!(route.paths[0][0].fee_msat, 200);
967 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
968 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
969 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
971 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
972 assert_eq!(route.paths[0][1].short_channel_id, 13);
973 assert_eq!(route.paths[0][1].fee_msat, 100);
974 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
975 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
976 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
978 // Note that we don't test disabling node 3 and failing to route to it, as we (somewhat
979 // naively) assume that the user checked the feature bits on the invoice, which override
980 // the node_announcement.
984 fn our_chans_test() {
985 let (secp_ctx, net_graph_msg_handler, logger) = build_graph();
986 let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
988 // Route to 1 via 2 and 3 because our channel to 1 is disabled
989 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[0], None, &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
990 assert_eq!(route.paths[0].len(), 3);
992 assert_eq!(route.paths[0][0].pubkey, nodes[1]);
993 assert_eq!(route.paths[0][0].short_channel_id, 2);
994 assert_eq!(route.paths[0][0].fee_msat, 200);
995 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
996 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
997 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
999 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
1000 assert_eq!(route.paths[0][1].short_channel_id, 4);
1001 assert_eq!(route.paths[0][1].fee_msat, 100);
1002 assert_eq!(route.paths[0][1].cltv_expiry_delta, (3 << 8) | 2);
1003 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
1004 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
1006 assert_eq!(route.paths[0][2].pubkey, nodes[0]);
1007 assert_eq!(route.paths[0][2].short_channel_id, 3);
1008 assert_eq!(route.paths[0][2].fee_msat, 100);
1009 assert_eq!(route.paths[0][2].cltv_expiry_delta, 42);
1010 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(1));
1011 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(3));
1013 // If we specify a channel to node7, that overrides our local channel view and that gets used
1014 let our_chans = vec![channelmanager::ChannelDetails {
1015 channel_id: [0; 32],
1016 short_channel_id: Some(42),
1017 remote_network_id: nodes[7].clone(),
1018 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1019 channel_value_satoshis: 0,
1021 outbound_capacity_msat: 0,
1022 inbound_capacity_msat: 0,
1025 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], Some(&our_chans.iter().collect::<Vec<_>>()), &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap();
1026 assert_eq!(route.paths[0].len(), 2);
1028 assert_eq!(route.paths[0][0].pubkey, nodes[7]);
1029 assert_eq!(route.paths[0][0].short_channel_id, 42);
1030 assert_eq!(route.paths[0][0].fee_msat, 200);
1031 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
1032 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
1033 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
1035 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
1036 assert_eq!(route.paths[0][1].short_channel_id, 13);
1037 assert_eq!(route.paths[0][1].fee_msat, 100);
1038 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1039 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
1040 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
1043 fn last_hops(nodes: &Vec<PublicKey>) -> Vec<RouteHint> {
1044 let zero_fees = RoutingFees {
1046 proportional_millionths: 0,
1049 src_node_id: nodes[3].clone(),
1050 short_channel_id: 8,
1052 cltv_expiry_delta: (8 << 8) | 1,
1053 htlc_minimum_msat: 0,
1055 src_node_id: nodes[4].clone(),
1056 short_channel_id: 9,
1059 proportional_millionths: 0,
1061 cltv_expiry_delta: (9 << 8) | 1,
1062 htlc_minimum_msat: 0,
1064 src_node_id: nodes[5].clone(),
1065 short_channel_id: 10,
1067 cltv_expiry_delta: (10 << 8) | 1,
1068 htlc_minimum_msat: 0,
1073 fn last_hops_test() {
1074 let (secp_ctx, net_graph_msg_handler, logger) = build_graph();
1075 let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
1077 // Simple test across 2, 3, 5, and 4 via a last_hop channel
1078 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[6], None, &last_hops(&nodes).iter().collect::<Vec<_>>(), 100, 42, Arc::clone(&logger)).unwrap();
1079 assert_eq!(route.paths[0].len(), 5);
1081 assert_eq!(route.paths[0][0].pubkey, nodes[1]);
1082 assert_eq!(route.paths[0][0].short_channel_id, 2);
1083 assert_eq!(route.paths[0][0].fee_msat, 100);
1084 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1085 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
1086 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
1088 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
1089 assert_eq!(route.paths[0][1].short_channel_id, 4);
1090 assert_eq!(route.paths[0][1].fee_msat, 0);
1091 assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1);
1092 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
1093 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
1095 assert_eq!(route.paths[0][2].pubkey, nodes[4]);
1096 assert_eq!(route.paths[0][2].short_channel_id, 6);
1097 assert_eq!(route.paths[0][2].fee_msat, 0);
1098 assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1);
1099 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(5));
1100 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(6));
1102 assert_eq!(route.paths[0][3].pubkey, nodes[3]);
1103 assert_eq!(route.paths[0][3].short_channel_id, 11);
1104 assert_eq!(route.paths[0][3].fee_msat, 0);
1105 assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1);
1106 // If we have a peer in the node map, we'll use their features here since we don't have
1107 // a way of figuring out their features from the invoice:
1108 assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags(4));
1109 assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags(11));
1111 assert_eq!(route.paths[0][4].pubkey, nodes[6]);
1112 assert_eq!(route.paths[0][4].short_channel_id, 8);
1113 assert_eq!(route.paths[0][4].fee_msat, 100);
1114 assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
1115 assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1116 assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1120 fn our_chans_last_hop_connect_test() {
1121 let (secp_ctx, net_graph_msg_handler, logger) = build_graph();
1122 let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
1124 // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1125 let our_chans = vec![channelmanager::ChannelDetails {
1126 channel_id: [0; 32],
1127 short_channel_id: Some(42),
1128 remote_network_id: nodes[3].clone(),
1129 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1130 channel_value_satoshis: 0,
1132 outbound_capacity_msat: 0,
1133 inbound_capacity_msat: 0,
1136 let mut last_hops = last_hops(&nodes);
1137 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[6], Some(&our_chans.iter().collect::<Vec<_>>()), &last_hops.iter().collect::<Vec<_>>(), 100, 42, Arc::clone(&logger)).unwrap();
1138 assert_eq!(route.paths[0].len(), 2);
1140 assert_eq!(route.paths[0][0].pubkey, nodes[3]);
1141 assert_eq!(route.paths[0][0].short_channel_id, 42);
1142 assert_eq!(route.paths[0][0].fee_msat, 0);
1143 assert_eq!(route.paths[0][0].cltv_expiry_delta, (8 << 8) | 1);
1144 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
1145 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
1147 assert_eq!(route.paths[0][1].pubkey, nodes[6]);
1148 assert_eq!(route.paths[0][1].short_channel_id, 8);
1149 assert_eq!(route.paths[0][1].fee_msat, 100);
1150 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1151 assert_eq!(route.paths[0][1].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1152 assert_eq!(route.paths[0][1].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1154 last_hops[0].fees.base_msat = 1000;
1156 // Revert to via 6 as the fee on 8 goes up
1157 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[6], None, &last_hops.iter().collect::<Vec<_>>(), 100, 42, Arc::clone(&logger)).unwrap();
1158 assert_eq!(route.paths[0].len(), 4);
1160 assert_eq!(route.paths[0][0].pubkey, nodes[1]);
1161 assert_eq!(route.paths[0][0].short_channel_id, 2);
1162 assert_eq!(route.paths[0][0].fee_msat, 200); // fee increased as its % of value transferred across node
1163 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1164 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
1165 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
1167 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
1168 assert_eq!(route.paths[0][1].short_channel_id, 4);
1169 assert_eq!(route.paths[0][1].fee_msat, 100);
1170 assert_eq!(route.paths[0][1].cltv_expiry_delta, (7 << 8) | 1);
1171 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
1172 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
1174 assert_eq!(route.paths[0][2].pubkey, nodes[5]);
1175 assert_eq!(route.paths[0][2].short_channel_id, 7);
1176 assert_eq!(route.paths[0][2].fee_msat, 0);
1177 assert_eq!(route.paths[0][2].cltv_expiry_delta, (10 << 8) | 1);
1178 // If we have a peer in the node map, we'll use their features here since we don't have
1179 // a way of figuring out their features from the invoice:
1180 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(6));
1181 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(7));
1183 assert_eq!(route.paths[0][3].pubkey, nodes[6]);
1184 assert_eq!(route.paths[0][3].short_channel_id, 10);
1185 assert_eq!(route.paths[0][3].fee_msat, 100);
1186 assert_eq!(route.paths[0][3].cltv_expiry_delta, 42);
1187 assert_eq!(route.paths[0][3].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1188 assert_eq!(route.paths[0][3].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1190 // ...but still use 8 for larger payments as 6 has a variable feerate
1191 let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[6], None, &last_hops.iter().collect::<Vec<_>>(), 2000, 42, Arc::clone(&logger)).unwrap();
1192 assert_eq!(route.paths[0].len(), 5);
1194 assert_eq!(route.paths[0][0].pubkey, nodes[1]);
1195 assert_eq!(route.paths[0][0].short_channel_id, 2);
1196 assert_eq!(route.paths[0][0].fee_msat, 3000);
1197 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1198 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
1199 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
1201 assert_eq!(route.paths[0][1].pubkey, nodes[2]);
1202 assert_eq!(route.paths[0][1].short_channel_id, 4);
1203 assert_eq!(route.paths[0][1].fee_msat, 0);
1204 assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1);
1205 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
1206 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
1208 assert_eq!(route.paths[0][2].pubkey, nodes[4]);
1209 assert_eq!(route.paths[0][2].short_channel_id, 6);
1210 assert_eq!(route.paths[0][2].fee_msat, 0);
1211 assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1);
1212 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(5));
1213 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(6));
1215 assert_eq!(route.paths[0][3].pubkey, nodes[3]);
1216 assert_eq!(route.paths[0][3].short_channel_id, 11);
1217 assert_eq!(route.paths[0][3].fee_msat, 1000);
1218 assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1);
1219 // If we have a peer in the node map, we'll use their features here since we don't have
1220 // a way of figuring out their features from the invoice:
1221 assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags(4));
1222 assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags(11));
1224 assert_eq!(route.paths[0][4].pubkey, nodes[6]);
1225 assert_eq!(route.paths[0][4].short_channel_id, 8);
1226 assert_eq!(route.paths[0][4].fee_msat, 2000);
1227 assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
1228 assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1229 assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1233 fn unannounced_path_test() {
1234 // We should be able to send a payment to a destination without any help of a routing graph
1235 // if we have a channel with a common counterparty that appears in the first and last hop
1237 let source_node_id = PublicKey::from_secret_key(&Secp256k1::new(), &SecretKey::from_slice(&hex::decode(format!("{:02}", 41).repeat(32)).unwrap()[..]).unwrap());
1238 let middle_node_id = PublicKey::from_secret_key(&Secp256k1::new(), &SecretKey::from_slice(&hex::decode(format!("{:02}", 42).repeat(32)).unwrap()[..]).unwrap());
1239 let target_node_id = PublicKey::from_secret_key(&Secp256k1::new(), &SecretKey::from_slice(&hex::decode(format!("{:02}", 43).repeat(32)).unwrap()[..]).unwrap());
1241 // If we specify a channel to a middle hop, that overrides our local channel view and that gets used
1242 let last_hops = vec![RouteHint {
1243 src_node_id: middle_node_id,
1244 short_channel_id: 8,
1247 proportional_millionths: 0,
1249 cltv_expiry_delta: (8 << 8) | 1,
1250 htlc_minimum_msat: 0,
1252 let our_chans = vec![channelmanager::ChannelDetails {
1253 channel_id: [0; 32],
1254 short_channel_id: Some(42),
1255 remote_network_id: middle_node_id,
1256 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1257 channel_value_satoshis: 100000,
1259 outbound_capacity_msat: 100000,
1260 inbound_capacity_msat: 100000,
1263 let route = get_route(&source_node_id, &NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash()), &target_node_id, Some(&our_chans.iter().collect::<Vec<_>>()), &last_hops.iter().collect::<Vec<_>>(), 100, 42, Arc::new(test_utils::TestLogger::new())).unwrap();
1265 assert_eq!(route.paths[0].len(), 2);
1267 assert_eq!(route.paths[0][0].pubkey, middle_node_id);
1268 assert_eq!(route.paths[0][0].short_channel_id, 42);
1269 assert_eq!(route.paths[0][0].fee_msat, 1000);
1270 assert_eq!(route.paths[0][0].cltv_expiry_delta, (8 << 8) | 1);
1271 assert_eq!(route.paths[0][0].node_features.le_flags(), &[0b11]);
1272 assert_eq!(route.paths[0][0].channel_features.le_flags(), &[0; 0]); // We can't learn any flags from invoices, sadly
1274 assert_eq!(route.paths[0][1].pubkey, target_node_id);
1275 assert_eq!(route.paths[0][1].short_channel_id, 8);
1276 assert_eq!(route.paths[0][1].fee_msat, 100);
1277 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1278 assert_eq!(route.paths[0][1].node_features.le_flags(), &[0; 0]); // We dont pass flags in from invoices yet
1279 assert_eq!(route.paths[0][1].channel_features.le_flags(), &[0; 0]); // We can't learn any flags from invoices, sadly
1283 #[cfg(all(test, feature = "unstable"))]
1286 use util::logger::{Logger, Record};
1291 struct DummyLogger {}
1292 impl Logger for DummyLogger {
1293 fn log(&self, _record: &Record) {}
1297 fn generate_routes(bench: &mut Bencher) {
1298 let mut d = File::open("net_graph-2021-02-12.bin").expect("Please fetch https://bitcoin.ninja/ldk-net_graph-879e309c128-2020-02-12.bin and place it at lightning/net_graph-2021-02-12.bin");
1299 let graph = NetworkGraph::read(&mut d).unwrap();
1301 // First, get 100 (source, destination) pairs for which route-getting actually succeeds...
1302 let mut path_endpoints = Vec::new();
1303 let mut seed: usize = 0xdeadbeef;
1304 'load_endpoints: for _ in 0..100 {
1307 let src = graph.get_nodes().keys().skip(seed % graph.get_nodes().len()).next().unwrap();
1309 let dst = graph.get_nodes().keys().skip(seed % graph.get_nodes().len()).next().unwrap();
1310 let amt = seed as u64 % 1_000_000;
1311 if get_route(src, &graph, dst, None, &[], amt, 42, &DummyLogger{}).is_ok() {
1312 path_endpoints.push((src, dst, amt));
1313 continue 'load_endpoints;
1318 // ...then benchmark finding paths between the nodes we learned.
1321 let (src, dst, amt) = path_endpoints[idx % path_endpoints.len()];
1322 assert!(get_route(src, &graph, dst, None, &[], amt, 42, &DummyLogger{}).is_ok());