@@ -5909,7 +5909,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
5909
5909
let mut inflight_htlcs = InFlightHtlcs :: new ( ) ;
5910
5910
5911
5911
for chan in self . channel_state . lock ( ) . unwrap ( ) . by_id . values ( ) {
5912
- for htlc_source in chan. inflight_htlc_sources ( ) {
5912
+ for ( htlc_source, _ ) in chan. inflight_htlc_sources ( ) {
5913
5913
if let HTLCSource :: OutboundRoute { path, .. } = htlc_source {
5914
5914
inflight_htlcs. process_path ( path, self . get_our_node_id ( ) ) ;
5915
5915
}
@@ -5927,6 +5927,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
5927
5927
events. into_inner ( )
5928
5928
}
5929
5929
5930
+ #[ cfg( test) ]
5931
+ pub fn pop_pending_event ( & self ) -> Option < events:: Event > {
5932
+ let mut events = self . pending_events . lock ( ) . unwrap ( ) ;
5933
+ if events. is_empty ( ) { None } else { Some ( events. remove ( 0 ) ) }
5934
+ }
5935
+
5930
5936
#[ cfg( test) ]
5931
5937
pub fn has_pending_payments ( & self ) -> bool {
5932
5938
!self . pending_outbound_payments . lock ( ) . unwrap ( ) . is_empty ( )
@@ -7420,6 +7426,25 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
7420
7426
user_channel_id : channel. get_user_id ( ) ,
7421
7427
reason : ClosureReason :: OutdatedChannelManager
7422
7428
} ) ;
7429
+ for ( channel_htlc_source, payment_hash) in channel. inflight_htlc_sources ( ) {
7430
+ let mut found_htlc = false ;
7431
+ for ( monitor_htlc_source, _) in monitor. get_all_current_outbound_htlcs ( ) {
7432
+ if * channel_htlc_source == monitor_htlc_source { found_htlc = true ; break ; }
7433
+ }
7434
+ if !found_htlc {
7435
+ // If we have some HTLCs in the channel which are not present in the newer
7436
+ // ChannelMonitor, they have been removed and should be failed back to
7437
+ // ensure we don't forget them entirely. Note that if the missing HTLC(s)
7438
+ // were actually claimed we'd have generated and ensured the previous-hop
7439
+ // claim update ChannelMonitor updates were persisted prior to persising
7440
+ // the ChannelMonitor update for the forward leg, so attempting to fail the
7441
+ // backwards leg of the HTLC will simply be rejected.
7442
+ log_info ! ( args. logger,
7443
+ "Failing HTLC with hash {} as it is missing in the ChannelMonitor for channel {} but was present in the (stale) ChannelManager" ,
7444
+ log_bytes!( channel. channel_id( ) ) , log_bytes!( payment_hash. 0 ) ) ;
7445
+ failed_htlcs. push ( ( channel_htlc_source. clone ( ) , * payment_hash, channel. get_counterparty_node_id ( ) , channel. channel_id ( ) ) ) ;
7446
+ }
7447
+ }
7423
7448
} else {
7424
7449
log_info ! ( args. logger, "Successfully loaded channel {}" , log_bytes!( channel. channel_id( ) ) ) ;
7425
7450
if let Some ( short_channel_id) = channel. get_short_channel_id ( ) {
@@ -7500,16 +7525,6 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
7500
7525
None => continue ,
7501
7526
}
7502
7527
}
7503
- if forward_htlcs_count > 0 {
7504
- // If we have pending HTLCs to forward, assume we either dropped a
7505
- // `PendingHTLCsForwardable` or the user received it but never processed it as they
7506
- // shut down before the timer hit. Either way, set the time_forwardable to a small
7507
- // constant as enough time has likely passed that we should simply handle the forwards
7508
- // now, or at least after the user gets a chance to reconnect to our peers.
7509
- pending_events_read. push ( events:: Event :: PendingHTLCsForwardable {
7510
- time_forwardable : Duration :: from_secs ( 2 ) ,
7511
- } ) ;
7512
- }
7513
7528
7514
7529
let background_event_count: u64 = Readable :: read ( reader) ?;
7515
7530
let mut pending_background_events_read: Vec < BackgroundEvent > = Vec :: with_capacity ( cmp:: min ( background_event_count as usize , MAX_ALLOC_SIZE /mem:: size_of :: < BackgroundEvent > ( ) ) ) ;
@@ -7620,10 +7635,44 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
7620
7635
}
7621
7636
}
7622
7637
}
7638
+ for ( htlc_source, htlc) in monitor. get_all_current_outbound_htlcs ( ) {
7639
+ if let HTLCSource :: PreviousHopData ( prev_hop_data) = htlc_source {
7640
+ // The ChannelMonitor is now responsible for this HTLC's
7641
+ // failure/success and will let us know what its outcome is. If we
7642
+ // still have an entry for this HTLC in `forward_htlcs`, we were
7643
+ // apparently not persisted after the monitor was when forwarding
7644
+ // the payment.
7645
+ forward_htlcs. retain ( |_, forwards| {
7646
+ forwards. retain ( |forward| {
7647
+ if let HTLCForwardInfo :: AddHTLC ( htlc_info) = forward {
7648
+ if htlc_info. prev_short_channel_id == prev_hop_data. short_channel_id &&
7649
+ htlc_info. prev_htlc_id == prev_hop_data. htlc_id
7650
+ {
7651
+ log_info ! ( args. logger, "Removing pending to-forward HTLC with hash {} as it was forwarded to the closed channel {}" ,
7652
+ log_bytes!( htlc. payment_hash. 0 ) , log_bytes!( monitor. get_funding_txo( ) . 0 . to_channel_id( ) ) ) ;
7653
+ false
7654
+ } else { true }
7655
+ } else { true }
7656
+ } ) ;
7657
+ !forwards. is_empty ( )
7658
+ } )
7659
+ }
7660
+ }
7623
7661
}
7624
7662
}
7625
7663
}
7626
7664
7665
+ if !forward_htlcs. is_empty ( ) {
7666
+ // If we have pending HTLCs to forward, assume we either dropped a
7667
+ // `PendingHTLCsForwardable` or the user received it but never processed it as they
7668
+ // shut down before the timer hit. Either way, set the time_forwardable to a small
7669
+ // constant as enough time has likely passed that we should simply handle the forwards
7670
+ // now, or at least after the user gets a chance to reconnect to our peers.
7671
+ pending_events_read. push ( events:: Event :: PendingHTLCsForwardable {
7672
+ time_forwardable : Duration :: from_secs ( 2 ) ,
7673
+ } ) ;
7674
+ }
7675
+
7627
7676
let inbound_pmt_key_material = args. keys_manager . get_inbound_payment_key_material ( ) ;
7628
7677
let expanded_inbound_key = inbound_payment:: ExpandedKey :: new ( & inbound_pmt_key_material) ;
7629
7678
0 commit comments