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