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::sync::atomic::{AtomicUsize, Ordering};
26 use std::collections::{HashMap,BinaryHeap,BTreeMap};
27 use std::collections::btree_map::Entry as BtreeEntry;
31 #[derive(Clone, PartialEq)]
33 /// The node_id of the node at this hop.
34 pub pubkey: PublicKey,
35 /// The node_announcement features of the node at this hop. For the last hop, these may be
36 /// amended to match the features present in the invoice this node generated.
37 pub node_features: NodeFeatures,
38 /// The channel that should be used from the previous hop to reach this node.
39 pub short_channel_id: u64,
40 /// The channel_announcement features of the channel that should be used from the previous hop
41 /// to reach this node.
42 pub channel_features: ChannelFeatures,
43 /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
45 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
46 /// expected at the destination, in excess of the current block height.
47 pub cltv_expiry_delta: u32,
50 impl Writeable for Vec<RouteHop> {
51 fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
52 (self.len() as u8).write(writer)?;
53 for hop in self.iter() {
54 hop.pubkey.write(writer)?;
55 hop.node_features.write(writer)?;
56 hop.short_channel_id.write(writer)?;
57 hop.channel_features.write(writer)?;
58 hop.fee_msat.write(writer)?;
59 hop.cltv_expiry_delta.write(writer)?;
65 impl Readable for Vec<RouteHop> {
66 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Vec<RouteHop>, 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 node_features: Readable::read(reader)?,
73 short_channel_id: Readable::read(reader)?,
74 channel_features: Readable::read(reader)?,
75 fee_msat: Readable::read(reader)?,
76 cltv_expiry_delta: Readable::read(reader)?,
83 /// A route from us through the network to a destination
84 #[derive(Clone, PartialEq)]
86 /// The list of routes taken for a single (potentially-multi-)path payment. The pubkey of the
87 /// last RouteHop in each path must be the same.
88 /// Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
89 /// destination. Thus, this must always be at least length one. While the maximum length of any
90 /// given path is variable, keeping the length of any path to less than 20 should currently
91 /// ensure it is viable.
92 pub paths: Vec<Vec<RouteHop>>,
95 impl Writeable for Route {
96 fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
97 (self.paths.len() as u64).write(writer)?;
98 for hops in self.paths.iter() {
105 impl Readable for Route {
106 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Route, DecodeError> {
107 let path_count: u64 = Readable::read(reader)?;
108 let mut paths = Vec::with_capacity(cmp::min(path_count, 128) as usize);
109 for _ in 0..path_count {
110 paths.push(Readable::read(reader)?);
117 struct DirectionalChannelInfo {
118 src_node_id: PublicKey,
121 cltv_expiry_delta: u16,
122 htlc_minimum_msat: u64,
124 fee_proportional_millionths: u32,
125 last_update_message: Option<msgs::ChannelUpdate>,
128 impl std::fmt::Display for DirectionalChannelInfo {
129 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
130 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)?;
135 impl_writeable!(DirectionalChannelInfo, 0, {
142 fee_proportional_millionths,
148 features: ChannelFeatures,
149 one_to_two: DirectionalChannelInfo,
150 two_to_one: DirectionalChannelInfo,
151 //this is cached here so we can send out it later if required by route_init_sync
152 //keep an eye on this to see if the extra memory is a problem
153 announcement_message: Option<msgs::ChannelAnnouncement>,
156 impl std::fmt::Display for ChannelInfo {
157 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
158 write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?;
163 impl_writeable!(ChannelInfo, 0, {
172 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
173 channels: Vec<(u64, Sha256dHash)>,
174 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
177 lowest_inbound_channel_fee_base_msat: u32,
178 lowest_inbound_channel_fee_proportional_millionths: u32,
180 features: NodeFeatures,
181 /// Unlike for channels, we may have a NodeInfo entry before having received a node_update.
182 /// Thus, we have to be able to capture "no update has been received", which we do with an
184 last_update: Option<u32>,
187 addresses: Vec<NetAddress>,
188 //this is cached here so we can send out it later if required by route_init_sync
189 //keep an eye on this to see if the extra memory is a problem
190 announcement_message: Option<msgs::NodeAnnouncement>,
193 impl std::fmt::Display for NodeInfo {
194 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
195 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[..])?;
200 impl Writeable for NodeInfo {
201 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
202 (self.channels.len() as u64).write(writer)?;
203 for ref chan in self.channels.iter() {
206 self.lowest_inbound_channel_fee_base_msat.write(writer)?;
207 self.lowest_inbound_channel_fee_proportional_millionths.write(writer)?;
208 self.features.write(writer)?;
209 self.last_update.write(writer)?;
210 self.rgb.write(writer)?;
211 self.alias.write(writer)?;
212 (self.addresses.len() as u64).write(writer)?;
213 for ref addr in &self.addresses {
216 self.announcement_message.write(writer)?;
221 const MAX_ALLOC_SIZE: u64 = 64*1024;
223 impl Readable for NodeInfo {
224 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeInfo, DecodeError> {
225 let channels_count: u64 = Readable::read(reader)?;
226 let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
227 for _ in 0..channels_count {
228 channels.push(Readable::read(reader)?);
230 let lowest_inbound_channel_fee_base_msat = Readable::read(reader)?;
231 let lowest_inbound_channel_fee_proportional_millionths = Readable::read(reader)?;
232 let features = Readable::read(reader)?;
233 let last_update = Readable::read(reader)?;
234 let rgb = Readable::read(reader)?;
235 let alias = Readable::read(reader)?;
236 let addresses_count: u64 = Readable::read(reader)?;
237 let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
238 for _ in 0..addresses_count {
239 match Readable::read(reader) {
240 Ok(Ok(addr)) => { addresses.push(addr); },
241 Ok(Err(_)) => return Err(DecodeError::InvalidValue),
242 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
246 let announcement_message = Readable::read(reader)?;
249 lowest_inbound_channel_fee_base_msat,
250 lowest_inbound_channel_fee_proportional_millionths,
263 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
264 channels: BTreeMap<(u64, Sha256dHash), ChannelInfo>,
265 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
266 channels: BTreeMap<u64, ChannelInfo>,
268 our_node_id: PublicKey,
269 nodes: BTreeMap<PublicKey, NodeInfo>,
272 impl Writeable for NetworkMap {
273 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
274 (self.channels.len() as u64).write(writer)?;
275 for (ref chan_id, ref chan_info) in self.channels.iter() {
276 (*chan_id).write(writer)?;
277 chan_info.write(writer)?;
279 self.our_node_id.write(writer)?;
280 (self.nodes.len() as u64).write(writer)?;
281 for (ref node_id, ref node_info) in self.nodes.iter() {
282 node_id.write(writer)?;
283 node_info.write(writer)?;
289 impl Readable for NetworkMap {
290 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NetworkMap, DecodeError> {
291 let channels_count: u64 = Readable::read(reader)?;
292 let mut channels = BTreeMap::new();
293 for _ in 0..channels_count {
294 let chan_id: u64 = Readable::read(reader)?;
295 let chan_info = Readable::read(reader)?;
296 channels.insert(chan_id, chan_info);
298 let our_node_id = Readable::read(reader)?;
299 let nodes_count: u64 = Readable::read(reader)?;
300 let mut nodes = BTreeMap::new();
301 for _ in 0..nodes_count {
302 let node_id = Readable::read(reader)?;
303 let node_info = Readable::read(reader)?;
304 nodes.insert(node_id, node_info);
314 impl std::fmt::Display for NetworkMap {
315 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
316 write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
317 for (key, val) in self.channels.iter() {
318 write!(f, " {}: {}\n", key, val)?;
320 write!(f, "[Nodes]\n")?;
321 for (key, val) in self.nodes.iter() {
322 write!(f, " {}: {}\n", log_pubkey!(key), val)?;
329 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
331 fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
332 (short_channel_id, chain_hash)
335 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
337 fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
341 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
343 fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
347 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
349 fn get_short_id(id: &u64) -> &u64 {
354 /// A channel descriptor which provides a last-hop route to get_route
355 pub struct RouteHint {
356 /// The node_id of the non-target end of the route
357 pub src_node_id: PublicKey,
358 /// The short_channel_id of this channel
359 pub short_channel_id: u64,
360 /// The static msat-denominated fee which must be paid to use this channel
361 pub fee_base_msat: u32,
362 /// The dynamic proportional fee which must be paid to use this channel, denominated in
363 /// millionths of the value being forwarded to the next hop.
364 pub fee_proportional_millionths: u32,
365 /// The difference in CLTV values between this node and the next node.
366 pub cltv_expiry_delta: u16,
367 /// The minimum value, in msat, which must be relayed to the next hop.
368 pub htlc_minimum_msat: u64,
371 /// Tracks a view of the network, receiving updates from peers and generating Routes to
372 /// payment destinations.
374 secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
375 network_map: RwLock<NetworkMap>,
376 full_syncs_requested: AtomicUsize,
377 chain_monitor: Arc<ChainWatchInterface>,
381 const SERIALIZATION_VERSION: u8 = 1;
382 const MIN_SERIALIZATION_VERSION: u8 = 1;
384 impl Writeable for Router {
385 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
386 writer.write_all(&[SERIALIZATION_VERSION; 1])?;
387 writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
389 let network = self.network_map.read().unwrap();
390 network.write(writer)?;
395 /// Arguments for the creation of a Router that are not deserialized.
396 /// At a high-level, the process for deserializing a Router and resuming normal operation is:
397 /// 1) Deserialize the Router by filling in this struct and calling <Router>::read(reaser, args).
398 /// 2) Register the new Router with your ChainWatchInterface
399 pub struct RouterReadArgs {
400 /// The ChainWatchInterface for use in the Router in the future.
402 /// No calls to the ChainWatchInterface will be made during deserialization.
403 pub chain_monitor: Arc<ChainWatchInterface>,
404 /// The Logger for use in the ChannelManager and which may be used to log information during
406 pub logger: Arc<Logger>,
409 impl ReadableArgs<RouterReadArgs> for Router {
410 fn read<R: ::std::io::Read>(reader: &mut R, args: RouterReadArgs) -> Result<Router, DecodeError> {
411 let _ver: u8 = Readable::read(reader)?;
412 let min_ver: u8 = Readable::read(reader)?;
413 if min_ver > SERIALIZATION_VERSION {
414 return Err(DecodeError::UnknownVersion);
416 let network_map = Readable::read(reader)?;
418 secp_ctx: Secp256k1::verification_only(),
419 network_map: RwLock::new(network_map),
420 full_syncs_requested: AtomicUsize::new(0),
421 chain_monitor: args.chain_monitor,
427 macro_rules! secp_verify_sig {
428 ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
429 match $secp_ctx.verify($msg, $sig, $pubkey) {
431 Err(_) => return Err(LightningError{err: "Invalid signature from remote node", action: ErrorAction::IgnoreError}),
436 impl RoutingMessageHandler for Router {
438 fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
439 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
440 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
442 let mut network = self.network_map.write().unwrap();
443 match network.nodes.get_mut(&msg.contents.node_id) {
444 None => Err(LightningError{err: "No existing channels for node_announcement", action: ErrorAction::IgnoreError}),
446 match node.last_update {
447 Some(last_update) => if last_update >= msg.contents.timestamp {
448 return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
453 node.features = msg.contents.features.clone();
454 node.last_update = Some(msg.contents.timestamp);
455 node.rgb = msg.contents.rgb;
456 node.alias = msg.contents.alias;
457 node.addresses = msg.contents.addresses.clone();
459 let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty();
460 node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
466 fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
467 if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
468 return Err(LightningError{err: "Channel announcement node had a channel with itself", action: ErrorAction::IgnoreError});
471 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
472 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
473 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
474 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
475 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
477 let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
478 Ok((script_pubkey, _value)) => {
479 let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
480 .push_slice(&msg.contents.bitcoin_key_1.serialize())
481 .push_slice(&msg.contents.bitcoin_key_2.serialize())
482 .push_opcode(opcodes::all::OP_PUSHNUM_2)
483 .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
484 if script_pubkey != expected_script {
485 return Err(LightningError{err: "Channel announcement keys didn't match on-chain script", action: ErrorAction::IgnoreError});
487 //TODO: Check if value is worth storing, use it to inform routing, and compare it
488 //to the new HTLC max field in channel_update
491 Err(ChainError::NotSupported) => {
492 // Tentatively accept, potentially exposing us to DoS attacks
495 Err(ChainError::NotWatched) => {
496 return Err(LightningError{err: "Channel announced on an unknown chain", action: ErrorAction::IgnoreError});
498 Err(ChainError::UnknownTx) => {
499 return Err(LightningError{err: "Channel announced without corresponding UTXO entry", action: ErrorAction::IgnoreError});
503 let mut network_lock = self.network_map.write().unwrap();
504 let network = &mut *network_lock;
506 let should_relay = msg.contents.excess_data.is_empty();
508 let chan_info = ChannelInfo {
509 features: msg.contents.features.clone(),
510 one_to_two: DirectionalChannelInfo {
511 src_node_id: msg.contents.node_id_1.clone(),
514 cltv_expiry_delta: u16::max_value(),
515 htlc_minimum_msat: u64::max_value(),
516 fee_base_msat: u32::max_value(),
517 fee_proportional_millionths: u32::max_value(),
518 last_update_message: None,
520 two_to_one: DirectionalChannelInfo {
521 src_node_id: msg.contents.node_id_2.clone(),
524 cltv_expiry_delta: u16::max_value(),
525 htlc_minimum_msat: u64::max_value(),
526 fee_base_msat: u32::max_value(),
527 fee_proportional_millionths: u32::max_value(),
528 last_update_message: None,
530 announcement_message: if should_relay { Some(msg.clone()) } else { None },
533 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
534 BtreeEntry::Occupied(mut entry) => {
535 //TODO: because asking the blockchain if short_channel_id is valid is only optional
536 //in the blockchain API, we need to handle it smartly here, though it's unclear
539 // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
540 // only sometimes returns results. In any case remove the previous entry. Note
541 // that the spec expects us to "blacklist" the node_ids involved, but we can't
543 // a) we don't *require* a UTXO provider that always returns results.
544 // b) we don't track UTXOs of channels we know about and remove them if they
546 // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
547 Self::remove_channel_in_nodes(&mut network.nodes, &entry.get(), msg.contents.short_channel_id);
548 *entry.get_mut() = chan_info;
550 return Err(LightningError{err: "Already have knowledge of channel", action: ErrorAction::IgnoreError})
553 BtreeEntry::Vacant(entry) => {
554 entry.insert(chan_info);
558 macro_rules! add_channel_to_node {
559 ( $node_id: expr ) => {
560 match network.nodes.entry($node_id) {
561 BtreeEntry::Occupied(node_entry) => {
562 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
564 BtreeEntry::Vacant(node_entry) => {
565 node_entry.insert(NodeInfo {
566 channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
567 lowest_inbound_channel_fee_base_msat: u32::max_value(),
568 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
569 features: NodeFeatures::empty(),
573 addresses: Vec::new(),
574 announcement_message: None,
581 add_channel_to_node!(msg.contents.node_id_1);
582 add_channel_to_node!(msg.contents.node_id_2);
584 log_trace!(self, "Added channel_announcement for {}{}", msg.contents.short_channel_id, if !should_relay { " with excess uninterpreted data!" } else { "" });
588 fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
590 &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
591 let _ = self.handle_channel_update(msg);
593 &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
594 let mut network = self.network_map.write().unwrap();
596 if let Some(chan) = network.channels.remove(short_channel_id) {
597 Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
600 if let Some(chan) = network.channels.get_mut(short_channel_id) {
601 chan.one_to_two.enabled = false;
602 chan.two_to_one.enabled = false;
606 &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
608 //TODO: Wholly remove the node
610 self.mark_node_bad(node_id, false);
616 fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> {
617 let mut network = self.network_map.write().unwrap();
619 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
620 let chan_was_enabled;
622 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
623 None => return Err(LightningError{err: "Couldn't find channel for update", action: ErrorAction::IgnoreError}),
625 macro_rules! maybe_update_channel_info {
626 ( $target: expr) => {
627 if $target.last_update >= msg.contents.timestamp {
628 return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
630 chan_was_enabled = $target.enabled;
631 $target.last_update = msg.contents.timestamp;
632 $target.enabled = chan_enabled;
633 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
634 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
635 $target.fee_base_msat = msg.contents.fee_base_msat;
636 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
637 $target.last_update_message = if msg.contents.excess_data.is_empty() {
644 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
645 if msg.contents.flags & 1 == 1 {
646 dest_node_id = channel.one_to_two.src_node_id.clone();
647 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
648 maybe_update_channel_info!(channel.two_to_one);
650 dest_node_id = channel.two_to_one.src_node_id.clone();
651 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
652 maybe_update_channel_info!(channel.one_to_two);
658 let node = network.nodes.get_mut(&dest_node_id).unwrap();
659 node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
660 node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
661 } else if chan_was_enabled {
662 let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
663 let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
666 let node = network.nodes.get(&dest_node_id).unwrap();
668 for chan_id in node.channels.iter() {
669 let chan = network.channels.get(chan_id).unwrap();
670 if chan.one_to_two.src_node_id == dest_node_id {
671 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
672 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
674 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
675 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
680 //TODO: satisfy the borrow-checker without a double-map-lookup :(
681 let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
682 mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
683 mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
686 Ok(msg.contents.excess_data.is_empty())
689 fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, Option<msgs::ChannelUpdate>, Option<msgs::ChannelUpdate>)> {
690 let mut result = Vec::with_capacity(batch_amount as usize);
691 let network = self.network_map.read().unwrap();
692 let mut iter = network.channels.range(starting_point..);
693 while result.len() < batch_amount as usize {
694 if let Some((_, ref chan)) = iter.next() {
695 if chan.announcement_message.is_some() {
696 result.push((chan.announcement_message.clone().unwrap(),
697 chan.one_to_two.last_update_message.clone(),
698 chan.two_to_one.last_update_message.clone()));
700 // TODO: We may end up sending un-announced channel_updates if we are sending
701 // initial sync data while receiving announce/updates for this channel.
710 fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
711 let mut result = Vec::with_capacity(batch_amount as usize);
712 let network = self.network_map.read().unwrap();
713 let mut iter = if let Some(pubkey) = starting_point {
714 let mut iter = network.nodes.range((*pubkey)..);
718 network.nodes.range(..)
720 while result.len() < batch_amount as usize {
721 if let Some((_, ref node)) = iter.next() {
722 if node.announcement_message.is_some() {
723 result.push(node.announcement_message.clone().unwrap());
732 fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
733 //TODO: Determine whether to request a full sync based on the network map.
734 const FULL_SYNCS_TO_REQUEST: usize = 5;
735 if self.full_syncs_requested.load(Ordering::Acquire) < FULL_SYNCS_TO_REQUEST {
736 self.full_syncs_requested.fetch_add(1, Ordering::AcqRel);
744 #[derive(Eq, PartialEq)]
745 struct RouteGraphNode {
747 lowest_fee_to_peer_through_node: u64,
748 lowest_fee_to_node: u64,
751 impl cmp::Ord for RouteGraphNode {
752 fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
753 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
754 .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
758 impl cmp::PartialOrd for RouteGraphNode {
759 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
760 Some(self.cmp(other))
764 struct DummyDirectionalChannelInfo {
765 src_node_id: PublicKey,
766 cltv_expiry_delta: u32,
767 htlc_minimum_msat: u64,
769 fee_proportional_millionths: u32,
773 /// Creates a new router with the given node_id to be used as the source for get_route()
774 pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
775 let mut nodes = BTreeMap::new();
776 nodes.insert(our_pubkey.clone(), NodeInfo {
777 channels: Vec::new(),
778 lowest_inbound_channel_fee_base_msat: u32::max_value(),
779 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
780 features: NodeFeatures::empty(),
784 addresses: Vec::new(),
785 announcement_message: None,
788 secp_ctx: Secp256k1::verification_only(),
789 network_map: RwLock::new(NetworkMap {
790 channels: BTreeMap::new(),
791 our_node_id: our_pubkey,
794 full_syncs_requested: AtomicUsize::new(0),
800 /// Dumps the entire network view of this Router to the logger provided in the constructor at
802 pub fn trace_state(&self) {
803 log_trace!(self, "{}", self.network_map.read().unwrap());
806 /// Get network addresses by node id
807 pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
808 let network = self.network_map.read().unwrap();
809 network.nodes.get(pubkey).map(|n| n.addresses.clone())
812 /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
813 /// with an exponential decay in node "badness". Note that there is deliberately no
814 /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
815 /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
816 /// boolean will reduce the penalty, returning the node to usability faster. If the node is
817 /// behaving correctly, it will disable the failing channel and we will use it again next time.
818 pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
822 fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
823 macro_rules! remove_from_node {
824 ($node_id: expr) => {
825 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
826 entry.get_mut().channels.retain(|chan_id| {
827 short_channel_id != *NetworkMap::get_short_id(chan_id)
829 if entry.get().channels.is_empty() {
830 entry.remove_entry();
833 panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
837 remove_from_node!(chan.one_to_two.src_node_id);
838 remove_from_node!(chan.two_to_one.src_node_id);
841 /// Gets a route from us to the given target node.
843 /// Extra routing hops between known nodes and the target will be used if they are included in
846 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
847 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
848 /// (this Router's) view of our local channels will be ignored, and only those in first_hops
851 /// Panics if first_hops contains channels without short_channel_ids
852 /// (ChannelManager::list_usable_channels will never include such channels).
854 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
855 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
856 /// *is* checked as they may change based on the receiving node.
857 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> {
858 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
859 // uptime/success in using a node in the past.
860 let network = self.network_map.read().unwrap();
862 if *target == network.our_node_id {
863 return Err(LightningError{err: "Cannot generate a route to ourselves", action: ErrorAction::IgnoreError});
866 if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
867 return Err(LightningError{err: "Cannot generate a route of more value than all existing satoshis", action: ErrorAction::IgnoreError});
870 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
871 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
872 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
873 // to use as the A* heuristic beyond just the cost to get one node further than the current
876 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
877 src_node_id: network.our_node_id.clone(),
878 cltv_expiry_delta: 0,
879 htlc_minimum_msat: 0,
881 fee_proportional_millionths: 0,
884 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
885 let mut dist = HashMap::with_capacity(network.nodes.len());
887 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
888 if let Some(hops) = first_hops {
890 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
891 if chan.remote_network_id == *target {
893 paths: vec![vec![RouteHop {
894 pubkey: chan.remote_network_id,
895 node_features: NodeFeatures::with_known_relevant_init_flags(&chan.counterparty_features),
897 channel_features: ChannelFeatures::with_known_relevant_init_flags(&chan.counterparty_features),
898 fee_msat: final_value_msat,
899 cltv_expiry_delta: final_cltv,
903 first_hop_targets.insert(chan.remote_network_id, (short_channel_id, chan.counterparty_features.clone()));
905 if first_hop_targets.is_empty() {
906 return Err(LightningError{err: "Cannot route when there are no outbound routes away from us", action: ErrorAction::IgnoreError});
910 macro_rules! add_entry {
911 // Adds entry which goes from the node pointed to by $directional_info to
912 // $dest_node_id over the channel with id $chan_id with fees described in
913 // $directional_info.
914 ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $chan_features: expr, $starting_fee_msat: expr ) => {
915 //TODO: Explore simply adding fee to hit htlc_minimum_msat
916 if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
917 let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
918 if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
919 ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
921 let mut total_fee = $starting_fee_msat as u64;
922 let hm_entry = dist.entry(&$directional_info.src_node_id);
923 let old_entry = hm_entry.or_insert_with(|| {
924 let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
926 node.lowest_inbound_channel_fee_base_msat,
927 node.lowest_inbound_channel_fee_proportional_millionths,
929 pubkey: $dest_node_id.clone(),
930 node_features: NodeFeatures::empty(),
932 channel_features: $chan_features.clone(),
934 cltv_expiry_delta: 0,
937 if $directional_info.src_node_id != network.our_node_id {
938 // Ignore new_fee for channel-from-us as we assume all channels-from-us
939 // will have the same effective-fee
940 total_fee += new_fee;
941 if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
942 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
944 // max_value means we'll always fail the old_entry.0 > total_fee check
945 total_fee = u64::max_value();
948 let new_graph_node = RouteGraphNode {
949 pubkey: $directional_info.src_node_id,
950 lowest_fee_to_peer_through_node: total_fee,
951 lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
953 if old_entry.0 > total_fee {
954 targets.push(new_graph_node);
955 old_entry.0 = total_fee;
956 old_entry.3 = RouteHop {
957 pubkey: $dest_node_id.clone(),
958 node_features: NodeFeatures::empty(),
959 short_channel_id: $chan_id.clone(),
960 channel_features: $chan_features.clone(),
961 fee_msat: new_fee, // This field is ignored on the last-hop anyway
962 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
970 macro_rules! add_entries_to_cheapest_to_target_node {
971 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
972 if first_hops.is_some() {
973 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&$node_id) {
974 add_entry!(first_hop, $node_id, dummy_directional_info, ChannelFeatures::with_known_relevant_init_flags(&features), $fee_to_target_msat);
978 if !$node.features.requires_unknown_bits() {
979 for chan_id in $node.channels.iter() {
980 let chan = network.channels.get(chan_id).unwrap();
981 if !chan.features.requires_unknown_bits() {
982 if chan.one_to_two.src_node_id == *$node_id {
983 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
984 if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
985 if chan.two_to_one.enabled {
986 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, chan.features, $fee_to_target_msat);
990 if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
991 if chan.one_to_two.enabled {
992 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, chan.features, $fee_to_target_msat);
1002 match network.nodes.get(target) {
1005 add_entries_to_cheapest_to_target_node!(node, target, 0);
1009 for hop in last_hops.iter() {
1010 if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
1011 if network.nodes.get(&hop.src_node_id).is_some() {
1012 if first_hops.is_some() {
1013 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&hop.src_node_id) {
1014 // Currently there are no channel-context features defined, so we are a
1015 // bit lazy here. In the future, we should pull them out via our
1016 // ChannelManager, but there's no reason to waste the space until we
1018 add_entry!(first_hop, hop.src_node_id, dummy_directional_info, ChannelFeatures::with_known_relevant_init_flags(&features), 0);
1021 // BOLT 11 doesn't allow inclusion of features for the last hop hints, which
1022 // really sucks, cause we're gonna need that eventually.
1023 add_entry!(hop.short_channel_id, target, hop, ChannelFeatures::empty(), 0);
1028 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
1029 if pubkey == network.our_node_id {
1030 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
1032 if let Some(&(_, ref features)) = first_hop_targets.get(&res.last().unwrap().pubkey) {
1033 res.last_mut().unwrap().node_features = NodeFeatures::with_known_relevant_init_flags(&features);
1034 } else if let Some(node) = network.nodes.get(&res.last().unwrap().pubkey) {
1035 res.last_mut().unwrap().node_features = node.features.clone();
1037 // We should be able to fill in features for everything except the last
1038 // hop, if the last hop was provided via a BOLT 11 invoice (though we
1039 // should be able to extend it further as BOLT 11 does have feature
1040 // flags for the last hop node itself).
1041 assert!(res.last().unwrap().pubkey == *target);
1043 if res.last().unwrap().pubkey == *target {
1047 let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
1049 None => return Err(LightningError{err: "Failed to find a non-fee-overflowing path to the given destination", action: ErrorAction::IgnoreError}),
1051 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
1052 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
1053 res.push(new_entry);
1055 res.last_mut().unwrap().fee_msat = final_value_msat;
1056 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
1057 let route = Route { paths: vec![res] };
1058 log_trace!(self, "Got route: {}", log_route!(route));
1062 match network.nodes.get(&pubkey) {
1065 add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
1070 Err(LightningError{err: "Failed to find a path to the given destination", action: ErrorAction::IgnoreError})
1076 use chain::chaininterface;
1077 use ln::channelmanager;
1078 use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
1079 use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
1080 use ln::msgs::{ErrorAction, LightningError, RoutingMessageHandler};
1081 use util::test_utils;
1082 use util::test_utils::TestVecWriter;
1083 use util::logger::Logger;
1084 use util::ser::{Writeable, Readable};
1086 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1087 use bitcoin_hashes::Hash;
1088 use bitcoin::network::constants::Network;
1092 use secp256k1::key::{PublicKey,SecretKey};
1094 use secp256k1::Secp256k1;
1098 fn create_router() -> (Secp256k1<All>, PublicKey, Router) {
1099 let secp_ctx = Secp256k1::new();
1100 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
1101 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1102 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
1103 let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
1104 (secp_ctx, our_id, router)
1109 let (secp_ctx, our_id, router) = create_router();
1111 // Build network from our_id to node8:
1113 // -1(1)2- node1 -1(3)2-
1115 // our_id -1(12)2- node8 -1(13)2--- node3
1117 // -1(2)2- node2 -1(4)2-
1120 // chan1 1-to-2: disabled
1121 // chan1 2-to-1: enabled, 0 fee
1123 // chan2 1-to-2: enabled, ignored fee
1124 // chan2 2-to-1: enabled, 0 fee
1126 // chan3 1-to-2: enabled, 0 fee
1127 // chan3 2-to-1: enabled, 100 msat fee
1129 // chan4 1-to-2: enabled, 100% fee
1130 // chan4 2-to-1: enabled, 0 fee
1132 // chan12 1-to-2: enabled, ignored fee
1133 // chan12 2-to-1: enabled, 0 fee
1135 // chan13 1-to-2: enabled, 200% fee
1136 // chan13 2-to-1: enabled, 0 fee
1139 // -1(5)2- node4 -1(8)2--
1143 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
1145 // -1(7)2- node6 -1(10)2-
1147 // chan5 1-to-2: enabled, 100 msat fee
1148 // chan5 2-to-1: enabled, 0 fee
1150 // chan6 1-to-2: enabled, 0 fee
1151 // chan6 2-to-1: enabled, 0 fee
1153 // chan7 1-to-2: enabled, 100% fee
1154 // chan7 2-to-1: enabled, 0 fee
1156 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
1157 // chan8 2-to-1: enabled, 0 fee
1159 // chan9 1-to-2: enabled, 1001 msat fee
1160 // chan9 2-to-1: enabled, 0 fee
1162 // chan10 1-to-2: enabled, 0 fee
1163 // chan10 2-to-1: enabled, 0 fee
1165 // chan11 1-to-2: enabled, 0 fee
1166 // chan11 2-to-1: enabled, 0 fee
1168 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
1169 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
1170 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
1171 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
1172 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
1173 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
1174 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
1175 let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
1177 let zero_hash = Sha256dHash::hash(&[0; 32]);
1179 macro_rules! id_to_feature_flags {
1180 // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can
1181 // test for it later.
1183 let idx = ($id - 1) * 2 + 1;
1185 vec![1 << (idx - 8*3), 0, 0, 0]
1186 } else if idx > 8*2 {
1187 vec![1 << (idx - 8*2), 0, 0]
1188 } else if idx > 8*1 {
1189 vec![1 << (idx - 8*1), 0]
1197 let mut network = router.network_map.write().unwrap();
1199 network.nodes.insert(node1.clone(), NodeInfo {
1200 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
1201 lowest_inbound_channel_fee_base_msat: 100,
1202 lowest_inbound_channel_fee_proportional_millionths: 0,
1203 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(1)),
1204 last_update: Some(1),
1207 addresses: Vec::new(),
1208 announcement_message: None,
1210 network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
1211 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(1)),
1212 one_to_two: DirectionalChannelInfo {
1213 src_node_id: our_id.clone(),
1216 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1217 htlc_minimum_msat: 0,
1218 fee_base_msat: u32::max_value(), // This value should be ignored
1219 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1220 last_update_message: None,
1221 }, two_to_one: DirectionalChannelInfo {
1222 src_node_id: node1.clone(),
1225 cltv_expiry_delta: 0,
1226 htlc_minimum_msat: 0,
1228 fee_proportional_millionths: 0,
1229 last_update_message: None,
1231 announcement_message: None,
1233 network.nodes.insert(node2.clone(), NodeInfo {
1234 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
1235 lowest_inbound_channel_fee_base_msat: 0,
1236 lowest_inbound_channel_fee_proportional_millionths: 0,
1237 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(2)),
1238 last_update: Some(1),
1241 addresses: Vec::new(),
1242 announcement_message: None,
1244 network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
1245 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(2)),
1246 one_to_two: DirectionalChannelInfo {
1247 src_node_id: our_id.clone(),
1250 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1251 htlc_minimum_msat: 0,
1252 fee_base_msat: u32::max_value(), // This value should be ignored
1253 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1254 last_update_message: None,
1255 }, two_to_one: DirectionalChannelInfo {
1256 src_node_id: node2.clone(),
1259 cltv_expiry_delta: 0,
1260 htlc_minimum_msat: 0,
1262 fee_proportional_millionths: 0,
1263 last_update_message: None,
1265 announcement_message: None,
1267 network.nodes.insert(node8.clone(), NodeInfo {
1268 channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
1269 lowest_inbound_channel_fee_base_msat: 0,
1270 lowest_inbound_channel_fee_proportional_millionths: 0,
1271 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(8)),
1272 last_update: Some(1),
1275 addresses: Vec::new(),
1276 announcement_message: None,
1278 network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
1279 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(12)),
1280 one_to_two: DirectionalChannelInfo {
1281 src_node_id: our_id.clone(),
1284 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1285 htlc_minimum_msat: 0,
1286 fee_base_msat: u32::max_value(), // This value should be ignored
1287 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1288 last_update_message: None,
1289 }, two_to_one: DirectionalChannelInfo {
1290 src_node_id: node8.clone(),
1293 cltv_expiry_delta: 0,
1294 htlc_minimum_msat: 0,
1296 fee_proportional_millionths: 0,
1297 last_update_message: None,
1299 announcement_message: None,
1301 network.nodes.insert(node3.clone(), NodeInfo {
1303 NetworkMap::get_key(3, zero_hash.clone()),
1304 NetworkMap::get_key(4, zero_hash.clone()),
1305 NetworkMap::get_key(13, zero_hash.clone()),
1306 NetworkMap::get_key(5, zero_hash.clone()),
1307 NetworkMap::get_key(6, zero_hash.clone()),
1308 NetworkMap::get_key(7, zero_hash.clone())),
1309 lowest_inbound_channel_fee_base_msat: 0,
1310 lowest_inbound_channel_fee_proportional_millionths: 0,
1311 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(3)),
1312 last_update: Some(1),
1315 addresses: Vec::new(),
1316 announcement_message: None,
1318 network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
1319 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(3)),
1320 one_to_two: DirectionalChannelInfo {
1321 src_node_id: node1.clone(),
1324 cltv_expiry_delta: (3 << 8) | 1,
1325 htlc_minimum_msat: 0,
1327 fee_proportional_millionths: 0,
1328 last_update_message: None,
1329 }, two_to_one: DirectionalChannelInfo {
1330 src_node_id: node3.clone(),
1333 cltv_expiry_delta: (3 << 8) | 2,
1334 htlc_minimum_msat: 0,
1336 fee_proportional_millionths: 0,
1337 last_update_message: None,
1339 announcement_message: None,
1341 network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
1342 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(4)),
1343 one_to_two: DirectionalChannelInfo {
1344 src_node_id: node2.clone(),
1347 cltv_expiry_delta: (4 << 8) | 1,
1348 htlc_minimum_msat: 0,
1350 fee_proportional_millionths: 1000000,
1351 last_update_message: None,
1352 }, two_to_one: DirectionalChannelInfo {
1353 src_node_id: node3.clone(),
1356 cltv_expiry_delta: (4 << 8) | 2,
1357 htlc_minimum_msat: 0,
1359 fee_proportional_millionths: 0,
1360 last_update_message: None,
1362 announcement_message: None,
1364 network.channels.insert(NetworkMap::get_key(13, zero_hash.clone()), ChannelInfo {
1365 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(13)),
1366 one_to_two: DirectionalChannelInfo {
1367 src_node_id: node8.clone(),
1370 cltv_expiry_delta: (13 << 8) | 1,
1371 htlc_minimum_msat: 0,
1373 fee_proportional_millionths: 2000000,
1374 last_update_message: None,
1375 }, two_to_one: DirectionalChannelInfo {
1376 src_node_id: node3.clone(),
1379 cltv_expiry_delta: (13 << 8) | 2,
1380 htlc_minimum_msat: 0,
1382 fee_proportional_millionths: 0,
1383 last_update_message: None,
1385 announcement_message: None,
1387 network.nodes.insert(node4.clone(), NodeInfo {
1388 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1389 lowest_inbound_channel_fee_base_msat: 0,
1390 lowest_inbound_channel_fee_proportional_millionths: 0,
1391 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(4)),
1392 last_update: Some(1),
1395 addresses: Vec::new(),
1396 announcement_message: None,
1398 network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
1399 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(5)),
1400 one_to_two: DirectionalChannelInfo {
1401 src_node_id: node3.clone(),
1404 cltv_expiry_delta: (5 << 8) | 1,
1405 htlc_minimum_msat: 0,
1407 fee_proportional_millionths: 0,
1408 last_update_message: None,
1409 }, two_to_one: DirectionalChannelInfo {
1410 src_node_id: node4.clone(),
1413 cltv_expiry_delta: (5 << 8) | 2,
1414 htlc_minimum_msat: 0,
1416 fee_proportional_millionths: 0,
1417 last_update_message: None,
1419 announcement_message: None,
1421 network.nodes.insert(node5.clone(), NodeInfo {
1422 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1423 lowest_inbound_channel_fee_base_msat: 0,
1424 lowest_inbound_channel_fee_proportional_millionths: 0,
1425 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(5)),
1426 last_update: Some(1),
1429 addresses: Vec::new(),
1430 announcement_message: None,
1432 network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
1433 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(6)),
1434 one_to_two: DirectionalChannelInfo {
1435 src_node_id: node3.clone(),
1438 cltv_expiry_delta: (6 << 8) | 1,
1439 htlc_minimum_msat: 0,
1441 fee_proportional_millionths: 0,
1442 last_update_message: None,
1443 }, two_to_one: DirectionalChannelInfo {
1444 src_node_id: node5.clone(),
1447 cltv_expiry_delta: (6 << 8) | 2,
1448 htlc_minimum_msat: 0,
1450 fee_proportional_millionths: 0,
1451 last_update_message: None,
1453 announcement_message: None,
1455 network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
1456 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(11)),
1457 one_to_two: DirectionalChannelInfo {
1458 src_node_id: node5.clone(),
1461 cltv_expiry_delta: (11 << 8) | 1,
1462 htlc_minimum_msat: 0,
1464 fee_proportional_millionths: 0,
1465 last_update_message: None,
1466 }, two_to_one: DirectionalChannelInfo {
1467 src_node_id: node4.clone(),
1470 cltv_expiry_delta: (11 << 8) | 2,
1471 htlc_minimum_msat: 0,
1473 fee_proportional_millionths: 0,
1474 last_update_message: None,
1476 announcement_message: None,
1478 network.nodes.insert(node6.clone(), NodeInfo {
1479 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
1480 lowest_inbound_channel_fee_base_msat: 0,
1481 lowest_inbound_channel_fee_proportional_millionths: 0,
1482 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(6)),
1483 last_update: Some(1),
1486 addresses: Vec::new(),
1487 announcement_message: None,
1489 network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
1490 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(7)),
1491 one_to_two: DirectionalChannelInfo {
1492 src_node_id: node3.clone(),
1495 cltv_expiry_delta: (7 << 8) | 1,
1496 htlc_minimum_msat: 0,
1498 fee_proportional_millionths: 1000000,
1499 last_update_message: None,
1500 }, two_to_one: DirectionalChannelInfo {
1501 src_node_id: node6.clone(),
1504 cltv_expiry_delta: (7 << 8) | 2,
1505 htlc_minimum_msat: 0,
1507 fee_proportional_millionths: 0,
1508 last_update_message: None,
1510 announcement_message: None,
1514 { // Simple route to 3 via 2
1515 let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
1516 assert_eq!(route.paths[0].len(), 2);
1518 assert_eq!(route.paths[0][0].pubkey, node2);
1519 assert_eq!(route.paths[0][0].short_channel_id, 2);
1520 assert_eq!(route.paths[0][0].fee_msat, 100);
1521 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1522 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1523 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1525 assert_eq!(route.paths[0][1].pubkey, node3);
1526 assert_eq!(route.paths[0][1].short_channel_id, 4);
1527 assert_eq!(route.paths[0][1].fee_msat, 100);
1528 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1529 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1530 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1533 { // Disable channels 4 and 12 by requiring unknown feature bits
1534 let mut network = router.network_map.write().unwrap();
1535 network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1536 network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1539 { // If all the channels require some features we don't understand, route should fail
1540 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1541 assert_eq!(err, "Failed to find a path to the given destination");
1542 } else { panic!(); }
1545 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1546 let our_chans = vec![channelmanager::ChannelDetails {
1547 channel_id: [0; 32],
1548 short_channel_id: Some(42),
1549 remote_network_id: node8.clone(),
1550 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1551 channel_value_satoshis: 0,
1553 outbound_capacity_msat: 0,
1554 inbound_capacity_msat: 0,
1557 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1558 assert_eq!(route.paths[0].len(), 2);
1560 assert_eq!(route.paths[0][0].pubkey, node8);
1561 assert_eq!(route.paths[0][0].short_channel_id, 42);
1562 assert_eq!(route.paths[0][0].fee_msat, 200);
1563 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
1564 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
1565 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
1567 assert_eq!(route.paths[0][1].pubkey, node3);
1568 assert_eq!(route.paths[0][1].short_channel_id, 13);
1569 assert_eq!(route.paths[0][1].fee_msat, 100);
1570 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1571 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1572 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13));
1575 { // Re-enable channels 4 and 12 by wiping the unknown feature bits
1576 let mut network = router.network_map.write().unwrap();
1577 network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1578 network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1581 { // Disable nodes 1, 2, and 8 by requiring unknown feature bits
1582 let mut network = router.network_map.write().unwrap();
1583 network.nodes.get_mut(&node1).unwrap().features.set_require_unknown_bits();
1584 network.nodes.get_mut(&node2).unwrap().features.set_require_unknown_bits();
1585 network.nodes.get_mut(&node8).unwrap().features.set_require_unknown_bits();
1588 { // If all nodes require some features we don't understand, route should fail
1589 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1590 assert_eq!(err, "Failed to find a path to the given destination");
1591 } else { panic!(); }
1594 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1595 let our_chans = vec![channelmanager::ChannelDetails {
1596 channel_id: [0; 32],
1597 short_channel_id: Some(42),
1598 remote_network_id: node8.clone(),
1599 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1600 channel_value_satoshis: 0,
1602 outbound_capacity_msat: 0,
1603 inbound_capacity_msat: 0,
1606 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1607 assert_eq!(route.paths[0].len(), 2);
1609 assert_eq!(route.paths[0][0].pubkey, node8);
1610 assert_eq!(route.paths[0][0].short_channel_id, 42);
1611 assert_eq!(route.paths[0][0].fee_msat, 200);
1612 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
1613 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
1614 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
1616 assert_eq!(route.paths[0][1].pubkey, node3);
1617 assert_eq!(route.paths[0][1].short_channel_id, 13);
1618 assert_eq!(route.paths[0][1].fee_msat, 100);
1619 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1620 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1621 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13));
1624 { // Re-enable nodes 1, 2, and 8
1625 let mut network = router.network_map.write().unwrap();
1626 network.nodes.get_mut(&node1).unwrap().features.clear_require_unknown_bits();
1627 network.nodes.get_mut(&node2).unwrap().features.clear_require_unknown_bits();
1628 network.nodes.get_mut(&node8).unwrap().features.clear_require_unknown_bits();
1631 // Note that we don't test disabling node 3 and failing to route to it, as we (somewhat
1632 // naively) assume that the user checked the feature bits on the invoice, which override
1633 // the node_announcement.
1635 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
1636 let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
1637 assert_eq!(route.paths[0].len(), 3);
1639 assert_eq!(route.paths[0][0].pubkey, node2);
1640 assert_eq!(route.paths[0][0].short_channel_id, 2);
1641 assert_eq!(route.paths[0][0].fee_msat, 200);
1642 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1643 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1644 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1646 assert_eq!(route.paths[0][1].pubkey, node3);
1647 assert_eq!(route.paths[0][1].short_channel_id, 4);
1648 assert_eq!(route.paths[0][1].fee_msat, 100);
1649 assert_eq!(route.paths[0][1].cltv_expiry_delta, (3 << 8) | 2);
1650 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1651 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1653 assert_eq!(route.paths[0][2].pubkey, node1);
1654 assert_eq!(route.paths[0][2].short_channel_id, 3);
1655 assert_eq!(route.paths[0][2].fee_msat, 100);
1656 assert_eq!(route.paths[0][2].cltv_expiry_delta, 42);
1657 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(1));
1658 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(3));
1661 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1662 let our_chans = vec![channelmanager::ChannelDetails {
1663 channel_id: [0; 32],
1664 short_channel_id: Some(42),
1665 remote_network_id: node8.clone(),
1666 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1667 channel_value_satoshis: 0,
1669 outbound_capacity_msat: 0,
1670 inbound_capacity_msat: 0,
1673 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1674 assert_eq!(route.paths[0].len(), 2);
1676 assert_eq!(route.paths[0][0].pubkey, node8);
1677 assert_eq!(route.paths[0][0].short_channel_id, 42);
1678 assert_eq!(route.paths[0][0].fee_msat, 200);
1679 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
1680 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
1681 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
1683 assert_eq!(route.paths[0][1].pubkey, node3);
1684 assert_eq!(route.paths[0][1].short_channel_id, 13);
1685 assert_eq!(route.paths[0][1].fee_msat, 100);
1686 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1687 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1688 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13));
1691 let mut last_hops = vec!(RouteHint {
1692 src_node_id: node4.clone(),
1693 short_channel_id: 8,
1695 fee_proportional_millionths: 0,
1696 cltv_expiry_delta: (8 << 8) | 1,
1697 htlc_minimum_msat: 0,
1699 src_node_id: node5.clone(),
1700 short_channel_id: 9,
1701 fee_base_msat: 1001,
1702 fee_proportional_millionths: 0,
1703 cltv_expiry_delta: (9 << 8) | 1,
1704 htlc_minimum_msat: 0,
1706 src_node_id: node6.clone(),
1707 short_channel_id: 10,
1709 fee_proportional_millionths: 0,
1710 cltv_expiry_delta: (10 << 8) | 1,
1711 htlc_minimum_msat: 0,
1714 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
1715 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1716 assert_eq!(route.paths[0].len(), 5);
1718 assert_eq!(route.paths[0][0].pubkey, node2);
1719 assert_eq!(route.paths[0][0].short_channel_id, 2);
1720 assert_eq!(route.paths[0][0].fee_msat, 100);
1721 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1722 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1723 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1725 assert_eq!(route.paths[0][1].pubkey, node3);
1726 assert_eq!(route.paths[0][1].short_channel_id, 4);
1727 assert_eq!(route.paths[0][1].fee_msat, 0);
1728 assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1);
1729 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1730 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1732 assert_eq!(route.paths[0][2].pubkey, node5);
1733 assert_eq!(route.paths[0][2].short_channel_id, 6);
1734 assert_eq!(route.paths[0][2].fee_msat, 0);
1735 assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1);
1736 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(5));
1737 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(6));
1739 assert_eq!(route.paths[0][3].pubkey, node4);
1740 assert_eq!(route.paths[0][3].short_channel_id, 11);
1741 assert_eq!(route.paths[0][3].fee_msat, 0);
1742 assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1);
1743 // If we have a peer in the node map, we'll use their features here since we don't have
1744 // a way of figuring out their features from the invoice:
1745 assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags!(4));
1746 assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags!(11));
1748 assert_eq!(route.paths[0][4].pubkey, node7);
1749 assert_eq!(route.paths[0][4].short_channel_id, 8);
1750 assert_eq!(route.paths[0][4].fee_msat, 100);
1751 assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
1752 assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1753 assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1756 { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1757 let our_chans = vec![channelmanager::ChannelDetails {
1758 channel_id: [0; 32],
1759 short_channel_id: Some(42),
1760 remote_network_id: node4.clone(),
1761 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1762 channel_value_satoshis: 0,
1764 outbound_capacity_msat: 0,
1765 inbound_capacity_msat: 0,
1768 let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
1769 assert_eq!(route.paths[0].len(), 2);
1771 assert_eq!(route.paths[0][0].pubkey, node4);
1772 assert_eq!(route.paths[0][0].short_channel_id, 42);
1773 assert_eq!(route.paths[0][0].fee_msat, 0);
1774 assert_eq!(route.paths[0][0].cltv_expiry_delta, (8 << 8) | 1);
1775 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
1776 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
1778 assert_eq!(route.paths[0][1].pubkey, node7);
1779 assert_eq!(route.paths[0][1].short_channel_id, 8);
1780 assert_eq!(route.paths[0][1].fee_msat, 100);
1781 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1782 assert_eq!(route.paths[0][1].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1783 assert_eq!(route.paths[0][1].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1786 last_hops[0].fee_base_msat = 1000;
1788 { // Revert to via 6 as the fee on 8 goes up
1789 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1790 assert_eq!(route.paths[0].len(), 4);
1792 assert_eq!(route.paths[0][0].pubkey, node2);
1793 assert_eq!(route.paths[0][0].short_channel_id, 2);
1794 assert_eq!(route.paths[0][0].fee_msat, 200); // fee increased as its % of value transferred across node
1795 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1796 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1797 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1799 assert_eq!(route.paths[0][1].pubkey, node3);
1800 assert_eq!(route.paths[0][1].short_channel_id, 4);
1801 assert_eq!(route.paths[0][1].fee_msat, 100);
1802 assert_eq!(route.paths[0][1].cltv_expiry_delta, (7 << 8) | 1);
1803 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1804 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1806 assert_eq!(route.paths[0][2].pubkey, node6);
1807 assert_eq!(route.paths[0][2].short_channel_id, 7);
1808 assert_eq!(route.paths[0][2].fee_msat, 0);
1809 assert_eq!(route.paths[0][2].cltv_expiry_delta, (10 << 8) | 1);
1810 // If we have a peer in the node map, we'll use their features here since we don't have
1811 // a way of figuring out their features from the invoice:
1812 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(6));
1813 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(7));
1815 assert_eq!(route.paths[0][3].pubkey, node7);
1816 assert_eq!(route.paths[0][3].short_channel_id, 10);
1817 assert_eq!(route.paths[0][3].fee_msat, 100);
1818 assert_eq!(route.paths[0][3].cltv_expiry_delta, 42);
1819 assert_eq!(route.paths[0][3].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1820 assert_eq!(route.paths[0][3].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1823 { // ...but still use 8 for larger payments as 6 has a variable feerate
1824 let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
1825 assert_eq!(route.paths[0].len(), 5);
1827 assert_eq!(route.paths[0][0].pubkey, node2);
1828 assert_eq!(route.paths[0][0].short_channel_id, 2);
1829 assert_eq!(route.paths[0][0].fee_msat, 3000);
1830 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1831 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1832 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1834 assert_eq!(route.paths[0][1].pubkey, node3);
1835 assert_eq!(route.paths[0][1].short_channel_id, 4);
1836 assert_eq!(route.paths[0][1].fee_msat, 0);
1837 assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1);
1838 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1839 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1841 assert_eq!(route.paths[0][2].pubkey, node5);
1842 assert_eq!(route.paths[0][2].short_channel_id, 6);
1843 assert_eq!(route.paths[0][2].fee_msat, 0);
1844 assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1);
1845 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(5));
1846 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(6));
1848 assert_eq!(route.paths[0][3].pubkey, node4);
1849 assert_eq!(route.paths[0][3].short_channel_id, 11);
1850 assert_eq!(route.paths[0][3].fee_msat, 1000);
1851 assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1);
1852 // If we have a peer in the node map, we'll use their features here since we don't have
1853 // a way of figuring out their features from the invoice:
1854 assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags!(4));
1855 assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags!(11));
1857 assert_eq!(route.paths[0][4].pubkey, node7);
1858 assert_eq!(route.paths[0][4].short_channel_id, 8);
1859 assert_eq!(route.paths[0][4].fee_msat, 2000);
1860 assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
1861 assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::<u8>::new()); // We dont pass flags in from invoices yet
1862 assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
1865 { // Test Router serialization/deserialization
1866 let mut w = TestVecWriter(Vec::new());
1867 let network = router.network_map.read().unwrap();
1868 assert!(!network.channels.is_empty());
1869 assert!(!network.nodes.is_empty());
1870 network.write(&mut w).unwrap();
1871 assert!(<NetworkMap>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);
1876 fn request_full_sync_finite_times() {
1877 let (secp_ctx, _, router) = create_router();
1878 let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
1880 assert!(router.should_request_full_sync(&node_id));
1881 assert!(router.should_request_full_sync(&node_id));
1882 assert!(router.should_request_full_sync(&node_id));
1883 assert!(router.should_request_full_sync(&node_id));
1884 assert!(router.should_request_full_sync(&node_id));
1885 assert!(!router.should_request_full_sync(&node_id));