@@ -4281,10 +4281,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4281
4281
} ;
4282
4282
debug_assert ! ( !sources. is_empty( ) ) ;
4283
4283
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.
4288
4292
//
4289
4293
// If we find an HTLC which we would need to claim but for which we do not have a
4290
4294
// 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
4297
4301
let mut valid_mpp = true ;
4298
4302
let mut errs = Vec :: new ( ) ;
4299
4303
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 ( ) ) ;
4302
4305
for htlc in sources. iter ( ) {
4303
4306
let chan_id = match self . short_to_chan_info . read ( ) . unwrap ( ) . get ( & htlc. prev_hop . short_channel_id ) {
4304
4307
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
4308
4311
}
4309
4312
} ;
4310
4313
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) {
4312
4315
valid_mpp = false ;
4313
4316
break ;
4314
4317
}
@@ -4346,7 +4349,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4346
4349
}
4347
4350
if valid_mpp {
4348
4351
for htlc in sources. drain ( ..) {
4349
- match self . claim_funds_from_hop ( & mut channel_state_lock, htlc. prev_hop , payment_preimage) {
4352
+ if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
4353
+ match self . claim_funds_from_hop ( channel_state. take ( ) . unwrap ( ) , htlc. prev_hop , payment_preimage) {
4350
4354
ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) => {
4351
4355
if let msgs:: ErrorAction :: IgnoreError = err. err . action {
4352
4356
// We got a temporary failure updating monitor, but will claim the
@@ -4355,7 +4359,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4355
4359
claimed_any_htlcs = true ;
4356
4360
} else { errs. push ( ( pk, err) ) ; }
4357
4361
} ,
4358
- ClaimFundsFromHop :: PrevHopForceClosed => unreachable ! ( "We already checked for channel existence, we can't fail here!" ) ,
4362
+ ClaimFundsFromHop :: PrevHopForceClosed => {
4363
+ // This should be incredibly rare - we checked that all the channels were
4364
+ // open above, though as we release the lock at each loop iteration it's
4365
+ // still possible. We should still claim the HTLC on-chain through the
4366
+ // closed-channel-update generated in claim_funds_from_hop.
4367
+ } ,
4359
4368
ClaimFundsFromHop :: DuplicateClaim => {
4360
4369
// While we should never get here in most cases, if we do, it likely
4361
4370
// indicates that the HTLC was timed out some time ago and is no longer
@@ -4366,7 +4375,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4366
4375
}
4367
4376
}
4368
4377
}
4369
- mem:: drop ( channel_state_lock ) ;
4378
+ mem:: drop ( channel_state ) ;
4370
4379
if !valid_mpp {
4371
4380
for htlc in sources. drain ( ..) {
4372
4381
let mut htlc_msat_height_data = htlc. value . to_be_bytes ( ) . to_vec ( ) ;
@@ -4393,11 +4402,11 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4393
4402
}
4394
4403
}
4395
4404
4396
- fn claim_funds_from_hop ( & self , channel_state_lock : & mut MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > , prev_hop : HTLCPreviousHopData , payment_preimage : PaymentPreimage ) -> ClaimFundsFromHop {
4405
+ fn claim_funds_from_hop ( & self , mut channel_state_lock : MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > , prev_hop : HTLCPreviousHopData , payment_preimage : PaymentPreimage ) -> ClaimFundsFromHop {
4397
4406
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
4398
4407
4399
4408
let chan_id = prev_hop. outpoint . to_channel_id ( ) ;
4400
- let channel_state = & mut * * channel_state_lock;
4409
+ let channel_state = & mut * channel_state_lock;
4401
4410
if let hash_map:: Entry :: Occupied ( mut chan) = channel_state. by_id . entry ( chan_id) {
4402
4411
match chan. get_mut ( ) . get_update_fulfill_htlc_and_commit ( prev_hop. htlc_id , payment_preimage, & self . logger ) {
4403
4412
Ok ( msgs_monitor_option) => {
@@ -4497,7 +4506,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4497
4506
}
4498
4507
}
4499
4508
4500
- 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 ] ) {
4509
+ 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 ] ) {
4501
4510
match source {
4502
4511
HTLCSource :: OutboundRoute { session_priv, payment_id, path, .. } => {
4503
4512
mem:: drop ( channel_state_lock) ;
@@ -4544,7 +4553,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4544
4553
} ,
4545
4554
HTLCSource :: PreviousHopData ( hop_data) => {
4546
4555
let prev_outpoint = hop_data. outpoint ;
4547
- let res = self . claim_funds_from_hop ( & mut channel_state_lock, hop_data, payment_preimage) ;
4556
+ let res = self . claim_funds_from_hop ( channel_state_lock, hop_data, payment_preimage) ;
4548
4557
let claimed_htlc = if let ClaimFundsFromHop :: DuplicateClaim = res { false } else { true } ;
4549
4558
let htlc_claim_value_msat = match res {
4550
4559
ClaimFundsFromHop :: MonitorUpdateFail ( _, _, amt_opt) => amt_opt,
@@ -4558,7 +4567,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4558
4567
// update to. Instead, we simply document in `PaymentForwarded` that this
4559
4568
// can happen.
4560
4569
}
4561
- mem:: drop ( channel_state_lock) ;
4562
4570
if let ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) = res {
4563
4571
let result: Result < ( ) , _ > = Err ( err) ;
4564
4572
let _ = handle_error ! ( self , result, pk) ;
0 commit comments