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;
10 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
11 use bitcoin_hashes::Hash;
12 use bitcoin::blockdata::script::Builder;
13 use bitcoin::blockdata::opcodes;
15 use chain::chaininterface::{ChainError, ChainWatchInterface};
16 use ln::channelmanager;
17 use ln::features::{ChannelFeatures, NodeFeatures};
18 use ln::msgs::{DecodeError,ErrorAction,LightningError,RoutingMessageHandler,NetAddress};
20 use util::ser::{Writeable, Readable, Writer, ReadableArgs};
21 use util::logger::Logger;
24 use std::sync::{RwLock,Arc};
25 use std::collections::{HashMap,BinaryHeap,BTreeMap};
26 use std::collections::btree_map::Entry as BtreeEntry;
30 #[derive(Clone, PartialEq)]
32 /// The node_id of the node at this hop.
33 pub pubkey: PublicKey,
34 /// The channel that should be used from the previous hop to reach this node.
35 pub short_channel_id: u64,
36 /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
38 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
39 /// expected at the destination, in excess of the current block height.
40 pub cltv_expiry_delta: u32,
43 /// A route from us through the network to a destination
44 #[derive(Clone, PartialEq)]
46 /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
47 /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
49 pub hops: Vec<RouteHop>,
52 impl Writeable for Route {
53 fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
54 (self.hops.len() as u8).write(writer)?;
55 for hop in self.hops.iter() {
56 hop.pubkey.write(writer)?;
57 hop.short_channel_id.write(writer)?;
58 hop.fee_msat.write(writer)?;
59 hop.cltv_expiry_delta.write(writer)?;
65 impl<R: ::std::io::Read> Readable<R> for Route {
66 fn read(reader: &mut R) -> Result<Route, DecodeError> {
67 let hops_count: u8 = Readable::read(reader)?;
68 let mut hops = Vec::with_capacity(hops_count as usize);
69 for _ in 0..hops_count {
71 pubkey: Readable::read(reader)?,
72 short_channel_id: Readable::read(reader)?,
73 fee_msat: Readable::read(reader)?,
74 cltv_expiry_delta: Readable::read(reader)?,
84 struct DirectionalChannelInfo {
85 src_node_id: PublicKey,
88 cltv_expiry_delta: u16,
89 htlc_minimum_msat: u64,
91 fee_proportional_millionths: u32,
92 last_update_message: Option<msgs::ChannelUpdate>,
95 impl std::fmt::Display for DirectionalChannelInfo {
96 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
97 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)?;
102 impl_writeable!(DirectionalChannelInfo, 0, {
109 fee_proportional_millionths,
115 features: ChannelFeatures,
116 one_to_two: DirectionalChannelInfo,
117 two_to_one: DirectionalChannelInfo,
118 //this is cached here so we can send out it later if required by route_init_sync
119 //keep an eye on this to see if the extra memory is a problem
120 announcement_message: Option<msgs::ChannelAnnouncement>,
123 impl std::fmt::Display for ChannelInfo {
124 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
125 write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?;
130 impl_writeable!(ChannelInfo, 0, {
139 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
140 channels: Vec<(u64, Sha256dHash)>,
141 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
144 lowest_inbound_channel_fee_base_msat: u32,
145 lowest_inbound_channel_fee_proportional_millionths: u32,
147 features: NodeFeatures,
151 addresses: Vec<NetAddress>,
152 //this is cached here so we can send out it later if required by route_init_sync
153 //keep an eye on this to see if the extra memory is a problem
154 announcement_message: Option<msgs::NodeAnnouncement>,
157 impl std::fmt::Display for NodeInfo {
158 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
159 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[..])?;
164 impl Writeable for NodeInfo {
165 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
166 (self.channels.len() as u64).write(writer)?;
167 for ref chan in self.channels.iter() {
170 self.lowest_inbound_channel_fee_base_msat.write(writer)?;
171 self.lowest_inbound_channel_fee_proportional_millionths.write(writer)?;
172 self.features.write(writer)?;
173 self.last_update.write(writer)?;
174 self.rgb.write(writer)?;
175 self.alias.write(writer)?;
176 (self.addresses.len() as u64).write(writer)?;
177 for ref addr in &self.addresses {
180 self.announcement_message.write(writer)?;
185 const MAX_ALLOC_SIZE: u64 = 64*1024;
187 impl<R: ::std::io::Read> Readable<R> for NodeInfo {
188 fn read(reader: &mut R) -> Result<NodeInfo, DecodeError> {
189 let channels_count: u64 = Readable::read(reader)?;
190 let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
191 for _ in 0..channels_count {
192 channels.push(Readable::read(reader)?);
194 let lowest_inbound_channel_fee_base_msat = Readable::read(reader)?;
195 let lowest_inbound_channel_fee_proportional_millionths = Readable::read(reader)?;
196 let features = Readable::read(reader)?;
197 let last_update = Readable::read(reader)?;
198 let rgb = Readable::read(reader)?;
199 let alias = Readable::read(reader)?;
200 let addresses_count: u64 = Readable::read(reader)?;
201 let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
202 for _ in 0..addresses_count {
203 match Readable::read(reader) {
204 Ok(Ok(addr)) => { addresses.push(addr); },
205 Ok(Err(_)) => return Err(DecodeError::InvalidValue),
206 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
210 let announcement_message = Readable::read(reader)?;
213 lowest_inbound_channel_fee_base_msat,
214 lowest_inbound_channel_fee_proportional_millionths,
227 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
228 channels: BTreeMap<(u64, Sha256dHash), ChannelInfo>,
229 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
230 channels: BTreeMap<u64, ChannelInfo>,
232 our_node_id: PublicKey,
233 nodes: BTreeMap<PublicKey, NodeInfo>,
236 impl Writeable for NetworkMap {
237 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
238 (self.channels.len() as u64).write(writer)?;
239 for (ref chan_id, ref chan_info) in self.channels.iter() {
240 (*chan_id).write(writer)?;
241 chan_info.write(writer)?;
243 self.our_node_id.write(writer)?;
244 (self.nodes.len() as u64).write(writer)?;
245 for (ref node_id, ref node_info) in self.nodes.iter() {
246 node_id.write(writer)?;
247 node_info.write(writer)?;
253 impl<R: ::std::io::Read> Readable<R> for NetworkMap {
254 fn read(reader: &mut R) -> Result<NetworkMap, DecodeError> {
255 let channels_count: u64 = Readable::read(reader)?;
256 let mut channels = BTreeMap::new();
257 for _ in 0..channels_count {
258 let chan_id: u64 = Readable::read(reader)?;
259 let chan_info = Readable::read(reader)?;
260 channels.insert(chan_id, chan_info);
262 let our_node_id = Readable::read(reader)?;
263 let nodes_count: u64 = Readable::read(reader)?;
264 let mut nodes = BTreeMap::new();
265 for _ in 0..nodes_count {
266 let node_id = Readable::read(reader)?;
267 let node_info = Readable::read(reader)?;
268 nodes.insert(node_id, node_info);
278 struct MutNetworkMap<'a> {
279 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
280 channels: &'a mut BTreeMap<(u64, Sha256dHash), ChannelInfo>,
281 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
282 channels: &'a mut BTreeMap<u64, ChannelInfo>,
283 nodes: &'a mut BTreeMap<PublicKey, NodeInfo>,
286 fn borrow_parts(&mut self) -> MutNetworkMap {
288 channels: &mut self.channels,
289 nodes: &mut self.nodes,
293 impl std::fmt::Display for NetworkMap {
294 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
295 write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
296 for (key, val) in self.channels.iter() {
297 write!(f, " {}: {}\n", key, val)?;
299 write!(f, "[Nodes]\n")?;
300 for (key, val) in self.nodes.iter() {
301 write!(f, " {}: {}\n", log_pubkey!(key), val)?;
308 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
310 fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
311 (short_channel_id, chain_hash)
314 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
316 fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
320 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
322 fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
326 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
328 fn get_short_id(id: &u64) -> &u64 {
333 /// A channel descriptor which provides a last-hop route to get_route
334 pub struct RouteHint {
335 /// The node_id of the non-target end of the route
336 pub src_node_id: PublicKey,
337 /// The short_channel_id of this channel
338 pub short_channel_id: u64,
339 /// The static msat-denominated fee which must be paid to use this channel
340 pub fee_base_msat: u32,
341 /// The dynamic proportional fee which must be paid to use this channel, denominated in
342 /// millionths of the value being forwarded to the next hop.
343 pub fee_proportional_millionths: u32,
344 /// The difference in CLTV values between this node and the next node.
345 pub cltv_expiry_delta: u16,
346 /// The minimum value, in msat, which must be relayed to the next hop.
347 pub htlc_minimum_msat: u64,
350 /// Tracks a view of the network, receiving updates from peers and generating Routes to
351 /// payment destinations.
353 secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
354 network_map: RwLock<NetworkMap>,
355 chain_monitor: Arc<ChainWatchInterface>,
359 const SERIALIZATION_VERSION: u8 = 1;
360 const MIN_SERIALIZATION_VERSION: u8 = 1;
362 impl Writeable for Router {
363 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
364 writer.write_all(&[SERIALIZATION_VERSION; 1])?;
365 writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
367 let network = self.network_map.read().unwrap();
368 network.write(writer)?;
373 /// Arguments for the creation of a Router that are not deserialized.
374 /// At a high-level, the process for deserializing a Router and resuming normal operation is:
375 /// 1) Deserialize the Router by filling in this struct and calling <Router>::read(reaser, args).
376 /// 2) Register the new Router with your ChainWatchInterface
377 pub struct RouterReadArgs {
378 /// The ChainWatchInterface for use in the Router in the future.
380 /// No calls to the ChainWatchInterface will be made during deserialization.
381 pub chain_monitor: Arc<ChainWatchInterface>,
382 /// The Logger for use in the ChannelManager and which may be used to log information during
384 pub logger: Arc<Logger>,
387 impl<R: ::std::io::Read> ReadableArgs<R, RouterReadArgs> for Router {
388 fn read(reader: &mut R, args: RouterReadArgs) -> Result<Router, DecodeError> {
389 let _ver: u8 = Readable::read(reader)?;
390 let min_ver: u8 = Readable::read(reader)?;
391 if min_ver > SERIALIZATION_VERSION {
392 return Err(DecodeError::UnknownVersion);
394 let network_map = Readable::read(reader)?;
396 secp_ctx: Secp256k1::verification_only(),
397 network_map: RwLock::new(network_map),
398 chain_monitor: args.chain_monitor,
404 macro_rules! secp_verify_sig {
405 ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
406 match $secp_ctx.verify($msg, $sig, $pubkey) {
408 Err(_) => return Err(LightningError{err: "Invalid signature from remote node", action: ErrorAction::IgnoreError}),
413 impl RoutingMessageHandler for Router {
414 fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
415 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
416 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
418 let mut network = self.network_map.write().unwrap();
419 match network.nodes.get_mut(&msg.contents.node_id) {
420 None => Err(LightningError{err: "No existing channels for node_announcement", action: ErrorAction::IgnoreError}),
422 if node.last_update >= msg.contents.timestamp {
423 return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
426 node.features = msg.contents.features.clone();
427 node.last_update = msg.contents.timestamp;
428 node.rgb = msg.contents.rgb;
429 node.alias = msg.contents.alias;
430 node.addresses = msg.contents.addresses.clone();
432 let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty();
433 node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
439 fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
440 if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
441 return Err(LightningError{err: "Channel announcement node had a channel with itself", action: ErrorAction::IgnoreError});
444 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
445 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
446 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
447 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
448 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
450 let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
451 Ok((script_pubkey, _value)) => {
452 let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
453 .push_slice(&msg.contents.bitcoin_key_1.serialize())
454 .push_slice(&msg.contents.bitcoin_key_2.serialize())
455 .push_opcode(opcodes::all::OP_PUSHNUM_2)
456 .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
457 if script_pubkey != expected_script {
458 return Err(LightningError{err: "Channel announcement keys didn't match on-chain script", action: ErrorAction::IgnoreError});
460 //TODO: Check if value is worth storing, use it to inform routing, and compare it
461 //to the new HTLC max field in channel_update
464 Err(ChainError::NotSupported) => {
465 // Tentatively accept, potentially exposing us to DoS attacks
468 Err(ChainError::NotWatched) => {
469 return Err(LightningError{err: "Channel announced on an unknown chain", action: ErrorAction::IgnoreError});
471 Err(ChainError::UnknownTx) => {
472 return Err(LightningError{err: "Channel announced without corresponding UTXO entry", action: ErrorAction::IgnoreError});
476 let mut network_lock = self.network_map.write().unwrap();
477 let network = network_lock.borrow_parts();
479 let should_relay = msg.contents.excess_data.is_empty();
481 let chan_info = ChannelInfo {
482 features: msg.contents.features.clone(),
483 one_to_two: DirectionalChannelInfo {
484 src_node_id: msg.contents.node_id_1.clone(),
487 cltv_expiry_delta: u16::max_value(),
488 htlc_minimum_msat: u64::max_value(),
489 fee_base_msat: u32::max_value(),
490 fee_proportional_millionths: u32::max_value(),
491 last_update_message: None,
493 two_to_one: DirectionalChannelInfo {
494 src_node_id: msg.contents.node_id_2.clone(),
497 cltv_expiry_delta: u16::max_value(),
498 htlc_minimum_msat: u64::max_value(),
499 fee_base_msat: u32::max_value(),
500 fee_proportional_millionths: u32::max_value(),
501 last_update_message: None,
503 announcement_message: if should_relay { Some(msg.clone()) } else { None },
506 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
507 BtreeEntry::Occupied(mut entry) => {
508 //TODO: because asking the blockchain if short_channel_id is valid is only optional
509 //in the blockchain API, we need to handle it smartly here, though it's unclear
512 // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
513 // only sometimes returns results. In any case remove the previous entry. Note
514 // that the spec expects us to "blacklist" the node_ids involved, but we can't
516 // a) we don't *require* a UTXO provider that always returns results.
517 // b) we don't track UTXOs of channels we know about and remove them if they
519 // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
520 Self::remove_channel_in_nodes(network.nodes, &entry.get(), msg.contents.short_channel_id);
521 *entry.get_mut() = chan_info;
523 return Err(LightningError{err: "Already have knowledge of channel", action: ErrorAction::IgnoreError})
526 BtreeEntry::Vacant(entry) => {
527 entry.insert(chan_info);
531 macro_rules! add_channel_to_node {
532 ( $node_id: expr ) => {
533 match network.nodes.entry($node_id) {
534 BtreeEntry::Occupied(node_entry) => {
535 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
537 BtreeEntry::Vacant(node_entry) => {
538 node_entry.insert(NodeInfo {
539 channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
540 lowest_inbound_channel_fee_base_msat: u32::max_value(),
541 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
542 features: NodeFeatures::empty(),
546 addresses: Vec::new(),
547 announcement_message: None,
554 add_channel_to_node!(msg.contents.node_id_1);
555 add_channel_to_node!(msg.contents.node_id_2);
560 fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
562 &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
563 let _ = self.handle_channel_update(msg);
565 &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
566 let mut network = self.network_map.write().unwrap();
568 if let Some(chan) = network.channels.remove(short_channel_id) {
569 Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
572 if let Some(chan) = network.channels.get_mut(short_channel_id) {
573 chan.one_to_two.enabled = false;
574 chan.two_to_one.enabled = false;
578 &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
580 //TODO: Wholly remove the node
582 self.mark_node_bad(node_id, false);
588 fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> {
589 let mut network = self.network_map.write().unwrap();
591 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
592 let chan_was_enabled;
594 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
595 None => return Err(LightningError{err: "Couldn't find channel for update", action: ErrorAction::IgnoreError}),
597 macro_rules! maybe_update_channel_info {
598 ( $target: expr) => {
599 if $target.last_update >= msg.contents.timestamp {
600 return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
602 chan_was_enabled = $target.enabled;
603 $target.last_update = msg.contents.timestamp;
604 $target.enabled = chan_enabled;
605 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
606 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
607 $target.fee_base_msat = msg.contents.fee_base_msat;
608 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
609 $target.last_update_message = if msg.contents.excess_data.is_empty() {
616 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
617 if msg.contents.flags & 1 == 1 {
618 dest_node_id = channel.one_to_two.src_node_id.clone();
619 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
620 maybe_update_channel_info!(channel.two_to_one);
622 dest_node_id = channel.two_to_one.src_node_id.clone();
623 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
624 maybe_update_channel_info!(channel.one_to_two);
630 let node = network.nodes.get_mut(&dest_node_id).unwrap();
631 node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
632 node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
633 } else if chan_was_enabled {
634 let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
635 let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
638 let node = network.nodes.get(&dest_node_id).unwrap();
640 for chan_id in node.channels.iter() {
641 let chan = network.channels.get(chan_id).unwrap();
642 if chan.one_to_two.src_node_id == dest_node_id {
643 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
644 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
646 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
647 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
652 //TODO: satisfy the borrow-checker without a double-map-lookup :(
653 let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
654 mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
655 mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
658 Ok(msg.contents.excess_data.is_empty())
662 fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
663 let mut result = Vec::with_capacity(batch_amount as usize);
664 let network = self.network_map.read().unwrap();
665 let mut iter = network.channels.range(starting_point..);
666 while result.len() < batch_amount as usize {
667 if let Some((_, ref chan)) = iter.next() {
668 if chan.announcement_message.is_some() &&
669 chan.one_to_two.last_update_message.is_some() &&
670 chan.two_to_one.last_update_message.is_some() {
671 result.push((chan.announcement_message.clone().unwrap(),
672 chan.one_to_two.last_update_message.clone().unwrap(),
673 chan.two_to_one.last_update_message.clone().unwrap()));
675 // TODO: We may end up sending un-announced channel_updates if we are sending
676 // initial sync data while receiving announce/updates for this channel.
685 fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
686 let mut result = Vec::with_capacity(batch_amount as usize);
687 let network = self.network_map.read().unwrap();
688 let mut iter = if let Some(pubkey) = starting_point {
689 let mut iter = network.nodes.range((*pubkey)..);
693 network.nodes.range(..)
695 while result.len() < batch_amount as usize {
696 if let Some((_, ref node)) = iter.next() {
697 if node.announcement_message.is_some() {
698 result.push(node.announcement_message.clone().unwrap());
708 #[derive(Eq, PartialEq)]
709 struct RouteGraphNode {
711 lowest_fee_to_peer_through_node: u64,
712 lowest_fee_to_node: u64,
715 impl cmp::Ord for RouteGraphNode {
716 fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
717 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
718 .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
722 impl cmp::PartialOrd for RouteGraphNode {
723 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
724 Some(self.cmp(other))
728 struct DummyDirectionalChannelInfo {
729 src_node_id: PublicKey,
730 cltv_expiry_delta: u32,
731 htlc_minimum_msat: u64,
733 fee_proportional_millionths: u32,
737 /// Creates a new router with the given node_id to be used as the source for get_route()
738 pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
739 let mut nodes = BTreeMap::new();
740 nodes.insert(our_pubkey.clone(), NodeInfo {
741 channels: Vec::new(),
742 lowest_inbound_channel_fee_base_msat: u32::max_value(),
743 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
744 features: NodeFeatures::empty(),
748 addresses: Vec::new(),
749 announcement_message: None,
752 secp_ctx: Secp256k1::verification_only(),
753 network_map: RwLock::new(NetworkMap {
754 channels: BTreeMap::new(),
755 our_node_id: our_pubkey,
763 /// Dumps the entire network view of this Router to the logger provided in the constructor at
765 pub fn trace_state(&self) {
766 log_trace!(self, "{}", self.network_map.read().unwrap());
769 /// Get network addresses by node id
770 pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
771 let network = self.network_map.read().unwrap();
772 network.nodes.get(pubkey).map(|n| n.addresses.clone())
775 /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
776 /// with an exponential decay in node "badness". Note that there is deliberately no
777 /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
778 /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
779 /// boolean will reduce the penalty, returning the node to usability faster. If the node is
780 /// behaving correctly, it will disable the failing channel and we will use it again next time.
781 pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
785 fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
786 macro_rules! remove_from_node {
787 ($node_id: expr) => {
788 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
789 entry.get_mut().channels.retain(|chan_id| {
790 short_channel_id != *NetworkMap::get_short_id(chan_id)
792 if entry.get().channels.is_empty() {
793 entry.remove_entry();
796 panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
800 remove_from_node!(chan.one_to_two.src_node_id);
801 remove_from_node!(chan.two_to_one.src_node_id);
804 /// Gets a route from us to the given target node.
806 /// Extra routing hops between known nodes and the target will be used if they are included in
809 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
810 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
811 /// (this Router's) view of our local channels will be ignored, and only those in first_hops
814 /// Panics if first_hops contains channels without short_channel_ids
815 /// (ChannelManager::list_usable_channels will never include such channels).
817 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
818 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
819 /// *is* checked as they may change based on the receiving node.
820 pub fn get_route(&self, target: &PublicKey, first_hops: Option<&[channelmanager::ChannelDetails]>, last_hops: &[RouteHint], final_value_msat: u64, final_cltv: u32) -> Result<Route, LightningError> {
821 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
822 // uptime/success in using a node in the past.
823 let network = self.network_map.read().unwrap();
825 if *target == network.our_node_id {
826 return Err(LightningError{err: "Cannot generate a route to ourselves", action: ErrorAction::IgnoreError});
829 if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
830 return Err(LightningError{err: "Cannot generate a route of more value than all existing satoshis", action: ErrorAction::IgnoreError});
833 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
834 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
835 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
836 // to use as the A* heuristic beyond just the cost to get one node further than the current
839 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
840 src_node_id: network.our_node_id.clone(),
841 cltv_expiry_delta: 0,
842 htlc_minimum_msat: 0,
844 fee_proportional_millionths: 0,
847 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
848 let mut dist = HashMap::with_capacity(network.nodes.len());
850 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
851 if let Some(hops) = first_hops {
853 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
854 if chan.remote_network_id == *target {
856 hops: vec![RouteHop {
857 pubkey: chan.remote_network_id,
859 fee_msat: final_value_msat,
860 cltv_expiry_delta: final_cltv,
864 first_hop_targets.insert(chan.remote_network_id, short_channel_id);
866 if first_hop_targets.is_empty() {
867 return Err(LightningError{err: "Cannot route when there are no outbound routes away from us", action: ErrorAction::IgnoreError});
871 macro_rules! add_entry {
872 // Adds entry which goes from the node pointed to by $directional_info to
873 // $dest_node_id over the channel with id $chan_id with fees described in
874 // $directional_info.
875 ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $starting_fee_msat: expr ) => {
876 //TODO: Explore simply adding fee to hit htlc_minimum_msat
877 if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
878 let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
879 if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
880 ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
882 let mut total_fee = $starting_fee_msat as u64;
883 let hm_entry = dist.entry(&$directional_info.src_node_id);
884 let old_entry = hm_entry.or_insert_with(|| {
885 let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
887 node.lowest_inbound_channel_fee_base_msat,
888 node.lowest_inbound_channel_fee_proportional_millionths,
890 pubkey: $dest_node_id.clone(),
893 cltv_expiry_delta: 0,
896 if $directional_info.src_node_id != network.our_node_id {
897 // Ignore new_fee for channel-from-us as we assume all channels-from-us
898 // will have the same effective-fee
899 total_fee += new_fee;
900 if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
901 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
903 // max_value means we'll always fail the old_entry.0 > total_fee check
904 total_fee = u64::max_value();
907 let new_graph_node = RouteGraphNode {
908 pubkey: $directional_info.src_node_id,
909 lowest_fee_to_peer_through_node: total_fee,
910 lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
912 if old_entry.0 > total_fee {
913 targets.push(new_graph_node);
914 old_entry.0 = total_fee;
915 old_entry.3 = RouteHop {
916 pubkey: $dest_node_id.clone(),
917 short_channel_id: $chan_id.clone(),
918 fee_msat: new_fee, // This field is ignored on the last-hop anyway
919 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
927 macro_rules! add_entries_to_cheapest_to_target_node {
928 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
929 if first_hops.is_some() {
930 if let Some(first_hop) = first_hop_targets.get(&$node_id) {
931 add_entry!(first_hop, $node_id, dummy_directional_info, $fee_to_target_msat);
935 if !$node.features.requires_unknown_bits() {
936 for chan_id in $node.channels.iter() {
937 let chan = network.channels.get(chan_id).unwrap();
938 if !chan.features.requires_unknown_bits() {
939 if chan.one_to_two.src_node_id == *$node_id {
940 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
941 if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
942 if chan.two_to_one.enabled {
943 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
947 if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
948 if chan.one_to_two.enabled {
949 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
959 match network.nodes.get(target) {
962 add_entries_to_cheapest_to_target_node!(node, target, 0);
966 for hop in last_hops.iter() {
967 if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
968 if network.nodes.get(&hop.src_node_id).is_some() {
969 if first_hops.is_some() {
970 if let Some(first_hop) = first_hop_targets.get(&hop.src_node_id) {
971 add_entry!(first_hop, hop.src_node_id, dummy_directional_info, 0);
974 add_entry!(hop.short_channel_id, target, hop, 0);
979 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
980 if pubkey == network.our_node_id {
981 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
982 while res.last().unwrap().pubkey != *target {
983 let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
985 None => return Err(LightningError{err: "Failed to find a non-fee-overflowing path to the given destination", action: ErrorAction::IgnoreError}),
987 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
988 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
991 res.last_mut().unwrap().fee_msat = final_value_msat;
992 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
993 let route = Route { hops: res };
994 log_trace!(self, "Got route: {}", log_route!(route));
998 match network.nodes.get(&pubkey) {
1001 add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
1006 Err(LightningError{err: "Failed to find a path to the given destination", action: ErrorAction::IgnoreError})
1012 use chain::chaininterface;
1013 use ln::channelmanager;
1014 use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
1015 use ln::features::{ChannelFeatures, NodeFeatures};
1016 use ln::msgs::{LightningError, ErrorAction};
1017 use util::test_utils;
1018 use util::test_utils::TestVecWriter;
1019 use util::logger::Logger;
1020 use util::ser::{Writeable, Readable};
1022 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1023 use bitcoin_hashes::Hash;
1024 use bitcoin::network::constants::Network;
1028 use secp256k1::key::{PublicKey,SecretKey};
1029 use secp256k1::Secp256k1;
1035 let secp_ctx = Secp256k1::new();
1036 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
1037 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1038 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
1039 let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
1041 // Build network from our_id to node8:
1043 // -1(1)2- node1 -1(3)2-
1045 // our_id -1(12)2- node8 -1(13)2--- node3
1047 // -1(2)2- node2 -1(4)2-
1050 // chan1 1-to-2: disabled
1051 // chan1 2-to-1: enabled, 0 fee
1053 // chan2 1-to-2: enabled, ignored fee
1054 // chan2 2-to-1: enabled, 0 fee
1056 // chan3 1-to-2: enabled, 0 fee
1057 // chan3 2-to-1: enabled, 100 msat fee
1059 // chan4 1-to-2: enabled, 100% fee
1060 // chan4 2-to-1: enabled, 0 fee
1062 // chan12 1-to-2: enabled, ignored fee
1063 // chan12 2-to-1: enabled, 0 fee
1065 // chan13 1-to-2: enabled, 200% fee
1066 // chan13 2-to-1: enabled, 0 fee
1069 // -1(5)2- node4 -1(8)2--
1073 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
1075 // -1(7)2- node6 -1(10)2-
1077 // chan5 1-to-2: enabled, 100 msat fee
1078 // chan5 2-to-1: enabled, 0 fee
1080 // chan6 1-to-2: enabled, 0 fee
1081 // chan6 2-to-1: enabled, 0 fee
1083 // chan7 1-to-2: enabled, 100% fee
1084 // chan7 2-to-1: enabled, 0 fee
1086 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
1087 // chan8 2-to-1: enabled, 0 fee
1089 // chan9 1-to-2: enabled, 1001 msat fee
1090 // chan9 2-to-1: enabled, 0 fee
1092 // chan10 1-to-2: enabled, 0 fee
1093 // chan10 2-to-1: enabled, 0 fee
1095 // chan11 1-to-2: enabled, 0 fee
1096 // chan11 2-to-1: enabled, 0 fee
1098 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
1099 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
1100 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
1101 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
1102 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
1103 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
1104 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
1105 let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
1107 let zero_hash = Sha256dHash::hash(&[0; 32]);
1110 let mut network = router.network_map.write().unwrap();
1112 network.nodes.insert(node1.clone(), NodeInfo {
1113 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
1114 lowest_inbound_channel_fee_base_msat: 100,
1115 lowest_inbound_channel_fee_proportional_millionths: 0,
1116 features: NodeFeatures::empty(),
1120 addresses: Vec::new(),
1121 announcement_message: None,
1123 network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
1124 features: ChannelFeatures::empty(),
1125 one_to_two: DirectionalChannelInfo {
1126 src_node_id: our_id.clone(),
1129 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1130 htlc_minimum_msat: 0,
1131 fee_base_msat: u32::max_value(), // This value should be ignored
1132 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1133 last_update_message: None,
1134 }, two_to_one: DirectionalChannelInfo {
1135 src_node_id: node1.clone(),
1138 cltv_expiry_delta: 0,
1139 htlc_minimum_msat: 0,
1141 fee_proportional_millionths: 0,
1142 last_update_message: None,
1144 announcement_message: None,
1146 network.nodes.insert(node2.clone(), NodeInfo {
1147 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
1148 lowest_inbound_channel_fee_base_msat: 0,
1149 lowest_inbound_channel_fee_proportional_millionths: 0,
1150 features: NodeFeatures::empty(),
1154 addresses: Vec::new(),
1155 announcement_message: None,
1157 network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
1158 features: ChannelFeatures::empty(),
1159 one_to_two: DirectionalChannelInfo {
1160 src_node_id: our_id.clone(),
1163 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1164 htlc_minimum_msat: 0,
1165 fee_base_msat: u32::max_value(), // This value should be ignored
1166 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1167 last_update_message: None,
1168 }, two_to_one: DirectionalChannelInfo {
1169 src_node_id: node2.clone(),
1172 cltv_expiry_delta: 0,
1173 htlc_minimum_msat: 0,
1175 fee_proportional_millionths: 0,
1176 last_update_message: None,
1178 announcement_message: None,
1180 network.nodes.insert(node8.clone(), NodeInfo {
1181 channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
1182 lowest_inbound_channel_fee_base_msat: 0,
1183 lowest_inbound_channel_fee_proportional_millionths: 0,
1184 features: NodeFeatures::empty(),
1188 addresses: Vec::new(),
1189 announcement_message: None,
1191 network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
1192 features: ChannelFeatures::empty(),
1193 one_to_two: DirectionalChannelInfo {
1194 src_node_id: our_id.clone(),
1197 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1198 htlc_minimum_msat: 0,
1199 fee_base_msat: u32::max_value(), // This value should be ignored
1200 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1201 last_update_message: None,
1202 }, two_to_one: DirectionalChannelInfo {
1203 src_node_id: node8.clone(),
1206 cltv_expiry_delta: 0,
1207 htlc_minimum_msat: 0,
1209 fee_proportional_millionths: 0,
1210 last_update_message: None,
1212 announcement_message: None,
1214 network.nodes.insert(node3.clone(), NodeInfo {
1216 NetworkMap::get_key(3, zero_hash.clone()),
1217 NetworkMap::get_key(4, zero_hash.clone()),
1218 NetworkMap::get_key(13, zero_hash.clone()),
1219 NetworkMap::get_key(5, zero_hash.clone()),
1220 NetworkMap::get_key(6, zero_hash.clone()),
1221 NetworkMap::get_key(7, zero_hash.clone())),
1222 lowest_inbound_channel_fee_base_msat: 0,
1223 lowest_inbound_channel_fee_proportional_millionths: 0,
1224 features: NodeFeatures::empty(),
1228 addresses: Vec::new(),
1229 announcement_message: None,
1231 network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
1232 features: ChannelFeatures::empty(),
1233 one_to_two: DirectionalChannelInfo {
1234 src_node_id: node1.clone(),
1237 cltv_expiry_delta: (3 << 8) | 1,
1238 htlc_minimum_msat: 0,
1240 fee_proportional_millionths: 0,
1241 last_update_message: None,
1242 }, two_to_one: DirectionalChannelInfo {
1243 src_node_id: node3.clone(),
1246 cltv_expiry_delta: (3 << 8) | 2,
1247 htlc_minimum_msat: 0,
1249 fee_proportional_millionths: 0,
1250 last_update_message: None,
1252 announcement_message: None,
1254 network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
1255 features: ChannelFeatures::empty(),
1256 one_to_two: DirectionalChannelInfo {
1257 src_node_id: node2.clone(),
1260 cltv_expiry_delta: (4 << 8) | 1,
1261 htlc_minimum_msat: 0,
1263 fee_proportional_millionths: 1000000,
1264 last_update_message: None,
1265 }, two_to_one: DirectionalChannelInfo {
1266 src_node_id: node3.clone(),
1269 cltv_expiry_delta: (4 << 8) | 2,
1270 htlc_minimum_msat: 0,
1272 fee_proportional_millionths: 0,
1273 last_update_message: None,
1275 announcement_message: None,
1277 network.channels.insert(NetworkMap::get_key(13, zero_hash.clone()), ChannelInfo {
1278 features: ChannelFeatures::empty(),
1279 one_to_two: DirectionalChannelInfo {
1280 src_node_id: node8.clone(),
1283 cltv_expiry_delta: (13 << 8) | 1,
1284 htlc_minimum_msat: 0,
1286 fee_proportional_millionths: 2000000,
1287 last_update_message: None,
1288 }, two_to_one: DirectionalChannelInfo {
1289 src_node_id: node3.clone(),
1292 cltv_expiry_delta: (13 << 8) | 2,
1293 htlc_minimum_msat: 0,
1295 fee_proportional_millionths: 0,
1296 last_update_message: None,
1298 announcement_message: None,
1300 network.nodes.insert(node4.clone(), NodeInfo {
1301 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1302 lowest_inbound_channel_fee_base_msat: 0,
1303 lowest_inbound_channel_fee_proportional_millionths: 0,
1304 features: NodeFeatures::empty(),
1308 addresses: Vec::new(),
1309 announcement_message: None,
1311 network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
1312 features: ChannelFeatures::empty(),
1313 one_to_two: DirectionalChannelInfo {
1314 src_node_id: node3.clone(),
1317 cltv_expiry_delta: (5 << 8) | 1,
1318 htlc_minimum_msat: 0,
1320 fee_proportional_millionths: 0,
1321 last_update_message: None,
1322 }, two_to_one: DirectionalChannelInfo {
1323 src_node_id: node4.clone(),
1326 cltv_expiry_delta: (5 << 8) | 2,
1327 htlc_minimum_msat: 0,
1329 fee_proportional_millionths: 0,
1330 last_update_message: None,
1332 announcement_message: None,
1334 network.nodes.insert(node5.clone(), NodeInfo {
1335 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1336 lowest_inbound_channel_fee_base_msat: 0,
1337 lowest_inbound_channel_fee_proportional_millionths: 0,
1338 features: NodeFeatures::empty(),
1342 addresses: Vec::new(),
1343 announcement_message: None,
1345 network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
1346 features: ChannelFeatures::empty(),
1347 one_to_two: DirectionalChannelInfo {
1348 src_node_id: node3.clone(),
1351 cltv_expiry_delta: (6 << 8) | 1,
1352 htlc_minimum_msat: 0,
1354 fee_proportional_millionths: 0,
1355 last_update_message: None,
1356 }, two_to_one: DirectionalChannelInfo {
1357 src_node_id: node5.clone(),
1360 cltv_expiry_delta: (6 << 8) | 2,
1361 htlc_minimum_msat: 0,
1363 fee_proportional_millionths: 0,
1364 last_update_message: None,
1366 announcement_message: None,
1368 network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
1369 features: ChannelFeatures::empty(),
1370 one_to_two: DirectionalChannelInfo {
1371 src_node_id: node5.clone(),
1374 cltv_expiry_delta: (11 << 8) | 1,
1375 htlc_minimum_msat: 0,
1377 fee_proportional_millionths: 0,
1378 last_update_message: None,
1379 }, two_to_one: DirectionalChannelInfo {
1380 src_node_id: node4.clone(),
1383 cltv_expiry_delta: (11 << 8) | 2,
1384 htlc_minimum_msat: 0,
1386 fee_proportional_millionths: 0,
1387 last_update_message: None,
1389 announcement_message: None,
1391 network.nodes.insert(node6.clone(), NodeInfo {
1392 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
1393 lowest_inbound_channel_fee_base_msat: 0,
1394 lowest_inbound_channel_fee_proportional_millionths: 0,
1395 features: NodeFeatures::empty(),
1399 addresses: Vec::new(),
1400 announcement_message: None,
1402 network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
1403 features: ChannelFeatures::empty(),
1404 one_to_two: DirectionalChannelInfo {
1405 src_node_id: node3.clone(),
1408 cltv_expiry_delta: (7 << 8) | 1,
1409 htlc_minimum_msat: 0,
1411 fee_proportional_millionths: 1000000,
1412 last_update_message: None,
1413 }, two_to_one: DirectionalChannelInfo {
1414 src_node_id: node6.clone(),
1417 cltv_expiry_delta: (7 << 8) | 2,
1418 htlc_minimum_msat: 0,
1420 fee_proportional_millionths: 0,
1421 last_update_message: None,
1423 announcement_message: None,
1427 { // Simple route to 3 via 2
1428 let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
1429 assert_eq!(route.hops.len(), 2);
1431 assert_eq!(route.hops[0].pubkey, node2);
1432 assert_eq!(route.hops[0].short_channel_id, 2);
1433 assert_eq!(route.hops[0].fee_msat, 100);
1434 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1436 assert_eq!(route.hops[1].pubkey, node3);
1437 assert_eq!(route.hops[1].short_channel_id, 4);
1438 assert_eq!(route.hops[1].fee_msat, 100);
1439 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1442 { // Disable channels 4 and 12 by requiring unknown feature bits
1443 let mut network = router.network_map.write().unwrap();
1444 network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1445 network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1448 { // If all the channels require some features we don't understand, route should fail
1449 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1450 assert_eq!(err, "Failed to find a path to the given destination");
1451 } else { panic!(); }
1454 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1455 let our_chans = vec![channelmanager::ChannelDetails {
1456 channel_id: [0; 32],
1457 short_channel_id: Some(42),
1458 remote_network_id: node8.clone(),
1459 channel_value_satoshis: 0,
1461 outbound_capacity_msat: 0,
1462 inbound_capacity_msat: 0,
1465 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1466 assert_eq!(route.hops.len(), 2);
1468 assert_eq!(route.hops[0].pubkey, node8);
1469 assert_eq!(route.hops[0].short_channel_id, 42);
1470 assert_eq!(route.hops[0].fee_msat, 200);
1471 assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1473 assert_eq!(route.hops[1].pubkey, node3);
1474 assert_eq!(route.hops[1].short_channel_id, 13);
1475 assert_eq!(route.hops[1].fee_msat, 100);
1476 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1479 { // Re-enable channels 4 and 12 by wiping the unknown feature bits
1480 let mut network = router.network_map.write().unwrap();
1481 network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1482 network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1485 { // Disable nodes 1, 2, and 8 by requiring unknown feature bits
1486 let mut network = router.network_map.write().unwrap();
1487 network.nodes.get_mut(&node1).unwrap().features.set_require_unknown_bits();
1488 network.nodes.get_mut(&node2).unwrap().features.set_require_unknown_bits();
1489 network.nodes.get_mut(&node8).unwrap().features.set_require_unknown_bits();
1492 { // If all nodes require some features we don't understand, route should fail
1493 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1494 assert_eq!(err, "Failed to find a path to the given destination");
1495 } else { panic!(); }
1498 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1499 let our_chans = vec![channelmanager::ChannelDetails {
1500 channel_id: [0; 32],
1501 short_channel_id: Some(42),
1502 remote_network_id: node8.clone(),
1503 channel_value_satoshis: 0,
1505 outbound_capacity_msat: 0,
1506 inbound_capacity_msat: 0,
1509 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1510 assert_eq!(route.hops.len(), 2);
1512 assert_eq!(route.hops[0].pubkey, node8);
1513 assert_eq!(route.hops[0].short_channel_id, 42);
1514 assert_eq!(route.hops[0].fee_msat, 200);
1515 assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1517 assert_eq!(route.hops[1].pubkey, node3);
1518 assert_eq!(route.hops[1].short_channel_id, 13);
1519 assert_eq!(route.hops[1].fee_msat, 100);
1520 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1523 { // Re-enable nodes 1, 2, and 8
1524 let mut network = router.network_map.write().unwrap();
1525 network.nodes.get_mut(&node1).unwrap().features.clear_require_unknown_bits();
1526 network.nodes.get_mut(&node2).unwrap().features.clear_require_unknown_bits();
1527 network.nodes.get_mut(&node8).unwrap().features.clear_require_unknown_bits();
1530 // Note that we don't test disabling node 3 and failing to route to it, as we (somewhat
1531 // naively) assume that the user checked the feature bits on the invoice, which override
1532 // the node_announcement.
1534 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
1535 let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
1536 assert_eq!(route.hops.len(), 3);
1538 assert_eq!(route.hops[0].pubkey, node2);
1539 assert_eq!(route.hops[0].short_channel_id, 2);
1540 assert_eq!(route.hops[0].fee_msat, 200);
1541 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1543 assert_eq!(route.hops[1].pubkey, node3);
1544 assert_eq!(route.hops[1].short_channel_id, 4);
1545 assert_eq!(route.hops[1].fee_msat, 100);
1546 assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
1548 assert_eq!(route.hops[2].pubkey, node1);
1549 assert_eq!(route.hops[2].short_channel_id, 3);
1550 assert_eq!(route.hops[2].fee_msat, 100);
1551 assert_eq!(route.hops[2].cltv_expiry_delta, 42);
1554 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1555 let our_chans = vec![channelmanager::ChannelDetails {
1556 channel_id: [0; 32],
1557 short_channel_id: Some(42),
1558 remote_network_id: node8.clone(),
1559 channel_value_satoshis: 0,
1561 outbound_capacity_msat: 0,
1562 inbound_capacity_msat: 0,
1565 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1566 assert_eq!(route.hops.len(), 2);
1568 assert_eq!(route.hops[0].pubkey, node8);
1569 assert_eq!(route.hops[0].short_channel_id, 42);
1570 assert_eq!(route.hops[0].fee_msat, 200);
1571 assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1573 assert_eq!(route.hops[1].pubkey, node3);
1574 assert_eq!(route.hops[1].short_channel_id, 13);
1575 assert_eq!(route.hops[1].fee_msat, 100);
1576 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1579 let mut last_hops = vec!(RouteHint {
1580 src_node_id: node4.clone(),
1581 short_channel_id: 8,
1583 fee_proportional_millionths: 0,
1584 cltv_expiry_delta: (8 << 8) | 1,
1585 htlc_minimum_msat: 0,
1587 src_node_id: node5.clone(),
1588 short_channel_id: 9,
1589 fee_base_msat: 1001,
1590 fee_proportional_millionths: 0,
1591 cltv_expiry_delta: (9 << 8) | 1,
1592 htlc_minimum_msat: 0,
1594 src_node_id: node6.clone(),
1595 short_channel_id: 10,
1597 fee_proportional_millionths: 0,
1598 cltv_expiry_delta: (10 << 8) | 1,
1599 htlc_minimum_msat: 0,
1602 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
1603 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1604 assert_eq!(route.hops.len(), 5);
1606 assert_eq!(route.hops[0].pubkey, node2);
1607 assert_eq!(route.hops[0].short_channel_id, 2);
1608 assert_eq!(route.hops[0].fee_msat, 100);
1609 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1611 assert_eq!(route.hops[1].pubkey, node3);
1612 assert_eq!(route.hops[1].short_channel_id, 4);
1613 assert_eq!(route.hops[1].fee_msat, 0);
1614 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1616 assert_eq!(route.hops[2].pubkey, node5);
1617 assert_eq!(route.hops[2].short_channel_id, 6);
1618 assert_eq!(route.hops[2].fee_msat, 0);
1619 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1621 assert_eq!(route.hops[3].pubkey, node4);
1622 assert_eq!(route.hops[3].short_channel_id, 11);
1623 assert_eq!(route.hops[3].fee_msat, 0);
1624 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1626 assert_eq!(route.hops[4].pubkey, node7);
1627 assert_eq!(route.hops[4].short_channel_id, 8);
1628 assert_eq!(route.hops[4].fee_msat, 100);
1629 assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1632 { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1633 let our_chans = vec![channelmanager::ChannelDetails {
1634 channel_id: [0; 32],
1635 short_channel_id: Some(42),
1636 remote_network_id: node4.clone(),
1637 channel_value_satoshis: 0,
1639 outbound_capacity_msat: 0,
1640 inbound_capacity_msat: 0,
1643 let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
1644 assert_eq!(route.hops.len(), 2);
1646 assert_eq!(route.hops[0].pubkey, node4);
1647 assert_eq!(route.hops[0].short_channel_id, 42);
1648 assert_eq!(route.hops[0].fee_msat, 0);
1649 assert_eq!(route.hops[0].cltv_expiry_delta, (8 << 8) | 1);
1651 assert_eq!(route.hops[1].pubkey, node7);
1652 assert_eq!(route.hops[1].short_channel_id, 8);
1653 assert_eq!(route.hops[1].fee_msat, 100);
1654 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1657 last_hops[0].fee_base_msat = 1000;
1659 { // Revert to via 6 as the fee on 8 goes up
1660 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1661 assert_eq!(route.hops.len(), 4);
1663 assert_eq!(route.hops[0].pubkey, node2);
1664 assert_eq!(route.hops[0].short_channel_id, 2);
1665 assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
1666 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1668 assert_eq!(route.hops[1].pubkey, node3);
1669 assert_eq!(route.hops[1].short_channel_id, 4);
1670 assert_eq!(route.hops[1].fee_msat, 100);
1671 assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
1673 assert_eq!(route.hops[2].pubkey, node6);
1674 assert_eq!(route.hops[2].short_channel_id, 7);
1675 assert_eq!(route.hops[2].fee_msat, 0);
1676 assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
1678 assert_eq!(route.hops[3].pubkey, node7);
1679 assert_eq!(route.hops[3].short_channel_id, 10);
1680 assert_eq!(route.hops[3].fee_msat, 100);
1681 assert_eq!(route.hops[3].cltv_expiry_delta, 42);
1684 { // ...but still use 8 for larger payments as 6 has a variable feerate
1685 let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
1686 assert_eq!(route.hops.len(), 5);
1688 assert_eq!(route.hops[0].pubkey, node2);
1689 assert_eq!(route.hops[0].short_channel_id, 2);
1690 assert_eq!(route.hops[0].fee_msat, 3000);
1691 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1693 assert_eq!(route.hops[1].pubkey, node3);
1694 assert_eq!(route.hops[1].short_channel_id, 4);
1695 assert_eq!(route.hops[1].fee_msat, 0);
1696 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1698 assert_eq!(route.hops[2].pubkey, node5);
1699 assert_eq!(route.hops[2].short_channel_id, 6);
1700 assert_eq!(route.hops[2].fee_msat, 0);
1701 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1703 assert_eq!(route.hops[3].pubkey, node4);
1704 assert_eq!(route.hops[3].short_channel_id, 11);
1705 assert_eq!(route.hops[3].fee_msat, 1000);
1706 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1708 assert_eq!(route.hops[4].pubkey, node7);
1709 assert_eq!(route.hops[4].short_channel_id, 8);
1710 assert_eq!(route.hops[4].fee_msat, 2000);
1711 assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1714 { // Test Router serialization/deserialization
1715 let mut w = TestVecWriter(Vec::new());
1716 let network = router.network_map.read().unwrap();
1717 assert!(!network.channels.is_empty());
1718 assert!(!network.nodes.is_empty());
1719 network.write(&mut w).unwrap();
1720 assert!(<NetworkMap>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);