b387d49998e9a38d6f1693e1a18d05c4da57e675
[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::BlockHash;
6 use bitcoin::secp256k1::PublicKey;
7
8 use lightning::ln::msgs::{
9         DecodeError, ErrorAction, LightningError, UnsignedChannelUpdate,
10 };
11 use lightning::routing::gossip::NetworkGraph;
12 use lightning::util::logger::Logger;
13 use lightning::{log_debug, log_warn, log_trace, log_given_level, log_gossip};
14 use lightning::util::ser::{BigSize, Readable};
15 use lightning::io;
16
17 use crate::error::GraphSyncError;
18 use crate::RapidGossipSync;
19
20 #[cfg(all(feature = "std", not(test)))]
21 use std::time::{SystemTime, UNIX_EPOCH};
22
23 #[cfg(not(feature = "std"))]
24 use alloc::{vec::Vec, borrow::ToOwned};
25
26 /// The purpose of this prefix is to identify the serialization format, should other rapid gossip
27 /// sync formats arise in the future.
28 ///
29 /// The fourth byte is the protocol version in case our format gets updated.
30 const GOSSIP_PREFIX: [u8; 4] = [76, 68, 75, 1];
31
32 /// Maximum vector allocation capacity for distinct node IDs. This constraint is necessary to
33 /// avoid malicious updates being able to trigger excessive memory allocation.
34 const MAX_INITIAL_NODE_ID_VECTOR_CAPACITY: u32 = 50_000;
35
36 /// We disallow gossip data that's more than two weeks old, per BOLT 7's
37 /// suggestion.
38 const STALE_RGS_UPDATE_AGE_LIMIT_SECS: u64 = 60 * 60 * 24 * 14;
39
40 impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L::Target: Logger {
41         #[cfg(feature = "std")]
42         pub(crate) fn update_network_graph_from_byte_stream<R: io::Read>(
43                 &self,
44                 read_cursor: &mut R,
45         ) -> Result<u32, GraphSyncError> {
46                 #[allow(unused_mut, unused_assignments)]
47                 let mut current_time_unix = None;
48                 #[cfg(not(test))]
49                 {
50                         // Note that many tests rely on being able to set arbitrarily old timestamps, thus we
51                         // disable this check during tests!
52                         current_time_unix = Some(SystemTime::now().duration_since(UNIX_EPOCH).expect("Time must be > 1970").as_secs());
53                 }
54                 self.update_network_graph_from_byte_stream_no_std(read_cursor, current_time_unix)
55         }
56
57         pub(crate) fn update_network_graph_from_byte_stream_no_std<R: io::Read>(
58                 &self,
59                 mut read_cursor: &mut R,
60                 current_time_unix: Option<u64>
61         ) -> Result<u32, GraphSyncError> {
62                 log_trace!(self.logger, "Processing RGS data...");
63                 let mut prefix = [0u8; 4];
64                 read_cursor.read_exact(&mut prefix)?;
65
66                 if prefix != GOSSIP_PREFIX {
67                         return Err(DecodeError::UnknownVersion.into());
68                 }
69
70                 let chain_hash: BlockHash = Readable::read(read_cursor)?;
71                 let latest_seen_timestamp: u32 = Readable::read(read_cursor)?;
72
73                 if let Some(time) = current_time_unix {
74                         if (latest_seen_timestamp as u64) < time.saturating_sub(STALE_RGS_UPDATE_AGE_LIMIT_SECS) {
75                                 return Err(LightningError{err: "Rapid Gossip Sync data is more than two weeks old".to_owned(), action: ErrorAction::IgnoreError}.into());
76                         }
77                 }
78
79                 // backdate the applied timestamp by a week
80                 let backdated_timestamp = latest_seen_timestamp.saturating_sub(24 * 3600 * 7);
81
82                 let node_id_count: u32 = Readable::read(read_cursor)?;
83                 let mut node_ids: Vec<PublicKey> = Vec::with_capacity(core::cmp::min(
84                         node_id_count,
85                         MAX_INITIAL_NODE_ID_VECTOR_CAPACITY,
86                 ) as usize);
87                 for _ in 0..node_id_count {
88                         let current_node_id = Readable::read(read_cursor)?;
89                         node_ids.push(current_node_id);
90                 }
91
92                 let network_graph = &self.network_graph;
93
94                 let mut previous_scid: u64 = 0;
95                 let announcement_count: u32 = Readable::read(read_cursor)?;
96                 for _ in 0..announcement_count {
97                         let features = Readable::read(read_cursor)?;
98
99                         // handle SCID
100                         let scid_delta: BigSize = Readable::read(read_cursor)?;
101                         let short_channel_id = previous_scid
102                                 .checked_add(scid_delta.0)
103                                 .ok_or(DecodeError::InvalidValue)?;
104                         previous_scid = short_channel_id;
105
106                         let node_id_1_index: BigSize = Readable::read(read_cursor)?;
107                         let node_id_2_index: BigSize = Readable::read(read_cursor)?;
108
109                         if max(node_id_1_index.0, node_id_2_index.0) >= node_id_count as u64 {
110                                 return Err(DecodeError::InvalidValue.into());
111                         };
112                         let node_id_1 = node_ids[node_id_1_index.0 as usize];
113                         let node_id_2 = node_ids[node_id_2_index.0 as usize];
114
115                         log_gossip!(self.logger, "Adding channel {} from RGS announcement at {}",
116                                 short_channel_id, latest_seen_timestamp);
117
118                         let announcement_result = network_graph.add_channel_from_partial_announcement(
119                                 short_channel_id,
120                                 backdated_timestamp as u64,
121                                 features,
122                                 node_id_1,
123                                 node_id_2,
124                         );
125                         if let Err(lightning_error) = announcement_result {
126                                 if let ErrorAction::IgnoreDuplicateGossip = lightning_error.action {
127                                         // everything is fine, just a duplicate channel announcement
128                                 } else {
129                                         log_warn!(self.logger, "Failed to process channel announcement: {:?}", lightning_error);
130                                         return Err(lightning_error.into());
131                                 }
132                         }
133                 }
134
135                 previous_scid = 0; // updates start at a new scid
136
137                 let update_count: u32 = Readable::read(read_cursor)?;
138                 log_debug!(self.logger, "Processing RGS update from {} with {} nodes, {} channel announcements and {} channel updates.",
139                         latest_seen_timestamp, node_id_count, announcement_count, update_count);
140                 if update_count == 0 {
141                         return Ok(latest_seen_timestamp);
142                 }
143
144                 // obtain default values for non-incremental updates
145                 let default_cltv_expiry_delta: u16 = Readable::read(&mut read_cursor)?;
146                 let default_htlc_minimum_msat: u64 = Readable::read(&mut read_cursor)?;
147                 let default_fee_base_msat: u32 = Readable::read(&mut read_cursor)?;
148                 let default_fee_proportional_millionths: u32 = Readable::read(&mut read_cursor)?;
149                 let default_htlc_maximum_msat: u64 = Readable::read(&mut read_cursor)?;
150
151                 for _ in 0..update_count {
152                         let scid_delta: BigSize = Readable::read(read_cursor)?;
153                         let short_channel_id = previous_scid
154                                 .checked_add(scid_delta.0)
155                                 .ok_or(DecodeError::InvalidValue)?;
156                         previous_scid = short_channel_id;
157
158                         let channel_flags: u8 = Readable::read(read_cursor)?;
159
160                         // flags are always sent in full, and hence always need updating
161                         let standard_channel_flags = channel_flags & 0b_0000_0011;
162
163                         let mut synthetic_update = UnsignedChannelUpdate {
164                                 chain_hash,
165                                 short_channel_id,
166                                 timestamp: backdated_timestamp,
167                                 flags: standard_channel_flags,
168                                 cltv_expiry_delta: default_cltv_expiry_delta,
169                                 htlc_minimum_msat: default_htlc_minimum_msat,
170                                 htlc_maximum_msat: default_htlc_maximum_msat,
171                                 fee_base_msat: default_fee_base_msat,
172                                 fee_proportional_millionths: default_fee_proportional_millionths,
173                                 excess_data: Vec::new(),
174                         };
175
176                         let mut skip_update_for_unknown_channel = false;
177
178                         if (channel_flags & 0b_1000_0000) != 0 {
179                                 // incremental update, field flags will indicate mutated values
180                                 let read_only_network_graph = network_graph.read_only();
181                                 if let Some(directional_info) =
182                                         read_only_network_graph.channels().get(&short_channel_id)
183                                         .and_then(|channel| channel.get_directional_info(channel_flags))
184                                 {
185                                         synthetic_update.cltv_expiry_delta = directional_info.cltv_expiry_delta;
186                                         synthetic_update.htlc_minimum_msat = directional_info.htlc_minimum_msat;
187                                         synthetic_update.htlc_maximum_msat = directional_info.htlc_maximum_msat;
188                                         synthetic_update.fee_base_msat = directional_info.fees.base_msat;
189                                         synthetic_update.fee_proportional_millionths = directional_info.fees.proportional_millionths;
190                                 } else {
191                                         log_trace!(self.logger,
192                                                 "Skipping application of channel update for chan {} with flags {} as original data is missing.",
193                                                 short_channel_id, channel_flags);
194                                         skip_update_for_unknown_channel = true;
195                                 }
196                         };
197
198                         if channel_flags & 0b_0100_0000 > 0 {
199                                 let cltv_expiry_delta: u16 = Readable::read(read_cursor)?;
200                                 synthetic_update.cltv_expiry_delta = cltv_expiry_delta;
201                         }
202
203                         if channel_flags & 0b_0010_0000 > 0 {
204                                 let htlc_minimum_msat: u64 = Readable::read(read_cursor)?;
205                                 synthetic_update.htlc_minimum_msat = htlc_minimum_msat;
206                         }
207
208                         if channel_flags & 0b_0001_0000 > 0 {
209                                 let fee_base_msat: u32 = Readable::read(read_cursor)?;
210                                 synthetic_update.fee_base_msat = fee_base_msat;
211                         }
212
213                         if channel_flags & 0b_0000_1000 > 0 {
214                                 let fee_proportional_millionths: u32 = Readable::read(read_cursor)?;
215                                 synthetic_update.fee_proportional_millionths = fee_proportional_millionths;
216                         }
217
218                         if channel_flags & 0b_0000_0100 > 0 {
219                                 let htlc_maximum_msat: u64 = Readable::read(read_cursor)?;
220                                 synthetic_update.htlc_maximum_msat = htlc_maximum_msat;
221                         }
222
223                         if skip_update_for_unknown_channel {
224                                 continue;
225                         }
226
227                         log_gossip!(self.logger, "Updating channel {} with flags {} from RGS announcement at {}",
228                                 short_channel_id, channel_flags, latest_seen_timestamp);
229                         match network_graph.update_channel_unsigned(&synthetic_update) {
230                                 Ok(_) => {},
231                                 Err(LightningError { action: ErrorAction::IgnoreDuplicateGossip, .. }) => {},
232                                 Err(LightningError { action: ErrorAction::IgnoreAndLog(level), err }) => {
233                                         log_given_level!(self.logger, level, "Failed to apply channel update: {:?}", err);
234                                 },
235                                 Err(LightningError { action: ErrorAction::IgnoreError, .. }) => {},
236                                 Err(e) => return Err(e.into()),
237                         }
238                 }
239
240                 self.network_graph.set_last_rapid_gossip_sync_timestamp(latest_seen_timestamp);
241
242                 if let Some(time) = current_time_unix {
243                         self.network_graph.remove_stale_channels_and_tracking_with_time(time)
244                 }
245
246                 self.is_initial_sync_complete.store(true, Ordering::Release);
247                 log_trace!(self.logger, "Done processing RGS data from {}", latest_seen_timestamp);
248                 Ok(latest_seen_timestamp)
249         }
250 }
251
252 #[cfg(test)]
253 mod tests {
254         use bitcoin::Network;
255
256         #[cfg(feature = "std")]
257         use lightning::ln::msgs::DecodeError;
258
259         use lightning::routing::gossip::NetworkGraph;
260         use lightning::util::test_utils::TestLogger;
261
262         use crate::error::GraphSyncError;
263         use crate::processing::STALE_RGS_UPDATE_AGE_LIMIT_SECS;
264         use crate::RapidGossipSync;
265
266         const VALID_RGS_BINARY: [u8; 300] = [
267                 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
268                 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
269                 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
270                 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
271                 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
272                 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
273                 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
274                 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
275                 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
276                 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
277                 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,
278                 0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 60, 0, 0,
279                 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,
280                 0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 255, 2, 68, 226, 0, 6, 11, 0, 1,
281                 0, 0, 1,
282         ];
283         const VALID_BINARY_TIMESTAMP: u64 = 1642291930;
284
285         #[test]
286         #[cfg(feature = "std")]
287         fn network_graph_fails_to_update_from_clipped_input() {
288                 let logger = TestLogger::new();
289                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
290
291                 let example_input = vec![
292                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
293                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
294                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
295                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
296                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
297                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
298                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
299                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
300                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
301                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
302                         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,
303                         0, 0, 2, 224, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 36, 0, 0,
304                         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,
305                         0, 3, 232, 0, 0, 0,
306                 ];
307                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
308                 let update_result = rapid_sync.update_network_graph(&example_input[..]);
309                 assert!(update_result.is_err());
310                 if let Err(GraphSyncError::DecodeError(DecodeError::ShortRead)) = update_result {
311                         // this is the expected error type
312                 } else {
313                         panic!("Unexpected update result: {:?}", update_result)
314                 }
315         }
316
317         #[test]
318         #[cfg(feature = "std")]
319         fn incremental_only_update_ignores_missing_channel() {
320                 let incremental_update_input = vec![
321                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
322                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
323                         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,
324                         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,
325                         68, 226, 0, 6, 11, 0, 1, 128,
326                 ];
327
328                 let logger = TestLogger::new();
329                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
330
331                 assert_eq!(network_graph.read_only().channels().len(), 0);
332
333                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
334                 let update_result = rapid_sync.update_network_graph(&incremental_update_input[..]);
335                 assert!(update_result.is_ok());
336         }
337
338         #[test]
339         #[cfg(feature = "std")]
340         fn incremental_only_update_fails_without_prior_updates() {
341                 let announced_update_input = vec![
342                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
343                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
344                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
345                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
346                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
347                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
348                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
349                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
350                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
351                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
352                         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,
353                         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,
354                         2, 68, 226, 0, 6, 11, 0, 1, 128,
355                 ];
356
357                 let logger = TestLogger::new();
358                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
359
360                 assert_eq!(network_graph.read_only().channels().len(), 0);
361
362                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
363                 rapid_sync.update_network_graph(&announced_update_input[..]).unwrap();
364         }
365
366         #[test]
367         #[cfg(feature = "std")]
368         fn incremental_only_update_fails_without_prior_same_direction_updates() {
369                 let initialization_input = vec![
370                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
371                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
372                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
373                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
374                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
375                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
376                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
377                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
378                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
379                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
380                         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,
381                         0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 25, 0, 0,
382                         0, 1, 0, 0, 0, 125, 255, 2, 68, 226, 0, 6, 11, 0, 1, 5, 0, 0, 0, 0, 29, 129, 25, 192,
383                 ];
384
385                 let logger = TestLogger::new();
386                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
387
388                 assert_eq!(network_graph.read_only().channels().len(), 0);
389
390                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
391                 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
392                 if initialization_result.is_err() {
393                         panic!(
394                                 "Unexpected initialization result: {:?}",
395                                 initialization_result
396                         )
397                 }
398
399                 assert_eq!(network_graph.read_only().channels().len(), 2);
400                 let initialized = network_graph.to_string();
401                 assert!(initialized
402                         .contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643"));
403                 assert!(initialized
404                         .contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b"));
405                 assert!(initialized
406                         .contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432"));
407                 assert!(initialized
408                         .contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61"));
409                 assert!(initialized.contains("619737530008010752"));
410                 assert!(initialized.contains("783241506229452801"));
411
412                 let opposite_direction_incremental_update_input = vec![
413                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
414                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
415                         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,
416                         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,
417                         68, 226, 0, 6, 11, 0, 1, 128,
418                 ];
419                 rapid_sync.update_network_graph(&opposite_direction_incremental_update_input[..]).unwrap();
420         }
421
422         #[test]
423         #[cfg(feature = "std")]
424         fn incremental_update_succeeds_with_prior_announcements_and_full_updates() {
425                 let initialization_input = vec![
426                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
427                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
428                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
429                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
430                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
431                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
432                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
433                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
434                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
435                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
436                         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,
437                         0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 56, 0, 0,
438                         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,
439                         68, 226, 0, 6, 11, 0, 1, 4, 0, 0, 0, 0, 29, 129, 25, 192, 0, 5, 0, 0, 0, 0, 29, 129,
440                         25, 192,
441                 ];
442
443                 let logger = TestLogger::new();
444                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
445
446                 assert_eq!(network_graph.read_only().channels().len(), 0);
447
448                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
449                 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
450                 assert!(initialization_result.is_ok());
451
452                 let single_direction_incremental_update_input = vec![
453                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
454                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
455                         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,
456                         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,
457                         68, 226, 0, 6, 11, 0, 1, 128,
458                 ];
459                 let update_result = rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
460                 if update_result.is_err() {
461                         panic!("Unexpected update result: {:?}", update_result)
462                 }
463
464                 assert_eq!(network_graph.read_only().channels().len(), 2);
465                 let after = network_graph.to_string();
466                 assert!(
467                         after.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643")
468                 );
469                 assert!(
470                         after.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b")
471                 );
472                 assert!(
473                         after.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432")
474                 );
475                 assert!(
476                         after.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61")
477                 );
478                 assert!(after.contains("619737530008010752"));
479                 assert!(after.contains("783241506229452801"));
480         }
481
482         #[test]
483         #[cfg(feature = "std")]
484         fn update_succeeds_when_duplicate_gossip_is_applied() {
485                 let initialization_input = vec![
486                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
487                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
488                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
489                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
490                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
491                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
492                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
493                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
494                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
495                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
496                         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,
497                         0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 56, 0, 0,
498                         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,
499                         68, 226, 0, 6, 11, 0, 1, 4, 0, 0, 0, 0, 29, 129, 25, 192, 0, 5, 0, 0, 0, 0, 29, 129,
500                         25, 192,
501                 ];
502
503                 let logger = TestLogger::new();
504                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
505
506                 assert_eq!(network_graph.read_only().channels().len(), 0);
507
508                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
509                 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
510                 assert!(initialization_result.is_ok());
511
512                 let single_direction_incremental_update_input = vec![
513                         76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
514                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
515                         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,
516                         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,
517                         68, 226, 0, 6, 11, 0, 1, 128,
518                 ];
519                 let update_result_1 = rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
520                 // Apply duplicate update
521                 let update_result_2 = rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
522                 assert!(update_result_1.is_ok());
523                 assert!(update_result_2.is_ok());
524         }
525
526         #[test]
527         #[cfg(feature = "std")]
528         fn full_update_succeeds() {
529                 let logger = TestLogger::new();
530                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
531
532                 assert_eq!(network_graph.read_only().channels().len(), 0);
533
534                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
535                 let update_result = rapid_sync.update_network_graph(&VALID_RGS_BINARY);
536                 if update_result.is_err() {
537                         panic!("Unexpected update result: {:?}", update_result)
538                 }
539
540                 assert_eq!(network_graph.read_only().channels().len(), 2);
541                 let after = network_graph.to_string();
542                 assert!(
543                         after.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643")
544                 );
545                 assert!(
546                         after.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b")
547                 );
548                 assert!(
549                         after.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432")
550                 );
551                 assert!(
552                         after.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61")
553                 );
554                 assert!(after.contains("619737530008010752"));
555                 assert!(after.contains("783241506229452801"));
556         }
557
558         #[test]
559         fn full_update_succeeds_at_the_beginning_of_the_unix_era() {
560                 let logger = TestLogger::new();
561                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
562
563                 assert_eq!(network_graph.read_only().channels().len(), 0);
564
565                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
566                 // this is mostly for checking uint underflow issues before the fuzzer does
567                 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(0));
568                 assert!(update_result.is_ok());
569                 assert_eq!(network_graph.read_only().channels().len(), 2);
570         }
571
572         #[test]
573         fn prunes_after_update() {
574                 // this is the timestamp encoded in the binary data of valid_input below
575                 let logger = TestLogger::new();
576
577                 let latest_nonpruning_time = VALID_BINARY_TIMESTAMP + 60 * 60 * 24 * 7;
578
579                 {
580                         let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
581                         assert_eq!(network_graph.read_only().channels().len(), 0);
582
583                         let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
584                         let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time));
585                         assert!(update_result.is_ok());
586                         assert_eq!(network_graph.read_only().channels().len(), 2);
587                 }
588
589                 {
590                         let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
591                         assert_eq!(network_graph.read_only().channels().len(), 0);
592
593                         let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
594                         let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time + 1));
595                         assert!(update_result.is_ok());
596                         assert_eq!(network_graph.read_only().channels().len(), 0);
597                 }
598         }
599
600         #[test]
601         fn timestamp_edge_cases_are_handled_correctly() {
602                 // this is the timestamp encoded in the binary data of valid_input below
603                 let logger = TestLogger::new();
604
605                 let latest_succeeding_time = VALID_BINARY_TIMESTAMP + STALE_RGS_UPDATE_AGE_LIMIT_SECS;
606                 let earliest_failing_time = latest_succeeding_time + 1;
607
608                 {
609                         let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
610                         assert_eq!(network_graph.read_only().channels().len(), 0);
611
612                         let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
613                         let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_succeeding_time));
614                         assert!(update_result.is_ok());
615                         assert_eq!(network_graph.read_only().channels().len(), 0);
616                 }
617
618                 {
619                         let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
620                         assert_eq!(network_graph.read_only().channels().len(), 0);
621
622                         let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
623                         let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(earliest_failing_time));
624                         assert!(update_result.is_err());
625                         if let Err(GraphSyncError::LightningError(lightning_error)) = update_result {
626                                 assert_eq!(
627                                         lightning_error.err,
628                                         "Rapid Gossip Sync data is more than two weeks old"
629                                 );
630                         } else {
631                                 panic!("Unexpected update result: {:?}", update_result)
632                         }
633                 }
634         }
635
636         #[test]
637         #[cfg(feature = "std")]
638         pub fn update_fails_with_unknown_version() {
639                 let unknown_version_input = vec![
640                         76, 68, 75, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
641                         79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
642                         0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
643                         187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
644                         157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
645                         88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
646                         204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
647                         181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
648                         110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
649                         76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
650                         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,
651                         0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 60, 0, 0,
652                         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,
653                         0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 255, 2, 68, 226, 0, 6, 11, 0, 1,
654                         0, 0, 1,
655                 ];
656
657                 let logger = TestLogger::new();
658                 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
659                 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
660                 let update_result = rapid_sync.update_network_graph(&unknown_version_input[..]);
661
662                 assert!(update_result.is_err());
663
664                 if let Err(GraphSyncError::DecodeError(DecodeError::UnknownVersion)) = update_result {
665                         // this is the expected error type
666                 } else {
667                         panic!("Unexpected update result: {:?}", update_result)
668                 }
669         }
670 }