1 use secp256k1::key::PublicKey;
2 use secp256k1::{Secp256k1,Message};
5 use bitcoin::util::hash::Sha256dHash;
6 use bitcoin::blockdata::script::Builder;
7 use bitcoin::blockdata::opcodes;
9 use chain::chaininterface::{ChainError, ChainWatchInterface};
10 use ln::channelmanager;
11 use ln::msgs::{ErrorAction,HandleError,RoutingMessageHandler,MsgEncodable,NetAddress,GlobalFeatures};
13 use util::logger::Logger;
16 use std::sync::{RwLock,Arc};
17 use std::collections::{HashMap,BinaryHeap};
18 use std::collections::hash_map::Entry;
24 pub pubkey: PublicKey,
25 /// The channel that should be used from the previous hop to reach this node.
26 pub short_channel_id: u64,
27 /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
29 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
30 /// expected at the destination, in excess of the current block height.
31 pub cltv_expiry_delta: u32,
34 /// A route from us through the network to a destination
37 /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
38 /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
40 pub hops: Vec<RouteHop>,
43 struct DirectionalChannelInfo {
44 src_node_id: PublicKey,
47 cltv_expiry_delta: u16,
48 htlc_minimum_msat: u64,
50 fee_proportional_millionths: u32,
53 impl std::fmt::Display for DirectionalChannelInfo {
54 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
55 write!(f, "src_node_id {}, last_update {}, enabled {}, cltv_expiry_delta {}, htlc_minimum_msat {}, fee_base_msat {}, fee_proportional_millionths {}", log_pubkey!(self.src_node_id), self.last_update, self.enabled, self.cltv_expiry_delta, self.htlc_minimum_msat, self.fee_base_msat, self.fee_proportional_millionths)?;
61 features: GlobalFeatures,
62 one_to_two: DirectionalChannelInfo,
63 two_to_one: DirectionalChannelInfo,
66 impl std::fmt::Display for ChannelInfo {
67 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
68 write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?;
74 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
75 channels: Vec<(u64, Sha256dHash)>,
76 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
79 lowest_inbound_channel_fee_base_msat: u32,
80 lowest_inbound_channel_fee_proportional_millionths: u32,
82 features: GlobalFeatures,
86 addresses: Vec<NetAddress>,
89 impl std::fmt::Display for NodeInfo {
90 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
91 write!(f, "features: {}, last_update: {}, lowest_inbound_channel_fee_base_msat: {}, lowest_inbound_channel_fee_proportional_millionths: {}, channels: {:?}", log_bytes!(self.features.encode()), self.last_update, self.lowest_inbound_channel_fee_base_msat, self.lowest_inbound_channel_fee_proportional_millionths, &self.channels[..])?;
97 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
98 channels: HashMap<(u64, Sha256dHash), ChannelInfo>,
99 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
100 channels: HashMap<u64, ChannelInfo>,
102 our_node_id: PublicKey,
103 nodes: HashMap<PublicKey, NodeInfo>,
106 impl std::fmt::Display for NetworkMap {
107 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
108 write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
109 for (key, val) in self.channels.iter() {
110 write!(f, " {}: {}\n", key, val)?;
112 write!(f, "[Nodes]\n")?;
113 for (key, val) in self.nodes.iter() {
114 write!(f, " {}: {}\n", log_pubkey!(key), val)?;
121 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
123 fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
124 (short_channel_id, chain_hash)
127 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
129 fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
133 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
135 fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
139 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
141 fn get_short_id(id: &u64) -> &u64 {
146 /// A channel descriptor which provides a last-hop route to get_route
147 pub struct RouteHint {
148 pub src_node_id: PublicKey,
149 pub short_channel_id: u64,
150 pub fee_base_msat: u32,
151 pub fee_proportional_millionths: u32,
152 pub cltv_expiry_delta: u16,
153 pub htlc_minimum_msat: u64,
156 /// Tracks a view of the network, receiving updates from peers and generating Routes to
157 /// payment destinations.
159 secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
160 network_map: RwLock<NetworkMap>,
161 chain_monitor: Arc<ChainWatchInterface>,
165 macro_rules! secp_verify_sig {
166 ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
167 match $secp_ctx.verify($msg, $sig, $pubkey) {
169 Err(_) => return Err(HandleError{err: "Invalid signature from remote node", action: None}),
174 impl RoutingMessageHandler for Router {
175 fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
176 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
177 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
179 if msg.contents.features.requires_unknown_bits() {
180 panic!("Unknown-required-features NodeAnnouncements should never deserialize!");
183 let mut network = self.network_map.write().unwrap();
184 match network.nodes.get_mut(&msg.contents.node_id) {
185 None => Err(HandleError{err: "No existing channels for node_announcement", action: Some(ErrorAction::IgnoreError)}),
187 if node.last_update >= msg.contents.timestamp {
188 return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
191 node.features = msg.contents.features.clone();
192 node.last_update = msg.contents.timestamp;
193 node.rgb = msg.contents.rgb;
194 node.alias = msg.contents.alias;
195 node.addresses = msg.contents.addresses.clone();
196 Ok(msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty() && !msg.contents.features.supports_unknown_bits())
201 fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
202 if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
203 return Err(HandleError{err: "Channel announcement node had a channel with itself", action: Some(ErrorAction::IgnoreError)});
206 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
207 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
208 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
209 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
210 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
212 if msg.contents.features.requires_unknown_bits() {
213 panic!("Unknown-required-features ChannelAnnouncements should never deserialize!");
216 match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
217 Ok((script_pubkey, _value)) => {
218 let expected_script = Builder::new().push_opcode(opcodes::All::OP_PUSHNUM_2)
219 .push_slice(&msg.contents.bitcoin_key_1.serialize())
220 .push_slice(&msg.contents.bitcoin_key_2.serialize())
221 .push_opcode(opcodes::All::OP_PUSHNUM_2).push_opcode(opcodes::All::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
222 if script_pubkey != expected_script {
223 return Err(HandleError{err: "Channel announcement keys didn't match on-chain script", action: Some(ErrorAction::IgnoreError)});
225 //TODO: Check if value is worth storing, use it to inform routing, and compare it
226 //to the new HTLC max field in channel_update
228 Err(ChainError::NotSupported) => {
229 // Tentatively accept, potentially exposing us to DoS attacks
231 Err(ChainError::NotWatched) => {
232 return Err(HandleError{err: "Channel announced on an unknown chain", action: Some(ErrorAction::IgnoreError)});
234 Err(ChainError::UnknownTx) => {
235 return Err(HandleError{err: "Channel announced without corresponding UTXO entry", action: Some(ErrorAction::IgnoreError)});
239 let mut network = self.network_map.write().unwrap();
241 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
242 Entry::Occupied(_) => {
243 //TODO: because asking the blockchain if short_channel_id is valid is only optional
244 //in the blockchain API, we need to handle it smartly here, though its unclear
246 return Err(HandleError{err: "Already have knowledge of channel", action: Some(ErrorAction::IgnoreError)})
248 Entry::Vacant(entry) => {
249 entry.insert(ChannelInfo {
250 features: msg.contents.features.clone(),
251 one_to_two: DirectionalChannelInfo {
252 src_node_id: msg.contents.node_id_1.clone(),
255 cltv_expiry_delta: u16::max_value(),
256 htlc_minimum_msat: u64::max_value(),
257 fee_base_msat: u32::max_value(),
258 fee_proportional_millionths: u32::max_value(),
260 two_to_one: DirectionalChannelInfo {
261 src_node_id: msg.contents.node_id_2.clone(),
264 cltv_expiry_delta: u16::max_value(),
265 htlc_minimum_msat: u64::max_value(),
266 fee_base_msat: u32::max_value(),
267 fee_proportional_millionths: u32::max_value(),
273 macro_rules! add_channel_to_node {
274 ( $node_id: expr ) => {
275 match network.nodes.entry($node_id) {
276 Entry::Occupied(node_entry) => {
277 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
279 Entry::Vacant(node_entry) => {
280 node_entry.insert(NodeInfo {
281 channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
282 lowest_inbound_channel_fee_base_msat: u32::max_value(),
283 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
284 features: GlobalFeatures::new(),
288 addresses: Vec::new(),
295 add_channel_to_node!(msg.contents.node_id_1);
296 add_channel_to_node!(msg.contents.node_id_2);
298 Ok(msg.contents.excess_data.is_empty() && !msg.contents.features.supports_unknown_bits())
301 fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
303 &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
304 let _ = self.handle_channel_update(msg);
306 &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id } => {
307 let mut network = self.network_map.write().unwrap();
308 if let Some(chan) = network.channels.remove(short_channel_id) {
309 Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
315 fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
316 let mut network = self.network_map.write().unwrap();
318 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
319 let chan_was_enabled;
321 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
322 None => return Err(HandleError{err: "Couldn't find channel for update", action: Some(ErrorAction::IgnoreError)}),
324 macro_rules! maybe_update_channel_info {
325 ( $target: expr) => {
326 if $target.last_update >= msg.contents.timestamp {
327 return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
329 chan_was_enabled = $target.enabled;
330 $target.last_update = msg.contents.timestamp;
331 $target.enabled = chan_enabled;
332 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
333 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
334 $target.fee_base_msat = msg.contents.fee_base_msat;
335 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
339 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
340 if msg.contents.flags & 1 == 1 {
341 dest_node_id = channel.one_to_two.src_node_id.clone();
342 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
343 maybe_update_channel_info!(channel.two_to_one);
345 dest_node_id = channel.two_to_one.src_node_id.clone();
346 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
347 maybe_update_channel_info!(channel.one_to_two);
353 let node = network.nodes.get_mut(&dest_node_id).unwrap();
354 node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
355 node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
356 } else if chan_was_enabled {
357 let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
358 let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
361 let node = network.nodes.get(&dest_node_id).unwrap();
363 for chan_id in node.channels.iter() {
364 let chan = network.channels.get(chan_id).unwrap();
365 if chan.one_to_two.src_node_id == dest_node_id {
366 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
367 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
369 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
370 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
375 //TODO: satisfy the borrow-checker without a double-map-lookup :(
376 let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
377 mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
378 mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
381 Ok(msg.contents.excess_data.is_empty())
385 #[derive(Eq, PartialEq)]
386 struct RouteGraphNode {
388 lowest_fee_to_peer_through_node: u64,
389 lowest_fee_to_node: u64,
392 impl cmp::Ord for RouteGraphNode {
393 fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
394 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
395 .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
399 impl cmp::PartialOrd for RouteGraphNode {
400 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
401 Some(self.cmp(other))
405 struct DummyDirectionalChannelInfo {
406 src_node_id: PublicKey,
407 cltv_expiry_delta: u32,
408 htlc_minimum_msat: u64,
410 fee_proportional_millionths: u32,
414 pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
415 let mut nodes = HashMap::new();
416 nodes.insert(our_pubkey.clone(), NodeInfo {
417 channels: Vec::new(),
418 lowest_inbound_channel_fee_base_msat: u32::max_value(),
419 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
420 features: GlobalFeatures::new(),
424 addresses: Vec::new(),
427 secp_ctx: Secp256k1::verification_only(),
428 network_map: RwLock::new(NetworkMap {
429 channels: HashMap::new(),
430 our_node_id: our_pubkey,
438 /// Dumps the entire network view of this Router to the logger provided in the constructor at
440 pub fn trace_state(&self) {
441 log_trace!(self, "{}", self.network_map.read().unwrap());
444 /// Get network addresses by node id
445 pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
446 let network = self.network_map.read().unwrap();
447 network.nodes.get(pubkey).map(|n| n.addresses.clone())
450 /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
451 /// with an expotnential decay in node "badness". Note that there is deliberately no
452 /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
453 /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
454 /// boolean will reduce the penalty, returning the node to usability faster. If the node is
455 /// behaving correctly, it will disable the failing channel and we will use it again next time.
456 pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
460 fn remove_channel_in_nodes(nodes: &mut HashMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
461 macro_rules! remove_from_node {
462 ($node_id: expr) => {
463 if let Entry::Occupied(mut entry) = nodes.entry($node_id) {
464 entry.get_mut().channels.retain(|chan_id| {
465 short_channel_id != *NetworkMap::get_short_id(chan_id)
467 if entry.get().channels.is_empty() {
468 entry.remove_entry();
471 panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
475 remove_from_node!(chan.one_to_two.src_node_id);
476 remove_from_node!(chan.two_to_one.src_node_id);
479 /// Gets a route from us to the given target node.
480 /// Extra routing hops between known nodes and the target will be used if they are included in
482 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
483 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
484 /// (this Router's) view of our local channels will be ignored, and only those in first_hops
485 /// will be used. Panics if first_hops contains channels without short_channel_ids
486 /// (ChannelManager::list_usable_channels will never include such channels).
487 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
488 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
489 /// *is* checked as they may change based on the receiving node.
490 pub fn get_route(&self, target: &PublicKey, first_hops: Option<&[channelmanager::ChannelDetails]>, last_hops: &[RouteHint], final_value_msat: u64, final_cltv: u32) -> Result<Route, HandleError> {
491 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
492 // uptime/success in using a node in the past.
493 let network = self.network_map.read().unwrap();
495 if *target == network.our_node_id {
496 return Err(HandleError{err: "Cannot generate a route to ourselves", action: None});
499 if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
500 return Err(HandleError{err: "Cannot generate a route of more value than all existing satoshis", action: None});
503 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
504 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
505 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
506 // to use as the A* heuristic beyond just the cost to get one node further than the current
509 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
510 src_node_id: network.our_node_id.clone(),
511 cltv_expiry_delta: 0,
512 htlc_minimum_msat: 0,
514 fee_proportional_millionths: 0,
517 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
518 let mut dist = HashMap::with_capacity(network.nodes.len());
520 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
521 if let Some(hops) = first_hops {
523 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
524 if chan.remote_network_id == *target {
526 hops: vec![RouteHop {
527 pubkey: chan.remote_network_id,
529 fee_msat: final_value_msat,
530 cltv_expiry_delta: final_cltv,
534 first_hop_targets.insert(chan.remote_network_id, short_channel_id);
536 if first_hop_targets.is_empty() {
537 return Err(HandleError{err: "Cannot route when there are no outbound routes away from us", action: None});
541 macro_rules! add_entry {
542 // Adds entry which goes from the node pointed to by $directional_info to
543 // $dest_node_id over the channel with id $chan_id with fees described in
544 // $directional_info.
545 ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $starting_fee_msat: expr ) => {
546 //TODO: Explore simply adding fee to hit htlc_minimum_msat
547 if $starting_fee_msat as u64 + final_value_msat > $directional_info.htlc_minimum_msat {
548 let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
549 if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
550 ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
552 let mut total_fee = $starting_fee_msat as u64;
553 let hm_entry = dist.entry(&$directional_info.src_node_id);
554 let old_entry = hm_entry.or_insert_with(|| {
555 let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
557 node.lowest_inbound_channel_fee_base_msat,
558 node.lowest_inbound_channel_fee_proportional_millionths,
560 pubkey: $dest_node_id.clone(),
563 cltv_expiry_delta: 0,
566 if $directional_info.src_node_id != network.our_node_id {
567 // Ignore new_fee for channel-from-us as we assume all channels-from-us
568 // will have the same effective-fee
569 total_fee += new_fee;
570 if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
571 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
573 // max_value means we'll always fail the old_entry.0 > total_fee check
574 total_fee = u64::max_value();
577 let new_graph_node = RouteGraphNode {
578 pubkey: $directional_info.src_node_id,
579 lowest_fee_to_peer_through_node: total_fee,
580 lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
582 if old_entry.0 > total_fee {
583 targets.push(new_graph_node);
584 old_entry.0 = total_fee;
585 old_entry.3 = RouteHop {
586 pubkey: $dest_node_id.clone(),
587 short_channel_id: $chan_id.clone(),
588 fee_msat: new_fee, // This field is ignored on the last-hop anyway
589 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
597 macro_rules! add_entries_to_cheapest_to_target_node {
598 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
599 if first_hops.is_some() {
600 if let Some(first_hop) = first_hop_targets.get(&$node_id) {
601 add_entry!(first_hop, $node_id, dummy_directional_info, $fee_to_target_msat);
605 for chan_id in $node.channels.iter() {
606 let chan = network.channels.get(chan_id).unwrap();
607 if chan.one_to_two.src_node_id == *$node_id {
608 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
609 if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
610 if chan.two_to_one.enabled {
611 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
615 if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
616 if chan.one_to_two.enabled {
617 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
625 match network.nodes.get(target) {
628 add_entries_to_cheapest_to_target_node!(node, target, 0);
632 for hop in last_hops.iter() {
633 if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
634 if network.nodes.get(&hop.src_node_id).is_some() {
635 if first_hops.is_some() {
636 if let Some(first_hop) = first_hop_targets.get(&hop.src_node_id) {
637 add_entry!(first_hop, hop.src_node_id, dummy_directional_info, 0);
640 add_entry!(hop.short_channel_id, target, hop, 0);
645 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
646 if pubkey == network.our_node_id {
647 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
648 while res.last().unwrap().pubkey != *target {
649 let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
651 None => return Err(HandleError{err: "Failed to find a non-fee-overflowing path to the given destination", action: None}),
653 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
654 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
657 res.last_mut().unwrap().fee_msat = final_value_msat;
658 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
659 let route = Route { hops: res };
660 log_trace!(self, "Got route: {}", log_route!(route));
664 match network.nodes.get(&pubkey) {
667 add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
672 Err(HandleError{err: "Failed to find a path to the given destination", action: None})
678 use chain::chaininterface;
679 use ln::channelmanager;
680 use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
681 use ln::msgs::GlobalFeatures;
682 use util::test_utils;
683 use util::logger::Logger;
685 use bitcoin::util::hash::Sha256dHash;
686 use bitcoin::network::constants::Network;
690 use secp256k1::key::{PublicKey,SecretKey};
691 use secp256k1::Secp256k1;
697 let secp_ctx = Secp256k1::new();
698 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
699 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
700 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
701 let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
703 // Build network from our_id to node8:
705 // -1(1)2- node1 -1(3)2-
707 // our_id -1(12)2- node8 -1(13)2--- node3
709 // -1(2)2- node2 -1(4)2-
712 // chan1 1-to-2: disabled
713 // chan1 2-to-1: enabled, 0 fee
715 // chan2 1-to-2: enabled, ignored fee
716 // chan2 2-to-1: enabled, 0 fee
718 // chan3 1-to-2: enabled, 0 fee
719 // chan3 2-to-1: enabled, 100 msat fee
721 // chan4 1-to-2: enabled, 100% fee
722 // chan4 2-to-1: enabled, 0 fee
724 // chan12 1-to-2: enabled, ignored fee
725 // chan12 2-to-1: enabled, 0 fee
727 // chan13 1-to-2: enabled, 200% fee
728 // chan13 2-to-1: enabled, 0 fee
731 // -1(5)2- node4 -1(8)2--
735 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
737 // -1(7)2- node6 -1(10)2-
739 // chan5 1-to-2: enabled, 100 msat fee
740 // chan5 2-to-1: enabled, 0 fee
742 // chan6 1-to-2: enabled, 0 fee
743 // chan6 2-to-1: enabled, 0 fee
745 // chan7 1-to-2: enabled, 100% fee
746 // chan7 2-to-1: enabled, 0 fee
748 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
749 // chan8 2-to-1: enabled, 0 fee
751 // chan9 1-to-2: enabled, 1001 msat fee
752 // chan9 2-to-1: enabled, 0 fee
754 // chan10 1-to-2: enabled, 0 fee
755 // chan10 2-to-1: enabled, 0 fee
757 // chan11 1-to-2: enabled, 0 fee
758 // chan11 2-to-1: enabled, 0 fee
760 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
761 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
762 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
763 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
764 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
765 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
766 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
767 let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
769 let zero_hash = Sha256dHash::from_data(&[0; 32]);
772 let mut network = router.network_map.write().unwrap();
774 network.nodes.insert(node1.clone(), NodeInfo {
775 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
776 lowest_inbound_channel_fee_base_msat: 100,
777 lowest_inbound_channel_fee_proportional_millionths: 0,
778 features: GlobalFeatures::new(),
782 addresses: Vec::new(),
784 network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
785 features: GlobalFeatures::new(),
786 one_to_two: DirectionalChannelInfo {
787 src_node_id: our_id.clone(),
790 cltv_expiry_delta: u16::max_value(), // This value should be ignored
791 htlc_minimum_msat: 0,
792 fee_base_msat: u32::max_value(), // This value should be ignored
793 fee_proportional_millionths: u32::max_value(), // This value should be ignored
794 }, two_to_one: DirectionalChannelInfo {
795 src_node_id: node1.clone(),
798 cltv_expiry_delta: 0,
799 htlc_minimum_msat: 0,
801 fee_proportional_millionths: 0,
804 network.nodes.insert(node2.clone(), NodeInfo {
805 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
806 lowest_inbound_channel_fee_base_msat: 0,
807 lowest_inbound_channel_fee_proportional_millionths: 0,
808 features: GlobalFeatures::new(),
812 addresses: Vec::new(),
814 network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
815 features: GlobalFeatures::new(),
816 one_to_two: DirectionalChannelInfo {
817 src_node_id: our_id.clone(),
820 cltv_expiry_delta: u16::max_value(), // This value should be ignored
821 htlc_minimum_msat: 0,
822 fee_base_msat: u32::max_value(), // This value should be ignored
823 fee_proportional_millionths: u32::max_value(), // This value should be ignored
824 }, two_to_one: DirectionalChannelInfo {
825 src_node_id: node2.clone(),
828 cltv_expiry_delta: 0,
829 htlc_minimum_msat: 0,
831 fee_proportional_millionths: 0,
834 network.nodes.insert(node8.clone(), NodeInfo {
835 channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
836 lowest_inbound_channel_fee_base_msat: 0,
837 lowest_inbound_channel_fee_proportional_millionths: 0,
838 features: GlobalFeatures::new(),
842 addresses: Vec::new(),
844 network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
845 features: GlobalFeatures::new(),
846 one_to_two: DirectionalChannelInfo {
847 src_node_id: our_id.clone(),
850 cltv_expiry_delta: u16::max_value(), // This value should be ignored
851 htlc_minimum_msat: 0,
852 fee_base_msat: u32::max_value(), // This value should be ignored
853 fee_proportional_millionths: u32::max_value(), // This value should be ignored
854 }, two_to_one: DirectionalChannelInfo {
855 src_node_id: node8.clone(),
858 cltv_expiry_delta: 0,
859 htlc_minimum_msat: 0,
861 fee_proportional_millionths: 0,
864 network.nodes.insert(node3.clone(), NodeInfo {
866 NetworkMap::get_key(3, zero_hash.clone()),
867 NetworkMap::get_key(4, zero_hash.clone()),
868 NetworkMap::get_key(13, zero_hash.clone()),
869 NetworkMap::get_key(5, zero_hash.clone()),
870 NetworkMap::get_key(6, zero_hash.clone()),
871 NetworkMap::get_key(7, zero_hash.clone())),
872 lowest_inbound_channel_fee_base_msat: 0,
873 lowest_inbound_channel_fee_proportional_millionths: 0,
874 features: GlobalFeatures::new(),
878 addresses: Vec::new(),
880 network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
881 features: GlobalFeatures::new(),
882 one_to_two: DirectionalChannelInfo {
883 src_node_id: node1.clone(),
886 cltv_expiry_delta: (3 << 8) | 1,
887 htlc_minimum_msat: 0,
889 fee_proportional_millionths: 0,
890 }, two_to_one: DirectionalChannelInfo {
891 src_node_id: node3.clone(),
894 cltv_expiry_delta: (3 << 8) | 2,
895 htlc_minimum_msat: 0,
897 fee_proportional_millionths: 0,
900 network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
901 features: GlobalFeatures::new(),
902 one_to_two: DirectionalChannelInfo {
903 src_node_id: node2.clone(),
906 cltv_expiry_delta: (4 << 8) | 1,
907 htlc_minimum_msat: 0,
909 fee_proportional_millionths: 1000000,
910 }, two_to_one: DirectionalChannelInfo {
911 src_node_id: node3.clone(),
914 cltv_expiry_delta: (4 << 8) | 2,
915 htlc_minimum_msat: 0,
917 fee_proportional_millionths: 0,
920 network.channels.insert(NetworkMap::get_key(13, zero_hash.clone()), ChannelInfo {
921 features: GlobalFeatures::new(),
922 one_to_two: DirectionalChannelInfo {
923 src_node_id: node8.clone(),
926 cltv_expiry_delta: (13 << 8) | 1,
927 htlc_minimum_msat: 0,
929 fee_proportional_millionths: 2000000,
930 }, two_to_one: DirectionalChannelInfo {
931 src_node_id: node3.clone(),
934 cltv_expiry_delta: (13 << 8) | 2,
935 htlc_minimum_msat: 0,
937 fee_proportional_millionths: 0,
940 network.nodes.insert(node4.clone(), NodeInfo {
941 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
942 lowest_inbound_channel_fee_base_msat: 0,
943 lowest_inbound_channel_fee_proportional_millionths: 0,
944 features: GlobalFeatures::new(),
948 addresses: Vec::new(),
950 network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
951 features: GlobalFeatures::new(),
952 one_to_two: DirectionalChannelInfo {
953 src_node_id: node3.clone(),
956 cltv_expiry_delta: (5 << 8) | 1,
957 htlc_minimum_msat: 0,
959 fee_proportional_millionths: 0,
960 }, two_to_one: DirectionalChannelInfo {
961 src_node_id: node4.clone(),
964 cltv_expiry_delta: (5 << 8) | 2,
965 htlc_minimum_msat: 0,
967 fee_proportional_millionths: 0,
970 network.nodes.insert(node5.clone(), NodeInfo {
971 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
972 lowest_inbound_channel_fee_base_msat: 0,
973 lowest_inbound_channel_fee_proportional_millionths: 0,
974 features: GlobalFeatures::new(),
978 addresses: Vec::new(),
980 network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
981 features: GlobalFeatures::new(),
982 one_to_two: DirectionalChannelInfo {
983 src_node_id: node3.clone(),
986 cltv_expiry_delta: (6 << 8) | 1,
987 htlc_minimum_msat: 0,
989 fee_proportional_millionths: 0,
990 }, two_to_one: DirectionalChannelInfo {
991 src_node_id: node5.clone(),
994 cltv_expiry_delta: (6 << 8) | 2,
995 htlc_minimum_msat: 0,
997 fee_proportional_millionths: 0,
1000 network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
1001 features: GlobalFeatures::new(),
1002 one_to_two: DirectionalChannelInfo {
1003 src_node_id: node5.clone(),
1006 cltv_expiry_delta: (11 << 8) | 1,
1007 htlc_minimum_msat: 0,
1009 fee_proportional_millionths: 0,
1010 }, two_to_one: DirectionalChannelInfo {
1011 src_node_id: node4.clone(),
1014 cltv_expiry_delta: (11 << 8) | 2,
1015 htlc_minimum_msat: 0,
1017 fee_proportional_millionths: 0,
1020 network.nodes.insert(node6.clone(), NodeInfo {
1021 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
1022 lowest_inbound_channel_fee_base_msat: 0,
1023 lowest_inbound_channel_fee_proportional_millionths: 0,
1024 features: GlobalFeatures::new(),
1028 addresses: Vec::new(),
1030 network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
1031 features: GlobalFeatures::new(),
1032 one_to_two: DirectionalChannelInfo {
1033 src_node_id: node3.clone(),
1036 cltv_expiry_delta: (7 << 8) | 1,
1037 htlc_minimum_msat: 0,
1039 fee_proportional_millionths: 1000000,
1040 }, two_to_one: DirectionalChannelInfo {
1041 src_node_id: node6.clone(),
1044 cltv_expiry_delta: (7 << 8) | 2,
1045 htlc_minimum_msat: 0,
1047 fee_proportional_millionths: 0,
1052 { // Simple route to 3 via 2
1053 let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
1054 assert_eq!(route.hops.len(), 2);
1056 assert_eq!(route.hops[0].pubkey, node2);
1057 assert_eq!(route.hops[0].short_channel_id, 2);
1058 assert_eq!(route.hops[0].fee_msat, 100);
1059 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1061 assert_eq!(route.hops[1].pubkey, node3);
1062 assert_eq!(route.hops[1].short_channel_id, 4);
1063 assert_eq!(route.hops[1].fee_msat, 100);
1064 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1067 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
1068 let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
1069 assert_eq!(route.hops.len(), 3);
1071 assert_eq!(route.hops[0].pubkey, node2);
1072 assert_eq!(route.hops[0].short_channel_id, 2);
1073 assert_eq!(route.hops[0].fee_msat, 200);
1074 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1076 assert_eq!(route.hops[1].pubkey, node3);
1077 assert_eq!(route.hops[1].short_channel_id, 4);
1078 assert_eq!(route.hops[1].fee_msat, 100);
1079 assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
1081 assert_eq!(route.hops[2].pubkey, node1);
1082 assert_eq!(route.hops[2].short_channel_id, 3);
1083 assert_eq!(route.hops[2].fee_msat, 100);
1084 assert_eq!(route.hops[2].cltv_expiry_delta, 42);
1087 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1088 let our_chans = vec![channelmanager::ChannelDetails {
1089 channel_id: [0; 32],
1090 short_channel_id: Some(42),
1091 remote_network_id: node8.clone(),
1092 channel_value_satoshis: 0,
1095 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1096 assert_eq!(route.hops.len(), 2);
1098 assert_eq!(route.hops[0].pubkey, node8);
1099 assert_eq!(route.hops[0].short_channel_id, 42);
1100 assert_eq!(route.hops[0].fee_msat, 200);
1101 assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1103 assert_eq!(route.hops[1].pubkey, node3);
1104 assert_eq!(route.hops[1].short_channel_id, 13);
1105 assert_eq!(route.hops[1].fee_msat, 100);
1106 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1109 let mut last_hops = vec!(RouteHint {
1110 src_node_id: node4.clone(),
1111 short_channel_id: 8,
1113 fee_proportional_millionths: 0,
1114 cltv_expiry_delta: (8 << 8) | 1,
1115 htlc_minimum_msat: 0,
1117 src_node_id: node5.clone(),
1118 short_channel_id: 9,
1119 fee_base_msat: 1001,
1120 fee_proportional_millionths: 0,
1121 cltv_expiry_delta: (9 << 8) | 1,
1122 htlc_minimum_msat: 0,
1124 src_node_id: node6.clone(),
1125 short_channel_id: 10,
1127 fee_proportional_millionths: 0,
1128 cltv_expiry_delta: (10 << 8) | 1,
1129 htlc_minimum_msat: 0,
1132 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
1133 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1134 assert_eq!(route.hops.len(), 5);
1136 assert_eq!(route.hops[0].pubkey, node2);
1137 assert_eq!(route.hops[0].short_channel_id, 2);
1138 assert_eq!(route.hops[0].fee_msat, 100);
1139 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1141 assert_eq!(route.hops[1].pubkey, node3);
1142 assert_eq!(route.hops[1].short_channel_id, 4);
1143 assert_eq!(route.hops[1].fee_msat, 0);
1144 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1146 assert_eq!(route.hops[2].pubkey, node5);
1147 assert_eq!(route.hops[2].short_channel_id, 6);
1148 assert_eq!(route.hops[2].fee_msat, 0);
1149 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1151 assert_eq!(route.hops[3].pubkey, node4);
1152 assert_eq!(route.hops[3].short_channel_id, 11);
1153 assert_eq!(route.hops[3].fee_msat, 0);
1154 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1156 assert_eq!(route.hops[4].pubkey, node7);
1157 assert_eq!(route.hops[4].short_channel_id, 8);
1158 assert_eq!(route.hops[4].fee_msat, 100);
1159 assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1162 { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1163 let our_chans = vec![channelmanager::ChannelDetails {
1164 channel_id: [0; 32],
1165 short_channel_id: Some(42),
1166 remote_network_id: node4.clone(),
1167 channel_value_satoshis: 0,
1170 let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
1171 assert_eq!(route.hops.len(), 2);
1173 assert_eq!(route.hops[0].pubkey, node4);
1174 assert_eq!(route.hops[0].short_channel_id, 42);
1175 assert_eq!(route.hops[0].fee_msat, 0);
1176 assert_eq!(route.hops[0].cltv_expiry_delta, (8 << 8) | 1);
1178 assert_eq!(route.hops[1].pubkey, node7);
1179 assert_eq!(route.hops[1].short_channel_id, 8);
1180 assert_eq!(route.hops[1].fee_msat, 100);
1181 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1184 last_hops[0].fee_base_msat = 1000;
1186 { // Revert to via 6 as the fee on 8 goes up
1187 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1188 assert_eq!(route.hops.len(), 4);
1190 assert_eq!(route.hops[0].pubkey, node2);
1191 assert_eq!(route.hops[0].short_channel_id, 2);
1192 assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
1193 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1195 assert_eq!(route.hops[1].pubkey, node3);
1196 assert_eq!(route.hops[1].short_channel_id, 4);
1197 assert_eq!(route.hops[1].fee_msat, 100);
1198 assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
1200 assert_eq!(route.hops[2].pubkey, node6);
1201 assert_eq!(route.hops[2].short_channel_id, 7);
1202 assert_eq!(route.hops[2].fee_msat, 0);
1203 assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
1205 assert_eq!(route.hops[3].pubkey, node7);
1206 assert_eq!(route.hops[3].short_channel_id, 10);
1207 assert_eq!(route.hops[3].fee_msat, 100);
1208 assert_eq!(route.hops[3].cltv_expiry_delta, 42);
1211 { // ...but still use 8 for larger payments as 6 has a variable feerate
1212 let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
1213 assert_eq!(route.hops.len(), 5);
1215 assert_eq!(route.hops[0].pubkey, node2);
1216 assert_eq!(route.hops[0].short_channel_id, 2);
1217 assert_eq!(route.hops[0].fee_msat, 3000);
1218 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1220 assert_eq!(route.hops[1].pubkey, node3);
1221 assert_eq!(route.hops[1].short_channel_id, 4);
1222 assert_eq!(route.hops[1].fee_msat, 0);
1223 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1225 assert_eq!(route.hops[2].pubkey, node5);
1226 assert_eq!(route.hops[2].short_channel_id, 6);
1227 assert_eq!(route.hops[2].fee_msat, 0);
1228 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1230 assert_eq!(route.hops[3].pubkey, node4);
1231 assert_eq!(route.hops[3].short_channel_id, 11);
1232 assert_eq!(route.hops[3].fee_msat, 1000);
1233 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1235 assert_eq!(route.hops[4].pubkey, node7);
1236 assert_eq!(route.hops[4].short_channel_id, 8);
1237 assert_eq!(route.hops[4].fee_msat, 2000);
1238 assert_eq!(route.hops[4].cltv_expiry_delta, 42);