@@ -4281,10 +4281,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42814281 } ;
42824282 debug_assert ! ( !sources. is_empty( ) ) ;
42834283
4284- // If we are claiming an MPP payment, we have to take special care to ensure that each
4285- // channel exists before claiming all of the payments (inside one lock).
4286- // Note that channel existance is sufficient as we should always get a monitor update
4287- // which will take care of the real HTLC claim enforcement.
4284+ // If we are claiming an MPP payment, we check that all channels which contain a claimable
4285+ // HTLC still exist. While this isn't guaranteed to remain true if a channel closes while
4286+ // we're claiming (or even after we claim, before the commitment update dance completes),
4287+ // it should be a relatively rare race, and we'd rather not claim HTLCs that require us to
4288+ // go on-chain (and lose the on-chain fee to do so) than just reject the payment.
4289+ //
4290+ // Note that we'll still always get our funds - as long as the generated
4291+ // `ChannelMonitorUpdate` makes it out to the relevant monitor we can claim on-chain.
42884292 //
42894293 // If we find an HTLC which we would need to claim but for which we do not have a
42904294 // channel, we will fail all parts of the MPP payment. While we could wait and see if
@@ -4297,8 +4301,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42974301 let mut valid_mpp = true ;
42984302 let mut errs = Vec :: new ( ) ;
42994303 let mut claimed_any_htlcs = false ;
4300- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4301- let channel_state = & mut * channel_state_lock;
4304+ let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
43024305 for htlc in sources. iter ( ) {
43034306 let chan_id = match self . short_to_chan_info . read ( ) . unwrap ( ) . get ( & htlc. prev_hop . short_channel_id ) {
43044307 Some ( ( _cp_id, chan_id) ) => chan_id. clone ( ) ,
@@ -4308,7 +4311,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43084311 }
43094312 } ;
43104313
4311- if let None = channel_state. by_id . get ( & chan_id) {
4314+ if let None = channel_state. as_ref ( ) . unwrap ( ) . by_id . get ( & chan_id) {
43124315 valid_mpp = false ;
43134316 break ;
43144317 }
@@ -4348,7 +4351,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43484351 }
43494352 if valid_mpp {
43504353 for htlc in sources. drain ( ..) {
4351- match self . claim_funds_from_hop ( & mut channel_state_lock, htlc. prev_hop , payment_preimage) {
4354+ if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
4355+ match self . claim_funds_from_hop ( channel_state. take ( ) . unwrap ( ) , htlc. prev_hop , payment_preimage) {
43524356 ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) => {
43534357 if let msgs:: ErrorAction :: IgnoreError = err. err . action {
43544358 // We got a temporary failure updating monitor, but will claim the
@@ -4357,7 +4361,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43574361 claimed_any_htlcs = true ;
43584362 } else { errs. push ( ( pk, err) ) ; }
43594363 } ,
4360- ClaimFundsFromHop :: PrevHopForceClosed => unreachable ! ( "We already checked for channel existence, we can't fail here!" ) ,
4364+ ClaimFundsFromHop :: PrevHopForceClosed => {
4365+ // This should be incredibly rare - we checked that all the channels were
4366+ // open above, though as we release the lock at each loop iteration it's
4367+ // still possible. We should still claim the HTLC on-chain through the
4368+ // closed-channel-update generated in claim_funds_from_hop.
4369+ } ,
43614370 ClaimFundsFromHop :: DuplicateClaim => {
43624371 // While we should never get here in most cases, if we do, it likely
43634372 // indicates that the HTLC was timed out some time ago and is no longer
@@ -4368,7 +4377,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43684377 }
43694378 }
43704379 }
4371- mem:: drop ( channel_state_lock ) ;
4380+ mem:: drop ( channel_state ) ;
43724381 if !valid_mpp {
43734382 for htlc in sources. drain ( ..) {
43744383 let mut htlc_msat_height_data = htlc. value . to_be_bytes ( ) . to_vec ( ) ;
@@ -4395,11 +4404,11 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43954404 }
43964405 }
43974406
4398- fn claim_funds_from_hop ( & self , channel_state_lock : & mut MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > , prev_hop : HTLCPreviousHopData , payment_preimage : PaymentPreimage ) -> ClaimFundsFromHop {
4407+ fn claim_funds_from_hop ( & self , mut channel_state_lock : MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > , prev_hop : HTLCPreviousHopData , payment_preimage : PaymentPreimage ) -> ClaimFundsFromHop {
43994408 //TODO: Delay the claimed_funds relaying just like we do outbound relay!
44004409
44014410 let chan_id = prev_hop. outpoint . to_channel_id ( ) ;
4402- let channel_state = & mut * * channel_state_lock;
4411+ let channel_state = & mut * channel_state_lock;
44034412 if let hash_map:: Entry :: Occupied ( mut chan) = channel_state. by_id . entry ( chan_id) {
44044413 match chan. get_mut ( ) . get_update_fulfill_htlc_and_commit ( prev_hop. htlc_id , payment_preimage, & self . logger ) {
44054414 Ok ( msgs_monitor_option) => {
@@ -4499,7 +4508,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44994508 }
45004509 }
45014510
4502- fn claim_funds_internal ( & self , mut channel_state_lock : MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > , source : HTLCSource , payment_preimage : PaymentPreimage , forwarded_htlc_value_msat : Option < u64 > , from_onchain : bool , next_channel_id : [ u8 ; 32 ] ) {
4511+ fn claim_funds_internal ( & self , channel_state_lock : MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > , source : HTLCSource , payment_preimage : PaymentPreimage , forwarded_htlc_value_msat : Option < u64 > , from_onchain : bool , next_channel_id : [ u8 ; 32 ] ) {
45034512 match source {
45044513 HTLCSource :: OutboundRoute { session_priv, payment_id, path, .. } => {
45054514 mem:: drop ( channel_state_lock) ;
@@ -4546,7 +4555,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
45464555 } ,
45474556 HTLCSource :: PreviousHopData ( hop_data) => {
45484557 let prev_outpoint = hop_data. outpoint ;
4549- let res = self . claim_funds_from_hop ( & mut channel_state_lock, hop_data, payment_preimage) ;
4558+ let res = self . claim_funds_from_hop ( channel_state_lock, hop_data, payment_preimage) ;
45504559 let claimed_htlc = if let ClaimFundsFromHop :: DuplicateClaim = res { false } else { true } ;
45514560 let htlc_claim_value_msat = match res {
45524561 ClaimFundsFromHop :: MonitorUpdateFail ( _, _, amt_opt) => amt_opt,
@@ -4560,7 +4569,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
45604569 // update to. Instead, we simply document in `PaymentForwarded` that this
45614570 // can happen.
45624571 }
4563- mem:: drop ( channel_state_lock) ;
45644572 if let ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) = res {
45654573 let result: Result < ( ) , _ > = Err ( err) ;
45664574 let _ = handle_error ! ( self , result, pk) ;
0 commit comments