@@ -4284,10 +4284,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4284
4284
} ;
4285
4285
debug_assert ! ( !sources. is_empty( ) ) ;
4286
4286
4287
- // If we are claiming an MPP payment, we have to take special care to ensure that each
4288
- // channel exists before claiming all of the payments (inside one lock).
4289
- // Note that channel existance is sufficient as we should always get a monitor update
4290
- // which will take care of the real HTLC claim enforcement.
4287
+ // If we are claiming an MPP payment, we check that all channels which contain a claimable
4288
+ // HTLC still exist. While this isn't guaranteed to remain true if a channel closes while
4289
+ // we're claiming (or even after we claim, before the commitment update dance completes),
4290
+ // it should be a relatively rare race, and we'd rather not claim HTLCs that require us to
4291
+ // go on-chain (and lose the on-chain fee to do so) than just reject the payment.
4292
+ //
4293
+ // Note that we'll still always get our funds - as long as the generated
4294
+ // `ChannelMonitorUpdate` makes it out to the relevant monitor we can claim on-chain.
4291
4295
//
4292
4296
// If we find an HTLC which we would need to claim but for which we do not have a
4293
4297
// channel, we will fail all parts of the MPP payment. While we could wait and see if
@@ -4300,8 +4304,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4300
4304
let mut valid_mpp = true ;
4301
4305
let mut errs = Vec :: new ( ) ;
4302
4306
let mut claimed_any_htlcs = false ;
4303
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4304
- let channel_state = & mut * channel_state_lock;
4307
+ let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
4305
4308
for htlc in sources. iter ( ) {
4306
4309
let chan_id = match self . short_to_chan_info . read ( ) . unwrap ( ) . get ( & htlc. prev_hop . short_channel_id ) {
4307
4310
Some ( ( _cp_id, chan_id) ) => chan_id. clone ( ) ,
@@ -4311,7 +4314,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4311
4314
}
4312
4315
} ;
4313
4316
4314
- if let None = channel_state. by_id . get ( & chan_id) {
4317
+ if let None = channel_state. as_ref ( ) . unwrap ( ) . by_id . get ( & chan_id) {
4315
4318
valid_mpp = false ;
4316
4319
break ;
4317
4320
}
@@ -4349,7 +4352,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4349
4352
}
4350
4353
if valid_mpp {
4351
4354
for htlc in sources. drain ( ..) {
4352
- match self . claim_funds_from_hop ( & mut channel_state_lock, htlc. prev_hop , payment_preimage) {
4355
+ if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
4356
+ match self . claim_funds_from_hop ( channel_state. take ( ) . unwrap ( ) , htlc. prev_hop , payment_preimage) {
4353
4357
ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) => {
4354
4358
if let msgs:: ErrorAction :: IgnoreError = err. err . action {
4355
4359
// We got a temporary failure updating monitor, but will claim the
@@ -4358,7 +4362,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4358
4362
claimed_any_htlcs = true ;
4359
4363
} else { errs. push ( ( pk, err) ) ; }
4360
4364
} ,
4361
- ClaimFundsFromHop :: PrevHopForceClosed => unreachable ! ( "We already checked for channel existence, we can't fail here!" ) ,
4365
+ ClaimFundsFromHop :: PrevHopForceClosed => {
4366
+ // This should be incredibly rare - we checked that all the channels were
4367
+ // open above, though as we release the lock at each loop iteration it's
4368
+ // still possible. We should still claim the HTLC on-chain through the
4369
+ // closed-channel-update generated in claim_funds_from_hop.
4370
+ } ,
4362
4371
ClaimFundsFromHop :: DuplicateClaim => {
4363
4372
// While we should never get here in most cases, if we do, it likely
4364
4373
// indicates that the HTLC was timed out some time ago and is no longer
@@ -4369,7 +4378,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4369
4378
}
4370
4379
}
4371
4380
}
4372
- mem:: drop ( channel_state_lock ) ;
4381
+ mem:: drop ( channel_state ) ;
4373
4382
if !valid_mpp {
4374
4383
for htlc in sources. drain ( ..) {
4375
4384
let mut htlc_msat_height_data = htlc. value . to_be_bytes ( ) . to_vec ( ) ;
@@ -4396,11 +4405,11 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4396
4405
}
4397
4406
}
4398
4407
4399
- fn claim_funds_from_hop ( & self , channel_state_lock : & mut MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > , prev_hop : HTLCPreviousHopData , payment_preimage : PaymentPreimage ) -> ClaimFundsFromHop {
4408
+ fn claim_funds_from_hop ( & self , mut channel_state_lock : MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > , prev_hop : HTLCPreviousHopData , payment_preimage : PaymentPreimage ) -> ClaimFundsFromHop {
4400
4409
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
4401
4410
4402
4411
let chan_id = prev_hop. outpoint . to_channel_id ( ) ;
4403
- let channel_state = & mut * * channel_state_lock;
4412
+ let channel_state = & mut * channel_state_lock;
4404
4413
if let hash_map:: Entry :: Occupied ( mut chan) = channel_state. by_id . entry ( chan_id) {
4405
4414
match chan. get_mut ( ) . get_update_fulfill_htlc_and_commit ( prev_hop. htlc_id , payment_preimage, & self . logger ) {
4406
4415
Ok ( msgs_monitor_option) => {
@@ -4500,7 +4509,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4500
4509
}
4501
4510
}
4502
4511
4503
- 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 ] ) {
4512
+ 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 ] ) {
4504
4513
match source {
4505
4514
HTLCSource :: OutboundRoute { session_priv, payment_id, path, .. } => {
4506
4515
mem:: drop ( channel_state_lock) ;
@@ -4547,7 +4556,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4547
4556
} ,
4548
4557
HTLCSource :: PreviousHopData ( hop_data) => {
4549
4558
let prev_outpoint = hop_data. outpoint ;
4550
- let res = self . claim_funds_from_hop ( & mut channel_state_lock, hop_data, payment_preimage) ;
4559
+ let res = self . claim_funds_from_hop ( channel_state_lock, hop_data, payment_preimage) ;
4551
4560
let claimed_htlc = if let ClaimFundsFromHop :: DuplicateClaim = res { false } else { true } ;
4552
4561
let htlc_claim_value_msat = match res {
4553
4562
ClaimFundsFromHop :: MonitorUpdateFail ( _, _, amt_opt) => amt_opt,
@@ -4561,7 +4570,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4561
4570
// update to. Instead, we simply document in `PaymentForwarded` that this
4562
4571
// can happen.
4563
4572
}
4564
- mem:: drop ( channel_state_lock) ;
4565
4573
if let ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) = res {
4566
4574
let result: Result < ( ) , _ > = Err ( err) ;
4567
4575
let _ = handle_error ! ( self , result, pk) ;
0 commit comments