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