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, InitSyncTracker};
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;
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 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)?;
117 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
118 channels: Vec<(u64, Sha256dHash)>,
119 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
122 lowest_inbound_channel_fee_base_msat: u32,
123 lowest_inbound_channel_fee_proportional_millionths: u32,
125 features: GlobalFeatures,
129 addresses: Vec<NetAddress>,
130 //this is cached here so we can send out it later if required by route_init_sync
131 //keep an eye on this to see if the extra memory is a problem
132 announcement_message : Option<msgs::NodeAnnouncement>,
135 impl std::fmt::Display for NodeInfo {
136 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
137 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[..])?;
143 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
144 channels: BTreeMap<(u64, Sha256dHash), ChannelInfo>,
145 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
146 channels: BTreeMap<u64, ChannelInfo>,
148 our_node_id: PublicKey,
149 nodes: BTreeMap<PublicKey, NodeInfo>,
151 struct MutNetworkMap<'a> {
152 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
153 channels: &'a mut BTreeMap<(u64, Sha256dHash), ChannelInfo>,
154 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
155 channels: &'a mut BTreeMap<u64, ChannelInfo>,
156 nodes: &'a mut BTreeMap<PublicKey, NodeInfo>,
159 fn borrow_parts(&mut self) -> MutNetworkMap {
161 channels: &mut self.channels,
162 nodes: &mut self.nodes,
166 impl std::fmt::Display for NetworkMap {
167 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
168 write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
169 for (key, val) in self.channels.iter() {
170 write!(f, " {}: {}\n", key, val)?;
172 write!(f, "[Nodes]\n")?;
173 for (key, val) in self.nodes.iter() {
174 write!(f, " {}: {}\n", log_pubkey!(key), val)?;
181 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
183 fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
184 (short_channel_id, chain_hash)
187 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
189 fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
193 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
195 fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
199 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
201 fn get_short_id(id: &u64) -> &u64 {
206 /// A channel descriptor which provides a last-hop route to get_route
207 pub struct RouteHint {
208 /// The node_id of the non-target end of the route
209 pub src_node_id: PublicKey,
210 /// The short_channel_id of this channel
211 pub short_channel_id: u64,
212 /// The static msat-denominated fee which must be paid to use this channel
213 pub fee_base_msat: u32,
214 /// The dynamic proportional fee which must be paid to use this channel, denominated in
215 /// millionths of the value being forwarded to the next hop.
216 pub fee_proportional_millionths: u32,
217 /// The difference in CLTV values between this node and the next node.
218 pub cltv_expiry_delta: u16,
219 /// The minimum value, in msat, which must be relayed to the next hop.
220 pub htlc_minimum_msat: u64,
223 /// Tracks a view of the network, receiving updates from peers and generating Routes to
224 /// payment destinations.
226 secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
227 network_map: RwLock<NetworkMap>,
228 chain_monitor: Arc<ChainWatchInterface>,
232 macro_rules! secp_verify_sig {
233 ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
234 match $secp_ctx.verify($msg, $sig, $pubkey) {
236 Err(_) => return Err(HandleError{err: "Invalid signature from remote node", action: None}),
241 impl RoutingMessageHandler for Router {
242 fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, HandleError> {
243 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
244 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
246 if msg.contents.features.requires_unknown_bits() {
247 panic!("Unknown-required-features NodeAnnouncements should never deserialize!");
250 let mut network = self.network_map.write().unwrap();
251 match network.nodes.get_mut(&msg.contents.node_id) {
252 None => Err(HandleError{err: "No existing channels for node_announcement", action: Some(ErrorAction::IgnoreError)}),
254 if node.last_update >= msg.contents.timestamp {
255 return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
258 node.features = msg.contents.features.clone();
259 node.last_update = msg.contents.timestamp;
260 node.rgb = msg.contents.rgb;
261 node.alias = msg.contents.alias;
262 node.addresses = msg.contents.addresses.clone();
263 node.announcement_message = if msg.contents.excess_data.is_empty(){
270 Ok(msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty() && !msg.contents.features.supports_unknown_bits())
275 fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
276 if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
277 return Err(HandleError{err: "Channel announcement node had a channel with itself", action: Some(ErrorAction::IgnoreError)});
280 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
281 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
282 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
283 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
284 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
286 if msg.contents.features.requires_unknown_bits() {
287 panic!("Unknown-required-features ChannelAnnouncements should never deserialize!");
290 let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
291 Ok((script_pubkey, _value)) => {
292 let expected_script = Builder::new().push_opcode(opcodes::All::OP_PUSHNUM_2)
293 .push_slice(&msg.contents.bitcoin_key_1.serialize())
294 .push_slice(&msg.contents.bitcoin_key_2.serialize())
295 .push_opcode(opcodes::All::OP_PUSHNUM_2).push_opcode(opcodes::All::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
296 if script_pubkey != expected_script {
297 return Err(HandleError{err: "Channel announcement keys didn't match on-chain script", action: Some(ErrorAction::IgnoreError)});
299 //TODO: Check if value is worth storing, use it to inform routing, and compare it
300 //to the new HTLC max field in channel_update
303 Err(ChainError::NotSupported) => {
304 // Tentatively accept, potentially exposing us to DoS attacks
307 Err(ChainError::NotWatched) => {
308 return Err(HandleError{err: "Channel announced on an unknown chain", action: Some(ErrorAction::IgnoreError)});
310 Err(ChainError::UnknownTx) => {
311 return Err(HandleError{err: "Channel announced without corresponding UTXO entry", action: Some(ErrorAction::IgnoreError)});
315 let mut network_lock = self.network_map.write().unwrap();
316 let network = network_lock.borrow_parts();
318 let chan_info = ChannelInfo {
319 features: msg.contents.features.clone(),
320 one_to_two: DirectionalChannelInfo {
321 src_node_id: msg.contents.node_id_1.clone(),
324 cltv_expiry_delta: u16::max_value(),
325 htlc_minimum_msat: u64::max_value(),
326 fee_base_msat: u32::max_value(),
327 fee_proportional_millionths: u32::max_value(),
328 last_update_message : None,
330 two_to_one: DirectionalChannelInfo {
331 src_node_id: msg.contents.node_id_2.clone(),
334 cltv_expiry_delta: u16::max_value(),
335 htlc_minimum_msat: u64::max_value(),
336 fee_base_msat: u32::max_value(),
337 fee_proportional_millionths: u32::max_value(),
338 last_update_message : None,
340 announcement_message : if msg.contents.excess_data.is_empty(){
348 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
349 BtreeEntry::Occupied(mut entry) => {
350 //TODO: because asking the blockchain if short_channel_id is valid is only optional
351 //in the blockchain API, we need to handle it smartly here, though its unclear
354 // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
355 // only sometimes returns results. In any case remove the previous entry. Note
356 // that the spec expects us to "blacklist" the node_ids involved, but we can't
358 // a) we don't *require* a UTXO provider that always returns results.
359 // b) we don't track UTXOs of channels we know about and remove them if they
361 // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
362 Self::remove_channel_in_nodes(network.nodes, &entry.get(), msg.contents.short_channel_id);
363 *entry.get_mut() = chan_info;
365 return Err(HandleError{err: "Already have knowledge of channel", action: Some(ErrorAction::IgnoreError)})
368 BtreeEntry::Vacant(entry) => {
369 entry.insert(chan_info);
373 macro_rules! add_channel_to_node {
374 ( $node_id: expr ) => {
375 match network.nodes.entry($node_id) {
376 BtreeEntry::Occupied(node_entry) => {
377 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
379 BtreeEntry::Vacant(node_entry) => {
380 node_entry.insert(NodeInfo {
381 channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
382 lowest_inbound_channel_fee_base_msat: u32::max_value(),
383 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
384 features: GlobalFeatures::new(),
388 addresses: Vec::new(),
389 announcement_message: None,
396 add_channel_to_node!(msg.contents.node_id_1);
397 add_channel_to_node!(msg.contents.node_id_2);
399 Ok(msg.contents.excess_data.is_empty() && !msg.contents.features.supports_unknown_bits())
402 fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
404 &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
405 let _ = self.handle_channel_update(msg);
407 &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
408 let mut network = self.network_map.write().unwrap();
410 if let Some(chan) = network.channels.remove(short_channel_id) {
411 Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
414 if let Some(chan) = network.channels.get_mut(short_channel_id) {
415 chan.one_to_two.enabled = false;
416 chan.two_to_one.enabled = false;
420 &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
422 //TODO: Wholly remove the node
424 self.mark_node_bad(node_id, false);
430 fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, HandleError> {
431 let mut network = self.network_map.write().unwrap();
433 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
434 let chan_was_enabled;
436 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
437 None => return Err(HandleError{err: "Couldn't find channel for update", action: Some(ErrorAction::IgnoreError)}),
439 macro_rules! maybe_update_channel_info {
440 ( $target: expr) => {
441 if $target.last_update >= msg.contents.timestamp {
442 return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
444 chan_was_enabled = $target.enabled;
445 $target.last_update = msg.contents.timestamp;
446 $target.enabled = chan_enabled;
447 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
448 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
449 $target.fee_base_msat = msg.contents.fee_base_msat;
450 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
451 $target.last_update_message = if msg.contents.excess_data.is_empty(){
459 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
460 if msg.contents.flags & 1 == 1 {
461 dest_node_id = channel.one_to_two.src_node_id.clone();
462 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
463 maybe_update_channel_info!(channel.two_to_one);
465 dest_node_id = channel.two_to_one.src_node_id.clone();
466 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
467 maybe_update_channel_info!(channel.one_to_two);
473 let node = network.nodes.get_mut(&dest_node_id).unwrap();
474 node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
475 node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
476 } else if chan_was_enabled {
477 let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
478 let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
481 let node = network.nodes.get(&dest_node_id).unwrap();
483 for chan_id in node.channels.iter() {
484 let chan = network.channels.get(chan_id).unwrap();
485 if chan.one_to_two.src_node_id == dest_node_id {
486 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
487 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
489 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
490 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
495 //TODO: satisfy the borrow-checker without a double-map-lookup :(
496 let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
497 mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
498 mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
501 Ok(msg.contents.excess_data.is_empty())
505 fn get_next_channel_announcements(&self, starting_point: &mut InitSyncTracker, batch_amount: u8)->(Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)>){
506 let mut result = Vec::new();
507 let network = self.network_map.read().unwrap();
508 let mut starting_for_next = if let InitSyncTracker::ChannelCounter(i) = *starting_point {i} else {0 as u64};
509 let mut iter = network.channels.range((starting_for_next)..);
510 for _x in 0..batch_amount{
511 if let Some(ref value) = iter.next(){
512 if value.1.announcement_message.is_some() && value.1.one_to_two.last_update_message.is_some() && value.1.two_to_one.last_update_message.is_some(){
513 let channel_announcement = value.1.announcement_message.clone().unwrap();
514 let channel_update1 = value.1.one_to_two.last_update_message.clone().unwrap();
515 let channel_update2 = value.1.two_to_one.last_update_message.clone().unwrap();
516 result.push((channel_announcement,channel_update1, channel_update2));
518 starting_for_next = *(value.0) ;//adjusting start value so we can pass the last used back
521 *starting_point = if result.len() == 0{
522 InitSyncTracker::Sync(false) //sync done so disable sync required
524 InitSyncTracker::ChannelCounter(starting_for_next)
529 fn get_next_node_announcements(&self, starting_point: &mut InitSyncTracker, batch_amount: u8)->(Vec<msgs::NodeAnnouncement>){
530 let mut result = Vec::new();
531 let network = self.network_map.read().unwrap();
532 let mut iter = if let InitSyncTracker::NodeCounter(i) = *starting_point {network.nodes.range((i)..)} else {
533 let first_item = network.nodes.iter().next();
534 if let None = first_item {*starting_point = InitSyncTracker::Sync(false); return result} //for some reason we know of no nodes
535 network.nodes.range(*(first_item.unwrap().0)..)};
536 for _x in 0..batch_amount{
537 if let Some(ref value) = iter.next(){
538 if value.1.announcement_message.is_some(){
539 let node_announcement = value.1.announcement_message.clone().unwrap();
540 result.push(node_announcement);
542 *starting_point = InitSyncTracker::NodeCounter(*(value.0)) ;//adjusting start value so we can pass the last used back
545 if result.len() == 0{
546 *starting_point = InitSyncTracker::ChannelCounter(0) //node syncing done, move on towards channels
552 #[derive(Eq, PartialEq)]
553 struct RouteGraphNode {
555 lowest_fee_to_peer_through_node: u64,
556 lowest_fee_to_node: u64,
559 impl cmp::Ord for RouteGraphNode {
560 fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
561 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
562 .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
566 impl cmp::PartialOrd for RouteGraphNode {
567 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
568 Some(self.cmp(other))
572 struct DummyDirectionalChannelInfo {
573 src_node_id: PublicKey,
574 cltv_expiry_delta: u32,
575 htlc_minimum_msat: u64,
577 fee_proportional_millionths: u32,
581 /// Creates a new router with the given node_id to be used as the source for get_route()
582 pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
583 let mut nodes = BTreeMap::new();
584 nodes.insert(our_pubkey.clone(), NodeInfo {
585 channels: Vec::new(),
586 lowest_inbound_channel_fee_base_msat: u32::max_value(),
587 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
588 features: GlobalFeatures::new(),
592 addresses: Vec::new(),
593 announcement_message: None,
596 secp_ctx: Secp256k1::verification_only(),
597 network_map: RwLock::new(NetworkMap {
598 channels: BTreeMap::new(),
599 our_node_id: our_pubkey,
607 /// Dumps the entire network view of this Router to the logger provided in the constructor at
609 pub fn trace_state(&self) {
610 log_trace!(self, "{}", self.network_map.read().unwrap());
613 /// Get network addresses by node id
614 pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
615 let network = self.network_map.read().unwrap();
616 network.nodes.get(pubkey).map(|n| n.addresses.clone())
619 /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
620 /// with an expotnential decay in node "badness". Note that there is deliberately no
621 /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
622 /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
623 /// boolean will reduce the penalty, returning the node to usability faster. If the node is
624 /// behaving correctly, it will disable the failing channel and we will use it again next time.
625 pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
629 fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
630 macro_rules! remove_from_node {
631 ($node_id: expr) => {
632 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
633 entry.get_mut().channels.retain(|chan_id| {
634 short_channel_id != *NetworkMap::get_short_id(chan_id)
636 if entry.get().channels.is_empty() {
637 entry.remove_entry();
640 panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
644 remove_from_node!(chan.one_to_two.src_node_id);
645 remove_from_node!(chan.two_to_one.src_node_id);
648 /// Gets a route from us to the given target node.
650 /// Extra routing hops between known nodes and the target will be used if they are included in
653 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
654 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
655 /// (this Router's) view of our local channels will be ignored, and only those in first_hops
658 /// Panics if first_hops contains channels without short_channel_ids
659 /// (ChannelManager::list_usable_channels will never include such channels).
661 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
662 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
663 /// *is* checked as they may change based on the receiving node.
664 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> {
665 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
666 // uptime/success in using a node in the past.
667 let network = self.network_map.read().unwrap();
669 if *target == network.our_node_id {
670 return Err(HandleError{err: "Cannot generate a route to ourselves", action: None});
673 if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
674 return Err(HandleError{err: "Cannot generate a route of more value than all existing satoshis", action: None});
677 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
678 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
679 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
680 // to use as the A* heuristic beyond just the cost to get one node further than the current
683 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
684 src_node_id: network.our_node_id.clone(),
685 cltv_expiry_delta: 0,
686 htlc_minimum_msat: 0,
688 fee_proportional_millionths: 0,
691 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
692 let mut dist = HashMap::with_capacity(network.nodes.len());
694 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
695 if let Some(hops) = first_hops {
697 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
698 if chan.remote_network_id == *target {
700 hops: vec![RouteHop {
701 pubkey: chan.remote_network_id,
703 fee_msat: final_value_msat,
704 cltv_expiry_delta: final_cltv,
708 first_hop_targets.insert(chan.remote_network_id, short_channel_id);
710 if first_hop_targets.is_empty() {
711 return Err(HandleError{err: "Cannot route when there are no outbound routes away from us", action: None});
715 macro_rules! add_entry {
716 // Adds entry which goes from the node pointed to by $directional_info to
717 // $dest_node_id over the channel with id $chan_id with fees described in
718 // $directional_info.
719 ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $starting_fee_msat: expr ) => {
720 //TODO: Explore simply adding fee to hit htlc_minimum_msat
721 if $starting_fee_msat as u64 + final_value_msat > $directional_info.htlc_minimum_msat {
722 let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
723 if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
724 ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
726 let mut total_fee = $starting_fee_msat as u64;
727 let hm_entry = dist.entry(&$directional_info.src_node_id);
728 let old_entry = hm_entry.or_insert_with(|| {
729 let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
731 node.lowest_inbound_channel_fee_base_msat,
732 node.lowest_inbound_channel_fee_proportional_millionths,
734 pubkey: $dest_node_id.clone(),
737 cltv_expiry_delta: 0,
740 if $directional_info.src_node_id != network.our_node_id {
741 // Ignore new_fee for channel-from-us as we assume all channels-from-us
742 // will have the same effective-fee
743 total_fee += new_fee;
744 if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
745 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
747 // max_value means we'll always fail the old_entry.0 > total_fee check
748 total_fee = u64::max_value();
751 let new_graph_node = RouteGraphNode {
752 pubkey: $directional_info.src_node_id,
753 lowest_fee_to_peer_through_node: total_fee,
754 lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
756 if old_entry.0 > total_fee {
757 targets.push(new_graph_node);
758 old_entry.0 = total_fee;
759 old_entry.3 = RouteHop {
760 pubkey: $dest_node_id.clone(),
761 short_channel_id: $chan_id.clone(),
762 fee_msat: new_fee, // This field is ignored on the last-hop anyway
763 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
771 macro_rules! add_entries_to_cheapest_to_target_node {
772 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
773 if first_hops.is_some() {
774 if let Some(first_hop) = first_hop_targets.get(&$node_id) {
775 add_entry!(first_hop, $node_id, dummy_directional_info, $fee_to_target_msat);
779 for chan_id in $node.channels.iter() {
780 let chan = network.channels.get(chan_id).unwrap();
781 if chan.one_to_two.src_node_id == *$node_id {
782 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
783 if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
784 if chan.two_to_one.enabled {
785 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
789 if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
790 if chan.one_to_two.enabled {
791 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
799 match network.nodes.get(target) {
802 add_entries_to_cheapest_to_target_node!(node, target, 0);
806 for hop in last_hops.iter() {
807 if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
808 if network.nodes.get(&hop.src_node_id).is_some() {
809 if first_hops.is_some() {
810 if let Some(first_hop) = first_hop_targets.get(&hop.src_node_id) {
811 add_entry!(first_hop, hop.src_node_id, dummy_directional_info, 0);
814 add_entry!(hop.short_channel_id, target, hop, 0);
819 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
820 if pubkey == network.our_node_id {
821 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
822 while res.last().unwrap().pubkey != *target {
823 let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
825 None => return Err(HandleError{err: "Failed to find a non-fee-overflowing path to the given destination", action: None}),
827 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
828 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
831 res.last_mut().unwrap().fee_msat = final_value_msat;
832 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
833 let route = Route { hops: res };
834 log_trace!(self, "Got route: {}", log_route!(route));
838 match network.nodes.get(&pubkey) {
841 add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
846 Err(HandleError{err: "Failed to find a path to the given destination", action: None})
852 use chain::chaininterface;
853 use ln::channelmanager;
854 use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
855 use ln::msgs::GlobalFeatures;
856 use util::test_utils;
857 use util::logger::Logger;
859 use bitcoin::util::hash::Sha256dHash;
860 use bitcoin::network::constants::Network;
864 use secp256k1::key::{PublicKey,SecretKey};
865 use secp256k1::Secp256k1;
871 let secp_ctx = Secp256k1::new();
872 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
873 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
874 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
875 let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
877 // Build network from our_id to node8:
879 // -1(1)2- node1 -1(3)2-
881 // our_id -1(12)2- node8 -1(13)2--- node3
883 // -1(2)2- node2 -1(4)2-
886 // chan1 1-to-2: disabled
887 // chan1 2-to-1: enabled, 0 fee
889 // chan2 1-to-2: enabled, ignored fee
890 // chan2 2-to-1: enabled, 0 fee
892 // chan3 1-to-2: enabled, 0 fee
893 // chan3 2-to-1: enabled, 100 msat fee
895 // chan4 1-to-2: enabled, 100% fee
896 // chan4 2-to-1: enabled, 0 fee
898 // chan12 1-to-2: enabled, ignored fee
899 // chan12 2-to-1: enabled, 0 fee
901 // chan13 1-to-2: enabled, 200% fee
902 // chan13 2-to-1: enabled, 0 fee
905 // -1(5)2- node4 -1(8)2--
909 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
911 // -1(7)2- node6 -1(10)2-
913 // chan5 1-to-2: enabled, 100 msat fee
914 // chan5 2-to-1: enabled, 0 fee
916 // chan6 1-to-2: enabled, 0 fee
917 // chan6 2-to-1: enabled, 0 fee
919 // chan7 1-to-2: enabled, 100% fee
920 // chan7 2-to-1: enabled, 0 fee
922 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
923 // chan8 2-to-1: enabled, 0 fee
925 // chan9 1-to-2: enabled, 1001 msat fee
926 // chan9 2-to-1: enabled, 0 fee
928 // chan10 1-to-2: enabled, 0 fee
929 // chan10 2-to-1: enabled, 0 fee
931 // chan11 1-to-2: enabled, 0 fee
932 // chan11 2-to-1: enabled, 0 fee
934 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
935 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
936 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
937 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
938 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
939 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
940 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
941 let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
943 let zero_hash = Sha256dHash::from_data(&[0; 32]);
946 let mut network = router.network_map.write().unwrap();
948 network.nodes.insert(node1.clone(), NodeInfo {
949 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
950 lowest_inbound_channel_fee_base_msat: 100,
951 lowest_inbound_channel_fee_proportional_millionths: 0,
952 features: GlobalFeatures::new(),
956 addresses: Vec::new(),
957 announcement_message : None,
959 network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
960 features: GlobalFeatures::new(),
961 one_to_two: DirectionalChannelInfo {
962 src_node_id: our_id.clone(),
965 cltv_expiry_delta: u16::max_value(), // This value should be ignored
966 htlc_minimum_msat: 0,
967 fee_base_msat: u32::max_value(), // This value should be ignored
968 fee_proportional_millionths: u32::max_value(), // This value should be ignored
969 last_update_message: None,
970 }, two_to_one: DirectionalChannelInfo {
971 src_node_id: node1.clone(),
974 cltv_expiry_delta: 0,
975 htlc_minimum_msat: 0,
977 fee_proportional_millionths: 0,
978 last_update_message: None,
980 announcement_message : None,
982 network.nodes.insert(node2.clone(), NodeInfo {
983 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
984 lowest_inbound_channel_fee_base_msat: 0,
985 lowest_inbound_channel_fee_proportional_millionths: 0,
986 features: GlobalFeatures::new(),
990 addresses: Vec::new(),
991 announcement_message : None,
993 network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
994 features: GlobalFeatures::new(),
995 one_to_two: DirectionalChannelInfo {
996 src_node_id: our_id.clone(),
999 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1000 htlc_minimum_msat: 0,
1001 fee_base_msat: u32::max_value(), // This value should be ignored
1002 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1003 last_update_message: None,
1004 }, two_to_one: DirectionalChannelInfo {
1005 src_node_id: node2.clone(),
1008 cltv_expiry_delta: 0,
1009 htlc_minimum_msat: 0,
1011 fee_proportional_millionths: 0,
1012 last_update_message: None,
1014 announcement_message : None,
1016 network.nodes.insert(node8.clone(), NodeInfo {
1017 channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
1018 lowest_inbound_channel_fee_base_msat: 0,
1019 lowest_inbound_channel_fee_proportional_millionths: 0,
1020 features: GlobalFeatures::new(),
1024 addresses: Vec::new(),
1025 announcement_message : None,
1027 network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
1028 features: GlobalFeatures::new(),
1029 one_to_two: DirectionalChannelInfo {
1030 src_node_id: our_id.clone(),
1033 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1034 htlc_minimum_msat: 0,
1035 fee_base_msat: u32::max_value(), // This value should be ignored
1036 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1037 last_update_message: None,
1038 }, two_to_one: DirectionalChannelInfo {
1039 src_node_id: node8.clone(),
1042 cltv_expiry_delta: 0,
1043 htlc_minimum_msat: 0,
1045 fee_proportional_millionths: 0,
1046 last_update_message: None,
1048 announcement_message : None,
1050 network.nodes.insert(node3.clone(), NodeInfo {
1052 NetworkMap::get_key(3, zero_hash.clone()),
1053 NetworkMap::get_key(4, zero_hash.clone()),
1054 NetworkMap::get_key(13, zero_hash.clone()),
1055 NetworkMap::get_key(5, zero_hash.clone()),
1056 NetworkMap::get_key(6, zero_hash.clone()),
1057 NetworkMap::get_key(7, zero_hash.clone())),
1058 lowest_inbound_channel_fee_base_msat: 0,
1059 lowest_inbound_channel_fee_proportional_millionths: 0,
1060 features: GlobalFeatures::new(),
1064 addresses: Vec::new(),
1065 announcement_message : None,
1067 network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
1068 features: GlobalFeatures::new(),
1069 one_to_two: DirectionalChannelInfo {
1070 src_node_id: node1.clone(),
1073 cltv_expiry_delta: (3 << 8) | 1,
1074 htlc_minimum_msat: 0,
1076 fee_proportional_millionths: 0,
1077 last_update_message: None,
1078 }, two_to_one: DirectionalChannelInfo {
1079 src_node_id: node3.clone(),
1082 cltv_expiry_delta: (3 << 8) | 2,
1083 htlc_minimum_msat: 0,
1085 fee_proportional_millionths: 0,
1086 last_update_message: None,
1088 announcement_message : None,
1090 network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
1091 features: GlobalFeatures::new(),
1092 one_to_two: DirectionalChannelInfo {
1093 src_node_id: node2.clone(),
1096 cltv_expiry_delta: (4 << 8) | 1,
1097 htlc_minimum_msat: 0,
1099 fee_proportional_millionths: 1000000,
1100 last_update_message: None,
1101 }, two_to_one: DirectionalChannelInfo {
1102 src_node_id: node3.clone(),
1105 cltv_expiry_delta: (4 << 8) | 2,
1106 htlc_minimum_msat: 0,
1108 fee_proportional_millionths: 0,
1109 last_update_message: None,
1111 announcement_message : None,
1113 network.channels.insert(NetworkMap::get_key(13, zero_hash.clone()), ChannelInfo {
1114 features: GlobalFeatures::new(),
1115 one_to_two: DirectionalChannelInfo {
1116 src_node_id: node8.clone(),
1119 cltv_expiry_delta: (13 << 8) | 1,
1120 htlc_minimum_msat: 0,
1122 fee_proportional_millionths: 2000000,
1123 last_update_message: None,
1124 }, two_to_one: DirectionalChannelInfo {
1125 src_node_id: node3.clone(),
1128 cltv_expiry_delta: (13 << 8) | 2,
1129 htlc_minimum_msat: 0,
1131 fee_proportional_millionths: 0,
1132 last_update_message: None,
1134 announcement_message : None,
1136 network.nodes.insert(node4.clone(), NodeInfo {
1137 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1138 lowest_inbound_channel_fee_base_msat: 0,
1139 lowest_inbound_channel_fee_proportional_millionths: 0,
1140 features: GlobalFeatures::new(),
1144 addresses: Vec::new(),
1145 announcement_message : None,
1147 network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
1148 features: GlobalFeatures::new(),
1149 one_to_two: DirectionalChannelInfo {
1150 src_node_id: node3.clone(),
1153 cltv_expiry_delta: (5 << 8) | 1,
1154 htlc_minimum_msat: 0,
1156 fee_proportional_millionths: 0,
1157 last_update_message: None,
1158 }, two_to_one: DirectionalChannelInfo {
1159 src_node_id: node4.clone(),
1162 cltv_expiry_delta: (5 << 8) | 2,
1163 htlc_minimum_msat: 0,
1165 fee_proportional_millionths: 0,
1166 last_update_message: None,
1168 announcement_message : None,
1170 network.nodes.insert(node5.clone(), NodeInfo {
1171 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1172 lowest_inbound_channel_fee_base_msat: 0,
1173 lowest_inbound_channel_fee_proportional_millionths: 0,
1174 features: GlobalFeatures::new(),
1178 addresses: Vec::new(),
1179 announcement_message : None,
1181 network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
1182 features: GlobalFeatures::new(),
1183 one_to_two: DirectionalChannelInfo {
1184 src_node_id: node3.clone(),
1187 cltv_expiry_delta: (6 << 8) | 1,
1188 htlc_minimum_msat: 0,
1190 fee_proportional_millionths: 0,
1191 last_update_message: None,
1192 }, two_to_one: DirectionalChannelInfo {
1193 src_node_id: node5.clone(),
1196 cltv_expiry_delta: (6 << 8) | 2,
1197 htlc_minimum_msat: 0,
1199 fee_proportional_millionths: 0,
1200 last_update_message: None,
1202 announcement_message : None,
1204 network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
1205 features: GlobalFeatures::new(),
1206 one_to_two: DirectionalChannelInfo {
1207 src_node_id: node5.clone(),
1210 cltv_expiry_delta: (11 << 8) | 1,
1211 htlc_minimum_msat: 0,
1213 fee_proportional_millionths: 0,
1214 last_update_message: None,
1215 }, two_to_one: DirectionalChannelInfo {
1216 src_node_id: node4.clone(),
1219 cltv_expiry_delta: (11 << 8) | 2,
1220 htlc_minimum_msat: 0,
1222 fee_proportional_millionths: 0,
1223 last_update_message: None,
1225 announcement_message : None,
1227 network.nodes.insert(node6.clone(), NodeInfo {
1228 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
1229 lowest_inbound_channel_fee_base_msat: 0,
1230 lowest_inbound_channel_fee_proportional_millionths: 0,
1231 features: GlobalFeatures::new(),
1235 addresses: Vec::new(),
1236 announcement_message : None,
1238 network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
1239 features: GlobalFeatures::new(),
1240 one_to_two: DirectionalChannelInfo {
1241 src_node_id: node3.clone(),
1244 cltv_expiry_delta: (7 << 8) | 1,
1245 htlc_minimum_msat: 0,
1247 fee_proportional_millionths: 1000000,
1248 last_update_message: None,
1249 }, two_to_one: DirectionalChannelInfo {
1250 src_node_id: node6.clone(),
1253 cltv_expiry_delta: (7 << 8) | 2,
1254 htlc_minimum_msat: 0,
1256 fee_proportional_millionths: 0,
1257 last_update_message: None,
1259 announcement_message : None,
1263 { // Simple route to 3 via 2
1264 let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
1265 assert_eq!(route.hops.len(), 2);
1267 assert_eq!(route.hops[0].pubkey, node2);
1268 assert_eq!(route.hops[0].short_channel_id, 2);
1269 assert_eq!(route.hops[0].fee_msat, 100);
1270 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1272 assert_eq!(route.hops[1].pubkey, node3);
1273 assert_eq!(route.hops[1].short_channel_id, 4);
1274 assert_eq!(route.hops[1].fee_msat, 100);
1275 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1278 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
1279 let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
1280 assert_eq!(route.hops.len(), 3);
1282 assert_eq!(route.hops[0].pubkey, node2);
1283 assert_eq!(route.hops[0].short_channel_id, 2);
1284 assert_eq!(route.hops[0].fee_msat, 200);
1285 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1287 assert_eq!(route.hops[1].pubkey, node3);
1288 assert_eq!(route.hops[1].short_channel_id, 4);
1289 assert_eq!(route.hops[1].fee_msat, 100);
1290 assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
1292 assert_eq!(route.hops[2].pubkey, node1);
1293 assert_eq!(route.hops[2].short_channel_id, 3);
1294 assert_eq!(route.hops[2].fee_msat, 100);
1295 assert_eq!(route.hops[2].cltv_expiry_delta, 42);
1298 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1299 let our_chans = vec![channelmanager::ChannelDetails {
1300 channel_id: [0; 32],
1301 short_channel_id: Some(42),
1302 remote_network_id: node8.clone(),
1303 channel_value_satoshis: 0,
1306 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1307 assert_eq!(route.hops.len(), 2);
1309 assert_eq!(route.hops[0].pubkey, node8);
1310 assert_eq!(route.hops[0].short_channel_id, 42);
1311 assert_eq!(route.hops[0].fee_msat, 200);
1312 assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1314 assert_eq!(route.hops[1].pubkey, node3);
1315 assert_eq!(route.hops[1].short_channel_id, 13);
1316 assert_eq!(route.hops[1].fee_msat, 100);
1317 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1320 let mut last_hops = vec!(RouteHint {
1321 src_node_id: node4.clone(),
1322 short_channel_id: 8,
1324 fee_proportional_millionths: 0,
1325 cltv_expiry_delta: (8 << 8) | 1,
1326 htlc_minimum_msat: 0,
1328 src_node_id: node5.clone(),
1329 short_channel_id: 9,
1330 fee_base_msat: 1001,
1331 fee_proportional_millionths: 0,
1332 cltv_expiry_delta: (9 << 8) | 1,
1333 htlc_minimum_msat: 0,
1335 src_node_id: node6.clone(),
1336 short_channel_id: 10,
1338 fee_proportional_millionths: 0,
1339 cltv_expiry_delta: (10 << 8) | 1,
1340 htlc_minimum_msat: 0,
1343 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
1344 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1345 assert_eq!(route.hops.len(), 5);
1347 assert_eq!(route.hops[0].pubkey, node2);
1348 assert_eq!(route.hops[0].short_channel_id, 2);
1349 assert_eq!(route.hops[0].fee_msat, 100);
1350 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1352 assert_eq!(route.hops[1].pubkey, node3);
1353 assert_eq!(route.hops[1].short_channel_id, 4);
1354 assert_eq!(route.hops[1].fee_msat, 0);
1355 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1357 assert_eq!(route.hops[2].pubkey, node5);
1358 assert_eq!(route.hops[2].short_channel_id, 6);
1359 assert_eq!(route.hops[2].fee_msat, 0);
1360 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1362 assert_eq!(route.hops[3].pubkey, node4);
1363 assert_eq!(route.hops[3].short_channel_id, 11);
1364 assert_eq!(route.hops[3].fee_msat, 0);
1365 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1367 assert_eq!(route.hops[4].pubkey, node7);
1368 assert_eq!(route.hops[4].short_channel_id, 8);
1369 assert_eq!(route.hops[4].fee_msat, 100);
1370 assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1373 { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1374 let our_chans = vec![channelmanager::ChannelDetails {
1375 channel_id: [0; 32],
1376 short_channel_id: Some(42),
1377 remote_network_id: node4.clone(),
1378 channel_value_satoshis: 0,
1381 let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
1382 assert_eq!(route.hops.len(), 2);
1384 assert_eq!(route.hops[0].pubkey, node4);
1385 assert_eq!(route.hops[0].short_channel_id, 42);
1386 assert_eq!(route.hops[0].fee_msat, 0);
1387 assert_eq!(route.hops[0].cltv_expiry_delta, (8 << 8) | 1);
1389 assert_eq!(route.hops[1].pubkey, node7);
1390 assert_eq!(route.hops[1].short_channel_id, 8);
1391 assert_eq!(route.hops[1].fee_msat, 100);
1392 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1395 last_hops[0].fee_base_msat = 1000;
1397 { // Revert to via 6 as the fee on 8 goes up
1398 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1399 assert_eq!(route.hops.len(), 4);
1401 assert_eq!(route.hops[0].pubkey, node2);
1402 assert_eq!(route.hops[0].short_channel_id, 2);
1403 assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
1404 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1406 assert_eq!(route.hops[1].pubkey, node3);
1407 assert_eq!(route.hops[1].short_channel_id, 4);
1408 assert_eq!(route.hops[1].fee_msat, 100);
1409 assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
1411 assert_eq!(route.hops[2].pubkey, node6);
1412 assert_eq!(route.hops[2].short_channel_id, 7);
1413 assert_eq!(route.hops[2].fee_msat, 0);
1414 assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
1416 assert_eq!(route.hops[3].pubkey, node7);
1417 assert_eq!(route.hops[3].short_channel_id, 10);
1418 assert_eq!(route.hops[3].fee_msat, 100);
1419 assert_eq!(route.hops[3].cltv_expiry_delta, 42);
1422 { // ...but still use 8 for larger payments as 6 has a variable feerate
1423 let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
1424 assert_eq!(route.hops.len(), 5);
1426 assert_eq!(route.hops[0].pubkey, node2);
1427 assert_eq!(route.hops[0].short_channel_id, 2);
1428 assert_eq!(route.hops[0].fee_msat, 3000);
1429 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1431 assert_eq!(route.hops[1].pubkey, node3);
1432 assert_eq!(route.hops[1].short_channel_id, 4);
1433 assert_eq!(route.hops[1].fee_msat, 0);
1434 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1436 assert_eq!(route.hops[2].pubkey, node5);
1437 assert_eq!(route.hops[2].short_channel_id, 6);
1438 assert_eq!(route.hops[2].fee_msat, 0);
1439 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1441 assert_eq!(route.hops[3].pubkey, node4);
1442 assert_eq!(route.hops[3].short_channel_id, 11);
1443 assert_eq!(route.hops[3].fee_msat, 1000);
1444 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1446 assert_eq!(route.hops[4].pubkey, node7);
1447 assert_eq!(route.hops[4].short_channel_id, 8);
1448 assert_eq!(route.hops[4].fee_msat, 2000);
1449 assert_eq!(route.hops[4].cltv_expiry_delta, 42);