@@ -5909,7 +5909,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
59095909 let mut inflight_htlcs = InFlightHtlcs :: new ( ) ;
59105910
59115911 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 ( ) {
59135913 if let HTLCSource :: OutboundRoute { path, .. } = htlc_source {
59145914 inflight_htlcs. process_path ( path, self . get_our_node_id ( ) ) ;
59155915 }
@@ -5927,6 +5927,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
59275927 events. into_inner ( )
59285928 }
59295929
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+
59305936 #[ cfg( test) ]
59315937 pub fn has_pending_payments ( & self ) -> bool {
59325938 !self . pending_outbound_payments . lock ( ) . unwrap ( ) . is_empty ( )
@@ -7420,6 +7426,25 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
74207426 user_channel_id : channel. get_user_id ( ) ,
74217427 reason : ClosureReason :: OutdatedChannelManager
74227428 } ) ;
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+ }
74237448 } else {
74247449 log_info ! ( args. logger, "Successfully loaded channel {}" , log_bytes!( channel. channel_id( ) ) ) ;
74257450 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>
75007525 None => continue ,
75017526 }
75027527 }
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- }
75137528
75147529 let background_event_count: u64 = Readable :: read ( reader) ?;
75157530 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>
76207635 }
76217636 }
76227637 }
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+ }
76237661 }
76247662 }
76257663 }
76267664
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+
76277676 let inbound_pmt_key_material = args. keys_manager . get_inbound_payment_key_material ( ) ;
76287677 let expanded_inbound_key = inbound_payment:: ExpandedKey :: new ( & inbound_pmt_key_material) ;
76297678
0 commit comments