@@ -4287,8 +4287,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4287
4287
let mut expected_amt_msat = None ;
4288
4288
let mut valid_mpp = true ;
4289
4289
let mut errs = Vec :: new ( ) ;
4290
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4291
- let channel_state = & mut * channel_state_lock;
4290
+ let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
4292
4291
for htlc in sources. iter ( ) {
4293
4292
let chan_id = match self . short_to_chan_info . read ( ) . unwrap ( ) . get ( & htlc. prev_hop . short_channel_id ) {
4294
4293
Some ( ( _cp_id, chan_id) ) => chan_id. clone ( ) ,
@@ -4298,7 +4297,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4298
4297
}
4299
4298
} ;
4300
4299
4301
- if let None = channel_state. by_id . get ( & chan_id) {
4300
+ if let None = channel_state. as_ref ( ) . unwrap ( ) . by_id . get ( & chan_id) {
4302
4301
valid_mpp = false ;
4303
4302
break ;
4304
4303
}
@@ -4336,7 +4335,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4336
4335
}
4337
4336
if valid_mpp {
4338
4337
for htlc in sources. drain ( ..) {
4339
- match self . claim_funds_from_hop ( & mut channel_state_lock, htlc. prev_hop , payment_preimage,
4338
+ if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
4339
+ match self . claim_funds_from_hop ( channel_state. take ( ) . unwrap ( ) , htlc. prev_hop ,
4340
+ payment_preimage,
4340
4341
|_| Some ( MonitorUpdateCompletionAction :: PaymentClaimed { payment_hash } ) )
4341
4342
{
4342
4343
ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) => {
@@ -4346,7 +4347,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4346
4347
log_error ! ( self . logger, "Temporary failure claiming HTLC, treating as success: {}" , err. err. err) ;
4347
4348
} else { errs. push ( ( pk, err) ) ; }
4348
4349
} ,
4349
- ClaimFundsFromHop :: PrevHopForceClosed => unreachable ! ( "We already checked for channel existence, we can't fail here!" ) ,
4350
+ ClaimFundsFromHop :: PrevHopForceClosed => {
4351
+ // This should be incredibly rare - we checked that all the channels were
4352
+ // open above, though as we release the lock at each loop iteration it's
4353
+ // still possible. We should still claim the HTLC on-chain through the
4354
+ // closed-channel-update generated in claim_funds_from_hop.
4355
+ } ,
4350
4356
ClaimFundsFromHop :: DuplicateClaim => {
4351
4357
// While we should never get here in most cases, if we do, it likely
4352
4358
// indicates that the HTLC was timed out some time ago and is no longer
@@ -4357,7 +4363,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4357
4363
}
4358
4364
}
4359
4365
}
4360
- mem:: drop ( channel_state_lock ) ;
4366
+ mem:: drop ( channel_state ) ;
4361
4367
if !valid_mpp {
4362
4368
for htlc in sources. drain ( ..) {
4363
4369
let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
@@ -4378,13 +4384,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4378
4384
}
4379
4385
4380
4386
fn claim_funds_from_hop < ComplFunc : FnOnce ( Option < u64 > ) -> Option < MonitorUpdateCompletionAction > > ( & self ,
4381
- channel_state_lock : & mut MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > ,
4387
+ mut channel_state_lock : MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > ,
4382
4388
prev_hop : HTLCPreviousHopData , payment_preimage : PaymentPreimage , completion_action : ComplFunc )
4383
4389
-> ClaimFundsFromHop {
4384
4390
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
4385
4391
4386
4392
let chan_id = prev_hop. outpoint . to_channel_id ( ) ;
4387
- let channel_state = & mut * * channel_state_lock;
4393
+ let channel_state = & mut * channel_state_lock;
4388
4394
if let hash_map:: Entry :: Occupied ( mut chan) = channel_state. by_id . entry ( chan_id) {
4389
4395
match chan. get_mut ( ) . get_update_fulfill_htlc_and_commit ( prev_hop. htlc_id , payment_preimage, & self . logger ) {
4390
4396
Ok ( msgs_monitor_option) => {
@@ -4494,7 +4500,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4494
4500
}
4495
4501
}
4496
4502
4497
- 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 ] ) {
4503
+ 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 ] ) {
4498
4504
match source {
4499
4505
HTLCSource :: OutboundRoute { session_priv, payment_id, path, .. } => {
4500
4506
mem:: drop ( channel_state_lock) ;
@@ -4541,7 +4547,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4541
4547
} ,
4542
4548
HTLCSource :: PreviousHopData ( hop_data) => {
4543
4549
let prev_outpoint = hop_data. outpoint ;
4544
- let res = self . claim_funds_from_hop ( & mut channel_state_lock, hop_data, payment_preimage,
4550
+ let res = self . claim_funds_from_hop ( channel_state_lock, hop_data, payment_preimage,
4545
4551
|htlc_claim_value_msat| {
4546
4552
if let Some ( forwarded_htlc_value) = forwarded_htlc_value_msat {
4547
4553
let fee_earned_msat = if let Some ( claimed_htlc_value) = htlc_claim_value_msat {
@@ -4559,7 +4565,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4559
4565
} } )
4560
4566
} else { None }
4561
4567
} ) ;
4562
- mem:: drop ( channel_state_lock) ;
4563
4568
if let ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) = res {
4564
4569
let result: Result < ( ) , _ > = Err ( err) ;
4565
4570
let _ = handle_error ! ( self , result, pk) ;
0 commit comments