Merge pull request #3144 from TheBlueMatt/2024-06-message-flags
[rust-lightning] / lightning-rapid-gossip-sync / src / processing.rs
1 use core::cmp::max;
2 use core::ops::Deref;
3 use core::sync::atomic::Ordering;
4
5 use bitcoin::blockdata::constants::ChainHash;
6 use bitcoin::secp256k1::PublicKey;
7
8 use lightning::io;
9 use lightning::ln::msgs::{
10         DecodeError, ErrorAction, LightningError, SocketAddress, UnsignedChannelUpdate,
11         UnsignedNodeAnnouncement,
12 };
13 use lightning::routing::gossip::{NetworkGraph, NodeAlias, NodeId};
14 use lightning::util::logger::Logger;
15 use lightning::util::ser::{BigSize, FixedLengthReader, Readable};
16 use lightning::{log_debug, log_given_level, log_gossip, log_trace, log_warn};
17
18 use crate::{GraphSyncError, RapidGossipSync};
19
20 #[cfg(all(feature = "std", not(test)))]
21 use std::time::{SystemTime, UNIX_EPOCH};
22
23 #[cfg(all(not(feature = "std"), not(test)))]
24 use alloc::{borrow::ToOwned, vec::Vec};
25 use lightning::ln::features::NodeFeatures;
26
27 /// The purpose of this prefix is to identify the serialization format, should other rapid gossip
28 /// sync formats arise in the future.
29 ///
30 /// The fourth byte is the protocol version in case our format gets updated.
31 const GOSSIP_PREFIX: [u8; 3] = [76, 68, 75];
32
33 /// Maximum vector allocation capacity for distinct node IDs. This constraint is necessary to
34 /// avoid malicious updates being able to trigger excessive memory allocation.
35 const MAX_INITIAL_NODE_ID_VECTOR_CAPACITY: u32 = 50_000;
36
37 /// We disallow gossip data that's more than two weeks old, per BOLT 7's
38 /// suggestion.
39 const STALE_RGS_UPDATE_AGE_LIMIT_SECS: u64 = 60 * 60 * 24 * 14;
40
41 impl<NG: Deref<Target = NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L>
42 where
43         L::Target: Logger,
44 {
45         #[cfg(feature = "std")]
46         pub(crate) fn update_network_graph_from_byte_stream<R: io::Read>(
47                 &self, read_cursor: &mut R,
48         ) -> Result<u32, GraphSyncError> {
49                 #[allow(unused_mut, unused_assignments)]
50                 let mut current_time_unix = None;
51                 #[cfg(not(test))]
52                 {
53                         // Note that many tests rely on being able to set arbitrarily old timestamps, thus we
54                         // disable this check during tests!
55                         current_time_unix = Some(
56                                 SystemTime::now()
57                                         .duration_since(UNIX_EPOCH)
58                                         .expect("Time must be > 1970")
59                                         .as_secs(),
60                         );
61                 }
62                 self.update_network_graph_from_byte_stream_no_std(read_cursor, current_time_unix)
63         }
64
65         pub(crate) fn update_network_graph_from_byte_stream_no_std<R: io::Read>(
66                 &self, mut read_cursor: &mut R, current_time_unix: Option<u64>,
67         ) -> Result<u32, GraphSyncError> {
68                 log_trace!(self.logger, "Processing RGS data...");
69                 let mut protocol_prefix = [0u8; 3];
70
71                 read_cursor.read_exact(&mut protocol_prefix)?;
72                 if protocol_prefix != GOSSIP_PREFIX {
73                         return Err(DecodeError::UnknownVersion.into());
74                 }
75
76                 let version: u8 = Readable::read(&mut read_cursor)?;
77                 if version != 1 && version != 2 {
78                         return Err(DecodeError::UnknownVersion.into());
79                 }
80
81                 let parse_node_details = version == 2;
82
83                 let chain_hash: ChainHash = Readable::read(read_cursor)?;
84                 let ng_chain_hash = self.network_graph.get_chain_hash();
85                 if chain_hash != ng_chain_hash {
86                         return Err(LightningError {
87                                 err: "Rapid Gossip Sync data's chain hash does not match the network graph's"
88                                         .to_owned(),
89                                 action: ErrorAction::IgnoreError,
90                         }
91                         .into());
92                 }
93
94                 let latest_seen_timestamp: u32 = Readable::read(read_cursor)?;
95
96                 if let Some(time) = current_time_unix {
97                         if (latest_seen_timestamp as u64) < time.saturating_sub(STALE_RGS_UPDATE_AGE_LIMIT_SECS)
98                         {
99                                 return Err(LightningError {
100                                         err: "Rapid Gossip Sync data is more than two weeks old".to_owned(),
101                                         action: ErrorAction::IgnoreError,
102                                 }
103                                 .into());
104                         }
105                 }
106
107                 // backdate the applied timestamp by a week
108                 let backdated_timestamp = latest_seen_timestamp.saturating_sub(24 * 3600 * 7);
109
110                 let mut default_node_features = Vec::new();
111                 if parse_node_details {
112                         let default_feature_count: u8 = Readable::read(read_cursor)?;
113                         for _ in 0..default_feature_count {
114                                 let current_default_feature: NodeFeatures = Readable::read(read_cursor)?;
115                                 default_node_features.push(current_default_feature);
116                         }
117                 };
118
119                 let node_id_count: u32 = Readable::read(read_cursor)?;
120                 let mut node_ids: Vec<PublicKey> = Vec::with_capacity(core::cmp::min(
121                         node_id_count,
122                         MAX_INITIAL_NODE_ID_VECTOR_CAPACITY,
123                 ) as usize);
124
125                 let network_graph = &self.network_graph;
126                 let mut node_modifications: Vec<UnsignedNodeAnnouncement> = Vec::new();
127
128                 if parse_node_details {
129                         let read_only_network_graph = network_graph.read_only();
130                         for _ in 0..node_id_count {
131                                 let mut pubkey_bytes = [0u8; 33];
132                                 read_cursor.read_exact(&mut pubkey_bytes)?;
133
134                                 /*
135                                 We encode additional information in the pubkey parity byte with the following mapping:
136
137                                 7: expect extra data after the pubkey
138                                 6: use this as a reminder without altering anything
139                                 5-3: index of new features among default (1-6). If index is 7 (all 3 bits are set, it's
140                                 outside the present default range). 0 means no feature changes.
141                                 2: addresses have changed
142
143                                 1: used for all keys
144                                 0: used for odd keys
145                                 */
146                                 let node_detail_flag = pubkey_bytes.first().ok_or(DecodeError::ShortRead)?;
147
148                                 let has_address_details = (node_detail_flag & (1 << 2)) > 0;
149                                 let feature_detail_marker = (node_detail_flag & (0b111 << 3)) >> 3;
150                                 let is_reminder = (node_detail_flag & (1 << 6)) > 0;
151                                 let has_additional_data = (node_detail_flag & (1 << 7)) > 0;
152
153                                 // extract the relevant bits for pubkey parity
154                                 let key_parity = node_detail_flag & 0b_0000_0011;
155                                 pubkey_bytes[0] = key_parity;
156
157                                 let current_pubkey = PublicKey::from_slice(&pubkey_bytes)?;
158                                 let current_node_id = NodeId::from_pubkey(&current_pubkey);
159                                 node_ids.push(current_pubkey);
160
161                                 if is_reminder || has_address_details || feature_detail_marker > 0 {
162                                         let mut synthetic_node_announcement = UnsignedNodeAnnouncement {
163                                                 features: NodeFeatures::empty(),
164                                                 timestamp: backdated_timestamp,
165                                                 node_id: current_node_id,
166                                                 rgb: [0, 0, 0],
167                                                 alias: NodeAlias([0u8; 32]),
168                                                 addresses: Vec::new(),
169                                                 excess_address_data: Vec::new(),
170                                                 excess_data: Vec::new(),
171                                         };
172
173                                         read_only_network_graph
174                                                 .nodes()
175                                                 .get(&current_node_id)
176                                                 .and_then(|node| node.announcement_info.as_ref())
177                                                 .map(|info| {
178                                                         synthetic_node_announcement.features = info.features().clone();
179                                                         synthetic_node_announcement.rgb = info.rgb().clone();
180                                                         synthetic_node_announcement.alias = info.alias().clone();
181                                                         synthetic_node_announcement.addresses = info.addresses().clone();
182                                                 });
183
184                                         if has_address_details {
185                                                 let address_count: u8 = Readable::read(read_cursor)?;
186                                                 let mut node_addresses: Vec<SocketAddress> = Vec::new();
187                                                 for address_index in 0..address_count {
188                                                         let current_byte_count: u8 = Readable::read(read_cursor)?;
189                                                         let mut address_reader =
190                                                                 FixedLengthReader::new(&mut read_cursor, current_byte_count as u64);
191                                                         if let Ok(current_address) = Readable::read(&mut address_reader) {
192                                                                 node_addresses.push(current_address);
193                                                                 if address_reader.bytes_remain() {
194                                                                         return Err(DecodeError::ShortRead.into());
195                                                                 }
196                                                         } else {
197                                                                 // Do not crash to allow future socket address forwards compatibility
198                                                                 log_gossip!(
199                                                                         self.logger,
200                                                                         "Failure to parse address at index {} for node ID {}",
201                                                                         address_index,
202                                                                         current_node_id
203                                                                 );
204                                                                 address_reader.eat_remaining()?;
205                                                         }
206                                                 }
207                                                 synthetic_node_announcement.addresses = node_addresses;
208                                         }
209
210                                         if feature_detail_marker > 0 {
211                                                 if feature_detail_marker < 7 {
212                                                         let feature_index = (feature_detail_marker - 1) as usize;
213                                                         synthetic_node_announcement.features = default_node_features
214                                                                 .get(feature_index)
215                                                                 .ok_or(DecodeError::InvalidValue)?
216                                                                 .clone();
217                                                 } else {
218                                                         let node_features: NodeFeatures = Readable::read(read_cursor)?;
219                                                         synthetic_node_announcement.features = node_features;
220                                                 }
221                                         }
222
223                                         node_modifications.push(synthetic_node_announcement);
224                                 }
225
226                                 if has_additional_data {
227                                         let additional_data: Vec<u8> = Readable::read(read_cursor)?;
228                                         log_gossip!(
229                                                 self.logger,
230                                                 "Ignoring {} bytes of additional data in node announcement",
231                                                 additional_data.len()
232                                         );
233                                 }
234                         }
235                 } else {
236                         for _ in 0..node_id_count {
237                                 let current_node_id = Readable::read(read_cursor)?;
238                                 node_ids.push(current_node_id);
239                         }
240                 }
241
242                 let mut previous_scid: u64 = 0;
243                 let announcement_count: u32 = Readable::read(read_cursor)?;
244                 for _ in 0..announcement_count {
245                         let features = Readable::read(read_cursor)?;
246
247                         // handle SCID
248                         let scid_delta: BigSize = Readable::read(read_cursor)?;
249                         let short_channel_id =
250                                 previous_scid.checked_add(scid_delta.0).ok_or(DecodeError::InvalidValue)?;
251                         previous_scid = short_channel_id;
252
253                         let node_id_1_index: BigSize = Readable::read(read_cursor)?;
254                         let mut node_id_2_index: BigSize = Readable::read(read_cursor)?;
255                         let has_additional_data = (node_id_2_index.0 & (1 << 63)) > 0;
256                         // ensure 63rd bit isn't set
257                         node_id_2_index.0 &= !(1 << 63);
258
259                         if max(node_id_1_index.0, node_id_2_index.0) >= node_id_count as u64 {
260                                 return Err(DecodeError::InvalidValue.into());
261                         };
262                         let node_id_1 = node_ids[node_id_1_index.0 as usize];
263                         let node_id_2 = node_ids[node_id_2_index.0 as usize];
264
265                         log_gossip!(
266                                 self.logger,
267                                 "Adding channel {} from RGS announcement at {}",
268                                 short_channel_id,
269                                 latest_seen_timestamp
270                         );
271
272                         let announcement_result = network_graph.add_channel_from_partial_announcement(
273                                 short_channel_id,
274                                 backdated_timestamp as u64,
275                                 features,
276                                 node_id_1,
277                                 node_id_2,
278                         );
279                         if let Err(lightning_error) = announcement_result {
280                                 if let ErrorAction::IgnoreDuplicateGossip = lightning_error.action {
281                                         // everything is fine, just a duplicate channel announcement
282                                 } else {
283                                         log_warn!(
284                                                 self.logger,
285                                                 "Failed to process channel announcement: {:?}",
286                                                 lightning_error
287                                         );
288                                         return Err(lightning_error.into());
289                                 }
290                         }
291
292                         if version >= 2 && has_additional_data {
293                                 // forwards compatibility
294                                 let additional_data: Vec<u8> = Readable::read(read_cursor)?;
295                                 log_gossip!(
296                                         self.logger,
297                                         "Ignoring {} bytes of additional data in channel announcement",
298                                         additional_data.len()
299                                 );
300                         }
301                 }
302
303                 for modification in node_modifications {
304                         match network_graph.update_node_from_unsigned_announcement(&modification) {
305                                 Ok(_) => {},
306                                 Err(LightningError { action: ErrorAction::IgnoreDuplicateGossip, .. }) => {},
307                                 Err(LightningError { action: ErrorAction::IgnoreAndLog(level), err }) => {
308                                         log_given_level!(
309                                                 self.logger,
310                                                 level,
311                                                 "Failed to apply node announcement: {:?}",
312                                                 err
313                                         );
314                                 },
315                                 Err(LightningError { action: ErrorAction::IgnoreError, err }) => {
316                                         log_gossip!(self.logger, "Failed to apply node announcement: {:?}", err);
317                                 },
318                                 Err(e) => return Err(e.into()),
319                         }
320                 }
321
322                 // updates start at a new scid
323                 previous_scid = 0;
324
325                 let update_count: u32 = Readable::read(read_cursor)?;
326                 log_debug!(self.logger, "Processing RGS update from {} with {} nodes, {} channel announcements and {} channel updates.",
327                         latest_seen_timestamp, node_id_count, announcement_count, update_count);
328                 if update_count == 0 {
329                         return Ok(latest_seen_timestamp);
330                 }
331
332                 // obtain default values for non-incremental updates
333                 let default_cltv_expiry_delta: u16 = Readable::read(&mut read_cursor)?;
334                 let default_htlc_minimum_msat: u64 = Readable::read(&mut read_cursor)?;
335                 let default_fee_base_msat: u32 = Readable::read(&mut read_cursor)?;
336                 let default_fee_proportional_millionths: u32 = Readable::read(&mut read_cursor)?;
337                 let default_htlc_maximum_msat: u64 = Readable::read(&mut read_cursor)?;
338
339                 let mut previous_channel_direction = None;
340
341                 for _ in 0..update_count {
342                         let scid_delta: BigSize = Readable::read(read_cursor)?;
343                         let short_channel_id =
344                                 previous_scid.checked_add(scid_delta.0).ok_or(DecodeError::InvalidValue)?;
345                         previous_scid = short_channel_id;
346
347                         let channel_flags: u8 = Readable::read(read_cursor)?;
348
349                         if version >= 2 {
350                                 let direction = (channel_flags & 1) == 1;
351                                 let is_same_direction_update = Some(direction) == previous_channel_direction;
352                                 previous_channel_direction = Some(direction);
353
354                                 if scid_delta.0 == 0 && is_same_direction_update {
355                                         // this is additional data for forwards compatibility
356                                         let additional_data: Vec<u8> = Readable::read(read_cursor)?;
357                                         log_gossip!(
358                                                 self.logger,
359                                                 "Ignoring {} bytes of additional data in channel update",
360                                                 additional_data.len()
361                                         );
362                                         continue;
363                                 }
364                         }
365
366                         // flags are always sent in full, and hence always need updating
367                         let standard_channel_flags = channel_flags & 0b_0000_0011;
368
369                         let mut synthetic_update = UnsignedChannelUpdate {
370                                 chain_hash,
371                                 short_channel_id,
372                                 timestamp: backdated_timestamp,
373                                 message_flags: 1, // Only must_be_one
374                                 channel_flags: standard_channel_flags,
375                                 cltv_expiry_delta: default_cltv_expiry_delta,
376                                 htlc_minimum_msat: default_htlc_minimum_msat,
377                                 htlc_maximum_msat: default_htlc_maximum_msat,
378                                 fee_base_msat: default_fee_base_msat,
379                                 fee_proportional_millionths: default_fee_proportional_millionths,
380                                 excess_data: Vec::new(),
381                         };
382
383                         let mut skip_update_for_unknown_channel = false;
384
385                         if (channel_flags & 0b_1000_0000) != 0 {
386                                 // incremental update, field flags will indicate mutated values
387                                 let read_only_network_graph = network_graph.read_only();
388                                 if let Some(directional_info) = read_only_network_graph
389                                         .channels()
390                                         .get(&short_channel_id)
391                                         .and_then(|channel| channel.get_directional_info(channel_flags))
392                                 {
393                                         synthetic_update.cltv_expiry_delta = directional_info.cltv_expiry_delta;
394                                         synthetic_update.htlc_minimum_msat = directional_info.htlc_minimum_msat;
395                                         synthetic_update.htlc_maximum_msat = directional_info.htlc_maximum_msat;
396                                         synthetic_update.fee_base_msat = directional_info.fees.base_msat;
397                                         synthetic_update.fee_proportional_millionths =
398                                                 directional_info.fees.proportional_millionths;
399                                 } else {
400                                         log_trace!(self.logger,
401                                                 "Skipping application of channel update for chan {} with flags {} as original data is missing.",
402                                                 short_channel_id, channel_flags);
403                                         skip_update_for_unknown_channel = true;
404                                 }
405                         };
406
407                         if channel_flags & 0b_0100_0000 > 0 {
408                                 let cltv_expiry_delta: u16 = Readable::read(read_cursor)?;
409                                 synthetic_update.cltv_expiry_delta = cltv_expiry_delta;
410                         }
411
412                         if channel_flags & 0b_0010_0000 > 0 {
413                                 let htlc_minimum_msat: u64 = Readable::read(read_cursor)?;
414                                 synthetic_update.htlc_minimum_msat = htlc_minimum_msat;
415                         }
416
417                         if channel_flags & 0b_0001_0000 > 0 {
418                                 let fee_base_msat: u32 = Readable::read(read_cursor)?;
419                                 synthetic_update.fee_base_msat = fee_base_msat;
420                         }
421
422                         if channel_flags & 0b_0000_1000 > 0 {
423                                 let fee_proportional_millionths: u32 = Readable::read(read_cursor)?;
424                                 synthetic_update.fee_proportional_millionths = fee_proportional_millionths;
425                         }
426
427                         if channel_flags & 0b_0000_0100 > 0 {
428                                 let htlc_maximum_msat: u64 = Readable::read(read_cursor)?;
429                                 synthetic_update.htlc_maximum_msat = htlc_maximum_msat;
430                         }
431
432                         if skip_update_for_unknown_channel {
433                                 continue;
434                         }
435
436                         log_gossip!(
437                                 self.logger,
438                                 "Updating channel {} with flags {} from RGS announcement at {}",
439                                 short_channel_id,
440                                 channel_flags,
441                                 latest_seen_timestamp
442                         );
443                         match network_graph.update_channel_unsigned(&synthetic_update) {
444                                 Ok(_) => {},
445                                 Err(LightningError { action: ErrorAction::IgnoreDuplicateGossip, .. }) => {},
446                                 Err(LightningError { action: ErrorAction::IgnoreAndLog(level), err }) => {
447                                         log_given_level!(
448                                                 self.logger,
449                                                 level,
450                                                 "Failed to apply channel update: {:?}",
451                                                 err
452                                         );
453                                 },
454                                 Err(LightningError { action: ErrorAction::IgnoreError, .. }) => {},
455                                 Err(e) => return Err(e.into()),
456                         }
457                 }
458
459                 self.network_graph.set_last_rapid_gossip_sync_timestamp(latest_seen_timestamp);
460
461                 if let Some(time) = current_time_unix {
462                         self.network_graph.remove_stale_channels_and_tracking_with_time(time)
463                 }
464
465                 self.is_initial_sync_complete.store(true, Ordering::Release);
466                 log_trace!(self.logger, "Done processing RGS data from {}", latest_seen_timestamp);
467                 Ok(latest_seen_timestamp)
468         }
469 }
470
471 #[cfg(test)]
472 mod tests {
473         use bitcoin::Network;
474
475         #[cfg(feature = "std")]
476         use lightning::ln::msgs::DecodeError;
477
478         use lightning::routing::gossip::{NetworkGraph, NodeId};
479         use lightning::util::logger::Level;
480         use lightning::util::test_utils::TestLogger;
481
482         use crate::processing::STALE_RGS_UPDATE_AGE_LIMIT_SECS;
483         use crate::{GraphSyncError, RapidGossipSync};
484
485         const VALID_RGS_BINARY: [u8; 300] = [
486                 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79,
487                 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218, 0, 0, 0,
488                 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251, 187, 172, 38,
489                 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125, 157, 176, 223,
490                 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136, 88, 216, 115, 11,
491                 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106, 204, 131, 186, 35,
492                 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138, 181, 64, 187, 103, 127,
493                 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175, 110, 32, 237, 0, 217, 90,
494                 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128, 76, 97, 0, 0, 0, 2, 0, 0, 255,
495                 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68, 226, 0, 6, 11, 0, 1, 2, 3, 0, 0, 0, 4,
496                 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 3, 232, 0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192,
497                 255, 8, 153, 192, 0, 2, 27, 0, 0, 60, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 100, 0, 0, 2, 224,
498                 0, 0, 0, 0, 58, 85, 116, 216, 0, 29, 0, 0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116,
499                 216, 255, 2, 68, 226, 0, 6, 11, 0, 1, 0, 0, 1,
500         ];
501         const VALID_BINARY_TIMESTAMP: u64 = 1642291930;
502
503         #[test]
504         #[cfg(feature = "std")]
505         fn network_graph_fails_to_update_from_clipped_input() {
506                 let logger = TestLogger::new();
507                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
508
509                 let example_input = vec![
510                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
511                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
512                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
513                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
514                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
515                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
516                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
517                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
518                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
519                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
520                         226, 0, 6, 11, 0, 1, 2, 3, 0, 0, 0, 2, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 0, 100,
521                         0, 0, 2, 224, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 36, 0, 0,
522                         0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 2, 68, 226, 0, 6, 11, 0, 1, 24, 0,
523                         0, 3, 232, 0, 0, 0,
524                 ];
525                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
526                 let update_result = rapid_sync.update_network_graph(&example_input[..]);
527                 assert!(update_result.is_err());
528                 if let Err(GraphSyncError::DecodeError(DecodeError::ShortRead)) = update_result {
529                         // this is the expected error type
530                 } else {
531                         panic!("Unexpected update result: {:?}", update_result)
532                 }
533         }
534
535         #[test]
536         fn node_data_update_succeeds_with_v2() {
537                 let mut logger = TestLogger::new();
538                 logger.enable(Level::Gossip);
539                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
540
541                 let example_input = vec![
542                         76, 68, 75, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
543                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 102, 97, 206, 240,
544                         0, 0, 0, 0, 2, 63, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5,
545                         101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143, 5, 38, 4,
546                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
547                         1, 1, 1, 0, 2, 3, 0, 4, 7, 1, 127, 0, 0, 1, 37, 163, 14, 5, 10, 103, 111, 111, 103,
548                         108, 101, 46, 99, 111, 109, 1, 187, 19, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
549                         1, 5, 57, 13, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 2, 23, 48, 62, 77, 75, 108,
550                         209, 54, 16, 50, 202, 155, 210, 174, 185, 217, 0, 170, 77, 69, 217, 234, 216, 10, 201,
551                         66, 51, 116, 196, 81, 167, 37, 77, 7, 102, 0, 0, 2, 25, 48, 0, 0, 0, 1, 0, 0, 1, 0, 1,
552                         0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
553                         0, 1, 0, 0, 1,
554                 ];
555
556                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
557                 let update_result = rapid_sync.update_network_graph_no_std(&example_input[..], None);
558                 assert!(update_result.is_ok());
559
560                 let read_only_graph = network_graph.read_only();
561                 let nodes = read_only_graph.nodes();
562                 assert_eq!(nodes.len(), 2);
563
564                 {
565                         // address node
566                         let node_id = NodeId::from_slice(&[
567                                 3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30,
568                                 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143,
569                         ])
570                         .unwrap();
571                         let node = nodes.get(&node_id).unwrap();
572                         let announcement_info = node.announcement_info.as_ref().unwrap();
573                         let addresses = announcement_info.addresses();
574                         assert_eq!(addresses.len(), 5);
575                 }
576
577                 {
578                         // feature node
579                         let node_id = NodeId::from_slice(&[
580                                 2, 77, 75, 108, 209, 54, 16, 50, 202, 155, 210, 174, 185, 217, 0, 170, 77, 69, 217,
581                                 234, 216, 10, 201, 66, 51, 116, 196, 81, 167, 37, 77, 7, 102,
582                         ])
583                         .unwrap();
584                         let node = nodes.get(&node_id).unwrap();
585                         let announcement_info = node.announcement_info.as_ref().unwrap();
586                         let features = announcement_info.features();
587                         println!("features: {}", features);
588                         // assert_eq!(addresses.len(), 5);
589                 }
590
591                 logger.assert_log_contains(
592                         "lightning_rapid_gossip_sync::processing",
593                         "Failed to apply node announcement",
594                         0,
595                 );
596         }
597
598         #[test]
599         fn node_date_update_succeeds_without_channels() {
600                 let mut logger = TestLogger::new();
601                 logger.enable(Level::Gossip);
602                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
603                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
604
605                 let example_input = vec![
606                         76, 68, 75, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
607                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 102, 105, 183,
608                         240, 0, 0, 0, 0, 1, 63, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186,
609                         5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143, 5, 13,
610                         3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 7, 1, 127, 0, 0, 1, 37, 163, 19, 2, 1, 1, 1,
611                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 57, 38, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
612                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 3, 0, 4, 14, 5,
613                         10, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 1, 187, 0, 2, 23, 48, 0, 0, 0, 0,
614                         0, 0, 0, 0,
615                 ];
616
617                 let update_result = rapid_sync.update_network_graph_no_std(&example_input[..], None);
618                 assert!(update_result.is_ok());
619
620                 logger.assert_log_contains(
621                         "lightning_rapid_gossip_sync::processing",
622                         "Failed to apply node announcement: \"No existing channels for node_announcement\"",
623                         1,
624                 );
625         }
626
627         #[test]
628         fn update_ignores_v2_additional_data() {
629                 let mut logger = TestLogger::new();
630                 logger.enable(Level::Gossip);
631                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
632                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
633
634                 let example_input = vec![
635                         76, 68, 75, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
636                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 102, 106, 12, 80,
637                         1, 0, 2, 23, 48, 0, 0, 0, 3, 143, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213,
638                         170, 186, 5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7,
639                         143, 5, 38, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
640                         1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 3, 0, 4, 19, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
641                         1, 1, 5, 57, 13, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 7, 1, 127, 0, 0, 1, 37, 163,
642                         14, 5, 10, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 1, 187, 0, 255, 0, 0, 0, 0,
643                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
644                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
645                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
646                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
647                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
648                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
649                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
650                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
651                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 138, 77, 75, 108, 209, 54, 16,
652                         50, 202, 155, 210, 174, 185, 217, 0, 170, 77, 69, 217, 234, 216, 10, 201, 66, 51, 116,
653                         196, 81, 167, 37, 77, 7, 102, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
654                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
655                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
656                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
657                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
658                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
659                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
660                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
661                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
662                         0, 0, 0, 0, 0, 0, 0, 186, 83, 31, 230, 6, 129, 52, 80, 61, 39, 35, 19, 50, 39, 200,
663                         103, 172, 143, 166, 200, 60, 83, 126, 154, 68, 195, 197, 189, 189, 203, 31, 227, 55, 0,
664                         2, 22, 49, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
665                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
666                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
667                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
668                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
669                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
670                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
671                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
672                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
673                         0, 0, 0, 1, 0, 0, 1, 0, 255, 128, 0, 0, 0, 0, 0, 0, 1, 0, 147, 23, 23, 23, 23, 23, 23,
674                         23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
675                         23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
676                         23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
677                         23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
678                         23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
679                         23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
680                         23, 23, 23, 23, 23, 23, 23, 23, 23, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
681                         6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 17, 42, 42, 42, 42, 42, 42, 42,
682                         42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 0, 1, 0, 1, 0, 17, 42, 42, 42, 42, 42, 42, 42,
683                         42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
684                 ];
685
686                 let update_result = rapid_sync.update_network_graph_no_std(&example_input[..], None);
687                 assert!(update_result.is_ok());
688
689                 logger.assert_log_contains(
690                         "lightning_rapid_gossip_sync::processing",
691                         "Ignoring 255 bytes of additional data in node announcement",
692                         3,
693                 );
694                 logger.assert_log_contains(
695                         "lightning_rapid_gossip_sync::processing",
696                         "Ignoring 147 bytes of additional data in channel announcement",
697                         1,
698                 );
699                 logger.assert_log_contains(
700                         "lightning_rapid_gossip_sync::processing",
701                         "Ignoring 17 bytes of additional data in channel update",
702                         1,
703                 );
704         }
705
706         #[test]
707         #[cfg(feature = "std")]
708         fn incremental_only_update_ignores_missing_channel() {
709                 let incremental_update_input = vec![
710                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
711                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
712                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
713                         0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 136, 0, 0, 0, 221, 255, 2,
714                         68, 226, 0, 6, 11, 0, 1, 128,
715                 ];
716
717                 let logger = TestLogger::new();
718                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
719
720                 assert_eq!(network_graph.read_only().channels().len(), 0);
721
722                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
723                 let update_result = rapid_sync.update_network_graph(&incremental_update_input[..]);
724                 assert!(update_result.is_ok());
725         }
726
727         #[test]
728         #[cfg(feature = "std")]
729         fn incremental_only_update_fails_without_prior_updates() {
730                 let announced_update_input = vec![
731                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
732                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
733                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
734                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
735                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
736                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
737                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
738                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
739                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
740                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
741                         226, 0, 6, 11, 0, 1, 2, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
742                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 136, 0, 0, 0, 221, 255,
743                         2, 68, 226, 0, 6, 11, 0, 1, 128,
744                 ];
745
746                 let logger = TestLogger::new();
747                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
748
749                 assert_eq!(network_graph.read_only().channels().len(), 0);
750
751                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
752                 rapid_sync.update_network_graph(&announced_update_input[..]).unwrap();
753         }
754
755         #[test]
756         #[cfg(feature = "std")]
757         fn incremental_only_update_fails_without_prior_same_direction_updates() {
758                 let initialization_input = vec![
759                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
760                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
761                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
762                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
763                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
764                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
765                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
766                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
767                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
768                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
769                         226, 0, 6, 11, 0, 1, 2, 3, 0, 0, 0, 2, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 3, 232,
770                         0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 25, 0, 0,
771                         0, 1, 0, 0, 0, 125, 255, 2, 68, 226, 0, 6, 11, 0, 1, 5, 0, 0, 0, 0, 29, 129, 25, 192,
772                 ];
773
774                 let logger = TestLogger::new();
775                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
776
777                 assert_eq!(network_graph.read_only().channels().len(), 0);
778
779                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
780                 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
781                 if initialization_result.is_err() {
782                         panic!("Unexpected initialization result: {:?}", initialization_result)
783                 }
784
785                 assert_eq!(network_graph.read_only().channels().len(), 2);
786                 let initialized = network_graph.to_string();
787                 assert!(initialized
788                         .contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643"));
789                 assert!(initialized
790                         .contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b"));
791                 assert!(initialized
792                         .contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432"));
793                 assert!(initialized
794                         .contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61"));
795                 assert!(initialized.contains("619737530008010752"));
796                 assert!(initialized.contains("783241506229452801"));
797
798                 let opposite_direction_incremental_update_input = vec![
799                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
800                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
801                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
802                         0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 136, 0, 0, 0, 221, 255, 2,
803                         68, 226, 0, 6, 11, 0, 1, 128,
804                 ];
805                 rapid_sync.update_network_graph(&opposite_direction_incremental_update_input[..]).unwrap();
806         }
807
808         #[test]
809         #[cfg(feature = "std")]
810         fn incremental_update_succeeds_with_prior_announcements_and_full_updates() {
811                 let initialization_input = vec![
812                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
813                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
814                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
815                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
816                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
817                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
818                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
819                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
820                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
821                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
822                         226, 0, 6, 11, 0, 1, 2, 3, 0, 0, 0, 4, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 3, 232,
823                         0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 56, 0, 0,
824                         0, 0, 0, 0, 0, 1, 0, 0, 0, 100, 0, 0, 2, 224, 0, 25, 0, 0, 0, 1, 0, 0, 0, 125, 255, 2,
825                         68, 226, 0, 6, 11, 0, 1, 4, 0, 0, 0, 0, 29, 129, 25, 192, 0, 5, 0, 0, 0, 0, 29, 129,
826                         25, 192,
827                 ];
828
829                 let logger = TestLogger::new();
830                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
831
832                 assert_eq!(network_graph.read_only().channels().len(), 0);
833
834                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
835                 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
836                 assert!(initialization_result.is_ok());
837
838                 let single_direction_incremental_update_input = vec![
839                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
840                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
841                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
842                         0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 136, 0, 0, 0, 221, 255, 2,
843                         68, 226, 0, 6, 11, 0, 1, 128,
844                 ];
845                 let update_result =
846                         rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
847                 if update_result.is_err() {
848                         panic!("Unexpected update result: {:?}", update_result)
849                 }
850
851                 assert_eq!(network_graph.read_only().channels().len(), 2);
852                 let after = network_graph.to_string();
853                 assert!(
854                         after.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643")
855                 );
856                 assert!(
857                         after.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b")
858                 );
859                 assert!(
860                         after.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432")
861                 );
862                 assert!(
863                         after.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61")
864                 );
865                 assert!(after.contains("619737530008010752"));
866                 assert!(after.contains("783241506229452801"));
867         }
868
869         #[test]
870         #[cfg(feature = "std")]
871         fn update_succeeds_when_duplicate_gossip_is_applied() {
872                 let initialization_input = vec![
873                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
874                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
875                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
876                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
877                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
878                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
879                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
880                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
881                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
882                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
883                         226, 0, 6, 11, 0, 1, 2, 3, 0, 0, 0, 4, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 3, 232,
884                         0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 56, 0, 0,
885                         0, 0, 0, 0, 0, 1, 0, 0, 0, 100, 0, 0, 2, 224, 0, 25, 0, 0, 0, 1, 0, 0, 0, 125, 255, 2,
886                         68, 226, 0, 6, 11, 0, 1, 4, 0, 0, 0, 0, 29, 129, 25, 192, 0, 5, 0, 0, 0, 0, 29, 129,
887                         25, 192,
888                 ];
889
890                 let logger = TestLogger::new();
891                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
892
893                 assert_eq!(network_graph.read_only().channels().len(), 0);
894
895                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
896                 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
897                 assert!(initialization_result.is_ok());
898
899                 let single_direction_incremental_update_input = vec![
900                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
901                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
902                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
903                         0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 136, 0, 0, 0, 221, 255, 2,
904                         68, 226, 0, 6, 11, 0, 1, 128,
905                 ];
906                 let update_result_1 =
907                         rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
908                 // Apply duplicate update
909                 let update_result_2 =
910                         rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
911                 assert!(update_result_1.is_ok());
912                 assert!(update_result_2.is_ok());
913         }
914
915         #[test]
916         #[cfg(feature = "std")]
917         fn full_update_succeeds() {
918                 let logger = TestLogger::new();
919                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
920
921                 assert_eq!(network_graph.read_only().channels().len(), 0);
922
923                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
924                 let update_result = rapid_sync.update_network_graph(&VALID_RGS_BINARY);
925                 if update_result.is_err() {
926                         panic!("Unexpected update result: {:?}", update_result)
927                 }
928
929                 assert_eq!(network_graph.read_only().channels().len(), 2);
930                 let after = network_graph.to_string();
931                 assert!(
932                         after.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643")
933                 );
934                 assert!(
935                         after.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b")
936                 );
937                 assert!(
938                         after.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432")
939                 );
940                 assert!(
941                         after.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61")
942                 );
943                 assert!(after.contains("619737530008010752"));
944                 assert!(after.contains("783241506229452801"));
945         }
946
947         #[test]
948         fn full_update_succeeds_at_the_beginning_of_the_unix_era() {
949                 let logger = TestLogger::new();
950                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
951
952                 assert_eq!(network_graph.read_only().channels().len(), 0);
953
954                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
955                 // this is mostly for checking uint underflow issues before the fuzzer does
956                 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(0));
957                 assert!(update_result.is_ok());
958                 assert_eq!(network_graph.read_only().channels().len(), 2);
959         }
960
961         #[test]
962         fn prunes_after_update() {
963                 // this is the timestamp encoded in the binary data of valid_input below
964                 let logger = TestLogger::new();
965
966                 let latest_nonpruning_time = VALID_BINARY_TIMESTAMP + 60 * 60 * 24 * 7;
967
968                 {
969                         let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
970                         assert_eq!(network_graph.read_only().channels().len(), 0);
971
972                         let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
973                         let update_result = rapid_sync
974                                 .update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time));
975                         assert!(update_result.is_ok());
976                         assert_eq!(network_graph.read_only().channels().len(), 2);
977                 }
978
979                 {
980                         let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
981                         assert_eq!(network_graph.read_only().channels().len(), 0);
982
983                         let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
984                         let update_result = rapid_sync
985                                 .update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time + 1));
986                         assert!(update_result.is_ok());
987                         assert_eq!(network_graph.read_only().channels().len(), 0);
988                 }
989         }
990
991         #[test]
992         fn timestamp_edge_cases_are_handled_correctly() {
993                 // this is the timestamp encoded in the binary data of valid_input below
994                 let logger = TestLogger::new();
995
996                 let latest_succeeding_time = VALID_BINARY_TIMESTAMP + STALE_RGS_UPDATE_AGE_LIMIT_SECS;
997                 let earliest_failing_time = latest_succeeding_time + 1;
998
999                 {
1000                         let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
1001                         assert_eq!(network_graph.read_only().channels().len(), 0);
1002
1003                         let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
1004                         let update_result = rapid_sync
1005                                 .update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_succeeding_time));
1006                         assert!(update_result.is_ok());
1007                         assert_eq!(network_graph.read_only().channels().len(), 0);
1008                 }
1009
1010                 {
1011                         let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
1012                         assert_eq!(network_graph.read_only().channels().len(), 0);
1013
1014                         let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
1015                         let update_result = rapid_sync
1016                                 .update_network_graph_no_std(&VALID_RGS_BINARY, Some(earliest_failing_time));
1017                         assert!(update_result.is_err());
1018                         if let Err(GraphSyncError::LightningError(lightning_error)) = update_result {
1019                                 assert_eq!(
1020                                         lightning_error.err,
1021                                         "Rapid Gossip Sync data is more than two weeks old"
1022                                 );
1023                         } else {
1024                                 panic!("Unexpected update result: {:?}", update_result)
1025                         }
1026                 }
1027         }
1028
1029         #[test]
1030         #[cfg(feature = "std")]
1031         pub fn update_fails_with_unknown_version() {
1032                 let unknown_version_input = vec![
1033                         76, 68, 75, 3, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
1034                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
1035                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
1036                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
1037                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
1038                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
1039                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
1040                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
1041                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
1042                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
1043                         226, 0, 6, 11, 0, 1, 2, 3, 0, 0, 0, 4, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 3, 232,
1044                         0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 60, 0, 0,
1045                         0, 0, 0, 0, 0, 1, 0, 0, 0, 100, 0, 0, 2, 224, 0, 0, 0, 0, 58, 85, 116, 216, 0, 29, 0,
1046                         0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 255, 2, 68, 226, 0, 6, 11, 0, 1,
1047                         0, 0, 1,
1048                 ];
1049
1050                 let logger = TestLogger::new();
1051                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
1052                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
1053                 let update_result = rapid_sync.update_network_graph(&unknown_version_input[..]);
1054
1055                 assert!(update_result.is_err());
1056
1057                 if let Err(GraphSyncError::DecodeError(DecodeError::UnknownVersion)) = update_result {
1058                         // this is the expected error type
1059                 } else {
1060                         panic!("Unexpected update result: {:?}", update_result)
1061                 }
1062         }
1063
1064         #[test]
1065         fn fails_early_on_chain_hash_mismatch() {
1066                 let logger = TestLogger::new();
1067                 // Set to testnet so that the VALID_RGS_BINARY chain hash of mainnet does not match.
1068                 let network_graph = NetworkGraph::new(Network::Testnet, &logger);
1069
1070                 assert_eq!(network_graph.read_only().channels().len(), 0);
1071
1072                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
1073                 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(0));
1074                 assert!(update_result.is_err());
1075                 if let Err(GraphSyncError::LightningError(err)) = update_result {
1076                         assert_eq!(
1077                                 err.err,
1078                                 "Rapid Gossip Sync data's chain hash does not match the network graph's"
1079                         );
1080                 } else {
1081                         panic!("Unexpected update result: {:?}", update_result)
1082                 }
1083         }
1084 }