Skip to content

Commit 30d4572

Browse files
committed
Update timestamp on network graph upon rapid sync completion.
1 parent f299bd3 commit 30d4572

File tree

6 files changed

+47
-27
lines changed

6 files changed

+47
-27
lines changed

fuzz/src/process_network_graph.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
// Import that needs to be added manually
1+
// Imports that need to be added manually
2+
use std::sync::Arc;
3+
4+
use lightning_rapid_gossip_sync::RapidGossipSync;
25
use utils::test_logger;
36

47
/// Actual fuzz test, method signature and name are fixed
58
fn do_test(data: &[u8]) {
69
let block_hash = bitcoin::BlockHash::default();
710
let network_graph = lightning::routing::network_graph::NetworkGraph::new(block_hash);
8-
lightning_rapid_gossip_sync::processing::update_network_graph(&network_graph, data);
11+
let rapid_sync = RapidGossipSync::new(Arc::new(network_graph));
12+
rapid_sync.update_network_graph(data);
913
}
1014

1115
/// Method that needs to be added manually, {name}_test

lightning-background-processor/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ rustdoc-args = ["--cfg", "docsrs"]
1616
[dependencies]
1717
bitcoin = "0.28.1"
1818
lightning = { version = "0.0.106", path = "../lightning", features = ["std"] }
19+
lightning-rapid-gossip-sync = { version = "0.0.106", path = "../lightning-rapid-gossip-sync" }
1920

2021
[dev-dependencies]
2122
lightning = { version = "0.0.106", path = "../lightning", features = ["_test_utils"] }
2223
lightning-invoice = { version = "0.14.0", path = "../lightning-invoice" }
2324
lightning-persister = { version = "0.0.106", path = "../lightning-persister" }
24-
lightning-rapid-gossip-sync = { version = "0.0.106", path = "../lightning-rapid-gossip-sync" }

lightning-background-processor/src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -293,24 +293,24 @@ impl BackgroundProcessor {
293293
if is_initial_sync_complete.load(Ordering::Acquire) {
294294
log_trace!(logger, "Pruning network graph of stale entries");
295295
handler.network_graph().remove_stale_channels();
296+
297+
if let Err(e) = persister.persist_graph(handler.network_graph()) {
298+
log_error!(logger, "Error: Failed to persist network graph, check your disk and permissions {}", e)
299+
}
300+
301+
last_prune_call = Instant::now();
302+
have_pruned = true;
296303
} else {
297304
log_trace!(logger, "Not pruning network graph due to pending gossip sync");
298-
continue;
299-
}
300-
301-
if let Err(e) = persister.persist_graph(handler.network_graph()) {
302-
log_error!(logger, "Error: Failed to persist network graph, check your disk and permissions {}", e)
303-
}
304-
}
305-
if let Some(ref scorer) = scorer {
306-
log_trace!(logger, "Persisting scorer");
307-
if let Err(e) = persister.persist_scorer(&scorer) {
308-
log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
309305
}
310306
}
307+
}
311308

312-
last_prune_call = Instant::now();
313-
have_pruned = true;
309+
if let Some(ref scorer) = scorer {
310+
log_trace!(logger, "Persisting scorer");
311+
if let Err(e) = persister.persist_scorer(&scorer) {
312+
log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
313+
}
314314
}
315315
}
316316

lightning-rapid-gossip-sync/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@
2727
//! its contents from disk, which we do by calling the `sync_network_graph_with_file_path` method:
2828
//!
2929
//! ```
30+
//! use std::sync::Arc;
3031
//! use bitcoin::blockdata::constants::genesis_block;
3132
//! use bitcoin::Network;
3233
//! use lightning::routing::network_graph::NetworkGraph;
34+
//! use lightning_rapid_gossip_sync::RapidGossipSync;
3335
//!
3436
//! let block_hash = genesis_block(Network::Bitcoin).header.block_hash();
35-
//! let network_graph = NetworkGraph::new(block_hash);
36-
//! let new_last_sync_timestamp_result = lightning_rapid_gossip_sync::sync_network_graph_with_file_path(&network_graph, "./rapid_sync.lngossip");
37+
//! let network_graph = Arc::new(NetworkGraph::new(block_hash));
38+
//! let rapid_sync = RapidGossipSync::new(network_graph);
39+
//! let new_last_sync_timestamp_result = rapid_sync.sync_network_graph_with_file_path("./rapid_sync.lngossip");
3740
//! ```
3841
//!
3942
//! The primary benefit this syncing mechanism provides is that given a trusted server, a

lightning-rapid-gossip-sync/src/processing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ impl<NG: Deref<Target=NetworkGraph>> RapidGossipSync<NG> {
226226
network_graph.update_channel_unsigned(&synthetic_update)?;
227227
}
228228

229+
self.network_graph.set_last_rapid_gossip_sync_timestamps(latest_seen_timestamp);
229230
self.is_initial_sync_complete.store(true, Ordering::Release);
230231
Ok(latest_seen_timestamp)
231232
}

lightning/src/routing/network_graph.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ impl Readable for NodeId {
123123

124124
/// Represents the network as nodes and channels between them
125125
pub struct NetworkGraph {
126-
/// The unix timestamp in UTC provided by the most recent rapid gossip sync
127-
/// It will be set by the rapid sync process after every sync completion
128-
pub last_rapid_gossip_sync_timestamp: Option<u32>,
126+
last_rapid_gossip_sync_timestamp: Mutex<Option<u32>>,
129127
genesis_hash: BlockHash,
130128
// Lock order: channels -> nodes
131129
channels: RwLock<BTreeMap<u64, ChannelInfo>>,
@@ -136,11 +134,12 @@ impl Clone for NetworkGraph {
136134
fn clone(&self) -> Self {
137135
let channels = self.channels.read().unwrap();
138136
let nodes = self.nodes.read().unwrap();
137+
let last_rapid_gossip_sync_timestamp = self.get_last_rapid_gossip_sync_timestamp();
139138
Self {
140139
genesis_hash: self.genesis_hash.clone(),
141140
channels: RwLock::new(channels.clone()),
142141
nodes: RwLock::new(nodes.clone()),
143-
last_rapid_gossip_sync_timestamp: self.last_rapid_gossip_sync_timestamp.clone(),
142+
last_rapid_gossip_sync_timestamp: Mutex::new(last_rapid_gossip_sync_timestamp)
144143
}
145144
}
146145
}
@@ -994,8 +993,9 @@ impl Writeable for NetworkGraph {
994993
node_info.write(writer)?;
995994
}
996995

996+
let last_rapid_gossip_sync_timestamp = self.get_last_rapid_gossip_sync_timestamp();
997997
write_tlv_fields!(writer, {
998-
(1, self.last_rapid_gossip_sync_timestamp, option),
998+
(1, last_rapid_gossip_sync_timestamp, option),
999999
});
10001000
Ok(())
10011001
}
@@ -1030,7 +1030,7 @@ impl Readable for NetworkGraph {
10301030
genesis_hash,
10311031
channels: RwLock::new(channels),
10321032
nodes: RwLock::new(nodes),
1033-
last_rapid_gossip_sync_timestamp,
1033+
last_rapid_gossip_sync_timestamp: Mutex::new(last_rapid_gossip_sync_timestamp),
10341034
})
10351035
}
10361036
}
@@ -1064,7 +1064,7 @@ impl NetworkGraph {
10641064
genesis_hash,
10651065
channels: RwLock::new(BTreeMap::new()),
10661066
nodes: RwLock::new(BTreeMap::new()),
1067-
last_rapid_gossip_sync_timestamp: None,
1067+
last_rapid_gossip_sync_timestamp: Mutex::new(None),
10681068
}
10691069
}
10701070

@@ -1078,6 +1078,18 @@ impl NetworkGraph {
10781078
}
10791079
}
10801080

1081+
/// The unix timestamp in UTC provided by the most recent rapid gossip sync.
1082+
/// It will be set by the rapid sync process after every sync completion.
1083+
pub fn get_last_rapid_gossip_sync_timestamp(&self) -> Option<u32> {
1084+
self.last_rapid_gossip_sync_timestamp.lock().unwrap().clone()
1085+
}
1086+
1087+
/// Update the UTC timestamp provided by the most recent rapid gossip sync.
1088+
/// This should be done automatically by the rapid sync process after every sync completion.
1089+
pub fn set_last_rapid_gossip_sync_timestamps(&self, last_rapid_gossip_sync_timestamp: u32) {
1090+
self.last_rapid_gossip_sync_timestamp.lock().unwrap().replace(last_rapid_gossip_sync_timestamp);
1091+
}
1092+
10811093
/// Clears the `NodeAnnouncementInfo` field for all nodes in the `NetworkGraph` for testing
10821094
/// purposes.
10831095
#[cfg(test)]
@@ -2374,13 +2386,13 @@ mod tests {
23742386
#[test]
23752387
fn network_graph_tlv_serialization() {
23762388
let mut network_graph = create_network_graph();
2377-
network_graph.last_rapid_gossip_sync_timestamp.replace(42);
2389+
network_graph.set_last_rapid_gossip_sync_timestamps(42);
23782390

23792391
let mut w = test_utils::TestVecWriter(Vec::new());
23802392
network_graph.write(&mut w).unwrap();
23812393
let reassembled_network_graph: NetworkGraph = Readable::read(&mut io::Cursor::new(&w.0)).unwrap();
23822394
assert!(reassembled_network_graph == network_graph);
2383-
assert_eq!(reassembled_network_graph.last_rapid_gossip_sync_timestamp.unwrap(), 42);
2395+
assert_eq!(reassembled_network_graph.get_last_rapid_gossip_sync_timestamp().unwrap(), 42);
23842396
}
23852397

23862398
#[test]

0 commit comments

Comments
 (0)