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 /// A route from us through the network to a destination
50 #[derive(Clone, PartialEq)]
52 /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
53 /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
55 pub hops: Vec<RouteHop>,
58 impl Writeable for Route {
59 fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
60 (self.hops.len() as u8).write(writer)?;
61 for hop in self.hops.iter() {
62 hop.pubkey.write(writer)?;
63 hop.node_features.write(writer)?;
64 hop.short_channel_id.write(writer)?;
65 hop.channel_features.write(writer)?;
66 hop.fee_msat.write(writer)?;
67 hop.cltv_expiry_delta.write(writer)?;
73 impl<R: ::std::io::Read> Readable<R> for Route {
74 fn read(reader: &mut R) -> Result<Route, DecodeError> {
75 let hops_count: u8 = Readable::read(reader)?;
76 let mut hops = Vec::with_capacity(hops_count as usize);
77 for _ in 0..hops_count {
79 pubkey: Readable::read(reader)?,
80 node_features: Readable::read(reader)?,
81 short_channel_id: Readable::read(reader)?,
82 channel_features: Readable::read(reader)?,
83 fee_msat: Readable::read(reader)?,
84 cltv_expiry_delta: Readable::read(reader)?,
94 struct DirectionalChannelInfo {
95 src_node_id: PublicKey,
98 cltv_expiry_delta: u16,
99 htlc_minimum_msat: u64,
101 fee_proportional_millionths: u32,
102 last_update_message: Option<msgs::ChannelUpdate>,
105 impl std::fmt::Display for DirectionalChannelInfo {
106 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
107 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)?;
112 impl_writeable!(DirectionalChannelInfo, 0, {
119 fee_proportional_millionths,
125 features: ChannelFeatures,
126 one_to_two: DirectionalChannelInfo,
127 two_to_one: DirectionalChannelInfo,
128 //this is cached here so we can send out it later if required by route_init_sync
129 //keep an eye on this to see if the extra memory is a problem
130 announcement_message: Option<msgs::ChannelAnnouncement>,
133 impl std::fmt::Display for ChannelInfo {
134 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
135 write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?;
140 impl_writeable!(ChannelInfo, 0, {
149 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
150 channels: Vec<(u64, Sha256dHash)>,
151 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
154 lowest_inbound_channel_fee_base_msat: u32,
155 lowest_inbound_channel_fee_proportional_millionths: u32,
157 features: NodeFeatures,
158 last_update: Option<u32>,
161 addresses: Vec<NetAddress>,
162 //this is cached here so we can send out it later if required by route_init_sync
163 //keep an eye on this to see if the extra memory is a problem
164 announcement_message: Option<msgs::NodeAnnouncement>,
167 impl std::fmt::Display for NodeInfo {
168 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
169 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[..])?;
174 impl Writeable for NodeInfo {
175 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
176 (self.channels.len() as u64).write(writer)?;
177 for ref chan in self.channels.iter() {
180 self.lowest_inbound_channel_fee_base_msat.write(writer)?;
181 self.lowest_inbound_channel_fee_proportional_millionths.write(writer)?;
182 self.features.write(writer)?;
183 self.last_update.write(writer)?;
184 self.rgb.write(writer)?;
185 self.alias.write(writer)?;
186 (self.addresses.len() as u64).write(writer)?;
187 for ref addr in &self.addresses {
190 self.announcement_message.write(writer)?;
195 const MAX_ALLOC_SIZE: u64 = 64*1024;
197 impl<R: ::std::io::Read> Readable<R> for NodeInfo {
198 fn read(reader: &mut R) -> Result<NodeInfo, DecodeError> {
199 let channels_count: u64 = Readable::read(reader)?;
200 let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
201 for _ in 0..channels_count {
202 channels.push(Readable::read(reader)?);
204 let lowest_inbound_channel_fee_base_msat = Readable::read(reader)?;
205 let lowest_inbound_channel_fee_proportional_millionths = Readable::read(reader)?;
206 let features = Readable::read(reader)?;
207 let last_update = Readable::read(reader)?;
208 let rgb = Readable::read(reader)?;
209 let alias = Readable::read(reader)?;
210 let addresses_count: u64 = Readable::read(reader)?;
211 let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
212 for _ in 0..addresses_count {
213 match Readable::read(reader) {
214 Ok(Ok(addr)) => { addresses.push(addr); },
215 Ok(Err(_)) => return Err(DecodeError::InvalidValue),
216 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
220 let announcement_message = Readable::read(reader)?;
223 lowest_inbound_channel_fee_base_msat,
224 lowest_inbound_channel_fee_proportional_millionths,
237 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
238 channels: BTreeMap<(u64, Sha256dHash), ChannelInfo>,
239 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
240 channels: BTreeMap<u64, ChannelInfo>,
242 our_node_id: PublicKey,
243 nodes: BTreeMap<PublicKey, NodeInfo>,
246 impl Writeable for NetworkMap {
247 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
248 (self.channels.len() as u64).write(writer)?;
249 for (ref chan_id, ref chan_info) in self.channels.iter() {
250 (*chan_id).write(writer)?;
251 chan_info.write(writer)?;
253 self.our_node_id.write(writer)?;
254 (self.nodes.len() as u64).write(writer)?;
255 for (ref node_id, ref node_info) in self.nodes.iter() {
256 node_id.write(writer)?;
257 node_info.write(writer)?;
263 impl<R: ::std::io::Read> Readable<R> for NetworkMap {
264 fn read(reader: &mut R) -> Result<NetworkMap, DecodeError> {
265 let channels_count: u64 = Readable::read(reader)?;
266 let mut channels = BTreeMap::new();
267 for _ in 0..channels_count {
268 let chan_id: u64 = Readable::read(reader)?;
269 let chan_info = Readable::read(reader)?;
270 channels.insert(chan_id, chan_info);
272 let our_node_id = Readable::read(reader)?;
273 let nodes_count: u64 = Readable::read(reader)?;
274 let mut nodes = BTreeMap::new();
275 for _ in 0..nodes_count {
276 let node_id = Readable::read(reader)?;
277 let node_info = Readable::read(reader)?;
278 nodes.insert(node_id, node_info);
288 impl std::fmt::Display for NetworkMap {
289 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
290 write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
291 for (key, val) in self.channels.iter() {
292 write!(f, " {}: {}\n", key, val)?;
294 write!(f, "[Nodes]\n")?;
295 for (key, val) in self.nodes.iter() {
296 write!(f, " {}: {}\n", log_pubkey!(key), val)?;
303 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
305 fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
306 (short_channel_id, chain_hash)
309 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
311 fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
315 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
317 fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
321 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
323 fn get_short_id(id: &u64) -> &u64 {
328 /// A channel descriptor which provides a last-hop route to get_route
329 pub struct RouteHint {
330 /// The node_id of the non-target end of the route
331 pub src_node_id: PublicKey,
332 /// The short_channel_id of this channel
333 pub short_channel_id: u64,
334 /// The static msat-denominated fee which must be paid to use this channel
335 pub fee_base_msat: u32,
336 /// The dynamic proportional fee which must be paid to use this channel, denominated in
337 /// millionths of the value being forwarded to the next hop.
338 pub fee_proportional_millionths: u32,
339 /// The difference in CLTV values between this node and the next node.
340 pub cltv_expiry_delta: u16,
341 /// The minimum value, in msat, which must be relayed to the next hop.
342 pub htlc_minimum_msat: u64,
345 /// Tracks a view of the network, receiving updates from peers and generating Routes to
346 /// payment destinations.
348 secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
349 network_map: RwLock<NetworkMap>,
350 chain_monitor: Arc<ChainWatchInterface>,
354 const SERIALIZATION_VERSION: u8 = 1;
355 const MIN_SERIALIZATION_VERSION: u8 = 1;
357 impl Writeable for Router {
358 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
359 writer.write_all(&[SERIALIZATION_VERSION; 1])?;
360 writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
362 let network = self.network_map.read().unwrap();
363 network.write(writer)?;
368 /// Arguments for the creation of a Router that are not deserialized.
369 /// At a high-level, the process for deserializing a Router and resuming normal operation is:
370 /// 1) Deserialize the Router by filling in this struct and calling <Router>::read(reaser, args).
371 /// 2) Register the new Router with your ChainWatchInterface
372 pub struct RouterReadArgs {
373 /// The ChainWatchInterface for use in the Router in the future.
375 /// No calls to the ChainWatchInterface will be made during deserialization.
376 pub chain_monitor: Arc<ChainWatchInterface>,
377 /// The Logger for use in the ChannelManager and which may be used to log information during
379 pub logger: Arc<Logger>,
382 impl<R: ::std::io::Read> ReadableArgs<R, RouterReadArgs> for Router {
383 fn read(reader: &mut R, args: RouterReadArgs) -> Result<Router, DecodeError> {
384 let _ver: u8 = Readable::read(reader)?;
385 let min_ver: u8 = Readable::read(reader)?;
386 if min_ver > SERIALIZATION_VERSION {
387 return Err(DecodeError::UnknownVersion);
389 let network_map = Readable::read(reader)?;
391 secp_ctx: Secp256k1::verification_only(),
392 network_map: RwLock::new(network_map),
393 chain_monitor: args.chain_monitor,
399 macro_rules! secp_verify_sig {
400 ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
401 match $secp_ctx.verify($msg, $sig, $pubkey) {
403 Err(_) => return Err(LightningError{err: "Invalid signature from remote node", action: ErrorAction::IgnoreError}),
408 impl RoutingMessageHandler for Router {
409 fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
410 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
411 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
413 let mut network = self.network_map.write().unwrap();
414 match network.nodes.get_mut(&msg.contents.node_id) {
415 None => Err(LightningError{err: "No existing channels for node_announcement", action: ErrorAction::IgnoreError}),
417 match node.last_update {
418 Some(last_update) => if last_update >= msg.contents.timestamp {
419 return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
424 node.features = msg.contents.features.clone();
425 node.last_update = Some(msg.contents.timestamp);
426 node.rgb = msg.contents.rgb;
427 node.alias = msg.contents.alias;
428 node.addresses = msg.contents.addresses.clone();
430 let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty();
431 node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
437 fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
438 if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
439 return Err(LightningError{err: "Channel announcement node had a channel with itself", action: ErrorAction::IgnoreError});
442 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
443 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
444 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
445 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
446 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
448 let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
449 Ok((script_pubkey, _value)) => {
450 let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
451 .push_slice(&msg.contents.bitcoin_key_1.serialize())
452 .push_slice(&msg.contents.bitcoin_key_2.serialize())
453 .push_opcode(opcodes::all::OP_PUSHNUM_2)
454 .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
455 if script_pubkey != expected_script {
456 return Err(LightningError{err: "Channel announcement keys didn't match on-chain script", action: ErrorAction::IgnoreError});
458 //TODO: Check if value is worth storing, use it to inform routing, and compare it
459 //to the new HTLC max field in channel_update
462 Err(ChainError::NotSupported) => {
463 // Tentatively accept, potentially exposing us to DoS attacks
466 Err(ChainError::NotWatched) => {
467 return Err(LightningError{err: "Channel announced on an unknown chain", action: ErrorAction::IgnoreError});
469 Err(ChainError::UnknownTx) => {
470 return Err(LightningError{err: "Channel announced without corresponding UTXO entry", action: ErrorAction::IgnoreError});
474 let mut network_lock = self.network_map.write().unwrap();
475 let network = &mut *network_lock;
477 let should_relay = msg.contents.excess_data.is_empty();
479 let chan_info = ChannelInfo {
480 features: msg.contents.features.clone(),
481 one_to_two: DirectionalChannelInfo {
482 src_node_id: msg.contents.node_id_1.clone(),
485 cltv_expiry_delta: u16::max_value(),
486 htlc_minimum_msat: u64::max_value(),
487 fee_base_msat: u32::max_value(),
488 fee_proportional_millionths: u32::max_value(),
489 last_update_message: None,
491 two_to_one: DirectionalChannelInfo {
492 src_node_id: msg.contents.node_id_2.clone(),
495 cltv_expiry_delta: u16::max_value(),
496 htlc_minimum_msat: u64::max_value(),
497 fee_base_msat: u32::max_value(),
498 fee_proportional_millionths: u32::max_value(),
499 last_update_message: None,
501 announcement_message: if should_relay { Some(msg.clone()) } else { None },
504 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
505 BtreeEntry::Occupied(mut entry) => {
506 //TODO: because asking the blockchain if short_channel_id is valid is only optional
507 //in the blockchain API, we need to handle it smartly here, though it's unclear
510 // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
511 // only sometimes returns results. In any case remove the previous entry. Note
512 // that the spec expects us to "blacklist" the node_ids involved, but we can't
514 // a) we don't *require* a UTXO provider that always returns results.
515 // b) we don't track UTXOs of channels we know about and remove them if they
517 // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
518 Self::remove_channel_in_nodes(&mut network.nodes, &entry.get(), msg.contents.short_channel_id);
519 *entry.get_mut() = chan_info;
521 return Err(LightningError{err: "Already have knowledge of channel", action: ErrorAction::IgnoreError})
524 BtreeEntry::Vacant(entry) => {
525 entry.insert(chan_info);
529 macro_rules! add_channel_to_node {
530 ( $node_id: expr ) => {
531 match network.nodes.entry($node_id) {
532 BtreeEntry::Occupied(node_entry) => {
533 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
535 BtreeEntry::Vacant(node_entry) => {
536 node_entry.insert(NodeInfo {
537 channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
538 lowest_inbound_channel_fee_base_msat: u32::max_value(),
539 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
540 features: NodeFeatures::empty(),
544 addresses: Vec::new(),
545 announcement_message: None,
552 add_channel_to_node!(msg.contents.node_id_1);
553 add_channel_to_node!(msg.contents.node_id_2);
558 fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
560 &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
561 let _ = self.handle_channel_update(msg);
563 &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
564 let mut network = self.network_map.write().unwrap();
566 if let Some(chan) = network.channels.remove(short_channel_id) {
567 Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
570 if let Some(chan) = network.channels.get_mut(short_channel_id) {
571 chan.one_to_two.enabled = false;
572 chan.two_to_one.enabled = false;
576 &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
578 //TODO: Wholly remove the node
580 self.mark_node_bad(node_id, false);
586 fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> {
587 let mut network = self.network_map.write().unwrap();
589 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
590 let chan_was_enabled;
592 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
593 None => return Err(LightningError{err: "Couldn't find channel for update", action: ErrorAction::IgnoreError}),
595 macro_rules! maybe_update_channel_info {
596 ( $target: expr) => {
597 if $target.last_update >= msg.contents.timestamp {
598 return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
600 chan_was_enabled = $target.enabled;
601 $target.last_update = msg.contents.timestamp;
602 $target.enabled = chan_enabled;
603 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
604 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
605 $target.fee_base_msat = msg.contents.fee_base_msat;
606 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
607 $target.last_update_message = if msg.contents.excess_data.is_empty() {
614 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
615 if msg.contents.flags & 1 == 1 {
616 dest_node_id = channel.one_to_two.src_node_id.clone();
617 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
618 maybe_update_channel_info!(channel.two_to_one);
620 dest_node_id = channel.two_to_one.src_node_id.clone();
621 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
622 maybe_update_channel_info!(channel.one_to_two);
628 let node = network.nodes.get_mut(&dest_node_id).unwrap();
629 node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
630 node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
631 } else if chan_was_enabled {
632 let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
633 let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
636 let node = network.nodes.get(&dest_node_id).unwrap();
638 for chan_id in node.channels.iter() {
639 let chan = network.channels.get(chan_id).unwrap();
640 if chan.one_to_two.src_node_id == dest_node_id {
641 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
642 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
644 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
645 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
650 //TODO: satisfy the borrow-checker without a double-map-lookup :(
651 let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
652 mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
653 mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
656 Ok(msg.contents.excess_data.is_empty())
660 fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
661 let mut result = Vec::with_capacity(batch_amount as usize);
662 let network = self.network_map.read().unwrap();
663 let mut iter = network.channels.range(starting_point..);
664 while result.len() < batch_amount as usize {
665 if let Some((_, ref chan)) = iter.next() {
666 if chan.announcement_message.is_some() &&
667 chan.one_to_two.last_update_message.is_some() &&
668 chan.two_to_one.last_update_message.is_some() {
669 result.push((chan.announcement_message.clone().unwrap(),
670 chan.one_to_two.last_update_message.clone().unwrap(),
671 chan.two_to_one.last_update_message.clone().unwrap()));
673 // TODO: We may end up sending un-announced channel_updates if we are sending
674 // initial sync data while receiving announce/updates for this channel.
683 fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
684 let mut result = Vec::with_capacity(batch_amount as usize);
685 let network = self.network_map.read().unwrap();
686 let mut iter = if let Some(pubkey) = starting_point {
687 let mut iter = network.nodes.range((*pubkey)..);
691 network.nodes.range(..)
693 while result.len() < batch_amount as usize {
694 if let Some((_, ref node)) = iter.next() {
695 if node.announcement_message.is_some() {
696 result.push(node.announcement_message.clone().unwrap());
706 #[derive(Eq, PartialEq)]
707 struct RouteGraphNode {
709 lowest_fee_to_peer_through_node: u64,
710 lowest_fee_to_node: u64,
713 impl cmp::Ord for RouteGraphNode {
714 fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
715 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
716 .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
720 impl cmp::PartialOrd for RouteGraphNode {
721 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
722 Some(self.cmp(other))
726 struct DummyDirectionalChannelInfo {
727 src_node_id: PublicKey,
728 cltv_expiry_delta: u32,
729 htlc_minimum_msat: u64,
731 fee_proportional_millionths: u32,
735 /// Creates a new router with the given node_id to be used as the source for get_route()
736 pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
737 let mut nodes = BTreeMap::new();
738 nodes.insert(our_pubkey.clone(), NodeInfo {
739 channels: Vec::new(),
740 lowest_inbound_channel_fee_base_msat: u32::max_value(),
741 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
742 features: NodeFeatures::empty(),
746 addresses: Vec::new(),
747 announcement_message: None,
750 secp_ctx: Secp256k1::verification_only(),
751 network_map: RwLock::new(NetworkMap {
752 channels: BTreeMap::new(),
753 our_node_id: our_pubkey,
761 /// Dumps the entire network view of this Router to the logger provided in the constructor at
763 pub fn trace_state(&self) {
764 log_trace!(self, "{}", self.network_map.read().unwrap());
767 /// Get network addresses by node id
768 pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
769 let network = self.network_map.read().unwrap();
770 network.nodes.get(pubkey).map(|n| n.addresses.clone())
773 /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
774 /// with an exponential decay in node "badness". Note that there is deliberately no
775 /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
776 /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
777 /// boolean will reduce the penalty, returning the node to usability faster. If the node is
778 /// behaving correctly, it will disable the failing channel and we will use it again next time.
779 pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
783 fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
784 macro_rules! remove_from_node {
785 ($node_id: expr) => {
786 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
787 entry.get_mut().channels.retain(|chan_id| {
788 short_channel_id != *NetworkMap::get_short_id(chan_id)
790 if entry.get().channels.is_empty() {
791 entry.remove_entry();
794 panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
798 remove_from_node!(chan.one_to_two.src_node_id);
799 remove_from_node!(chan.two_to_one.src_node_id);
802 /// Gets a route from us to the given target node.
804 /// Extra routing hops between known nodes and the target will be used if they are included in
807 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
808 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
809 /// (this Router's) view of our local channels will be ignored, and only those in first_hops
812 /// Panics if first_hops contains channels without short_channel_ids
813 /// (ChannelManager::list_usable_channels will never include such channels).
815 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
816 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
817 /// *is* checked as they may change based on the receiving node.
818 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> {
819 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
820 // uptime/success in using a node in the past.
821 let network = self.network_map.read().unwrap();
823 if *target == network.our_node_id {
824 return Err(LightningError{err: "Cannot generate a route to ourselves", action: ErrorAction::IgnoreError});
827 if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
828 return Err(LightningError{err: "Cannot generate a route of more value than all existing satoshis", action: ErrorAction::IgnoreError});
831 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
832 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
833 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
834 // to use as the A* heuristic beyond just the cost to get one node further than the current
837 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
838 src_node_id: network.our_node_id.clone(),
839 cltv_expiry_delta: 0,
840 htlc_minimum_msat: 0,
842 fee_proportional_millionths: 0,
845 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
846 let mut dist = HashMap::with_capacity(network.nodes.len());
848 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
849 if let Some(hops) = first_hops {
851 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
852 if chan.remote_network_id == *target {
854 hops: vec![RouteHop {
855 pubkey: chan.remote_network_id,
856 node_features: NodeFeatures::with_known_relevant_init_flags(&chan.counterparty_features),
858 channel_features: ChannelFeatures::with_known_relevant_init_flags(&chan.counterparty_features),
859 fee_msat: final_value_msat,
860 cltv_expiry_delta: final_cltv,
864 first_hop_targets.insert(chan.remote_network_id, (short_channel_id, chan.counterparty_features.clone()));
866 if first_hop_targets.is_empty() {
867 return Err(LightningError{err: "Cannot route when there are no outbound routes away from us", action: ErrorAction::IgnoreError});
871 macro_rules! add_entry {
872 // Adds entry which goes from the node pointed to by $directional_info to
873 // $dest_node_id over the channel with id $chan_id with fees described in
874 // $directional_info.
875 ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $chan_features: expr, $starting_fee_msat: expr ) => {
876 //TODO: Explore simply adding fee to hit htlc_minimum_msat
877 if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
878 let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
879 if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
880 ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
882 let mut total_fee = $starting_fee_msat as u64;
883 let hm_entry = dist.entry(&$directional_info.src_node_id);
884 let old_entry = hm_entry.or_insert_with(|| {
885 let node = network.nodes.get(&$directional_info.src_node_id).unwrap();
887 node.lowest_inbound_channel_fee_base_msat,
888 node.lowest_inbound_channel_fee_proportional_millionths,
890 pubkey: $dest_node_id.clone(),
891 node_features: NodeFeatures::empty(),
893 channel_features: $chan_features.clone(),
895 cltv_expiry_delta: 0,
898 if $directional_info.src_node_id != network.our_node_id {
899 // Ignore new_fee for channel-from-us as we assume all channels-from-us
900 // will have the same effective-fee
901 total_fee += new_fee;
902 if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
903 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
905 // max_value means we'll always fail the old_entry.0 > total_fee check
906 total_fee = u64::max_value();
909 let new_graph_node = RouteGraphNode {
910 pubkey: $directional_info.src_node_id,
911 lowest_fee_to_peer_through_node: total_fee,
912 lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
914 if old_entry.0 > total_fee {
915 targets.push(new_graph_node);
916 old_entry.0 = total_fee;
917 old_entry.3 = RouteHop {
918 pubkey: $dest_node_id.clone(),
919 node_features: NodeFeatures::empty(),
920 short_channel_id: $chan_id.clone(),
921 channel_features: $chan_features.clone(),
922 fee_msat: new_fee, // This field is ignored on the last-hop anyway
923 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
931 macro_rules! add_entries_to_cheapest_to_target_node {
932 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
933 if first_hops.is_some() {
934 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&$node_id) {
935 add_entry!(first_hop, $node_id, dummy_directional_info, ChannelFeatures::with_known_relevant_init_flags(&features), $fee_to_target_msat);
939 if !$node.features.requires_unknown_bits() {
940 for chan_id in $node.channels.iter() {
941 let chan = network.channels.get(chan_id).unwrap();
942 if !chan.features.requires_unknown_bits() {
943 if chan.one_to_two.src_node_id == *$node_id {
944 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
945 if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
946 if chan.two_to_one.enabled {
947 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, chan.features, $fee_to_target_msat);
951 if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
952 if chan.one_to_two.enabled {
953 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, chan.features, $fee_to_target_msat);
963 match network.nodes.get(target) {
966 add_entries_to_cheapest_to_target_node!(node, target, 0);
970 for hop in last_hops.iter() {
971 if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
972 if network.nodes.get(&hop.src_node_id).is_some() {
973 if first_hops.is_some() {
974 if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&hop.src_node_id) {
975 // Currently there are no channel-context features defined, so we are a
976 // bit lazy here. In the future, we should pull them out via our
977 // ChannelManager, but there's no reason to waste the space until we
979 add_entry!(first_hop, hop.src_node_id, dummy_directional_info, ChannelFeatures::with_known_relevant_init_flags(&features), 0);
982 // BOLT 11 doesn't allow inclusion of features for the last hop hints, which
983 // really sucks, cause we're gonna need that eventually.
984 add_entry!(hop.short_channel_id, target, hop, ChannelFeatures::empty(), 0);
989 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
990 if pubkey == network.our_node_id {
991 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
993 if let Some(&(_, ref features)) = first_hop_targets.get(&res.last().unwrap().pubkey) {
994 res.last_mut().unwrap().node_features = NodeFeatures::with_known_relevant_init_flags(&features);
995 } else if let Some(node) = network.nodes.get(&res.last().unwrap().pubkey) {
996 res.last_mut().unwrap().node_features = node.features.clone();
998 // We should be able to fill in features for everything except the last
999 // hop, if the last hop was provided via a BOLT 11 invoice (though we
1000 // should be able to extend it further as BOLT 11 does have feature
1001 // flags for the last hop node itself).
1002 assert!(res.last().unwrap().pubkey == *target);
1004 if res.last().unwrap().pubkey == *target {
1008 let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
1010 None => return Err(LightningError{err: "Failed to find a non-fee-overflowing path to the given destination", action: ErrorAction::IgnoreError}),
1012 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
1013 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
1014 res.push(new_entry);
1016 res.last_mut().unwrap().fee_msat = final_value_msat;
1017 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
1018 let route = Route { hops: res };
1019 log_trace!(self, "Got route: {}", log_route!(route));
1023 match network.nodes.get(&pubkey) {
1026 add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
1031 Err(LightningError{err: "Failed to find a path to the given destination", action: ErrorAction::IgnoreError})
1037 use chain::chaininterface;
1038 use ln::channelmanager;
1039 use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
1040 use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
1041 use ln::msgs::{LightningError, ErrorAction};
1042 use util::test_utils;
1043 use util::test_utils::TestVecWriter;
1044 use util::logger::Logger;
1045 use util::ser::{Writeable, Readable};
1047 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1048 use bitcoin_hashes::Hash;
1049 use bitcoin::network::constants::Network;
1053 use secp256k1::key::{PublicKey,SecretKey};
1054 use secp256k1::Secp256k1;
1060 let secp_ctx = Secp256k1::new();
1061 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
1062 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1063 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
1064 let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
1066 // Build network from our_id to node8:
1068 // -1(1)2- node1 -1(3)2-
1070 // our_id -1(12)2- node8 -1(13)2--- node3
1072 // -1(2)2- node2 -1(4)2-
1075 // chan1 1-to-2: disabled
1076 // chan1 2-to-1: enabled, 0 fee
1078 // chan2 1-to-2: enabled, ignored fee
1079 // chan2 2-to-1: enabled, 0 fee
1081 // chan3 1-to-2: enabled, 0 fee
1082 // chan3 2-to-1: enabled, 100 msat fee
1084 // chan4 1-to-2: enabled, 100% fee
1085 // chan4 2-to-1: enabled, 0 fee
1087 // chan12 1-to-2: enabled, ignored fee
1088 // chan12 2-to-1: enabled, 0 fee
1090 // chan13 1-to-2: enabled, 200% fee
1091 // chan13 2-to-1: enabled, 0 fee
1094 // -1(5)2- node4 -1(8)2--
1098 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
1100 // -1(7)2- node6 -1(10)2-
1102 // chan5 1-to-2: enabled, 100 msat fee
1103 // chan5 2-to-1: enabled, 0 fee
1105 // chan6 1-to-2: enabled, 0 fee
1106 // chan6 2-to-1: enabled, 0 fee
1108 // chan7 1-to-2: enabled, 100% fee
1109 // chan7 2-to-1: enabled, 0 fee
1111 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
1112 // chan8 2-to-1: enabled, 0 fee
1114 // chan9 1-to-2: enabled, 1001 msat fee
1115 // chan9 2-to-1: enabled, 0 fee
1117 // chan10 1-to-2: enabled, 0 fee
1118 // chan10 2-to-1: enabled, 0 fee
1120 // chan11 1-to-2: enabled, 0 fee
1121 // chan11 2-to-1: enabled, 0 fee
1123 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
1124 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
1125 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
1126 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
1127 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
1128 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
1129 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
1130 let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
1132 let zero_hash = Sha256dHash::hash(&[0; 32]);
1134 macro_rules! id_to_feature_flags {
1135 // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can
1136 // test for it later.
1138 let idx = ($id - 1) * 2 + 1;
1140 vec![1 << (idx - 8*3), 0, 0, 0]
1141 } else if idx > 8*2 {
1142 vec![1 << (idx - 8*2), 0, 0]
1143 } else if idx > 8*1 {
1144 vec![1 << (idx - 8*1), 0]
1152 let mut network = router.network_map.write().unwrap();
1154 network.nodes.insert(node1.clone(), NodeInfo {
1155 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
1156 lowest_inbound_channel_fee_base_msat: 100,
1157 lowest_inbound_channel_fee_proportional_millionths: 0,
1158 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(1)),
1159 last_update: Some(1),
1162 addresses: Vec::new(),
1163 announcement_message: None,
1165 network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
1166 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(1)),
1167 one_to_two: DirectionalChannelInfo {
1168 src_node_id: our_id.clone(),
1171 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1172 htlc_minimum_msat: 0,
1173 fee_base_msat: u32::max_value(), // This value should be ignored
1174 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1175 last_update_message: None,
1176 }, two_to_one: DirectionalChannelInfo {
1177 src_node_id: node1.clone(),
1180 cltv_expiry_delta: 0,
1181 htlc_minimum_msat: 0,
1183 fee_proportional_millionths: 0,
1184 last_update_message: None,
1186 announcement_message: None,
1188 network.nodes.insert(node2.clone(), NodeInfo {
1189 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
1190 lowest_inbound_channel_fee_base_msat: 0,
1191 lowest_inbound_channel_fee_proportional_millionths: 0,
1192 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(2)),
1193 last_update: Some(1),
1196 addresses: Vec::new(),
1197 announcement_message: None,
1199 network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
1200 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(2)),
1201 one_to_two: DirectionalChannelInfo {
1202 src_node_id: our_id.clone(),
1205 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1206 htlc_minimum_msat: 0,
1207 fee_base_msat: u32::max_value(), // This value should be ignored
1208 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1209 last_update_message: None,
1210 }, two_to_one: DirectionalChannelInfo {
1211 src_node_id: node2.clone(),
1214 cltv_expiry_delta: 0,
1215 htlc_minimum_msat: 0,
1217 fee_proportional_millionths: 0,
1218 last_update_message: None,
1220 announcement_message: None,
1222 network.nodes.insert(node8.clone(), NodeInfo {
1223 channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
1224 lowest_inbound_channel_fee_base_msat: 0,
1225 lowest_inbound_channel_fee_proportional_millionths: 0,
1226 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(8)),
1227 last_update: Some(1),
1230 addresses: Vec::new(),
1231 announcement_message: None,
1233 network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
1234 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(12)),
1235 one_to_two: DirectionalChannelInfo {
1236 src_node_id: our_id.clone(),
1239 cltv_expiry_delta: u16::max_value(), // This value should be ignored
1240 htlc_minimum_msat: 0,
1241 fee_base_msat: u32::max_value(), // This value should be ignored
1242 fee_proportional_millionths: u32::max_value(), // This value should be ignored
1243 last_update_message: None,
1244 }, two_to_one: DirectionalChannelInfo {
1245 src_node_id: node8.clone(),
1248 cltv_expiry_delta: 0,
1249 htlc_minimum_msat: 0,
1251 fee_proportional_millionths: 0,
1252 last_update_message: None,
1254 announcement_message: None,
1256 network.nodes.insert(node3.clone(), NodeInfo {
1258 NetworkMap::get_key(3, zero_hash.clone()),
1259 NetworkMap::get_key(4, zero_hash.clone()),
1260 NetworkMap::get_key(13, zero_hash.clone()),
1261 NetworkMap::get_key(5, zero_hash.clone()),
1262 NetworkMap::get_key(6, zero_hash.clone()),
1263 NetworkMap::get_key(7, zero_hash.clone())),
1264 lowest_inbound_channel_fee_base_msat: 0,
1265 lowest_inbound_channel_fee_proportional_millionths: 0,
1266 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(3)),
1267 last_update: Some(1),
1270 addresses: Vec::new(),
1271 announcement_message: None,
1273 network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
1274 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(3)),
1275 one_to_two: DirectionalChannelInfo {
1276 src_node_id: node1.clone(),
1279 cltv_expiry_delta: (3 << 8) | 1,
1280 htlc_minimum_msat: 0,
1282 fee_proportional_millionths: 0,
1283 last_update_message: None,
1284 }, two_to_one: DirectionalChannelInfo {
1285 src_node_id: node3.clone(),
1288 cltv_expiry_delta: (3 << 8) | 2,
1289 htlc_minimum_msat: 0,
1291 fee_proportional_millionths: 0,
1292 last_update_message: None,
1294 announcement_message: None,
1296 network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
1297 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(4)),
1298 one_to_two: DirectionalChannelInfo {
1299 src_node_id: node2.clone(),
1302 cltv_expiry_delta: (4 << 8) | 1,
1303 htlc_minimum_msat: 0,
1305 fee_proportional_millionths: 1000000,
1306 last_update_message: None,
1307 }, two_to_one: DirectionalChannelInfo {
1308 src_node_id: node3.clone(),
1311 cltv_expiry_delta: (4 << 8) | 2,
1312 htlc_minimum_msat: 0,
1314 fee_proportional_millionths: 0,
1315 last_update_message: None,
1317 announcement_message: None,
1319 network.channels.insert(NetworkMap::get_key(13, zero_hash.clone()), ChannelInfo {
1320 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(13)),
1321 one_to_two: DirectionalChannelInfo {
1322 src_node_id: node8.clone(),
1325 cltv_expiry_delta: (13 << 8) | 1,
1326 htlc_minimum_msat: 0,
1328 fee_proportional_millionths: 2000000,
1329 last_update_message: None,
1330 }, two_to_one: DirectionalChannelInfo {
1331 src_node_id: node3.clone(),
1334 cltv_expiry_delta: (13 << 8) | 2,
1335 htlc_minimum_msat: 0,
1337 fee_proportional_millionths: 0,
1338 last_update_message: None,
1340 announcement_message: None,
1342 network.nodes.insert(node4.clone(), NodeInfo {
1343 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1344 lowest_inbound_channel_fee_base_msat: 0,
1345 lowest_inbound_channel_fee_proportional_millionths: 0,
1346 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(4)),
1347 last_update: Some(1),
1350 addresses: Vec::new(),
1351 announcement_message: None,
1353 network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
1354 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(5)),
1355 one_to_two: DirectionalChannelInfo {
1356 src_node_id: node3.clone(),
1359 cltv_expiry_delta: (5 << 8) | 1,
1360 htlc_minimum_msat: 0,
1362 fee_proportional_millionths: 0,
1363 last_update_message: None,
1364 }, two_to_one: DirectionalChannelInfo {
1365 src_node_id: node4.clone(),
1368 cltv_expiry_delta: (5 << 8) | 2,
1369 htlc_minimum_msat: 0,
1371 fee_proportional_millionths: 0,
1372 last_update_message: None,
1374 announcement_message: None,
1376 network.nodes.insert(node5.clone(), NodeInfo {
1377 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1378 lowest_inbound_channel_fee_base_msat: 0,
1379 lowest_inbound_channel_fee_proportional_millionths: 0,
1380 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(5)),
1381 last_update: Some(1),
1384 addresses: Vec::new(),
1385 announcement_message: None,
1387 network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
1388 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(6)),
1389 one_to_two: DirectionalChannelInfo {
1390 src_node_id: node3.clone(),
1393 cltv_expiry_delta: (6 << 8) | 1,
1394 htlc_minimum_msat: 0,
1396 fee_proportional_millionths: 0,
1397 last_update_message: None,
1398 }, two_to_one: DirectionalChannelInfo {
1399 src_node_id: node5.clone(),
1402 cltv_expiry_delta: (6 << 8) | 2,
1403 htlc_minimum_msat: 0,
1405 fee_proportional_millionths: 0,
1406 last_update_message: None,
1408 announcement_message: None,
1410 network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
1411 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(11)),
1412 one_to_two: DirectionalChannelInfo {
1413 src_node_id: node5.clone(),
1416 cltv_expiry_delta: (11 << 8) | 1,
1417 htlc_minimum_msat: 0,
1419 fee_proportional_millionths: 0,
1420 last_update_message: None,
1421 }, two_to_one: DirectionalChannelInfo {
1422 src_node_id: node4.clone(),
1425 cltv_expiry_delta: (11 << 8) | 2,
1426 htlc_minimum_msat: 0,
1428 fee_proportional_millionths: 0,
1429 last_update_message: None,
1431 announcement_message: None,
1433 network.nodes.insert(node6.clone(), NodeInfo {
1434 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
1435 lowest_inbound_channel_fee_base_msat: 0,
1436 lowest_inbound_channel_fee_proportional_millionths: 0,
1437 features: NodeFeatures::from_le_bytes(id_to_feature_flags!(6)),
1438 last_update: Some(1),
1441 addresses: Vec::new(),
1442 announcement_message: None,
1444 network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
1445 features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(7)),
1446 one_to_two: DirectionalChannelInfo {
1447 src_node_id: node3.clone(),
1450 cltv_expiry_delta: (7 << 8) | 1,
1451 htlc_minimum_msat: 0,
1453 fee_proportional_millionths: 1000000,
1454 last_update_message: None,
1455 }, two_to_one: DirectionalChannelInfo {
1456 src_node_id: node6.clone(),
1459 cltv_expiry_delta: (7 << 8) | 2,
1460 htlc_minimum_msat: 0,
1462 fee_proportional_millionths: 0,
1463 last_update_message: None,
1465 announcement_message: None,
1469 { // Simple route to 3 via 2
1470 let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
1471 assert_eq!(route.hops.len(), 2);
1473 assert_eq!(route.hops[0].pubkey, node2);
1474 assert_eq!(route.hops[0].short_channel_id, 2);
1475 assert_eq!(route.hops[0].fee_msat, 100);
1476 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1477 assert_eq!(route.hops[0].node_features.le_flags(), &id_to_feature_flags!(2));
1478 assert_eq!(route.hops[0].channel_features.le_flags(), &id_to_feature_flags!(2));
1480 assert_eq!(route.hops[1].pubkey, node3);
1481 assert_eq!(route.hops[1].short_channel_id, 4);
1482 assert_eq!(route.hops[1].fee_msat, 100);
1483 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1484 assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1485 assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(4));
1488 { // Disable channels 4 and 12 by requiring unknown feature bits
1489 let mut network = router.network_map.write().unwrap();
1490 network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1491 network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1494 { // If all the channels require some features we don't understand, route should fail
1495 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1496 assert_eq!(err, "Failed to find a path to the given destination");
1497 } else { panic!(); }
1500 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1501 let our_chans = vec![channelmanager::ChannelDetails {
1502 channel_id: [0; 32],
1503 short_channel_id: Some(42),
1504 remote_network_id: node8.clone(),
1505 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1506 channel_value_satoshis: 0,
1508 outbound_capacity_msat: 0,
1509 inbound_capacity_msat: 0,
1512 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1513 assert_eq!(route.hops.len(), 2);
1515 assert_eq!(route.hops[0].pubkey, node8);
1516 assert_eq!(route.hops[0].short_channel_id, 42);
1517 assert_eq!(route.hops[0].fee_msat, 200);
1518 assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1519 assert_eq!(route.hops[0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
1520 assert_eq!(route.hops[0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion
1522 assert_eq!(route.hops[1].pubkey, node3);
1523 assert_eq!(route.hops[1].short_channel_id, 13);
1524 assert_eq!(route.hops[1].fee_msat, 100);
1525 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1526 assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1527 assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(13));
1530 { // Re-enable channels 4 and 12 by wiping the unknown feature bits
1531 let mut network = router.network_map.write().unwrap();
1532 network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1533 network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1536 { // Disable nodes 1, 2, and 8 by requiring unknown feature bits
1537 let mut network = router.network_map.write().unwrap();
1538 network.nodes.get_mut(&node1).unwrap().features.set_require_unknown_bits();
1539 network.nodes.get_mut(&node2).unwrap().features.set_require_unknown_bits();
1540 network.nodes.get_mut(&node8).unwrap().features.set_require_unknown_bits();
1543 { // If all nodes require some features we don't understand, route should fail
1544 if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1545 assert_eq!(err, "Failed to find a path to the given destination");
1546 } else { panic!(); }
1549 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1550 let our_chans = vec![channelmanager::ChannelDetails {
1551 channel_id: [0; 32],
1552 short_channel_id: Some(42),
1553 remote_network_id: node8.clone(),
1554 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1555 channel_value_satoshis: 0,
1557 outbound_capacity_msat: 0,
1558 inbound_capacity_msat: 0,
1561 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1562 assert_eq!(route.hops.len(), 2);
1564 assert_eq!(route.hops[0].pubkey, node8);
1565 assert_eq!(route.hops[0].short_channel_id, 42);
1566 assert_eq!(route.hops[0].fee_msat, 200);
1567 assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1568 assert_eq!(route.hops[0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
1569 assert_eq!(route.hops[0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion
1571 assert_eq!(route.hops[1].pubkey, node3);
1572 assert_eq!(route.hops[1].short_channel_id, 13);
1573 assert_eq!(route.hops[1].fee_msat, 100);
1574 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1575 assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1576 assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(13));
1579 { // Re-enable nodes 1, 2, and 8
1580 let mut network = router.network_map.write().unwrap();
1581 network.nodes.get_mut(&node1).unwrap().features.clear_require_unknown_bits();
1582 network.nodes.get_mut(&node2).unwrap().features.clear_require_unknown_bits();
1583 network.nodes.get_mut(&node8).unwrap().features.clear_require_unknown_bits();
1586 // Note that we don't test disabling node 3 and failing to route to it, as we (somewhat
1587 // naively) assume that the user checked the feature bits on the invoice, which override
1588 // the node_announcement.
1590 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
1591 let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
1592 assert_eq!(route.hops.len(), 3);
1594 assert_eq!(route.hops[0].pubkey, node2);
1595 assert_eq!(route.hops[0].short_channel_id, 2);
1596 assert_eq!(route.hops[0].fee_msat, 200);
1597 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1598 assert_eq!(route.hops[0].node_features.le_flags(), &id_to_feature_flags!(2));
1599 assert_eq!(route.hops[0].channel_features.le_flags(), &id_to_feature_flags!(2));
1601 assert_eq!(route.hops[1].pubkey, node3);
1602 assert_eq!(route.hops[1].short_channel_id, 4);
1603 assert_eq!(route.hops[1].fee_msat, 100);
1604 assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
1605 assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1606 assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(4));
1608 assert_eq!(route.hops[2].pubkey, node1);
1609 assert_eq!(route.hops[2].short_channel_id, 3);
1610 assert_eq!(route.hops[2].fee_msat, 100);
1611 assert_eq!(route.hops[2].cltv_expiry_delta, 42);
1612 assert_eq!(route.hops[2].node_features.le_flags(), &id_to_feature_flags!(1));
1613 assert_eq!(route.hops[2].channel_features.le_flags(), &id_to_feature_flags!(3));
1616 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1617 let our_chans = vec![channelmanager::ChannelDetails {
1618 channel_id: [0; 32],
1619 short_channel_id: Some(42),
1620 remote_network_id: node8.clone(),
1621 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1622 channel_value_satoshis: 0,
1624 outbound_capacity_msat: 0,
1625 inbound_capacity_msat: 0,
1628 let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1629 assert_eq!(route.hops.len(), 2);
1631 assert_eq!(route.hops[0].pubkey, node8);
1632 assert_eq!(route.hops[0].short_channel_id, 42);
1633 assert_eq!(route.hops[0].fee_msat, 200);
1634 assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1635 assert_eq!(route.hops[0].node_features.le_flags(), &vec![0b11]);
1636 assert_eq!(route.hops[0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion
1638 assert_eq!(route.hops[1].pubkey, node3);
1639 assert_eq!(route.hops[1].short_channel_id, 13);
1640 assert_eq!(route.hops[1].fee_msat, 100);
1641 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1642 assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1643 assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(13));
1646 let mut last_hops = vec!(RouteHint {
1647 src_node_id: node4.clone(),
1648 short_channel_id: 8,
1650 fee_proportional_millionths: 0,
1651 cltv_expiry_delta: (8 << 8) | 1,
1652 htlc_minimum_msat: 0,
1654 src_node_id: node5.clone(),
1655 short_channel_id: 9,
1656 fee_base_msat: 1001,
1657 fee_proportional_millionths: 0,
1658 cltv_expiry_delta: (9 << 8) | 1,
1659 htlc_minimum_msat: 0,
1661 src_node_id: node6.clone(),
1662 short_channel_id: 10,
1664 fee_proportional_millionths: 0,
1665 cltv_expiry_delta: (10 << 8) | 1,
1666 htlc_minimum_msat: 0,
1669 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
1670 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1671 assert_eq!(route.hops.len(), 5);
1673 assert_eq!(route.hops[0].pubkey, node2);
1674 assert_eq!(route.hops[0].short_channel_id, 2);
1675 assert_eq!(route.hops[0].fee_msat, 100);
1676 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1677 assert_eq!(route.hops[0].node_features.le_flags(), &id_to_feature_flags!(2));
1678 assert_eq!(route.hops[0].channel_features.le_flags(), &id_to_feature_flags!(2));
1680 assert_eq!(route.hops[1].pubkey, node3);
1681 assert_eq!(route.hops[1].short_channel_id, 4);
1682 assert_eq!(route.hops[1].fee_msat, 0);
1683 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1684 assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1685 assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(4));
1687 assert_eq!(route.hops[2].pubkey, node5);
1688 assert_eq!(route.hops[2].short_channel_id, 6);
1689 assert_eq!(route.hops[2].fee_msat, 0);
1690 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1691 assert_eq!(route.hops[2].node_features.le_flags(), &id_to_feature_flags!(5));
1692 assert_eq!(route.hops[2].channel_features.le_flags(), &id_to_feature_flags!(6));
1694 assert_eq!(route.hops[3].pubkey, node4);
1695 assert_eq!(route.hops[3].short_channel_id, 11);
1696 assert_eq!(route.hops[3].fee_msat, 0);
1697 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1698 // If we have a peer in the node map, we'll use their features here since we don't have
1699 // a way of figuring out their features from the invoice:
1700 assert_eq!(route.hops[3].node_features.le_flags(), &id_to_feature_flags!(4));
1701 assert_eq!(route.hops[3].channel_features.le_flags(), &id_to_feature_flags!(11));
1703 assert_eq!(route.hops[4].pubkey, node7);
1704 assert_eq!(route.hops[4].short_channel_id, 8);
1705 assert_eq!(route.hops[4].fee_msat, 100);
1706 assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1707 assert_eq!(route.hops[4].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet
1708 assert_eq!(route.hops[4].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly
1711 { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1712 let our_chans = vec![channelmanager::ChannelDetails {
1713 channel_id: [0; 32],
1714 short_channel_id: Some(42),
1715 remote_network_id: node4.clone(),
1716 counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
1717 channel_value_satoshis: 0,
1719 outbound_capacity_msat: 0,
1720 inbound_capacity_msat: 0,
1723 let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
1724 assert_eq!(route.hops.len(), 2);
1726 assert_eq!(route.hops[0].pubkey, node4);
1727 assert_eq!(route.hops[0].short_channel_id, 42);
1728 assert_eq!(route.hops[0].fee_msat, 0);
1729 assert_eq!(route.hops[0].cltv_expiry_delta, (8 << 8) | 1);
1730 assert_eq!(route.hops[0].node_features.le_flags(), &vec![0b11]);
1731 assert_eq!(route.hops[0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion
1733 assert_eq!(route.hops[1].pubkey, node7);
1734 assert_eq!(route.hops[1].short_channel_id, 8);
1735 assert_eq!(route.hops[1].fee_msat, 100);
1736 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1737 assert_eq!(route.hops[1].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet
1738 assert_eq!(route.hops[1].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly
1741 last_hops[0].fee_base_msat = 1000;
1743 { // Revert to via 6 as the fee on 8 goes up
1744 let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1745 assert_eq!(route.hops.len(), 4);
1747 assert_eq!(route.hops[0].pubkey, node2);
1748 assert_eq!(route.hops[0].short_channel_id, 2);
1749 assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
1750 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1751 assert_eq!(route.hops[0].node_features.le_flags(), &id_to_feature_flags!(2));
1752 assert_eq!(route.hops[0].channel_features.le_flags(), &id_to_feature_flags!(2));
1754 assert_eq!(route.hops[1].pubkey, node3);
1755 assert_eq!(route.hops[1].short_channel_id, 4);
1756 assert_eq!(route.hops[1].fee_msat, 100);
1757 assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
1758 assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1759 assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(4));
1761 assert_eq!(route.hops[2].pubkey, node6);
1762 assert_eq!(route.hops[2].short_channel_id, 7);
1763 assert_eq!(route.hops[2].fee_msat, 0);
1764 assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
1765 // If we have a peer in the node map, we'll use their features here since we don't have
1766 // a way of figuring out their features from the invoice:
1767 assert_eq!(route.hops[2].node_features.le_flags(), &id_to_feature_flags!(6));
1768 assert_eq!(route.hops[2].channel_features.le_flags(), &id_to_feature_flags!(7));
1770 assert_eq!(route.hops[3].pubkey, node7);
1771 assert_eq!(route.hops[3].short_channel_id, 10);
1772 assert_eq!(route.hops[3].fee_msat, 100);
1773 assert_eq!(route.hops[3].cltv_expiry_delta, 42);
1774 assert_eq!(route.hops[3].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet
1775 assert_eq!(route.hops[3].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly
1778 { // ...but still use 8 for larger payments as 6 has a variable feerate
1779 let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
1780 assert_eq!(route.hops.len(), 5);
1782 assert_eq!(route.hops[0].pubkey, node2);
1783 assert_eq!(route.hops[0].short_channel_id, 2);
1784 assert_eq!(route.hops[0].fee_msat, 3000);
1785 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1786 assert_eq!(route.hops[0].node_features.le_flags(), &id_to_feature_flags!(2));
1787 assert_eq!(route.hops[0].channel_features.le_flags(), &id_to_feature_flags!(2));
1789 assert_eq!(route.hops[1].pubkey, node3);
1790 assert_eq!(route.hops[1].short_channel_id, 4);
1791 assert_eq!(route.hops[1].fee_msat, 0);
1792 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1793 assert_eq!(route.hops[1].node_features.le_flags(), &id_to_feature_flags!(3));
1794 assert_eq!(route.hops[1].channel_features.le_flags(), &id_to_feature_flags!(4));
1796 assert_eq!(route.hops[2].pubkey, node5);
1797 assert_eq!(route.hops[2].short_channel_id, 6);
1798 assert_eq!(route.hops[2].fee_msat, 0);
1799 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1800 assert_eq!(route.hops[2].node_features.le_flags(), &id_to_feature_flags!(5));
1801 assert_eq!(route.hops[2].channel_features.le_flags(), &id_to_feature_flags!(6));
1803 assert_eq!(route.hops[3].pubkey, node4);
1804 assert_eq!(route.hops[3].short_channel_id, 11);
1805 assert_eq!(route.hops[3].fee_msat, 1000);
1806 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1807 // If we have a peer in the node map, we'll use their features here since we don't have
1808 // a way of figuring out their features from the invoice:
1809 assert_eq!(route.hops[3].node_features.le_flags(), &id_to_feature_flags!(4));
1810 assert_eq!(route.hops[3].channel_features.le_flags(), &id_to_feature_flags!(11));
1812 assert_eq!(route.hops[4].pubkey, node7);
1813 assert_eq!(route.hops[4].short_channel_id, 8);
1814 assert_eq!(route.hops[4].fee_msat, 2000);
1815 assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1816 assert_eq!(route.hops[4].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet
1817 assert_eq!(route.hops[4].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly
1820 { // Test Router serialization/deserialization
1821 let mut w = TestVecWriter(Vec::new());
1822 let network = router.network_map.read().unwrap();
1823 assert!(!network.channels.is_empty());
1824 assert!(!network.nodes.is_empty());
1825 network.write(&mut w).unwrap();
1826 assert!(<NetworkMap>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);