3 use core::sync::atomic::Ordering;
5 use bitcoin::BlockHash;
6 use bitcoin::secp256k1::PublicKey;
8 use lightning::ln::msgs::{
9 DecodeError, ErrorAction, LightningError, UnsignedChannelUpdate,
11 use lightning::routing::gossip::NetworkGraph;
12 use lightning::util::logger::Logger;
13 use lightning::util::ser::{BigSize, Readable};
16 use crate::error::GraphSyncError;
17 use crate::RapidGossipSync;
19 #[cfg(all(feature = "std", not(test)))]
20 use std::time::{SystemTime, UNIX_EPOCH};
22 #[cfg(not(feature = "std"))]
23 use alloc::{vec::Vec, borrow::ToOwned};
25 /// The purpose of this prefix is to identify the serialization format, should other rapid gossip
26 /// sync formats arise in the future.
28 /// The fourth byte is the protocol version in case our format gets updated.
29 const GOSSIP_PREFIX: [u8; 4] = [76, 68, 75, 1];
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;
35 /// We disallow gossip data that's more than two weeks old, per BOLT 7's
37 const STALE_RGS_UPDATE_AGE_LIMIT_SECS: u64 = 60 * 60 * 24 * 14;
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>(
43 ) -> Result<u32, GraphSyncError> {
45 let mut current_time_unix = None;
46 #[cfg(all(feature = "std", not(test)))]
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());
52 self.update_network_graph_from_byte_stream_no_std(read_cursor, current_time_unix)
55 pub(crate) fn update_network_graph_from_byte_stream_no_std<R: io::Read>(
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)?;
63 if prefix != GOSSIP_PREFIX {
64 return Err(DecodeError::UnknownVersion.into());
67 let chain_hash: BlockHash = Readable::read(read_cursor)?;
68 let latest_seen_timestamp: u32 = Readable::read(read_cursor)?;
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());
76 // backdate the applied timestamp by a week
77 let backdated_timestamp = latest_seen_timestamp.saturating_sub(24 * 3600 * 7);
79 let node_id_count: u32 = Readable::read(read_cursor)?;
80 let mut node_ids: Vec<PublicKey> = Vec::with_capacity(core::cmp::min(
82 MAX_INITIAL_NODE_ID_VECTOR_CAPACITY,
84 for _ in 0..node_id_count {
85 let current_node_id = Readable::read(read_cursor)?;
86 node_ids.push(current_node_id);
89 let network_graph = &self.network_graph;
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)?;
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;
103 let node_id_1_index: BigSize = Readable::read(read_cursor)?;
104 let node_id_2_index: BigSize = Readable::read(read_cursor)?;
106 if max(node_id_1_index.0, node_id_2_index.0) >= node_id_count as u64 {
107 return Err(DecodeError::InvalidValue.into());
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];
112 let announcement_result = network_graph.add_channel_from_partial_announcement(
114 backdated_timestamp as u64,
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
123 return Err(lightning_error.into());
128 previous_scid = 0; // updates start at a new scid
130 let update_count: u32 = Readable::read(read_cursor)?;
131 if update_count == 0 {
132 return Ok(latest_seen_timestamp);
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)?;
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;
149 let channel_flags: u8 = Readable::read(read_cursor)?;
151 // flags are always sent in full, and hence always need updating
152 let standard_channel_flags = channel_flags & 0b_0000_0011;
154 let mut synthetic_update = UnsignedChannelUpdate {
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(),
167 let mut skip_update_for_unknown_channel = false;
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(directional_info) =
173 read_only_network_graph.channels().get(&short_channel_id)
174 .and_then(|channel| channel.get_directional_info(channel_flags))
176 synthetic_update.cltv_expiry_delta = directional_info.cltv_expiry_delta;
177 synthetic_update.htlc_minimum_msat = directional_info.htlc_minimum_msat;
178 synthetic_update.htlc_maximum_msat = directional_info.htlc_maximum_msat;
179 synthetic_update.fee_base_msat = directional_info.fees.base_msat;
180 synthetic_update.fee_proportional_millionths = directional_info.fees.proportional_millionths;
182 skip_update_for_unknown_channel = true;
186 if channel_flags & 0b_0100_0000 > 0 {
187 let cltv_expiry_delta: u16 = Readable::read(read_cursor)?;
188 synthetic_update.cltv_expiry_delta = cltv_expiry_delta;
191 if channel_flags & 0b_0010_0000 > 0 {
192 let htlc_minimum_msat: u64 = Readable::read(read_cursor)?;
193 synthetic_update.htlc_minimum_msat = htlc_minimum_msat;
196 if channel_flags & 0b_0001_0000 > 0 {
197 let fee_base_msat: u32 = Readable::read(read_cursor)?;
198 synthetic_update.fee_base_msat = fee_base_msat;
201 if channel_flags & 0b_0000_1000 > 0 {
202 let fee_proportional_millionths: u32 = Readable::read(read_cursor)?;
203 synthetic_update.fee_proportional_millionths = fee_proportional_millionths;
206 if channel_flags & 0b_0000_0100 > 0 {
207 let htlc_maximum_msat: u64 = Readable::read(read_cursor)?;
208 synthetic_update.htlc_maximum_msat = htlc_maximum_msat;
211 if skip_update_for_unknown_channel {
215 match network_graph.update_channel_unsigned(&synthetic_update) {
217 Err(LightningError { action: ErrorAction::IgnoreDuplicateGossip, .. }) => {},
218 Err(LightningError { action: ErrorAction::IgnoreAndLog(_), .. }) => {},
219 Err(LightningError { action: ErrorAction::IgnoreError, .. }) => {},
220 Err(e) => return Err(e.into()),
224 self.network_graph.set_last_rapid_gossip_sync_timestamp(latest_seen_timestamp);
225 self.is_initial_sync_complete.store(true, Ordering::Release);
226 Ok(latest_seen_timestamp)
232 use bitcoin::Network;
234 use lightning::ln::msgs::DecodeError;
235 use lightning::routing::gossip::NetworkGraph;
236 use lightning::util::test_utils::TestLogger;
238 use crate::error::GraphSyncError;
239 use crate::processing::STALE_RGS_UPDATE_AGE_LIMIT_SECS;
240 use crate::RapidGossipSync;
242 const VALID_RGS_BINARY: [u8; 300] = [
243 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
244 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
245 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
246 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
247 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
248 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
249 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
250 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
251 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
252 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
253 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,
254 0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 60, 0, 0,
255 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,
256 0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 255, 2, 68, 226, 0, 6, 11, 0, 1,
259 const VALID_BINARY_TIMESTAMP: u64 = 1642291930;
262 fn network_graph_fails_to_update_from_clipped_input() {
263 let logger = TestLogger::new();
264 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
266 let example_input = vec![
267 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
268 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
269 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
270 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
271 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
272 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
273 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
274 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
275 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
276 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
277 226, 0, 6, 11, 0, 1, 2, 3, 0, 0, 0, 2, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 0, 100,
278 0, 0, 2, 224, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 36, 0, 0,
279 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,
282 let rapid_sync = RapidGossipSync::new(&network_graph);
283 let update_result = rapid_sync.update_network_graph(&example_input[..]);
284 assert!(update_result.is_err());
285 if let Err(GraphSyncError::DecodeError(DecodeError::ShortRead)) = update_result {
286 // this is the expected error type
288 panic!("Unexpected update result: {:?}", update_result)
293 fn incremental_only_update_ignores_missing_channel() {
294 let incremental_update_input = vec![
295 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
296 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
297 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,
298 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,
299 68, 226, 0, 6, 11, 0, 1, 128,
302 let logger = TestLogger::new();
303 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
305 assert_eq!(network_graph.read_only().channels().len(), 0);
307 let rapid_sync = RapidGossipSync::new(&network_graph);
308 let update_result = rapid_sync.update_network_graph(&incremental_update_input[..]);
309 assert!(update_result.is_ok());
313 fn incremental_only_update_fails_without_prior_updates() {
314 let announced_update_input = vec![
315 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
316 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
317 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
318 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
319 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
320 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
321 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
322 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
323 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
324 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
325 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,
326 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,
327 2, 68, 226, 0, 6, 11, 0, 1, 128,
330 let logger = TestLogger::new();
331 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
333 assert_eq!(network_graph.read_only().channels().len(), 0);
335 let rapid_sync = RapidGossipSync::new(&network_graph);
336 rapid_sync.update_network_graph(&announced_update_input[..]).unwrap();
340 fn incremental_only_update_fails_without_prior_same_direction_updates() {
341 let initialization_input = vec![
342 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
343 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
344 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
345 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
346 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
347 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
348 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
349 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
350 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
351 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
352 226, 0, 6, 11, 0, 1, 2, 3, 0, 0, 0, 2, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 3, 232,
353 0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 25, 0, 0,
354 0, 1, 0, 0, 0, 125, 255, 2, 68, 226, 0, 6, 11, 0, 1, 5, 0, 0, 0, 0, 29, 129, 25, 192,
357 let logger = TestLogger::new();
358 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
360 assert_eq!(network_graph.read_only().channels().len(), 0);
362 let rapid_sync = RapidGossipSync::new(&network_graph);
363 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
364 if initialization_result.is_err() {
366 "Unexpected initialization result: {:?}",
367 initialization_result
371 assert_eq!(network_graph.read_only().channels().len(), 2);
372 let initialized = network_graph.to_string();
374 .contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643"));
376 .contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b"));
378 .contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432"));
380 .contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61"));
381 assert!(initialized.contains("619737530008010752"));
382 assert!(initialized.contains("783241506229452801"));
384 let opposite_direction_incremental_update_input = vec![
385 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
386 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
387 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,
388 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,
389 68, 226, 0, 6, 11, 0, 1, 128,
391 rapid_sync.update_network_graph(&opposite_direction_incremental_update_input[..]).unwrap();
395 fn incremental_update_succeeds_with_prior_announcements_and_full_updates() {
396 let initialization_input = vec![
397 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
398 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
399 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
400 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
401 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
402 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
403 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
404 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
405 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
406 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
407 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,
408 0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 56, 0, 0,
409 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,
410 68, 226, 0, 6, 11, 0, 1, 4, 0, 0, 0, 0, 29, 129, 25, 192, 0, 5, 0, 0, 0, 0, 29, 129,
414 let logger = TestLogger::new();
415 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
417 assert_eq!(network_graph.read_only().channels().len(), 0);
419 let rapid_sync = RapidGossipSync::new(&network_graph);
420 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
421 assert!(initialization_result.is_ok());
423 let single_direction_incremental_update_input = vec![
424 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
425 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
426 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,
427 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,
428 68, 226, 0, 6, 11, 0, 1, 128,
430 let update_result = rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
431 if update_result.is_err() {
432 panic!("Unexpected update result: {:?}", update_result)
435 assert_eq!(network_graph.read_only().channels().len(), 2);
436 let after = network_graph.to_string();
438 after.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643")
441 after.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b")
444 after.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432")
447 after.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61")
449 assert!(after.contains("619737530008010752"));
450 assert!(after.contains("783241506229452801"));
454 fn update_succeeds_when_duplicate_gossip_is_applied() {
455 let initialization_input = vec![
456 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
457 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
458 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
459 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
460 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
461 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
462 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
463 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
464 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
465 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
466 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,
467 0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 56, 0, 0,
468 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,
469 68, 226, 0, 6, 11, 0, 1, 4, 0, 0, 0, 0, 29, 129, 25, 192, 0, 5, 0, 0, 0, 0, 29, 129,
473 let logger = TestLogger::new();
474 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
476 assert_eq!(network_graph.read_only().channels().len(), 0);
478 let rapid_sync = RapidGossipSync::new(&network_graph);
479 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
480 assert!(initialization_result.is_ok());
482 let single_direction_incremental_update_input = vec![
483 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
484 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
485 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,
486 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,
487 68, 226, 0, 6, 11, 0, 1, 128,
489 let update_result_1 = rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
490 // Apply duplicate update
491 let update_result_2 = rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
492 assert!(update_result_1.is_ok());
493 assert!(update_result_2.is_ok());
497 fn full_update_succeeds() {
498 let logger = TestLogger::new();
499 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
501 assert_eq!(network_graph.read_only().channels().len(), 0);
503 let rapid_sync = RapidGossipSync::new(&network_graph);
504 let update_result = rapid_sync.update_network_graph(&VALID_RGS_BINARY);
505 if update_result.is_err() {
506 panic!("Unexpected update result: {:?}", update_result)
509 assert_eq!(network_graph.read_only().channels().len(), 2);
510 let after = network_graph.to_string();
512 after.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643")
515 after.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b")
518 after.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432")
521 after.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61")
523 assert!(after.contains("619737530008010752"));
524 assert!(after.contains("783241506229452801"));
528 fn full_update_succeeds_at_the_beginning_of_the_unix_era() {
529 let logger = TestLogger::new();
530 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
532 assert_eq!(network_graph.read_only().channels().len(), 0);
534 let rapid_sync = RapidGossipSync::new(&network_graph);
535 // this is mostly for checking uint underflow issues before the fuzzer does
536 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(0));
537 assert!(update_result.is_ok());
538 assert_eq!(network_graph.read_only().channels().len(), 2);
542 fn timestamp_edge_cases_are_handled_correctly() {
543 // this is the timestamp encoded in the binary data of valid_input below
544 let logger = TestLogger::new();
546 let latest_succeeding_time = VALID_BINARY_TIMESTAMP + STALE_RGS_UPDATE_AGE_LIMIT_SECS;
547 let earliest_failing_time = latest_succeeding_time + 1;
550 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
551 assert_eq!(network_graph.read_only().channels().len(), 0);
553 let rapid_sync = RapidGossipSync::new(&network_graph);
554 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_succeeding_time));
555 assert!(update_result.is_ok());
556 assert_eq!(network_graph.read_only().channels().len(), 2);
560 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
561 assert_eq!(network_graph.read_only().channels().len(), 0);
563 let rapid_sync = RapidGossipSync::new(&network_graph);
564 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(earliest_failing_time));
565 assert!(update_result.is_err());
566 if let Err(GraphSyncError::LightningError(lightning_error)) = update_result {
569 "Rapid Gossip Sync data is more than two weeks old"
572 panic!("Unexpected update result: {:?}", update_result)
578 pub fn update_fails_with_unknown_version() {
579 let unknown_version_input = vec![
580 76, 68, 75, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
581 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
582 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
583 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
584 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
585 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
586 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
587 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
588 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
589 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
590 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,
591 0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 60, 0, 0,
592 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,
593 0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 255, 2, 68, 226, 0, 6, 11, 0, 1,
597 let logger = TestLogger::new();
598 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
599 let rapid_sync = RapidGossipSync::new(&network_graph);
600 let update_result = rapid_sync.update_network_graph(&unknown_version_input[..]);
602 assert!(update_result.is_err());
604 if let Err(GraphSyncError::DecodeError(DecodeError::UnknownVersion)) = update_result {
605 // this is the expected error type
607 panic!("Unexpected update result: {:?}", update_result)