Replace ChainWatchInterface in NetGraphMsgHandler
[rust-lightning] / lightning / src / routing / network_graph.rs
1 //! The top-level network map tracking logic lives here.
2
3 use bitcoin::secp256k1::key::PublicKey;
4 use bitcoin::secp256k1::Secp256k1;
5 use bitcoin::secp256k1;
6
7 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
8 use bitcoin::hashes::Hash;
9 use bitcoin::blockdata::script::Builder;
10 use bitcoin::blockdata::transaction::TxOut;
11 use bitcoin::blockdata::opcodes;
12
13 use chain;
14 use chain::Access;
15 use ln::features::{ChannelFeatures, NodeFeatures};
16 use ln::msgs::{DecodeError, ErrorAction, LightningError, RoutingMessageHandler, NetAddress, OptionalField, MAX_VALUE_MSAT};
17 use ln::msgs;
18 use util::ser::{Writeable, Readable, Writer};
19 use util::logger::Logger;
20
21 use std::{cmp, fmt};
22 use std::sync::RwLock;
23 use std::sync::atomic::{AtomicUsize, Ordering};
24 use std::collections::BTreeMap;
25 use std::collections::btree_map::Entry as BtreeEntry;
26 use std::ops::Deref;
27 use bitcoin::hashes::hex::ToHex;
28
29 /// Receives and validates network updates from peers,
30 /// stores authentic and relevant data as a network graph.
31 /// This network graph is then used for routing payments.
32 /// Provides interface to help with initial routing sync by
33 /// serving historical announcements.
34 pub struct NetGraphMsgHandler<C: Deref, L: Deref> where C::Target: chain::Access, L::Target: Logger {
35         secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
36         /// Representation of the payment channel network
37         pub network_graph: RwLock<NetworkGraph>,
38         chain_access: Option<C>,
39         full_syncs_requested: AtomicUsize,
40         logger: L,
41 }
42
43 impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: chain::Access, L::Target: Logger {
44         /// Creates a new tracker of the actual state of the network of channels and nodes,
45         /// assuming a fresh network graph.
46         /// Chain monitor is used to make sure announced channels exist on-chain,
47         /// channel data is correct, and that the announcement is signed with
48         /// channel owners' keys.
49         pub fn new(chain_access: Option<C>, logger: L) -> Self {
50                 NetGraphMsgHandler {
51                         secp_ctx: Secp256k1::verification_only(),
52                         network_graph: RwLock::new(NetworkGraph {
53                                 channels: BTreeMap::new(),
54                                 nodes: BTreeMap::new(),
55                         }),
56                         full_syncs_requested: AtomicUsize::new(0),
57                         chain_access,
58                         logger,
59                 }
60         }
61
62         /// Creates a new tracker of the actual state of the network of channels and nodes,
63         /// assuming an existing Network Graph.
64         pub fn from_net_graph(chain_access: Option<C>, logger: L, network_graph: NetworkGraph) -> Self {
65                 NetGraphMsgHandler {
66                         secp_ctx: Secp256k1::verification_only(),
67                         network_graph: RwLock::new(network_graph),
68                         full_syncs_requested: AtomicUsize::new(0),
69                         chain_access,
70                         logger,
71                 }
72         }
73 }
74
75
76 macro_rules! secp_verify_sig {
77         ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
78                 match $secp_ctx.verify($msg, $sig, $pubkey) {
79                         Ok(_) => {},
80                         Err(_) => return Err(LightningError{err: "Invalid signature from remote node".to_owned(), action: ErrorAction::IgnoreError}),
81                 }
82         };
83 }
84
85 impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for NetGraphMsgHandler<C, L> where C::Target: chain::Access, L::Target: Logger {
86         fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
87                 self.network_graph.write().unwrap().update_node_from_announcement(msg, Some(&self.secp_ctx))
88         }
89
90         fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
91                 if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 {
92                         return Err(LightningError{err: "Channel announcement node had a channel with itself".to_owned(), action: ErrorAction::IgnoreError});
93                 }
94
95                 let utxo_value = match &self.chain_access {
96                         &None => {
97                                 // Tentatively accept, potentially exposing us to DoS attacks
98                                 None
99                         },
100                         &Some(ref chain_access) => {
101                                 match chain_access.get_utxo(&msg.contents.chain_hash, msg.contents.short_channel_id) {
102                                         Ok(TxOut { value, script_pubkey }) => {
103                                                 let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
104                                                                                                                         .push_slice(&msg.contents.bitcoin_key_1.serialize())
105                                                                                                                         .push_slice(&msg.contents.bitcoin_key_2.serialize())
106                                                                                                                         .push_opcode(opcodes::all::OP_PUSHNUM_2)
107                                                                                                                         .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
108                                                 if script_pubkey != expected_script {
109                                                         return Err(LightningError{err: format!("Channel announcement key ({}) didn't match on-chain script ({})", script_pubkey.to_hex(), expected_script.to_hex()), action: ErrorAction::IgnoreError});
110                                                 }
111                                                 //TODO: Check if value is worth storing, use it to inform routing, and compare it
112                                                 //to the new HTLC max field in channel_update
113                                                 Some(value)
114                                         },
115                                         Err(chain::AccessError::UnknownChain) => {
116                                                 return Err(LightningError{err: format!("Channel announced on an unknown chain ({})", msg.contents.chain_hash.encode().to_hex()), action: ErrorAction::IgnoreError});
117                                         },
118                                         Err(chain::AccessError::UnknownTx) => {
119                                                 return Err(LightningError{err: "Channel announced without corresponding UTXO entry".to_owned(), action: ErrorAction::IgnoreError});
120                                         },
121                                         // format!("Channel announced on an unknown chain ({})", msg.contents.chain_hash.encode().to_hex())
122                                 }
123                         },
124                 };
125                 let result = self.network_graph.write().unwrap().update_channel_from_announcement(msg, utxo_value, Some(&self.secp_ctx));
126                 log_trace!(self.logger, "Added channel_announcement for {}{}", msg.contents.short_channel_id, if !msg.contents.excess_data.is_empty() { " with excess uninterpreted data!" } else { "" });
127                 result
128         }
129
130         fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
131                 match update {
132                         &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
133                                 let _ = self.network_graph.write().unwrap().update_channel(msg, Some(&self.secp_ctx));
134                         },
135                         &msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id, is_permanent } => {
136                                 self.network_graph.write().unwrap().close_channel_from_update(short_channel_id, is_permanent);
137                         },
138                         &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, is_permanent } => {
139                                 self.network_graph.write().unwrap().fail_node(node_id, is_permanent);
140                         },
141                 }
142         }
143
144         fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> {
145                 self.network_graph.write().unwrap().update_channel(msg, Some(&self.secp_ctx))
146         }
147
148         fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, Option<msgs::ChannelUpdate>, Option<msgs::ChannelUpdate>)> {
149                 let network_graph = self.network_graph.read().unwrap();
150                 let mut result = Vec::with_capacity(batch_amount as usize);
151                 let mut iter = network_graph.get_channels().range(starting_point..);
152                 while result.len() < batch_amount as usize {
153                         if let Some((_, ref chan)) = iter.next() {
154                                 if chan.announcement_message.is_some() {
155                                         let chan_announcement = chan.announcement_message.clone().unwrap();
156                                         let mut one_to_two_announcement: Option<msgs::ChannelUpdate> = None;
157                                         let mut two_to_one_announcement: Option<msgs::ChannelUpdate> = None;
158                                         if let Some(one_to_two) = chan.one_to_two.as_ref() {
159                                                 one_to_two_announcement = one_to_two.last_update_message.clone();
160                                         }
161                                         if let Some(two_to_one) = chan.two_to_one.as_ref() {
162                                                 two_to_one_announcement = two_to_one.last_update_message.clone();
163                                         }
164                                         result.push((chan_announcement, one_to_two_announcement, two_to_one_announcement));
165                                 } else {
166                                         // TODO: We may end up sending un-announced channel_updates if we are sending
167                                         // initial sync data while receiving announce/updates for this channel.
168                                 }
169                         } else {
170                                 return result;
171                         }
172                 }
173                 result
174         }
175
176         fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
177                 let network_graph = self.network_graph.read().unwrap();
178                 let mut result = Vec::with_capacity(batch_amount as usize);
179                 let mut iter = if let Some(pubkey) = starting_point {
180                                 let mut iter = network_graph.get_nodes().range((*pubkey)..);
181                                 iter.next();
182                                 iter
183                         } else {
184                                 network_graph.get_nodes().range(..)
185                         };
186                 while result.len() < batch_amount as usize {
187                         if let Some((_, ref node)) = iter.next() {
188                                 if let Some(node_info) = node.announcement_info.as_ref() {
189                                         if node_info.announcement_message.is_some() {
190                                                 result.push(node_info.announcement_message.clone().unwrap());
191                                         }
192                                 }
193                         } else {
194                                 return result;
195                         }
196                 }
197                 result
198         }
199
200         fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
201                 //TODO: Determine whether to request a full sync based on the network map.
202                 const FULL_SYNCS_TO_REQUEST: usize = 5;
203                 if self.full_syncs_requested.load(Ordering::Acquire) < FULL_SYNCS_TO_REQUEST {
204                         self.full_syncs_requested.fetch_add(1, Ordering::AcqRel);
205                         true
206                 } else {
207                         false
208                 }
209         }
210 }
211
212 #[derive(PartialEq, Debug)]
213 /// Details about one direction of a channel. Received
214 /// within a channel update.
215 pub struct DirectionalChannelInfo {
216         /// When the last update to the channel direction was issued.
217         /// Value is opaque, as set in the announcement.
218         pub last_update: u32,
219         /// Whether the channel can be currently used for payments (in this one direction).
220         pub enabled: bool,
221         /// The difference in CLTV values that you must have when routing through this channel.
222         pub cltv_expiry_delta: u16,
223         /// The minimum value, which must be relayed to the next hop via the channel
224         pub htlc_minimum_msat: u64,
225         /// The maximum value which may be relayed to the next hop via the channel.
226         pub htlc_maximum_msat: Option<u64>,
227         /// Fees charged when the channel is used for routing
228         pub fees: RoutingFees,
229         /// Most recent update for the channel received from the network
230         /// Mostly redundant with the data we store in fields explicitly.
231         /// Everything else is useful only for sending out for initial routing sync.
232         /// Not stored if contains excess data to prevent DoS.
233         pub last_update_message: Option<msgs::ChannelUpdate>,
234 }
235
236 impl fmt::Display for DirectionalChannelInfo {
237         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
238                 write!(f, "last_update {}, enabled {}, cltv_expiry_delta {}, htlc_minimum_msat {}, fees {:?}", self.last_update, self.enabled, self.cltv_expiry_delta, self.htlc_minimum_msat, self.fees)?;
239                 Ok(())
240         }
241 }
242
243 impl_writeable!(DirectionalChannelInfo, 0, {
244         last_update,
245         enabled,
246         cltv_expiry_delta,
247         htlc_minimum_msat,
248         htlc_maximum_msat,
249         fees,
250         last_update_message
251 });
252
253 #[derive(PartialEq)]
254 /// Details about a channel (both directions).
255 /// Received within a channel announcement.
256 pub struct ChannelInfo {
257         /// Protocol features of a channel communicated during its announcement
258         pub features: ChannelFeatures,
259         /// Source node of the first direction of a channel
260         pub node_one: PublicKey,
261         /// Details about the first direction of a channel
262         pub one_to_two: Option<DirectionalChannelInfo>,
263         /// Source node of the second direction of a channel
264         pub node_two: PublicKey,
265         /// Details about the second direction of a channel
266         pub two_to_one: Option<DirectionalChannelInfo>,
267         /// The channel capacity as seen on-chain, if chain lookup is available.
268         pub capacity_sats: Option<u64>,
269         /// An initial announcement of the channel
270         /// Mostly redundant with the data we store in fields explicitly.
271         /// Everything else is useful only for sending out for initial routing sync.
272         /// Not stored if contains excess data to prevent DoS.
273         pub announcement_message: Option<msgs::ChannelAnnouncement>,
274 }
275
276 impl fmt::Display for ChannelInfo {
277         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
278                 write!(f, "features: {}, node_one: {}, one_to_two: {:?}, node_two: {}, two_to_one: {:?}",
279                    log_bytes!(self.features.encode()), log_pubkey!(self.node_one), self.one_to_two, log_pubkey!(self.node_two), self.two_to_one)?;
280                 Ok(())
281         }
282 }
283
284 impl_writeable!(ChannelInfo, 0, {
285         features,
286         node_one,
287         one_to_two,
288         node_two,
289         two_to_one,
290         capacity_sats,
291         announcement_message
292 });
293
294
295 /// Fees for routing via a given channel or a node
296 #[derive(Eq, PartialEq, Copy, Clone, Debug)]
297 pub struct RoutingFees {
298         /// Flat routing fee in satoshis
299         pub base_msat: u32,
300         /// Liquidity-based routing fee in millionths of a routed amount.
301         /// In other words, 10000 is 1%.
302         pub proportional_millionths: u32,
303 }
304
305 impl Readable for RoutingFees{
306         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<RoutingFees, DecodeError> {
307                 let base_msat: u32 = Readable::read(reader)?;
308                 let proportional_millionths: u32 = Readable::read(reader)?;
309                 Ok(RoutingFees {
310                         base_msat,
311                         proportional_millionths,
312                 })
313         }
314 }
315
316 impl Writeable for RoutingFees {
317         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
318                 self.base_msat.write(writer)?;
319                 self.proportional_millionths.write(writer)?;
320                 Ok(())
321         }
322 }
323
324 #[derive(PartialEq, Debug)]
325 /// Information received in the latest node_announcement from this node.
326 pub struct NodeAnnouncementInfo {
327         /// Protocol features the node announced support for
328         pub features: NodeFeatures,
329         /// When the last known update to the node state was issued.
330         /// Value is opaque, as set in the announcement.
331         pub last_update: u32,
332         /// Color assigned to the node
333         pub rgb: [u8; 3],
334         /// Moniker assigned to the node.
335         /// May be invalid or malicious (eg control chars),
336         /// should not be exposed to the user.
337         pub alias: [u8; 32],
338         /// Internet-level addresses via which one can connect to the node
339         pub addresses: Vec<NetAddress>,
340         /// An initial announcement of the node
341         /// Mostly redundant with the data we store in fields explicitly.
342         /// Everything else is useful only for sending out for initial routing sync.
343         /// Not stored if contains excess data to prevent DoS.
344         pub announcement_message: Option<msgs::NodeAnnouncement>
345 }
346
347 impl Writeable for NodeAnnouncementInfo {
348         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
349                 self.features.write(writer)?;
350                 self.last_update.write(writer)?;
351                 self.rgb.write(writer)?;
352                 self.alias.write(writer)?;
353                 (self.addresses.len() as u64).write(writer)?;
354                 for ref addr in &self.addresses {
355                         addr.write(writer)?;
356                 }
357                 self.announcement_message.write(writer)?;
358                 Ok(())
359         }
360 }
361
362 impl Readable for NodeAnnouncementInfo {
363         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeAnnouncementInfo, DecodeError> {
364                 let features = Readable::read(reader)?;
365                 let last_update = Readable::read(reader)?;
366                 let rgb = Readable::read(reader)?;
367                 let alias = Readable::read(reader)?;
368                 let addresses_count: u64 = Readable::read(reader)?;
369                 let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
370                 for _ in 0..addresses_count {
371                         match Readable::read(reader) {
372                                 Ok(Ok(addr)) => { addresses.push(addr); },
373                                 Ok(Err(_)) => return Err(DecodeError::InvalidValue),
374                                 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
375                                 _ => unreachable!(),
376                         }
377                 }
378                 let announcement_message = Readable::read(reader)?;
379                 Ok(NodeAnnouncementInfo {
380                         features,
381                         last_update,
382                         rgb,
383                         alias,
384                         addresses,
385                         announcement_message
386                 })
387         }
388 }
389
390 #[derive(PartialEq)]
391 /// Details about a node in the network, known from the network announcement.
392 pub struct NodeInfo {
393         /// All valid channels a node has announced
394         pub channels: Vec<u64>,
395         /// Lowest fees enabling routing via any of the enabled, known channels to a node.
396         /// The two fields (flat and proportional fee) are independent,
397         /// meaning they don't have to refer to the same channel.
398         pub lowest_inbound_channel_fees: Option<RoutingFees>,
399         /// More information about a node from node_announcement.
400         /// Optional because we store a Node entry after learning about it from
401         /// a channel announcement, but before receiving a node announcement.
402         pub announcement_info: Option<NodeAnnouncementInfo>
403 }
404
405 impl fmt::Display for NodeInfo {
406         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
407                 write!(f, "lowest_inbound_channel_fees: {:?}, channels: {:?}, announcement_info: {:?}",
408                    self.lowest_inbound_channel_fees, &self.channels[..], self.announcement_info)?;
409                 Ok(())
410         }
411 }
412
413 impl Writeable for NodeInfo {
414         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
415                 (self.channels.len() as u64).write(writer)?;
416                 for ref chan in self.channels.iter() {
417                         chan.write(writer)?;
418                 }
419                 self.lowest_inbound_channel_fees.write(writer)?;
420                 self.announcement_info.write(writer)?;
421                 Ok(())
422         }
423 }
424
425 const MAX_ALLOC_SIZE: u64 = 64*1024;
426
427 impl Readable for NodeInfo {
428         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeInfo, DecodeError> {
429                 let channels_count: u64 = Readable::read(reader)?;
430                 let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
431                 for _ in 0..channels_count {
432                         channels.push(Readable::read(reader)?);
433                 }
434                 let lowest_inbound_channel_fees = Readable::read(reader)?;
435                 let announcement_info = Readable::read(reader)?;
436                 Ok(NodeInfo {
437                         channels,
438                         lowest_inbound_channel_fees,
439                         announcement_info,
440                 })
441         }
442 }
443
444 /// Represents the network as nodes and channels between them
445 #[derive(PartialEq)]
446 pub struct NetworkGraph {
447         channels: BTreeMap<u64, ChannelInfo>,
448         nodes: BTreeMap<PublicKey, NodeInfo>,
449 }
450
451 impl Writeable for NetworkGraph {
452         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
453                 (self.channels.len() as u64).write(writer)?;
454                 for (ref chan_id, ref chan_info) in self.channels.iter() {
455                         (*chan_id).write(writer)?;
456                         chan_info.write(writer)?;
457                 }
458                 (self.nodes.len() as u64).write(writer)?;
459                 for (ref node_id, ref node_info) in self.nodes.iter() {
460                         node_id.write(writer)?;
461                         node_info.write(writer)?;
462                 }
463                 Ok(())
464         }
465 }
466
467 impl Readable for NetworkGraph {
468         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NetworkGraph, DecodeError> {
469                 let channels_count: u64 = Readable::read(reader)?;
470                 let mut channels = BTreeMap::new();
471                 for _ in 0..channels_count {
472                         let chan_id: u64 = Readable::read(reader)?;
473                         let chan_info = Readable::read(reader)?;
474                         channels.insert(chan_id, chan_info);
475                 }
476                 let nodes_count: u64 = Readable::read(reader)?;
477                 let mut nodes = BTreeMap::new();
478                 for _ in 0..nodes_count {
479                         let node_id = Readable::read(reader)?;
480                         let node_info = Readable::read(reader)?;
481                         nodes.insert(node_id, node_info);
482                 }
483                 Ok(NetworkGraph {
484                         channels,
485                         nodes,
486                 })
487         }
488 }
489
490 impl fmt::Display for NetworkGraph {
491         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
492                 write!(f, "Network map\n[Channels]\n")?;
493                 for (key, val) in self.channels.iter() {
494                         write!(f, " {}: {}\n", key, val)?;
495                 }
496                 write!(f, "[Nodes]\n")?;
497                 for (key, val) in self.nodes.iter() {
498                         write!(f, " {}: {}\n", log_pubkey!(key), val)?;
499                 }
500                 Ok(())
501         }
502 }
503
504 impl NetworkGraph {
505         /// Returns all known valid channels' short ids along with announced channel info.
506         pub fn get_channels<'a>(&'a self) -> &'a BTreeMap<u64, ChannelInfo> { &self.channels }
507         /// Returns all known nodes' public keys along with announced node info.
508         pub fn get_nodes<'a>(&'a self) -> &'a BTreeMap<PublicKey, NodeInfo> { &self.nodes }
509
510         /// Get network addresses by node id.
511         /// Returns None if the requested node is completely unknown,
512         /// or if node announcement for the node was never received.
513         pub fn get_addresses<'a>(&'a self, pubkey: &PublicKey) -> Option<&'a Vec<NetAddress>> {
514                 if let Some(node) = self.nodes.get(pubkey) {
515                         if let Some(node_info) = node.announcement_info.as_ref() {
516                                 return Some(&node_info.addresses)
517                         }
518                 }
519                 None
520         }
521
522         /// Creates a new, empty, network graph.
523         pub fn new() -> NetworkGraph {
524                 Self {
525                         channels: BTreeMap::new(),
526                         nodes: BTreeMap::new(),
527                 }
528         }
529
530         /// For an already known node (from channel announcements), update its stored properties from a given node announcement
531         /// Announcement signatures are checked here only if Secp256k1 object is provided.
532         fn update_node_from_announcement(&mut self, msg: &msgs::NodeAnnouncement, secp_ctx: Option<&Secp256k1<secp256k1::VerifyOnly>>) -> Result<bool, LightningError> {
533                 if let Some(sig_verifier) = secp_ctx {
534                         let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
535                         secp_verify_sig!(sig_verifier, &msg_hash, &msg.signature, &msg.contents.node_id);
536                 }
537
538                 match self.nodes.get_mut(&msg.contents.node_id) {
539                         None => Err(LightningError{err: "No existing channels for node_announcement".to_owned(), action: ErrorAction::IgnoreError}),
540                         Some(node) => {
541                                 if let Some(node_info) = node.announcement_info.as_ref() {
542                                         if node_info.last_update  >= msg.contents.timestamp {
543                                                 return Err(LightningError{err: "Update older than last processed update".to_owned(), action: ErrorAction::IgnoreError});
544                                         }
545                                 }
546
547                                 let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty();
548                                 node.announcement_info = Some(NodeAnnouncementInfo {
549                                         features: msg.contents.features.clone(),
550                                         last_update: msg.contents.timestamp,
551                                         rgb: msg.contents.rgb,
552                                         alias: msg.contents.alias,
553                                         addresses: msg.contents.addresses.clone(),
554                                         announcement_message: if should_relay { Some(msg.clone()) } else { None },
555                                 });
556
557                                 Ok(should_relay)
558                         }
559                 }
560         }
561
562         /// For a new or already known (from previous announcement) channel, store or update channel info.
563         /// Also store nodes (if not stored yet) the channel is between, and make node aware of this channel.
564         /// Checking utxo on-chain is useful if we receive an update for already known channel id,
565         /// which is probably result of a reorg. In that case, we update channel info only if the
566         /// utxo was checked, otherwise stick to the existing update, to prevent DoS risks.
567         /// Announcement signatures are checked here only if Secp256k1 object is provided.
568         fn update_channel_from_announcement(&mut self, msg: &msgs::ChannelAnnouncement, utxo_value: Option<u64>, secp_ctx: Option<&Secp256k1<secp256k1::VerifyOnly>>) -> Result<bool, LightningError> {
569                 if let Some(sig_verifier) = secp_ctx {
570                         let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
571                         secp_verify_sig!(sig_verifier, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
572                         secp_verify_sig!(sig_verifier, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
573                         secp_verify_sig!(sig_verifier, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
574                         secp_verify_sig!(sig_verifier, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
575                 }
576
577                 let should_relay = msg.contents.excess_data.is_empty();
578
579                 let chan_info = ChannelInfo {
580                                 features: msg.contents.features.clone(),
581                                 node_one: msg.contents.node_id_1.clone(),
582                                 one_to_two: None,
583                                 node_two: msg.contents.node_id_2.clone(),
584                                 two_to_one: None,
585                                 capacity_sats: utxo_value,
586                                 announcement_message: if should_relay { Some(msg.clone()) } else { None },
587                         };
588
589                 match self.channels.entry(msg.contents.short_channel_id) {
590                         BtreeEntry::Occupied(mut entry) => {
591                                 //TODO: because asking the blockchain if short_channel_id is valid is only optional
592                                 //in the blockchain API, we need to handle it smartly here, though it's unclear
593                                 //exactly how...
594                                 if utxo_value.is_some() {
595                                         // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
596                                         // only sometimes returns results. In any case remove the previous entry. Note
597                                         // that the spec expects us to "blacklist" the node_ids involved, but we can't
598                                         // do that because
599                                         // a) we don't *require* a UTXO provider that always returns results.
600                                         // b) we don't track UTXOs of channels we know about and remove them if they
601                                         //    get reorg'd out.
602                                         // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
603                                         Self::remove_channel_in_nodes(&mut self.nodes, &entry.get(), msg.contents.short_channel_id);
604                                         *entry.get_mut() = chan_info;
605                                 } else {
606                                         return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreError})
607                                 }
608                         },
609                         BtreeEntry::Vacant(entry) => {
610                                 entry.insert(chan_info);
611                         }
612                 };
613
614                 macro_rules! add_channel_to_node {
615                         ( $node_id: expr ) => {
616                                 match self.nodes.entry($node_id) {
617                                         BtreeEntry::Occupied(node_entry) => {
618                                                 node_entry.into_mut().channels.push(msg.contents.short_channel_id);
619                                         },
620                                         BtreeEntry::Vacant(node_entry) => {
621                                                 node_entry.insert(NodeInfo {
622                                                         channels: vec!(msg.contents.short_channel_id),
623                                                         lowest_inbound_channel_fees: None,
624                                                         announcement_info: None,
625                                                 });
626                                         }
627                                 }
628                         };
629                 }
630
631                 add_channel_to_node!(msg.contents.node_id_1);
632                 add_channel_to_node!(msg.contents.node_id_2);
633
634                 Ok(should_relay)
635         }
636
637         /// Close a channel if a corresponding HTLC fail was sent.
638         /// If permanent, removes a channel from the local storage.
639         /// May cause the removal of nodes too, if this was their last channel.
640         /// If not permanent, makes channels unavailable for routing.
641         pub fn close_channel_from_update(&mut self, short_channel_id: u64, is_permanent: bool) {
642                 if is_permanent {
643                         if let Some(chan) = self.channels.remove(&short_channel_id) {
644                                 Self::remove_channel_in_nodes(&mut self.nodes, &chan, short_channel_id);
645                         }
646                 } else {
647                         if let Some(chan) = self.channels.get_mut(&short_channel_id) {
648                                 if let Some(one_to_two) = chan.one_to_two.as_mut() {
649                                         one_to_two.enabled = false;
650                                 }
651                                 if let Some(two_to_one) = chan.two_to_one.as_mut() {
652                                         two_to_one.enabled = false;
653                                 }
654                         }
655                 }
656         }
657
658         fn fail_node(&mut self, _node_id: &PublicKey, is_permanent: bool) {
659                 if is_permanent {
660                         // TODO: Wholly remove the node
661                 } else {
662                         // TODO: downgrade the node
663                 }
664         }
665
666         /// For an already known (from announcement) channel, update info about one of the directions of a channel.
667         /// Announcement signatures are checked here only if Secp256k1 object is provided.
668         fn update_channel(&mut self, msg: &msgs::ChannelUpdate, secp_ctx: Option<&Secp256k1<secp256k1::VerifyOnly>>) -> Result<bool, LightningError> {
669                 let dest_node_id;
670                 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
671                 let chan_was_enabled;
672
673                 match self.channels.get_mut(&msg.contents.short_channel_id) {
674                         None => return Err(LightningError{err: "Couldn't find channel for update".to_owned(), action: ErrorAction::IgnoreError}),
675                         Some(channel) => {
676                                 if let OptionalField::Present(htlc_maximum_msat) = msg.contents.htlc_maximum_msat {
677                                         if htlc_maximum_msat > MAX_VALUE_MSAT {
678                                                 return Err(LightningError{err: "htlc_maximum_msat is larger than maximum possible msats".to_owned(), action: ErrorAction::IgnoreError});
679                                         }
680
681                                         if let Some(capacity_sats) = channel.capacity_sats {
682                                                 // It's possible channel capacity is available now, although it wasn't available at announcement (so the field is None).
683                                                 // Don't query UTXO set here to reduce DoS risks.
684                                                 if htlc_maximum_msat > capacity_sats * 1000 {
685                                                         return Err(LightningError{err: "htlc_maximum_msat is larger than channel capacity".to_owned(), action: ErrorAction::IgnoreError});
686                                                 }
687                                         }
688                                 }
689                                 macro_rules! maybe_update_channel_info {
690                                         ( $target: expr, $src_node: expr) => {
691                                                 if let Some(existing_chan_info) = $target.as_ref() {
692                                                         if existing_chan_info.last_update >= msg.contents.timestamp {
693                                                                 return Err(LightningError{err: "Update older than last processed update".to_owned(), action: ErrorAction::IgnoreError});
694                                                         }
695                                                         chan_was_enabled = existing_chan_info.enabled;
696                                                 } else {
697                                                         chan_was_enabled = false;
698                                                 }
699
700                                                 let last_update_message = if msg.contents.excess_data.is_empty() {
701                                                         Some(msg.clone())
702                                                 } else {
703                                                         None
704                                                 };
705
706                                                 let updated_channel_dir_info = DirectionalChannelInfo {
707                                                         enabled: chan_enabled,
708                                                         last_update: msg.contents.timestamp,
709                                                         cltv_expiry_delta: msg.contents.cltv_expiry_delta,
710                                                         htlc_minimum_msat: msg.contents.htlc_minimum_msat,
711                                                         htlc_maximum_msat: if let OptionalField::Present(max_value) = msg.contents.htlc_maximum_msat { Some(max_value) } else { None },
712                                                         fees: RoutingFees {
713                                                                 base_msat: msg.contents.fee_base_msat,
714                                                                 proportional_millionths: msg.contents.fee_proportional_millionths,
715                                                         },
716                                                         last_update_message
717                                                 };
718                                                 $target = Some(updated_channel_dir_info);
719                                         }
720                                 }
721
722                                 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
723                                 if msg.contents.flags & 1 == 1 {
724                                         dest_node_id = channel.node_one.clone();
725                                         if let Some(sig_verifier) = secp_ctx {
726                                                 secp_verify_sig!(sig_verifier, &msg_hash, &msg.signature, &channel.node_two);
727                                         }
728                                         maybe_update_channel_info!(channel.two_to_one, channel.node_two);
729                                 } else {
730                                         dest_node_id = channel.node_two.clone();
731                                         if let Some(sig_verifier) = secp_ctx {
732                                                 secp_verify_sig!(sig_verifier, &msg_hash, &msg.signature, &channel.node_one);
733                                         }
734                                         maybe_update_channel_info!(channel.one_to_two, channel.node_one);
735                                 }
736                         }
737                 }
738
739                 if chan_enabled {
740                         let node = self.nodes.get_mut(&dest_node_id).unwrap();
741                         let mut base_msat = msg.contents.fee_base_msat;
742                         let mut proportional_millionths = msg.contents.fee_proportional_millionths;
743                         if let Some(fees) = node.lowest_inbound_channel_fees {
744                                 base_msat = cmp::min(base_msat, fees.base_msat);
745                                 proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
746                         }
747                         node.lowest_inbound_channel_fees = Some(RoutingFees {
748                                 base_msat,
749                                 proportional_millionths
750                         });
751                 } else if chan_was_enabled {
752                         let node = self.nodes.get_mut(&dest_node_id).unwrap();
753                         let mut lowest_inbound_channel_fees = None;
754
755                         for chan_id in node.channels.iter() {
756                                 let chan = self.channels.get(chan_id).unwrap();
757                                 let chan_info_opt;
758                                 if chan.node_one == dest_node_id {
759                                         chan_info_opt = chan.two_to_one.as_ref();
760                                 } else {
761                                         chan_info_opt = chan.one_to_two.as_ref();
762                                 }
763                                 if let Some(chan_info) = chan_info_opt {
764                                         if chan_info.enabled {
765                                                 let fees = lowest_inbound_channel_fees.get_or_insert(RoutingFees {
766                                                         base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
767                                                 fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
768                                                 fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
769                                         }
770                                 }
771                         }
772
773                         node.lowest_inbound_channel_fees = lowest_inbound_channel_fees;
774                 }
775
776                 Ok(msg.contents.excess_data.is_empty())
777         }
778
779         fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
780                 macro_rules! remove_from_node {
781                         ($node_id: expr) => {
782                                 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
783                                         entry.get_mut().channels.retain(|chan_id| {
784                                                 short_channel_id != *chan_id
785                                         });
786                                         if entry.get().channels.is_empty() {
787                                                 entry.remove_entry();
788                                         }
789                                 } else {
790                                         panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
791                                 }
792                         }
793                 }
794
795                 remove_from_node!(chan.node_one);
796                 remove_from_node!(chan.node_two);
797         }
798 }
799
800 #[cfg(test)]
801 mod tests {
802         use chain;
803         use ln::features::{ChannelFeatures, NodeFeatures};
804         use routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
805         use ln::msgs::{OptionalField, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
806                 UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate,
807                 MAX_VALUE_MSAT};
808         use util::test_utils;
809         use util::logger::Logger;
810         use util::ser::{Readable, Writeable};
811
812         use bitcoin::hashes::sha256d::Hash as Sha256dHash;
813         use bitcoin::hashes::Hash;
814         use bitcoin::network::constants::Network;
815         use bitcoin::blockdata::constants::genesis_block;
816         use bitcoin::blockdata::script::Builder;
817         use bitcoin::blockdata::transaction::TxOut;
818         use bitcoin::blockdata::opcodes;
819         use bitcoin::util::hash::BitcoinHash;
820
821         use hex;
822
823         use bitcoin::secp256k1::key::{PublicKey, SecretKey};
824         use bitcoin::secp256k1::{All, Secp256k1};
825
826         use std::sync::Arc;
827
828         fn create_net_graph_msg_handler() -> (Secp256k1<All>, NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>) {
829                 let secp_ctx = Secp256k1::new();
830                 let logger = Arc::new(test_utils::TestLogger::new());
831                 let net_graph_msg_handler = NetGraphMsgHandler::new(None, Arc::clone(&logger));
832                 (secp_ctx, net_graph_msg_handler)
833         }
834
835         #[test]
836         fn request_full_sync_finite_times() {
837                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
838                 let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
839
840                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
841                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
842                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
843                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
844                 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
845                 assert!(!net_graph_msg_handler.should_request_full_sync(&node_id));
846         }
847
848         #[test]
849         fn handling_node_announcements() {
850                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
851
852                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
853                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
854                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
855                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
856                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
857                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
858                 let zero_hash = Sha256dHash::hash(&[0; 32]);
859                 let first_announcement_time = 500;
860
861                 let mut unsigned_announcement = UnsignedNodeAnnouncement {
862                         features: NodeFeatures::known(),
863                         timestamp: first_announcement_time,
864                         node_id: node_id_1,
865                         rgb: [0; 3],
866                         alias: [0; 32],
867                         addresses: Vec::new(),
868                         excess_address_data: Vec::new(),
869                         excess_data: Vec::new(),
870                 };
871                 let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
872                 let valid_announcement = NodeAnnouncement {
873                         signature: secp_ctx.sign(&msghash, node_1_privkey),
874                         contents: unsigned_announcement.clone()
875                 };
876
877                 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
878                         Ok(_) => panic!(),
879                         Err(e) => assert_eq!("No existing channels for node_announcement", e.err)
880                 };
881
882                 {
883                         // Announce a channel to add a corresponding node.
884                         let unsigned_announcement = UnsignedChannelAnnouncement {
885                                 features: ChannelFeatures::known(),
886                                 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
887                                 short_channel_id: 0,
888                                 node_id_1,
889                                 node_id_2,
890                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
891                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
892                                 excess_data: Vec::new(),
893                         };
894
895                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
896                         let valid_announcement = ChannelAnnouncement {
897                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
898                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
899                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
900                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
901                                 contents: unsigned_announcement.clone(),
902                         };
903                         match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
904                                 Ok(res) => assert!(res),
905                                 _ => panic!()
906                         };
907                 }
908
909                 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
910                         Ok(res) => assert!(res),
911                         Err(_) => panic!()
912                 };
913
914                 let fake_msghash = hash_to_message!(&zero_hash);
915                 match net_graph_msg_handler.handle_node_announcement(
916                         &NodeAnnouncement {
917                                 signature: secp_ctx.sign(&fake_msghash, node_1_privkey),
918                                 contents: unsigned_announcement.clone()
919                 }) {
920                         Ok(_) => panic!(),
921                         Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
922                 };
923
924                 unsigned_announcement.timestamp += 1000;
925                 unsigned_announcement.excess_data.push(1);
926                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
927                 let announcement_with_data = NodeAnnouncement {
928                         signature: secp_ctx.sign(&msghash, node_1_privkey),
929                         contents: unsigned_announcement.clone()
930                 };
931                 // Return false because contains excess data.
932                 match net_graph_msg_handler.handle_node_announcement(&announcement_with_data) {
933                         Ok(res) => assert!(!res),
934                         Err(_) => panic!()
935                 };
936                 unsigned_announcement.excess_data = Vec::new();
937
938                 // Even though previous announcement was not relayed further, we still accepted it,
939                 // so we now won't accept announcements before the previous one.
940                 unsigned_announcement.timestamp -= 10;
941                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
942                 let outdated_announcement = NodeAnnouncement {
943                         signature: secp_ctx.sign(&msghash, node_1_privkey),
944                         contents: unsigned_announcement.clone()
945                 };
946                 match net_graph_msg_handler.handle_node_announcement(&outdated_announcement) {
947                         Ok(_) => panic!(),
948                         Err(e) => assert_eq!(e.err, "Update older than last processed update")
949                 };
950         }
951
952         #[test]
953         fn handling_channel_announcements() {
954                 let secp_ctx = Secp256k1::new();
955                 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
956
957                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
958                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
959                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
960                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
961                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
962                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
963
964                 let good_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
965                    .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_1_btckey).serialize())
966                    .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_2_btckey).serialize())
967                    .push_opcode(opcodes::all::OP_PUSHNUM_2)
968                    .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
969
970
971                 let mut unsigned_announcement = UnsignedChannelAnnouncement {
972                         features: ChannelFeatures::known(),
973                         chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
974                         short_channel_id: 0,
975                         node_id_1,
976                         node_id_2,
977                         bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
978                         bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
979                         excess_data: Vec::new(),
980                 };
981
982                 let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
983                 let valid_announcement = ChannelAnnouncement {
984                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
985                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
986                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
987                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
988                         contents: unsigned_announcement.clone(),
989                 };
990
991                 // Test if the UTXO lookups were not supported
992                 let mut net_graph_msg_handler = NetGraphMsgHandler::new(None, Arc::clone(&logger));
993                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
994                         Ok(res) => assert!(res),
995                         _ => panic!()
996                 };
997
998                 {
999                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1000                         match network.get_channels().get(&unsigned_announcement.short_channel_id) {
1001                                 None => panic!(),
1002                                 Some(_) => ()
1003                         }
1004                 }
1005
1006                 // If we receive announcement for the same channel (with UTXO lookups disabled),
1007                 // drop new one on the floor, since we can't see any changes.
1008                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1009                         Ok(_) => panic!(),
1010                         Err(e) => assert_eq!(e.err, "Already have knowledge of channel")
1011                 };
1012
1013                 // Test if an associated transaction were not on-chain (or not confirmed).
1014                 let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
1015                 *chain_source.utxo_ret.lock().unwrap() = Err(chain::AccessError::UnknownTx);
1016                 net_graph_msg_handler = NetGraphMsgHandler::new(Some(chain_source.clone()), Arc::clone(&logger));
1017                 unsigned_announcement.short_channel_id += 1;
1018
1019                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1020                 let valid_announcement = ChannelAnnouncement {
1021                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1022                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1023                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1024                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1025                         contents: unsigned_announcement.clone(),
1026                 };
1027
1028                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1029                         Ok(_) => panic!(),
1030                         Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry")
1031                 };
1032
1033                 // Now test if the transaction is found in the UTXO set and the script is correct.
1034                 unsigned_announcement.short_channel_id += 1;
1035                 *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script.clone() });
1036
1037                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1038                 let valid_announcement = ChannelAnnouncement {
1039                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1040                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1041                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1042                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1043                         contents: unsigned_announcement.clone(),
1044                 };
1045                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1046                         Ok(res) => assert!(res),
1047                         _ => panic!()
1048                 };
1049
1050                 {
1051                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1052                         match network.get_channels().get(&unsigned_announcement.short_channel_id) {
1053                                 None => panic!(),
1054                                 Some(_) => ()
1055                         }
1056                 }
1057
1058                 // If we receive announcement for the same channel (but TX is not confirmed),
1059                 // drop new one on the floor, since we can't see any changes.
1060                 *chain_source.utxo_ret.lock().unwrap() = Err(chain::AccessError::UnknownTx);
1061                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1062                         Ok(_) => panic!(),
1063                         Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry")
1064                 };
1065
1066                 // But if it is confirmed, replace the channel
1067                 *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script });
1068                 unsigned_announcement.features = ChannelFeatures::empty();
1069                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1070                 let valid_announcement = ChannelAnnouncement {
1071                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1072                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1073                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1074                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1075                         contents: unsigned_announcement.clone(),
1076                 };
1077                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1078                         Ok(res) => assert!(res),
1079                         _ => panic!()
1080                 };
1081                 {
1082                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1083                         match network.get_channels().get(&unsigned_announcement.short_channel_id) {
1084                                 Some(channel_entry) => {
1085                                         assert_eq!(channel_entry.features, ChannelFeatures::empty());
1086                                 },
1087                                 _ => panic!()
1088                         }
1089                 }
1090
1091                 // Don't relay valid channels with excess data
1092                 unsigned_announcement.short_channel_id += 1;
1093                 unsigned_announcement.excess_data.push(1);
1094                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1095                 let valid_announcement = ChannelAnnouncement {
1096                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1097                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1098                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1099                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1100                         contents: unsigned_announcement.clone(),
1101                 };
1102                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1103                         Ok(res) => assert!(!res),
1104                         _ => panic!()
1105                 };
1106
1107                 unsigned_announcement.excess_data = Vec::new();
1108                 let invalid_sig_announcement = ChannelAnnouncement {
1109                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1110                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1111                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1112                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_1_btckey),
1113                         contents: unsigned_announcement.clone(),
1114                 };
1115                 match net_graph_msg_handler.handle_channel_announcement(&invalid_sig_announcement) {
1116                         Ok(_) => panic!(),
1117                         Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1118                 };
1119
1120                 unsigned_announcement.node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1121                 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1122                 let channel_to_itself_announcement = ChannelAnnouncement {
1123                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1124                         node_signature_2: secp_ctx.sign(&msghash, node_1_privkey),
1125                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1126                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1127                         contents: unsigned_announcement.clone(),
1128                 };
1129                 match net_graph_msg_handler.handle_channel_announcement(&channel_to_itself_announcement) {
1130                         Ok(_) => panic!(),
1131                         Err(e) => assert_eq!(e.err, "Channel announcement node had a channel with itself")
1132                 };
1133         }
1134
1135         #[test]
1136         fn handling_channel_update() {
1137                 let secp_ctx = Secp256k1::new();
1138                 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1139                 let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
1140                 let net_graph_msg_handler = NetGraphMsgHandler::new(Some(chain_source.clone()), Arc::clone(&logger));
1141
1142                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1143                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1144                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1145                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1146                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1147                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1148
1149                 let zero_hash = Sha256dHash::hash(&[0; 32]);
1150                 let short_channel_id = 0;
1151                 let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
1152                 let amount_sats = 1000_000;
1153
1154                 {
1155                         // Announce a channel we will update
1156                         let good_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
1157                            .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_1_btckey).serialize())
1158                            .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_2_btckey).serialize())
1159                            .push_opcode(opcodes::all::OP_PUSHNUM_2)
1160                            .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
1161                         *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() });
1162                         let unsigned_announcement = UnsignedChannelAnnouncement {
1163                                 features: ChannelFeatures::empty(),
1164                                 chain_hash,
1165                                 short_channel_id,
1166                                 node_id_1,
1167                                 node_id_2,
1168                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1169                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1170                                 excess_data: Vec::new(),
1171                         };
1172
1173                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1174                         let valid_channel_announcement = ChannelAnnouncement {
1175                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1176                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1177                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1178                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1179                                 contents: unsigned_announcement.clone(),
1180                         };
1181                         match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1182                                 Ok(_) => (),
1183                                 Err(_) => panic!()
1184                         };
1185
1186                 }
1187
1188                 let mut unsigned_channel_update = UnsignedChannelUpdate {
1189                         chain_hash,
1190                         short_channel_id,
1191                         timestamp: 100,
1192                         flags: 0,
1193                         cltv_expiry_delta: 144,
1194                         htlc_minimum_msat: 1000000,
1195                         htlc_maximum_msat: OptionalField::Absent,
1196                         fee_base_msat: 10000,
1197                         fee_proportional_millionths: 20,
1198                         excess_data: Vec::new()
1199                 };
1200                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1201                 let valid_channel_update = ChannelUpdate {
1202                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1203                         contents: unsigned_channel_update.clone()
1204                 };
1205
1206                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1207                         Ok(res) => assert!(res),
1208                         _ => panic!()
1209                 };
1210
1211                 {
1212                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1213                         match network.get_channels().get(&short_channel_id) {
1214                                 None => panic!(),
1215                                 Some(channel_info) => {
1216                                         assert_eq!(channel_info.one_to_two.as_ref().unwrap().cltv_expiry_delta, 144);
1217                                         assert!(channel_info.two_to_one.is_none());
1218                                 }
1219                         }
1220                 }
1221
1222                 unsigned_channel_update.timestamp += 100;
1223                 unsigned_channel_update.excess_data.push(1);
1224                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1225                 let valid_channel_update = ChannelUpdate {
1226                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1227                         contents: unsigned_channel_update.clone()
1228                 };
1229                 // Return false because contains excess data
1230                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1231                         Ok(res) => assert!(!res),
1232                         _ => panic!()
1233                 };
1234                 unsigned_channel_update.timestamp += 10;
1235
1236                 unsigned_channel_update.short_channel_id += 1;
1237                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1238                 let valid_channel_update = ChannelUpdate {
1239                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1240                         contents: unsigned_channel_update.clone()
1241                 };
1242
1243                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1244                         Ok(_) => panic!(),
1245                         Err(e) => assert_eq!(e.err, "Couldn't find channel for update")
1246                 };
1247                 unsigned_channel_update.short_channel_id = short_channel_id;
1248
1249                 unsigned_channel_update.htlc_maximum_msat = OptionalField::Present(MAX_VALUE_MSAT + 1);
1250                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1251                 let valid_channel_update = ChannelUpdate {
1252                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1253                         contents: unsigned_channel_update.clone()
1254                 };
1255
1256                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1257                         Ok(_) => panic!(),
1258                         Err(e) => assert_eq!(e.err, "htlc_maximum_msat is larger than maximum possible msats")
1259                 };
1260                 unsigned_channel_update.htlc_maximum_msat = OptionalField::Absent;
1261
1262                 unsigned_channel_update.htlc_maximum_msat = OptionalField::Present(amount_sats * 1000 + 1);
1263                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1264                 let valid_channel_update = ChannelUpdate {
1265                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1266                         contents: unsigned_channel_update.clone()
1267                 };
1268
1269                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1270                         Ok(_) => panic!(),
1271                         Err(e) => assert_eq!(e.err, "htlc_maximum_msat is larger than channel capacity")
1272                 };
1273                 unsigned_channel_update.htlc_maximum_msat = OptionalField::Absent;
1274
1275                 // Even though previous update was not relayed further, we still accepted it,
1276                 // so we now won't accept update before the previous one.
1277                 unsigned_channel_update.timestamp -= 10;
1278                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1279                 let valid_channel_update = ChannelUpdate {
1280                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1281                         contents: unsigned_channel_update.clone()
1282                 };
1283
1284                 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1285                         Ok(_) => panic!(),
1286                         Err(e) => assert_eq!(e.err, "Update older than last processed update")
1287                 };
1288                 unsigned_channel_update.timestamp += 500;
1289
1290                 let fake_msghash = hash_to_message!(&zero_hash);
1291                 let invalid_sig_channel_update = ChannelUpdate {
1292                         signature: secp_ctx.sign(&fake_msghash, node_1_privkey),
1293                         contents: unsigned_channel_update.clone()
1294                 };
1295
1296                 match net_graph_msg_handler.handle_channel_update(&invalid_sig_channel_update) {
1297                         Ok(_) => panic!(),
1298                         Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1299                 };
1300
1301         }
1302
1303         #[test]
1304         fn handling_htlc_fail_channel_update() {
1305                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1306                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1307                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1308                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1309                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1310                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1311                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1312
1313                 let short_channel_id = 0;
1314                 let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
1315
1316                 {
1317                         // There is no nodes in the table at the beginning.
1318                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1319                         assert_eq!(network.get_nodes().len(), 0);
1320                 }
1321
1322                 {
1323                         // Announce a channel we will update
1324                         let unsigned_announcement = UnsignedChannelAnnouncement {
1325                                 features: ChannelFeatures::empty(),
1326                                 chain_hash,
1327                                 short_channel_id,
1328                                 node_id_1,
1329                                 node_id_2,
1330                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1331                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1332                                 excess_data: Vec::new(),
1333                         };
1334
1335                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1336                         let valid_channel_announcement = ChannelAnnouncement {
1337                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1338                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1339                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1340                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1341                                 contents: unsigned_announcement.clone(),
1342                         };
1343                         match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1344                                 Ok(_) => (),
1345                                 Err(_) => panic!()
1346                         };
1347
1348                         let unsigned_channel_update = UnsignedChannelUpdate {
1349                                 chain_hash,
1350                                 short_channel_id,
1351                                 timestamp: 100,
1352                                 flags: 0,
1353                                 cltv_expiry_delta: 144,
1354                                 htlc_minimum_msat: 1000000,
1355                                 htlc_maximum_msat: OptionalField::Absent,
1356                                 fee_base_msat: 10000,
1357                                 fee_proportional_millionths: 20,
1358                                 excess_data: Vec::new()
1359                         };
1360                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1361                         let valid_channel_update = ChannelUpdate {
1362                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
1363                                 contents: unsigned_channel_update.clone()
1364                         };
1365
1366                         match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1367                                 Ok(res) => assert!(res),
1368                                 _ => panic!()
1369                         };
1370                 }
1371
1372                 // Non-permanent closing just disables a channel
1373                 {
1374                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1375                         match network.get_channels().get(&short_channel_id) {
1376                                 None => panic!(),
1377                                 Some(channel_info) => {
1378                                         assert!(channel_info.one_to_two.is_some());
1379                                 }
1380                         }
1381                 }
1382
1383                 let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
1384                         short_channel_id,
1385                         is_permanent: false
1386                 };
1387
1388                 net_graph_msg_handler.handle_htlc_fail_channel_update(&channel_close_msg);
1389
1390                 // Non-permanent closing just disables a channel
1391                 {
1392                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1393                         match network.get_channels().get(&short_channel_id) {
1394                                 None => panic!(),
1395                                 Some(channel_info) => {
1396                                         assert!(!channel_info.one_to_two.as_ref().unwrap().enabled);
1397                                 }
1398                         }
1399                 }
1400
1401                 let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
1402                         short_channel_id,
1403                         is_permanent: true
1404                 };
1405
1406                 net_graph_msg_handler.handle_htlc_fail_channel_update(&channel_close_msg);
1407
1408                 // Permanent closing deletes a channel
1409                 {
1410                         let network = net_graph_msg_handler.network_graph.read().unwrap();
1411                         assert_eq!(network.get_channels().len(), 0);
1412                         // Nodes are also deleted because there are no associated channels anymore
1413                         assert_eq!(network.get_nodes().len(), 0);
1414                 }
1415                 // TODO: Test HTLCFailChannelUpdate::NodeFailure, which is not implemented yet.
1416         }
1417
1418         #[test]
1419         fn getting_next_channel_announcements() {
1420                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1421                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1422                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1423                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1424                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1425                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1426                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1427
1428                 let short_channel_id = 1;
1429                 let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
1430
1431                 // Channels were not announced yet.
1432                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(0, 1);
1433                 assert_eq!(channels_with_announcements.len(), 0);
1434
1435                 {
1436                         // Announce a channel we will update
1437                         let unsigned_announcement = UnsignedChannelAnnouncement {
1438                                 features: ChannelFeatures::empty(),
1439                                 chain_hash,
1440                                 short_channel_id,
1441                                 node_id_1,
1442                                 node_id_2,
1443                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1444                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1445                                 excess_data: Vec::new(),
1446                         };
1447
1448                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1449                         let valid_channel_announcement = ChannelAnnouncement {
1450                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1451                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1452                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1453                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1454                                 contents: unsigned_announcement.clone(),
1455                         };
1456                         match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1457                                 Ok(_) => (),
1458                                 Err(_) => panic!()
1459                         };
1460                 }
1461
1462                 // Contains initial channel announcement now.
1463                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1);
1464                 assert_eq!(channels_with_announcements.len(), 1);
1465                 if let Some(channel_announcements) = channels_with_announcements.first() {
1466                         let &(_, ref update_1, ref update_2) = channel_announcements;
1467                         assert_eq!(update_1, &None);
1468                         assert_eq!(update_2, &None);
1469                 } else {
1470                         panic!();
1471                 }
1472
1473
1474                 {
1475                         // Valid channel update
1476                         let unsigned_channel_update = UnsignedChannelUpdate {
1477                                 chain_hash,
1478                                 short_channel_id,
1479                                 timestamp: 101,
1480                                 flags: 0,
1481                                 cltv_expiry_delta: 144,
1482                                 htlc_minimum_msat: 1000000,
1483                                 htlc_maximum_msat: OptionalField::Absent,
1484                                 fee_base_msat: 10000,
1485                                 fee_proportional_millionths: 20,
1486                                 excess_data: Vec::new()
1487                         };
1488                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1489                         let valid_channel_update = ChannelUpdate {
1490                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
1491                                 contents: unsigned_channel_update.clone()
1492                         };
1493                         match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1494                                 Ok(_) => (),
1495                                 Err(_) => panic!()
1496                         };
1497                 }
1498
1499                 // Now contains an initial announcement and an update.
1500                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1);
1501                 assert_eq!(channels_with_announcements.len(), 1);
1502                 if let Some(channel_announcements) = channels_with_announcements.first() {
1503                         let &(_, ref update_1, ref update_2) = channel_announcements;
1504                         assert_ne!(update_1, &None);
1505                         assert_eq!(update_2, &None);
1506                 } else {
1507                         panic!();
1508                 }
1509
1510
1511                 {
1512                         // Channel update with excess data.
1513                         let unsigned_channel_update = UnsignedChannelUpdate {
1514                                 chain_hash,
1515                                 short_channel_id,
1516                                 timestamp: 102,
1517                                 flags: 0,
1518                                 cltv_expiry_delta: 144,
1519                                 htlc_minimum_msat: 1000000,
1520                                 htlc_maximum_msat: OptionalField::Absent,
1521                                 fee_base_msat: 10000,
1522                                 fee_proportional_millionths: 20,
1523                                 excess_data: [1; 3].to_vec()
1524                         };
1525                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1526                         let valid_channel_update = ChannelUpdate {
1527                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
1528                                 contents: unsigned_channel_update.clone()
1529                         };
1530                         match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1531                                 Ok(_) => (),
1532                                 Err(_) => panic!()
1533                         };
1534                 }
1535
1536                 // Test that announcements with excess data won't be returned
1537                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1);
1538                 assert_eq!(channels_with_announcements.len(), 1);
1539                 if let Some(channel_announcements) = channels_with_announcements.first() {
1540                         let &(_, ref update_1, ref update_2) = channel_announcements;
1541                         assert_eq!(update_1, &None);
1542                         assert_eq!(update_2, &None);
1543                 } else {
1544                         panic!();
1545                 }
1546
1547                 // Further starting point have no channels after it
1548                 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id + 1000, 1);
1549                 assert_eq!(channels_with_announcements.len(), 0);
1550         }
1551
1552         #[test]
1553         fn getting_next_node_announcements() {
1554                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1555                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1556                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1557                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1558                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1559                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1560                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1561
1562                 let short_channel_id = 1;
1563                 let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
1564
1565                 // No nodes yet.
1566                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 10);
1567                 assert_eq!(next_announcements.len(), 0);
1568
1569                 {
1570                         // Announce a channel to add 2 nodes
1571                         let unsigned_announcement = UnsignedChannelAnnouncement {
1572                                 features: ChannelFeatures::empty(),
1573                                 chain_hash,
1574                                 short_channel_id,
1575                                 node_id_1,
1576                                 node_id_2,
1577                                 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1578                                 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1579                                 excess_data: Vec::new(),
1580                         };
1581
1582                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1583                         let valid_channel_announcement = ChannelAnnouncement {
1584                                 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1585                                 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1586                                 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1587                                 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1588                                 contents: unsigned_announcement.clone(),
1589                         };
1590                         match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1591                                 Ok(_) => (),
1592                                 Err(_) => panic!()
1593                         };
1594                 }
1595
1596
1597                 // Nodes were never announced
1598                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 3);
1599                 assert_eq!(next_announcements.len(), 0);
1600
1601                 {
1602                         let mut unsigned_announcement = UnsignedNodeAnnouncement {
1603                                 features: NodeFeatures::known(),
1604                                 timestamp: 1000,
1605                                 node_id: node_id_1,
1606                                 rgb: [0; 3],
1607                                 alias: [0; 32],
1608                                 addresses: Vec::new(),
1609                                 excess_address_data: Vec::new(),
1610                                 excess_data: Vec::new(),
1611                         };
1612                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1613                         let valid_announcement = NodeAnnouncement {
1614                                 signature: secp_ctx.sign(&msghash, node_1_privkey),
1615                                 contents: unsigned_announcement.clone()
1616                         };
1617                         match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1618                                 Ok(_) => (),
1619                                 Err(_) => panic!()
1620                         };
1621
1622                         unsigned_announcement.node_id = node_id_2;
1623                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1624                         let valid_announcement = NodeAnnouncement {
1625                                 signature: secp_ctx.sign(&msghash, node_2_privkey),
1626                                 contents: unsigned_announcement.clone()
1627                         };
1628
1629                         match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1630                                 Ok(_) => (),
1631                                 Err(_) => panic!()
1632                         };
1633                 }
1634
1635                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 3);
1636                 assert_eq!(next_announcements.len(), 2);
1637
1638                 // Skip the first node.
1639                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(Some(&node_id_1), 2);
1640                 assert_eq!(next_announcements.len(), 1);
1641
1642                 {
1643                         // Later announcement which should not be relayed (excess data) prevent us from sharing a node
1644                         let unsigned_announcement = UnsignedNodeAnnouncement {
1645                                 features: NodeFeatures::known(),
1646                                 timestamp: 1010,
1647                                 node_id: node_id_2,
1648                                 rgb: [0; 3],
1649                                 alias: [0; 32],
1650                                 addresses: Vec::new(),
1651                                 excess_address_data: Vec::new(),
1652                                 excess_data: [1; 3].to_vec(),
1653                         };
1654                         let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1655                         let valid_announcement = NodeAnnouncement {
1656                                 signature: secp_ctx.sign(&msghash, node_2_privkey),
1657                                 contents: unsigned_announcement.clone()
1658                         };
1659                         match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1660                                 Ok(res) => assert!(!res),
1661                                 Err(_) => panic!()
1662                         };
1663                 }
1664
1665                 let next_announcements = net_graph_msg_handler.get_next_node_announcements(Some(&node_id_1), 2);
1666                 assert_eq!(next_announcements.len(), 0);
1667         }
1668
1669         #[test]
1670         fn network_graph_serialization() {
1671                 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1672
1673                 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1674                 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1675                 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1676                 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1677
1678                 // Announce a channel to add a corresponding node.
1679                 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1680                 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1681                 let unsigned_announcement = UnsignedChannelAnnouncement {
1682                         features: ChannelFeatures::known(),
1683                         chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
1684                         short_channel_id: 0,
1685                         node_id_1,
1686                         node_id_2,
1687                         bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1688                         bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1689                         excess_data: Vec::new(),
1690                 };
1691
1692                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1693                 let valid_announcement = ChannelAnnouncement {
1694                         node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1695                         node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1696                         bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1697                         bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1698                         contents: unsigned_announcement.clone(),
1699                 };
1700                 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1701                         Ok(res) => assert!(res),
1702                         _ => panic!()
1703                 };
1704
1705
1706                 let node_id = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1707                 let unsigned_announcement = UnsignedNodeAnnouncement {
1708                         features: NodeFeatures::known(),
1709                         timestamp: 100,
1710                         node_id,
1711                         rgb: [0; 3],
1712                         alias: [0; 32],
1713                         addresses: Vec::new(),
1714                         excess_address_data: Vec::new(),
1715                         excess_data: Vec::new(),
1716                 };
1717                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1718                 let valid_announcement = NodeAnnouncement {
1719                         signature: secp_ctx.sign(&msghash, node_1_privkey),
1720                         contents: unsigned_announcement.clone()
1721                 };
1722
1723                 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1724                         Ok(_) => (),
1725                         Err(_) => panic!()
1726                 };
1727
1728                 let network = net_graph_msg_handler.network_graph.write().unwrap();
1729                 let mut w = test_utils::TestVecWriter(Vec::new());
1730                 assert!(!network.get_nodes().is_empty());
1731                 assert!(!network.get_channels().is_empty());
1732                 network.write(&mut w).unwrap();
1733                 assert!(<NetworkGraph>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);
1734         }
1735 }