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