1 //! The top-level routing/network map tracking logic lives here.
3 //! You probably want to create a Router and use that as your RoutingMessageHandler and then
4 //! interrogate it to get routes for your own payments.
6 use secp256k1::key::PublicKey;
7 use secp256k1::{Secp256k1,Message};
10 use bitcoin::util::hash::Sha256dHash;
11 use bitcoin::blockdata::script::Builder;
12 use bitcoin::blockdata::opcodes;
14 use chain::chaininterface::{ChainError, ChainWatchInterface};
15 use ln::channelmanager;
16 use ln::msgs::{ErrorAction,HandleError,RoutingMessageHandler,NetAddress,GlobalFeatures};
18 use util::ser::Writeable;
19 use util::logger::Logger;
22 use std::sync::{RwLock,Arc};
23 use std::collections::{HashMap,BinaryHeap};
24 use std::collections::hash_map::Entry;
30 /// The node_id of the node at this hop.
31 pub pubkey: PublicKey,
32 /// The channel that should be used from the previous hop to reach this node.
33 pub short_channel_id: u64,
34 /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
36 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
37 /// expected at the destination, in excess of the current block height.
38 pub cltv_expiry_delta: u32,
41 /// A route from us through the network to a destination
44 /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
45 /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
47 pub hops: Vec<RouteHop>,
50 struct DirectionalChannelInfo {
51 src_node_id: PublicKey,
54 cltv_expiry_delta: u16,
55 htlc_minimum_msat: u64,
57 fee_proportional_millionths: u32,
60 impl std::fmt::Display for DirectionalChannelInfo {
61 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
62 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)?;
68 features: GlobalFeatures,
69 one_to_two: DirectionalChannelInfo,
70 two_to_one: DirectionalChannelInfo,
73 impl std::fmt::Display for ChannelInfo {
74 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
75 write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?;
81 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
82 channels: Vec<(u64, Sha256dHash)>,
83 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
86 lowest_inbound_channel_fee_base_msat: u32,
87 lowest_inbound_channel_fee_proportional_millionths: u32,
89 features: GlobalFeatures,
93 addresses: Vec<NetAddress>,
96 impl std::fmt::Display for NodeInfo {
97 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
98 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[..])?;
104 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
105 channels: HashMap<(u64, Sha256dHash), ChannelInfo>,
106 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
107 channels: HashMap<u64, ChannelInfo>,
109 our_node_id: PublicKey,
110 nodes: HashMap<PublicKey, NodeInfo>,
112 struct MutNetworkMap<'a> {
113 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
114 channels: &'a mut HashMap<(u64, Sha256dHash), ChannelInfo>,
115 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
116 channels: &'a mut HashMap<u64, ChannelInfo>,
117 nodes: &'a mut HashMap<PublicKey, NodeInfo>,
120 fn borrow_parts(&mut self) -> MutNetworkMap {
122 channels: &mut self.channels,
123 nodes: &mut self.nodes,
127 impl std::fmt::Display for NetworkMap {
128 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
129 write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
130 for (key, val) in self.channels.iter() {
131 write!(f, " {}: {}\n", key, val)?;
133 write!(f, "[Nodes]\n")?;
134 for (key, val) in self.nodes.iter() {
135 write!(f, " {}: {}\n", log_pubkey!(key), val)?;
142 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
144 fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
145 (short_channel_id, chain_hash)
148 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
150 fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
154 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
156 fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
160 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
162 fn get_short_id(id: &u64) -> &u64 {
167 /// A channel descriptor which provides a last-hop route to get_route
168 pub struct RouteHint {
169 /// The node_id of the non-target end of the route
170 pub src_node_id: PublicKey,
171 /// The short_channel_id of this channel
172 pub short_channel_id: u64,
173 /// The static msat-denominated fee which must be paid to use this channel
174 pub fee_base_msat: u32,
175 /// The dynamic proportional fee which must be paid to use this channel, denominated in
176 /// millionths of the value being forwarded to the next hop.
177 pub fee_proportional_millionths: u32,
178 /// The difference in CLTV values between this node and the next node.
179 pub cltv_expiry_delta: u16,
180 /// The minimum value, in msat, which must be relayed to the next hop.
181 pub htlc_minimum_msat: u64,
184 /// Tracks a view of the network, receiving updates from peers and generating Routes to
185 /// payment destinations.
187 secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
188 network_map: RwLock<NetworkMap>,
189 chain_monitor: Arc<ChainWatchInterface>,
193 macro_rules! secp_verify_sig {
194 ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
195 match $secp_ctx.verify($msg, $sig, $pubkey) {
197 Err(_) => return Err(HandleError{err: "Invalid signature from remote node", action: None}),
202 impl RoutingMessageHandler for Router {
203 fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
204 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
205 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
207 if msg.contents.features.requires_unknown_bits() {
208 panic!("Unknown-required-features NodeAnnouncements should never deserialize!");
211 let mut network = self.network_map.write().unwrap();
212 match network.nodes.get_mut(&msg.contents.node_id) {
213 None => Err(HandleError{err: "No existing channels for node_announcement", action: Some(ErrorAction::IgnoreError)}),
215 if node.last_update >= msg.contents.timestamp {
216 return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
219 node.features = msg.contents.features.clone();
220 node.last_update = msg.contents.timestamp;
221 node.rgb = msg.contents.rgb;
222 node.alias = msg.contents.alias;
223 node.addresses = msg.contents.addresses.clone();
224 Ok(msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty() && !msg.contents.features.supports_unknown_bits())
229 fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
230 if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
231 return Err(HandleError{err: "Channel announcement node had a channel with itself", action: Some(ErrorAction::IgnoreError)});
234 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
235 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
236 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
237 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
238 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
240 if msg.contents.features.requires_unknown_bits() {
241 panic!("Unknown-required-features ChannelAnnouncements should never deserialize!");
244 let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
245 Ok((script_pubkey, _value)) => {
246 let expected_script = Builder::new().push_opcode(opcodes::All::OP_PUSHNUM_2)
247 .push_slice(&msg.contents.bitcoin_key_1.serialize())
248 .push_slice(&msg.contents.bitcoin_key_2.serialize())
249 .push_opcode(opcodes::All::OP_PUSHNUM_2).push_opcode(opcodes::All::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
250 if script_pubkey != expected_script {
251 return Err(HandleError{err: "Channel announcement keys didn't match on-chain script", action: Some(ErrorAction::IgnoreError)});
253 //TODO: Check if value is worth storing, use it to inform routing, and compare it
254 //to the new HTLC max field in channel_update
257 Err(ChainError::NotSupported) => {
258 // Tentatively accept, potentially exposing us to DoS attacks
261 Err(ChainError::NotWatched) => {
262 return Err(HandleError{err: "Channel announced on an unknown chain", action: Some(ErrorAction::IgnoreError)});
264 Err(ChainError::UnknownTx) => {
265 return Err(HandleError{err: "Channel announced without corresponding UTXO entry", action: Some(ErrorAction::IgnoreError)});
269 let mut network_lock = self.network_map.write().unwrap();
270 let network = network_lock.borrow_parts();
272 let chan_info = ChannelInfo {
273 features: msg.contents.features.clone(),
274 one_to_two: DirectionalChannelInfo {
275 src_node_id: msg.contents.node_id_1.clone(),
278 cltv_expiry_delta: u16::max_value(),
279 htlc_minimum_msat: u64::max_value(),
280 fee_base_msat: u32::max_value(),
281 fee_proportional_millionths: u32::max_value(),
283 two_to_one: DirectionalChannelInfo {
284 src_node_id: msg.contents.node_id_2.clone(),
287 cltv_expiry_delta: u16::max_value(),
288 htlc_minimum_msat: u64::max_value(),
289 fee_base_msat: u32::max_value(),
290 fee_proportional_millionths: u32::max_value(),
294 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
295 Entry::Occupied(mut entry) => {
296 //TODO: because asking the blockchain if short_channel_id is valid is only optional
297 //in the blockchain API, we need to handle it smartly here, though its unclear
300 // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
301 // only sometimes returns results. In any case remove the previous entry. Note
302 // that the spec expects us to "blacklist" the node_ids involved, but we can't
304 // a) we don't *require* a UTXO provider that always returns results.
305 // b) we don't track UTXOs of channels we know about and remove them if they
307 // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
308 Self::remove_channel_in_nodes(network.nodes, &entry.get(), msg.contents.short_channel_id);
309 *entry.get_mut() = chan_info;
311 return Err(HandleError{err: "Already have knowledge of channel", action: Some(ErrorAction::IgnoreError)})
314 Entry::Vacant(entry) => {
315 entry.insert(chan_info);
319 macro_rules! add_channel_to_node {
320 ( $node_id: expr ) => {
321 match network.nodes.entry($node_id) {
322 Entry::Occupied(node_entry) => {
323 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
325 Entry::Vacant(node_entry) => {
326 node_entry.insert(NodeInfo {
327 channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
328 lowest_inbound_channel_fee_base_msat: u32::max_value(),
329 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
330 features: GlobalFeatures::new(),
334 addresses: Vec::new(),
341 add_channel_to_node!(msg.contents.node_id_1);
342 add_channel_to_node!(msg.contents.node_id_2);
344 Ok(msg.contents.excess_data.is_empty() && !msg.contents.features.supports_unknown_bits())
347 fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
349 &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
350 let _ = self.handle_channel_update(msg);
352 &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
353 let mut network = self.network_map.write().unwrap();
355 if let Some(chan) = network.channels.remove(short_channel_id) {
356 Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
359 if let Some(chan) = network.channels.get_mut(short_channel_id) {
360 chan.one_to_two.enabled = false;
361 chan.two_to_one.enabled = false;
365 &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
367 //TODO: Wholly remove the node
369 self.mark_node_bad(node_id, false);
375 fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
376 let mut network = self.network_map.write().unwrap();
378 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
379 let chan_was_enabled;
381 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
382 None => return Err(HandleError{err: "Couldn't find channel for update", action: Some(ErrorAction::IgnoreError)}),
384 macro_rules! maybe_update_channel_info {
385 ( $target: expr) => {
386 if $target.last_update >= msg.contents.timestamp {
387 return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
389 chan_was_enabled = $target.enabled;
390 $target.last_update = msg.contents.timestamp;
391 $target.enabled = chan_enabled;
392 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
393 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
394 $target.fee_base_msat = msg.contents.fee_base_msat;
395 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
399 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
400 if msg.contents.flags & 1 == 1 {
401 dest_node_id = channel.one_to_two.src_node_id.clone();
402 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
403 maybe_update_channel_info!(channel.two_to_one);
405 dest_node_id = channel.two_to_one.src_node_id.clone();
406 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
407 maybe_update_channel_info!(channel.one_to_two);
413 let node = network.nodes.get_mut(&dest_node_id).unwrap();
414 node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
415 node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
416 } else if chan_was_enabled {
417 let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
418 let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
421 let node = network.nodes.get(&dest_node_id).unwrap();
423 for chan_id in node.channels.iter() {
424 let chan = network.channels.get(chan_id).unwrap();
425 if chan.one_to_two.src_node_id == dest_node_id {
426 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
427 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
429 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
430 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
435 //TODO: satisfy the borrow-checker without a double-map-lookup :(
436 let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
437 mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
438 mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
441 Ok(msg.contents.excess_data.is_empty())
445 #[derive(Eq, PartialEq)]
446 struct RouteGraphNode {
448 lowest_fee_to_peer_through_node: u64,
449 lowest_fee_to_node: u64,
452 impl cmp::Ord for RouteGraphNode {
453 fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
454 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
455 .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
459 impl cmp::PartialOrd for RouteGraphNode {
460 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
461 Some(self.cmp(other))
465 struct DummyDirectionalChannelInfo {
466 src_node_id: PublicKey,
467 cltv_expiry_delta: u32,
468 htlc_minimum_msat: u64,
470 fee_proportional_millionths: u32,
474 /// Creates a new router with the given node_id to be used as the source for get_route()
475 pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
476 let mut nodes = HashMap::new();
477 nodes.insert(our_pubkey.clone(), NodeInfo {
478 channels: Vec::new(),
479 lowest_inbound_channel_fee_base_msat: u32::max_value(),
480 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
481 features: GlobalFeatures::new(),
485 addresses: Vec::new(),
488 secp_ctx: Secp256k1::verification_only(),
489 network_map: RwLock::new(NetworkMap {
490 channels: HashMap::new(),
491 our_node_id: our_pubkey,
499 /// Dumps the entire network view of this Router to the logger provided in the constructor at
501 pub fn trace_state(&self) {
502 log_trace!(self, "{}", self.network_map.read().unwrap());
505 /// Get network addresses by node id
506 pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
507 let network = self.network_map.read().unwrap();
508 network.nodes.get(pubkey).map(|n| n.addresses.clone())
511 /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
512 /// with an expotnential decay in node "badness". Note that there is deliberately no
513 /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
514 /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
515 /// boolean will reduce the penalty, returning the node to usability faster. If the node is
516 /// behaving correctly, it will disable the failing channel and we will use it again next time.
517 pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
521 fn remove_channel_in_nodes(nodes: &mut HashMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
522 macro_rules! remove_from_node {
523 ($node_id: expr) => {
524 if let Entry::Occupied(mut entry) = nodes.entry($node_id) {
525 entry.get_mut().channels.retain(|chan_id| {
526 short_channel_id != *NetworkMap::get_short_id(chan_id)
528 if entry.get().channels.is_empty() {
529 entry.remove_entry();
532 panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
536 remove_from_node!(chan.one_to_two.src_node_id);
537 remove_from_node!(chan.two_to_one.src_node_id);
540 /// Gets a route from us to the given target node.
542 /// Extra routing hops between known nodes and the target will be used if they are included in
545 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
546 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
547 /// (this Router's) view of our local channels will be ignored, and only those in first_hops
550 /// Panics if first_hops contains channels without short_channel_ids
551 /// (ChannelManager::list_usable_channels will never include such channels).
553 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
554 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
555 /// *is* checked as they may change based on the receiving node.
556 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> {
557 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
558 // uptime/success in using a node in the past.
559 let network = self.network_map.read().unwrap();
561 if *target == network.our_node_id {
562 return Err(HandleError{err: "Cannot generate a route to ourselves", action: None});
565 if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
566 return Err(HandleError{err: "Cannot generate a route of more value than all existing satoshis", action: None});
569 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
570 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
571 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
572 // to use as the A* heuristic beyond just the cost to get one node further than the current
575 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
576 src_node_id: network.our_node_id.clone(),
577 cltv_expiry_delta: 0,
578 htlc_minimum_msat: 0,
580 fee_proportional_millionths: 0,
583 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
584 let mut dist = HashMap::with_capacity(network.nodes.len());
586 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
587 if let Some(hops) = first_hops {
589 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
590 if chan.remote_network_id == *target {
592 hops: vec![RouteHop {
593 pubkey: chan.remote_network_id,
595 fee_msat: final_value_msat,
596 cltv_expiry_delta: final_cltv,
600 first_hop_targets.insert(chan.remote_network_id, short_channel_id);
602 if first_hop_targets.is_empty() {
603 return Err(HandleError{err: "Cannot route when there are no outbound routes away from us", action: None});
607 macro_rules! add_entry {
608 // Adds entry which goes from the node pointed to by $directional_info to
609 // $dest_node_id over the channel with id $chan_id with fees described in
610 // $directional_info.
611 ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $starting_fee_msat: expr ) => {
612 //TODO: Explore simply adding fee to hit htlc_minimum_msat
613 if $starting_fee_msat as u64 + final_value_msat > $directional_info.htlc_minimum_msat {
614 let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
615 if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
616 ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
618 let mut total_fee = $starting_fee_msat as u64;
619 let hm_entry = dist.entry(&$directional_info.src_node_id);
620 let old_entry = hm_entry.or_insert_with(|| {
621 let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
623 node.lowest_inbound_channel_fee_base_msat,
624 node.lowest_inbound_channel_fee_proportional_millionths,
626 pubkey: $dest_node_id.clone(),
629 cltv_expiry_delta: 0,
632 if $directional_info.src_node_id != network.our_node_id {
633 // Ignore new_fee for channel-from-us as we assume all channels-from-us
634 // will have the same effective-fee
635 total_fee += new_fee;
636 if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
637 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
639 // max_value means we'll always fail the old_entry.0 > total_fee check
640 total_fee = u64::max_value();
643 let new_graph_node = RouteGraphNode {
644 pubkey: $directional_info.src_node_id,
645 lowest_fee_to_peer_through_node: total_fee,
646 lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
648 if old_entry.0 > total_fee {
649 targets.push(new_graph_node);
650 old_entry.0 = total_fee;
651 old_entry.3 = RouteHop {
652 pubkey: $dest_node_id.clone(),
653 short_channel_id: $chan_id.clone(),
654 fee_msat: new_fee, // This field is ignored on the last-hop anyway
655 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
663 macro_rules! add_entries_to_cheapest_to_target_node {
664 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
665 if first_hops.is_some() {
666 if let Some(first_hop) = first_hop_targets.get(&$node_id) {
667 add_entry!(first_hop, $node_id, dummy_directional_info, $fee_to_target_msat);
671 for chan_id in $node.channels.iter() {
672 let chan = network.channels.get(chan_id).unwrap();
673 if chan.one_to_two.src_node_id == *$node_id {
674 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
675 if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
676 if chan.two_to_one.enabled {
677 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
681 if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
682 if chan.one_to_two.enabled {
683 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
691 match network.nodes.get(target) {
694 add_entries_to_cheapest_to_target_node!(node, target, 0);
698 for hop in last_hops.iter() {
699 if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
700 if network.nodes.get(&hop.src_node_id).is_some() {
701 if first_hops.is_some() {
702 if let Some(first_hop) = first_hop_targets.get(&hop.src_node_id) {
703 add_entry!(first_hop, hop.src_node_id, dummy_directional_info, 0);
706 add_entry!(hop.short_channel_id, target, hop, 0);
711 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
712 if pubkey == network.our_node_id {
713 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
714 while res.last().unwrap().pubkey != *target {
715 let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
717 None => return Err(HandleError{err: "Failed to find a non-fee-overflowing path to the given destination", action: None}),
719 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
720 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
723 res.last_mut().unwrap().fee_msat = final_value_msat;
724 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
725 let route = Route { hops: res };
726 log_trace!(self, "Got route: {}", log_route!(route));
730 match network.nodes.get(&pubkey) {
733 add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
738 Err(HandleError{err: "Failed to find a path to the given destination", action: None})
744 use chain::chaininterface;
745 use ln::channelmanager;
746 use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
747 use ln::msgs::GlobalFeatures;
748 use util::test_utils;
749 use util::logger::Logger;
751 use bitcoin::util::hash::Sha256dHash;
752 use bitcoin::network::constants::Network;
756 use secp256k1::key::{PublicKey,SecretKey};
757 use secp256k1::Secp256k1;
763 let secp_ctx = Secp256k1::new();
764 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
765 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
766 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
767 let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
769 // Build network from our_id to node8:
771 // -1(1)2- node1 -1(3)2-
773 // our_id -1(12)2- node8 -1(13)2--- node3
775 // -1(2)2- node2 -1(4)2-
778 // chan1 1-to-2: disabled
779 // chan1 2-to-1: enabled, 0 fee
781 // chan2 1-to-2: enabled, ignored fee
782 // chan2 2-to-1: enabled, 0 fee
784 // chan3 1-to-2: enabled, 0 fee
785 // chan3 2-to-1: enabled, 100 msat fee
787 // chan4 1-to-2: enabled, 100% fee
788 // chan4 2-to-1: enabled, 0 fee
790 // chan12 1-to-2: enabled, ignored fee
791 // chan12 2-to-1: enabled, 0 fee
793 // chan13 1-to-2: enabled, 200% fee
794 // chan13 2-to-1: enabled, 0 fee
797 // -1(5)2- node4 -1(8)2--
801 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
803 // -1(7)2- node6 -1(10)2-
805 // chan5 1-to-2: enabled, 100 msat fee
806 // chan5 2-to-1: enabled, 0 fee
808 // chan6 1-to-2: enabled, 0 fee
809 // chan6 2-to-1: enabled, 0 fee
811 // chan7 1-to-2: enabled, 100% fee
812 // chan7 2-to-1: enabled, 0 fee
814 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
815 // chan8 2-to-1: enabled, 0 fee
817 // chan9 1-to-2: enabled, 1001 msat fee
818 // chan9 2-to-1: enabled, 0 fee
820 // chan10 1-to-2: enabled, 0 fee
821 // chan10 2-to-1: enabled, 0 fee
823 // chan11 1-to-2: enabled, 0 fee
824 // chan11 2-to-1: enabled, 0 fee
826 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
827 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
828 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
829 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
830 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
831 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
832 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
833 let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
835 let zero_hash = Sha256dHash::from_data(&[0; 32]);
838 let mut network = router.network_map.write().unwrap();
840 network.nodes.insert(node1.clone(), NodeInfo {
841 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
842 lowest_inbound_channel_fee_base_msat: 100,
843 lowest_inbound_channel_fee_proportional_millionths: 0,
844 features: GlobalFeatures::new(),
848 addresses: Vec::new(),
850 network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
851 features: GlobalFeatures::new(),
852 one_to_two: DirectionalChannelInfo {
853 src_node_id: our_id.clone(),
856 cltv_expiry_delta: u16::max_value(), // This value should be ignored
857 htlc_minimum_msat: 0,
858 fee_base_msat: u32::max_value(), // This value should be ignored
859 fee_proportional_millionths: u32::max_value(), // This value should be ignored
860 }, two_to_one: DirectionalChannelInfo {
861 src_node_id: node1.clone(),
864 cltv_expiry_delta: 0,
865 htlc_minimum_msat: 0,
867 fee_proportional_millionths: 0,
870 network.nodes.insert(node2.clone(), NodeInfo {
871 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, 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(2, zero_hash.clone()), ChannelInfo {
881 features: GlobalFeatures::new(),
882 one_to_two: DirectionalChannelInfo {
883 src_node_id: our_id.clone(),
886 cltv_expiry_delta: u16::max_value(), // This value should be ignored
887 htlc_minimum_msat: 0,
888 fee_base_msat: u32::max_value(), // This value should be ignored
889 fee_proportional_millionths: u32::max_value(), // This value should be ignored
890 }, two_to_one: DirectionalChannelInfo {
891 src_node_id: node2.clone(),
894 cltv_expiry_delta: 0,
895 htlc_minimum_msat: 0,
897 fee_proportional_millionths: 0,
900 network.nodes.insert(node8.clone(), NodeInfo {
901 channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
902 lowest_inbound_channel_fee_base_msat: 0,
903 lowest_inbound_channel_fee_proportional_millionths: 0,
904 features: GlobalFeatures::new(),
908 addresses: Vec::new(),
910 network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
911 features: GlobalFeatures::new(),
912 one_to_two: DirectionalChannelInfo {
913 src_node_id: our_id.clone(),
916 cltv_expiry_delta: u16::max_value(), // This value should be ignored
917 htlc_minimum_msat: 0,
918 fee_base_msat: u32::max_value(), // This value should be ignored
919 fee_proportional_millionths: u32::max_value(), // This value should be ignored
920 }, two_to_one: DirectionalChannelInfo {
921 src_node_id: node8.clone(),
924 cltv_expiry_delta: 0,
925 htlc_minimum_msat: 0,
927 fee_proportional_millionths: 0,
930 network.nodes.insert(node3.clone(), NodeInfo {
932 NetworkMap::get_key(3, zero_hash.clone()),
933 NetworkMap::get_key(4, zero_hash.clone()),
934 NetworkMap::get_key(13, zero_hash.clone()),
935 NetworkMap::get_key(5, zero_hash.clone()),
936 NetworkMap::get_key(6, zero_hash.clone()),
937 NetworkMap::get_key(7, zero_hash.clone())),
938 lowest_inbound_channel_fee_base_msat: 0,
939 lowest_inbound_channel_fee_proportional_millionths: 0,
940 features: GlobalFeatures::new(),
944 addresses: Vec::new(),
946 network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
947 features: GlobalFeatures::new(),
948 one_to_two: DirectionalChannelInfo {
949 src_node_id: node1.clone(),
952 cltv_expiry_delta: (3 << 8) | 1,
953 htlc_minimum_msat: 0,
955 fee_proportional_millionths: 0,
956 }, two_to_one: DirectionalChannelInfo {
957 src_node_id: node3.clone(),
960 cltv_expiry_delta: (3 << 8) | 2,
961 htlc_minimum_msat: 0,
963 fee_proportional_millionths: 0,
966 network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
967 features: GlobalFeatures::new(),
968 one_to_two: DirectionalChannelInfo {
969 src_node_id: node2.clone(),
972 cltv_expiry_delta: (4 << 8) | 1,
973 htlc_minimum_msat: 0,
975 fee_proportional_millionths: 1000000,
976 }, two_to_one: DirectionalChannelInfo {
977 src_node_id: node3.clone(),
980 cltv_expiry_delta: (4 << 8) | 2,
981 htlc_minimum_msat: 0,
983 fee_proportional_millionths: 0,
986 network.channels.insert(NetworkMap::get_key(13, zero_hash.clone()), ChannelInfo {
987 features: GlobalFeatures::new(),
988 one_to_two: DirectionalChannelInfo {
989 src_node_id: node8.clone(),
992 cltv_expiry_delta: (13 << 8) | 1,
993 htlc_minimum_msat: 0,
995 fee_proportional_millionths: 2000000,
996 }, two_to_one: DirectionalChannelInfo {
997 src_node_id: node3.clone(),
1000 cltv_expiry_delta: (13 << 8) | 2,
1001 htlc_minimum_msat: 0,
1003 fee_proportional_millionths: 0,
1006 network.nodes.insert(node4.clone(), NodeInfo {
1007 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1008 lowest_inbound_channel_fee_base_msat: 0,
1009 lowest_inbound_channel_fee_proportional_millionths: 0,
1010 features: GlobalFeatures::new(),
1014 addresses: Vec::new(),
1016 network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
1017 features: GlobalFeatures::new(),
1018 one_to_two: DirectionalChannelInfo {
1019 src_node_id: node3.clone(),
1022 cltv_expiry_delta: (5 << 8) | 1,
1023 htlc_minimum_msat: 0,
1025 fee_proportional_millionths: 0,
1026 }, two_to_one: DirectionalChannelInfo {
1027 src_node_id: node4.clone(),
1030 cltv_expiry_delta: (5 << 8) | 2,
1031 htlc_minimum_msat: 0,
1033 fee_proportional_millionths: 0,
1036 network.nodes.insert(node5.clone(), NodeInfo {
1037 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1038 lowest_inbound_channel_fee_base_msat: 0,
1039 lowest_inbound_channel_fee_proportional_millionths: 0,
1040 features: GlobalFeatures::new(),
1044 addresses: Vec::new(),
1046 network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
1047 features: GlobalFeatures::new(),
1048 one_to_two: DirectionalChannelInfo {
1049 src_node_id: node3.clone(),
1052 cltv_expiry_delta: (6 << 8) | 1,
1053 htlc_minimum_msat: 0,
1055 fee_proportional_millionths: 0,
1056 }, two_to_one: DirectionalChannelInfo {
1057 src_node_id: node5.clone(),
1060 cltv_expiry_delta: (6 << 8) | 2,
1061 htlc_minimum_msat: 0,
1063 fee_proportional_millionths: 0,
1066 network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
1067 features: GlobalFeatures::new(),
1068 one_to_two: DirectionalChannelInfo {
1069 src_node_id: node5.clone(),
1072 cltv_expiry_delta: (11 << 8) | 1,
1073 htlc_minimum_msat: 0,
1075 fee_proportional_millionths: 0,
1076 }, two_to_one: DirectionalChannelInfo {
1077 src_node_id: node4.clone(),
1080 cltv_expiry_delta: (11 << 8) | 2,
1081 htlc_minimum_msat: 0,
1083 fee_proportional_millionths: 0,
1086 network.nodes.insert(node6.clone(), NodeInfo {
1087 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
1088 lowest_inbound_channel_fee_base_msat: 0,
1089 lowest_inbound_channel_fee_proportional_millionths: 0,
1090 features: GlobalFeatures::new(),
1094 addresses: Vec::new(),
1096 network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
1097 features: GlobalFeatures::new(),
1098 one_to_two: DirectionalChannelInfo {
1099 src_node_id: node3.clone(),
1102 cltv_expiry_delta: (7 << 8) | 1,
1103 htlc_minimum_msat: 0,
1105 fee_proportional_millionths: 1000000,
1106 }, two_to_one: DirectionalChannelInfo {
1107 src_node_id: node6.clone(),
1110 cltv_expiry_delta: (7 << 8) | 2,
1111 htlc_minimum_msat: 0,
1113 fee_proportional_millionths: 0,
1118 { // Simple route to 3 via 2
1119 let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
1120 assert_eq!(route.hops.len(), 2);
1122 assert_eq!(route.hops[0].pubkey, node2);
1123 assert_eq!(route.hops[0].short_channel_id, 2);
1124 assert_eq!(route.hops[0].fee_msat, 100);
1125 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1127 assert_eq!(route.hops[1].pubkey, node3);
1128 assert_eq!(route.hops[1].short_channel_id, 4);
1129 assert_eq!(route.hops[1].fee_msat, 100);
1130 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1133 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
1134 let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
1135 assert_eq!(route.hops.len(), 3);
1137 assert_eq!(route.hops[0].pubkey, node2);
1138 assert_eq!(route.hops[0].short_channel_id, 2);
1139 assert_eq!(route.hops[0].fee_msat, 200);
1140 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1142 assert_eq!(route.hops[1].pubkey, node3);
1143 assert_eq!(route.hops[1].short_channel_id, 4);
1144 assert_eq!(route.hops[1].fee_msat, 100);
1145 assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
1147 assert_eq!(route.hops[2].pubkey, node1);
1148 assert_eq!(route.hops[2].short_channel_id, 3);
1149 assert_eq!(route.hops[2].fee_msat, 100);
1150 assert_eq!(route.hops[2].cltv_expiry_delta, 42);
1153 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1154 let our_chans = vec![channelmanager::ChannelDetails {
1155 channel_id: [0; 32],
1156 short_channel_id: Some(42),
1157 remote_network_id: node8.clone(),
1158 channel_value_satoshis: 0,
1161 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1162 assert_eq!(route.hops.len(), 2);
1164 assert_eq!(route.hops[0].pubkey, node8);
1165 assert_eq!(route.hops[0].short_channel_id, 42);
1166 assert_eq!(route.hops[0].fee_msat, 200);
1167 assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1169 assert_eq!(route.hops[1].pubkey, node3);
1170 assert_eq!(route.hops[1].short_channel_id, 13);
1171 assert_eq!(route.hops[1].fee_msat, 100);
1172 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1175 let mut last_hops = vec!(RouteHint {
1176 src_node_id: node4.clone(),
1177 short_channel_id: 8,
1179 fee_proportional_millionths: 0,
1180 cltv_expiry_delta: (8 << 8) | 1,
1181 htlc_minimum_msat: 0,
1183 src_node_id: node5.clone(),
1184 short_channel_id: 9,
1185 fee_base_msat: 1001,
1186 fee_proportional_millionths: 0,
1187 cltv_expiry_delta: (9 << 8) | 1,
1188 htlc_minimum_msat: 0,
1190 src_node_id: node6.clone(),
1191 short_channel_id: 10,
1193 fee_proportional_millionths: 0,
1194 cltv_expiry_delta: (10 << 8) | 1,
1195 htlc_minimum_msat: 0,
1198 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
1199 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1200 assert_eq!(route.hops.len(), 5);
1202 assert_eq!(route.hops[0].pubkey, node2);
1203 assert_eq!(route.hops[0].short_channel_id, 2);
1204 assert_eq!(route.hops[0].fee_msat, 100);
1205 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1207 assert_eq!(route.hops[1].pubkey, node3);
1208 assert_eq!(route.hops[1].short_channel_id, 4);
1209 assert_eq!(route.hops[1].fee_msat, 0);
1210 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1212 assert_eq!(route.hops[2].pubkey, node5);
1213 assert_eq!(route.hops[2].short_channel_id, 6);
1214 assert_eq!(route.hops[2].fee_msat, 0);
1215 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1217 assert_eq!(route.hops[3].pubkey, node4);
1218 assert_eq!(route.hops[3].short_channel_id, 11);
1219 assert_eq!(route.hops[3].fee_msat, 0);
1220 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1222 assert_eq!(route.hops[4].pubkey, node7);
1223 assert_eq!(route.hops[4].short_channel_id, 8);
1224 assert_eq!(route.hops[4].fee_msat, 100);
1225 assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1228 { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1229 let our_chans = vec![channelmanager::ChannelDetails {
1230 channel_id: [0; 32],
1231 short_channel_id: Some(42),
1232 remote_network_id: node4.clone(),
1233 channel_value_satoshis: 0,
1236 let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
1237 assert_eq!(route.hops.len(), 2);
1239 assert_eq!(route.hops[0].pubkey, node4);
1240 assert_eq!(route.hops[0].short_channel_id, 42);
1241 assert_eq!(route.hops[0].fee_msat, 0);
1242 assert_eq!(route.hops[0].cltv_expiry_delta, (8 << 8) | 1);
1244 assert_eq!(route.hops[1].pubkey, node7);
1245 assert_eq!(route.hops[1].short_channel_id, 8);
1246 assert_eq!(route.hops[1].fee_msat, 100);
1247 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1250 last_hops[0].fee_base_msat = 1000;
1252 { // Revert to via 6 as the fee on 8 goes up
1253 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1254 assert_eq!(route.hops.len(), 4);
1256 assert_eq!(route.hops[0].pubkey, node2);
1257 assert_eq!(route.hops[0].short_channel_id, 2);
1258 assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
1259 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1261 assert_eq!(route.hops[1].pubkey, node3);
1262 assert_eq!(route.hops[1].short_channel_id, 4);
1263 assert_eq!(route.hops[1].fee_msat, 100);
1264 assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
1266 assert_eq!(route.hops[2].pubkey, node6);
1267 assert_eq!(route.hops[2].short_channel_id, 7);
1268 assert_eq!(route.hops[2].fee_msat, 0);
1269 assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
1271 assert_eq!(route.hops[3].pubkey, node7);
1272 assert_eq!(route.hops[3].short_channel_id, 10);
1273 assert_eq!(route.hops[3].fee_msat, 100);
1274 assert_eq!(route.hops[3].cltv_expiry_delta, 42);
1277 { // ...but still use 8 for larger payments as 6 has a variable feerate
1278 let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
1279 assert_eq!(route.hops.len(), 5);
1281 assert_eq!(route.hops[0].pubkey, node2);
1282 assert_eq!(route.hops[0].short_channel_id, 2);
1283 assert_eq!(route.hops[0].fee_msat, 3000);
1284 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1286 assert_eq!(route.hops[1].pubkey, node3);
1287 assert_eq!(route.hops[1].short_channel_id, 4);
1288 assert_eq!(route.hops[1].fee_msat, 0);
1289 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1291 assert_eq!(route.hops[2].pubkey, node5);
1292 assert_eq!(route.hops[2].short_channel_id, 6);
1293 assert_eq!(route.hops[2].fee_msat, 0);
1294 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1296 assert_eq!(route.hops[3].pubkey, node4);
1297 assert_eq!(route.hops[3].short_channel_id, 11);
1298 assert_eq!(route.hops[3].fee_msat, 1000);
1299 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1301 assert_eq!(route.hops[4].pubkey, node7);
1302 assert_eq!(route.hops[4].short_channel_id, 8);
1303 assert_eq!(route.hops[4].fee_msat, 2000);
1304 assert_eq!(route.hops[4].cltv_expiry_delta, 42);