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