57
57
extern crate test;
58
58
59
59
use std:: fs:: File ;
60
+ use std:: ops:: Deref ;
61
+ use std:: sync:: Arc ;
62
+ use std:: sync:: atomic:: AtomicBool ;
60
63
61
- use lightning:: routing:: network_graph;
64
+ use lightning:: routing:: network_graph:: NetworkGraph ;
62
65
63
66
use crate :: error:: GraphSyncError ;
64
67
@@ -68,32 +71,48 @@ pub mod error;
68
71
/// Core functionality of this crate
69
72
pub mod processing;
70
73
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
+ }
84
103
}
85
104
86
105
#[ cfg( test) ]
87
106
mod tests {
88
107
use std:: fs;
108
+ use std:: sync:: Arc ;
89
109
90
110
use bitcoin:: blockdata:: constants:: genesis_block;
91
111
use bitcoin:: Network ;
92
112
93
113
use lightning:: ln:: msgs:: DecodeError ;
94
114
use lightning:: routing:: network_graph:: NetworkGraph ;
95
-
96
- use crate :: sync_network_graph_with_file_path;
115
+ use crate :: RapidGossipSync ;
97
116
98
117
#[ test]
99
118
fn test_sync_from_file ( ) {
@@ -152,11 +171,12 @@ mod tests {
152
171
let graph_sync_test_file = sync_test. get_test_file_path ( ) ;
153
172
154
173
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) ) ;
156
175
157
176
assert_eq ! ( network_graph. read_only( ) . channels( ) . len( ) , 0 ) ;
158
177
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) ;
160
180
161
181
if sync_result. is_err ( ) {
162
182
panic ! ( "Unexpected sync result: {:?}" , sync_result)
@@ -187,9 +207,10 @@ mod tests {
187
207
188
208
assert_eq ! ( network_graph. read_only( ) . channels( ) . len( ) , 0 ) ;
189
209
210
+ let rapid_sync = RapidGossipSync :: new ( Arc :: new ( network_graph) ) ;
190
211
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" ) ;
193
214
if let Err ( crate :: error:: GraphSyncError :: DecodeError ( DecodeError :: Io ( io_error) ) ) = & sync_result {
194
215
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) ;
195
216
#[ cfg( not( require_route_graph_test) ) ]
@@ -218,17 +239,15 @@ pub mod bench {
218
239
use lightning:: ln:: msgs:: DecodeError ;
219
240
use lightning:: routing:: network_graph:: NetworkGraph ;
220
241
221
- use crate :: sync_network_graph_with_file_path;
242
+ use crate :: { RapidGossipSync , sync_network_graph_with_file_path} ;
222
243
223
244
#[ bench]
224
245
fn bench_reading_full_graph_from_file ( b : & mut Bencher ) {
225
246
let block_hash = genesis_block ( Network :: Bitcoin ) . block_hash ( ) ;
226
247
b. iter ( || {
227
248
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" ) ;
232
251
if let Err ( crate :: error:: GraphSyncError :: DecodeError ( DecodeError :: Io ( io_error) ) ) = & sync_result {
233
252
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) ;
234
253
#[ cfg( not( require_route_graph_test) ) ]
0 commit comments