55// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66// accordance with one or both of these licenses.
77
8+ use crate :: chain:: ChainSource ;
89use 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 } ;
1112use crate :: Error ;
1213
13- use lightning :: routing :: utxo :: UtxoLookup ;
14+ use lightning_block_sync :: gossip :: { FutureSpawner , GossipVerifier } ;
1415
16+ use std:: future:: Future ;
1517use std:: sync:: atomic:: { AtomicU32 , Ordering } ;
16- use std:: sync:: Arc ;
18+ use std:: sync:: { Arc , RwLock } ;
1719use std:: time:: Duration ;
1820
1921pub ( crate ) enum GossipSource {
2022 P2PNetwork {
2123 gossip_sync : Arc < P2PGossipSync > ,
24+ logger : Arc < FilesystemLogger > ,
2225 } ,
2326 RapidGossipSync {
2427 gossip_sync : Arc < RapidGossipSync > ,
@@ -32,10 +35,10 @@ impl GossipSource {
3235 pub fn new_p2p ( network_graph : Arc < Graph > , logger : Arc < FilesystemLogger > ) -> Self {
3336 let gossip_sync = Arc :: new ( P2PGossipSync :: new (
3437 network_graph,
35- None :: < Arc < dyn UtxoLookup + Send + Sync > > ,
36- logger,
38+ None :: < Arc < UtxoLookup > > ,
39+ Arc :: clone ( & logger) ,
3740 ) ) ;
38- Self :: P2PNetwork { gossip_sync }
41+ Self :: P2PNetwork { gossip_sync, logger }
3942 }
4043
4144 pub fn new_rgs (
@@ -58,9 +61,30 @@ impl GossipSource {
5861 }
5962 }
6063
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+
6185 pub async fn update_rgs_snapshot ( & self ) -> Result < u32 , Error > {
6286 match self {
63- Self :: P2PNetwork { gossip_sync : _ } => Ok ( 0 ) ,
87+ Self :: P2PNetwork { gossip_sync : _, .. } => Ok ( 0 ) ,
6488 Self :: RapidGossipSync { gossip_sync, server_url, latest_sync_timestamp, logger } => {
6589 let query_timestamp = latest_sync_timestamp. load ( Ordering :: Acquire ) ;
6690 let query_url = format ! ( "{}/{}" , server_url, query_timestamp) ;
@@ -101,3 +125,30 @@ impl GossipSource {
101125 }
102126 }
103127}
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