@@ -29,7 +29,7 @@ use crate::ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds
29
29
use crate :: ln:: msgs;
30
30
use crate :: util:: ser:: { Readable , ReadableArgs , Writeable , Writer , MaybeReadable } ;
31
31
use crate :: util:: logger:: { Logger , Level } ;
32
- use crate :: util:: events:: { Event , EventHandler , MessageSendEvent , MessageSendEventsProvider } ;
32
+ use crate :: util:: events:: { MessageSendEvent , MessageSendEventsProvider } ;
33
33
use crate :: util:: scid_utils:: { block_from_scid, scid_from_parts, MAX_SCID_BLOCK } ;
34
34
35
35
use crate :: io;
@@ -213,8 +213,8 @@ impl_writeable_tlv_based_enum_upgradable!(NetworkUpdate,
213
213
/// Provides interface to help with initial routing sync by
214
214
/// serving historical announcements.
215
215
///
216
- /// Serves as an [`EventHandler`] for applying updates from [`Event::PaymentPathFailed`] to the
217
- /// [`NetworkGraph`].
216
+ /// Implements a referenced version of [`EventHandler`] for applying updates from
217
+ /// [`Event::PaymentPathFailed`] to the [` NetworkGraph`].
218
218
pub struct P2PGossipSync < G : Deref < Target =NetworkGraph < L > > , C : Deref , L : Deref >
219
219
where C :: Target : chain:: Access , L :: Target : Logger
220
220
{
@@ -274,32 +274,29 @@ where C::Target: chain::Access, L::Target: Logger
274
274
}
275
275
}
276
276
277
- impl < L : Deref > EventHandler for NetworkGraph < L > where L :: Target : Logger {
278
- fn handle_event ( & self , event : Event ) {
279
- if let Event :: PaymentPathFailed { network_update, .. } = event {
280
- if let Some ( network_update) = network_update {
281
- match network_update {
282
- NetworkUpdate :: ChannelUpdateMessage { ref msg } => {
283
- let short_channel_id = msg. contents . short_channel_id ;
284
- let is_enabled = msg. contents . flags & ( 1 << 1 ) != ( 1 << 1 ) ;
285
- let status = if is_enabled { "enabled" } else { "disabled" } ;
286
- log_debug ! ( self . logger, "Updating channel with channel_update from a payment failure. Channel {} is {}." , short_channel_id, status) ;
287
- let _ = self . update_channel ( msg) ;
288
- } ,
289
- NetworkUpdate :: ChannelFailure { short_channel_id, is_permanent } => {
290
- let action = if is_permanent { "Removing" } else { "Disabling" } ;
291
- log_debug ! ( self . logger, "{} channel graph entry for {} due to a payment failure." , action, short_channel_id) ;
292
- self . channel_failed ( short_channel_id, is_permanent) ;
293
- } ,
294
- NetworkUpdate :: NodeFailure { ref node_id, is_permanent } => {
295
- if is_permanent {
296
- log_debug ! ( self . logger,
297
- "Removed node graph entry for {} due to a payment failure." , log_pubkey!( node_id) ) ;
298
- self . node_failed_permanent ( node_id) ;
299
- } ;
300
- } ,
301
- }
302
- }
277
+ impl < L : Deref > NetworkGraph < L > where L :: Target : Logger {
278
+ /// Handles any network updates originating from [`Event`]s.
279
+ pub fn handle_network_update ( & self , network_update : & NetworkUpdate ) {
280
+ match network_update {
281
+ NetworkUpdate :: ChannelUpdateMessage { ref msg } => {
282
+ let short_channel_id = msg. contents . short_channel_id ;
283
+ let is_enabled = msg. contents . flags & ( 1 << 1 ) != ( 1 << 1 ) ;
284
+ let status = if is_enabled { "enabled" } else { "disabled" } ;
285
+ log_debug ! ( self . logger, "Updating channel with channel_update from a payment failure. Channel {} is {}." , short_channel_id, status) ;
286
+ let _ = self . update_channel ( msg) ;
287
+ } ,
288
+ NetworkUpdate :: ChannelFailure { short_channel_id, is_permanent } => {
289
+ let action = if * is_permanent { "Removing" } else { "Disabling" } ;
290
+ log_debug ! ( self . logger, "{} channel graph entry for {} due to a payment failure." , action, short_channel_id) ;
291
+ self . channel_failed ( * short_channel_id, * is_permanent) ;
292
+ } ,
293
+ NetworkUpdate :: NodeFailure { ref node_id, is_permanent } => {
294
+ if * is_permanent {
295
+ log_debug ! ( self . logger,
296
+ "Removed node graph entry for {} due to a payment failure." , log_pubkey!( node_id) ) ;
297
+ self . node_failed_permanent ( node_id) ;
298
+ } ;
299
+ } ,
303
300
}
304
301
}
305
302
}
@@ -1972,15 +1969,14 @@ mod tests {
1972
1969
use crate :: chain;
1973
1970
use crate :: ln:: channelmanager;
1974
1971
use crate :: ln:: chan_utils:: make_funding_redeemscript;
1975
- use crate :: ln:: PaymentHash ;
1976
1972
use crate :: ln:: features:: InitFeatures ;
1977
1973
use crate :: routing:: gossip:: { P2PGossipSync , NetworkGraph , NetworkUpdate , NodeAlias , MAX_EXCESS_BYTES_FOR_RELAY , NodeId , RoutingFees , ChannelUpdateInfo , ChannelInfo , NodeAnnouncementInfo , NodeInfo } ;
1978
1974
use crate :: ln:: msgs:: { RoutingMessageHandler , UnsignedNodeAnnouncement , NodeAnnouncement ,
1979
1975
UnsignedChannelAnnouncement , ChannelAnnouncement , UnsignedChannelUpdate , ChannelUpdate ,
1980
1976
ReplyChannelRange , QueryChannelRange , QueryShortChannelIds , MAX_VALUE_MSAT } ;
1981
1977
use crate :: util:: test_utils;
1982
1978
use crate :: util:: ser:: { ReadableArgs , Writeable } ;
1983
- use crate :: util:: events:: { Event , EventHandler , MessageSendEvent , MessageSendEventsProvider } ;
1979
+ use crate :: util:: events:: { MessageSendEvent , MessageSendEventsProvider } ;
1984
1980
use crate :: util:: scid_utils:: scid_from_parts;
1985
1981
1986
1982
use crate :: routing:: gossip:: REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS ;
@@ -2424,19 +2420,8 @@ mod tests {
2424
2420
let valid_channel_update = get_signed_channel_update ( |_| { } , node_1_privkey, & secp_ctx) ;
2425
2421
assert ! ( network_graph. read_only( ) . channels( ) . get( & short_channel_id) . unwrap( ) . one_to_two. is_none( ) ) ;
2426
2422
2427
- network_graph. handle_event ( Event :: PaymentPathFailed {
2428
- payment_id : None ,
2429
- payment_hash : PaymentHash ( [ 0 ; 32 ] ) ,
2430
- payment_failed_permanently : false ,
2431
- all_paths_failed : true ,
2432
- path : vec ! [ ] ,
2433
- network_update : Some ( NetworkUpdate :: ChannelUpdateMessage {
2434
- msg : valid_channel_update,
2435
- } ) ,
2436
- short_channel_id : None ,
2437
- retry : None ,
2438
- error_code : None ,
2439
- error_data : None ,
2423
+ network_graph. handle_network_update ( & NetworkUpdate :: ChannelUpdateMessage {
2424
+ msg : valid_channel_update,
2440
2425
} ) ;
2441
2426
2442
2427
assert ! ( network_graph. read_only( ) . channels( ) . get( & short_channel_id) . unwrap( ) . one_to_two. is_some( ) ) ;
@@ -2451,20 +2436,9 @@ mod tests {
2451
2436
}
2452
2437
} ;
2453
2438
2454
- network_graph. handle_event ( Event :: PaymentPathFailed {
2455
- payment_id : None ,
2456
- payment_hash : PaymentHash ( [ 0 ; 32 ] ) ,
2457
- payment_failed_permanently : false ,
2458
- all_paths_failed : true ,
2459
- path : vec ! [ ] ,
2460
- network_update : Some ( NetworkUpdate :: ChannelFailure {
2461
- short_channel_id,
2462
- is_permanent : false ,
2463
- } ) ,
2464
- short_channel_id : None ,
2465
- retry : None ,
2466
- error_code : None ,
2467
- error_data : None ,
2439
+ network_graph. handle_network_update ( & NetworkUpdate :: ChannelFailure {
2440
+ short_channel_id,
2441
+ is_permanent : false ,
2468
2442
} ) ;
2469
2443
2470
2444
match network_graph. read_only ( ) . channels ( ) . get ( & short_channel_id) {
@@ -2476,20 +2450,9 @@ mod tests {
2476
2450
}
2477
2451
2478
2452
// Permanent closing deletes a channel
2479
- network_graph. handle_event ( Event :: PaymentPathFailed {
2480
- payment_id : None ,
2481
- payment_hash : PaymentHash ( [ 0 ; 32 ] ) ,
2482
- payment_failed_permanently : false ,
2483
- all_paths_failed : true ,
2484
- path : vec ! [ ] ,
2485
- network_update : Some ( NetworkUpdate :: ChannelFailure {
2486
- short_channel_id,
2487
- is_permanent : true ,
2488
- } ) ,
2489
- short_channel_id : None ,
2490
- retry : None ,
2491
- error_code : None ,
2492
- error_data : None ,
2453
+ network_graph. handle_network_update ( & NetworkUpdate :: ChannelFailure {
2454
+ short_channel_id,
2455
+ is_permanent : true ,
2493
2456
} ) ;
2494
2457
2495
2458
assert_eq ! ( network_graph. read_only( ) . channels( ) . len( ) , 0 ) ;
@@ -2508,40 +2471,18 @@ mod tests {
2508
2471
assert ! ( network_graph. read_only( ) . channels( ) . get( & short_channel_id) . is_some( ) ) ;
2509
2472
2510
2473
// Non-permanent node failure does not delete any nodes or channels
2511
- network_graph. handle_event ( Event :: PaymentPathFailed {
2512
- payment_id : None ,
2513
- payment_hash : PaymentHash ( [ 0 ; 32 ] ) ,
2514
- payment_failed_permanently : false ,
2515
- all_paths_failed : true ,
2516
- path : vec ! [ ] ,
2517
- network_update : Some ( NetworkUpdate :: NodeFailure {
2518
- node_id : node_2_id,
2519
- is_permanent : false ,
2520
- } ) ,
2521
- short_channel_id : None ,
2522
- retry : None ,
2523
- error_code : None ,
2524
- error_data : None ,
2474
+ network_graph. handle_network_update ( & NetworkUpdate :: NodeFailure {
2475
+ node_id : node_2_id,
2476
+ is_permanent : false ,
2525
2477
} ) ;
2526
2478
2527
2479
assert ! ( network_graph. read_only( ) . channels( ) . get( & short_channel_id) . is_some( ) ) ;
2528
2480
assert ! ( network_graph. read_only( ) . nodes( ) . get( & NodeId :: from_pubkey( & node_2_id) ) . is_some( ) ) ;
2529
2481
2530
2482
// Permanent node failure deletes node and its channels
2531
- network_graph. handle_event ( Event :: PaymentPathFailed {
2532
- payment_id : None ,
2533
- payment_hash : PaymentHash ( [ 0 ; 32 ] ) ,
2534
- payment_failed_permanently : false ,
2535
- all_paths_failed : true ,
2536
- path : vec ! [ ] ,
2537
- network_update : Some ( NetworkUpdate :: NodeFailure {
2538
- node_id : node_2_id,
2539
- is_permanent : true ,
2540
- } ) ,
2541
- short_channel_id : None ,
2542
- retry : None ,
2543
- error_code : None ,
2544
- error_data : None ,
2483
+ network_graph. handle_network_update ( & NetworkUpdate :: NodeFailure {
2484
+ node_id : node_2_id,
2485
+ is_permanent : true ,
2545
2486
} ) ;
2546
2487
2547
2488
assert_eq ! ( network_graph. read_only( ) . nodes( ) . len( ) , 0 ) ;
0 commit comments