@@ -23,6 +23,8 @@ use lightning::ln::msgs::{ChannelMessageHandler, OnionMessageHandler, RoutingMes
23
23
use lightning:: ln:: peer_handler:: { CustomMessageHandler , PeerManager , SocketDescriptor } ;
24
24
use lightning:: routing:: gossip:: { NetworkGraph , P2PGossipSync } ;
25
25
use lightning:: routing:: scoring:: WriteableScore ;
26
+ #[ cfg( feature = "futures" ) ]
27
+ use lightning:: util:: events:: AsyncEventHandler ;
26
28
use lightning:: util:: events:: { Event , EventHandler , EventsProvider } ;
27
29
use lightning:: util:: logger:: Logger ;
28
30
use lightning:: util:: persist:: Persister ;
@@ -34,6 +36,11 @@ use std::thread::JoinHandle;
34
36
use std:: time:: { Duration , Instant } ;
35
37
use std:: ops:: Deref ;
36
38
39
+ #[ cfg( feature = "futures" ) ]
40
+ use core:: future:: Future as StdFuture ;
41
+ #[ cfg( feature = "futures" ) ]
42
+ use core:: pin:: Pin ;
43
+
37
44
#[ cfg( feature = "futures" ) ]
38
45
use futures_util:: { select_biased, future:: FutureExt } ;
39
46
@@ -225,16 +232,49 @@ where A::Target: chain::Access, L::Target: Logger {
225
232
}
226
233
}
227
234
235
+ #[ cfg( feature = "futures" ) ]
236
+ /// Decorates an [`EventHandler`] with common functionality provided by standard [`EventHandler`]s.
237
+ struct DecoratingAsyncEventHandler <
238
+ ' a ,
239
+ E : AsyncEventHandler ,
240
+ PGS : Deref < Target = P2PGossipSync < G , A , L > > ,
241
+ RGS : Deref < Target = RapidGossipSync < G , L > > ,
242
+ G : Deref < Target = NetworkGraph < L > > ,
243
+ A : Deref ,
244
+ L : Deref ,
245
+ >
246
+ where A :: Target : chain:: Access , L :: Target : Logger {
247
+ async_event_handler : E ,
248
+ gossip_sync : & ' a GossipSync < PGS , RGS , G , A , L > ,
249
+ }
250
+
251
+ #[ cfg( feature = "futures" ) ]
252
+ impl <
253
+ ' a ,
254
+ E : AsyncEventHandler ,
255
+ PGS : Deref < Target = P2PGossipSync < G , A , L > > ,
256
+ RGS : Deref < Target = RapidGossipSync < G , L > > ,
257
+ G : Deref < Target = NetworkGraph < L > > ,
258
+ A : Deref ,
259
+ L : Deref ,
260
+ > AsyncEventHandler for DecoratingAsyncEventHandler < ' a , E , PGS , RGS , G , A , L >
261
+ where A :: Target : chain:: Access , L :: Target : Logger {
262
+ fn handle_event_async < ' b > ( & ' b self , event : & ' b Event ) -> Pin < Box < dyn StdFuture < Output = ( ) > + ' _ > > {
263
+ Box :: pin ( async move {
264
+ if let Some ( network_graph) = self . gossip_sync . network_graph ( ) {
265
+ network_graph. handle_event ( event) ; // TODO: Also make async?
266
+ }
267
+ self . async_event_handler . handle_event_async ( event) . await ;
268
+ } )
269
+ }
270
+ }
271
+
228
272
macro_rules! define_run_body {
229
- ( $persister: ident, $event_handler: ident, $chain_monitor: ident, $channel_manager: ident,
273
+ ( $persister: ident, $chain_monitor: ident, $process_chain_monitor_events: expr,
274
+ $channel_manager: ident, $process_channel_manager_events: expr,
230
275
$gossip_sync: ident, $peer_manager: ident, $logger: ident, $scorer: ident,
231
276
$loop_exit_check: expr, $await: expr)
232
277
=> { {
233
- let event_handler = DecoratingEventHandler {
234
- event_handler: $event_handler,
235
- gossip_sync: & $gossip_sync,
236
- } ;
237
-
238
278
log_trace!( $logger, "Calling ChannelManager's timer_tick_occurred on startup" ) ;
239
279
$channel_manager. timer_tick_occurred( ) ;
240
280
@@ -245,8 +285,8 @@ macro_rules! define_run_body {
245
285
let mut have_pruned = false ;
246
286
247
287
loop {
248
- $channel_manager . process_pending_events ( & event_handler ) ;
249
- $chain_monitor . process_pending_events ( & event_handler ) ;
288
+ $process_channel_manager_events ;
289
+ $process_chain_monitor_events ;
250
290
251
291
// Note that the PeerManager::process_events may block on ChannelManager's locks,
252
292
// hence it comes last here. When the ChannelManager finishes whatever it's doing,
@@ -379,7 +419,7 @@ pub async fn process_events_async<
379
419
CMH : ' static + Deref + Send + Sync ,
380
420
RMH : ' static + Deref + Send + Sync ,
381
421
OMH : ' static + Deref + Send + Sync ,
382
- EH : ' static + EventHandler + Send ,
422
+ EH : ' static + AsyncEventHandler + Send ,
383
423
PS : ' static + Deref + Send ,
384
424
M : ' static + Deref < Target = ChainMonitor < Signer , CF , T , F , L , P > > + Send + Sync ,
385
425
CM : ' static + Deref < Target = ChannelManager < Signer , CW , T , K , F , L > > + Send + Sync ,
@@ -392,7 +432,7 @@ pub async fn process_events_async<
392
432
SleepFuture : core:: future:: Future < Output = bool > ,
393
433
Sleeper : Fn ( Duration ) -> SleepFuture
394
434
> (
395
- persister : PS , event_handler : EH , chain_monitor : M , channel_manager : CM ,
435
+ persister : PS , async_event_handler : EH , chain_monitor : M , channel_manager : CM ,
396
436
gossip_sync : GossipSync < PGS , RGS , G , CA , L > , peer_manager : PM , logger : L , scorer : Option < S > ,
397
437
sleeper : Sleeper ,
398
438
) -> Result < ( ) , std:: io:: Error >
@@ -412,7 +452,12 @@ where
412
452
PS :: Target : ' static + Persister < ' a , Signer , CW , T , K , F , L , SC > ,
413
453
{
414
454
let mut should_continue = true ;
415
- define_run_body ! ( persister, event_handler, chain_monitor, channel_manager,
455
+ let async_event_handler = DecoratingAsyncEventHandler {
456
+ async_event_handler,
457
+ gossip_sync : & gossip_sync,
458
+ } ;
459
+ define_run_body ! ( persister, chain_monitor, chain_monitor. process_pending_events_async( & async_event_handler) . await ,
460
+ channel_manager, channel_manager. process_pending_events_async( & async_event_handler) . await ,
416
461
gossip_sync, peer_manager, logger, scorer, should_continue, {
417
462
select_biased! {
418
463
_ = channel_manager. get_persistable_update_future( ) . fuse( ) => true ,
@@ -517,7 +562,12 @@ impl BackgroundProcessor {
517
562
let stop_thread = Arc :: new ( AtomicBool :: new ( false ) ) ;
518
563
let stop_thread_clone = stop_thread. clone ( ) ;
519
564
let handle = thread:: spawn ( move || -> Result < ( ) , std:: io:: Error > {
520
- define_run_body ! ( persister, event_handler, chain_monitor, channel_manager,
565
+ let event_handler = DecoratingEventHandler {
566
+ event_handler,
567
+ gossip_sync : & gossip_sync,
568
+ } ;
569
+ define_run_body ! ( persister, chain_monitor, chain_monitor. process_pending_events( & event_handler) ,
570
+ channel_manager, channel_manager. process_pending_events( & event_handler) ,
521
571
gossip_sync, peer_manager, logger, scorer, stop_thread. load( Ordering :: Acquire ) ,
522
572
channel_manager. await_persistable_update_timeout( Duration :: from_millis( 100 ) ) )
523
573
} ) ;
0 commit comments