5
5
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
6
6
// accordance with one or both of these licenses.
7
7
8
+ use crate :: chain:: ChainSource ;
8
9
use crate :: config:: RGS_SYNC_TIMEOUT_SECS ;
9
- use crate :: logger:: { log_trace, FilesystemLogger , Logger } ;
10
- use crate :: types:: { GossipSync , Graph , P2PGossipSync , RapidGossipSync } ;
10
+ use crate :: logger:: { log_error , log_trace, FilesystemLogger , Logger } ;
11
+ use crate :: types:: { GossipSync , Graph , P2PGossipSync , PeerManager , RapidGossipSync , UtxoLookup } ;
11
12
use crate :: Error ;
12
13
13
- use lightning :: routing :: utxo :: UtxoLookup ;
14
+ use lightning_block_sync :: gossip :: { FutureSpawner , GossipVerifier } ;
14
15
16
+ use std:: future:: Future ;
15
17
use std:: sync:: atomic:: { AtomicU32 , Ordering } ;
16
- use std:: sync:: Arc ;
18
+ use std:: sync:: { Arc , RwLock } ;
17
19
use std:: time:: Duration ;
18
20
19
21
pub ( crate ) enum GossipSource {
20
22
P2PNetwork {
21
23
gossip_sync : Arc < P2PGossipSync > ,
24
+ logger : Arc < FilesystemLogger > ,
22
25
} ,
23
26
RapidGossipSync {
24
27
gossip_sync : Arc < RapidGossipSync > ,
@@ -32,10 +35,10 @@ impl GossipSource {
32
35
pub fn new_p2p ( network_graph : Arc < Graph > , logger : Arc < FilesystemLogger > ) -> Self {
33
36
let gossip_sync = Arc :: new ( P2PGossipSync :: new (
34
37
network_graph,
35
- None :: < Arc < dyn UtxoLookup + Send + Sync > > ,
36
- logger,
38
+ None :: < Arc < UtxoLookup > > ,
39
+ Arc :: clone ( & logger) ,
37
40
) ) ;
38
- Self :: P2PNetwork { gossip_sync }
41
+ Self :: P2PNetwork { gossip_sync, logger }
39
42
}
40
43
41
44
pub fn new_rgs (
@@ -58,9 +61,30 @@ impl GossipSource {
58
61
}
59
62
}
60
63
64
+ pub ( crate ) fn set_gossip_verifier (
65
+ & self , chain_source : Arc < ChainSource > , peer_manager : Arc < PeerManager > ,
66
+ runtime : Arc < RwLock < Option < Arc < tokio:: runtime:: Runtime > > > > ,
67
+ ) {
68
+ match self {
69
+ Self :: P2PNetwork { gossip_sync, logger } => {
70
+ if let Some ( utxo_source) = chain_source. as_utxo_source ( ) {
71
+ let spawner = RuntimeSpawner :: new ( Arc :: clone ( & runtime) , Arc :: clone ( & logger) ) ;
72
+ let gossip_verifier = Arc :: new ( GossipVerifier :: new (
73
+ utxo_source,
74
+ spawner,
75
+ Arc :: clone ( gossip_sync) ,
76
+ peer_manager,
77
+ ) ) ;
78
+ gossip_sync. add_utxo_lookup ( Some ( gossip_verifier) ) ;
79
+ }
80
+ } ,
81
+ _ => ( ) ,
82
+ }
83
+ }
84
+
61
85
pub async fn update_rgs_snapshot ( & self ) -> Result < u32 , Error > {
62
86
match self {
63
- Self :: P2PNetwork { gossip_sync : _ } => Ok ( 0 ) ,
87
+ Self :: P2PNetwork { gossip_sync : _, .. } => Ok ( 0 ) ,
64
88
Self :: RapidGossipSync { gossip_sync, server_url, latest_sync_timestamp, logger } => {
65
89
let query_timestamp = latest_sync_timestamp. load ( Ordering :: Acquire ) ;
66
90
let query_url = format ! ( "{}/{}" , server_url, query_timestamp) ;
@@ -101,3 +125,30 @@ impl GossipSource {
101
125
}
102
126
}
103
127
}
128
+
129
+ pub ( crate ) struct RuntimeSpawner {
130
+ runtime : Arc < RwLock < Option < Arc < tokio:: runtime:: Runtime > > > > ,
131
+ logger : Arc < FilesystemLogger > ,
132
+ }
133
+
134
+ impl RuntimeSpawner {
135
+ pub ( crate ) fn new (
136
+ runtime : Arc < RwLock < Option < Arc < tokio:: runtime:: Runtime > > > > , logger : Arc < FilesystemLogger > ,
137
+ ) -> Self {
138
+ Self { runtime, logger }
139
+ }
140
+ }
141
+
142
+ impl FutureSpawner for RuntimeSpawner {
143
+ fn spawn < T : Future < Output = ( ) > + Send + ' static > ( & self , future : T ) {
144
+ let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
145
+ if rt_lock. is_none ( ) {
146
+ log_error ! ( self . logger, "Tried spawing a future while the runtime wasn't available. This should never happen." ) ;
147
+ debug_assert ! ( false , "Tried spawing a future while the runtime wasn't available. This should never happen." ) ;
148
+ return ;
149
+ }
150
+
151
+ let runtime = rt_lock. as_ref ( ) . unwrap ( ) ;
152
+ runtime. spawn ( future) ;
153
+ }
154
+ }
0 commit comments