3 use core::sync::atomic::Ordering;
5 use bitcoin::blockdata::constants::ChainHash;
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::{log_debug, log_warn, log_trace, log_given_level, log_gossip};
14 use lightning::util::ser::{BigSize, Readable};
17 use crate::error::GraphSyncError;
18 use crate::RapidGossipSync;
20 #[cfg(all(feature = "std", not(test)))]
21 use std::time::{SystemTime, UNIX_EPOCH};
23 #[cfg(not(feature = "std"))]
24 use alloc::{vec::Vec, borrow::ToOwned};
26 /// The purpose of this prefix is to identify the serialization format, should other rapid gossip
27 /// sync formats arise in the future.
29 /// The fourth byte is the protocol version in case our format gets updated.
30 const GOSSIP_PREFIX: [u8; 4] = [76, 68, 75, 1];
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;
36 /// We disallow gossip data that's more than two weeks old, per BOLT 7's
38 const STALE_RGS_UPDATE_AGE_LIMIT_SECS: u64 = 60 * 60 * 24 * 14;
40 impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L::Target: Logger {
41 #[cfg(feature = "std")]
42 pub(crate) fn update_network_graph_from_byte_stream<R: io::Read>(
45 ) -> Result<u32, GraphSyncError> {
46 #[allow(unused_mut, unused_assignments)]
47 let mut current_time_unix = None;
50 // Note that many tests rely on being able to set arbitrarily old timestamps, thus we
51 // disable this check during tests!
52 current_time_unix = Some(SystemTime::now().duration_since(UNIX_EPOCH).expect("Time must be > 1970").as_secs());
54 self.update_network_graph_from_byte_stream_no_std(read_cursor, current_time_unix)
57 pub(crate) fn update_network_graph_from_byte_stream_no_std<R: io::Read>(
59 mut read_cursor: &mut R,
60 current_time_unix: Option<u64>
61 ) -> Result<u32, GraphSyncError> {
62 log_trace!(self.logger, "Processing RGS data...");
63 let mut prefix = [0u8; 4];
64 read_cursor.read_exact(&mut prefix)?;
66 if prefix != GOSSIP_PREFIX {
67 return Err(DecodeError::UnknownVersion.into());
70 let chain_hash: ChainHash = Readable::read(read_cursor)?;
71 let ng_chain_hash = self.network_graph.get_chain_hash();
72 if chain_hash != ng_chain_hash {
75 err: "Rapid Gossip Sync data's chain hash does not match the network graph's".to_owned(),
76 action: ErrorAction::IgnoreError,
81 let latest_seen_timestamp: u32 = Readable::read(read_cursor)?;
83 if let Some(time) = current_time_unix {
84 if (latest_seen_timestamp as u64) < time.saturating_sub(STALE_RGS_UPDATE_AGE_LIMIT_SECS) {
85 return Err(LightningError{err: "Rapid Gossip Sync data is more than two weeks old".to_owned(), action: ErrorAction::IgnoreError}.into());
89 // backdate the applied timestamp by a week
90 let backdated_timestamp = latest_seen_timestamp.saturating_sub(24 * 3600 * 7);
92 let node_id_count: u32 = Readable::read(read_cursor)?;
93 let mut node_ids: Vec<PublicKey> = Vec::with_capacity(core::cmp::min(
95 MAX_INITIAL_NODE_ID_VECTOR_CAPACITY,
97 for _ in 0..node_id_count {
98 let current_node_id = Readable::read(read_cursor)?;
99 node_ids.push(current_node_id);
102 let network_graph = &self.network_graph;
104 let mut previous_scid: u64 = 0;
105 let announcement_count: u32 = Readable::read(read_cursor)?;
106 for _ in 0..announcement_count {
107 let features = Readable::read(read_cursor)?;
110 let scid_delta: BigSize = Readable::read(read_cursor)?;
111 let short_channel_id = previous_scid
112 .checked_add(scid_delta.0)
113 .ok_or(DecodeError::InvalidValue)?;
114 previous_scid = short_channel_id;
116 let node_id_1_index: BigSize = Readable::read(read_cursor)?;
117 let node_id_2_index: BigSize = Readable::read(read_cursor)?;
119 if max(node_id_1_index.0, node_id_2_index.0) >= node_id_count as u64 {
120 return Err(DecodeError::InvalidValue.into());
122 let node_id_1 = node_ids[node_id_1_index.0 as usize];
123 let node_id_2 = node_ids[node_id_2_index.0 as usize];
125 log_gossip!(self.logger, "Adding channel {} from RGS announcement at {}",
126 short_channel_id, latest_seen_timestamp);
128 let announcement_result = network_graph.add_channel_from_partial_announcement(
130 backdated_timestamp as u64,
135 if let Err(lightning_error) = announcement_result {
136 if let ErrorAction::IgnoreDuplicateGossip = lightning_error.action {
137 // everything is fine, just a duplicate channel announcement
139 log_warn!(self.logger, "Failed to process channel announcement: {:?}", lightning_error);
140 return Err(lightning_error.into());
145 previous_scid = 0; // updates start at a new scid
147 let update_count: u32 = Readable::read(read_cursor)?;
148 log_debug!(self.logger, "Processing RGS update from {} with {} nodes, {} channel announcements and {} channel updates.",
149 latest_seen_timestamp, node_id_count, announcement_count, update_count);
150 if update_count == 0 {
151 return Ok(latest_seen_timestamp);
154 // obtain default values for non-incremental updates
155 let default_cltv_expiry_delta: u16 = Readable::read(&mut read_cursor)?;
156 let default_htlc_minimum_msat: u64 = Readable::read(&mut read_cursor)?;
157 let default_fee_base_msat: u32 = Readable::read(&mut read_cursor)?;
158 let default_fee_proportional_millionths: u32 = Readable::read(&mut read_cursor)?;
159 let default_htlc_maximum_msat: u64 = Readable::read(&mut read_cursor)?;
161 for _ in 0..update_count {
162 let scid_delta: BigSize = Readable::read(read_cursor)?;
163 let short_channel_id = previous_scid
164 .checked_add(scid_delta.0)
165 .ok_or(DecodeError::InvalidValue)?;
166 previous_scid = short_channel_id;
168 let channel_flags: u8 = Readable::read(read_cursor)?;
170 // flags are always sent in full, and hence always need updating
171 let standard_channel_flags = channel_flags & 0b_0000_0011;
173 let mut synthetic_update = UnsignedChannelUpdate {
176 timestamp: backdated_timestamp,
177 flags: standard_channel_flags,
178 cltv_expiry_delta: default_cltv_expiry_delta,
179 htlc_minimum_msat: default_htlc_minimum_msat,
180 htlc_maximum_msat: default_htlc_maximum_msat,
181 fee_base_msat: default_fee_base_msat,
182 fee_proportional_millionths: default_fee_proportional_millionths,
183 excess_data: Vec::new(),
186 let mut skip_update_for_unknown_channel = false;
188 if (channel_flags & 0b_1000_0000) != 0 {
189 // incremental update, field flags will indicate mutated values
190 let read_only_network_graph = network_graph.read_only();
191 if let Some(directional_info) =
192 read_only_network_graph.channels().get(&short_channel_id)
193 .and_then(|channel| channel.get_directional_info(channel_flags))
195 synthetic_update.cltv_expiry_delta = directional_info.cltv_expiry_delta;
196 synthetic_update.htlc_minimum_msat = directional_info.htlc_minimum_msat;
197 synthetic_update.htlc_maximum_msat = directional_info.htlc_maximum_msat;
198 synthetic_update.fee_base_msat = directional_info.fees.base_msat;
199 synthetic_update.fee_proportional_millionths = directional_info.fees.proportional_millionths;
201 log_trace!(self.logger,
202 "Skipping application of channel update for chan {} with flags {} as original data is missing.",
203 short_channel_id, channel_flags);
204 skip_update_for_unknown_channel = true;
208 if channel_flags & 0b_0100_0000 > 0 {
209 let cltv_expiry_delta: u16 = Readable::read(read_cursor)?;
210 synthetic_update.cltv_expiry_delta = cltv_expiry_delta;
213 if channel_flags & 0b_0010_0000 > 0 {
214 let htlc_minimum_msat: u64 = Readable::read(read_cursor)?;
215 synthetic_update.htlc_minimum_msat = htlc_minimum_msat;
218 if channel_flags & 0b_0001_0000 > 0 {
219 let fee_base_msat: u32 = Readable::read(read_cursor)?;
220 synthetic_update.fee_base_msat = fee_base_msat;
223 if channel_flags & 0b_0000_1000 > 0 {
224 let fee_proportional_millionths: u32 = Readable::read(read_cursor)?;
225 synthetic_update.fee_proportional_millionths = fee_proportional_millionths;
228 if channel_flags & 0b_0000_0100 > 0 {
229 let htlc_maximum_msat: u64 = Readable::read(read_cursor)?;
230 synthetic_update.htlc_maximum_msat = htlc_maximum_msat;
233 if skip_update_for_unknown_channel {
237 log_gossip!(self.logger, "Updating channel {} with flags {} from RGS announcement at {}",
238 short_channel_id, channel_flags, latest_seen_timestamp);
239 match network_graph.update_channel_unsigned(&synthetic_update) {
241 Err(LightningError { action: ErrorAction::IgnoreDuplicateGossip, .. }) => {},
242 Err(LightningError { action: ErrorAction::IgnoreAndLog(level), err }) => {
243 log_given_level!(self.logger, level, "Failed to apply channel update: {:?}", err);
245 Err(LightningError { action: ErrorAction::IgnoreError, .. }) => {},
246 Err(e) => return Err(e.into()),
250 self.network_graph.set_last_rapid_gossip_sync_timestamp(latest_seen_timestamp);
252 if let Some(time) = current_time_unix {
253 self.network_graph.remove_stale_channels_and_tracking_with_time(time)
256 self.is_initial_sync_complete.store(true, Ordering::Release);
257 log_trace!(self.logger, "Done processing RGS data from {}", latest_seen_timestamp);
258 Ok(latest_seen_timestamp)
264 use bitcoin::Network;
266 #[cfg(feature = "std")]
267 use lightning::ln::msgs::DecodeError;
269 use lightning::routing::gossip::NetworkGraph;
270 use lightning::util::test_utils::TestLogger;
272 use crate::error::GraphSyncError;
273 use crate::processing::STALE_RGS_UPDATE_AGE_LIMIT_SECS;
274 use crate::RapidGossipSync;
276 const VALID_RGS_BINARY: [u8; 300] = [
277 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
278 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
279 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
280 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
281 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
282 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
283 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
284 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
285 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
286 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
287 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,
288 0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 60, 0, 0,
289 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,
290 0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 255, 2, 68, 226, 0, 6, 11, 0, 1,
293 const VALID_BINARY_TIMESTAMP: u64 = 1642291930;
296 #[cfg(feature = "std")]
297 fn network_graph_fails_to_update_from_clipped_input() {
298 let logger = TestLogger::new();
299 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
301 let example_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, 227, 98, 218,
304 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
305 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
306 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
307 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
308 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
309 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
310 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
311 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
312 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,
313 0, 0, 2, 224, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 36, 0, 0,
314 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,
317 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
318 let update_result = rapid_sync.update_network_graph(&example_input[..]);
319 assert!(update_result.is_err());
320 if let Err(GraphSyncError::DecodeError(DecodeError::ShortRead)) = update_result {
321 // this is the expected error type
323 panic!("Unexpected update result: {:?}", update_result)
328 #[cfg(feature = "std")]
329 fn incremental_only_update_ignores_missing_channel() {
330 let incremental_update_input = vec![
331 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
332 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
333 0, 0, 0, 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,
334 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,
335 68, 226, 0, 6, 11, 0, 1, 128,
338 let logger = TestLogger::new();
339 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
341 assert_eq!(network_graph.read_only().channels().len(), 0);
343 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
344 let update_result = rapid_sync.update_network_graph(&incremental_update_input[..]);
345 assert!(update_result.is_ok());
349 #[cfg(feature = "std")]
350 fn incremental_only_update_fails_without_prior_updates() {
351 let announced_update_input = vec![
352 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
353 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
354 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
355 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
356 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
357 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
358 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
359 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
360 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
361 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
362 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,
363 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,
364 2, 68, 226, 0, 6, 11, 0, 1, 128,
367 let logger = TestLogger::new();
368 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
370 assert_eq!(network_graph.read_only().channels().len(), 0);
372 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
373 rapid_sync.update_network_graph(&announced_update_input[..]).unwrap();
377 #[cfg(feature = "std")]
378 fn incremental_only_update_fails_without_prior_same_direction_updates() {
379 let initialization_input = vec![
380 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
381 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
382 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
383 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
384 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
385 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
386 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
387 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
388 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
389 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
390 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,
391 0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 25, 0, 0,
392 0, 1, 0, 0, 0, 125, 255, 2, 68, 226, 0, 6, 11, 0, 1, 5, 0, 0, 0, 0, 29, 129, 25, 192,
395 let logger = TestLogger::new();
396 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
398 assert_eq!(network_graph.read_only().channels().len(), 0);
400 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
401 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
402 if initialization_result.is_err() {
404 "Unexpected initialization result: {:?}",
405 initialization_result
409 assert_eq!(network_graph.read_only().channels().len(), 2);
410 let initialized = network_graph.to_string();
412 .contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643"));
414 .contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b"));
416 .contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432"));
418 .contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61"));
419 assert!(initialized.contains("619737530008010752"));
420 assert!(initialized.contains("783241506229452801"));
422 let opposite_direction_incremental_update_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, 229, 183, 167,
425 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,
426 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,
427 68, 226, 0, 6, 11, 0, 1, 128,
429 rapid_sync.update_network_graph(&opposite_direction_incremental_update_input[..]).unwrap();
433 #[cfg(feature = "std")]
434 fn incremental_update_succeeds_with_prior_announcements_and_full_updates() {
435 let initialization_input = vec![
436 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
437 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
438 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
439 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
440 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
441 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
442 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
443 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
444 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
445 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
446 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,
447 0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 56, 0, 0,
448 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,
449 68, 226, 0, 6, 11, 0, 1, 4, 0, 0, 0, 0, 29, 129, 25, 192, 0, 5, 0, 0, 0, 0, 29, 129,
453 let logger = TestLogger::new();
454 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
456 assert_eq!(network_graph.read_only().channels().len(), 0);
458 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
459 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
460 assert!(initialization_result.is_ok());
462 let single_direction_incremental_update_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, 229, 183, 167,
465 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,
466 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,
467 68, 226, 0, 6, 11, 0, 1, 128,
469 let update_result = rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
470 if update_result.is_err() {
471 panic!("Unexpected update result: {:?}", update_result)
474 assert_eq!(network_graph.read_only().channels().len(), 2);
475 let after = network_graph.to_string();
477 after.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643")
480 after.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b")
483 after.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432")
486 after.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61")
488 assert!(after.contains("619737530008010752"));
489 assert!(after.contains("783241506229452801"));
493 #[cfg(feature = "std")]
494 fn update_succeeds_when_duplicate_gossip_is_applied() {
495 let initialization_input = vec![
496 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
497 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
498 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
499 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
500 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
501 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
502 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
503 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
504 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
505 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
506 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,
507 0, 0, 0, 1, 0, 0, 0, 0, 58, 85, 116, 216, 255, 8, 153, 192, 0, 2, 27, 0, 0, 56, 0, 0,
508 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,
509 68, 226, 0, 6, 11, 0, 1, 4, 0, 0, 0, 0, 29, 129, 25, 192, 0, 5, 0, 0, 0, 0, 29, 129,
513 let logger = TestLogger::new();
514 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
516 assert_eq!(network_graph.read_only().channels().len(), 0);
518 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
519 let initialization_result = rapid_sync.update_network_graph(&initialization_input[..]);
520 assert!(initialization_result.is_ok());
522 let single_direction_incremental_update_input = vec![
523 76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
524 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 229, 183, 167,
525 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,
526 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,
527 68, 226, 0, 6, 11, 0, 1, 128,
529 let update_result_1 = rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
530 // Apply duplicate update
531 let update_result_2 = rapid_sync.update_network_graph(&single_direction_incremental_update_input[..]);
532 assert!(update_result_1.is_ok());
533 assert!(update_result_2.is_ok());
537 #[cfg(feature = "std")]
538 fn full_update_succeeds() {
539 let logger = TestLogger::new();
540 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
542 assert_eq!(network_graph.read_only().channels().len(), 0);
544 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
545 let update_result = rapid_sync.update_network_graph(&VALID_RGS_BINARY);
546 if update_result.is_err() {
547 panic!("Unexpected update result: {:?}", update_result)
550 assert_eq!(network_graph.read_only().channels().len(), 2);
551 let after = network_graph.to_string();
553 after.contains("021607cfce19a4c5e7e6e738663dfafbbbac262e4ff76c2c9b30dbeefc35c00643")
556 after.contains("02247d9db0dfafea745ef8c9e161eb322f73ac3f8858d8730b6fd97254747ce76b")
559 after.contains("029e01f279986acc83ba235d46d80aede0b7595f410353b93a8ab540bb677f4432")
562 after.contains("02c913118a8895b9e29c89af6e20ed00d95a1f64e4952edbafa84d048f26804c61")
564 assert!(after.contains("619737530008010752"));
565 assert!(after.contains("783241506229452801"));
569 fn full_update_succeeds_at_the_beginning_of_the_unix_era() {
570 let logger = TestLogger::new();
571 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
573 assert_eq!(network_graph.read_only().channels().len(), 0);
575 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
576 // this is mostly for checking uint underflow issues before the fuzzer does
577 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(0));
578 assert!(update_result.is_ok());
579 assert_eq!(network_graph.read_only().channels().len(), 2);
583 fn prunes_after_update() {
584 // this is the timestamp encoded in the binary data of valid_input below
585 let logger = TestLogger::new();
587 let latest_nonpruning_time = VALID_BINARY_TIMESTAMP + 60 * 60 * 24 * 7;
590 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
591 assert_eq!(network_graph.read_only().channels().len(), 0);
593 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
594 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time));
595 assert!(update_result.is_ok());
596 assert_eq!(network_graph.read_only().channels().len(), 2);
600 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
601 assert_eq!(network_graph.read_only().channels().len(), 0);
603 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
604 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time + 1));
605 assert!(update_result.is_ok());
606 assert_eq!(network_graph.read_only().channels().len(), 0);
611 fn timestamp_edge_cases_are_handled_correctly() {
612 // this is the timestamp encoded in the binary data of valid_input below
613 let logger = TestLogger::new();
615 let latest_succeeding_time = VALID_BINARY_TIMESTAMP + STALE_RGS_UPDATE_AGE_LIMIT_SECS;
616 let earliest_failing_time = latest_succeeding_time + 1;
619 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
620 assert_eq!(network_graph.read_only().channels().len(), 0);
622 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
623 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_succeeding_time));
624 assert!(update_result.is_ok());
625 assert_eq!(network_graph.read_only().channels().len(), 0);
629 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
630 assert_eq!(network_graph.read_only().channels().len(), 0);
632 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
633 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(earliest_failing_time));
634 assert!(update_result.is_err());
635 if let Err(GraphSyncError::LightningError(lightning_error)) = update_result {
638 "Rapid Gossip Sync data is more than two weeks old"
641 panic!("Unexpected update result: {:?}", update_result)
647 #[cfg(feature = "std")]
648 pub fn update_fails_with_unknown_version() {
649 let unknown_version_input = vec![
650 76, 68, 75, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
651 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
652 0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
653 187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
654 157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
655 88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
656 204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
657 181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
658 110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
659 76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
660 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,
661 0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 60, 0, 0,
662 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,
663 0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 255, 2, 68, 226, 0, 6, 11, 0, 1,
667 let logger = TestLogger::new();
668 let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
669 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
670 let update_result = rapid_sync.update_network_graph(&unknown_version_input[..]);
672 assert!(update_result.is_err());
674 if let Err(GraphSyncError::DecodeError(DecodeError::UnknownVersion)) = update_result {
675 // this is the expected error type
677 panic!("Unexpected update result: {:?}", update_result)
682 fn fails_early_on_chain_hash_mismatch() {
683 let logger = TestLogger::new();
684 // Set to testnet so that the VALID_RGS_BINARY chain hash of mainnet does not match.
685 let network_graph = NetworkGraph::new(Network::Testnet, &logger);
687 assert_eq!(network_graph.read_only().channels().len(), 0);
689 let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
690 let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(0));
691 assert!(update_result.is_err());
692 if let Err(GraphSyncError::LightningError(err)) = update_result {
693 assert_eq!(err.err, "Rapid Gossip Sync data's chain hash does not match the network graph's");
695 panic!("Unexpected update result: {:?}", update_result)