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 node_announcement features of the node at this hop. For the last hop, these may be
35 /// amended to match the features present in the invoice this node generated.
36 pub node_features: NodeFeatures,
37 /// The channel that should be used from the previous hop to reach this node.
38 pub short_channel_id: u64,
39 /// The channel_announcement features of the channel that should be used from the previous hop
40 /// to reach this node.
41 pub channel_features: ChannelFeatures,
42 /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
44 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
45 /// expected at the destination, in excess of the current block height.
46 pub cltv_expiry_delta: u32,
49 impl Writeable for Vec<RouteHop> {
50 fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
51 (self.len() as u8).write(writer)?;
52 for hop in self.iter() {
53 hop.pubkey.write(writer)?;
54 hop.node_features.write(writer)?;
55 hop.short_channel_id.write(writer)?;
56 hop.channel_features.write(writer)?;
57 hop.fee_msat.write(writer)?;
58 hop.cltv_expiry_delta.write(writer)?;
64 impl<R: ::std::io::Read> Readable<R> for Vec<RouteHop> {
65 fn read(reader: &mut R) -> Result<Vec<RouteHop>, DecodeError> {
66 let hops_count: u8 = Readable::read(reader)?;
67 let mut hops = Vec::with_capacity(hops_count as usize);
68 for _ in 0..hops_count {
70 pubkey: Readable::read(reader)?,
71 node_features: Readable::read(reader)?,
72 short_channel_id: Readable::read(reader)?,
73 channel_features: Readable::read(reader)?,
74 fee_msat: Readable::read(reader)?,
75 cltv_expiry_delta: Readable::read(reader)?,
82 /// A route from us through the network to a destination
83 #[derive(Clone, PartialEq)]
85 /// The list of routes taken for a single (potentially-multi-)path payment. The pubkey of the
86 /// last RouteHop in each path must be the same.
87 /// Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
88 /// destination. Thus, this must always be at least length one. While the maximum length of any
89 /// given path is variable, keeping the length of any path to less than 20 should currently
90 /// ensure it is viable.
91 pub paths: Vec<Vec<RouteHop>>,
94 impl Writeable for Route {
95 fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
96 (self.paths.len() as u64).write(writer)?;
97 for hops in self.paths.iter() {
104 impl<R: ::std::io::Read> Readable<R> for Route {
105 fn read(reader: &mut R) -> Result<Route, DecodeError> {
106 let path_count: u64 = Readable::read(reader)?;
107 let mut paths = Vec::with_capacity(cmp::min(path_count, 128) as usize);
108 for _ in 0..path_count {
109 paths.push(Readable::read(reader)?);
116 struct DirectionalChannelInfo {
117 src_node_id: PublicKey,
120 cltv_expiry_delta: u16,
121 htlc_minimum_msat: u64,
123 fee_proportional_millionths: u32,
124 last_update_message: Option<msgs::ChannelUpdate>,
127 impl std::fmt::Display for DirectionalChannelInfo {
128 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
129 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)?;
134 impl_writeable!(DirectionalChannelInfo, 0, {
141 fee_proportional_millionths,
147 features: ChannelFeatures,
148 one_to_two: DirectionalChannelInfo,
149 two_to_one: DirectionalChannelInfo,
150 //this is cached here so we can send out it later if required by route_init_sync
151 //keep an eye on this to see if the extra memory is a problem
152 announcement_message: Option<msgs::ChannelAnnouncement>,
155 impl std::fmt::Display for ChannelInfo {
156 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
157 write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?;
162 impl_writeable!(ChannelInfo, 0, {
171 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
172 channels: Vec<(u64, Sha256dHash)>,
173 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
176 lowest_inbound_channel_fee_base_msat: u32,
177 lowest_inbound_channel_fee_proportional_millionths: u32,
179 features: NodeFeatures,
180 last_update: Option<u32>,
183 addresses: Vec<NetAddress>,
184 //this is cached here so we can send out it later if required by route_init_sync
185 //keep an eye on this to see if the extra memory is a problem
186 announcement_message: Option<msgs::NodeAnnouncement>,
189 impl std::fmt::Display for NodeInfo {
190 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
191 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[..])?;
196 impl Writeable for NodeInfo {
197 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
198 (self.channels.len() as u64).write(writer)?;
199 for ref chan in self.channels.iter() {
202 self.lowest_inbound_channel_fee_base_msat.write(writer)?;
203 self.lowest_inbound_channel_fee_proportional_millionths.write(writer)?;
204 self.features.write(writer)?;
205 self.last_update.write(writer)?;
206 self.rgb.write(writer)?;
207 self.alias.write(writer)?;
208 (self.addresses.len() as u64).write(writer)?;
209 for ref addr in &self.addresses {
212 self.announcement_message.write(writer)?;
217 const MAX_ALLOC_SIZE: u64 = 64*1024;
219 impl<R: ::std::io::Read> Readable<R> for NodeInfo {
220 fn read(reader: &mut R) -> Result<NodeInfo, DecodeError> {
221 let channels_count: u64 = Readable::read(reader)?;
222 let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
223 for _ in 0..channels_count {
224 channels.push(Readable::read(reader)?);
226 let lowest_inbound_channel_fee_base_msat = Readable::read(reader)?;
227 let lowest_inbound_channel_fee_proportional_millionths = Readable::read(reader)?;
228 let features = Readable::read(reader)?;
229 let last_update = Readable::read(reader)?;
230 let rgb = Readable::read(reader)?;
231 let alias = Readable::read(reader)?;
232 let addresses_count: u64 = Readable::read(reader)?;
233 let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
234 for _ in 0..addresses_count {
235 match Readable::read(reader) {
236 Ok(Ok(addr)) => { addresses.push(addr); },
237 Ok(Err(_)) => return Err(DecodeError::InvalidValue),
238 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
242 let announcement_message = Readable::read(reader)?;
245 lowest_inbound_channel_fee_base_msat,
246 lowest_inbound_channel_fee_proportional_millionths,
259 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
260 channels: BTreeMap<(u64, Sha256dHash), ChannelInfo>,
261 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
262 channels: BTreeMap<u64, ChannelInfo>,
264 our_node_id: PublicKey,
265 nodes: BTreeMap<PublicKey, NodeInfo>,
268 impl Writeable for NetworkMap {
269 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
270 (self.channels.len() as u64).write(writer)?;
271 for (ref chan_id, ref chan_info) in self.channels.iter() {
272 (*chan_id).write(writer)?;
273 chan_info.write(writer)?;
275 self.our_node_id.write(writer)?;
276 (self.nodes.len() as u64).write(writer)?;
277 for (ref node_id, ref node_info) in self.nodes.iter() {
278 node_id.write(writer)?;
279 node_info.write(writer)?;
285 impl<R: ::std::io::Read> Readable<R> for NetworkMap {
286 fn read(reader: &mut R) -> Result<NetworkMap, DecodeError> {
287 let channels_count: u64 = Readable::read(reader)?;
288 let mut channels = BTreeMap::new();
289 for _ in 0..channels_count {
290 let chan_id: u64 = Readable::read(reader)?;
291 let chan_info = Readable::read(reader)?;
292 channels.insert(chan_id, chan_info);
294 let our_node_id = Readable::read(reader)?;
295 let nodes_count: u64 = Readable::read(reader)?;
296 let mut nodes = BTreeMap::new();
297 for _ in 0..nodes_count {
298 let node_id = Readable::read(reader)?;
299 let node_info = Readable::read(reader)?;
300 nodes.insert(node_id, node_info);
310 impl std::fmt::Display for NetworkMap {
311 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
312 write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
313 for (key, val) in self.channels.iter() {
314 write!(f, " {}: {}\n", key, val)?;
316 write!(f, "[Nodes]\n")?;
317 for (key, val) in self.nodes.iter() {
318 write!(f, " {}: {}\n", log_pubkey!(key), val)?;
325 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
327 fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
328 (short_channel_id, chain_hash)
331 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
333 fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
337 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
339 fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
343 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
345 fn get_short_id(id: &u64) -> &u64 {
350 /// A channel descriptor which provides a last-hop route to get_route
351 pub struct RouteHint {
352 /// The node_id of the non-target end of the route
353 pub src_node_id: PublicKey,
354 /// The short_channel_id of this channel
355 pub short_channel_id: u64,
356 /// The static msat-denominated fee which must be paid to use this channel
357 pub fee_base_msat: u32,
358 /// The dynamic proportional fee which must be paid to use this channel, denominated in
359 /// millionths of the value being forwarded to the next hop.
360 pub fee_proportional_millionths: u32,
361 /// The difference in CLTV values between this node and the next node.
362 pub cltv_expiry_delta: u16,
363 /// The minimum value, in msat, which must be relayed to the next hop.
364 pub htlc_minimum_msat: u64,
367 /// Tracks a view of the network, receiving updates from peers and generating Routes to
368 /// payment destinations.
370 secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
371 network_map: RwLock<NetworkMap>,
372 chain_monitor: Arc<ChainWatchInterface>,
376 const SERIALIZATION_VERSION: u8 = 1;
377 const MIN_SERIALIZATION_VERSION: u8 = 1;
379 impl Writeable for Router {
380 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
381 writer.write_all(&[SERIALIZATION_VERSION; 1])?;
382 writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
384 let network = self.network_map.read().unwrap();
385 network.write(writer)?;
390 /// Arguments for the creation of a Router that are not deserialized.
391 /// At a high-level, the process for deserializing a Router and resuming normal operation is:
392 /// 1) Deserialize the Router by filling in this struct and calling <Router>::read(reaser, args).
393 /// 2) Register the new Router with your ChainWatchInterface
394 pub struct RouterReadArgs {
395 /// The ChainWatchInterface for use in the Router in the future.
397 /// No calls to the ChainWatchInterface will be made during deserialization.
398 pub chain_monitor: Arc<ChainWatchInterface>,
399 /// The Logger for use in the ChannelManager and which may be used to log information during
401 pub logger: Arc<Logger>,
404 impl<R: ::std::io::Read> ReadableArgs<R, RouterReadArgs> for Router {
405 fn read(reader: &mut R, args: RouterReadArgs) -> Result<Router, DecodeError> {
406 let _ver: u8 = Readable::read(reader)?;
407 let min_ver: u8 = Readable::read(reader)?;
408 if min_ver > SERIALIZATION_VERSION {
409 return Err(DecodeError::UnknownVersion);
411 let network_map = Readable::read(reader)?;
413 secp_ctx: Secp256k1::verification_only(),
414 network_map: RwLock::new(network_map),
415 chain_monitor: args.chain_monitor,
421 macro_rules! secp_verify_sig {
422 ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
423 match $secp_ctx.verify($msg, $sig, $pubkey) {
425 Err(_) => return Err(LightningError{err: "Invalid signature from remote node", action: ErrorAction::IgnoreError}),
430 impl RoutingMessageHandler for Router {
431 fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
432 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
433 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
435 let mut network = self.network_map.write().unwrap();
436 match network.nodes.get_mut(&msg.contents.node_id) {
437 None => Err(LightningError{err: "No existing channels for node_announcement", action: ErrorAction::IgnoreError}),
439 match node.last_update {
440 Some(last_update) => if last_update >= msg.contents.timestamp {
441 return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
446 node.features = msg.contents.features.clone();
447 node.last_update = Some(msg.contents.timestamp);
448 node.rgb = msg.contents.rgb;
449 node.alias = msg.contents.alias;
450 node.addresses = msg.contents.addresses.clone();
452 let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty();
453 node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
459 fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
460 if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
461 return Err(LightningError{err: "Channel announcement node had a channel with itself", action: ErrorAction::IgnoreError});
464 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
465 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
466 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
467 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
468 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
470 let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
471 Ok((script_pubkey, _value)) => {
472 let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
473 .push_slice(&msg.contents.bitcoin_key_1.serialize())
474 .push_slice(&msg.contents.bitcoin_key_2.serialize())
475 .push_opcode(opcodes::all::OP_PUSHNUM_2)
476 .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
477 if script_pubkey != expected_script {
478 return Err(LightningError{err: "Channel announcement keys didn't match on-chain script", action: ErrorAction::IgnoreError});
480 //TODO: Check if value is worth storing, use it to inform routing, and compare it
481 //to the new HTLC max field in channel_update
484 Err(ChainError::NotSupported) => {
485 // Tentatively accept, potentially exposing us to DoS attacks
488 Err(ChainError::NotWatched) => {
489 return Err(LightningError{err: "Channel announced on an unknown chain", action: ErrorAction::IgnoreError});
491 Err(ChainError::UnknownTx) => {
492 return Err(LightningError{err: "Channel announced without corresponding UTXO entry", action: ErrorAction::IgnoreError});
496 let mut network_lock = self.network_map.write().unwrap();
497 let network = &mut *network_lock;
499 let should_relay = msg.contents.excess_data.is_empty();
501 let chan_info = ChannelInfo {
502 features: msg.contents.features.clone(),
503 one_to_two: DirectionalChannelInfo {
504 src_node_id: msg.contents.node_id_1.clone(),
507 cltv_expiry_delta: u16::max_value(),
508 htlc_minimum_msat: u64::max_value(),
509 fee_base_msat: u32::max_value(),
510 fee_proportional_millionths: u32::max_value(),
511 last_update_message: None,
513 two_to_one: DirectionalChannelInfo {
514 src_node_id: msg.contents.node_id_2.clone(),
517 cltv_expiry_delta: u16::max_value(),
518 htlc_minimum_msat: u64::max_value(),
519 fee_base_msat: u32::max_value(),
520 fee_proportional_millionths: u32::max_value(),
521 last_update_message: None,
523 announcement_message: if should_relay { Some(msg.clone()) } else { None },
526 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
527 BtreeEntry::Occupied(mut entry) => {
528 //TODO: because asking the blockchain if short_channel_id is valid is only optional
529 //in the blockchain API, we need to handle it smartly here, though it's unclear
532 // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
533 // only sometimes returns results. In any case remove the previous entry. Note
534 // that the spec expects us to "blacklist" the node_ids involved, but we can't
536 // a) we don't *require* a UTXO provider that always returns results.
537 // b) we don't track UTXOs of channels we know about and remove them if they
539 // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
540 Self::remove_channel_in_nodes(&mut network.nodes, &entry.get(), msg.contents.short_channel_id);
541 *entry.get_mut() = chan_info;
543 return Err(LightningError{err: "Already have knowledge of channel", action: ErrorAction::IgnoreError})
546 BtreeEntry::Vacant(entry) => {
547 entry.insert(chan_info);
551 macro_rules! add_channel_to_node {
552 ( $node_id: expr ) => {
553 match network.nodes.entry($node_id) {
554 BtreeEntry::Occupied(node_entry) => {
555 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
557 BtreeEntry::Vacant(node_entry) => {
558 node_entry.insert(NodeInfo {
559 channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
560 lowest_inbound_channel_fee_base_msat: u32::max_value(),
561 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
562 features: NodeFeatures::empty(),
566 addresses: Vec::new(),
567 announcement_message: None,
574 add_channel_to_node!(msg.contents.node_id_1);
575 add_channel_to_node!(msg.contents.node_id_2);
580 fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
582 &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
583 let _ = self.handle_channel_update(msg);
585 &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
586 let mut network = self.network_map.write().unwrap();
588 if let Some(chan) = network.channels.remove(short_channel_id) {
589 Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
592 if let Some(chan) = network.channels.get_mut(short_channel_id) {
593 chan.one_to_two.enabled = false;
594 chan.two_to_one.enabled = false;
598 &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
600 //TODO: Wholly remove the node
602 self.mark_node_bad(node_id, false);
608 fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> {
609 let mut network = self.network_map.write().unwrap();
611 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
612 let chan_was_enabled;
614 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
615 None => return Err(LightningError{err: "Couldn't find channel for update", action: ErrorAction::IgnoreError}),
617 macro_rules! maybe_update_channel_info {
618 ( $target: expr) => {
619 if $target.last_update >= msg.contents.timestamp {
620 return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
622 chan_was_enabled = $target.enabled;
623 $target.last_update = msg.contents.timestamp;
624 $target.enabled = chan_enabled;
625 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
626 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
627 $target.fee_base_msat = msg.contents.fee_base_msat;
628 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
629 $target.last_update_message = if msg.contents.excess_data.is_empty() {
636 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
637 if msg.contents.flags & 1 == 1 {
638 dest_node_id = channel.one_to_two.src_node_id.clone();
639 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
640 maybe_update_channel_info!(channel.two_to_one);
642 dest_node_id = channel.two_to_one.src_node_id.clone();
643 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
644 maybe_update_channel_info!(channel.one_to_two);
650 let node = network.nodes.get_mut(&dest_node_id).unwrap();
651 node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
652 node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
653 } else if chan_was_enabled {
654 let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
655 let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
658 let node = network.nodes.get(&dest_node_id).unwrap();
660 for chan_id in node.channels.iter() {
661 let chan = network.channels.get(chan_id).unwrap();
662 if chan.one_to_two.src_node_id == dest_node_id {
663 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
664 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
666 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
667 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
672 //TODO: satisfy the borrow-checker without a double-map-lookup :(
673 let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
674 mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
675 mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
678 Ok(msg.contents.excess_data.is_empty())
682 fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
683 let mut result = Vec::with_capacity(batch_amount as usize);
684 let network = self.network_map.read().unwrap();
685 let mut iter = network.channels.range(starting_point..);
686 while result.len() < batch_amount as usize {
687 if let Some((_, ref chan)) = iter.next() {
688 if chan.announcement_message.is_some() &&
689 chan.one_to_two.last_update_message.is_some() &&
690 chan.two_to_one.last_update_message.is_some() {
691 result.push((chan.announcement_message.clone().unwrap(),
692 chan.one_to_two.last_update_message.clone().unwrap(),
693 chan.two_to_one.last_update_message.clone().unwrap()));
695 // TODO: We may end up sending un-announced channel_updates if we are sending
696 // initial sync data while receiving announce/updates for this channel.
705 fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
706 let mut result = Vec::with_capacity(batch_amount as usize);
707 let network = self.network_map.read().unwrap();
708 let mut iter = if let Some(pubkey) = starting_point {
709 let mut iter = network.nodes.range((*pubkey)..);
713 network.nodes.range(..)
715 while result.len() < batch_amount as usize {
716 if let Some((_, ref node)) = iter.next() {
717 if node.announcement_message.is_some() {
718 result.push(node.announcement_message.clone().unwrap());
728 #[derive(Eq, PartialEq)]
729 struct RouteGraphNode {
731 lowest_fee_to_peer_through_node: u64,
732 lowest_fee_to_node: u64,
735 impl cmp::Ord for RouteGraphNode {
736 fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
737 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
738 .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
742 impl cmp::PartialOrd for RouteGraphNode {
743 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
744 Some(self.cmp(other))
748 struct DummyDirectionalChannelInfo {
749 src_node_id: PublicKey,
750 cltv_expiry_delta: u32,
751 htlc_minimum_msat: u64,
753 fee_proportional_millionths: u32,
757 /// Creates a new router with the given node_id to be used as the source for get_route()
758 pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
759 let mut nodes = BTreeMap::new();
760 nodes.insert(our_pubkey.clone(), NodeInfo {
761 channels: Vec::new(),
762 lowest_inbound_channel_fee_base_msat: u32::max_value(),
763 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
764 features: NodeFeatures::empty(),
768 addresses: Vec::new(),
769 announcement_message: None,
772 secp_ctx: Secp256k1::verification_only(),
773 network_map: RwLock::new(NetworkMap {
774 channels: BTreeMap::new(),
775 our_node_id: our_pubkey,
783 /// Dumps the entire network view of this Router to the logger provided in the constructor at
785 pub fn trace_state(&self) {
786 log_trace!(self, "{}", self.network_map.read().unwrap());
789 /// Get network addresses by node id
790 pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
791 let network = self.network_map.read().unwrap();
792 network.nodes.get(pubkey).map(|n| n.addresses.clone())
795 /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
796 /// with an exponential decay in node "badness". Note that there is deliberately no
797 /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
798 /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
799 /// boolean will reduce the penalty, returning the node to usability faster. If the node is
800 /// behaving correctly, it will disable the failing channel and we will use it again next time.
801 pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
805 fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
806 macro_rules! remove_from_node {
807 ($node_id: expr) => {
808 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
809 entry.get_mut().channels.retain(|chan_id| {
810 short_channel_id != *NetworkMap::get_short_id(chan_id)
812 if entry.get().channels.is_empty() {
813 entry.remove_entry();
816 panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
820 remove_from_node!(chan.one_to_two.src_node_id);
821 remove_from_node!(chan.two_to_one.src_node_id);
824 /// Gets a route from us to the given target node.
826 /// Extra routing hops between known nodes and the target will be used if they are included in
829 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
830 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
831 /// (this Router's) view of our local channels will be ignored, and only those in first_hops
834 /// Panics if first_hops contains channels without short_channel_ids
835 /// (ChannelManager::list_usable_channels will never include such channels).
837 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
838 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
839 /// *is* checked as they may change based on the receiving node.
840 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> {
841 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
842 // uptime/success in using a node in the past.
843 let network = self.network_map.read().unwrap();
845 if *target == network.our_node_id {
846 return Err(LightningError{err: "Cannot generate a route to ourselves", action: ErrorAction::IgnoreError});
849 if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
850 return Err(LightningError{err: "Cannot generate a route of more value than all existing satoshis", action: ErrorAction::IgnoreError});
853 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
854 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
855 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
856 // to use as the A* heuristic beyond just the cost to get one node further than the current
859 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
860 src_node_id: network.our_node_id.clone(),
861 cltv_expiry_delta: 0,
862 htlc_minimum_msat: 0,
864 fee_proportional_millionths: 0,
867 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
868 let mut dist = HashMap::with_capacity(network.nodes.len());
870 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
871 if let Some(hops) = first_hops {
873 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
874 if chan.remote_network_id == *target {
876 paths: vec![vec![RouteHop {
877 pubkey: chan.remote_network_id,
878 node_features: NodeFeatures::with_known_relevant_init_flags(&chan.counterparty_features),
880 channel_features: ChannelFeatures::with_known_relevant_init_flags(&chan.counterparty_features),
881 fee_msat: final_value_msat,
882 cltv_expiry_delta: final_cltv,
886 first_hop_targets.insert(chan.remote_network_id, (short_channel_id, chan.counterparty_features.clone()));
888 if first_hop_targets.is_empty() {
889 return Err(LightningError{err: "Cannot route when there are no outbound routes away from us", action: ErrorAction::IgnoreError});
893 macro_rules! add_entry {
894 // Adds entry which goes from the node pointed to by $directional_info to
895 // $dest_node_id over the channel with id $chan_id with fees described in
896 // $directional_info.
897 ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $chan_features: expr, $starting_fee_msat: expr ) => {
898 //TODO: Explore simply adding fee to hit htlc_minimum_msat
899 if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
900 let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
901 if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
902 ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
904 let mut total_fee = $starting_fee_msat as u64;
905 let hm_entry = dist.entry(&$directional_info.src_node_id);
906 let old_entry = hm_entry.or_insert_with(|| {
907 let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
909 node.lowest_inbound_channel_fee_base_msat,
910 node.lowest_inbound_channel_fee_proportional_millionths,
912 pubkey: $dest_node_id.clone(),
913 node_features: NodeFeatures::empty(),
915 channel_features: $chan_features.clone(),
917 cltv_expiry_delta: 0,
920 if $directional_info.src_node_id != network.our_node_id {
921 // Ignore new_fee for channel-from-us as we assume all channels-from-us
922 // will have the same effective-fee
923 total_fee += new_fee;
924 if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
925 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
927 // max_value means we'll always fail the old_entry.0 > total_fee check
928 total_fee = u64::max_value();
931 let new_graph_node = RouteGraphNode {
932 pubkey: $directional_info.src_node_id,
933 lowest_fee_to_peer_through_node: total_fee,
934 lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
936 if old_entry.0 > total_fee {
937 targets.push(new_graph_node);
938 old_entry.0 = total_fee;
939 old_entry.3 = RouteHop {
940 pubkey: $dest_node_id.clone(),
941 node_features: NodeFeatures::empty(),
942 short_channel_id: $chan_id.clone(),
943 channel_features: $chan_features.clone(),
944 fee_msat: new_fee, // This field is ignored on the last-hop anyway
945 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
953 macro_rules! add_entries_to_cheapest_to_target_node {
954 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
955 if first_hops.is_some() {
956 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&$node_id) {
957 add_entry!(first_hop, $node_id, dummy_directional_info, ChannelFeatures::with_known_relevant_init_flags(&features), $fee_to_target_msat);
961 if !$node.features.requires_unknown_bits() {
962 for chan_id in $node.channels.iter() {
963 let chan = network.channels.get(chan_id).unwrap();
964 if !chan.features.requires_unknown_bits() {
965 if chan.one_to_two.src_node_id == *$node_id {
966 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
967 if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
968 if chan.two_to_one.enabled {
969 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, chan.features, $fee_to_target_msat);
973 if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
974 if chan.one_to_two.enabled {
975 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, chan.features, $fee_to_target_msat);
985 match network.nodes.get(target) {
988 add_entries_to_cheapest_to_target_node!(node, target, 0);
992 for hop in last_hops.iter() {
993 if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
994 if network.nodes.get(&hop.src_node_id).is_some() {
995 if first_hops.is_some() {
996 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&hop.src_node_id) {
997 // Currently there are no channel-context features defined, so we are a
998 // bit lazy here. In the future, we should pull them out via our
999 // ChannelManager, but there's no reason to waste the space until we
1001 add_entry!(first_hop, hop.src_node_id, dummy_directional_info, ChannelFeatures::with_known_relevant_init_flags(&features), 0);
1004 // BOLT 11 doesn't allow inclusion of features for the last hop hints, which
1005 // really sucks, cause we're gonna need that eventually.
1006 add_entry!(hop.short_channel_id, target, hop, ChannelFeatures::empty(), 0);
1011 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
1012 if pubkey == network.our_node_id {
1013 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
1015 if let Some(&(_, ref features)) = first_hop_targets.get(&res.last().unwrap().pubkey) {
1016 res.last_mut().unwrap().node_features = NodeFeatures::with_known_relevant_init_flags(&features);
1017 } else if let Some(node) = network.nodes.get(&res.last().unwrap().pubkey) {
1018 res.last_mut().unwrap().node_features = node.features.clone();
1020 // We should be able to fill in features for everything except the last
1021 // hop, if the last hop was provided via a BOLT 11 invoice (though we
1022 // should be able to extend it further as BOLT 11 does have feature
1023 // flags for the last hop node itself).
1024 assert!(res.last().unwrap().pubkey == *target);
1026 if res.last().unwrap().pubkey == *target {
1030 let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
1032 None => return Err(LightningError{err: "Failed to find a non-fee-overflowing path to the given destination", action: ErrorAction::IgnoreError}),
1034 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
1035 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
1036 res.push(new_entry);
1038 res.last_mut().unwrap().fee_msat = final_value_msat;
1039 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
1040 let route = Route { paths: vec![res] };
1041 log_trace!(self, "Got route: {}", log_route!(route));
1045 match network.nodes.get(&pubkey) {
1048 add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
1053 Err(LightningError{err: "Failed to find a path to the given destination", action: ErrorAction::IgnoreError})
1059 use chain::chaininterface;
1060 use ln::channelmanager;
1061 use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
1062 use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
1063 use ln::msgs::{LightningError, ErrorAction};
1064 use util::test_utils;
1065 use util::test_utils::TestVecWriter;
1066 use util::logger::Logger;
1067 use util::ser::{Writeable, Readable};
1069 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1070 use bitcoin_hashes::Hash;
1071 use bitcoin::network::constants::Network;
1075 use secp256k1::key::{PublicKey,SecretKey};
1076 use secp256k1::Secp256k1;
1082 let secp_ctx = Secp256k1::new();
1083 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
1084 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1085 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
1086 let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
1088 // Build network from our_id to node8:
1090 // -1(1)2- node1 -1(3)2-
1092 // our_id -1(12)2- node8 -1(13)2--- node3
1094 // -1(2)2- node2 -1(4)2-
1097 // chan1 1-to-2: disabled
1098 // chan1 2-to-1: enabled, 0 fee
1100 // chan2 1-to-2: enabled, ignored fee
1101 // chan2 2-to-1: enabled, 0 fee
1103 // chan3 1-to-2: enabled, 0 fee
1104 // chan3 2-to-1: enabled, 100 msat fee
1106 // chan4 1-to-2: enabled, 100% fee
1107 // chan4 2-to-1: enabled, 0 fee
1109 // chan12 1-to-2: enabled, ignored fee
1110 // chan12 2-to-1: enabled, 0 fee
1112 // chan13 1-to-2: enabled, 200% fee
1113 // chan13 2-to-1: enabled, 0 fee
1116 // -1(5)2- node4 -1(8)2--
1120 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
1122 // -1(7)2- node6 -1(10)2-
1124 // chan5 1-to-2: enabled, 100 msat fee
1125 // chan5 2-to-1: enabled, 0 fee
1127 // chan6 1-to-2: enabled, 0 fee
1128 // chan6 2-to-1: enabled, 0 fee
1130 // chan7 1-to-2: enabled, 100% fee
1131 // chan7 2-to-1: enabled, 0 fee
1133 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
1134 // chan8 2-to-1: enabled, 0 fee
1136 // chan9 1-to-2: enabled, 1001 msat fee
1137 // chan9 2-to-1: enabled, 0 fee
1139 // chan10 1-to-2: enabled, 0 fee
1140 // chan10 2-to-1: enabled, 0 fee
1142 // chan11 1-to-2: enabled, 0 fee
1143 // chan11 2-to-1: enabled, 0 fee
1145 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
1146 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
1147 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
1148 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
1149 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
1150 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
1151 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
1152 let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
1154 let zero_hash = Sha256dHash::hash(&[0; 32]);
1156 macro_rules! id_to_feature_flags {
1157 // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can
1158 // test for it later.
1160 let idx = ($id - 1) * 2 + 1;
1162 vec![1 << (idx - 8*3), 0, 0, 0]
1163 } else if idx > 8*2 {
1164 vec![1 << (idx - 8*2), 0, 0]
1165 } else if idx > 8*1 {
1166 vec![1 << (idx - 8*1), 0]
1174 let mut network = router.network_map.write().unwrap();
1176 network.nodes.insert(node1.clone(), NodeInfo {
1177 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
1178 lowest_inbound_channel_fee_base_msat: 100,
1179 lowest_inbound_channel_fee_proportional_millionths: 0,
1180 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(1)),
1181 last_update: Some(1),
1184 addresses: Vec::new(),
1185 announcement_message: None,
1187 network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
1188 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(1)),
1189 one_to_two: DirectionalChannelInfo {
1190 src_node_id: our_id.clone(),
1193 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1194 htlc_minimum_msat: 0,
1195 fee_base_msat: u32::max_value(), // This value should be ignored
1196 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1197 last_update_message: None,
1198 }, two_to_one: DirectionalChannelInfo {
1199 src_node_id: node1.clone(),
1202 cltv_expiry_delta: 0,
1203 htlc_minimum_msat: 0,
1205 fee_proportional_millionths: 0,
1206 last_update_message: None,
1208 announcement_message: None,
1210 network.nodes.insert(node2.clone(), NodeInfo {
1211 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
1212 lowest_inbound_channel_fee_base_msat: 0,
1213 lowest_inbound_channel_fee_proportional_millionths: 0,
1214 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(2)),
1215 last_update: Some(1),
1218 addresses: Vec::new(),
1219 announcement_message: None,
1221 network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
1222 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(2)),
1223 one_to_two: DirectionalChannelInfo {
1224 src_node_id: our_id.clone(),
1227 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1228 htlc_minimum_msat: 0,
1229 fee_base_msat: u32::max_value(), // This value should be ignored
1230 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1231 last_update_message: None,
1232 }, two_to_one: DirectionalChannelInfo {
1233 src_node_id: node2.clone(),
1236 cltv_expiry_delta: 0,
1237 htlc_minimum_msat: 0,
1239 fee_proportional_millionths: 0,
1240 last_update_message: None,
1242 announcement_message: None,
1244 network.nodes.insert(node8.clone(), NodeInfo {
1245 channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
1246 lowest_inbound_channel_fee_base_msat: 0,
1247 lowest_inbound_channel_fee_proportional_millionths: 0,
1248 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(8)),
1249 last_update: Some(1),
1252 addresses: Vec::new(),
1253 announcement_message: None,
1255 network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
1256 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(12)),
1257 one_to_two: DirectionalChannelInfo {
1258 src_node_id: our_id.clone(),
1261 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1262 htlc_minimum_msat: 0,
1263 fee_base_msat: u32::max_value(), // This value should be ignored
1264 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1265 last_update_message: None,
1266 }, two_to_one: DirectionalChannelInfo {
1267 src_node_id: node8.clone(),
1270 cltv_expiry_delta: 0,
1271 htlc_minimum_msat: 0,
1273 fee_proportional_millionths: 0,
1274 last_update_message: None,
1276 announcement_message: None,
1278 network.nodes.insert(node3.clone(), NodeInfo {
1280 NetworkMap::get_key(3, zero_hash.clone()),
1281 NetworkMap::get_key(4, zero_hash.clone()),
1282 NetworkMap::get_key(13, zero_hash.clone()),
1283 NetworkMap::get_key(5, zero_hash.clone()),
1284 NetworkMap::get_key(6, zero_hash.clone()),
1285 NetworkMap::get_key(7, zero_hash.clone())),
1286 lowest_inbound_channel_fee_base_msat: 0,
1287 lowest_inbound_channel_fee_proportional_millionths: 0,
1288 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(3)),
1289 last_update: Some(1),
1292 addresses: Vec::new(),
1293 announcement_message: None,
1295 network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
1296 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(3)),
1297 one_to_two: DirectionalChannelInfo {
1298 src_node_id: node1.clone(),
1301 cltv_expiry_delta: (3 << 8) | 1,
1302 htlc_minimum_msat: 0,
1304 fee_proportional_millionths: 0,
1305 last_update_message: None,
1306 }, two_to_one: DirectionalChannelInfo {
1307 src_node_id: node3.clone(),
1310 cltv_expiry_delta: (3 << 8) | 2,
1311 htlc_minimum_msat: 0,
1313 fee_proportional_millionths: 0,
1314 last_update_message: None,
1316 announcement_message: None,
1318 network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
1319 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(4)),
1320 one_to_two: DirectionalChannelInfo {
1321 src_node_id: node2.clone(),
1324 cltv_expiry_delta: (4 << 8) | 1,
1325 htlc_minimum_msat: 0,
1327 fee_proportional_millionths: 1000000,
1328 last_update_message: None,
1329 }, two_to_one: DirectionalChannelInfo {
1330 src_node_id: node3.clone(),
1333 cltv_expiry_delta: (4 << 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(13, zero_hash.clone()), ChannelInfo {
1342 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(13)),
1343 one_to_two: DirectionalChannelInfo {
1344 src_node_id: node8.clone(),
1347 cltv_expiry_delta: (13 << 8) | 1,
1348 htlc_minimum_msat: 0,
1350 fee_proportional_millionths: 2000000,
1351 last_update_message: None,
1352 }, two_to_one: DirectionalChannelInfo {
1353 src_node_id: node3.clone(),
1356 cltv_expiry_delta: (13 << 8) | 2,
1357 htlc_minimum_msat: 0,
1359 fee_proportional_millionths: 0,
1360 last_update_message: None,
1362 announcement_message: None,
1364 network.nodes.insert(node4.clone(), NodeInfo {
1365 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1366 lowest_inbound_channel_fee_base_msat: 0,
1367 lowest_inbound_channel_fee_proportional_millionths: 0,
1368 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(4)),
1369 last_update: Some(1),
1372 addresses: Vec::new(),
1373 announcement_message: None,
1375 network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
1376 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(5)),
1377 one_to_two: DirectionalChannelInfo {
1378 src_node_id: node3.clone(),
1381 cltv_expiry_delta: (5 << 8) | 1,
1382 htlc_minimum_msat: 0,
1384 fee_proportional_millionths: 0,
1385 last_update_message: None,
1386 }, two_to_one: DirectionalChannelInfo {
1387 src_node_id: node4.clone(),
1390 cltv_expiry_delta: (5 << 8) | 2,
1391 htlc_minimum_msat: 0,
1393 fee_proportional_millionths: 0,
1394 last_update_message: None,
1396 announcement_message: None,
1398 network.nodes.insert(node5.clone(), NodeInfo {
1399 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1400 lowest_inbound_channel_fee_base_msat: 0,
1401 lowest_inbound_channel_fee_proportional_millionths: 0,
1402 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(5)),
1403 last_update: Some(1),
1406 addresses: Vec::new(),
1407 announcement_message: None,
1409 network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
1410 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(6)),
1411 one_to_two: DirectionalChannelInfo {
1412 src_node_id: node3.clone(),
1415 cltv_expiry_delta: (6 << 8) | 1,
1416 htlc_minimum_msat: 0,
1418 fee_proportional_millionths: 0,
1419 last_update_message: None,
1420 }, two_to_one: DirectionalChannelInfo {
1421 src_node_id: node5.clone(),
1424 cltv_expiry_delta: (6 << 8) | 2,
1425 htlc_minimum_msat: 0,
1427 fee_proportional_millionths: 0,
1428 last_update_message: None,
1430 announcement_message: None,
1432 network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
1433 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(11)),
1434 one_to_two: DirectionalChannelInfo {
1435 src_node_id: node5.clone(),
1438 cltv_expiry_delta: (11 << 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: node4.clone(),
1447 cltv_expiry_delta: (11 << 8) | 2,
1448 htlc_minimum_msat: 0,
1450 fee_proportional_millionths: 0,
1451 last_update_message: None,
1453 announcement_message: None,
1455 network.nodes.insert(node6.clone(), NodeInfo {
1456 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
1457 lowest_inbound_channel_fee_base_msat: 0,
1458 lowest_inbound_channel_fee_proportional_millionths: 0,
1459 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(6)),
1460 last_update: Some(1),
1463 addresses: Vec::new(),
1464 announcement_message: None,
1466 network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
1467 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(7)),
1468 one_to_two: DirectionalChannelInfo {
1469 src_node_id: node3.clone(),
1472 cltv_expiry_delta: (7 << 8) | 1,
1473 htlc_minimum_msat: 0,
1475 fee_proportional_millionths: 1000000,
1476 last_update_message: None,
1477 }, two_to_one: DirectionalChannelInfo {
1478 src_node_id: node6.clone(),
1481 cltv_expiry_delta: (7 << 8) | 2,
1482 htlc_minimum_msat: 0,
1484 fee_proportional_millionths: 0,
1485 last_update_message: None,
1487 announcement_message: None,
1491 { // Simple route to 3 via 2
1492 let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
1493 assert_eq!(route.paths[0].len(), 2);
1495 assert_eq!(route.paths[0][0].pubkey, node2);
1496 assert_eq!(route.paths[0][0].short_channel_id, 2);
1497 assert_eq!(route.paths[0][0].fee_msat, 100);
1498 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1499 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1500 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1502 assert_eq!(route.paths[0][1].pubkey, node3);
1503 assert_eq!(route.paths[0][1].short_channel_id, 4);
1504 assert_eq!(route.paths[0][1].fee_msat, 100);
1505 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1506 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1507 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1510 { // Disable channels 4 and 12 by requiring unknown feature bits
1511 let mut network = router.network_map.write().unwrap();
1512 network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1513 network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1516 { // If all the channels require some features we don't understand, route should fail
1517 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1518 assert_eq!(err, "Failed to find a path to the given destination");
1519 } else { panic!(); }
1522 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1523 let our_chans = vec![channelmanager::ChannelDetails {
1524 channel_id: [0; 32],
1525 short_channel_id: Some(42),
1526 remote_network_id: node8.clone(),
1527 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1528 channel_value_satoshis: 0,
1530 outbound_capacity_msat: 0,
1531 inbound_capacity_msat: 0,
1534 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1535 assert_eq!(route.paths[0].len(), 2);
1537 assert_eq!(route.paths[0][0].pubkey, node8);
1538 assert_eq!(route.paths[0][0].short_channel_id, 42);
1539 assert_eq!(route.paths[0][0].fee_msat, 200);
1540 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
1541 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
1542 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion
1544 assert_eq!(route.paths[0][1].pubkey, node3);
1545 assert_eq!(route.paths[0][1].short_channel_id, 13);
1546 assert_eq!(route.paths[0][1].fee_msat, 100);
1547 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1548 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1549 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13));
1552 { // Re-enable channels 4 and 12 by wiping the unknown feature bits
1553 let mut network = router.network_map.write().unwrap();
1554 network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1555 network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1558 { // Disable nodes 1, 2, and 8 by requiring unknown feature bits
1559 let mut network = router.network_map.write().unwrap();
1560 network.nodes.get_mut(&node1).unwrap().features.set_require_unknown_bits();
1561 network.nodes.get_mut(&node2).unwrap().features.set_require_unknown_bits();
1562 network.nodes.get_mut(&node8).unwrap().features.set_require_unknown_bits();
1565 { // If all nodes require some features we don't understand, route should fail
1566 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1567 assert_eq!(err, "Failed to find a path to the given destination");
1568 } else { panic!(); }
1571 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1572 let our_chans = vec![channelmanager::ChannelDetails {
1573 channel_id: [0; 32],
1574 short_channel_id: Some(42),
1575 remote_network_id: node8.clone(),
1576 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1577 channel_value_satoshis: 0,
1579 outbound_capacity_msat: 0,
1580 inbound_capacity_msat: 0,
1583 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1584 assert_eq!(route.paths[0].len(), 2);
1586 assert_eq!(route.paths[0][0].pubkey, node8);
1587 assert_eq!(route.paths[0][0].short_channel_id, 42);
1588 assert_eq!(route.paths[0][0].fee_msat, 200);
1589 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
1590 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
1591 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion
1593 assert_eq!(route.paths[0][1].pubkey, node3);
1594 assert_eq!(route.paths[0][1].short_channel_id, 13);
1595 assert_eq!(route.paths[0][1].fee_msat, 100);
1596 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1597 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1598 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13));
1601 { // Re-enable nodes 1, 2, and 8
1602 let mut network = router.network_map.write().unwrap();
1603 network.nodes.get_mut(&node1).unwrap().features.clear_require_unknown_bits();
1604 network.nodes.get_mut(&node2).unwrap().features.clear_require_unknown_bits();
1605 network.nodes.get_mut(&node8).unwrap().features.clear_require_unknown_bits();
1608 // Note that we don't test disabling node 3 and failing to route to it, as we (somewhat
1609 // naively) assume that the user checked the feature bits on the invoice, which override
1610 // the node_announcement.
1612 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
1613 let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
1614 assert_eq!(route.paths[0].len(), 3);
1616 assert_eq!(route.paths[0][0].pubkey, node2);
1617 assert_eq!(route.paths[0][0].short_channel_id, 2);
1618 assert_eq!(route.paths[0][0].fee_msat, 200);
1619 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1620 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1621 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1623 assert_eq!(route.paths[0][1].pubkey, node3);
1624 assert_eq!(route.paths[0][1].short_channel_id, 4);
1625 assert_eq!(route.paths[0][1].fee_msat, 100);
1626 assert_eq!(route.paths[0][1].cltv_expiry_delta, (3 << 8) | 2);
1627 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1628 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1630 assert_eq!(route.paths[0][2].pubkey, node1);
1631 assert_eq!(route.paths[0][2].short_channel_id, 3);
1632 assert_eq!(route.paths[0][2].fee_msat, 100);
1633 assert_eq!(route.paths[0][2].cltv_expiry_delta, 42);
1634 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(1));
1635 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(3));
1638 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1639 let our_chans = vec![channelmanager::ChannelDetails {
1640 channel_id: [0; 32],
1641 short_channel_id: Some(42),
1642 remote_network_id: node8.clone(),
1643 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1644 channel_value_satoshis: 0,
1646 outbound_capacity_msat: 0,
1647 inbound_capacity_msat: 0,
1650 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1651 assert_eq!(route.paths[0].len(), 2);
1653 assert_eq!(route.paths[0][0].pubkey, node8);
1654 assert_eq!(route.paths[0][0].short_channel_id, 42);
1655 assert_eq!(route.paths[0][0].fee_msat, 200);
1656 assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
1657 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
1658 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion
1660 assert_eq!(route.paths[0][1].pubkey, node3);
1661 assert_eq!(route.paths[0][1].short_channel_id, 13);
1662 assert_eq!(route.paths[0][1].fee_msat, 100);
1663 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1664 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1665 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13));
1668 let mut last_hops = vec!(RouteHint {
1669 src_node_id: node4.clone(),
1670 short_channel_id: 8,
1672 fee_proportional_millionths: 0,
1673 cltv_expiry_delta: (8 << 8) | 1,
1674 htlc_minimum_msat: 0,
1676 src_node_id: node5.clone(),
1677 short_channel_id: 9,
1678 fee_base_msat: 1001,
1679 fee_proportional_millionths: 0,
1680 cltv_expiry_delta: (9 << 8) | 1,
1681 htlc_minimum_msat: 0,
1683 src_node_id: node6.clone(),
1684 short_channel_id: 10,
1686 fee_proportional_millionths: 0,
1687 cltv_expiry_delta: (10 << 8) | 1,
1688 htlc_minimum_msat: 0,
1691 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
1692 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1693 assert_eq!(route.paths[0].len(), 5);
1695 assert_eq!(route.paths[0][0].pubkey, node2);
1696 assert_eq!(route.paths[0][0].short_channel_id, 2);
1697 assert_eq!(route.paths[0][0].fee_msat, 100);
1698 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1699 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1700 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1702 assert_eq!(route.paths[0][1].pubkey, node3);
1703 assert_eq!(route.paths[0][1].short_channel_id, 4);
1704 assert_eq!(route.paths[0][1].fee_msat, 0);
1705 assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1);
1706 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1707 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1709 assert_eq!(route.paths[0][2].pubkey, node5);
1710 assert_eq!(route.paths[0][2].short_channel_id, 6);
1711 assert_eq!(route.paths[0][2].fee_msat, 0);
1712 assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1);
1713 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(5));
1714 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(6));
1716 assert_eq!(route.paths[0][3].pubkey, node4);
1717 assert_eq!(route.paths[0][3].short_channel_id, 11);
1718 assert_eq!(route.paths[0][3].fee_msat, 0);
1719 assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1);
1720 // If we have a peer in the node map, we'll use their features here since we don't have
1721 // a way of figuring out their features from the invoice:
1722 assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags!(4));
1723 assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags!(11));
1725 assert_eq!(route.paths[0][4].pubkey, node7);
1726 assert_eq!(route.paths[0][4].short_channel_id, 8);
1727 assert_eq!(route.paths[0][4].fee_msat, 100);
1728 assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
1729 assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet
1730 assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly
1733 { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1734 let our_chans = vec![channelmanager::ChannelDetails {
1735 channel_id: [0; 32],
1736 short_channel_id: Some(42),
1737 remote_network_id: node4.clone(),
1738 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1739 channel_value_satoshis: 0,
1741 outbound_capacity_msat: 0,
1742 inbound_capacity_msat: 0,
1745 let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
1746 assert_eq!(route.paths[0].len(), 2);
1748 assert_eq!(route.paths[0][0].pubkey, node4);
1749 assert_eq!(route.paths[0][0].short_channel_id, 42);
1750 assert_eq!(route.paths[0][0].fee_msat, 0);
1751 assert_eq!(route.paths[0][0].cltv_expiry_delta, (8 << 8) | 1);
1752 assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
1753 assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion
1755 assert_eq!(route.paths[0][1].pubkey, node7);
1756 assert_eq!(route.paths[0][1].short_channel_id, 8);
1757 assert_eq!(route.paths[0][1].fee_msat, 100);
1758 assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
1759 assert_eq!(route.paths[0][1].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet
1760 assert_eq!(route.paths[0][1].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly
1763 last_hops[0].fee_base_msat = 1000;
1765 { // Revert to via 6 as the fee on 8 goes up
1766 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1767 assert_eq!(route.paths[0].len(), 4);
1769 assert_eq!(route.paths[0][0].pubkey, node2);
1770 assert_eq!(route.paths[0][0].short_channel_id, 2);
1771 assert_eq!(route.paths[0][0].fee_msat, 200); // fee increased as its % of value transferred across node
1772 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1773 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1774 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1776 assert_eq!(route.paths[0][1].pubkey, node3);
1777 assert_eq!(route.paths[0][1].short_channel_id, 4);
1778 assert_eq!(route.paths[0][1].fee_msat, 100);
1779 assert_eq!(route.paths[0][1].cltv_expiry_delta, (7 << 8) | 1);
1780 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1781 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1783 assert_eq!(route.paths[0][2].pubkey, node6);
1784 assert_eq!(route.paths[0][2].short_channel_id, 7);
1785 assert_eq!(route.paths[0][2].fee_msat, 0);
1786 assert_eq!(route.paths[0][2].cltv_expiry_delta, (10 << 8) | 1);
1787 // If we have a peer in the node map, we'll use their features here since we don't have
1788 // a way of figuring out their features from the invoice:
1789 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(6));
1790 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(7));
1792 assert_eq!(route.paths[0][3].pubkey, node7);
1793 assert_eq!(route.paths[0][3].short_channel_id, 10);
1794 assert_eq!(route.paths[0][3].fee_msat, 100);
1795 assert_eq!(route.paths[0][3].cltv_expiry_delta, 42);
1796 assert_eq!(route.paths[0][3].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet
1797 assert_eq!(route.paths[0][3].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly
1800 { // ...but still use 8 for larger payments as 6 has a variable feerate
1801 let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
1802 assert_eq!(route.paths[0].len(), 5);
1804 assert_eq!(route.paths[0][0].pubkey, node2);
1805 assert_eq!(route.paths[0][0].short_channel_id, 2);
1806 assert_eq!(route.paths[0][0].fee_msat, 3000);
1807 assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1);
1808 assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2));
1809 assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2));
1811 assert_eq!(route.paths[0][1].pubkey, node3);
1812 assert_eq!(route.paths[0][1].short_channel_id, 4);
1813 assert_eq!(route.paths[0][1].fee_msat, 0);
1814 assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1);
1815 assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3));
1816 assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4));
1818 assert_eq!(route.paths[0][2].pubkey, node5);
1819 assert_eq!(route.paths[0][2].short_channel_id, 6);
1820 assert_eq!(route.paths[0][2].fee_msat, 0);
1821 assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1);
1822 assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(5));
1823 assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(6));
1825 assert_eq!(route.paths[0][3].pubkey, node4);
1826 assert_eq!(route.paths[0][3].short_channel_id, 11);
1827 assert_eq!(route.paths[0][3].fee_msat, 1000);
1828 assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1);
1829 // If we have a peer in the node map, we'll use their features here since we don't have
1830 // a way of figuring out their features from the invoice:
1831 assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags!(4));
1832 assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags!(11));
1834 assert_eq!(route.paths[0][4].pubkey, node7);
1835 assert_eq!(route.paths[0][4].short_channel_id, 8);
1836 assert_eq!(route.paths[0][4].fee_msat, 2000);
1837 assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
1838 assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet
1839 assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly
1842 { // Test Router serialization/deserialization
1843 let mut w = TestVecWriter(Vec::new());
1844 let network = router.network_map.read().unwrap();
1845 assert!(!network.channels.is_empty());
1846 assert!(!network.nodes.is_empty());
1847 network.write(&mut w).unwrap();
1848 assert!(<NetworkMap>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);