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::{DecodeError,ErrorAction,HandleError,RoutingMessageHandler,NetAddress,GlobalFeatures};
18 use util::ser::{Writeable, Readable};
19 use util::logger::Logger;
22 use std::sync::{RwLock,Arc};
23 use std::collections::{HashMap,BinaryHeap,BTreeMap};
24 use std::collections::btree_map::Entry as BtreeEntry;
28 #[derive(Clone, PartialEq)]
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
42 #[derive(Clone, PartialEq)]
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 impl Writeable for Route {
51 fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
52 (self.hops.len() as u8).write(writer)?;
53 for hop in self.hops.iter() {
54 hop.pubkey.write(writer)?;
55 hop.short_channel_id.write(writer)?;
56 hop.fee_msat.write(writer)?;
57 hop.cltv_expiry_delta.write(writer)?;
63 impl<R: ::std::io::Read> Readable<R> for Route {
64 fn read(reader: &mut R) -> Result<Route, DecodeError> {
65 let hops_count: u8 = Readable::read(reader)?;
66 let mut hops = Vec::with_capacity(hops_count as usize);
67 for _ in 0..hops_count {
69 pubkey: Readable::read(reader)?,
70 short_channel_id: Readable::read(reader)?,
71 fee_msat: Readable::read(reader)?,
72 cltv_expiry_delta: Readable::read(reader)?,
81 struct DirectionalChannelInfo {
82 src_node_id: PublicKey,
85 cltv_expiry_delta: u16,
86 htlc_minimum_msat: u64,
88 fee_proportional_millionths: u32,
89 last_update_message: Option<msgs::ChannelUpdate>,
92 impl std::fmt::Display for DirectionalChannelInfo {
93 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
94 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)?;
100 features: GlobalFeatures,
101 one_to_two: DirectionalChannelInfo,
102 two_to_one: DirectionalChannelInfo,
103 //this is cached here so we can send out it later if required by route_init_sync
104 //keep an eye on this to see if the extra memory is a problem
105 announcement_message: Option<msgs::ChannelAnnouncement>,
108 impl std::fmt::Display for ChannelInfo {
109 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
110 write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?;
116 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
117 channels: Vec<(u64, Sha256dHash)>,
118 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
121 lowest_inbound_channel_fee_base_msat: u32,
122 lowest_inbound_channel_fee_proportional_millionths: u32,
124 features: GlobalFeatures,
128 addresses: Vec<NetAddress>,
129 //this is cached here so we can send out it later if required by route_init_sync
130 //keep an eye on this to see if the extra memory is a problem
131 announcement_message: Option<msgs::NodeAnnouncement>,
134 impl std::fmt::Display for NodeInfo {
135 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
136 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[..])?;
142 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
143 channels: BTreeMap<(u64, Sha256dHash), ChannelInfo>,
144 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
145 channels: BTreeMap<u64, ChannelInfo>,
147 our_node_id: PublicKey,
148 nodes: BTreeMap<PublicKey, NodeInfo>,
150 struct MutNetworkMap<'a> {
151 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
152 channels: &'a mut BTreeMap<(u64, Sha256dHash), ChannelInfo>,
153 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
154 channels: &'a mut BTreeMap<u64, ChannelInfo>,
155 nodes: &'a mut BTreeMap<PublicKey, NodeInfo>,
158 fn borrow_parts(&mut self) -> MutNetworkMap {
160 channels: &mut self.channels,
161 nodes: &mut self.nodes,
165 impl std::fmt::Display for NetworkMap {
166 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
167 write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
168 for (key, val) in self.channels.iter() {
169 write!(f, " {}: {}\n", key, val)?;
171 write!(f, "[Nodes]\n")?;
172 for (key, val) in self.nodes.iter() {
173 write!(f, " {}: {}\n", log_pubkey!(key), val)?;
180 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
182 fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
183 (short_channel_id, chain_hash)
186 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
188 fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
192 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
194 fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
198 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
200 fn get_short_id(id: &u64) -> &u64 {
205 /// A channel descriptor which provides a last-hop route to get_route
206 pub struct RouteHint {
207 /// The node_id of the non-target end of the route
208 pub src_node_id: PublicKey,
209 /// The short_channel_id of this channel
210 pub short_channel_id: u64,
211 /// The static msat-denominated fee which must be paid to use this channel
212 pub fee_base_msat: u32,
213 /// The dynamic proportional fee which must be paid to use this channel, denominated in
214 /// millionths of the value being forwarded to the next hop.
215 pub fee_proportional_millionths: u32,
216 /// The difference in CLTV values between this node and the next node.
217 pub cltv_expiry_delta: u16,
218 /// The minimum value, in msat, which must be relayed to the next hop.
219 pub htlc_minimum_msat: u64,
222 /// Tracks a view of the network, receiving updates from peers and generating Routes to
223 /// payment destinations.
225 secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
226 network_map: RwLock<NetworkMap>,
227 chain_monitor: Arc<ChainWatchInterface>,
231 macro_rules! secp_verify_sig {
232 ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
233 match $secp_ctx.verify($msg, $sig, $pubkey) {
235 Err(_) => return Err(HandleError{err: "Invalid signature from remote node", action: None}),
240 impl RoutingMessageHandler for Router {
241 fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
242 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
243 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
245 if msg.contents.features.requires_unknown_bits() {
246 panic!("Unknown-required-features NodeAnnouncements should never deserialize!");
249 let mut network = self.network_map.write().unwrap();
250 match network.nodes.get_mut(&msg.contents.node_id) {
251 None => Err(HandleError{err: "No existing channels for node_announcement", action: Some(ErrorAction::IgnoreError)}),
253 if node.last_update >= msg.contents.timestamp {
254 return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
257 node.features = msg.contents.features.clone();
258 node.last_update = msg.contents.timestamp;
259 node.rgb = msg.contents.rgb;
260 node.alias = msg.contents.alias;
261 node.addresses = msg.contents.addresses.clone();
263 let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty() && !msg.contents.features.supports_unknown_bits();
264 node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
270 fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
271 if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
272 return Err(HandleError{err: "Channel announcement node had a channel with itself", action: Some(ErrorAction::IgnoreError)});
275 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
276 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
277 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
278 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
279 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
281 if msg.contents.features.requires_unknown_bits() {
282 panic!("Unknown-required-features ChannelAnnouncements should never deserialize!");
285 let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
286 Ok((script_pubkey, _value)) => {
287 let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
288 .push_slice(&msg.contents.bitcoin_key_1.serialize())
289 .push_slice(&msg.contents.bitcoin_key_2.serialize())
290 .push_opcode(opcodes::all::OP_PUSHNUM_2)
291 .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
292 if script_pubkey != expected_script {
293 return Err(HandleError{err: "Channel announcement keys didn't match on-chain script", action: Some(ErrorAction::IgnoreError)});
295 //TODO: Check if value is worth storing, use it to inform routing, and compare it
296 //to the new HTLC max field in channel_update
299 Err(ChainError::NotSupported) => {
300 // Tentatively accept, potentially exposing us to DoS attacks
303 Err(ChainError::NotWatched) => {
304 return Err(HandleError{err: "Channel announced on an unknown chain", action: Some(ErrorAction::IgnoreError)});
306 Err(ChainError::UnknownTx) => {
307 return Err(HandleError{err: "Channel announced without corresponding UTXO entry", action: Some(ErrorAction::IgnoreError)});
311 let mut network_lock = self.network_map.write().unwrap();
312 let network = network_lock.borrow_parts();
314 let should_relay = msg.contents.excess_data.is_empty() && !msg.contents.features.supports_unknown_bits();
316 let chan_info = ChannelInfo {
317 features: msg.contents.features.clone(),
318 one_to_two: DirectionalChannelInfo {
319 src_node_id: msg.contents.node_id_1.clone(),
322 cltv_expiry_delta: u16::max_value(),
323 htlc_minimum_msat: u64::max_value(),
324 fee_base_msat: u32::max_value(),
325 fee_proportional_millionths: u32::max_value(),
326 last_update_message: None,
328 two_to_one: DirectionalChannelInfo {
329 src_node_id: msg.contents.node_id_2.clone(),
332 cltv_expiry_delta: u16::max_value(),
333 htlc_minimum_msat: u64::max_value(),
334 fee_base_msat: u32::max_value(),
335 fee_proportional_millionths: u32::max_value(),
336 last_update_message: None,
338 announcement_message: if should_relay { Some(msg.clone()) } else { None },
341 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
342 BtreeEntry::Occupied(mut entry) => {
343 //TODO: because asking the blockchain if short_channel_id is valid is only optional
344 //in the blockchain API, we need to handle it smartly here, though its unclear
347 // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
348 // only sometimes returns results. In any case remove the previous entry. Note
349 // that the spec expects us to "blacklist" the node_ids involved, but we can't
351 // a) we don't *require* a UTXO provider that always returns results.
352 // b) we don't track UTXOs of channels we know about and remove them if they
354 // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
355 Self::remove_channel_in_nodes(network.nodes, &entry.get(), msg.contents.short_channel_id);
356 *entry.get_mut() = chan_info;
358 return Err(HandleError{err: "Already have knowledge of channel", action: Some(ErrorAction::IgnoreError)})
361 BtreeEntry::Vacant(entry) => {
362 entry.insert(chan_info);
366 macro_rules! add_channel_to_node {
367 ( $node_id: expr ) => {
368 match network.nodes.entry($node_id) {
369 BtreeEntry::Occupied(node_entry) => {
370 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
372 BtreeEntry::Vacant(node_entry) => {
373 node_entry.insert(NodeInfo {
374 channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
375 lowest_inbound_channel_fee_base_msat: u32::max_value(),
376 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
377 features: GlobalFeatures::new(),
381 addresses: Vec::new(),
382 announcement_message: None,
389 add_channel_to_node!(msg.contents.node_id_1);
390 add_channel_to_node!(msg.contents.node_id_2);
395 fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
397 &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
398 let _ = self.handle_channel_update(msg);
400 &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
401 let mut network = self.network_map.write().unwrap();
403 if let Some(chan) = network.channels.remove(short_channel_id) {
404 Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
407 if let Some(chan) = network.channels.get_mut(short_channel_id) {
408 chan.one_to_two.enabled = false;
409 chan.two_to_one.enabled = false;
413 &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
415 //TODO: Wholly remove the node
417 self.mark_node_bad(node_id, false);
423 fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
424 let mut network = self.network_map.write().unwrap();
426 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
427 let chan_was_enabled;
429 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
430 None => return Err(HandleError{err: "Couldn't find channel for update", action: Some(ErrorAction::IgnoreError)}),
432 macro_rules! maybe_update_channel_info {
433 ( $target: expr) => {
434 if $target.last_update >= msg.contents.timestamp {
435 return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
437 chan_was_enabled = $target.enabled;
438 $target.last_update = msg.contents.timestamp;
439 $target.enabled = chan_enabled;
440 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
441 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
442 $target.fee_base_msat = msg.contents.fee_base_msat;
443 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
444 $target.last_update_message = if msg.contents.excess_data.is_empty() {
451 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
452 if msg.contents.flags & 1 == 1 {
453 dest_node_id = channel.one_to_two.src_node_id.clone();
454 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
455 maybe_update_channel_info!(channel.two_to_one);
457 dest_node_id = channel.two_to_one.src_node_id.clone();
458 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
459 maybe_update_channel_info!(channel.one_to_two);
465 let node = network.nodes.get_mut(&dest_node_id).unwrap();
466 node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
467 node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
468 } else if chan_was_enabled {
469 let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
470 let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
473 let node = network.nodes.get(&dest_node_id).unwrap();
475 for chan_id in node.channels.iter() {
476 let chan = network.channels.get(chan_id).unwrap();
477 if chan.one_to_two.src_node_id == dest_node_id {
478 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
479 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
481 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
482 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
487 //TODO: satisfy the borrow-checker without a double-map-lookup :(
488 let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
489 mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
490 mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
493 Ok(msg.contents.excess_data.is_empty())
497 fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
498 let mut result = Vec::with_capacity(batch_amount as usize);
499 let network = self.network_map.read().unwrap();
500 let mut iter = network.channels.range(starting_point..);
501 while result.len() < batch_amount as usize {
502 if let Some((_, ref chan)) = iter.next() {
503 if chan.announcement_message.is_some() &&
504 chan.one_to_two.last_update_message.is_some() &&
505 chan.two_to_one.last_update_message.is_some() {
506 result.push((chan.announcement_message.clone().unwrap(),
507 chan.one_to_two.last_update_message.clone().unwrap(),
508 chan.two_to_one.last_update_message.clone().unwrap()));
510 // TODO: We may end up sending un-announced channel_updates if we are sending
511 // initial sync data while receiving announce/updates for this channel.
520 fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
521 let mut result = Vec::with_capacity(batch_amount as usize);
522 let network = self.network_map.read().unwrap();
523 let mut iter = if let Some(pubkey) = starting_point {
524 let mut iter = network.nodes.range((*pubkey)..);
528 network.nodes.range(..)
530 while result.len() < batch_amount as usize {
531 if let Some((_, ref node)) = iter.next() {
532 if node.announcement_message.is_some() {
533 result.push(node.announcement_message.clone().unwrap());
543 #[derive(Eq, PartialEq)]
544 struct RouteGraphNode {
546 lowest_fee_to_peer_through_node: u64,
547 lowest_fee_to_node: u64,
550 impl cmp::Ord for RouteGraphNode {
551 fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
552 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
553 .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
557 impl cmp::PartialOrd for RouteGraphNode {
558 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
559 Some(self.cmp(other))
563 struct DummyDirectionalChannelInfo {
564 src_node_id: PublicKey,
565 cltv_expiry_delta: u32,
566 htlc_minimum_msat: u64,
568 fee_proportional_millionths: u32,
572 /// Creates a new router with the given node_id to be used as the source for get_route()
573 pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
574 let mut nodes = BTreeMap::new();
575 nodes.insert(our_pubkey.clone(), NodeInfo {
576 channels: Vec::new(),
577 lowest_inbound_channel_fee_base_msat: u32::max_value(),
578 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
579 features: GlobalFeatures::new(),
583 addresses: Vec::new(),
584 announcement_message: None,
587 secp_ctx: Secp256k1::verification_only(),
588 network_map: RwLock::new(NetworkMap {
589 channels: BTreeMap::new(),
590 our_node_id: our_pubkey,
598 /// Dumps the entire network view of this Router to the logger provided in the constructor at
600 pub fn trace_state(&self) {
601 log_trace!(self, "{}", self.network_map.read().unwrap());
604 /// Get network addresses by node id
605 pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
606 let network = self.network_map.read().unwrap();
607 network.nodes.get(pubkey).map(|n| n.addresses.clone())
610 /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
611 /// with an expotnential decay in node "badness". Note that there is deliberately no
612 /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
613 /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
614 /// boolean will reduce the penalty, returning the node to usability faster. If the node is
615 /// behaving correctly, it will disable the failing channel and we will use it again next time.
616 pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
620 fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
621 macro_rules! remove_from_node {
622 ($node_id: expr) => {
623 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
624 entry.get_mut().channels.retain(|chan_id| {
625 short_channel_id != *NetworkMap::get_short_id(chan_id)
627 if entry.get().channels.is_empty() {
628 entry.remove_entry();
631 panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
635 remove_from_node!(chan.one_to_two.src_node_id);
636 remove_from_node!(chan.two_to_one.src_node_id);
639 /// Gets a route from us to the given target node.
641 /// Extra routing hops between known nodes and the target will be used if they are included in
644 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
645 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
646 /// (this Router's) view of our local channels will be ignored, and only those in first_hops
649 /// Panics if first_hops contains channels without short_channel_ids
650 /// (ChannelManager::list_usable_channels will never include such channels).
652 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
653 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
654 /// *is* checked as they may change based on the receiving node.
655 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> {
656 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
657 // uptime/success in using a node in the past.
658 let network = self.network_map.read().unwrap();
660 if *target == network.our_node_id {
661 return Err(HandleError{err: "Cannot generate a route to ourselves", action: None});
664 if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
665 return Err(HandleError{err: "Cannot generate a route of more value than all existing satoshis", action: None});
668 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
669 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
670 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
671 // to use as the A* heuristic beyond just the cost to get one node further than the current
674 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
675 src_node_id: network.our_node_id.clone(),
676 cltv_expiry_delta: 0,
677 htlc_minimum_msat: 0,
679 fee_proportional_millionths: 0,
682 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
683 let mut dist = HashMap::with_capacity(network.nodes.len());
685 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
686 if let Some(hops) = first_hops {
688 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
689 if chan.remote_network_id == *target {
691 hops: vec![RouteHop {
692 pubkey: chan.remote_network_id,
694 fee_msat: final_value_msat,
695 cltv_expiry_delta: final_cltv,
699 first_hop_targets.insert(chan.remote_network_id, short_channel_id);
701 if first_hop_targets.is_empty() {
702 return Err(HandleError{err: "Cannot route when there are no outbound routes away from us", action: None});
706 macro_rules! add_entry {
707 // Adds entry which goes from the node pointed to by $directional_info to
708 // $dest_node_id over the channel with id $chan_id with fees described in
709 // $directional_info.
710 ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $starting_fee_msat: expr ) => {
711 //TODO: Explore simply adding fee to hit htlc_minimum_msat
712 if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
713 let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
714 if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
715 ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
717 let mut total_fee = $starting_fee_msat as u64;
718 let hm_entry = dist.entry(&$directional_info.src_node_id);
719 let old_entry = hm_entry.or_insert_with(|| {
720 let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
722 node.lowest_inbound_channel_fee_base_msat,
723 node.lowest_inbound_channel_fee_proportional_millionths,
725 pubkey: $dest_node_id.clone(),
728 cltv_expiry_delta: 0,
731 if $directional_info.src_node_id != network.our_node_id {
732 // Ignore new_fee for channel-from-us as we assume all channels-from-us
733 // will have the same effective-fee
734 total_fee += new_fee;
735 if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
736 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
738 // max_value means we'll always fail the old_entry.0 > total_fee check
739 total_fee = u64::max_value();
742 let new_graph_node = RouteGraphNode {
743 pubkey: $directional_info.src_node_id,
744 lowest_fee_to_peer_through_node: total_fee,
745 lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
747 if old_entry.0 > total_fee {
748 targets.push(new_graph_node);
749 old_entry.0 = total_fee;
750 old_entry.3 = RouteHop {
751 pubkey: $dest_node_id.clone(),
752 short_channel_id: $chan_id.clone(),
753 fee_msat: new_fee, // This field is ignored on the last-hop anyway
754 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
762 macro_rules! add_entries_to_cheapest_to_target_node {
763 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
764 if first_hops.is_some() {
765 if let Some(first_hop) = first_hop_targets.get(&$node_id) {
766 add_entry!(first_hop, $node_id, dummy_directional_info, $fee_to_target_msat);
770 for chan_id in $node.channels.iter() {
771 let chan = network.channels.get(chan_id).unwrap();
772 if chan.one_to_two.src_node_id == *$node_id {
773 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
774 if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
775 if chan.two_to_one.enabled {
776 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
780 if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
781 if chan.one_to_two.enabled {
782 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
790 match network.nodes.get(target) {
793 add_entries_to_cheapest_to_target_node!(node, target, 0);
797 for hop in last_hops.iter() {
798 if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
799 if network.nodes.get(&hop.src_node_id).is_some() {
800 if first_hops.is_some() {
801 if let Some(first_hop) = first_hop_targets.get(&hop.src_node_id) {
802 add_entry!(first_hop, hop.src_node_id, dummy_directional_info, 0);
805 add_entry!(hop.short_channel_id, target, hop, 0);
810 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
811 if pubkey == network.our_node_id {
812 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
813 while res.last().unwrap().pubkey != *target {
814 let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
816 None => return Err(HandleError{err: "Failed to find a non-fee-overflowing path to the given destination", action: None}),
818 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
819 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
822 res.last_mut().unwrap().fee_msat = final_value_msat;
823 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
824 let route = Route { hops: res };
825 log_trace!(self, "Got route: {}", log_route!(route));
829 match network.nodes.get(&pubkey) {
832 add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
837 Err(HandleError{err: "Failed to find a path to the given destination", action: None})
843 use chain::chaininterface;
844 use ln::channelmanager;
845 use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
846 use ln::msgs::GlobalFeatures;
847 use util::test_utils;
848 use util::logger::Logger;
850 use bitcoin::util::hash::Sha256dHash;
851 use bitcoin::network::constants::Network;
855 use secp256k1::key::{PublicKey,SecretKey};
856 use secp256k1::Secp256k1;
862 let secp_ctx = Secp256k1::new();
863 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
864 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
865 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
866 let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
868 // Build network from our_id to node8:
870 // -1(1)2- node1 -1(3)2-
872 // our_id -1(12)2- node8 -1(13)2--- node3
874 // -1(2)2- node2 -1(4)2-
877 // chan1 1-to-2: disabled
878 // chan1 2-to-1: enabled, 0 fee
880 // chan2 1-to-2: enabled, ignored fee
881 // chan2 2-to-1: enabled, 0 fee
883 // chan3 1-to-2: enabled, 0 fee
884 // chan3 2-to-1: enabled, 100 msat fee
886 // chan4 1-to-2: enabled, 100% fee
887 // chan4 2-to-1: enabled, 0 fee
889 // chan12 1-to-2: enabled, ignored fee
890 // chan12 2-to-1: enabled, 0 fee
892 // chan13 1-to-2: enabled, 200% fee
893 // chan13 2-to-1: enabled, 0 fee
896 // -1(5)2- node4 -1(8)2--
900 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
902 // -1(7)2- node6 -1(10)2-
904 // chan5 1-to-2: enabled, 100 msat fee
905 // chan5 2-to-1: enabled, 0 fee
907 // chan6 1-to-2: enabled, 0 fee
908 // chan6 2-to-1: enabled, 0 fee
910 // chan7 1-to-2: enabled, 100% fee
911 // chan7 2-to-1: enabled, 0 fee
913 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
914 // chan8 2-to-1: enabled, 0 fee
916 // chan9 1-to-2: enabled, 1001 msat fee
917 // chan9 2-to-1: enabled, 0 fee
919 // chan10 1-to-2: enabled, 0 fee
920 // chan10 2-to-1: enabled, 0 fee
922 // chan11 1-to-2: enabled, 0 fee
923 // chan11 2-to-1: enabled, 0 fee
925 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
926 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
927 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
928 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
929 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
930 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
931 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
932 let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
934 let zero_hash = Sha256dHash::from_data(&[0; 32]);
937 let mut network = router.network_map.write().unwrap();
939 network.nodes.insert(node1.clone(), NodeInfo {
940 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
941 lowest_inbound_channel_fee_base_msat: 100,
942 lowest_inbound_channel_fee_proportional_millionths: 0,
943 features: GlobalFeatures::new(),
947 addresses: Vec::new(),
948 announcement_message: None,
950 network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
951 features: GlobalFeatures::new(),
952 one_to_two: DirectionalChannelInfo {
953 src_node_id: our_id.clone(),
956 cltv_expiry_delta: u16::max_value(), // This value should be ignored
957 htlc_minimum_msat: 0,
958 fee_base_msat: u32::max_value(), // This value should be ignored
959 fee_proportional_millionths: u32::max_value(), // This value should be ignored
960 last_update_message: None,
961 }, two_to_one: DirectionalChannelInfo {
962 src_node_id: node1.clone(),
965 cltv_expiry_delta: 0,
966 htlc_minimum_msat: 0,
968 fee_proportional_millionths: 0,
969 last_update_message: None,
971 announcement_message: None,
973 network.nodes.insert(node2.clone(), NodeInfo {
974 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
975 lowest_inbound_channel_fee_base_msat: 0,
976 lowest_inbound_channel_fee_proportional_millionths: 0,
977 features: GlobalFeatures::new(),
981 addresses: Vec::new(),
982 announcement_message: None,
984 network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
985 features: GlobalFeatures::new(),
986 one_to_two: DirectionalChannelInfo {
987 src_node_id: our_id.clone(),
990 cltv_expiry_delta: u16::max_value(), // This value should be ignored
991 htlc_minimum_msat: 0,
992 fee_base_msat: u32::max_value(), // This value should be ignored
993 fee_proportional_millionths: u32::max_value(), // This value should be ignored
994 last_update_message: None,
995 }, two_to_one: DirectionalChannelInfo {
996 src_node_id: node2.clone(),
999 cltv_expiry_delta: 0,
1000 htlc_minimum_msat: 0,
1002 fee_proportional_millionths: 0,
1003 last_update_message: None,
1005 announcement_message: None,
1007 network.nodes.insert(node8.clone(), NodeInfo {
1008 channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
1009 lowest_inbound_channel_fee_base_msat: 0,
1010 lowest_inbound_channel_fee_proportional_millionths: 0,
1011 features: GlobalFeatures::new(),
1015 addresses: Vec::new(),
1016 announcement_message: None,
1018 network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
1019 features: GlobalFeatures::new(),
1020 one_to_two: DirectionalChannelInfo {
1021 src_node_id: our_id.clone(),
1024 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1025 htlc_minimum_msat: 0,
1026 fee_base_msat: u32::max_value(), // This value should be ignored
1027 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1028 last_update_message: None,
1029 }, two_to_one: DirectionalChannelInfo {
1030 src_node_id: node8.clone(),
1033 cltv_expiry_delta: 0,
1034 htlc_minimum_msat: 0,
1036 fee_proportional_millionths: 0,
1037 last_update_message: None,
1039 announcement_message: None,
1041 network.nodes.insert(node3.clone(), NodeInfo {
1043 NetworkMap::get_key(3, zero_hash.clone()),
1044 NetworkMap::get_key(4, zero_hash.clone()),
1045 NetworkMap::get_key(13, zero_hash.clone()),
1046 NetworkMap::get_key(5, zero_hash.clone()),
1047 NetworkMap::get_key(6, zero_hash.clone()),
1048 NetworkMap::get_key(7, zero_hash.clone())),
1049 lowest_inbound_channel_fee_base_msat: 0,
1050 lowest_inbound_channel_fee_proportional_millionths: 0,
1051 features: GlobalFeatures::new(),
1055 addresses: Vec::new(),
1056 announcement_message: None,
1058 network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
1059 features: GlobalFeatures::new(),
1060 one_to_two: DirectionalChannelInfo {
1061 src_node_id: node1.clone(),
1064 cltv_expiry_delta: (3 << 8) | 1,
1065 htlc_minimum_msat: 0,
1067 fee_proportional_millionths: 0,
1068 last_update_message: None,
1069 }, two_to_one: DirectionalChannelInfo {
1070 src_node_id: node3.clone(),
1073 cltv_expiry_delta: (3 << 8) | 2,
1074 htlc_minimum_msat: 0,
1076 fee_proportional_millionths: 0,
1077 last_update_message: None,
1079 announcement_message: None,
1081 network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
1082 features: GlobalFeatures::new(),
1083 one_to_two: DirectionalChannelInfo {
1084 src_node_id: node2.clone(),
1087 cltv_expiry_delta: (4 << 8) | 1,
1088 htlc_minimum_msat: 0,
1090 fee_proportional_millionths: 1000000,
1091 last_update_message: None,
1092 }, two_to_one: DirectionalChannelInfo {
1093 src_node_id: node3.clone(),
1096 cltv_expiry_delta: (4 << 8) | 2,
1097 htlc_minimum_msat: 0,
1099 fee_proportional_millionths: 0,
1100 last_update_message: None,
1102 announcement_message: None,
1104 network.channels.insert(NetworkMap::get_key(13, zero_hash.clone()), ChannelInfo {
1105 features: GlobalFeatures::new(),
1106 one_to_two: DirectionalChannelInfo {
1107 src_node_id: node8.clone(),
1110 cltv_expiry_delta: (13 << 8) | 1,
1111 htlc_minimum_msat: 0,
1113 fee_proportional_millionths: 2000000,
1114 last_update_message: None,
1115 }, two_to_one: DirectionalChannelInfo {
1116 src_node_id: node3.clone(),
1119 cltv_expiry_delta: (13 << 8) | 2,
1120 htlc_minimum_msat: 0,
1122 fee_proportional_millionths: 0,
1123 last_update_message: None,
1125 announcement_message: None,
1127 network.nodes.insert(node4.clone(), NodeInfo {
1128 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1129 lowest_inbound_channel_fee_base_msat: 0,
1130 lowest_inbound_channel_fee_proportional_millionths: 0,
1131 features: GlobalFeatures::new(),
1135 addresses: Vec::new(),
1136 announcement_message: None,
1138 network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
1139 features: GlobalFeatures::new(),
1140 one_to_two: DirectionalChannelInfo {
1141 src_node_id: node3.clone(),
1144 cltv_expiry_delta: (5 << 8) | 1,
1145 htlc_minimum_msat: 0,
1147 fee_proportional_millionths: 0,
1148 last_update_message: None,
1149 }, two_to_one: DirectionalChannelInfo {
1150 src_node_id: node4.clone(),
1153 cltv_expiry_delta: (5 << 8) | 2,
1154 htlc_minimum_msat: 0,
1156 fee_proportional_millionths: 0,
1157 last_update_message: None,
1159 announcement_message: None,
1161 network.nodes.insert(node5.clone(), NodeInfo {
1162 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1163 lowest_inbound_channel_fee_base_msat: 0,
1164 lowest_inbound_channel_fee_proportional_millionths: 0,
1165 features: GlobalFeatures::new(),
1169 addresses: Vec::new(),
1170 announcement_message: None,
1172 network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
1173 features: GlobalFeatures::new(),
1174 one_to_two: DirectionalChannelInfo {
1175 src_node_id: node3.clone(),
1178 cltv_expiry_delta: (6 << 8) | 1,
1179 htlc_minimum_msat: 0,
1181 fee_proportional_millionths: 0,
1182 last_update_message: None,
1183 }, two_to_one: DirectionalChannelInfo {
1184 src_node_id: node5.clone(),
1187 cltv_expiry_delta: (6 << 8) | 2,
1188 htlc_minimum_msat: 0,
1190 fee_proportional_millionths: 0,
1191 last_update_message: None,
1193 announcement_message: None,
1195 network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
1196 features: GlobalFeatures::new(),
1197 one_to_two: DirectionalChannelInfo {
1198 src_node_id: node5.clone(),
1201 cltv_expiry_delta: (11 << 8) | 1,
1202 htlc_minimum_msat: 0,
1204 fee_proportional_millionths: 0,
1205 last_update_message: None,
1206 }, two_to_one: DirectionalChannelInfo {
1207 src_node_id: node4.clone(),
1210 cltv_expiry_delta: (11 << 8) | 2,
1211 htlc_minimum_msat: 0,
1213 fee_proportional_millionths: 0,
1214 last_update_message: None,
1216 announcement_message: None,
1218 network.nodes.insert(node6.clone(), NodeInfo {
1219 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
1220 lowest_inbound_channel_fee_base_msat: 0,
1221 lowest_inbound_channel_fee_proportional_millionths: 0,
1222 features: GlobalFeatures::new(),
1226 addresses: Vec::new(),
1227 announcement_message: None,
1229 network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
1230 features: GlobalFeatures::new(),
1231 one_to_two: DirectionalChannelInfo {
1232 src_node_id: node3.clone(),
1235 cltv_expiry_delta: (7 << 8) | 1,
1236 htlc_minimum_msat: 0,
1238 fee_proportional_millionths: 1000000,
1239 last_update_message: None,
1240 }, two_to_one: DirectionalChannelInfo {
1241 src_node_id: node6.clone(),
1244 cltv_expiry_delta: (7 << 8) | 2,
1245 htlc_minimum_msat: 0,
1247 fee_proportional_millionths: 0,
1248 last_update_message: None,
1250 announcement_message: None,
1254 { // Simple route to 3 via 2
1255 let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
1256 assert_eq!(route.hops.len(), 2);
1258 assert_eq!(route.hops[0].pubkey, node2);
1259 assert_eq!(route.hops[0].short_channel_id, 2);
1260 assert_eq!(route.hops[0].fee_msat, 100);
1261 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1263 assert_eq!(route.hops[1].pubkey, node3);
1264 assert_eq!(route.hops[1].short_channel_id, 4);
1265 assert_eq!(route.hops[1].fee_msat, 100);
1266 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1269 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
1270 let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
1271 assert_eq!(route.hops.len(), 3);
1273 assert_eq!(route.hops[0].pubkey, node2);
1274 assert_eq!(route.hops[0].short_channel_id, 2);
1275 assert_eq!(route.hops[0].fee_msat, 200);
1276 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1278 assert_eq!(route.hops[1].pubkey, node3);
1279 assert_eq!(route.hops[1].short_channel_id, 4);
1280 assert_eq!(route.hops[1].fee_msat, 100);
1281 assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
1283 assert_eq!(route.hops[2].pubkey, node1);
1284 assert_eq!(route.hops[2].short_channel_id, 3);
1285 assert_eq!(route.hops[2].fee_msat, 100);
1286 assert_eq!(route.hops[2].cltv_expiry_delta, 42);
1289 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1290 let our_chans = vec![channelmanager::ChannelDetails {
1291 channel_id: [0; 32],
1292 short_channel_id: Some(42),
1293 remote_network_id: node8.clone(),
1294 channel_value_satoshis: 0,
1297 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1298 assert_eq!(route.hops.len(), 2);
1300 assert_eq!(route.hops[0].pubkey, node8);
1301 assert_eq!(route.hops[0].short_channel_id, 42);
1302 assert_eq!(route.hops[0].fee_msat, 200);
1303 assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1305 assert_eq!(route.hops[1].pubkey, node3);
1306 assert_eq!(route.hops[1].short_channel_id, 13);
1307 assert_eq!(route.hops[1].fee_msat, 100);
1308 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1311 let mut last_hops = vec!(RouteHint {
1312 src_node_id: node4.clone(),
1313 short_channel_id: 8,
1315 fee_proportional_millionths: 0,
1316 cltv_expiry_delta: (8 << 8) | 1,
1317 htlc_minimum_msat: 0,
1319 src_node_id: node5.clone(),
1320 short_channel_id: 9,
1321 fee_base_msat: 1001,
1322 fee_proportional_millionths: 0,
1323 cltv_expiry_delta: (9 << 8) | 1,
1324 htlc_minimum_msat: 0,
1326 src_node_id: node6.clone(),
1327 short_channel_id: 10,
1329 fee_proportional_millionths: 0,
1330 cltv_expiry_delta: (10 << 8) | 1,
1331 htlc_minimum_msat: 0,
1334 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
1335 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1336 assert_eq!(route.hops.len(), 5);
1338 assert_eq!(route.hops[0].pubkey, node2);
1339 assert_eq!(route.hops[0].short_channel_id, 2);
1340 assert_eq!(route.hops[0].fee_msat, 100);
1341 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1343 assert_eq!(route.hops[1].pubkey, node3);
1344 assert_eq!(route.hops[1].short_channel_id, 4);
1345 assert_eq!(route.hops[1].fee_msat, 0);
1346 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1348 assert_eq!(route.hops[2].pubkey, node5);
1349 assert_eq!(route.hops[2].short_channel_id, 6);
1350 assert_eq!(route.hops[2].fee_msat, 0);
1351 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1353 assert_eq!(route.hops[3].pubkey, node4);
1354 assert_eq!(route.hops[3].short_channel_id, 11);
1355 assert_eq!(route.hops[3].fee_msat, 0);
1356 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1358 assert_eq!(route.hops[4].pubkey, node7);
1359 assert_eq!(route.hops[4].short_channel_id, 8);
1360 assert_eq!(route.hops[4].fee_msat, 100);
1361 assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1364 { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1365 let our_chans = vec![channelmanager::ChannelDetails {
1366 channel_id: [0; 32],
1367 short_channel_id: Some(42),
1368 remote_network_id: node4.clone(),
1369 channel_value_satoshis: 0,
1372 let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
1373 assert_eq!(route.hops.len(), 2);
1375 assert_eq!(route.hops[0].pubkey, node4);
1376 assert_eq!(route.hops[0].short_channel_id, 42);
1377 assert_eq!(route.hops[0].fee_msat, 0);
1378 assert_eq!(route.hops[0].cltv_expiry_delta, (8 << 8) | 1);
1380 assert_eq!(route.hops[1].pubkey, node7);
1381 assert_eq!(route.hops[1].short_channel_id, 8);
1382 assert_eq!(route.hops[1].fee_msat, 100);
1383 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1386 last_hops[0].fee_base_msat = 1000;
1388 { // Revert to via 6 as the fee on 8 goes up
1389 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1390 assert_eq!(route.hops.len(), 4);
1392 assert_eq!(route.hops[0].pubkey, node2);
1393 assert_eq!(route.hops[0].short_channel_id, 2);
1394 assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
1395 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1397 assert_eq!(route.hops[1].pubkey, node3);
1398 assert_eq!(route.hops[1].short_channel_id, 4);
1399 assert_eq!(route.hops[1].fee_msat, 100);
1400 assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
1402 assert_eq!(route.hops[2].pubkey, node6);
1403 assert_eq!(route.hops[2].short_channel_id, 7);
1404 assert_eq!(route.hops[2].fee_msat, 0);
1405 assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
1407 assert_eq!(route.hops[3].pubkey, node7);
1408 assert_eq!(route.hops[3].short_channel_id, 10);
1409 assert_eq!(route.hops[3].fee_msat, 100);
1410 assert_eq!(route.hops[3].cltv_expiry_delta, 42);
1413 { // ...but still use 8 for larger payments as 6 has a variable feerate
1414 let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
1415 assert_eq!(route.hops.len(), 5);
1417 assert_eq!(route.hops[0].pubkey, node2);
1418 assert_eq!(route.hops[0].short_channel_id, 2);
1419 assert_eq!(route.hops[0].fee_msat, 3000);
1420 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1422 assert_eq!(route.hops[1].pubkey, node3);
1423 assert_eq!(route.hops[1].short_channel_id, 4);
1424 assert_eq!(route.hops[1].fee_msat, 0);
1425 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1427 assert_eq!(route.hops[2].pubkey, node5);
1428 assert_eq!(route.hops[2].short_channel_id, 6);
1429 assert_eq!(route.hops[2].fee_msat, 0);
1430 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1432 assert_eq!(route.hops[3].pubkey, node4);
1433 assert_eq!(route.hops[3].short_channel_id, 11);
1434 assert_eq!(route.hops[3].fee_msat, 1000);
1435 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1437 assert_eq!(route.hops[4].pubkey, node7);
1438 assert_eq!(route.hops[4].short_channel_id, 8);
1439 assert_eq!(route.hops[4].fee_msat, 2000);
1440 assert_eq!(route.hops[4].cltv_expiry_delta, 42);