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