Merge pull request #2989 from optout21/iatx-shared-output
[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                                 flags: standard_channel_flags,
374                                 cltv_expiry_delta: default_cltv_expiry_delta,
375                                 htlc_minimum_msat: default_htlc_minimum_msat,
376                                 htlc_maximum_msat: default_htlc_maximum_msat,
377                                 fee_base_msat: default_fee_base_msat,
378                                 fee_proportional_millionths: default_fee_proportional_millionths,
379                                 excess_data: Vec::new(),
380                         };
381
382                         let mut skip_update_for_unknown_channel = false;
383
384                         if (channel_flags & 0b_1000_0000) != 0 {
385                                 // incremental update, field flags will indicate mutated values
386                                 let read_only_network_graph = network_graph.read_only();
387                                 if let Some(directional_info) = read_only_network_graph
388                                         .channels()
389                                         .get(&short_channel_id)
390                                         .and_then(|channel| channel.get_directional_info(channel_flags))
391                                 {
392                                         synthetic_update.cltv_expiry_delta = directional_info.cltv_expiry_delta;
393                                         synthetic_update.htlc_minimum_msat = directional_info.htlc_minimum_msat;
394                                         synthetic_update.htlc_maximum_msat = directional_info.htlc_maximum_msat;
395                                         synthetic_update.fee_base_msat = directional_info.fees.base_msat;
396                                         synthetic_update.fee_proportional_millionths =
397                                                 directional_info.fees.proportional_millionths;
398                                 } else {
399                                         log_trace!(self.logger,
400                                                 "Skipping application of channel update for chan {} with flags {} as original data is missing.",
401                                                 short_channel_id, channel_flags);
402                                         skip_update_for_unknown_channel = true;
403                                 }
404                         };
405
406                         if channel_flags & 0b_0100_0000 > 0 {
407                                 let cltv_expiry_delta: u16 = Readable::read(read_cursor)?;
408                                 synthetic_update.cltv_expiry_delta = cltv_expiry_delta;
409                         }
410
411                         if channel_flags & 0b_0010_0000 > 0 {
412                                 let htlc_minimum_msat: u64 = Readable::read(read_cursor)?;
413                                 synthetic_update.htlc_minimum_msat = htlc_minimum_msat;
414                         }
415
416                         if channel_flags & 0b_0001_0000 > 0 {
417                                 let fee_base_msat: u32 = Readable::read(read_cursor)?;
418                                 synthetic_update.fee_base_msat = fee_base_msat;
419                         }
420
421                         if channel_flags & 0b_0000_1000 > 0 {
422                                 let fee_proportional_millionths: u32 = Readable::read(read_cursor)?;
423                                 synthetic_update.fee_proportional_millionths = fee_proportional_millionths;
424                         }
425
426                         if channel_flags & 0b_0000_0100 > 0 {
427                                 let htlc_maximum_msat: u64 = Readable::read(read_cursor)?;
428                                 synthetic_update.htlc_maximum_msat = htlc_maximum_msat;
429                         }
430
431                         if skip_update_for_unknown_channel {
432                                 continue;
433                         }
434
435                         log_gossip!(
436                                 self.logger,
437                                 "Updating channel {} with flags {} from RGS announcement at {}",
438                                 short_channel_id,
439                                 channel_flags,
440                                 latest_seen_timestamp
441                         );
442                         match network_graph.update_channel_unsigned(&synthetic_update) {
443                                 Ok(_) => {},
444                                 Err(LightningError { action: ErrorAction::IgnoreDuplicateGossip, .. }) => {},
445                                 Err(LightningError { action: ErrorAction::IgnoreAndLog(level), err }) => {
446                                         log_given_level!(
447                                                 self.logger,
448                                                 level,
449                                                 "Failed to apply channel update: {:?}",
450                                                 err
451                                         );
452                                 },
453                                 Err(LightningError { action: ErrorAction::IgnoreError, .. }) => {},
454                                 Err(e) => return Err(e.into()),
455                         }
456                 }
457
458                 self.network_graph.set_last_rapid_gossip_sync_timestamp(latest_seen_timestamp);
459
460                 if let Some(time) = current_time_unix {
461                         self.network_graph.remove_stale_channels_and_tracking_with_time(time)
462                 }
463
464                 self.is_initial_sync_complete.store(true, Ordering::Release);
465                 log_trace!(self.logger, "Done processing RGS data from {}", latest_seen_timestamp);
466                 Ok(latest_seen_timestamp)
467         }
468 }
469
470 #[cfg(test)]
471 mod tests {
472         use bitcoin::Network;
473
474         #[cfg(feature = "std")]
475         use lightning::ln::msgs::DecodeError;
476
477         use lightning::routing::gossip::{NetworkGraph, NodeId};
478         use lightning::util::logger::Level;
479         use lightning::util::test_utils::TestLogger;
480
481         use crate::processing::STALE_RGS_UPDATE_AGE_LIMIT_SECS;
482         use crate::{GraphSyncError, RapidGossipSync};
483
484         const VALID_RGS_BINARY: [u8; 300] = [
485                 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79,
486                 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218, 0, 0, 0,
487                 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251, 187, 172, 38,
488                 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125, 157, 176, 223,
489                 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136, 88, 216, 115, 11,
490                 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106, 204, 131, 186, 35,
491                 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138, 181, 64, 187, 103, 127,
492                 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175, 110, 32, 237, 0, 217, 90,
493                 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128, 76, 97, 0, 0, 0, 2, 0, 0, 255,
494                 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,
495                 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,
496                 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,
497                 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,
498                 216, 255, 2, 68, 226, 0, 6, 11, 0, 1, 0, 0, 1,
499         ];
500         const VALID_BINARY_TIMESTAMP: u64 = 1642291930;
501
502         #[test]
503         #[cfg(feature = "std")]
504         fn network_graph_fails_to_update_from_clipped_input() {
505                 let logger = TestLogger::new();
506                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
507
508                 let example_input = vec![
509                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
510                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
511                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
512                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
513                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
514                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
515                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
516                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
517                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
518                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
519                         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,
520                         0, 0, 2, 224, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 36, 0, 0,
521                         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,
522                         0, 3, 232, 0, 0, 0,
523                 ];
524                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
525                 let update_result = rapid_sync.update_network_graph(&example_input[..]);
526                 assert!(update_result.is_err());
527                 if let Err(GraphSyncError::DecodeError(DecodeError::ShortRead)) = update_result {
528                         // this is the expected error type
529                 } else {
530                         panic!("Unexpected update result: {:?}", update_result)
531                 }
532         }
533
534         #[test]
535         fn node_data_update_succeeds_with_v2() {
536                 let mut logger = TestLogger::new();
537                 logger.enable(Level::Gossip);
538                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
539
540                 let example_input = vec![
541                         76, 68, 75, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
542                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 102, 97, 206, 240,
543                         0, 0, 0, 0, 2, 63, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5,
544                         101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143, 5, 38, 4,
545                         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,
546                         1, 1, 1, 0, 2, 3, 0, 4, 7, 1, 127, 0, 0, 1, 37, 163, 14, 5, 10, 103, 111, 111, 103,
547                         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,
548                         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,
549                         209, 54, 16, 50, 202, 155, 210, 174, 185, 217, 0, 170, 77, 69, 217, 234, 216, 10, 201,
550                         66, 51, 116, 196, 81, 167, 37, 77, 7, 102, 0, 0, 2, 25, 48, 0, 0, 0, 1, 0, 0, 1, 0, 1,
551                         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,
552                         0, 1, 0, 0, 1,
553                 ];
554
555                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
556                 let update_result = rapid_sync.update_network_graph_no_std(&example_input[..], None);
557                 assert!(update_result.is_ok());
558
559                 let read_only_graph = network_graph.read_only();
560                 let nodes = read_only_graph.nodes();
561                 assert_eq!(nodes.len(), 2);
562
563                 {
564                         // address node
565                         let node_id = NodeId::from_slice(&[
566                                 3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30,
567                                 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143,
568                         ])
569                         .unwrap();
570                         let node = nodes.get(&node_id).unwrap();
571                         let announcement_info = node.announcement_info.as_ref().unwrap();
572                         let addresses = announcement_info.addresses();
573                         assert_eq!(addresses.len(), 5);
574                 }
575
576                 {
577                         // feature node
578                         let node_id = NodeId::from_slice(&[
579                                 2, 77, 75, 108, 209, 54, 16, 50, 202, 155, 210, 174, 185, 217, 0, 170, 77, 69, 217,
580                                 234, 216, 10, 201, 66, 51, 116, 196, 81, 167, 37, 77, 7, 102,
581                         ])
582                         .unwrap();
583                         let node = nodes.get(&node_id).unwrap();
584                         let announcement_info = node.announcement_info.as_ref().unwrap();
585                         let features = announcement_info.features();
586                         println!("features: {}", features);
587                         // assert_eq!(addresses.len(), 5);
588                 }
589
590                 logger.assert_log_contains(
591                         "lightning_rapid_gossip_sync::processing",
592                         "Failed to apply node announcement",
593                         0,
594                 );
595         }
596
597         #[test]
598         fn node_date_update_succeeds_without_channels() {
599                 let mut logger = TestLogger::new();
600                 logger.enable(Level::Gossip);
601                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
602                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
603
604                 let example_input = vec![
605                         76, 68, 75, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
606                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 102, 105, 183,
607                         240, 0, 0, 0, 0, 1, 63, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186,
608                         5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143, 5, 13,
609                         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,
610                         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,
611                         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,
612                         10, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 1, 187, 0, 2, 23, 48, 0, 0, 0, 0,
613                         0, 0, 0, 0,
614                 ];
615
616                 let update_result = rapid_sync.update_network_graph_no_std(&example_input[..], None);
617                 assert!(update_result.is_ok());
618
619                 logger.assert_log_contains(
620                         "lightning_rapid_gossip_sync::processing",
621                         "Failed to apply node announcement: \"No existing channels for node_announcement\"",
622                         1,
623                 );
624         }
625
626         #[test]
627         fn update_ignores_v2_additional_data() {
628                 let mut logger = TestLogger::new();
629                 logger.enable(Level::Gossip);
630                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
631                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
632
633                 let example_input = vec![
634                         76, 68, 75, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
635                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 102, 106, 12, 80,
636                         1, 0, 2, 23, 48, 0, 0, 0, 3, 143, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213,
637                         170, 186, 5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7,
638                         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,
639                         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,
640                         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,
641                         14, 5, 10, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 1, 187, 0, 255, 0, 0, 0, 0,
642                         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,
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, 138, 77, 75, 108, 209, 54, 16,
651                         50, 202, 155, 210, 174, 185, 217, 0, 170, 77, 69, 217, 234, 216, 10, 201, 66, 51, 116,
652                         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,
653                         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,
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, 186, 83, 31, 230, 6, 129, 52, 80, 61, 39, 35, 19, 50, 39, 200,
662                         103, 172, 143, 166, 200, 60, 83, 126, 154, 68, 195, 197, 189, 189, 203, 31, 227, 55, 0,
663                         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,
664                         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,
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, 1, 0, 0, 1, 0, 255, 128, 0, 0, 0, 0, 0, 0, 1, 0, 147, 23, 23, 23, 23, 23, 23,
673                         23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 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, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
680                         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,
681                         42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 0, 1, 0, 1, 0, 17, 42, 42, 42, 42, 42, 42, 42,
682                         42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
683                 ];
684
685                 let update_result = rapid_sync.update_network_graph_no_std(&example_input[..], None);
686                 assert!(update_result.is_ok());
687
688                 logger.assert_log_contains(
689                         "lightning_rapid_gossip_sync::processing",
690                         "Ignoring 255 bytes of additional data in node announcement",
691                         3,
692                 );
693                 logger.assert_log_contains(
694                         "lightning_rapid_gossip_sync::processing",
695                         "Ignoring 147 bytes of additional data in channel announcement",
696                         1,
697                 );
698                 logger.assert_log_contains(
699                         "lightning_rapid_gossip_sync::processing",
700                         "Ignoring 17 bytes of additional data in channel update",
701                         1,
702                 );
703         }
704
705         #[test]
706         #[cfg(feature = "std")]
707         fn incremental_only_update_ignores_missing_channel() {
708                 let incremental_update_input = vec![
709                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
710                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
711                         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,
712                         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,
713                         68, 226, 0, 6, 11, 0, 1, 128,
714                 ];
715
716                 let logger = TestLogger::new();
717                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
718
719                 assert_eq!(network_graph.read_only().channels().len(), 0);
720
721                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
722                 let update_result = rapid_sync.update_network_graph(&incremental_update_input[..]);
723                 assert!(update_result.is_ok());
724         }
725
726         #[test]
727         #[cfg(feature = "std")]
728         fn incremental_only_update_fails_without_prior_updates() {
729                 let announced_update_input = vec![
730                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
731                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
732                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
733                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
734                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
735                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
736                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
737                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
738                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
739                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
740                         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,
741                         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,
742                         2, 68, 226, 0, 6, 11, 0, 1, 128,
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                 rapid_sync.update_network_graph(&announced_update_input[..]).unwrap();
752         }
753
754         #[test]
755         #[cfg(feature = "std")]
756         fn incremental_only_update_fails_without_prior_same_direction_updates() {
757                 let initialization_input = vec![
758                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
759                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
760                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
761                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
762                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
763                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
764                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
765                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
766                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
767                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
768                         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,
769                         0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 25, 0, 0,
770                         0, 1, 0, 0, 0, 125, 255, 2, 68, 226, 0, 6, 11, 0, 1, 5, 0, 0, 0, 0, 29, 129, 25, 192,
771                 ];
772
773                 let logger = TestLogger::new();
774                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
775
776                 assert_eq!(network_graph.read_only().channels().len(), 0);
777
778                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
779                 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
780                 if initialization_result.is_err() {
781                         panic!("Unexpected initialization result: {:?}", initialization_result)
782                 }
783
784                 assert_eq!(network_graph.read_only().channels().len(), 2);
785                 let initialized = network_graph.to_string();
786                 assert!(initialized
787                         .contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643"));
788                 assert!(initialized
789                         .contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b"));
790                 assert!(initialized
791                         .contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432"));
792                 assert!(initialized
793                         .contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61"));
794                 assert!(initialized.contains("619737530008010752"));
795                 assert!(initialized.contains("783241506229452801"));
796
797                 let opposite_direction_incremental_update_input = vec![
798                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
799                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
800                         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,
801                         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,
802                         68, 226, 0, 6, 11, 0, 1, 128,
803                 ];
804                 rapid_sync.update_network_graph(&opposite_direction_incremental_update_input[..]).unwrap();
805         }
806
807         #[test]
808         #[cfg(feature = "std")]
809         fn incremental_update_succeeds_with_prior_announcements_and_full_updates() {
810                 let initialization_input = vec![
811                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
812                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
813                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
814                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
815                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
816                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
817                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
818                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
819                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
820                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
821                         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,
822                         0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 56, 0, 0,
823                         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,
824                         68, 226, 0, 6, 11, 0, 1, 4, 0, 0, 0, 0, 29, 129, 25, 192, 0, 5, 0, 0, 0, 0, 29, 129,
825                         25, 192,
826                 ];
827
828                 let logger = TestLogger::new();
829                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
830
831                 assert_eq!(network_graph.read_only().channels().len(), 0);
832
833                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
834                 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
835                 assert!(initialization_result.is_ok());
836
837                 let single_direction_incremental_update_input = vec![
838                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
839                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
840                         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,
841                         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,
842                         68, 226, 0, 6, 11, 0, 1, 128,
843                 ];
844                 let update_result =
845                         rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
846                 if update_result.is_err() {
847                         panic!("Unexpected update result: {:?}", update_result)
848                 }
849
850                 assert_eq!(network_graph.read_only().channels().len(), 2);
851                 let after = network_graph.to_string();
852                 assert!(
853                         after.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643")
854                 );
855                 assert!(
856                         after.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b")
857                 );
858                 assert!(
859                         after.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432")
860                 );
861                 assert!(
862                         after.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61")
863                 );
864                 assert!(after.contains("619737530008010752"));
865                 assert!(after.contains("783241506229452801"));
866         }
867
868         #[test]
869         #[cfg(feature = "std")]
870         fn update_succeeds_when_duplicate_gossip_is_applied() {
871                 let initialization_input = vec![
872                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
873                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
874                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
875                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
876                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
877                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
878                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
879                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
880                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
881                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
882                         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,
883                         0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 56, 0, 0,
884                         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,
885                         68, 226, 0, 6, 11, 0, 1, 4, 0, 0, 0, 0, 29, 129, 25, 192, 0, 5, 0, 0, 0, 0, 29, 129,
886                         25, 192,
887                 ];
888
889                 let logger = TestLogger::new();
890                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
891
892                 assert_eq!(network_graph.read_only().channels().len(), 0);
893
894                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
895                 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
896                 assert!(initialization_result.is_ok());
897
898                 let single_direction_incremental_update_input = vec![
899                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
900                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
901                         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,
902                         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,
903                         68, 226, 0, 6, 11, 0, 1, 128,
904                 ];
905                 let update_result_1 =
906                         rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
907                 // Apply duplicate update
908                 let update_result_2 =
909                         rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
910                 assert!(update_result_1.is_ok());
911                 assert!(update_result_2.is_ok());
912         }
913
914         #[test]
915         #[cfg(feature = "std")]
916         fn full_update_succeeds() {
917                 let logger = TestLogger::new();
918                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
919
920                 assert_eq!(network_graph.read_only().channels().len(), 0);
921
922                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
923                 let update_result = rapid_sync.update_network_graph(&VALID_RGS_BINARY);
924                 if update_result.is_err() {
925                         panic!("Unexpected update result: {:?}", update_result)
926                 }
927
928                 assert_eq!(network_graph.read_only().channels().len(), 2);
929                 let after = network_graph.to_string();
930                 assert!(
931                         after.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643")
932                 );
933                 assert!(
934                         after.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b")
935                 );
936                 assert!(
937                         after.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432")
938                 );
939                 assert!(
940                         after.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61")
941                 );
942                 assert!(after.contains("619737530008010752"));
943                 assert!(after.contains("783241506229452801"));
944         }
945
946         #[test]
947         fn full_update_succeeds_at_the_beginning_of_the_unix_era() {
948                 let logger = TestLogger::new();
949                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
950
951                 assert_eq!(network_graph.read_only().channels().len(), 0);
952
953                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
954                 // this is mostly for checking uint underflow issues before the fuzzer does
955                 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(0));
956                 assert!(update_result.is_ok());
957                 assert_eq!(network_graph.read_only().channels().len(), 2);
958         }
959
960         #[test]
961         fn prunes_after_update() {
962                 // this is the timestamp encoded in the binary data of valid_input below
963                 let logger = TestLogger::new();
964
965                 let latest_nonpruning_time = VALID_BINARY_TIMESTAMP + 60 * 60 * 24 * 7;
966
967                 {
968                         let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
969                         assert_eq!(network_graph.read_only().channels().len(), 0);
970
971                         let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
972                         let update_result = rapid_sync
973                                 .update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time));
974                         assert!(update_result.is_ok());
975                         assert_eq!(network_graph.read_only().channels().len(), 2);
976                 }
977
978                 {
979                         let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
980                         assert_eq!(network_graph.read_only().channels().len(), 0);
981
982                         let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
983                         let update_result = rapid_sync
984                                 .update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time + 1));
985                         assert!(update_result.is_ok());
986                         assert_eq!(network_graph.read_only().channels().len(), 0);
987                 }
988         }
989
990         #[test]
991         fn timestamp_edge_cases_are_handled_correctly() {
992                 // this is the timestamp encoded in the binary data of valid_input below
993                 let logger = TestLogger::new();
994
995                 let latest_succeeding_time = VALID_BINARY_TIMESTAMP + STALE_RGS_UPDATE_AGE_LIMIT_SECS;
996                 let earliest_failing_time = latest_succeeding_time + 1;
997
998                 {
999                         let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
1000                         assert_eq!(network_graph.read_only().channels().len(), 0);
1001
1002                         let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
1003                         let update_result = rapid_sync
1004                                 .update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_succeeding_time));
1005                         assert!(update_result.is_ok());
1006                         assert_eq!(network_graph.read_only().channels().len(), 0);
1007                 }
1008
1009                 {
1010                         let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
1011                         assert_eq!(network_graph.read_only().channels().len(), 0);
1012
1013                         let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
1014                         let update_result = rapid_sync
1015                                 .update_network_graph_no_std(&VALID_RGS_BINARY, Some(earliest_failing_time));
1016                         assert!(update_result.is_err());
1017                         if let Err(GraphSyncError::LightningError(lightning_error)) = update_result {
1018                                 assert_eq!(
1019                                         lightning_error.err,
1020                                         "Rapid Gossip Sync data is more than two weeks old"
1021                                 );
1022                         } else {
1023                                 panic!("Unexpected update result: {:?}", update_result)
1024                         }
1025                 }
1026         }
1027
1028         #[test]
1029         #[cfg(feature = "std")]
1030         pub fn update_fails_with_unknown_version() {
1031                 let unknown_version_input = vec![
1032                         76, 68, 75, 3, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
1033                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
1034                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
1035                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
1036                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
1037                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
1038                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
1039                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
1040                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
1041                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
1042                         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,
1043                         0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 60, 0, 0,
1044                         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,
1045                         0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 255, 2, 68, 226, 0, 6, 11, 0, 1,
1046                         0, 0, 1,
1047                 ];
1048
1049                 let logger = TestLogger::new();
1050                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
1051                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
1052                 let update_result = rapid_sync.update_network_graph(&unknown_version_input[..]);
1053
1054                 assert!(update_result.is_err());
1055
1056                 if let Err(GraphSyncError::DecodeError(DecodeError::UnknownVersion)) = update_result {
1057                         // this is the expected error type
1058                 } else {
1059                         panic!("Unexpected update result: {:?}", update_result)
1060                 }
1061         }
1062
1063         #[test]
1064         fn fails_early_on_chain_hash_mismatch() {
1065                 let logger = TestLogger::new();
1066                 // Set to testnet so that the VALID_RGS_BINARY chain hash of mainnet does not match.
1067                 let network_graph = NetworkGraph::new(Network::Testnet, &logger);
1068
1069                 assert_eq!(network_graph.read_only().channels().len(), 0);
1070
1071                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
1072                 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(0));
1073                 assert!(update_result.is_err());
1074                 if let Err(GraphSyncError::LightningError(err)) = update_result {
1075                         assert_eq!(
1076                                 err.err,
1077                                 "Rapid Gossip Sync data's chain hash does not match the network graph's"
1078                         );
1079                 } else {
1080                         panic!("Unexpected update result: {:?}", update_result)
1081                 }
1082         }
1083 }