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