1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! The top-level network map tracking logic lives here.
12 use bitcoin::secp256k1::key::PublicKey;
13 use bitcoin::secp256k1::Secp256k1;
14 use bitcoin::secp256k1;
16 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
17 use bitcoin::hashes::Hash;
18 use bitcoin::blockdata::script::Builder;
19 use bitcoin::blockdata::transaction::TxOut;
20 use bitcoin::blockdata::opcodes;
21 use bitcoin::hash_types::BlockHash;
25 use ln::features::{ChannelFeatures, NodeFeatures};
26 use ln::msgs::{DecodeError, ErrorAction, LightningError, RoutingMessageHandler, NetAddress, MAX_VALUE_MSAT};
27 use ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, OptionalField};
28 use ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd};
30 use util::ser::{Writeable, Readable, Writer};
31 use util::logger::Logger;
35 use std::sync::{RwLock, RwLockReadGuard};
36 use std::sync::atomic::{AtomicUsize, Ordering};
38 use std::collections::BTreeMap;
39 use std::collections::btree_map::Entry as BtreeEntry;
40 use std::collections::HashMap;
42 use bitcoin::hashes::hex::ToHex;
44 /// Maximum number of short_channel_id values that can be encoded in a
45 /// single reply_channel_range or query_short_channel_ids messages when
46 /// using raw encoding. The maximum value ensures that the 8-byte SCIDs
47 /// fit inside the maximum size of the Lightning message, 65535-bytes.
48 const MAX_SHORT_CHANNEL_ID_BATCH_SIZE: usize = 8000;
50 /// Maximum number of reply_channel_range messages we will allow in
51 /// reply to a query_channel_range. This value creates an upper-limit
52 /// on the number of SCIDs we process in reply to a single query.
53 const MAX_REPLY_CHANNEL_RANGE_PER_QUERY: usize = 250;
55 /// Represents the network as nodes and channels between them
57 pub struct NetworkGraph {
58 channels: BTreeMap<u64, ChannelInfo>,
59 nodes: BTreeMap<PublicKey, NodeInfo>,
62 /// A simple newtype for RwLockReadGuard<'a, NetworkGraph>.
63 /// This exists only to make accessing a RwLock<NetworkGraph> possible from
64 /// the C bindings, as it can be done directly in Rust code.
65 pub struct LockedNetworkGraph<'a>(pub RwLockReadGuard<'a, NetworkGraph>);
67 /// Receives and validates network updates from peers,
68 /// stores authentic and relevant data as a network graph.
69 /// This network graph is then used for routing payments.
70 /// Provides interface to help with initial routing sync by
71 /// serving historical announcements.
72 pub struct NetGraphMsgHandler<C: Deref, L: Deref> where C::Target: chain::Access, L::Target: Logger {
73 secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
74 /// Representation of the payment channel network
75 pub network_graph: RwLock<NetworkGraph>,
76 chain_access: Option<C>,
77 full_syncs_requested: AtomicUsize,
78 pending_events: Mutex<Vec<events::MessageSendEvent>>,
79 chan_range_query_tasks: Mutex<HashMap<PublicKey, ChanRangeQueryTask>>,
80 scid_query_tasks: Mutex<HashMap<PublicKey, ScidQueryTask>>,
84 impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: chain::Access, L::Target: Logger {
85 /// Creates a new tracker of the actual state of the network of channels and nodes,
86 /// assuming a fresh network graph.
87 /// Chain monitor is used to make sure announced channels exist on-chain,
88 /// channel data is correct, and that the announcement is signed with
89 /// channel owners' keys.
90 pub fn new(chain_access: Option<C>, logger: L) -> Self {
92 secp_ctx: Secp256k1::verification_only(),
93 network_graph: RwLock::new(NetworkGraph {
94 channels: BTreeMap::new(),
95 nodes: BTreeMap::new(),
97 full_syncs_requested: AtomicUsize::new(0),
99 pending_events: Mutex::new(vec![]),
100 chan_range_query_tasks: Mutex::new(HashMap::new()),
101 scid_query_tasks: Mutex::new(HashMap::new()),
106 /// Creates a new tracker of the actual state of the network of channels and nodes,
107 /// assuming an existing Network Graph.
108 pub fn from_net_graph(chain_access: Option<C>, logger: L, network_graph: NetworkGraph) -> Self {
110 secp_ctx: Secp256k1::verification_only(),
111 network_graph: RwLock::new(network_graph),
112 full_syncs_requested: AtomicUsize::new(0),
114 pending_events: Mutex::new(vec![]),
115 chan_range_query_tasks: Mutex::new(HashMap::new()),
116 scid_query_tasks: Mutex::new(HashMap::new()),
121 /// Take a read lock on the network_graph and return it in the C-bindings
122 /// newtype helper. This is likely only useful when called via the C
123 /// bindings as you can call `self.network_graph.read().unwrap()` in Rust
125 pub fn read_locked_graph<'a>(&'a self) -> LockedNetworkGraph<'a> {
126 LockedNetworkGraph(self.network_graph.read().unwrap())
129 /// Enqueues a message send event for a batch of short_channel_ids
131 fn finalize_query_short_ids(&self, task: &mut ScidQueryTask) {
132 let scid_size = std::cmp::min(task.short_channel_ids.len(), MAX_SHORT_CHANNEL_ID_BATCH_SIZE);
133 let mut short_channel_ids: Vec<u64> = Vec::with_capacity(scid_size);
134 for scid in task.short_channel_ids.drain(..scid_size) {
135 short_channel_ids.push(scid);
138 log_debug!(self.logger, "Sending query_short_channel_ids peer={}, batch_size={}", log_pubkey!(task.node_id), scid_size);
140 // enqueue the message to the peer
141 let mut pending_events = self.pending_events.lock().unwrap();
142 pending_events.push(events::MessageSendEvent::SendShortIdsQuery {
143 node_id: task.node_id.clone(),
144 msg: QueryShortChannelIds {
145 chain_hash: task.chain_hash.clone(),
152 impl<'a> LockedNetworkGraph<'a> {
153 /// Get a reference to the NetworkGraph which this read-lock contains.
154 pub fn graph(&self) -> &NetworkGraph {
160 macro_rules! secp_verify_sig {
161 ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
162 match $secp_ctx.verify($msg, $sig, $pubkey) {
164 Err(_) => return Err(LightningError{err: "Invalid signature from remote node".to_owned(), action: ErrorAction::IgnoreError}),
169 impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for NetGraphMsgHandler<C, L> where C::Target: chain::Access, L::Target: Logger {
170 fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
171 self.network_graph.write().unwrap().update_node_from_announcement(msg, &self.secp_ctx)?;
172 Ok(msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty())
175 fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
176 self.network_graph.write().unwrap().update_channel_from_announcement(msg, &self.chain_access, &self.secp_ctx)?;
177 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 { "" });
178 Ok(msg.contents.excess_data.is_empty())
181 fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
183 &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
184 let _ = self.network_graph.write().unwrap().update_channel(msg, &self.secp_ctx);
186 &msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id, is_permanent } => {
187 self.network_graph.write().unwrap().close_channel_from_update(short_channel_id, is_permanent);
189 &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, is_permanent } => {
190 self.network_graph.write().unwrap().fail_node(node_id, is_permanent);
195 fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> {
196 self.network_graph.write().unwrap().update_channel(msg, &self.secp_ctx)?;
197 Ok(msg.contents.excess_data.is_empty())
200 fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)> {
201 let network_graph = self.network_graph.read().unwrap();
202 let mut result = Vec::with_capacity(batch_amount as usize);
203 let mut iter = network_graph.get_channels().range(starting_point..);
204 while result.len() < batch_amount as usize {
205 if let Some((_, ref chan)) = iter.next() {
206 if chan.announcement_message.is_some() {
207 let chan_announcement = chan.announcement_message.clone().unwrap();
208 let mut one_to_two_announcement: Option<msgs::ChannelUpdate> = None;
209 let mut two_to_one_announcement: Option<msgs::ChannelUpdate> = None;
210 if let Some(one_to_two) = chan.one_to_two.as_ref() {
211 one_to_two_announcement = one_to_two.last_update_message.clone();
213 if let Some(two_to_one) = chan.two_to_one.as_ref() {
214 two_to_one_announcement = two_to_one.last_update_message.clone();
216 result.push((chan_announcement, one_to_two_announcement, two_to_one_announcement));
218 // TODO: We may end up sending un-announced channel_updates if we are sending
219 // initial sync data while receiving announce/updates for this channel.
228 fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement> {
229 let network_graph = self.network_graph.read().unwrap();
230 let mut result = Vec::with_capacity(batch_amount as usize);
231 let mut iter = if let Some(pubkey) = starting_point {
232 let mut iter = network_graph.get_nodes().range((*pubkey)..);
236 network_graph.get_nodes().range(..)
238 while result.len() < batch_amount as usize {
239 if let Some((_, ref node)) = iter.next() {
240 if let Some(node_info) = node.announcement_info.as_ref() {
241 if node_info.announcement_message.is_some() {
242 result.push(node_info.announcement_message.clone().unwrap());
252 fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
253 //TODO: Determine whether to request a full sync based on the network map.
254 const FULL_SYNCS_TO_REQUEST: usize = 5;
255 if self.full_syncs_requested.load(Ordering::Acquire) < FULL_SYNCS_TO_REQUEST {
256 self.full_syncs_requested.fetch_add(1, Ordering::AcqRel);
263 fn query_channel_range(&self, their_node_id: &PublicKey, chain_hash: BlockHash, first_blocknum: u32, number_of_blocks: u32) -> Result<(), LightningError> {
264 // We must ensure that we only have a single in-flight query
265 // to the remote peer. If we already have a query, then we fail
266 let mut query_range_tasks_lock = self.chan_range_query_tasks.lock().unwrap();
267 let query_range_tasks = &mut *query_range_tasks_lock;
268 if query_range_tasks.contains_key(their_node_id) {
269 return Err(LightningError {
270 err: String::from("query_channel_range already in-flight"),
271 action: ErrorAction::IgnoreError,
275 // Construct a new task to keep track of the query until the full
276 // range query has been completed
277 let task = ChanRangeQueryTask::new(their_node_id, chain_hash, first_blocknum, number_of_blocks);
278 query_range_tasks.insert(their_node_id.clone(), task);
280 // Enqueue the message send event
281 log_debug!(self.logger, "Sending query_channel_range peer={}, first_blocknum={}, number_of_blocks={}", log_pubkey!(their_node_id), first_blocknum, number_of_blocks);
282 let mut pending_events = self.pending_events.lock().unwrap();
283 pending_events.push(events::MessageSendEvent::SendChannelRangeQuery {
284 node_id: their_node_id.clone(),
285 msg: QueryChannelRange {
294 /// A query should only request channels referring to unspent outputs.
295 /// This method does not validate this requirement and expects the
296 /// caller to ensure SCIDs are unspent.
297 fn query_short_channel_ids(&self, their_node_id: &PublicKey, chain_hash: BlockHash, short_channel_ids: Vec<u64>) -> Result<(), LightningError> {
298 // Create a new task or add to the existing task
299 let mut query_scids_tasks_lock = self.scid_query_tasks.lock().unwrap();
300 let query_scids_tasks = &mut *query_scids_tasks_lock;
302 // For an existing task we append the short_channel_ids which will be sent when the
303 // current in-flight batch completes.
304 if let Some(task) = query_scids_tasks.get_mut(their_node_id) {
305 task.add(short_channel_ids);
309 // For a new task we create the task with short_channel_ids and send the first
310 // batch immediately.
311 query_scids_tasks.insert(their_node_id.clone(), ScidQueryTask::new(
316 let task = query_scids_tasks.get_mut(their_node_id).unwrap();
317 self.finalize_query_short_ids(task);
321 fn handle_reply_channel_range(&self, their_node_id: &PublicKey, msg: &ReplyChannelRange) -> Result<(), LightningError> {
322 log_debug!(self.logger, "Handling reply_channel_range peer={}, first_blocknum={}, number_of_blocks={}, full_information={}, scids={}", log_pubkey!(their_node_id), msg.first_blocknum, msg.number_of_blocks, msg.full_information, msg.short_channel_ids.len(),);
324 // First we obtain a lock on the task hashmap. In order to avoid borrowing issues
325 // we will access the task as needed.
326 let mut query_range_tasks = self.chan_range_query_tasks.lock().unwrap();
328 // If there is no currently executing task then we have received
329 // an invalid message and will return an error
330 if query_range_tasks.get(their_node_id).is_none() {
331 return Err(LightningError {
332 err: String::from("Received unknown reply_channel_range message"),
333 action: ErrorAction::IgnoreError,
337 // Now that we know we have a task, we can extract a few values for use
338 // in validations without having to access the task repeatedly
339 let (task_chain_hash, task_first_blocknum, task_number_of_blocks, task_received_first_block, task_received_last_block, task_number_of_replies) = {
340 let task = query_range_tasks.get(their_node_id).unwrap();
341 (task.chain_hash, task.first_blocknum, task.number_of_blocks, task.received_first_block, task.received_last_block, task.number_of_replies)
344 // Validate the chain_hash matches the chain_hash we used in the query.
345 // If it does not, then the message is malformed and we return an error
346 if msg.chain_hash != task_chain_hash {
347 query_range_tasks.remove(their_node_id);
348 return Err(LightningError {
349 err: String::from("Received reply_channel_range with invalid chain_hash"),
350 action: ErrorAction::IgnoreError,
354 // Validate that the remote node maintains up-to-date channel
355 // information for chain_hash. Some nodes use the full_information
356 // flag to indicate multi-part messages so we must check whether
357 // we received information as well.
358 if !msg.full_information && msg.short_channel_ids.len() == 0 {
359 query_range_tasks.remove(their_node_id);
360 return Err(LightningError {
361 err: String::from("Received reply_channel_range with no information available"),
362 action: ErrorAction::IgnoreError,
366 // Calculate the last block for the message and the task
367 let msg_last_block = last_blocknum(msg.first_blocknum, msg.number_of_blocks);
368 let task_last_block = last_blocknum(task_first_blocknum, task_number_of_blocks);
370 // On the first message...
371 if task_received_first_block.is_none() {
372 // The replies can be a superset of the queried block range, but the
373 // replies must include our requested query range. We check if the
374 // start of the replies is greater than the start of our query. If
375 // so, the start of our query is excluded and the message is malformed.
376 if msg.first_blocknum > task_first_blocknum {
377 query_range_tasks.remove(their_node_id);
378 return Err(LightningError {
379 err: String::from("Failing reply_channel_range with invalid first_blocknum"),
380 action: ErrorAction::IgnoreError,
384 // Next, we ensure the reply has at least some information matching
385 // our query. If the received last_blocknum is less than our query's
386 // first_blocknum then the reply does not encompass the query range
387 // and the message is malformed.
388 if msg_last_block < task_first_blocknum {
389 query_range_tasks.remove(their_node_id);
390 return Err(LightningError {
391 err: String::from("Failing reply_channel_range with non-overlapping first reply"),
392 action: ErrorAction::IgnoreError,
396 // Capture the first block and last block so that subsequent messages
398 let task = query_range_tasks.get_mut(their_node_id).unwrap();
399 task.received_first_block = Some(msg.first_blocknum);
400 task.received_last_block = Some(msg_last_block);
402 // On subsequent message(s)...
404 // We need to validate the sequence of the reply message is expected.
405 // Subsequent messages must set the first_blocknum to the previous
406 // message's first_blocknum plus number_of_blocks. There is discrepancy
407 // in implementation where some resume on the last sent block. We will
408 // loosen the restriction and accept either, and otherwise consider the
409 // message malformed and return an error.
410 let task_received_last_block = task_received_last_block.unwrap();
411 if msg.first_blocknum != task_received_last_block && msg.first_blocknum != task_received_last_block + 1 {
412 query_range_tasks.remove(their_node_id);
413 return Err(LightningError {
414 err: String::from("Failing reply_channel_range with invalid sequence"),
415 action: ErrorAction::IgnoreError,
419 // Next we check to see that we have received a realistic number of
420 // reply messages for a query. This caps the allocation exposure
421 // for short_channel_ids that will be batched and sent in query channels.
422 if task_number_of_replies + 1 > MAX_REPLY_CHANNEL_RANGE_PER_QUERY {
423 query_range_tasks.remove(their_node_id);
424 return Err(LightningError {
425 err: String::from("Failing reply_channel_range due to excessive messages"),
426 action: ErrorAction::IgnoreError,
430 // Capture the last_block in our task so that subsequent messages
432 let task = query_range_tasks.get_mut(their_node_id).unwrap();
433 task.number_of_replies += 1;
434 task.received_last_block = Some(msg_last_block);
437 // We filter the short_channel_ids to those inside the query range.
438 // The most significant 3-bytes of the short_channel_id are the block.
440 let mut filtered_short_channel_ids: Vec<u64> = msg.short_channel_ids.clone().into_iter().filter(|short_channel_id| {
441 let block = short_channel_id >> 40;
442 return block >= query_range_tasks.get(their_node_id).unwrap().first_blocknum as u64 && block <= task_last_block as u64;
444 let task = query_range_tasks.get_mut(their_node_id).unwrap();
445 task.short_channel_ids.append(&mut filtered_short_channel_ids);
448 // The final message is indicated by a last_blocknum that is equal to
449 // or greater than the query's last_blocknum.
450 if msg_last_block >= task_last_block {
451 log_debug!(self.logger, "Completed query_channel_range: peer={}, first_blocknum={}, number_of_blocks={}", log_pubkey!(their_node_id), task_first_blocknum, task_number_of_blocks);
453 // We can now fire off a query to obtain routing messages for the
454 // accumulated short_channel_ids.
456 let task = query_range_tasks.get_mut(their_node_id).unwrap();
457 let mut short_channel_ids = Vec::new();
458 std::mem::swap(&mut short_channel_ids, &mut task.short_channel_ids);
459 self.query_short_channel_ids(their_node_id, task.chain_hash, short_channel_ids)?;
462 // We can remove the query range task now that the query is complete.
463 query_range_tasks.remove(their_node_id);
468 /// When a query is initiated the remote peer will begin streaming
469 /// gossip messages. In the event of a failure, we may have received
470 /// some channel information. Before trying with another peer, the
471 /// caller should update its set of SCIDs that need to be queried.
472 fn handle_reply_short_channel_ids_end(&self, their_node_id: &PublicKey, msg: &ReplyShortChannelIdsEnd) -> Result<(), LightningError> {
473 log_debug!(self.logger, "Handling reply_short_channel_ids_end peer={}, full_information={}", log_pubkey!(their_node_id), msg.full_information);
475 // First we obtain a lock on the task hashmap. In order to avoid borrowing issues
476 // we will access the task as needed.
477 let mut query_short_channel_ids_tasks = self.scid_query_tasks.lock().unwrap();
479 // If there is no existing task then we have received an unknown
480 // message and should return an error.
481 if query_short_channel_ids_tasks.get(their_node_id).is_none() {
482 return Err(LightningError {
483 err: String::from("Unknown reply_short_channel_ids_end message"),
484 action: ErrorAction::IgnoreError,
488 // If the reply's chain_hash does not match the task's chain_hash then
489 // the reply is malformed and we should return an error.
490 if msg.chain_hash != query_short_channel_ids_tasks.get(their_node_id).unwrap().chain_hash {
491 query_short_channel_ids_tasks.remove(their_node_id);
492 return Err(LightningError {
493 err: String::from("Received reply_short_channel_ids_end with incorrect chain_hash"),
494 action: ErrorAction::IgnoreError
498 // If the remote node does not have up-to-date information for the
499 // chain_hash they will set full_information=false. We can fail
500 // the result and try again with a different peer.
501 if !msg.full_information {
502 query_short_channel_ids_tasks.remove(their_node_id);
503 return Err(LightningError {
504 err: String::from("Received reply_short_channel_ids_end with no information"),
505 action: ErrorAction::IgnoreError
509 // If we have more scids to process we send the next batch in the task
511 let task = query_short_channel_ids_tasks.get_mut(their_node_id).unwrap();
512 if task.short_channel_ids.len() > 0 {
513 self.finalize_query_short_ids(task);
518 // Otherwise the task is complete and we can remove it
519 log_debug!(self.logger, "Completed query_short_channel_ids peer={}", log_pubkey!(their_node_id));
520 query_short_channel_ids_tasks.remove(their_node_id);
524 /// There are potential DoS vectors when handling inbound queries.
525 /// Handling requests with first_blocknum very far away may trigger repeated
526 /// disk I/O if the NetworkGraph is not fully in-memory.
527 fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: &QueryChannelRange) -> Result<(), LightningError> {
530 err: String::from("Not implemented"),
531 action: ErrorAction::IgnoreError,
535 /// There are potential DoS vectors when handling inbound queries.
536 /// Handling requests with first_blocknum very far away may trigger repeated
537 /// disk I/O if the NetworkGraph is not fully in-memory.
538 fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: &QueryShortChannelIds) -> Result<(), LightningError> {
541 err: String::from("Not implemented"),
542 action: ErrorAction::IgnoreError,
547 impl<C: Deref, L: Deref> events::MessageSendEventsProvider for NetGraphMsgHandler<C, L>
549 C::Target: chain::Access,
552 fn get_and_clear_pending_msg_events(&self) -> Vec<events::MessageSendEvent> {
553 let mut ret = Vec::new();
554 let mut pending_events = self.pending_events.lock().unwrap();
555 std::mem::swap(&mut ret, &mut pending_events);
560 /// Safely calculates the last_blocknum given a first_blocknum and
561 /// number_of_blocks by returning the u32::MAX-1 if there is an overflow
562 fn last_blocknum(first_blocknum: u32, number_of_blocks: u32) -> u32 {
563 match first_blocknum.checked_add(number_of_blocks) {
564 Some(val) => val - 1,
565 None => 0xffff_ffff - 1,
569 /// Maintains state for a channel range query that we initiated.
570 /// The query may result in one or more reply_channel_range messages
571 /// being received. This struct helps determine the status of the query
572 /// when there are multiple replies. It also collects results for initiating
575 /// The task is complete and can be cleaned up when a reply meets or
576 /// exceeds the last block in the query. The collected SCIDs in the task
577 /// can be used to generate an ScidQueryTask.
579 /// A query may fail if the recipient does not maintain up-to-date
580 /// information for the chain or if the recipient fails to reply within
581 /// a reasonable amount of time. In either event, the query can be
582 /// re-initiated with a different peer.
583 pub struct ChanRangeQueryTask {
584 /// The public key of the node we will be sending queries to
585 pub node_id: PublicKey,
586 /// The genesis hash of the blockchain being queried
587 pub chain_hash: BlockHash,
588 /// The height of the first block for the channel UTXOs being queried
589 pub first_blocknum: u32,
590 /// The number of blocks to include in the query results
591 pub number_of_blocks: u32,
592 /// Tracks the number of reply messages we have received
593 pub number_of_replies: usize,
594 /// The height of the first block received in a reply. This value
595 /// should be less than or equal to the first_blocknum requested in
596 /// the query_channel_range. This allows the range of the replies to
597 /// contain, but not necessarily strictly, the queried range.
598 pub received_first_block: Option<u32>,
599 /// The height of the last block received in a reply. This value
600 /// will get incrementally closer to the target of
601 /// first_blocknum plus number_of_blocks from the query_channel_range.
602 pub received_last_block: Option<u32>,
603 /// Contains short_channel_ids received in one or more reply messages.
604 /// These will be sent in one ore more query_short_channel_ids messages
605 /// when the task is complete.
606 pub short_channel_ids: Vec<u64>,
609 impl ChanRangeQueryTask {
610 /// Constructs a new GossipQueryRangeTask
611 pub fn new(their_node_id: &PublicKey, chain_hash: BlockHash, first_blocknum: u32, number_of_blocks: u32) -> Self {
613 node_id: their_node_id.clone(),
617 number_of_replies: 0,
618 received_first_block: None,
619 received_last_block: None,
620 short_channel_ids: vec![],
625 /// Maintains state when sending one or more short_channel_ids messages
626 /// to a peer. Only a single SCID query can be in-flight with a peer. The
627 /// number of SCIDs per query is limited by the size of a Lightning message
628 /// payload. When querying a large number of SCIDs (results of a large
629 /// channel range query for instance), multiple query_short_channel_ids
630 /// messages need to be sent. This task maintains the list of awaiting
631 /// SCIDs to be queried.
633 /// When a successful reply_short_channel_ids_end message is received, the
634 /// next batch of SCIDs can be sent. When no remaining SCIDs exist in the
635 /// task, the task is complete and can be cleaned up.
637 /// The recipient may reply indicating that up-to-date information for the
638 /// chain is not maintained. A query may also fail to complete within a
639 /// reasonable amount of time. In either event, the short_channel_ids
640 /// can be queried from a different peer after validating the set of
641 /// SCIDs that still need to be queried.
642 pub struct ScidQueryTask {
643 /// The public key of the node we will be sending queries to
644 pub node_id: PublicKey,
645 /// The genesis hash of the blockchain being queried
646 pub chain_hash: BlockHash,
647 /// A vector of short_channel_ids that we would like routing gossip
648 /// information for. This list will be chunked and sent to the peer
649 /// in one or more query_short_channel_ids messages.
650 pub short_channel_ids: Vec<u64>,
654 /// Constructs a new GossipQueryShortChannelIdsTask
655 pub fn new(their_node_id: &PublicKey, chain_hash: BlockHash, short_channel_ids: Vec<u64>) -> Self {
657 node_id: their_node_id.clone(),
663 /// Adds short_channel_ids to the pending list of short_channel_ids
664 /// to be sent in the next request. You can add additional values
665 /// while a query is in-flight. These new values will be sent once
666 /// the active query has completed.
667 pub fn add(&mut self, mut short_channel_ids: Vec<u64>) {
668 self.short_channel_ids.append(&mut short_channel_ids);
672 #[derive(PartialEq, Debug)]
673 /// Details about one direction of a channel. Received
674 /// within a channel update.
675 pub struct DirectionalChannelInfo {
676 /// When the last update to the channel direction was issued.
677 /// Value is opaque, as set in the announcement.
678 pub last_update: u32,
679 /// Whether the channel can be currently used for payments (in this one direction).
681 /// The difference in CLTV values that you must have when routing through this channel.
682 pub cltv_expiry_delta: u16,
683 /// The minimum value, which must be relayed to the next hop via the channel
684 pub htlc_minimum_msat: u64,
685 /// The maximum value which may be relayed to the next hop via the channel.
686 pub htlc_maximum_msat: Option<u64>,
687 /// Fees charged when the channel is used for routing
688 pub fees: RoutingFees,
689 /// Most recent update for the channel received from the network
690 /// Mostly redundant with the data we store in fields explicitly.
691 /// Everything else is useful only for sending out for initial routing sync.
692 /// Not stored if contains excess data to prevent DoS.
693 pub last_update_message: Option<ChannelUpdate>,
696 impl fmt::Display for DirectionalChannelInfo {
697 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
698 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)?;
703 impl_writeable!(DirectionalChannelInfo, 0, {
714 /// Details about a channel (both directions).
715 /// Received within a channel announcement.
716 pub struct ChannelInfo {
717 /// Protocol features of a channel communicated during its announcement
718 pub features: ChannelFeatures,
719 /// Source node of the first direction of a channel
720 pub node_one: PublicKey,
721 /// Details about the first direction of a channel
722 pub one_to_two: Option<DirectionalChannelInfo>,
723 /// Source node of the second direction of a channel
724 pub node_two: PublicKey,
725 /// Details about the second direction of a channel
726 pub two_to_one: Option<DirectionalChannelInfo>,
727 /// The channel capacity as seen on-chain, if chain lookup is available.
728 pub capacity_sats: Option<u64>,
729 /// An initial announcement of the channel
730 /// Mostly redundant with the data we store in fields explicitly.
731 /// Everything else is useful only for sending out for initial routing sync.
732 /// Not stored if contains excess data to prevent DoS.
733 pub announcement_message: Option<ChannelAnnouncement>,
736 impl fmt::Display for ChannelInfo {
737 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
738 write!(f, "features: {}, node_one: {}, one_to_two: {:?}, node_two: {}, two_to_one: {:?}",
739 log_bytes!(self.features.encode()), log_pubkey!(self.node_one), self.one_to_two, log_pubkey!(self.node_two), self.two_to_one)?;
744 impl_writeable!(ChannelInfo, 0, {
755 /// Fees for routing via a given channel or a node
756 #[derive(Eq, PartialEq, Copy, Clone, Debug)]
757 pub struct RoutingFees {
758 /// Flat routing fee in satoshis
760 /// Liquidity-based routing fee in millionths of a routed amount.
761 /// In other words, 10000 is 1%.
762 pub proportional_millionths: u32,
765 impl Readable for RoutingFees{
766 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<RoutingFees, DecodeError> {
767 let base_msat: u32 = Readable::read(reader)?;
768 let proportional_millionths: u32 = Readable::read(reader)?;
771 proportional_millionths,
776 impl Writeable for RoutingFees {
777 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
778 self.base_msat.write(writer)?;
779 self.proportional_millionths.write(writer)?;
784 #[derive(PartialEq, Debug)]
785 /// Information received in the latest node_announcement from this node.
786 pub struct NodeAnnouncementInfo {
787 /// Protocol features the node announced support for
788 pub features: NodeFeatures,
789 /// When the last known update to the node state was issued.
790 /// Value is opaque, as set in the announcement.
791 pub last_update: u32,
792 /// Color assigned to the node
794 /// Moniker assigned to the node.
795 /// May be invalid or malicious (eg control chars),
796 /// should not be exposed to the user.
798 /// Internet-level addresses via which one can connect to the node
799 pub addresses: Vec<NetAddress>,
800 /// An initial announcement of the node
801 /// Mostly redundant with the data we store in fields explicitly.
802 /// Everything else is useful only for sending out for initial routing sync.
803 /// Not stored if contains excess data to prevent DoS.
804 pub announcement_message: Option<NodeAnnouncement>
807 impl Writeable for NodeAnnouncementInfo {
808 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
809 self.features.write(writer)?;
810 self.last_update.write(writer)?;
811 self.rgb.write(writer)?;
812 self.alias.write(writer)?;
813 (self.addresses.len() as u64).write(writer)?;
814 for ref addr in &self.addresses {
817 self.announcement_message.write(writer)?;
822 impl Readable for NodeAnnouncementInfo {
823 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeAnnouncementInfo, DecodeError> {
824 let features = Readable::read(reader)?;
825 let last_update = Readable::read(reader)?;
826 let rgb = Readable::read(reader)?;
827 let alias = Readable::read(reader)?;
828 let addresses_count: u64 = Readable::read(reader)?;
829 let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
830 for _ in 0..addresses_count {
831 match Readable::read(reader) {
832 Ok(Ok(addr)) => { addresses.push(addr); },
833 Ok(Err(_)) => return Err(DecodeError::InvalidValue),
834 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
838 let announcement_message = Readable::read(reader)?;
839 Ok(NodeAnnouncementInfo {
851 /// Details about a node in the network, known from the network announcement.
852 pub struct NodeInfo {
853 /// All valid channels a node has announced
854 pub channels: Vec<u64>,
855 /// Lowest fees enabling routing via any of the enabled, known channels to a node.
856 /// The two fields (flat and proportional fee) are independent,
857 /// meaning they don't have to refer to the same channel.
858 pub lowest_inbound_channel_fees: Option<RoutingFees>,
859 /// More information about a node from node_announcement.
860 /// Optional because we store a Node entry after learning about it from
861 /// a channel announcement, but before receiving a node announcement.
862 pub announcement_info: Option<NodeAnnouncementInfo>
865 impl fmt::Display for NodeInfo {
866 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
867 write!(f, "lowest_inbound_channel_fees: {:?}, channels: {:?}, announcement_info: {:?}",
868 self.lowest_inbound_channel_fees, &self.channels[..], self.announcement_info)?;
873 impl Writeable for NodeInfo {
874 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
875 (self.channels.len() as u64).write(writer)?;
876 for ref chan in self.channels.iter() {
879 self.lowest_inbound_channel_fees.write(writer)?;
880 self.announcement_info.write(writer)?;
885 const MAX_ALLOC_SIZE: u64 = 64*1024;
887 impl Readable for NodeInfo {
888 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeInfo, DecodeError> {
889 let channels_count: u64 = Readable::read(reader)?;
890 let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
891 for _ in 0..channels_count {
892 channels.push(Readable::read(reader)?);
894 let lowest_inbound_channel_fees = Readable::read(reader)?;
895 let announcement_info = Readable::read(reader)?;
898 lowest_inbound_channel_fees,
904 impl Writeable for NetworkGraph {
905 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
906 (self.channels.len() as u64).write(writer)?;
907 for (ref chan_id, ref chan_info) in self.channels.iter() {
908 (*chan_id).write(writer)?;
909 chan_info.write(writer)?;
911 (self.nodes.len() as u64).write(writer)?;
912 for (ref node_id, ref node_info) in self.nodes.iter() {
913 node_id.write(writer)?;
914 node_info.write(writer)?;
920 impl Readable for NetworkGraph {
921 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NetworkGraph, DecodeError> {
922 let channels_count: u64 = Readable::read(reader)?;
923 let mut channels = BTreeMap::new();
924 for _ in 0..channels_count {
925 let chan_id: u64 = Readable::read(reader)?;
926 let chan_info = Readable::read(reader)?;
927 channels.insert(chan_id, chan_info);
929 let nodes_count: u64 = Readable::read(reader)?;
930 let mut nodes = BTreeMap::new();
931 for _ in 0..nodes_count {
932 let node_id = Readable::read(reader)?;
933 let node_info = Readable::read(reader)?;
934 nodes.insert(node_id, node_info);
943 impl fmt::Display for NetworkGraph {
944 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
945 writeln!(f, "Network map\n[Channels]")?;
946 for (key, val) in self.channels.iter() {
947 writeln!(f, " {}: {}", key, val)?;
949 writeln!(f, "[Nodes]")?;
950 for (key, val) in self.nodes.iter() {
951 writeln!(f, " {}: {}", log_pubkey!(key), val)?;
958 /// Returns all known valid channels' short ids along with announced channel info.
960 /// (C-not exported) because we have no mapping for `BTreeMap`s
961 pub fn get_channels<'a>(&'a self) -> &'a BTreeMap<u64, ChannelInfo> { &self.channels }
962 /// Returns all known nodes' public keys along with announced node info.
964 /// (C-not exported) because we have no mapping for `BTreeMap`s
965 pub fn get_nodes<'a>(&'a self) -> &'a BTreeMap<PublicKey, NodeInfo> { &self.nodes }
967 /// Get network addresses by node id.
968 /// Returns None if the requested node is completely unknown,
969 /// or if node announcement for the node was never received.
971 /// (C-not exported) as there is no practical way to track lifetimes of returned values.
972 pub fn get_addresses<'a>(&'a self, pubkey: &PublicKey) -> Option<&'a Vec<NetAddress>> {
973 if let Some(node) = self.nodes.get(pubkey) {
974 if let Some(node_info) = node.announcement_info.as_ref() {
975 return Some(&node_info.addresses)
981 /// Creates a new, empty, network graph.
982 pub fn new() -> NetworkGraph {
984 channels: BTreeMap::new(),
985 nodes: BTreeMap::new(),
989 /// For an already known node (from channel announcements), update its stored properties from a
990 /// given node announcement.
992 /// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
993 /// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
994 /// routing messages from a source using a protocol other than the lightning P2P protocol.
995 pub fn update_node_from_announcement<T: secp256k1::Verification>(&mut self, msg: &msgs::NodeAnnouncement, secp_ctx: &Secp256k1<T>) -> Result<(), LightningError> {
996 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
997 secp_verify_sig!(secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
998 self.update_node_from_announcement_intern(&msg.contents, Some(&msg))
1001 /// For an already known node (from channel announcements), update its stored properties from a
1002 /// given node announcement without verifying the associated signatures. Because we aren't
1003 /// given the associated signatures here we cannot relay the node announcement to any of our
1005 pub fn update_node_from_unsigned_announcement(&mut self, msg: &msgs::UnsignedNodeAnnouncement) -> Result<(), LightningError> {
1006 self.update_node_from_announcement_intern(msg, None)
1009 fn update_node_from_announcement_intern(&mut self, msg: &msgs::UnsignedNodeAnnouncement, full_msg: Option<&msgs::NodeAnnouncement>) -> Result<(), LightningError> {
1010 match self.nodes.get_mut(&msg.node_id) {
1011 None => Err(LightningError{err: "No existing channels for node_announcement".to_owned(), action: ErrorAction::IgnoreError}),
1013 if let Some(node_info) = node.announcement_info.as_ref() {
1014 if node_info.last_update >= msg.timestamp {
1015 return Err(LightningError{err: "Update older than last processed update".to_owned(), action: ErrorAction::IgnoreError});
1019 let should_relay = msg.excess_data.is_empty() && msg.excess_address_data.is_empty();
1020 node.announcement_info = Some(NodeAnnouncementInfo {
1021 features: msg.features.clone(),
1022 last_update: msg.timestamp,
1025 addresses: msg.addresses.clone(),
1026 announcement_message: if should_relay { full_msg.cloned() } else { None },
1034 /// Store or update channel info from a channel announcement.
1036 /// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
1037 /// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
1038 /// routing messages from a source using a protocol other than the lightning P2P protocol.
1040 /// If a `chain::Access` object is provided via `chain_access`, it will be called to verify
1041 /// the corresponding UTXO exists on chain and is correctly-formatted.
1042 pub fn update_channel_from_announcement<T: secp256k1::Verification, C: Deref>
1043 (&mut self, msg: &msgs::ChannelAnnouncement, chain_access: &Option<C>, secp_ctx: &Secp256k1<T>)
1044 -> Result<(), LightningError>
1045 where C::Target: chain::Access {
1046 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
1047 secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
1048 secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
1049 secp_verify_sig!(secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
1050 secp_verify_sig!(secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
1051 self.update_channel_from_unsigned_announcement_intern(&msg.contents, Some(msg), chain_access)
1054 /// Store or update channel info from a channel announcement without verifying the associated
1055 /// signatures. Because we aren't given the associated signatures here we cannot relay the
1056 /// channel announcement to any of our peers.
1058 /// If a `chain::Access` object is provided via `chain_access`, it will be called to verify
1059 /// the corresponding UTXO exists on chain and is correctly-formatted.
1060 pub fn update_channel_from_unsigned_announcement<C: Deref>
1061 (&mut self, msg: &msgs::UnsignedChannelAnnouncement, chain_access: &Option<C>)
1062 -> Result<(), LightningError>
1063 where C::Target: chain::Access {
1064 self.update_channel_from_unsigned_announcement_intern(msg, None, chain_access)
1067 fn update_channel_from_unsigned_announcement_intern<C: Deref>
1068 (&mut self, msg: &msgs::UnsignedChannelAnnouncement, full_msg: Option<&msgs::ChannelAnnouncement>, chain_access: &Option<C>)
1069 -> Result<(), LightningError>
1070 where C::Target: chain::Access {
1071 if msg.node_id_1 == msg.node_id_2 || msg.bitcoin_key_1 == msg.bitcoin_key_2 {
1072 return Err(LightningError{err: "Channel announcement node had a channel with itself".to_owned(), action: ErrorAction::IgnoreError});
1075 let utxo_value = match &chain_access {
1077 // Tentatively accept, potentially exposing us to DoS attacks
1080 &Some(ref chain_access) => {
1081 match chain_access.get_utxo(&msg.chain_hash, msg.short_channel_id) {
1082 Ok(TxOut { value, script_pubkey }) => {
1083 let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
1084 .push_slice(&msg.bitcoin_key_1.serialize())
1085 .push_slice(&msg.bitcoin_key_2.serialize())
1086 .push_opcode(opcodes::all::OP_PUSHNUM_2)
1087 .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
1088 if script_pubkey != expected_script {
1089 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});
1091 //TODO: Check if value is worth storing, use it to inform routing, and compare it
1092 //to the new HTLC max field in channel_update
1095 Err(chain::AccessError::UnknownChain) => {
1096 return Err(LightningError{err: format!("Channel announced on an unknown chain ({})", msg.chain_hash.encode().to_hex()), action: ErrorAction::IgnoreError});
1098 Err(chain::AccessError::UnknownTx) => {
1099 return Err(LightningError{err: "Channel announced without corresponding UTXO entry".to_owned(), action: ErrorAction::IgnoreError});
1105 let chan_info = ChannelInfo {
1106 features: msg.features.clone(),
1107 node_one: msg.node_id_1.clone(),
1109 node_two: msg.node_id_2.clone(),
1111 capacity_sats: utxo_value,
1112 announcement_message: if msg.excess_data.is_empty() { full_msg.cloned() } else { None },
1115 match self.channels.entry(msg.short_channel_id) {
1116 BtreeEntry::Occupied(mut entry) => {
1117 //TODO: because asking the blockchain if short_channel_id is valid is only optional
1118 //in the blockchain API, we need to handle it smartly here, though it's unclear
1120 if utxo_value.is_some() {
1121 // Either our UTXO provider is busted, there was a reorg, or the UTXO provider
1122 // only sometimes returns results. In any case remove the previous entry. Note
1123 // that the spec expects us to "blacklist" the node_ids involved, but we can't
1125 // a) we don't *require* a UTXO provider that always returns results.
1126 // b) we don't track UTXOs of channels we know about and remove them if they
1128 // c) it's unclear how to do so without exposing ourselves to massive DoS risk.
1129 Self::remove_channel_in_nodes(&mut self.nodes, &entry.get(), msg.short_channel_id);
1130 *entry.get_mut() = chan_info;
1132 return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreError})
1135 BtreeEntry::Vacant(entry) => {
1136 entry.insert(chan_info);
1140 macro_rules! add_channel_to_node {
1141 ( $node_id: expr ) => {
1142 match self.nodes.entry($node_id) {
1143 BtreeEntry::Occupied(node_entry) => {
1144 node_entry.into_mut().channels.push(msg.short_channel_id);
1146 BtreeEntry::Vacant(node_entry) => {
1147 node_entry.insert(NodeInfo {
1148 channels: vec!(msg.short_channel_id),
1149 lowest_inbound_channel_fees: None,
1150 announcement_info: None,
1157 add_channel_to_node!(msg.node_id_1);
1158 add_channel_to_node!(msg.node_id_2);
1163 /// Close a channel if a corresponding HTLC fail was sent.
1164 /// If permanent, removes a channel from the local storage.
1165 /// May cause the removal of nodes too, if this was their last channel.
1166 /// If not permanent, makes channels unavailable for routing.
1167 pub fn close_channel_from_update(&mut self, short_channel_id: u64, is_permanent: bool) {
1169 if let Some(chan) = self.channels.remove(&short_channel_id) {
1170 Self::remove_channel_in_nodes(&mut self.nodes, &chan, short_channel_id);
1173 if let Some(chan) = self.channels.get_mut(&short_channel_id) {
1174 if let Some(one_to_two) = chan.one_to_two.as_mut() {
1175 one_to_two.enabled = false;
1177 if let Some(two_to_one) = chan.two_to_one.as_mut() {
1178 two_to_one.enabled = false;
1184 fn fail_node(&mut self, _node_id: &PublicKey, is_permanent: bool) {
1186 // TODO: Wholly remove the node
1188 // TODO: downgrade the node
1192 /// For an already known (from announcement) channel, update info about one of the directions
1195 /// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
1196 /// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
1197 /// routing messages from a source using a protocol other than the lightning P2P protocol.
1198 pub fn update_channel<T: secp256k1::Verification>(&mut self, msg: &msgs::ChannelUpdate, secp_ctx: &Secp256k1<T>) -> Result<(), LightningError> {
1199 self.update_channel_intern(&msg.contents, Some(&msg), Some((&msg.signature, secp_ctx)))
1202 /// For an already known (from announcement) channel, update info about one of the directions
1203 /// of the channel without verifying the associated signatures. Because we aren't given the
1204 /// associated signatures here we cannot relay the channel update to any of our peers.
1205 pub fn update_channel_unsigned(&mut self, msg: &msgs::UnsignedChannelUpdate) -> Result<(), LightningError> {
1206 self.update_channel_intern(msg, None, None::<(&secp256k1::Signature, &Secp256k1<secp256k1::VerifyOnly>)>)
1209 fn update_channel_intern<T: secp256k1::Verification>(&mut self, msg: &msgs::UnsignedChannelUpdate, full_msg: Option<&msgs::ChannelUpdate>, sig_info: Option<(&secp256k1::Signature, &Secp256k1<T>)>) -> Result<(), LightningError> {
1211 let chan_enabled = msg.flags & (1 << 1) != (1 << 1);
1212 let chan_was_enabled;
1214 match self.channels.get_mut(&msg.short_channel_id) {
1215 None => return Err(LightningError{err: "Couldn't find channel for update".to_owned(), action: ErrorAction::IgnoreError}),
1217 if let OptionalField::Present(htlc_maximum_msat) = msg.htlc_maximum_msat {
1218 if htlc_maximum_msat > MAX_VALUE_MSAT {
1219 return Err(LightningError{err: "htlc_maximum_msat is larger than maximum possible msats".to_owned(), action: ErrorAction::IgnoreError});
1222 if let Some(capacity_sats) = channel.capacity_sats {
1223 // It's possible channel capacity is available now, although it wasn't available at announcement (so the field is None).
1224 // Don't query UTXO set here to reduce DoS risks.
1225 if capacity_sats > MAX_VALUE_MSAT / 1000 || htlc_maximum_msat > capacity_sats * 1000 {
1226 return Err(LightningError{err: "htlc_maximum_msat is larger than channel capacity or capacity is bogus".to_owned(), action: ErrorAction::IgnoreError});
1230 macro_rules! maybe_update_channel_info {
1231 ( $target: expr, $src_node: expr) => {
1232 if let Some(existing_chan_info) = $target.as_ref() {
1233 if existing_chan_info.last_update >= msg.timestamp {
1234 return Err(LightningError{err: "Update older than last processed update".to_owned(), action: ErrorAction::IgnoreError});
1236 chan_was_enabled = existing_chan_info.enabled;
1238 chan_was_enabled = false;
1241 let last_update_message = if msg.excess_data.is_empty() { full_msg.cloned() } else { None };
1243 let updated_channel_dir_info = DirectionalChannelInfo {
1244 enabled: chan_enabled,
1245 last_update: msg.timestamp,
1246 cltv_expiry_delta: msg.cltv_expiry_delta,
1247 htlc_minimum_msat: msg.htlc_minimum_msat,
1248 htlc_maximum_msat: if let OptionalField::Present(max_value) = msg.htlc_maximum_msat { Some(max_value) } else { None },
1250 base_msat: msg.fee_base_msat,
1251 proportional_millionths: msg.fee_proportional_millionths,
1255 $target = Some(updated_channel_dir_info);
1259 let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
1260 if msg.flags & 1 == 1 {
1261 dest_node_id = channel.node_one.clone();
1262 if let Some((sig, ctx)) = sig_info {
1263 secp_verify_sig!(ctx, &msg_hash, &sig, &channel.node_two);
1265 maybe_update_channel_info!(channel.two_to_one, channel.node_two);
1267 dest_node_id = channel.node_two.clone();
1268 if let Some((sig, ctx)) = sig_info {
1269 secp_verify_sig!(ctx, &msg_hash, &sig, &channel.node_one);
1271 maybe_update_channel_info!(channel.one_to_two, channel.node_one);
1277 let node = self.nodes.get_mut(&dest_node_id).unwrap();
1278 let mut base_msat = msg.fee_base_msat;
1279 let mut proportional_millionths = msg.fee_proportional_millionths;
1280 if let Some(fees) = node.lowest_inbound_channel_fees {
1281 base_msat = cmp::min(base_msat, fees.base_msat);
1282 proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths);
1284 node.lowest_inbound_channel_fees = Some(RoutingFees {
1286 proportional_millionths
1288 } else if chan_was_enabled {
1289 let node = self.nodes.get_mut(&dest_node_id).unwrap();
1290 let mut lowest_inbound_channel_fees = None;
1292 for chan_id in node.channels.iter() {
1293 let chan = self.channels.get(chan_id).unwrap();
1295 if chan.node_one == dest_node_id {
1296 chan_info_opt = chan.two_to_one.as_ref();
1298 chan_info_opt = chan.one_to_two.as_ref();
1300 if let Some(chan_info) = chan_info_opt {
1301 if chan_info.enabled {
1302 let fees = lowest_inbound_channel_fees.get_or_insert(RoutingFees {
1303 base_msat: u32::max_value(), proportional_millionths: u32::max_value() });
1304 fees.base_msat = cmp::min(fees.base_msat, chan_info.fees.base_msat);
1305 fees.proportional_millionths = cmp::min(fees.proportional_millionths, chan_info.fees.proportional_millionths);
1310 node.lowest_inbound_channel_fees = lowest_inbound_channel_fees;
1316 fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
1317 macro_rules! remove_from_node {
1318 ($node_id: expr) => {
1319 if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
1320 entry.get_mut().channels.retain(|chan_id| {
1321 short_channel_id != *chan_id
1323 if entry.get().channels.is_empty() {
1324 entry.remove_entry();
1327 panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
1332 remove_from_node!(chan.node_one);
1333 remove_from_node!(chan.node_two);
1340 use ln::features::{ChannelFeatures, NodeFeatures};
1341 use routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
1342 use ln::msgs::{OptionalField, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
1343 UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate,
1344 ReplyChannelRange, ReplyShortChannelIdsEnd, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
1345 use util::test_utils;
1346 use util::logger::Logger;
1347 use util::ser::{Readable, Writeable};
1348 use util::events::{MessageSendEvent, MessageSendEventsProvider};
1350 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
1351 use bitcoin::hashes::Hash;
1352 use bitcoin::network::constants::Network;
1353 use bitcoin::blockdata::constants::genesis_block;
1354 use bitcoin::blockdata::script::Builder;
1355 use bitcoin::blockdata::transaction::TxOut;
1356 use bitcoin::blockdata::opcodes;
1360 use bitcoin::secp256k1::key::{PublicKey, SecretKey};
1361 use bitcoin::secp256k1::{All, Secp256k1};
1365 fn create_net_graph_msg_handler() -> (Secp256k1<All>, NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>) {
1366 let secp_ctx = Secp256k1::new();
1367 let logger = Arc::new(test_utils::TestLogger::new());
1368 let net_graph_msg_handler = NetGraphMsgHandler::new(None, Arc::clone(&logger));
1369 (secp_ctx, net_graph_msg_handler)
1373 fn request_full_sync_finite_times() {
1374 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1375 let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
1377 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1378 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1379 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1380 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1381 assert!(net_graph_msg_handler.should_request_full_sync(&node_id));
1382 assert!(!net_graph_msg_handler.should_request_full_sync(&node_id));
1386 fn handling_node_announcements() {
1387 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1389 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1390 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1391 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1392 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1393 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1394 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1395 let zero_hash = Sha256dHash::hash(&[0; 32]);
1396 let first_announcement_time = 500;
1398 let mut unsigned_announcement = UnsignedNodeAnnouncement {
1399 features: NodeFeatures::known(),
1400 timestamp: first_announcement_time,
1404 addresses: Vec::new(),
1405 excess_address_data: Vec::new(),
1406 excess_data: Vec::new(),
1408 let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1409 let valid_announcement = NodeAnnouncement {
1410 signature: secp_ctx.sign(&msghash, node_1_privkey),
1411 contents: unsigned_announcement.clone()
1414 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1416 Err(e) => assert_eq!("No existing channels for node_announcement", e.err)
1420 // Announce a channel to add a corresponding node.
1421 let unsigned_announcement = UnsignedChannelAnnouncement {
1422 features: ChannelFeatures::known(),
1423 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
1424 short_channel_id: 0,
1427 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1428 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1429 excess_data: Vec::new(),
1432 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1433 let valid_announcement = ChannelAnnouncement {
1434 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1435 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1436 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1437 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1438 contents: unsigned_announcement.clone(),
1440 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1441 Ok(res) => assert!(res),
1446 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
1447 Ok(res) => assert!(res),
1451 let fake_msghash = hash_to_message!(&zero_hash);
1452 match net_graph_msg_handler.handle_node_announcement(
1454 signature: secp_ctx.sign(&fake_msghash, node_1_privkey),
1455 contents: unsigned_announcement.clone()
1458 Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1461 unsigned_announcement.timestamp += 1000;
1462 unsigned_announcement.excess_data.push(1);
1463 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1464 let announcement_with_data = NodeAnnouncement {
1465 signature: secp_ctx.sign(&msghash, node_1_privkey),
1466 contents: unsigned_announcement.clone()
1468 // Return false because contains excess data.
1469 match net_graph_msg_handler.handle_node_announcement(&announcement_with_data) {
1470 Ok(res) => assert!(!res),
1473 unsigned_announcement.excess_data = Vec::new();
1475 // Even though previous announcement was not relayed further, we still accepted it,
1476 // so we now won't accept announcements before the previous one.
1477 unsigned_announcement.timestamp -= 10;
1478 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1479 let outdated_announcement = NodeAnnouncement {
1480 signature: secp_ctx.sign(&msghash, node_1_privkey),
1481 contents: unsigned_announcement.clone()
1483 match net_graph_msg_handler.handle_node_announcement(&outdated_announcement) {
1485 Err(e) => assert_eq!(e.err, "Update older than last processed update")
1490 fn handling_channel_announcements() {
1491 let secp_ctx = Secp256k1::new();
1492 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1494 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1495 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1496 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1497 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1498 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1499 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1501 let good_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
1502 .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_1_btckey).serialize())
1503 .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_2_btckey).serialize())
1504 .push_opcode(opcodes::all::OP_PUSHNUM_2)
1505 .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
1508 let mut unsigned_announcement = UnsignedChannelAnnouncement {
1509 features: ChannelFeatures::known(),
1510 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
1511 short_channel_id: 0,
1514 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1515 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1516 excess_data: Vec::new(),
1519 let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1520 let valid_announcement = ChannelAnnouncement {
1521 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1522 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1523 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1524 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1525 contents: unsigned_announcement.clone(),
1528 // Test if the UTXO lookups were not supported
1529 let mut net_graph_msg_handler = NetGraphMsgHandler::new(None, Arc::clone(&logger));
1530 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1531 Ok(res) => assert!(res),
1536 let network = net_graph_msg_handler.network_graph.read().unwrap();
1537 match network.get_channels().get(&unsigned_announcement.short_channel_id) {
1543 // If we receive announcement for the same channel (with UTXO lookups disabled),
1544 // drop new one on the floor, since we can't see any changes.
1545 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1547 Err(e) => assert_eq!(e.err, "Already have knowledge of channel")
1550 // Test if an associated transaction were not on-chain (or not confirmed).
1551 let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
1552 *chain_source.utxo_ret.lock().unwrap() = Err(chain::AccessError::UnknownTx);
1553 net_graph_msg_handler = NetGraphMsgHandler::new(Some(chain_source.clone()), Arc::clone(&logger));
1554 unsigned_announcement.short_channel_id += 1;
1556 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1557 let valid_announcement = ChannelAnnouncement {
1558 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1559 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1560 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1561 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1562 contents: unsigned_announcement.clone(),
1565 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1567 Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry")
1570 // Now test if the transaction is found in the UTXO set and the script is correct.
1571 unsigned_announcement.short_channel_id += 1;
1572 *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script.clone() });
1574 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1575 let valid_announcement = ChannelAnnouncement {
1576 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1577 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1578 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1579 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1580 contents: unsigned_announcement.clone(),
1582 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1583 Ok(res) => assert!(res),
1588 let network = net_graph_msg_handler.network_graph.read().unwrap();
1589 match network.get_channels().get(&unsigned_announcement.short_channel_id) {
1595 // If we receive announcement for the same channel (but TX is not confirmed),
1596 // drop new one on the floor, since we can't see any changes.
1597 *chain_source.utxo_ret.lock().unwrap() = Err(chain::AccessError::UnknownTx);
1598 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1600 Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry")
1603 // But if it is confirmed, replace the channel
1604 *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script });
1605 unsigned_announcement.features = ChannelFeatures::empty();
1606 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1607 let valid_announcement = ChannelAnnouncement {
1608 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1609 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1610 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1611 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1612 contents: unsigned_announcement.clone(),
1614 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1615 Ok(res) => assert!(res),
1619 let network = net_graph_msg_handler.network_graph.read().unwrap();
1620 match network.get_channels().get(&unsigned_announcement.short_channel_id) {
1621 Some(channel_entry) => {
1622 assert_eq!(channel_entry.features, ChannelFeatures::empty());
1628 // Don't relay valid channels with excess data
1629 unsigned_announcement.short_channel_id += 1;
1630 unsigned_announcement.excess_data.push(1);
1631 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1632 let valid_announcement = ChannelAnnouncement {
1633 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1634 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1635 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1636 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1637 contents: unsigned_announcement.clone(),
1639 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
1640 Ok(res) => assert!(!res),
1644 unsigned_announcement.excess_data = Vec::new();
1645 let invalid_sig_announcement = ChannelAnnouncement {
1646 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1647 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1648 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1649 bitcoin_signature_2: secp_ctx.sign(&msghash, node_1_btckey),
1650 contents: unsigned_announcement.clone(),
1652 match net_graph_msg_handler.handle_channel_announcement(&invalid_sig_announcement) {
1654 Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1657 unsigned_announcement.node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1658 msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1659 let channel_to_itself_announcement = ChannelAnnouncement {
1660 node_signature_1: secp_ctx.sign(&msghash, node_2_privkey),
1661 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1662 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1663 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1664 contents: unsigned_announcement.clone(),
1666 match net_graph_msg_handler.handle_channel_announcement(&channel_to_itself_announcement) {
1668 Err(e) => assert_eq!(e.err, "Channel announcement node had a channel with itself")
1673 fn handling_channel_update() {
1674 let secp_ctx = Secp256k1::new();
1675 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
1676 let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
1677 let net_graph_msg_handler = NetGraphMsgHandler::new(Some(chain_source.clone()), Arc::clone(&logger));
1679 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1680 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1681 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1682 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1683 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1684 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1686 let zero_hash = Sha256dHash::hash(&[0; 32]);
1687 let short_channel_id = 0;
1688 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
1689 let amount_sats = 1000_000;
1692 // Announce a channel we will update
1693 let good_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2)
1694 .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_1_btckey).serialize())
1695 .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_2_btckey).serialize())
1696 .push_opcode(opcodes::all::OP_PUSHNUM_2)
1697 .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
1698 *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() });
1699 let unsigned_announcement = UnsignedChannelAnnouncement {
1700 features: ChannelFeatures::empty(),
1705 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1706 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1707 excess_data: Vec::new(),
1710 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1711 let valid_channel_announcement = ChannelAnnouncement {
1712 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1713 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1714 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1715 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1716 contents: unsigned_announcement.clone(),
1718 match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1725 let mut unsigned_channel_update = UnsignedChannelUpdate {
1730 cltv_expiry_delta: 144,
1731 htlc_minimum_msat: 1000000,
1732 htlc_maximum_msat: OptionalField::Absent,
1733 fee_base_msat: 10000,
1734 fee_proportional_millionths: 20,
1735 excess_data: Vec::new()
1737 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1738 let valid_channel_update = ChannelUpdate {
1739 signature: secp_ctx.sign(&msghash, node_1_privkey),
1740 contents: unsigned_channel_update.clone()
1743 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1744 Ok(res) => assert!(res),
1749 let network = net_graph_msg_handler.network_graph.read().unwrap();
1750 match network.get_channels().get(&short_channel_id) {
1752 Some(channel_info) => {
1753 assert_eq!(channel_info.one_to_two.as_ref().unwrap().cltv_expiry_delta, 144);
1754 assert!(channel_info.two_to_one.is_none());
1759 unsigned_channel_update.timestamp += 100;
1760 unsigned_channel_update.excess_data.push(1);
1761 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1762 let valid_channel_update = ChannelUpdate {
1763 signature: secp_ctx.sign(&msghash, node_1_privkey),
1764 contents: unsigned_channel_update.clone()
1766 // Return false because contains excess data
1767 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1768 Ok(res) => assert!(!res),
1771 unsigned_channel_update.timestamp += 10;
1773 unsigned_channel_update.short_channel_id += 1;
1774 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1775 let valid_channel_update = ChannelUpdate {
1776 signature: secp_ctx.sign(&msghash, node_1_privkey),
1777 contents: unsigned_channel_update.clone()
1780 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1782 Err(e) => assert_eq!(e.err, "Couldn't find channel for update")
1784 unsigned_channel_update.short_channel_id = short_channel_id;
1786 unsigned_channel_update.htlc_maximum_msat = OptionalField::Present(MAX_VALUE_MSAT + 1);
1787 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1788 let valid_channel_update = ChannelUpdate {
1789 signature: secp_ctx.sign(&msghash, node_1_privkey),
1790 contents: unsigned_channel_update.clone()
1793 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1795 Err(e) => assert_eq!(e.err, "htlc_maximum_msat is larger than maximum possible msats")
1797 unsigned_channel_update.htlc_maximum_msat = OptionalField::Absent;
1799 unsigned_channel_update.htlc_maximum_msat = OptionalField::Present(amount_sats * 1000 + 1);
1800 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1801 let valid_channel_update = ChannelUpdate {
1802 signature: secp_ctx.sign(&msghash, node_1_privkey),
1803 contents: unsigned_channel_update.clone()
1806 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1808 Err(e) => assert_eq!(e.err, "htlc_maximum_msat is larger than channel capacity or capacity is bogus")
1810 unsigned_channel_update.htlc_maximum_msat = OptionalField::Absent;
1812 // Even though previous update was not relayed further, we still accepted it,
1813 // so we now won't accept update before the previous one.
1814 unsigned_channel_update.timestamp -= 10;
1815 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1816 let valid_channel_update = ChannelUpdate {
1817 signature: secp_ctx.sign(&msghash, node_1_privkey),
1818 contents: unsigned_channel_update.clone()
1821 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1823 Err(e) => assert_eq!(e.err, "Update older than last processed update")
1825 unsigned_channel_update.timestamp += 500;
1827 let fake_msghash = hash_to_message!(&zero_hash);
1828 let invalid_sig_channel_update = ChannelUpdate {
1829 signature: secp_ctx.sign(&fake_msghash, node_1_privkey),
1830 contents: unsigned_channel_update.clone()
1833 match net_graph_msg_handler.handle_channel_update(&invalid_sig_channel_update) {
1835 Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1841 fn handling_htlc_fail_channel_update() {
1842 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1843 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1844 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1845 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1846 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1847 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1848 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1850 let short_channel_id = 0;
1851 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
1854 // There is no nodes in the table at the beginning.
1855 let network = net_graph_msg_handler.network_graph.read().unwrap();
1856 assert_eq!(network.get_nodes().len(), 0);
1860 // Announce a channel we will update
1861 let unsigned_announcement = UnsignedChannelAnnouncement {
1862 features: ChannelFeatures::empty(),
1867 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1868 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1869 excess_data: Vec::new(),
1872 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1873 let valid_channel_announcement = ChannelAnnouncement {
1874 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1875 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1876 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1877 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1878 contents: unsigned_announcement.clone(),
1880 match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1885 let unsigned_channel_update = UnsignedChannelUpdate {
1890 cltv_expiry_delta: 144,
1891 htlc_minimum_msat: 1000000,
1892 htlc_maximum_msat: OptionalField::Absent,
1893 fee_base_msat: 10000,
1894 fee_proportional_millionths: 20,
1895 excess_data: Vec::new()
1897 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
1898 let valid_channel_update = ChannelUpdate {
1899 signature: secp_ctx.sign(&msghash, node_1_privkey),
1900 contents: unsigned_channel_update.clone()
1903 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
1904 Ok(res) => assert!(res),
1909 // Non-permanent closing just disables a channel
1911 let network = net_graph_msg_handler.network_graph.read().unwrap();
1912 match network.get_channels().get(&short_channel_id) {
1914 Some(channel_info) => {
1915 assert!(channel_info.one_to_two.is_some());
1920 let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
1925 net_graph_msg_handler.handle_htlc_fail_channel_update(&channel_close_msg);
1927 // Non-permanent closing just disables a channel
1929 let network = net_graph_msg_handler.network_graph.read().unwrap();
1930 match network.get_channels().get(&short_channel_id) {
1932 Some(channel_info) => {
1933 assert!(!channel_info.one_to_two.as_ref().unwrap().enabled);
1938 let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
1943 net_graph_msg_handler.handle_htlc_fail_channel_update(&channel_close_msg);
1945 // Permanent closing deletes a channel
1947 let network = net_graph_msg_handler.network_graph.read().unwrap();
1948 assert_eq!(network.get_channels().len(), 0);
1949 // Nodes are also deleted because there are no associated channels anymore
1950 assert_eq!(network.get_nodes().len(), 0);
1952 // TODO: Test HTLCFailChannelUpdate::NodeFailure, which is not implemented yet.
1956 fn getting_next_channel_announcements() {
1957 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
1958 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
1959 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
1960 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
1961 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
1962 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
1963 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
1965 let short_channel_id = 1;
1966 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
1968 // Channels were not announced yet.
1969 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(0, 1);
1970 assert_eq!(channels_with_announcements.len(), 0);
1973 // Announce a channel we will update
1974 let unsigned_announcement = UnsignedChannelAnnouncement {
1975 features: ChannelFeatures::empty(),
1980 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
1981 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
1982 excess_data: Vec::new(),
1985 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1986 let valid_channel_announcement = ChannelAnnouncement {
1987 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
1988 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
1989 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
1990 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
1991 contents: unsigned_announcement.clone(),
1993 match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
1999 // Contains initial channel announcement now.
2000 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1);
2001 assert_eq!(channels_with_announcements.len(), 1);
2002 if let Some(channel_announcements) = channels_with_announcements.first() {
2003 let &(_, ref update_1, ref update_2) = channel_announcements;
2004 assert_eq!(update_1, &None);
2005 assert_eq!(update_2, &None);
2012 // Valid channel update
2013 let unsigned_channel_update = UnsignedChannelUpdate {
2018 cltv_expiry_delta: 144,
2019 htlc_minimum_msat: 1000000,
2020 htlc_maximum_msat: OptionalField::Absent,
2021 fee_base_msat: 10000,
2022 fee_proportional_millionths: 20,
2023 excess_data: Vec::new()
2025 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
2026 let valid_channel_update = ChannelUpdate {
2027 signature: secp_ctx.sign(&msghash, node_1_privkey),
2028 contents: unsigned_channel_update.clone()
2030 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
2036 // Now contains an initial announcement and an update.
2037 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1);
2038 assert_eq!(channels_with_announcements.len(), 1);
2039 if let Some(channel_announcements) = channels_with_announcements.first() {
2040 let &(_, ref update_1, ref update_2) = channel_announcements;
2041 assert_ne!(update_1, &None);
2042 assert_eq!(update_2, &None);
2049 // Channel update with excess data.
2050 let unsigned_channel_update = UnsignedChannelUpdate {
2055 cltv_expiry_delta: 144,
2056 htlc_minimum_msat: 1000000,
2057 htlc_maximum_msat: OptionalField::Absent,
2058 fee_base_msat: 10000,
2059 fee_proportional_millionths: 20,
2060 excess_data: [1; 3].to_vec()
2062 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]);
2063 let valid_channel_update = ChannelUpdate {
2064 signature: secp_ctx.sign(&msghash, node_1_privkey),
2065 contents: unsigned_channel_update.clone()
2067 match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
2073 // Test that announcements with excess data won't be returned
2074 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1);
2075 assert_eq!(channels_with_announcements.len(), 1);
2076 if let Some(channel_announcements) = channels_with_announcements.first() {
2077 let &(_, ref update_1, ref update_2) = channel_announcements;
2078 assert_eq!(update_1, &None);
2079 assert_eq!(update_2, &None);
2084 // Further starting point have no channels after it
2085 let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id + 1000, 1);
2086 assert_eq!(channels_with_announcements.len(), 0);
2090 fn getting_next_node_announcements() {
2091 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2092 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
2093 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2094 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2095 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
2096 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
2097 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
2099 let short_channel_id = 1;
2100 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2103 let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 10);
2104 assert_eq!(next_announcements.len(), 0);
2107 // Announce a channel to add 2 nodes
2108 let unsigned_announcement = UnsignedChannelAnnouncement {
2109 features: ChannelFeatures::empty(),
2114 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
2115 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
2116 excess_data: Vec::new(),
2119 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2120 let valid_channel_announcement = ChannelAnnouncement {
2121 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2122 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2123 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2124 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2125 contents: unsigned_announcement.clone(),
2127 match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) {
2134 // Nodes were never announced
2135 let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 3);
2136 assert_eq!(next_announcements.len(), 0);
2139 let mut unsigned_announcement = UnsignedNodeAnnouncement {
2140 features: NodeFeatures::known(),
2145 addresses: Vec::new(),
2146 excess_address_data: Vec::new(),
2147 excess_data: Vec::new(),
2149 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2150 let valid_announcement = NodeAnnouncement {
2151 signature: secp_ctx.sign(&msghash, node_1_privkey),
2152 contents: unsigned_announcement.clone()
2154 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
2159 unsigned_announcement.node_id = node_id_2;
2160 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2161 let valid_announcement = NodeAnnouncement {
2162 signature: secp_ctx.sign(&msghash, node_2_privkey),
2163 contents: unsigned_announcement.clone()
2166 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
2172 let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 3);
2173 assert_eq!(next_announcements.len(), 2);
2175 // Skip the first node.
2176 let next_announcements = net_graph_msg_handler.get_next_node_announcements(Some(&node_id_1), 2);
2177 assert_eq!(next_announcements.len(), 1);
2180 // Later announcement which should not be relayed (excess data) prevent us from sharing a node
2181 let unsigned_announcement = UnsignedNodeAnnouncement {
2182 features: NodeFeatures::known(),
2187 addresses: Vec::new(),
2188 excess_address_data: Vec::new(),
2189 excess_data: [1; 3].to_vec(),
2191 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2192 let valid_announcement = NodeAnnouncement {
2193 signature: secp_ctx.sign(&msghash, node_2_privkey),
2194 contents: unsigned_announcement.clone()
2196 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
2197 Ok(res) => assert!(!res),
2202 let next_announcements = net_graph_msg_handler.get_next_node_announcements(Some(&node_id_1), 2);
2203 assert_eq!(next_announcements.len(), 0);
2207 fn network_graph_serialization() {
2208 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2210 let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
2211 let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2212 let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
2213 let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
2215 // Announce a channel to add a corresponding node.
2216 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2217 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
2218 let unsigned_announcement = UnsignedChannelAnnouncement {
2219 features: ChannelFeatures::known(),
2220 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
2221 short_channel_id: 0,
2224 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
2225 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
2226 excess_data: Vec::new(),
2229 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2230 let valid_announcement = ChannelAnnouncement {
2231 node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2232 node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2233 bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2234 bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2235 contents: unsigned_announcement.clone(),
2237 match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
2238 Ok(res) => assert!(res),
2243 let node_id = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2244 let unsigned_announcement = UnsignedNodeAnnouncement {
2245 features: NodeFeatures::known(),
2250 addresses: Vec::new(),
2251 excess_address_data: Vec::new(),
2252 excess_data: Vec::new(),
2254 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2255 let valid_announcement = NodeAnnouncement {
2256 signature: secp_ctx.sign(&msghash, node_1_privkey),
2257 contents: unsigned_announcement.clone()
2260 match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
2265 let network = net_graph_msg_handler.network_graph.write().unwrap();
2266 let mut w = test_utils::TestVecWriter(Vec::new());
2267 assert!(!network.get_nodes().is_empty());
2268 assert!(!network.get_channels().is_empty());
2269 network.write(&mut w).unwrap();
2270 assert!(<NetworkGraph>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);
2274 fn sending_query_channel_range() {
2275 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2276 let node_privkey_1 = &SecretKey::from_slice(&[42; 32]).unwrap();
2277 let node_privkey_2 = &SecretKey::from_slice(&[41; 32]).unwrap();
2278 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_privkey_1);
2279 let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_privkey_2);
2281 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2282 let first_blocknum = 0;
2283 let number_of_blocks = 0xffff_ffff;
2285 // When no active query exists for the node, it should send a query message and generate a task
2287 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, first_blocknum, number_of_blocks);
2288 assert!(result.is_ok());
2290 // It should create a task for the query
2291 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().contains_key(&node_id_1));
2293 // It should send a query_channel_range message with the correct information
2294 let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2295 assert_eq!(events.len(), 1);
2297 MessageSendEvent::SendChannelRangeQuery{ node_id, msg } => {
2298 assert_eq!(node_id, &node_id_1);
2299 assert_eq!(msg.chain_hash, chain_hash);
2300 assert_eq!(msg.first_blocknum, first_blocknum);
2301 assert_eq!(msg.number_of_blocks, number_of_blocks);
2303 _ => panic!("Expected MessageSendEvent::SendChannelRangeQuery")
2307 // When an active query exists for the node, when there is a subsequent query request, it
2308 // should fail to initiate a new query
2310 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, first_blocknum, number_of_blocks);
2311 assert_eq!(result.is_err(), true);
2314 // When no active query exists for a different node, it should send a query message
2316 let result = net_graph_msg_handler.query_channel_range(&node_id_2, chain_hash, first_blocknum, number_of_blocks);
2317 assert_eq!(result.is_ok(), true);
2319 // It should create a task for the query
2320 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().contains_key(&node_id_2));
2322 // It should send a query_channel_message with the correct information
2323 let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2324 assert_eq!(events.len(), 1);
2326 MessageSendEvent::SendChannelRangeQuery{ node_id, msg } => {
2327 assert_eq!(node_id, &node_id_2);
2328 assert_eq!(msg.chain_hash, chain_hash);
2329 assert_eq!(msg.first_blocknum, first_blocknum);
2330 assert_eq!(msg.number_of_blocks, number_of_blocks);
2332 _ => panic!("Expected MessageSendEvent::SendChannelRangeQuery")
2338 fn sending_query_short_channel_ids() {
2339 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2340 let node_privkey_1 = &SecretKey::from_slice(&[42; 32]).unwrap();
2341 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_privkey_1);
2343 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2345 // The first query should send the batch of scids to the peer
2347 let short_channel_ids: Vec<u64> = vec![0, 1, 2];
2348 let result = net_graph_msg_handler.query_short_channel_ids(&node_id_1, chain_hash, short_channel_ids.clone());
2349 assert!(result.is_ok());
2351 // Validate that we have enqueued a send message event and that it contains the correct information
2352 let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2353 assert_eq!(events.len(), 1);
2355 MessageSendEvent::SendShortIdsQuery{ node_id, msg } => {
2356 assert_eq!(node_id, &node_id_1);
2357 assert_eq!(msg.chain_hash, chain_hash);
2358 assert_eq!(msg.short_channel_ids, short_channel_ids);
2360 _ => panic!("Expected MessageSendEvent::SendShortIdsQuery")
2364 // Subsequent queries for scids should enqueue them to be sent in the next batch which will
2365 // be sent when a reply_short_channel_ids_end message is handled.
2367 let short_channel_ids: Vec<u64> = vec![3, 4, 5];
2368 let result = net_graph_msg_handler.query_short_channel_ids(&node_id_1, chain_hash, short_channel_ids.clone());
2369 assert!(result.is_ok());
2371 // Validate that we have not enqueued another send message event yet
2372 let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2373 assert_eq!(events.len(), 0);
2375 // Validate the task has the queued scids
2377 net_graph_msg_handler.scid_query_tasks.lock().unwrap().get(&node_id_1).unwrap().short_channel_ids,
2384 fn handling_reply_channel_range() {
2385 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2386 let node_privkey_1 = &SecretKey::from_slice(&[42; 32]).unwrap();
2387 let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_privkey_1);
2389 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2391 // Test receipt of an unknown reply message. We expect an error
2393 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2395 full_information: true,
2396 first_blocknum: 1000,
2397 number_of_blocks: 1050,
2398 short_channel_ids: vec![
2399 0x0003e8_000000_0000, // 1000x0x0
2400 0x0003e9_000000_0000, // 1001x0x0
2401 0x0003f0_000000_0000 // 1008x0x0
2404 assert!(result.is_err());
2407 // Test receipt of a single reply_channel_range that exactly matches the queried range.
2408 // It sends a query_short_channel_ids with the returned scids and removes the pending task
2410 // Initiate a channel range query to create a query task
2411 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2412 assert!(result.is_ok());
2414 // Clear the SendRangeQuery event
2415 net_graph_msg_handler.get_and_clear_pending_msg_events();
2417 // Handle a single successful reply that matches the queried channel range
2418 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2420 full_information: true,
2421 first_blocknum: 1000,
2422 number_of_blocks: 100,
2423 short_channel_ids: vec![
2424 0x0003e8_000000_0000, // 1000x0x0
2425 0x0003e9_000000_0000, // 1001x0x0
2426 0x0003f0_000000_0000 // 1008x0x0
2429 assert!(result.is_ok());
2431 // The query is now complete, so we expect the task to be removed
2432 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2434 // We expect to emit a query_short_channel_ids message with scids in our query range
2435 let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2436 assert_eq!(events.len(), 1);
2438 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2439 assert_eq!(node_id, &node_id_1);
2440 assert_eq!(msg.chain_hash, chain_hash);
2441 assert_eq!(msg.short_channel_ids, vec![0x0003e8_000000_0000,0x0003e9_000000_0000,0x0003f0_000000_0000]);
2443 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2446 // Clean up scid_task
2447 net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
2450 // Test receipt of a single reply_channel_range for a query that has a u32 overflow. We expect
2451 // it sends a query_short_channel_ids with the returned scids and removes the pending task.
2453 // Initiate a channel range query to create a query task
2454 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 0xffff_ffff);
2455 assert!(result.is_ok());
2457 // Clear the SendRangeQuery event
2458 net_graph_msg_handler.get_and_clear_pending_msg_events();
2460 // Handle a single successful reply that matches the queried channel range
2461 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2463 full_information: true,
2464 first_blocknum: 1000,
2465 number_of_blocks: 0xffff_ffff,
2466 short_channel_ids: vec![
2467 0x0003e8_000000_0000, // 1000x0x0
2468 0x0003e9_000000_0000, // 1001x0x0
2469 0x0003f0_000000_0000 // 1008x0x0
2472 assert!(result.is_ok());
2474 // The query is now complete, so we expect the task to be removed
2475 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2477 // We expect to emit a query_short_channel_ids message with scids in our query range
2478 let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2479 assert_eq!(events.len(), 1);
2481 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2482 assert_eq!(node_id, &node_id_1);
2483 assert_eq!(msg.chain_hash, chain_hash);
2484 assert_eq!(msg.short_channel_ids, vec![0x0003e8_000000_0000,0x0003e9_000000_0000,0x0003f0_000000_0000]);
2486 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2489 // Clean up scid_task
2490 net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
2493 // Test receipt of a single reply that encompasses the queried channel range. This is allowed
2494 // since a reply must contain at least part of the query range. Receipt of the reply should
2495 // send a query_short_channel_ids message with scids filtered to the query range and remove
2496 // the pending task.
2498 // Initiate a channel range query to create a query task
2499 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2500 assert!(result.is_ok());
2502 // Clear the SendRangeQuery event
2503 net_graph_msg_handler.get_and_clear_pending_msg_events();
2505 // Handle a single successful reply that encompasses the queried channel range
2506 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2508 full_information: true,
2510 number_of_blocks: 2000,
2511 short_channel_ids: vec![
2512 0x0003e0_000000_0000, // 992x0x0
2513 0x0003e8_000000_0000, // 1000x0x0
2514 0x0003e9_000000_0000, // 1001x0x0
2515 0x0003f0_000000_0000, // 1008x0x0
2516 0x00044c_000000_0000, // 1100x0x0
2517 0x0006e0_000000_0000, // 1760x0x0
2520 assert!(result.is_ok());
2522 // The query is now complete, so we expect the task to be removed
2523 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2525 // We expect to emit a query_short_channel_ids message with scids filtered to those
2526 // within the original query range.
2527 let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2528 assert_eq!(events.len(), 1);
2530 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2531 assert_eq!(node_id, &node_id_1);
2532 assert_eq!(msg.chain_hash, chain_hash);
2533 assert_eq!(msg.short_channel_ids, vec![0x0003e8_000000_0000,0x0003e9_000000_0000,0x0003f0_000000_0000]);
2535 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2538 // Clean up scid_task
2539 net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
2542 // Test receipt of multiple reply messages for a single query. This happens when the number
2543 // of scids in the query range exceeds the size limits of a single reply message. We expect
2544 // to initiate a query_short_channel_ids for the first batch of scids and we enqueue the
2545 // remaining scids for later processing. We remove the range query task after receipt of all
2548 // Initiate a channel range query to create a query task
2549 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2550 assert!(result.is_ok());
2552 // Clear the SendRangeQuery event
2553 net_graph_msg_handler.get_and_clear_pending_msg_events();
2555 // Handle the first reply message
2556 let reply_1_scids = vec![
2557 0x0003e8_000000_0000, // 1000x0x0
2558 0x0003e9_000000_0000, // 1001x0x0
2559 0x000419_000000_0000, // 1049x0x0
2561 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2563 full_information: true,
2564 first_blocknum: 1000,
2565 number_of_blocks: 50,
2566 short_channel_ids: reply_1_scids.clone(),
2568 assert!(result.is_ok());
2570 // Handle the next reply in the sequence, which must start at the previous message's
2571 // first_blocknum plus number_of_blocks. The scids in this reply will be queued.
2572 let reply_2_scids = vec![
2573 0x00041a_000000_0000, // 1050x0x0
2574 0x000432_000000_0000, // 1074x0x0
2576 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2578 full_information: true,
2579 first_blocknum: 1050,
2580 number_of_blocks: 25,
2581 short_channel_ids: reply_2_scids.clone(),
2583 assert!(result.is_ok());
2585 // Handle the final reply in the sequence, which must meet or exceed the initial query's
2586 // first_blocknum plus number_of_blocks. The scids in this reply will be queued.
2587 let reply_3_scids = vec![
2588 0x000433_000000_0000, // 1075x0x0
2589 0x00044b_000000_0000, // 1099x0x0
2591 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2593 full_information: true,
2594 first_blocknum: 1075,
2595 number_of_blocks: 25,
2596 short_channel_ids: reply_3_scids.clone(),
2598 assert!(result.is_ok());
2600 // After the final reply we expect the query task to be removed
2601 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2603 // We expect to emit a query_short_channel_ids message with the accumulated scids that
2604 // match the queried channel range.
2605 let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2606 assert_eq!(events.len(), 1);
2608 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2609 assert_eq!(node_id, &node_id_1);
2610 assert_eq!(msg.chain_hash, chain_hash);
2611 assert_eq!(msg.short_channel_ids, [reply_1_scids, reply_2_scids, reply_3_scids].concat());
2613 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2616 // Clean up scid_task
2617 net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
2620 // Test receipt of a sequence of replies with a valid first reply and a second reply that
2621 // resumes on the same block as the first reply. The spec requires a subsequent
2622 // first_blocknum to equal the prior first_blocknum plus number_of_blocks, however
2623 // due to discrepancies in implementation we must loosen this restriction.
2625 // Initiate a channel range query to create a query task
2626 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2627 assert!(result.is_ok());
2629 // Clear the SendRangeQuery event
2630 net_graph_msg_handler.get_and_clear_pending_msg_events();
2632 // Handle the first reply message
2633 let reply_1_scids = vec![
2634 0x0003e8_000000_0000, // 1000x0x0
2635 0x0003e9_000000_0000, // 1001x0x0
2636 0x000419_000000_0000, // 1049x0x0
2638 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2640 full_information: true,
2641 first_blocknum: 1000,
2642 number_of_blocks: 50,
2643 short_channel_ids: reply_1_scids.clone(),
2645 assert!(result.is_ok());
2647 // Handle the next reply in the sequence, which is non-spec but resumes on the last block
2648 // of the first message.
2649 let reply_2_scids = vec![
2650 0x000419_000001_0000, // 1049x1x0
2651 0x00041a_000000_0000, // 1050x0x0
2652 0x000432_000000_0000, // 1074x0x0
2654 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2656 full_information: true,
2657 first_blocknum: 1049,
2658 number_of_blocks: 51,
2659 short_channel_ids: reply_2_scids.clone(),
2661 assert!(result.is_ok());
2663 // After the final reply we expect the query task to be removed
2664 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2666 // We expect to emit a query_short_channel_ids message with the accumulated scids that
2667 // match the queried channel range
2668 let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2669 assert_eq!(events.len(), 1);
2671 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2672 assert_eq!(node_id, &node_id_1);
2673 assert_eq!(msg.chain_hash, chain_hash);
2674 assert_eq!(msg.short_channel_ids, [reply_1_scids, reply_2_scids].concat());
2676 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2679 // Clean up scid_task
2680 net_graph_msg_handler.scid_query_tasks.lock().unwrap().clear();
2683 // Test receipt of reply with a chain_hash that does not match the query. We expect to return
2684 // an error and to remove the query task.
2686 // Initiate a channel range query to create a query task
2687 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2688 assert!(result.is_ok());
2690 // Clear the SendRangeQuery event
2691 net_graph_msg_handler.get_and_clear_pending_msg_events();
2693 // Handle the reply with a mismatched chain_hash. We expect IgnoreError result and the
2694 // task should be removed.
2695 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2696 chain_hash: genesis_block(Network::Bitcoin).header.block_hash(),
2697 full_information: true,
2698 first_blocknum: 1000,
2699 number_of_blocks: 1050,
2700 short_channel_ids: vec![0x0003e8_000000_0000,0x0003e9_000000_0000,0x0003f0_000000_0000],
2702 assert!(result.is_err());
2703 assert_eq!(result.err().unwrap().err, "Received reply_channel_range with invalid chain_hash");
2704 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2707 // Test receipt of a reply that indicates the remote node does not maintain up-to-date
2708 // information for the chain_hash. Because of discrepancies in implementation we use
2709 // full_information=false and short_channel_ids=[] as the signal. We should expect an error
2710 // and the task should be removed.
2712 // Initiate a channel range query to create a query task
2713 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2714 assert!(result.is_ok());
2716 // Clear the SendRangeQuery event
2717 net_graph_msg_handler.get_and_clear_pending_msg_events();
2719 // Handle the reply indicating the peer was unable to fulfill our request.
2720 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2722 full_information: false,
2723 first_blocknum: 1000,
2724 number_of_blocks: 100,
2725 short_channel_ids: vec![],
2727 assert!(result.is_err());
2728 assert_eq!(result.err().unwrap().err, "Received reply_channel_range with no information available");
2729 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2732 // Test receipt of a reply that has a first_blocknum that is above the first_blocknum
2733 // requested in our query. The reply must contain the queried block range. We expect an
2734 // error result and the task should be removed.
2736 // Initiate a channel range query to create a query task
2737 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2738 assert!(result.is_ok());
2740 // Clear the SendRangeQuery event
2741 net_graph_msg_handler.get_and_clear_pending_msg_events();
2743 // Handle the reply that has a first_blocknum above the query's first_blocknum
2744 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2746 full_information: true,
2747 first_blocknum: 1001,
2748 number_of_blocks: 100,
2749 short_channel_ids: vec![],
2751 assert!(result.is_err());
2752 assert_eq!(result.err().unwrap().err, "Failing reply_channel_range with invalid first_blocknum");
2753 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2756 // Test receipt of a first reply that does not overlap the query range at all. The first message
2757 // must have some overlap with the query. We expect an error result and the task should
2760 // Initiate a channel range query to create a query task
2761 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2762 assert!(result.is_ok());
2764 // Clear the SendRangeQuery event
2765 net_graph_msg_handler.get_and_clear_pending_msg_events();
2767 // Handle a reply that contains a block range that precedes the queried block range
2768 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2770 full_information: true,
2772 number_of_blocks: 1000,
2773 short_channel_ids: vec![],
2775 assert!(result.is_err());
2776 assert_eq!(result.err().unwrap().err, "Failing reply_channel_range with non-overlapping first reply");
2777 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2780 // Test receipt of a sequence of replies with a valid first reply and a second reply that is
2781 // non-sequential. The spec requires a subsequent first_blocknum to equal the prior
2782 // first_blocknum plus number_of_blocks. We expect an IgnoreError result and the task should
2785 // Initiate a channel range query to create a query task
2786 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 100);
2787 assert!(result.is_ok());
2789 // Clear the SendRangeQuery event
2790 net_graph_msg_handler.get_and_clear_pending_msg_events();
2792 // Handle the first reply
2793 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2795 full_information: true,
2796 first_blocknum: 1000,
2797 number_of_blocks: 50,
2798 short_channel_ids: vec![0x0003e8_000000_0000,0x0003e9_000000_0000,0x0003f0_000000_0000],
2800 assert!(result.is_ok());
2802 // Handle the second reply which does not start at the proper first_blocknum. We expect
2803 // to return an error and remove the task.
2804 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2806 full_information: true,
2807 first_blocknum: 1051,
2808 number_of_blocks: 50,
2809 short_channel_ids: vec![0x0003f1_000000_0000,0x0003f2_000000_0000],
2811 assert!(result.is_err());
2812 assert_eq!(result.err().unwrap().err, "Failing reply_channel_range with invalid sequence");
2813 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2816 // Test receipt of too many reply messages. We expect an IgnoreError result and the task should
2819 // Initiate a channel range query to create a query task
2820 let result = net_graph_msg_handler.query_channel_range(&node_id_1, chain_hash, 1000, 0xffff_ffff);
2821 assert!(result.is_ok());
2823 // Clear the SendRangeQuery event
2824 net_graph_msg_handler.get_and_clear_pending_msg_events();
2826 // Handle a sequence of replies that will fail once the max number of reply has been exceeded.
2827 for block in 1000..=1000 + super::MAX_REPLY_CHANNEL_RANGE_PER_QUERY + 10 {
2828 let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
2830 full_information: true,
2831 first_blocknum: block as u32,
2832 number_of_blocks: 1,
2833 short_channel_ids: vec![(block as u64) << 40],
2835 if block <= 1000 + super::MAX_REPLY_CHANNEL_RANGE_PER_QUERY {
2836 assert!(result.is_ok());
2837 } else if block == 1001 + super::MAX_REPLY_CHANNEL_RANGE_PER_QUERY {
2838 assert!(result.is_err());
2839 assert_eq!(result.err().unwrap().err, "Failing reply_channel_range due to excessive messages");
2841 assert!(result.is_err());
2842 assert_eq!(result.err().unwrap().err, "Received unknown reply_channel_range message");
2846 // Expect the task to be removed
2847 assert!(net_graph_msg_handler.chan_range_query_tasks.lock().unwrap().is_empty());
2852 fn handling_reply_short_channel_ids() {
2853 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2854 let node_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2855 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
2857 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2859 // Test receipt of a reply when no query exists. We expect an error to be returned
2861 let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2863 full_information: true,
2865 assert!(result.is_err());
2866 assert_eq!(result.err().unwrap().err, "Unknown reply_short_channel_ids_end message");
2869 // Test receipt of a reply that is for a different chain_hash. We expect an error and the task
2870 // should be removed.
2872 // Initiate a query to create a pending query task
2873 let result = net_graph_msg_handler.query_short_channel_ids(&node_id, chain_hash, vec![0x0003e8_000000_0000]);
2874 assert!(result.is_ok());
2876 // Process reply with incorrect chain_hash
2877 let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2878 chain_hash: genesis_block(Network::Bitcoin).header.block_hash(),
2879 full_information: true,
2881 assert!(result.is_err());
2882 assert_eq!(result.err().unwrap().err, "Received reply_short_channel_ids_end with incorrect chain_hash");
2884 // Expect the task to be removed
2885 assert!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().is_empty());
2888 // Test receipt of a reply that indicates the peer does not maintain up-to-date information
2889 // for the chain_hash requested in the query. We expect an error and task should be removed.
2891 // Initiate a query to create a pending query task
2892 let result = net_graph_msg_handler.query_short_channel_ids(&node_id, chain_hash, vec![0x0003e8_000000_0000]);
2893 assert!(result.is_ok());
2895 // Process failed reply
2896 let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2898 full_information: false,
2900 assert!(result.is_err());
2901 assert_eq!(result.err().unwrap().err, "Received reply_short_channel_ids_end with no information");
2903 // Expect the task to be removed
2904 assert!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().is_empty());
2907 // Test receipt of a successful reply when there are no additional scids to query. We expect
2908 // the task to be removed.
2910 // Initiate a query to create a pending query task
2911 let result = net_graph_msg_handler.query_short_channel_ids(&node_id, chain_hash, vec![0x0003e8_000000_0000]);
2912 assert!(result.is_ok());
2914 // Process success reply
2915 let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2917 full_information: true,
2919 assert!(result.is_ok());
2921 // Expect the task to be removed
2922 assert!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().is_empty());
2925 // Test receipt of a successful reply when there are additional scids to query. We expect
2926 // additional queries to be sent until the task can be removed.
2928 // Initiate a query to create a pending query task
2929 let result = net_graph_msg_handler.query_short_channel_ids(&node_id, chain_hash, vec![0x0003e8_000000_0000]);
2930 assert!(result.is_ok());
2932 // Initiate a second query to add pending scids to the task
2933 let result = net_graph_msg_handler.query_short_channel_ids(&node_id, chain_hash, vec![0x0003e9_000000_0000]);
2934 assert!(result.is_ok());
2935 assert_eq!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().get(&node_id).unwrap().short_channel_ids, vec![0x0003e9_000000_0000]);
2937 // Initiate a third query to add pending scids to the task
2938 let result = net_graph_msg_handler.query_short_channel_ids(&node_id, chain_hash, vec![0x0003f0_000000_0000]);
2939 assert!(result.is_ok());
2940 assert_eq!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().get(&node_id).unwrap().short_channel_ids, vec![0x0003e9_000000_0000, 0x0003f0_000000_0000]);
2942 // Clear all of the pending send events
2943 net_graph_msg_handler.get_and_clear_pending_msg_events();
2945 // Handle the first successful reply, which will send the next batch of scids in a new query
2946 let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2948 full_information: true,
2950 assert!(result.is_ok());
2952 // We expect the second batch to be sent in an event
2953 let expected_node_id = &node_id;
2954 let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
2955 assert_eq!(events.len(), 1);
2957 MessageSendEvent::SendShortIdsQuery { node_id, msg } => {
2958 assert_eq!(node_id, expected_node_id);
2959 assert_eq!(msg.chain_hash, chain_hash);
2960 assert_eq!(msg.short_channel_ids, vec![0x0003e9_000000_0000, 0x0003f0_000000_0000]);
2962 _ => panic!("expected MessageSendEvent::SendShortIdsQuery"),
2965 // We expect the scids to be cleared from the task
2966 assert_eq!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().get(&node_id).unwrap().short_channel_ids.len(), 0);
2968 // Handle the second successful reply
2969 let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
2971 full_information: true,
2973 assert!(result.is_ok());
2975 // We expect the task should be removed
2976 assert!(net_graph_msg_handler.scid_query_tasks.lock().unwrap().is_empty());
2981 fn handling_query_channel_range() {
2982 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2983 let node_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2984 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
2986 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
2988 let result = net_graph_msg_handler.handle_query_channel_range(&node_id, &QueryChannelRange {
2991 number_of_blocks: 0xffff_ffff,
2993 assert!(result.is_err());
2997 fn handling_query_short_channel_ids() {
2998 let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
2999 let node_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
3000 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
3002 let chain_hash = genesis_block(Network::Testnet).header.block_hash();
3004 let result = net_graph_msg_handler.handle_query_short_channel_ids(&node_id, &QueryShortChannelIds {
3006 short_channel_ids: vec![0x0003e8_000000_0000],
3008 assert!(result.is_err());