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