Merge pull request #428 from TheBlueMatt/2019-12-flat-features
[rust-lightning] / lightning / src / ln / router.rs
1 //! The top-level routing/network map tracking logic lives here.
2 //!
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.
5
6 use secp256k1::key::PublicKey;
7 use secp256k1::Secp256k1;
8 use secp256k1;
9
10 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
11 use bitcoin_hashes::Hash;
12 use bitcoin::blockdata::script::Builder;
13 use bitcoin::blockdata::opcodes;
14
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};
19 use ln::msgs;
20 use util::ser::{Writeable, Readable, Writer, ReadableArgs};
21 use util::logger::Logger;
22
23 use std::cmp;
24 use std::sync::{RwLock,Arc};
25 use std::collections::{HashMap,BinaryHeap,BTreeMap};
26 use std::collections::btree_map::Entry as BtreeEntry;
27 use std;
28
29 /// A hop in a route
30 #[derive(Clone, PartialEq)]
31 pub struct RouteHop {
32         /// The node_id of the node at this hop.
33         pub pubkey: PublicKey,
34         /// The channel that should be used from the previous hop to reach this node.
35         pub short_channel_id: u64,
36         /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
37         pub fee_msat: u64,
38         /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
39         /// expected at the destination, in excess of the current block height.
40         pub cltv_expiry_delta: u32,
41 }
42
43 /// A route from us through the network to a destination
44 #[derive(Clone, PartialEq)]
45 pub struct Route {
46         /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
47         /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
48         /// length.
49         pub hops: Vec<RouteHop>,
50 }
51
52 impl Writeable for Route {
53         fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
54                 (self.hops.len() as u8).write(writer)?;
55                 for hop in self.hops.iter() {
56                         hop.pubkey.write(writer)?;
57                         hop.short_channel_id.write(writer)?;
58                         hop.fee_msat.write(writer)?;
59                         hop.cltv_expiry_delta.write(writer)?;
60                 }
61                 Ok(())
62         }
63 }
64
65 impl<R: ::std::io::Read> Readable<R> for Route {
66         fn read(reader: &mut R) -> Result<Route, DecodeError> {
67                 let hops_count: u8 = Readable::read(reader)?;
68                 let mut hops = Vec::with_capacity(hops_count as usize);
69                 for _ in 0..hops_count {
70                         hops.push(RouteHop {
71                                 pubkey: Readable::read(reader)?,
72                                 short_channel_id: Readable::read(reader)?,
73                                 fee_msat: Readable::read(reader)?,
74                                 cltv_expiry_delta: Readable::read(reader)?,
75                         });
76                 }
77                 Ok(Route {
78                         hops
79                 })
80         }
81 }
82
83 #[derive(PartialEq)]
84 struct DirectionalChannelInfo {
85         src_node_id: PublicKey,
86         last_update: u32,
87         enabled: bool,
88         cltv_expiry_delta: u16,
89         htlc_minimum_msat: u64,
90         fee_base_msat: u32,
91         fee_proportional_millionths: u32,
92         last_update_message: Option<msgs::ChannelUpdate>,
93 }
94
95 impl std::fmt::Display for DirectionalChannelInfo {
96         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
97                 write!(f, "src_node_id {}, last_update {}, enabled {}, cltv_expiry_delta {}, htlc_minimum_msat {}, fee_base_msat {}, fee_proportional_millionths {}", log_pubkey!(self.src_node_id), self.last_update, self.enabled, self.cltv_expiry_delta, self.htlc_minimum_msat, self.fee_base_msat, self.fee_proportional_millionths)?;
98                 Ok(())
99         }
100 }
101
102 impl_writeable!(DirectionalChannelInfo, 0, {
103         src_node_id,
104         last_update,
105         enabled,
106         cltv_expiry_delta,
107         htlc_minimum_msat,
108         fee_base_msat,
109         fee_proportional_millionths,
110         last_update_message
111 });
112
113 #[derive(PartialEq)]
114 struct ChannelInfo {
115         features: ChannelFeatures,
116         one_to_two: DirectionalChannelInfo,
117         two_to_one: DirectionalChannelInfo,
118         //this is cached here so we can send out it later if required by route_init_sync
119         //keep an eye on this to see if the extra memory is a problem
120         announcement_message: Option<msgs::ChannelAnnouncement>,
121 }
122
123 impl std::fmt::Display for ChannelInfo {
124         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
125                 write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?;
126                 Ok(())
127         }
128 }
129
130 impl_writeable!(ChannelInfo, 0, {
131         features,
132         one_to_two,
133         two_to_one,
134         announcement_message
135 });
136
137 #[derive(PartialEq)]
138 struct NodeInfo {
139         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
140         channels: Vec<(u64, Sha256dHash)>,
141         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
142         channels: Vec<u64>,
143
144         lowest_inbound_channel_fee_base_msat: u32,
145         lowest_inbound_channel_fee_proportional_millionths: u32,
146
147         features: NodeFeatures,
148         last_update: u32,
149         rgb: [u8; 3],
150         alias: [u8; 32],
151         addresses: Vec<NetAddress>,
152         //this is cached here so we can send out it later if required by route_init_sync
153         //keep an eye on this to see if the extra memory is a problem
154         announcement_message: Option<msgs::NodeAnnouncement>,
155 }
156
157 impl std::fmt::Display for NodeInfo {
158         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
159                 write!(f, "features: {}, last_update: {}, lowest_inbound_channel_fee_base_msat: {}, lowest_inbound_channel_fee_proportional_millionths: {}, channels: {:?}", log_bytes!(self.features.encode()), self.last_update, self.lowest_inbound_channel_fee_base_msat, self.lowest_inbound_channel_fee_proportional_millionths, &self.channels[..])?;
160                 Ok(())
161         }
162 }
163
164 impl Writeable for NodeInfo {
165         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
166                 (self.channels.len() as u64).write(writer)?;
167                 for ref chan in self.channels.iter() {
168                         chan.write(writer)?;
169                 }
170                 self.lowest_inbound_channel_fee_base_msat.write(writer)?;
171                 self.lowest_inbound_channel_fee_proportional_millionths.write(writer)?;
172                 self.features.write(writer)?;
173                 self.last_update.write(writer)?;
174                 self.rgb.write(writer)?;
175                 self.alias.write(writer)?;
176                 (self.addresses.len() as u64).write(writer)?;
177                 for ref addr in &self.addresses {
178                         addr.write(writer)?;
179                 }
180                 self.announcement_message.write(writer)?;
181                 Ok(())
182         }
183 }
184
185 const MAX_ALLOC_SIZE: u64 = 64*1024;
186
187 impl<R: ::std::io::Read> Readable<R> for NodeInfo {
188         fn read(reader: &mut R) -> Result<NodeInfo, DecodeError> {
189                 let channels_count: u64 = Readable::read(reader)?;
190                 let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
191                 for _ in 0..channels_count {
192                         channels.push(Readable::read(reader)?);
193                 }
194                 let lowest_inbound_channel_fee_base_msat = Readable::read(reader)?;
195                 let lowest_inbound_channel_fee_proportional_millionths = Readable::read(reader)?;
196                 let features = Readable::read(reader)?;
197                 let last_update = Readable::read(reader)?;
198                 let rgb = Readable::read(reader)?;
199                 let alias = Readable::read(reader)?;
200                 let addresses_count: u64 = Readable::read(reader)?;
201                 let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
202                 for _ in 0..addresses_count {
203                         match Readable::read(reader) {
204                                 Ok(Ok(addr)) => { addresses.push(addr); },
205                                 Ok(Err(_)) => return Err(DecodeError::InvalidValue),
206                                 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
207                                 _ => unreachable!(),
208                         }
209                 }
210                 let announcement_message = Readable::read(reader)?;
211                 Ok(NodeInfo {
212                         channels,
213                         lowest_inbound_channel_fee_base_msat,
214                         lowest_inbound_channel_fee_proportional_millionths,
215                         features,
216                         last_update,
217                         rgb,
218                         alias,
219                         addresses,
220                         announcement_message
221                 })
222         }
223 }
224
225 #[derive(PartialEq)]
226 struct NetworkMap {
227         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
228         channels: BTreeMap<(u64, Sha256dHash), ChannelInfo>,
229         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
230         channels: BTreeMap<u64, ChannelInfo>,
231
232         our_node_id: PublicKey,
233         nodes: BTreeMap<PublicKey, NodeInfo>,
234 }
235
236 impl Writeable for NetworkMap {
237         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
238                 (self.channels.len() as u64).write(writer)?;
239                 for (ref chan_id, ref chan_info) in self.channels.iter() {
240                         (*chan_id).write(writer)?;
241                         chan_info.write(writer)?;
242                 }
243                 self.our_node_id.write(writer)?;
244                 (self.nodes.len() as u64).write(writer)?;
245                 for (ref node_id, ref node_info) in self.nodes.iter() {
246                         node_id.write(writer)?;
247                         node_info.write(writer)?;
248                 }
249                 Ok(())
250         }
251 }
252
253 impl<R: ::std::io::Read> Readable<R> for NetworkMap {
254         fn read(reader: &mut R) -> Result<NetworkMap, DecodeError> {
255                 let channels_count: u64 = Readable::read(reader)?;
256                 let mut channels = BTreeMap::new();
257                 for _ in 0..channels_count {
258                         let chan_id: u64 = Readable::read(reader)?;
259                         let chan_info = Readable::read(reader)?;
260                         channels.insert(chan_id, chan_info);
261                 }
262                 let our_node_id = Readable::read(reader)?;
263                 let nodes_count: u64 = Readable::read(reader)?;
264                 let mut nodes = BTreeMap::new();
265                 for _ in 0..nodes_count {
266                         let node_id = Readable::read(reader)?;
267                         let node_info = Readable::read(reader)?;
268                         nodes.insert(node_id, node_info);
269                 }
270                 Ok(NetworkMap {
271                         channels,
272                         our_node_id,
273                         nodes,
274                 })
275         }
276 }
277
278 struct MutNetworkMap<'a> {
279         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
280         channels: &'a mut BTreeMap<(u64, Sha256dHash), ChannelInfo>,
281         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
282         channels: &'a mut BTreeMap<u64, ChannelInfo>,
283         nodes: &'a mut BTreeMap<PublicKey, NodeInfo>,
284 }
285 impl NetworkMap {
286         fn borrow_parts(&mut self) -> MutNetworkMap {
287                 MutNetworkMap {
288                         channels: &mut self.channels,
289                         nodes: &mut self.nodes,
290                 }
291         }
292 }
293 impl std::fmt::Display for NetworkMap {
294         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
295                 write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?;
296                 for (key, val) in self.channels.iter() {
297                         write!(f, " {}: {}\n", key, val)?;
298                 }
299                 write!(f, "[Nodes]\n")?;
300                 for (key, val) in self.nodes.iter() {
301                         write!(f, " {}: {}\n", log_pubkey!(key), val)?;
302                 }
303                 Ok(())
304         }
305 }
306
307 impl NetworkMap {
308         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
309         #[inline]
310         fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
311                 (short_channel_id, chain_hash)
312         }
313
314         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
315         #[inline]
316         fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
317                 short_channel_id
318         }
319
320         #[cfg(feature = "non_bitcoin_chain_hash_routing")]
321         #[inline]
322         fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 {
323                 &id.0
324         }
325
326         #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
327         #[inline]
328         fn get_short_id(id: &u64) -> &u64 {
329                 id
330         }
331 }
332
333 /// A channel descriptor which provides a last-hop route to get_route
334 pub struct RouteHint {
335         /// The node_id of the non-target end of the route
336         pub src_node_id: PublicKey,
337         /// The short_channel_id of this channel
338         pub short_channel_id: u64,
339         /// The static msat-denominated fee which must be paid to use this channel
340         pub fee_base_msat: u32,
341         /// The dynamic proportional fee which must be paid to use this channel, denominated in
342         /// millionths of the value being forwarded to the next hop.
343         pub fee_proportional_millionths: u32,
344         /// The difference in CLTV values between this node and the next node.
345         pub cltv_expiry_delta: u16,
346         /// The minimum value, in msat, which must be relayed to the next hop.
347         pub htlc_minimum_msat: u64,
348 }
349
350 /// Tracks a view of the network, receiving updates from peers and generating Routes to
351 /// payment destinations.
352 pub struct Router {
353         secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
354         network_map: RwLock<NetworkMap>,
355         chain_monitor: Arc<ChainWatchInterface>,
356         logger: Arc<Logger>,
357 }
358
359 const SERIALIZATION_VERSION: u8 = 1;
360 const MIN_SERIALIZATION_VERSION: u8 = 1;
361
362 impl Writeable for Router {
363         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
364                 writer.write_all(&[SERIALIZATION_VERSION; 1])?;
365                 writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
366
367                 let network = self.network_map.read().unwrap();
368                 network.write(writer)?;
369                 Ok(())
370         }
371 }
372
373 /// Arguments for the creation of a Router that are not deserialized.
374 /// At a high-level, the process for deserializing a Router and resuming normal operation is:
375 /// 1) Deserialize the Router by filling in this struct and calling <Router>::read(reaser, args).
376 /// 2) Register the new Router with your ChainWatchInterface
377 pub struct RouterReadArgs {
378         /// The ChainWatchInterface for use in the Router in the future.
379         ///
380         /// No calls to the ChainWatchInterface will be made during deserialization.
381         pub chain_monitor: Arc<ChainWatchInterface>,
382         /// The Logger for use in the ChannelManager and which may be used to log information during
383         /// deserialization.
384         pub logger: Arc<Logger>,
385 }
386
387 impl<R: ::std::io::Read> ReadableArgs<R, RouterReadArgs> for Router {
388         fn read(reader: &mut R, args: RouterReadArgs) -> Result<Router, DecodeError> {
389                 let _ver: u8 = Readable::read(reader)?;
390                 let min_ver: u8 = Readable::read(reader)?;
391                 if min_ver > SERIALIZATION_VERSION {
392                         return Err(DecodeError::UnknownVersion);
393                 }
394                 let network_map = Readable::read(reader)?;
395                 Ok(Router {
396                         secp_ctx: Secp256k1::verification_only(),
397                         network_map: RwLock::new(network_map),
398                         chain_monitor: args.chain_monitor,
399                         logger: args.logger,
400                 })
401         }
402 }
403
404 macro_rules! secp_verify_sig {
405         ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
406                 match $secp_ctx.verify($msg, $sig, $pubkey) {
407                         Ok(_) => {},
408                         Err(_) => return Err(LightningError{err: "Invalid signature from remote node", action: ErrorAction::IgnoreError}),
409                 }
410         };
411 }
412
413 impl RoutingMessageHandler for Router {
414         fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
415                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
416                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
417
418                 let mut network = self.network_map.write().unwrap();
419                 match network.nodes.get_mut(&msg.contents.node_id) {
420                         None => Err(LightningError{err: "No existing channels for node_announcement", action: ErrorAction::IgnoreError}),
421                         Some(node) => {
422                                 if node.last_update >= msg.contents.timestamp {
423                                         return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
424                                 }
425
426                                 node.features = msg.contents.features.clone();
427                                 node.last_update = msg.contents.timestamp;
428                                 node.rgb = msg.contents.rgb;
429                                 node.alias = msg.contents.alias;
430                                 node.addresses = msg.contents.addresses.clone();
431
432                                 let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty();
433                                 node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
434                                 Ok(should_relay)
435                         }
436                 }
437         }
438
439         fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
440                 if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
441                         return Err(LightningError{err: "Channel announcement node had a channel with itself", action: ErrorAction::IgnoreError});
442                 }
443
444                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
445                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
446                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
447                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
448                 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
449
450                 let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
451                         Ok((script_pubkey, _value)) => {
452                                 let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
453                                                                     .push_slice(&msg.contents.bitcoin_key_1.serialize())
454                                                                     .push_slice(&msg.contents.bitcoin_key_2.serialize())
455                                                                     .push_opcode(opcodes::all::OP_PUSHNUM_2)
456                                                                     .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
457                                 if script_pubkey != expected_script {
458                                         return Err(LightningError{err: "Channel announcement keys didn't match on-chain script", action: ErrorAction::IgnoreError});
459                                 }
460                                 //TODO: Check if value is worth storing, use it to inform routing, and compare it
461                                 //to the new HTLC max field in channel_update
462                                 true
463                         },
464                         Err(ChainError::NotSupported) => {
465                                 // Tentatively accept, potentially exposing us to DoS attacks
466                                 false
467                         },
468                         Err(ChainError::NotWatched) => {
469                                 return Err(LightningError{err: "Channel announced on an unknown chain", action: ErrorAction::IgnoreError});
470                         },
471                         Err(ChainError::UnknownTx) => {
472                                 return Err(LightningError{err: "Channel announced without corresponding UTXO entry", action: ErrorAction::IgnoreError});
473                         },
474                 };
475
476                 let mut network_lock = self.network_map.write().unwrap();
477                 let network = network_lock.borrow_parts();
478
479                 let should_relay = msg.contents.excess_data.is_empty();
480
481                 let chan_info = ChannelInfo {
482                                 features: msg.contents.features.clone(),
483                                 one_to_two: DirectionalChannelInfo {
484                                         src_node_id: msg.contents.node_id_1.clone(),
485                                         last_update: 0,
486                                         enabled: false,
487                                         cltv_expiry_delta: u16::max_value(),
488                                         htlc_minimum_msat: u64::max_value(),
489                                         fee_base_msat: u32::max_value(),
490                                         fee_proportional_millionths: u32::max_value(),
491                                         last_update_message: None,
492                                 },
493                                 two_to_one: DirectionalChannelInfo {
494                                         src_node_id: msg.contents.node_id_2.clone(),
495                                         last_update: 0,
496                                         enabled: false,
497                                         cltv_expiry_delta: u16::max_value(),
498                                         htlc_minimum_msat: u64::max_value(),
499                                         fee_base_msat: u32::max_value(),
500                                         fee_proportional_millionths: u32::max_value(),
501                                         last_update_message: None,
502                                 },
503                                 announcement_message: if should_relay { Some(msg.clone()) } else { None },
504                         };
505
506                 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
507                         BtreeEntry::Occupied(mut entry) => {
508                                 //TODO: because asking the blockchain if short_channel_id is valid is only optional
509                                 //in the blockchain API, we need to handle it smartly here, though it's unclear
510                                 //exactly how...
511                                 if checked_utxo {
512                                         // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
513                                         // only sometimes returns results. In any case remove the previous entry. Note
514                                         // that the spec expects us to "blacklist" the node_ids involved, but we can't
515                                         // do that because
516                                         // a) we don't *require* a UTXO provider that always returns results.
517                                         // b) we don't track UTXOs of channels we know about and remove them if they
518                                         //    get reorg'd out.
519                                         // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
520                                         Self::remove_channel_in_nodes(network.nodes, &entry.get(), msg.contents.short_channel_id);
521                                         *entry.get_mut() = chan_info;
522                                 } else {
523                                         return Err(LightningError{err: "Already have knowledge of channel", action: ErrorAction::IgnoreError})
524                                 }
525                         },
526                         BtreeEntry::Vacant(entry) => {
527                                 entry.insert(chan_info);
528                         }
529                 };
530
531                 macro_rules! add_channel_to_node {
532                         ( $node_id: expr ) => {
533                                 match network.nodes.entry($node_id) {
534                                         BtreeEntry::Occupied(node_entry) => {
535                                                 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
536                                         },
537                                         BtreeEntry::Vacant(node_entry) => {
538                                                 node_entry.insert(NodeInfo {
539                                                         channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
540                                                         lowest_inbound_channel_fee_base_msat: u32::max_value(),
541                                                         lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
542                                                         features: NodeFeatures::empty(),
543                                                         last_update: 0,
544                                                         rgb: [0; 3],
545                                                         alias: [0; 32],
546                                                         addresses: Vec::new(),
547                                                         announcement_message: None,
548                                                 });
549                                         }
550                                 }
551                         };
552                 }
553
554                 add_channel_to_node!(msg.contents.node_id_1);
555                 add_channel_to_node!(msg.contents.node_id_2);
556
557                 Ok(should_relay)
558         }
559
560         fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
561                 match update {
562                         &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
563                                 let _ = self.handle_channel_update(msg);
564                         },
565                         &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
566                                 let mut network = self.network_map.write().unwrap();
567                                 if *is_permanent {
568                                         if let Some(chan) = network.channels.remove(short_channel_id) {
569                                                 Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
570                                         }
571                                 } else {
572                                         if let Some(chan) = network.channels.get_mut(short_channel_id) {
573                                                 chan.one_to_two.enabled = false;
574                                                 chan.two_to_one.enabled = false;
575                                         }
576                                 }
577                         },
578                         &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
579                                 if *is_permanent {
580                                         //TODO: Wholly remove the node
581                                 } else {
582                                         self.mark_node_bad(node_id, false);
583                                 }
584                         },
585                 }
586         }
587
588         fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> {
589                 let mut network = self.network_map.write().unwrap();
590                 let dest_node_id;
591                 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
592                 let chan_was_enabled;
593
594                 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
595                         None => return Err(LightningError{err: "Couldn't find channel for update", action: ErrorAction::IgnoreError}),
596                         Some(channel) => {
597                                 macro_rules! maybe_update_channel_info {
598                                         ( $target: expr) => {
599                                                 if $target.last_update >= msg.contents.timestamp {
600                                                         return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
601                                                 }
602                                                 chan_was_enabled = $target.enabled;
603                                                 $target.last_update = msg.contents.timestamp;
604                                                 $target.enabled = chan_enabled;
605                                                 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
606                                                 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
607                                                 $target.fee_base_msat = msg.contents.fee_base_msat;
608                                                 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
609                                                 $target.last_update_message = if msg.contents.excess_data.is_empty() {
610                                                         Some(msg.clone())
611                                                 } else {
612                                                         None
613                                                 };
614                                         }
615                                 }
616                                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
617                                 if msg.contents.flags & 1 == 1 {
618                                         dest_node_id = channel.one_to_two.src_node_id.clone();
619                                         secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
620                                         maybe_update_channel_info!(channel.two_to_one);
621                                 } else {
622                                         dest_node_id = channel.two_to_one.src_node_id.clone();
623                                         secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
624                                         maybe_update_channel_info!(channel.one_to_two);
625                                 }
626                         }
627                 }
628
629                 if chan_enabled {
630                         let node = network.nodes.get_mut(&dest_node_id).unwrap();
631                         node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
632                         node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
633                 } else if chan_was_enabled {
634                         let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
635                         let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
636
637                         {
638                                 let node = network.nodes.get(&dest_node_id).unwrap();
639
640                                 for chan_id in node.channels.iter() {
641                                         let chan = network.channels.get(chan_id).unwrap();
642                                         if chan.one_to_two.src_node_id == dest_node_id {
643                                                 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
644                                                 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
645                                         } else {
646                                                 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
647                                                 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
648                                         }
649                                 }
650                         }
651
652                         //TODO: satisfy the borrow-checker without a double-map-lookup :(
653                         let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
654                         mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
655                         mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
656                 }
657
658                 Ok(msg.contents.excess_data.is_empty())
659         }
660
661
662         fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
663                 let mut result = Vec::with_capacity(batch_amount as usize);
664                 let network = self.network_map.read().unwrap();
665                 let mut iter = network.channels.range(starting_point..);
666                 while result.len() < batch_amount as usize {
667                         if let Some((_, ref chan)) = iter.next() {
668                                 if chan.announcement_message.is_some() &&
669                                                 chan.one_to_two.last_update_message.is_some() &&
670                                                 chan.two_to_one.last_update_message.is_some() {
671                                         result.push((chan.announcement_message.clone().unwrap(),
672                                                 chan.one_to_two.last_update_message.clone().unwrap(),
673                                                 chan.two_to_one.last_update_message.clone().unwrap()));
674                                 } else {
675                                         // TODO: We may end up sending un-announced channel_updates if we are sending
676                                         // initial sync data while receiving announce/updates for this channel.
677                                 }
678                         } else {
679                                 return result;
680                         }
681                 }
682                 result
683         }
684
685         fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
686                 let mut result = Vec::with_capacity(batch_amount as usize);
687                 let network = self.network_map.read().unwrap();
688                 let mut iter = if let Some(pubkey) = starting_point {
689                                 let mut iter = network.nodes.range((*pubkey)..);
690                                 iter.next();
691                                 iter
692                         } else {
693                                 network.nodes.range(..)
694                         };
695                 while result.len() < batch_amount as usize {
696                         if let Some((_, ref node)) = iter.next() {
697                                 if node.announcement_message.is_some() {
698                                         result.push(node.announcement_message.clone().unwrap());
699                                 }
700                         } else {
701                                 return result;
702                         }
703                 }
704                 result
705         }
706 }
707
708 #[derive(Eq, PartialEq)]
709 struct RouteGraphNode {
710         pubkey: PublicKey,
711         lowest_fee_to_peer_through_node: u64,
712         lowest_fee_to_node: u64,
713 }
714
715 impl cmp::Ord for RouteGraphNode {
716         fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
717                 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
718                         .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
719         }
720 }
721
722 impl cmp::PartialOrd for RouteGraphNode {
723         fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
724                 Some(self.cmp(other))
725         }
726 }
727
728 struct DummyDirectionalChannelInfo {
729         src_node_id: PublicKey,
730         cltv_expiry_delta: u32,
731         htlc_minimum_msat: u64,
732         fee_base_msat: u32,
733         fee_proportional_millionths: u32,
734 }
735
736 impl Router {
737         /// Creates a new router with the given node_id to be used as the source for get_route()
738         pub fn new(our_pubkey: PublicKey, chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Router {
739                 let mut nodes = BTreeMap::new();
740                 nodes.insert(our_pubkey.clone(), NodeInfo {
741                         channels: Vec::new(),
742                         lowest_inbound_channel_fee_base_msat: u32::max_value(),
743                         lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
744                         features: NodeFeatures::empty(),
745                         last_update: 0,
746                         rgb: [0; 3],
747                         alias: [0; 32],
748                         addresses: Vec::new(),
749                         announcement_message: None,
750                 });
751                 Router {
752                         secp_ctx: Secp256k1::verification_only(),
753                         network_map: RwLock::new(NetworkMap {
754                                 channels: BTreeMap::new(),
755                                 our_node_id: our_pubkey,
756                                 nodes: nodes,
757                         }),
758                         chain_monitor,
759                         logger,
760                 }
761         }
762
763         /// Dumps the entire network view of this Router to the logger provided in the constructor at
764         /// level Trace
765         pub fn trace_state(&self) {
766                 log_trace!(self, "{}", self.network_map.read().unwrap());
767         }
768
769         /// Get network addresses by node id
770         pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
771                 let network = self.network_map.read().unwrap();
772                 network.nodes.get(pubkey).map(|n| n.addresses.clone())
773         }
774
775         /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
776         /// with an exponential decay in node "badness". Note that there is deliberately no
777         /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
778         /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
779         /// boolean will reduce the penalty, returning the node to usability faster. If the node is
780         /// behaving correctly, it will disable the failing channel and we will use it again next time.
781         pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
782                 unimplemented!();
783         }
784
785         fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
786                 macro_rules! remove_from_node {
787                         ($node_id: expr) => {
788                                 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
789                                         entry.get_mut().channels.retain(|chan_id| {
790                                                 short_channel_id != *NetworkMap::get_short_id(chan_id)
791                                         });
792                                         if entry.get().channels.is_empty() {
793                                                 entry.remove_entry();
794                                         }
795                                 } else {
796                                         panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
797                                 }
798                         }
799                 }
800                 remove_from_node!(chan.one_to_two.src_node_id);
801                 remove_from_node!(chan.two_to_one.src_node_id);
802         }
803
804         /// Gets a route from us to the given target node.
805         ///
806         /// Extra routing hops between known nodes and the target will be used if they are included in
807         /// last_hops.
808         ///
809         /// If some channels aren't announced, it may be useful to fill in a first_hops with the
810         /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
811         /// (this Router's) view of our local channels will be ignored, and only those in first_hops
812         /// will be used.
813         ///
814         /// Panics if first_hops contains channels without short_channel_ids
815         /// (ChannelManager::list_usable_channels will never include such channels).
816         ///
817         /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
818         /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
819         /// *is* checked as they may change based on the receiving node.
820         pub fn get_route(&self, target: &PublicKey, first_hops: Option<&[channelmanager::ChannelDetails]>, last_hops: &[RouteHint], final_value_msat: u64, final_cltv: u32) -> Result<Route, LightningError> {
821                 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
822                 // uptime/success in using a node in the past.
823                 let network = self.network_map.read().unwrap();
824
825                 if *target == network.our_node_id {
826                         return Err(LightningError{err: "Cannot generate a route to ourselves", action: ErrorAction::IgnoreError});
827                 }
828
829                 if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 {
830                         return Err(LightningError{err: "Cannot generate a route of more value than all existing satoshis", action: ErrorAction::IgnoreError});
831                 }
832
833                 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
834                 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
835                 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
836                 // to use as the A* heuristic beyond just the cost to get one node further than the current
837                 // one.
838
839                 let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes
840                         src_node_id: network.our_node_id.clone(),
841                         cltv_expiry_delta: 0,
842                         htlc_minimum_msat: 0,
843                         fee_base_msat: 0,
844                         fee_proportional_millionths: 0,
845                 };
846
847                 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
848                 let mut dist = HashMap::with_capacity(network.nodes.len());
849
850                 let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 });
851                 if let Some(hops) = first_hops {
852                         for chan in hops {
853                                 let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones");
854                                 if chan.remote_network_id == *target {
855                                         return Ok(Route {
856                                                 hops: vec![RouteHop {
857                                                         pubkey: chan.remote_network_id,
858                                                         short_channel_id,
859                                                         fee_msat: final_value_msat,
860                                                         cltv_expiry_delta: final_cltv,
861                                                 }],
862                                         });
863                                 }
864                                 first_hop_targets.insert(chan.remote_network_id, short_channel_id);
865                         }
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});
868                         }
869                 }
870
871                 macro_rules! add_entry {
872                         // Adds entry which goes from the node pointed to by $directional_info to
873                         // $dest_node_id over the channel with id $chan_id with fees described in
874                         // $directional_info.
875                         ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $starting_fee_msat: expr ) => {
876                                 //TODO: Explore simply adding fee to hit htlc_minimum_msat
877                                 if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat {
878                                         let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64);
879                                         if let Some(new_fee) = proportional_fee_millions.and_then(|part| {
880                                                         ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) })
881                                         {
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();
886                                                         (u64::max_value(),
887                                                                 node.lowest_inbound_channel_fee_base_msat,
888                                                                 node.lowest_inbound_channel_fee_proportional_millionths,
889                                                                 RouteHop {
890                                                                         pubkey: $dest_node_id.clone(),
891                                                                         short_channel_id: 0,
892                                                                         fee_msat: 0,
893                                                                         cltv_expiry_delta: 0,
894                                                         })
895                                                 });
896                                                 if $directional_info.src_node_id != network.our_node_id {
897                                                         // Ignore new_fee for channel-from-us as we assume all channels-from-us
898                                                         // will have the same effective-fee
899                                                         total_fee += new_fee;
900                                                         if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) {
901                                                                 total_fee += fee_inc / 1000000 + (old_entry.1 as u64);
902                                                         } else {
903                                                                 // max_value means we'll always fail the old_entry.0 > total_fee check
904                                                                 total_fee = u64::max_value();
905                                                         }
906                                                 }
907                                                 let new_graph_node = RouteGraphNode {
908                                                         pubkey: $directional_info.src_node_id,
909                                                         lowest_fee_to_peer_through_node: total_fee,
910                                                         lowest_fee_to_node: $starting_fee_msat as u64 + new_fee,
911                                                 };
912                                                 if old_entry.0 > total_fee {
913                                                         targets.push(new_graph_node);
914                                                         old_entry.0 = total_fee;
915                                                         old_entry.3 = RouteHop {
916                                                                 pubkey: $dest_node_id.clone(),
917                                                                 short_channel_id: $chan_id.clone(),
918                                                                 fee_msat: new_fee, // This field is ignored on the last-hop anyway
919                                                                 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
920                                                         }
921                                                 }
922                                         }
923                                 }
924                         };
925                 }
926
927                 macro_rules! add_entries_to_cheapest_to_target_node {
928                         ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
929                                 if first_hops.is_some() {
930                                         if let Some(first_hop) = first_hop_targets.get(&$node_id) {
931                                                 add_entry!(first_hop, $node_id, dummy_directional_info, $fee_to_target_msat);
932                                         }
933                                 }
934
935                                 if !$node.features.requires_unknown_bits() {
936                                         for chan_id in $node.channels.iter() {
937                                                 let chan = network.channels.get(chan_id).unwrap();
938                                                 if !chan.features.requires_unknown_bits() {
939                                                         if chan.one_to_two.src_node_id == *$node_id {
940                                                                 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
941                                                                 if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id {
942                                                                         if chan.two_to_one.enabled {
943                                                                                 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
944                                                                         }
945                                                                 }
946                                                         } else {
947                                                                 if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id {
948                                                                         if chan.one_to_two.enabled {
949                                                                                 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
950                                                                         }
951                                                                 }
952                                                         }
953                                                 }
954                                         }
955                                 }
956                         };
957                 }
958
959                 match network.nodes.get(target) {
960                         None => {},
961                         Some(node) => {
962                                 add_entries_to_cheapest_to_target_node!(node, target, 0);
963                         },
964                 }
965
966                 for hop in last_hops.iter() {
967                         if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops
968                                 if network.nodes.get(&hop.src_node_id).is_some() {
969                                         if first_hops.is_some() {
970                                                 if let Some(first_hop) = first_hop_targets.get(&hop.src_node_id) {
971                                                         add_entry!(first_hop, hop.src_node_id, dummy_directional_info, 0);
972                                                 }
973                                         }
974                                         add_entry!(hop.short_channel_id, target, hop, 0);
975                                 }
976                         }
977                 }
978
979                 while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() {
980                         if pubkey == network.our_node_id {
981                                 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
982                                 while res.last().unwrap().pubkey != *target {
983                                         let new_entry = match dist.remove(&res.last().unwrap().pubkey) {
984                                                 Some(hop) => hop.3,
985                                                 None => return Err(LightningError{err: "Failed to find a non-fee-overflowing path to the given destination", action: ErrorAction::IgnoreError}),
986                                         };
987                                         res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
988                                         res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
989                                         res.push(new_entry);
990                                 }
991                                 res.last_mut().unwrap().fee_msat = final_value_msat;
992                                 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
993                                 let route = Route { hops: res };
994                                 log_trace!(self, "Got route: {}", log_route!(route));
995                                 return Ok(route);
996                         }
997
998                         match network.nodes.get(&pubkey) {
999                                 None => {},
1000                                 Some(node) => {
1001                                         add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node);
1002                                 },
1003                         }
1004                 }
1005
1006                 Err(LightningError{err: "Failed to find a path to the given destination", action: ErrorAction::IgnoreError})
1007         }
1008 }
1009
1010 #[cfg(test)]
1011 mod tests {
1012         use chain::chaininterface;
1013         use ln::channelmanager;
1014         use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
1015         use ln::features::{ChannelFeatures, NodeFeatures};
1016         use ln::msgs::{LightningError, ErrorAction};
1017         use util::test_utils;
1018         use util::test_utils::TestVecWriter;
1019         use util::logger::Logger;
1020         use util::ser::{Writeable, Readable};
1021
1022         use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1023         use bitcoin_hashes::Hash;
1024         use bitcoin::network::constants::Network;
1025
1026         use hex;
1027
1028         use secp256k1::key::{PublicKey,SecretKey};
1029         use secp256k1::Secp256k1;
1030
1031         use std::sync::Arc;
1032
1033         #[test]
1034         fn route_test() {
1035                 let secp_ctx = Secp256k1::new();
1036                 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
1037                 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1038                 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
1039                 let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
1040
1041                 // Build network from our_id to node8:
1042                 //
1043                 //        -1(1)2-  node1  -1(3)2-
1044                 //       /                       \
1045                 // our_id -1(12)2- node8 -1(13)2--- node3
1046                 //       \                       /
1047                 //        -1(2)2-  node2  -1(4)2-
1048                 //
1049                 //
1050                 // chan1  1-to-2: disabled
1051                 // chan1  2-to-1: enabled, 0 fee
1052                 //
1053                 // chan2  1-to-2: enabled, ignored fee
1054                 // chan2  2-to-1: enabled, 0 fee
1055                 //
1056                 // chan3  1-to-2: enabled, 0 fee
1057                 // chan3  2-to-1: enabled, 100 msat fee
1058                 //
1059                 // chan4  1-to-2: enabled, 100% fee
1060                 // chan4  2-to-1: enabled, 0 fee
1061                 //
1062                 // chan12 1-to-2: enabled, ignored fee
1063                 // chan12 2-to-1: enabled, 0 fee
1064                 //
1065                 // chan13 1-to-2: enabled, 200% fee
1066                 // chan13 2-to-1: enabled, 0 fee
1067                 //
1068                 //
1069                 //       -1(5)2- node4 -1(8)2--
1070                 //       |         2          |
1071                 //       |       (11)         |
1072                 //      /          1           \
1073                 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
1074                 //      \                      /
1075                 //       -1(7)2- node6 -1(10)2-
1076                 //
1077                 // chan5  1-to-2: enabled, 100 msat fee
1078                 // chan5  2-to-1: enabled, 0 fee
1079                 //
1080                 // chan6  1-to-2: enabled, 0 fee
1081                 // chan6  2-to-1: enabled, 0 fee
1082                 //
1083                 // chan7  1-to-2: enabled, 100% fee
1084                 // chan7  2-to-1: enabled, 0 fee
1085                 //
1086                 // chan8  1-to-2: enabled, variable fee (0 then 1000 msat)
1087                 // chan8  2-to-1: enabled, 0 fee
1088                 //
1089                 // chan9  1-to-2: enabled, 1001 msat fee
1090                 // chan9  2-to-1: enabled, 0 fee
1091                 //
1092                 // chan10 1-to-2: enabled, 0 fee
1093                 // chan10 2-to-1: enabled, 0 fee
1094                 //
1095                 // chan11 1-to-2: enabled, 0 fee
1096                 // chan11 2-to-1: enabled, 0 fee
1097
1098                 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
1099                 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap());
1100                 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap());
1101                 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap());
1102                 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap());
1103                 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap());
1104                 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap());
1105                 let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap());
1106
1107                 let zero_hash = Sha256dHash::hash(&[0; 32]);
1108
1109                 {
1110                         let mut network = router.network_map.write().unwrap();
1111
1112                         network.nodes.insert(node1.clone(), NodeInfo {
1113                                 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
1114                                 lowest_inbound_channel_fee_base_msat: 100,
1115                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1116                                 features: NodeFeatures::empty(),
1117                                 last_update: 1,
1118                                 rgb: [0; 3],
1119                                 alias: [0; 32],
1120                                 addresses: Vec::new(),
1121                                 announcement_message: None,
1122                         });
1123                         network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
1124                                 features: ChannelFeatures::empty(),
1125                                 one_to_two: DirectionalChannelInfo {
1126                                         src_node_id: our_id.clone(),
1127                                         last_update: 0,
1128                                         enabled: false,
1129                                         cltv_expiry_delta: u16::max_value(), // This value should be ignored
1130                                         htlc_minimum_msat: 0,
1131                                         fee_base_msat: u32::max_value(), // This value should be ignored
1132                                         fee_proportional_millionths: u32::max_value(), // This value should be ignored
1133                                         last_update_message: None,
1134                                 }, two_to_one: DirectionalChannelInfo {
1135                                         src_node_id: node1.clone(),
1136                                         last_update: 0,
1137                                         enabled: true,
1138                                         cltv_expiry_delta: 0,
1139                                         htlc_minimum_msat: 0,
1140                                         fee_base_msat: 0,
1141                                         fee_proportional_millionths: 0,
1142                                         last_update_message: None,
1143                                 },
1144                                 announcement_message: None,
1145                         });
1146                         network.nodes.insert(node2.clone(), NodeInfo {
1147                                 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
1148                                 lowest_inbound_channel_fee_base_msat: 0,
1149                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1150                                 features: NodeFeatures::empty(),
1151                                 last_update: 1,
1152                                 rgb: [0; 3],
1153                                 alias: [0; 32],
1154                                 addresses: Vec::new(),
1155                                 announcement_message: None,
1156                         });
1157                         network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
1158                                 features: ChannelFeatures::empty(),
1159                                 one_to_two: DirectionalChannelInfo {
1160                                         src_node_id: our_id.clone(),
1161                                         last_update: 0,
1162                                         enabled: true,
1163                                         cltv_expiry_delta: u16::max_value(), // This value should be ignored
1164                                         htlc_minimum_msat: 0,
1165                                         fee_base_msat: u32::max_value(), // This value should be ignored
1166                                         fee_proportional_millionths: u32::max_value(), // This value should be ignored
1167                                         last_update_message: None,
1168                                 }, two_to_one: DirectionalChannelInfo {
1169                                         src_node_id: node2.clone(),
1170                                         last_update: 0,
1171                                         enabled: true,
1172                                         cltv_expiry_delta: 0,
1173                                         htlc_minimum_msat: 0,
1174                                         fee_base_msat: 0,
1175                                         fee_proportional_millionths: 0,
1176                                         last_update_message: None,
1177                                 },
1178                                 announcement_message: None,
1179                         });
1180                         network.nodes.insert(node8.clone(), NodeInfo {
1181                                 channels: vec!(NetworkMap::get_key(12, zero_hash.clone()), NetworkMap::get_key(13, zero_hash.clone())),
1182                                 lowest_inbound_channel_fee_base_msat: 0,
1183                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1184                                 features: NodeFeatures::empty(),
1185                                 last_update: 1,
1186                                 rgb: [0; 3],
1187                                 alias: [0; 32],
1188                                 addresses: Vec::new(),
1189                                 announcement_message: None,
1190                         });
1191                         network.channels.insert(NetworkMap::get_key(12, zero_hash.clone()), ChannelInfo {
1192                                 features: ChannelFeatures::empty(),
1193                                 one_to_two: DirectionalChannelInfo {
1194                                         src_node_id: our_id.clone(),
1195                                         last_update: 0,
1196                                         enabled: true,
1197                                         cltv_expiry_delta: u16::max_value(), // This value should be ignored
1198                                         htlc_minimum_msat: 0,
1199                                         fee_base_msat: u32::max_value(), // This value should be ignored
1200                                         fee_proportional_millionths: u32::max_value(), // This value should be ignored
1201                                         last_update_message: None,
1202                                 }, two_to_one: DirectionalChannelInfo {
1203                                         src_node_id: node8.clone(),
1204                                         last_update: 0,
1205                                         enabled: true,
1206                                         cltv_expiry_delta: 0,
1207                                         htlc_minimum_msat: 0,
1208                                         fee_base_msat: 0,
1209                                         fee_proportional_millionths: 0,
1210                                         last_update_message: None,
1211                                 },
1212                                 announcement_message: None,
1213                         });
1214                         network.nodes.insert(node3.clone(), NodeInfo {
1215                                 channels: vec!(
1216                                         NetworkMap::get_key(3, zero_hash.clone()),
1217                                         NetworkMap::get_key(4, zero_hash.clone()),
1218                                         NetworkMap::get_key(13, zero_hash.clone()),
1219                                         NetworkMap::get_key(5, zero_hash.clone()),
1220                                         NetworkMap::get_key(6, zero_hash.clone()),
1221                                         NetworkMap::get_key(7, zero_hash.clone())),
1222                                 lowest_inbound_channel_fee_base_msat: 0,
1223                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1224                                 features: NodeFeatures::empty(),
1225                                 last_update: 1,
1226                                 rgb: [0; 3],
1227                                 alias: [0; 32],
1228                                 addresses: Vec::new(),
1229                                 announcement_message: None,
1230                         });
1231                         network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
1232                                 features: ChannelFeatures::empty(),
1233                                 one_to_two: DirectionalChannelInfo {
1234                                         src_node_id: node1.clone(),
1235                                         last_update: 0,
1236                                         enabled: true,
1237                                         cltv_expiry_delta: (3 << 8) | 1,
1238                                         htlc_minimum_msat: 0,
1239                                         fee_base_msat: 0,
1240                                         fee_proportional_millionths: 0,
1241                                         last_update_message: None,
1242                                 }, two_to_one: DirectionalChannelInfo {
1243                                         src_node_id: node3.clone(),
1244                                         last_update: 0,
1245                                         enabled: true,
1246                                         cltv_expiry_delta: (3 << 8) | 2,
1247                                         htlc_minimum_msat: 0,
1248                                         fee_base_msat: 100,
1249                                         fee_proportional_millionths: 0,
1250                                         last_update_message: None,
1251                                 },
1252                                 announcement_message: None,
1253                         });
1254                         network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
1255                                 features: ChannelFeatures::empty(),
1256                                 one_to_two: DirectionalChannelInfo {
1257                                         src_node_id: node2.clone(),
1258                                         last_update: 0,
1259                                         enabled: true,
1260                                         cltv_expiry_delta: (4 << 8) | 1,
1261                                         htlc_minimum_msat: 0,
1262                                         fee_base_msat: 0,
1263                                         fee_proportional_millionths: 1000000,
1264                                         last_update_message: None,
1265                                 }, two_to_one: DirectionalChannelInfo {
1266                                         src_node_id: node3.clone(),
1267                                         last_update: 0,
1268                                         enabled: true,
1269                                         cltv_expiry_delta: (4 << 8) | 2,
1270                                         htlc_minimum_msat: 0,
1271                                         fee_base_msat: 0,
1272                                         fee_proportional_millionths: 0,
1273                                         last_update_message: None,
1274                                 },
1275                                 announcement_message: None,
1276                         });
1277                         network.channels.insert(NetworkMap::get_key(13, zero_hash.clone()), ChannelInfo {
1278                                 features: ChannelFeatures::empty(),
1279                                 one_to_two: DirectionalChannelInfo {
1280                                         src_node_id: node8.clone(),
1281                                         last_update: 0,
1282                                         enabled: true,
1283                                         cltv_expiry_delta: (13 << 8) | 1,
1284                                         htlc_minimum_msat: 0,
1285                                         fee_base_msat: 0,
1286                                         fee_proportional_millionths: 2000000,
1287                                         last_update_message: None,
1288                                 }, two_to_one: DirectionalChannelInfo {
1289                                         src_node_id: node3.clone(),
1290                                         last_update: 0,
1291                                         enabled: true,
1292                                         cltv_expiry_delta: (13 << 8) | 2,
1293                                         htlc_minimum_msat: 0,
1294                                         fee_base_msat: 0,
1295                                         fee_proportional_millionths: 0,
1296                                         last_update_message: None,
1297                                 },
1298                                 announcement_message: None,
1299                         });
1300                         network.nodes.insert(node4.clone(), NodeInfo {
1301                                 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1302                                 lowest_inbound_channel_fee_base_msat: 0,
1303                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1304                                 features: NodeFeatures::empty(),
1305                                 last_update: 1,
1306                                 rgb: [0; 3],
1307                                 alias: [0; 32],
1308                                 addresses: Vec::new(),
1309                                 announcement_message: None,
1310                         });
1311                         network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
1312                                 features: ChannelFeatures::empty(),
1313                                 one_to_two: DirectionalChannelInfo {
1314                                         src_node_id: node3.clone(),
1315                                         last_update: 0,
1316                                         enabled: true,
1317                                         cltv_expiry_delta: (5 << 8) | 1,
1318                                         htlc_minimum_msat: 0,
1319                                         fee_base_msat: 100,
1320                                         fee_proportional_millionths: 0,
1321                                         last_update_message: None,
1322                                 }, two_to_one: DirectionalChannelInfo {
1323                                         src_node_id: node4.clone(),
1324                                         last_update: 0,
1325                                         enabled: true,
1326                                         cltv_expiry_delta: (5 << 8) | 2,
1327                                         htlc_minimum_msat: 0,
1328                                         fee_base_msat: 0,
1329                                         fee_proportional_millionths: 0,
1330                                         last_update_message: None,
1331                                 },
1332                                 announcement_message: None,
1333                         });
1334                         network.nodes.insert(node5.clone(), NodeInfo {
1335                                 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
1336                                 lowest_inbound_channel_fee_base_msat: 0,
1337                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1338                                 features: NodeFeatures::empty(),
1339                                 last_update: 1,
1340                                 rgb: [0; 3],
1341                                 alias: [0; 32],
1342                                 addresses: Vec::new(),
1343                                 announcement_message: None,
1344                         });
1345                         network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
1346                                 features: ChannelFeatures::empty(),
1347                                 one_to_two: DirectionalChannelInfo {
1348                                         src_node_id: node3.clone(),
1349                                         last_update: 0,
1350                                         enabled: true,
1351                                         cltv_expiry_delta: (6 << 8) | 1,
1352                                         htlc_minimum_msat: 0,
1353                                         fee_base_msat: 0,
1354                                         fee_proportional_millionths: 0,
1355                                         last_update_message: None,
1356                                 }, two_to_one: DirectionalChannelInfo {
1357                                         src_node_id: node5.clone(),
1358                                         last_update: 0,
1359                                         enabled: true,
1360                                         cltv_expiry_delta: (6 << 8) | 2,
1361                                         htlc_minimum_msat: 0,
1362                                         fee_base_msat: 0,
1363                                         fee_proportional_millionths: 0,
1364                                         last_update_message: None,
1365                                 },
1366                                 announcement_message: None,
1367                         });
1368                         network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
1369                                 features: ChannelFeatures::empty(),
1370                                 one_to_two: DirectionalChannelInfo {
1371                                         src_node_id: node5.clone(),
1372                                         last_update: 0,
1373                                         enabled: true,
1374                                         cltv_expiry_delta: (11 << 8) | 1,
1375                                         htlc_minimum_msat: 0,
1376                                         fee_base_msat: 0,
1377                                         fee_proportional_millionths: 0,
1378                                         last_update_message: None,
1379                                 }, two_to_one: DirectionalChannelInfo {
1380                                         src_node_id: node4.clone(),
1381                                         last_update: 0,
1382                                         enabled: true,
1383                                         cltv_expiry_delta: (11 << 8) | 2,
1384                                         htlc_minimum_msat: 0,
1385                                         fee_base_msat: 0,
1386                                         fee_proportional_millionths: 0,
1387                                         last_update_message: None,
1388                                 },
1389                                 announcement_message: None,
1390                         });
1391                         network.nodes.insert(node6.clone(), NodeInfo {
1392                                 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
1393                                 lowest_inbound_channel_fee_base_msat: 0,
1394                                 lowest_inbound_channel_fee_proportional_millionths: 0,
1395                                 features: NodeFeatures::empty(),
1396                                 last_update: 1,
1397                                 rgb: [0; 3],
1398                                 alias: [0; 32],
1399                                 addresses: Vec::new(),
1400                                 announcement_message: None,
1401                         });
1402                         network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
1403                                 features: ChannelFeatures::empty(),
1404                                 one_to_two: DirectionalChannelInfo {
1405                                         src_node_id: node3.clone(),
1406                                         last_update: 0,
1407                                         enabled: true,
1408                                         cltv_expiry_delta: (7 << 8) | 1,
1409                                         htlc_minimum_msat: 0,
1410                                         fee_base_msat: 0,
1411                                         fee_proportional_millionths: 1000000,
1412                                         last_update_message: None,
1413                                 }, two_to_one: DirectionalChannelInfo {
1414                                         src_node_id: node6.clone(),
1415                                         last_update: 0,
1416                                         enabled: true,
1417                                         cltv_expiry_delta: (7 << 8) | 2,
1418                                         htlc_minimum_msat: 0,
1419                                         fee_base_msat: 0,
1420                                         fee_proportional_millionths: 0,
1421                                         last_update_message: None,
1422                                 },
1423                                 announcement_message: None,
1424                         });
1425                 }
1426
1427                 { // Simple route to 3 via 2
1428                         let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap();
1429                         assert_eq!(route.hops.len(), 2);
1430
1431                         assert_eq!(route.hops[0].pubkey, node2);
1432                         assert_eq!(route.hops[0].short_channel_id, 2);
1433                         assert_eq!(route.hops[0].fee_msat, 100);
1434                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1435
1436                         assert_eq!(route.hops[1].pubkey, node3);
1437                         assert_eq!(route.hops[1].short_channel_id, 4);
1438                         assert_eq!(route.hops[1].fee_msat, 100);
1439                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1440                 }
1441
1442                 { // Disable channels 4 and 12 by requiring unknown feature bits
1443                         let mut network = router.network_map.write().unwrap();
1444                         network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1445                         network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.set_require_unknown_bits();
1446                 }
1447
1448                 { // If all the channels require some features we don't understand, route should fail
1449                         if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1450                                 assert_eq!(err, "Failed to find a path to the given destination");
1451                         } else { panic!(); }
1452                 }
1453
1454                 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1455                         let our_chans = vec![channelmanager::ChannelDetails {
1456                                 channel_id: [0; 32],
1457                                 short_channel_id: Some(42),
1458                                 remote_network_id: node8.clone(),
1459                                 channel_value_satoshis: 0,
1460                                 user_id: 0,
1461                                 outbound_capacity_msat: 0,
1462                                 inbound_capacity_msat: 0,
1463                                 is_live: true,
1464                         }];
1465                         let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1466                         assert_eq!(route.hops.len(), 2);
1467
1468                         assert_eq!(route.hops[0].pubkey, node8);
1469                         assert_eq!(route.hops[0].short_channel_id, 42);
1470                         assert_eq!(route.hops[0].fee_msat, 200);
1471                         assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1472
1473                         assert_eq!(route.hops[1].pubkey, node3);
1474                         assert_eq!(route.hops[1].short_channel_id, 13);
1475                         assert_eq!(route.hops[1].fee_msat, 100);
1476                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1477                 }
1478
1479                 { // Re-enable channels 4 and 12 by wiping the unknown feature bits
1480                         let mut network = router.network_map.write().unwrap();
1481                         network.channels.get_mut(&NetworkMap::get_key(4, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1482                         network.channels.get_mut(&NetworkMap::get_key(12, zero_hash.clone())).unwrap().features.clear_require_unknown_bits();
1483                 }
1484
1485                 { // Disable nodes 1, 2, and 8 by requiring unknown feature bits
1486                         let mut network = router.network_map.write().unwrap();
1487                         network.nodes.get_mut(&node1).unwrap().features.set_require_unknown_bits();
1488                         network.nodes.get_mut(&node2).unwrap().features.set_require_unknown_bits();
1489                         network.nodes.get_mut(&node8).unwrap().features.set_require_unknown_bits();
1490                 }
1491
1492                 { // If all nodes require some features we don't understand, route should fail
1493                         if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) {
1494                                 assert_eq!(err, "Failed to find a path to the given destination");
1495                         } else { panic!(); }
1496                 }
1497
1498                 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1499                         let our_chans = vec![channelmanager::ChannelDetails {
1500                                 channel_id: [0; 32],
1501                                 short_channel_id: Some(42),
1502                                 remote_network_id: node8.clone(),
1503                                 channel_value_satoshis: 0,
1504                                 user_id: 0,
1505                                 outbound_capacity_msat: 0,
1506                                 inbound_capacity_msat: 0,
1507                                 is_live: true,
1508                         }];
1509                         let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1510                         assert_eq!(route.hops.len(), 2);
1511
1512                         assert_eq!(route.hops[0].pubkey, node8);
1513                         assert_eq!(route.hops[0].short_channel_id, 42);
1514                         assert_eq!(route.hops[0].fee_msat, 200);
1515                         assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1516
1517                         assert_eq!(route.hops[1].pubkey, node3);
1518                         assert_eq!(route.hops[1].short_channel_id, 13);
1519                         assert_eq!(route.hops[1].fee_msat, 100);
1520                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1521                 }
1522
1523                 { // Re-enable nodes 1, 2, and 8
1524                         let mut network = router.network_map.write().unwrap();
1525                         network.nodes.get_mut(&node1).unwrap().features.clear_require_unknown_bits();
1526                         network.nodes.get_mut(&node2).unwrap().features.clear_require_unknown_bits();
1527                         network.nodes.get_mut(&node8).unwrap().features.clear_require_unknown_bits();
1528                 }
1529
1530                 // Note that we don't test disabling node 3 and failing to route to it, as we (somewhat
1531                 // naively) assume that the user checked the feature bits on the invoice, which override
1532                 // the node_announcement.
1533
1534                 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
1535                         let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap();
1536                         assert_eq!(route.hops.len(), 3);
1537
1538                         assert_eq!(route.hops[0].pubkey, node2);
1539                         assert_eq!(route.hops[0].short_channel_id, 2);
1540                         assert_eq!(route.hops[0].fee_msat, 200);
1541                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1542
1543                         assert_eq!(route.hops[1].pubkey, node3);
1544                         assert_eq!(route.hops[1].short_channel_id, 4);
1545                         assert_eq!(route.hops[1].fee_msat, 100);
1546                         assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
1547
1548                         assert_eq!(route.hops[2].pubkey, node1);
1549                         assert_eq!(route.hops[2].short_channel_id, 3);
1550                         assert_eq!(route.hops[2].fee_msat, 100);
1551                         assert_eq!(route.hops[2].cltv_expiry_delta, 42);
1552                 }
1553
1554                 { // If we specify a channel to node8, that overrides our local channel view and that gets used
1555                         let our_chans = vec![channelmanager::ChannelDetails {
1556                                 channel_id: [0; 32],
1557                                 short_channel_id: Some(42),
1558                                 remote_network_id: node8.clone(),
1559                                 channel_value_satoshis: 0,
1560                                 user_id: 0,
1561                                 outbound_capacity_msat: 0,
1562                                 inbound_capacity_msat: 0,
1563                                 is_live: true,
1564                         }];
1565                         let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap();
1566                         assert_eq!(route.hops.len(), 2);
1567
1568                         assert_eq!(route.hops[0].pubkey, node8);
1569                         assert_eq!(route.hops[0].short_channel_id, 42);
1570                         assert_eq!(route.hops[0].fee_msat, 200);
1571                         assert_eq!(route.hops[0].cltv_expiry_delta, (13 << 8) | 1);
1572
1573                         assert_eq!(route.hops[1].pubkey, node3);
1574                         assert_eq!(route.hops[1].short_channel_id, 13);
1575                         assert_eq!(route.hops[1].fee_msat, 100);
1576                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1577                 }
1578
1579                 let mut last_hops = vec!(RouteHint {
1580                                 src_node_id: node4.clone(),
1581                                 short_channel_id: 8,
1582                                 fee_base_msat: 0,
1583                                 fee_proportional_millionths: 0,
1584                                 cltv_expiry_delta: (8 << 8) | 1,
1585                                 htlc_minimum_msat: 0,
1586                         }, RouteHint {
1587                                 src_node_id: node5.clone(),
1588                                 short_channel_id: 9,
1589                                 fee_base_msat: 1001,
1590                                 fee_proportional_millionths: 0,
1591                                 cltv_expiry_delta: (9 << 8) | 1,
1592                                 htlc_minimum_msat: 0,
1593                         }, RouteHint {
1594                                 src_node_id: node6.clone(),
1595                                 short_channel_id: 10,
1596                                 fee_base_msat: 0,
1597                                 fee_proportional_millionths: 0,
1598                                 cltv_expiry_delta: (10 << 8) | 1,
1599                                 htlc_minimum_msat: 0,
1600                         });
1601
1602                 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
1603                         let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1604                         assert_eq!(route.hops.len(), 5);
1605
1606                         assert_eq!(route.hops[0].pubkey, node2);
1607                         assert_eq!(route.hops[0].short_channel_id, 2);
1608                         assert_eq!(route.hops[0].fee_msat, 100);
1609                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1610
1611                         assert_eq!(route.hops[1].pubkey, node3);
1612                         assert_eq!(route.hops[1].short_channel_id, 4);
1613                         assert_eq!(route.hops[1].fee_msat, 0);
1614                         assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1615
1616                         assert_eq!(route.hops[2].pubkey, node5);
1617                         assert_eq!(route.hops[2].short_channel_id, 6);
1618                         assert_eq!(route.hops[2].fee_msat, 0);
1619                         assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1620
1621                         assert_eq!(route.hops[3].pubkey, node4);
1622                         assert_eq!(route.hops[3].short_channel_id, 11);
1623                         assert_eq!(route.hops[3].fee_msat, 0);
1624                         assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1625
1626                         assert_eq!(route.hops[4].pubkey, node7);
1627                         assert_eq!(route.hops[4].short_channel_id, 8);
1628                         assert_eq!(route.hops[4].fee_msat, 100);
1629                         assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1630                 }
1631
1632                 { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect
1633                         let our_chans = vec![channelmanager::ChannelDetails {
1634                                 channel_id: [0; 32],
1635                                 short_channel_id: Some(42),
1636                                 remote_network_id: node4.clone(),
1637                                 channel_value_satoshis: 0,
1638                                 user_id: 0,
1639                                 outbound_capacity_msat: 0,
1640                                 inbound_capacity_msat: 0,
1641                                 is_live: true,
1642                         }];
1643                         let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap();
1644                         assert_eq!(route.hops.len(), 2);
1645
1646                         assert_eq!(route.hops[0].pubkey, node4);
1647                         assert_eq!(route.hops[0].short_channel_id, 42);
1648                         assert_eq!(route.hops[0].fee_msat, 0);
1649                         assert_eq!(route.hops[0].cltv_expiry_delta, (8 << 8) | 1);
1650
1651                         assert_eq!(route.hops[1].pubkey, node7);
1652                         assert_eq!(route.hops[1].short_channel_id, 8);
1653                         assert_eq!(route.hops[1].fee_msat, 100);
1654                         assert_eq!(route.hops[1].cltv_expiry_delta, 42);
1655                 }
1656
1657                 last_hops[0].fee_base_msat = 1000;
1658
1659                 { // Revert to via 6 as the fee on 8 goes up
1660                         let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap();
1661                         assert_eq!(route.hops.len(), 4);
1662
1663                         assert_eq!(route.hops[0].pubkey, node2);
1664                         assert_eq!(route.hops[0].short_channel_id, 2);
1665                         assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
1666                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1667
1668                         assert_eq!(route.hops[1].pubkey, node3);
1669                         assert_eq!(route.hops[1].short_channel_id, 4);
1670                         assert_eq!(route.hops[1].fee_msat, 100);
1671                         assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
1672
1673                         assert_eq!(route.hops[2].pubkey, node6);
1674                         assert_eq!(route.hops[2].short_channel_id, 7);
1675                         assert_eq!(route.hops[2].fee_msat, 0);
1676                         assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
1677
1678                         assert_eq!(route.hops[3].pubkey, node7);
1679                         assert_eq!(route.hops[3].short_channel_id, 10);
1680                         assert_eq!(route.hops[3].fee_msat, 100);
1681                         assert_eq!(route.hops[3].cltv_expiry_delta, 42);
1682                 }
1683
1684                 { // ...but still use 8 for larger payments as 6 has a variable feerate
1685                         let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap();
1686                         assert_eq!(route.hops.len(), 5);
1687
1688                         assert_eq!(route.hops[0].pubkey, node2);
1689                         assert_eq!(route.hops[0].short_channel_id, 2);
1690                         assert_eq!(route.hops[0].fee_msat, 3000);
1691                         assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
1692
1693                         assert_eq!(route.hops[1].pubkey, node3);
1694                         assert_eq!(route.hops[1].short_channel_id, 4);
1695                         assert_eq!(route.hops[1].fee_msat, 0);
1696                         assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
1697
1698                         assert_eq!(route.hops[2].pubkey, node5);
1699                         assert_eq!(route.hops[2].short_channel_id, 6);
1700                         assert_eq!(route.hops[2].fee_msat, 0);
1701                         assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
1702
1703                         assert_eq!(route.hops[3].pubkey, node4);
1704                         assert_eq!(route.hops[3].short_channel_id, 11);
1705                         assert_eq!(route.hops[3].fee_msat, 1000);
1706                         assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
1707
1708                         assert_eq!(route.hops[4].pubkey, node7);
1709                         assert_eq!(route.hops[4].short_channel_id, 8);
1710                         assert_eq!(route.hops[4].fee_msat, 2000);
1711                         assert_eq!(route.hops[4].cltv_expiry_delta, 42);
1712                 }
1713
1714                 { // Test Router serialization/deserialization
1715                         let mut w = TestVecWriter(Vec::new());
1716                         let network = router.network_map.read().unwrap();
1717                         assert!(!network.channels.is_empty());
1718                         assert!(!network.nodes.is_empty());
1719                         network.write(&mut w).unwrap();
1720                         assert!(<NetworkMap>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);
1721                 }
1722         }
1723 }