Skip to content

Commit 8df6fbb

Browse files
committed
Create wrapper struct for rapid sync.
1 parent ddbcf5c commit 8df6fbb

File tree

2 files changed

+250
-221
lines changed

2 files changed

+250
-221
lines changed

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

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@
5757
extern crate test;
5858

5959
use std::fs::File;
60+
use std::ops::Deref;
61+
use std::sync::Arc;
62+
use std::sync::atomic::AtomicBool;
6063

61-
use lightning::routing::network_graph;
64+
use lightning::routing::network_graph::NetworkGraph;
6265

6366
use crate::error::GraphSyncError;
6467

@@ -68,32 +71,48 @@ pub mod error;
6871
/// Core functionality of this crate
6972
pub mod processing;
7073

71-
/// Sync gossip data from a file
72-
/// Returns the last sync timestamp to be used the next time rapid sync data is queried.
73-
///
74-
/// `network_graph`: The network graph to apply the updates to
75-
///
76-
/// `sync_path`: Path to the file where the gossip update data is located
77-
///
78-
pub fn sync_network_graph_with_file_path(
79-
network_graph: &network_graph::NetworkGraph,
80-
sync_path: &str,
81-
) -> Result<u32, GraphSyncError> {
82-
let mut file = File::open(sync_path)?;
83-
processing::update_network_graph_from_byte_stream(&network_graph, &mut file)
74+
/// Rapid Gossip Sync holder struct
75+
pub struct RapidGossipSync<NG: Deref<Target=NetworkGraph>> {
76+
network_graph: NG,
77+
/// Atomic bool indicating whether a rapid gossip sync has completed at least once
78+
pub is_initial_sync_complete: Arc<AtomicBool>
79+
}
80+
81+
impl<NG: Deref<Target=NetworkGraph>> RapidGossipSync<NG> {
82+
pub fn new(network_graph: NG) -> Self {
83+
Self {
84+
network_graph,
85+
is_initial_sync_complete: Arc::new(AtomicBool::new(false))
86+
}
87+
}
88+
89+
/// Sync gossip data from a file
90+
/// Returns the last sync timestamp to be used the next time rapid sync data is queried.
91+
///
92+
/// `network_graph`: The network graph to apply the updates to
93+
///
94+
/// `sync_path`: Path to the file where the gossip update data is located
95+
///
96+
pub fn sync_network_graph_with_file_path(
97+
&self,
98+
sync_path: &str,
99+
) -> Result<u32, GraphSyncError> {
100+
let mut file = File::open(sync_path)?;
101+
self.update_network_graph_from_byte_stream(&mut file)
102+
}
84103
}
85104

86105
#[cfg(test)]
87106
mod tests {
88107
use std::fs;
108+
use std::sync::Arc;
89109

90110
use bitcoin::blockdata::constants::genesis_block;
91111
use bitcoin::Network;
92112

93113
use lightning::ln::msgs::DecodeError;
94114
use lightning::routing::network_graph::NetworkGraph;
95-
96-
use crate::sync_network_graph_with_file_path;
115+
use crate::RapidGossipSync;
97116

98117
#[test]
99118
fn test_sync_from_file() {
@@ -152,11 +171,12 @@ mod tests {
152171
let graph_sync_test_file = sync_test.get_test_file_path();
153172

154173
let block_hash = genesis_block(Network::Bitcoin).block_hash();
155-
let network_graph = NetworkGraph::new(block_hash);
174+
let network_graph = Arc::new(NetworkGraph::new(block_hash));
156175

157176
assert_eq!(network_graph.read_only().channels().len(), 0);
158177

159-
let sync_result = sync_network_graph_with_file_path(&network_graph, &graph_sync_test_file);
178+
let rapid_sync = RapidGossipSync::new(network_graph.clone());
179+
let sync_result = rapid_sync.sync_network_graph_with_file_path(&graph_sync_test_file);
160180

161181
if sync_result.is_err() {
162182
panic!("Unexpected sync result: {:?}", sync_result)
@@ -187,9 +207,10 @@ mod tests {
187207

188208
assert_eq!(network_graph.read_only().channels().len(), 0);
189209

210+
let rapid_sync = RapidGossipSync::new(Arc::new(network_graph));
190211
let start = std::time::Instant::now();
191-
let sync_result =
192-
sync_network_graph_with_file_path(&network_graph, "./res/full_graph.lngossip");
212+
let sync_result = rapid_sync
213+
.sync_network_graph_with_file_path("./res/full_graph.lngossip");
193214
if let Err(crate::error::GraphSyncError::DecodeError(DecodeError::Io(io_error))) = &sync_result {
194215
let error_string = format!("Input file lightning-graph-sync/res/full_graph.lngossip is missing! Download it from https://bitcoin.ninja/ldk-compressed_graph-bc08df7542-2022-05-05.bin\n\n{:?}", io_error);
195216
#[cfg(not(require_route_graph_test))]
@@ -218,17 +239,15 @@ pub mod bench {
218239
use lightning::ln::msgs::DecodeError;
219240
use lightning::routing::network_graph::NetworkGraph;
220241

221-
use crate::sync_network_graph_with_file_path;
242+
use crate::{RapidGossipSync, sync_network_graph_with_file_path};
222243

223244
#[bench]
224245
fn bench_reading_full_graph_from_file(b: &mut Bencher) {
225246
let block_hash = genesis_block(Network::Bitcoin).block_hash();
226247
b.iter(|| {
227248
let network_graph = NetworkGraph::new(block_hash);
228-
let sync_result = sync_network_graph_with_file_path(
229-
&network_graph,
230-
"./res/full_graph.lngossip",
231-
);
249+
let rapid_sync = RapidGossipSync::new(network_graph);
250+
let sync_result = rapid_sync.sync_network_graph_with_file_path("./res/full_graph.lngossip");
232251
if let Err(crate::error::GraphSyncError::DecodeError(DecodeError::Io(io_error))) = &sync_result {
233252
let error_string = format!("Input file lightning-graph-sync/res/full_graph.lngossip is missing! Download it from https://bitcoin.ninja/ldk-compressed_graph-bc08df7542-2022-05-05.bin\n\n{:?}", io_error);
234253
#[cfg(not(require_route_graph_test))]

0 commit comments

Comments
 (0)