@@ -4294,8 +4294,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4294
4294
let mut expected_amt_msat = None ;
4295
4295
let mut valid_mpp = true ;
4296
4296
let mut errs = Vec :: new ( ) ;
4297
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4298
- let channel_state = & mut * channel_state_lock;
4297
+ let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
4299
4298
for htlc in sources. iter ( ) {
4300
4299
let chan_id = match self . short_to_chan_info . read ( ) . unwrap ( ) . get ( & htlc. prev_hop . short_channel_id ) {
4301
4300
Some ( ( _cp_id, chan_id) ) => chan_id. clone ( ) ,
@@ -4305,7 +4304,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4305
4304
}
4306
4305
} ;
4307
4306
4308
- if let None = channel_state. by_id . get ( & chan_id) {
4307
+ if let None = channel_state. as_ref ( ) . unwrap ( ) . by_id . get ( & chan_id) {
4309
4308
valid_mpp = false ;
4310
4309
break ;
4311
4310
}
@@ -4343,7 +4342,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4343
4342
}
4344
4343
if valid_mpp {
4345
4344
for htlc in sources. drain ( ..) {
4346
- match self . claim_funds_from_hop ( & mut channel_state_lock, htlc. prev_hop , payment_preimage,
4345
+ if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
4346
+ match self . claim_funds_from_hop ( channel_state. take ( ) . unwrap ( ) , htlc. prev_hop ,
4347
+ payment_preimage,
4347
4348
|_| Some ( MonitorUpdateCompletionAction :: PaymentClaimed { payment_hash } ) )
4348
4349
{
4349
4350
ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) => {
@@ -4353,7 +4354,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4353
4354
log_error ! ( self . logger, "Temporary failure claiming HTLC, treating as success: {}" , err. err. err) ;
4354
4355
} else { errs. push ( ( pk, err) ) ; }
4355
4356
} ,
4356
- ClaimFundsFromHop :: PrevHopForceClosed => unreachable ! ( "We already checked for channel existence, we can't fail here!" ) ,
4357
+ ClaimFundsFromHop :: PrevHopForceClosed => {
4358
+ // This should be incredibly rare - we checked that all the channels were
4359
+ // open above, though as we release the lock at each loop iteration it's
4360
+ // still possible. We should still claim the HTLC on-chain through the
4361
+ // closed-channel-update generated in claim_funds_from_hop.
4362
+ } ,
4357
4363
ClaimFundsFromHop :: DuplicateClaim => {
4358
4364
// While we should never get here in most cases, if we do, it likely
4359
4365
// indicates that the HTLC was timed out some time ago and is no longer
@@ -4364,7 +4370,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4364
4370
}
4365
4371
}
4366
4372
}
4367
- mem:: drop ( channel_state_lock ) ;
4373
+ mem:: drop ( channel_state ) ;
4368
4374
if !valid_mpp {
4369
4375
for htlc in sources. drain ( ..) {
4370
4376
let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
@@ -4385,13 +4391,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4385
4391
}
4386
4392
4387
4393
fn claim_funds_from_hop < ComplFunc : FnOnce ( Option < u64 > ) -> Option < MonitorUpdateCompletionAction > > ( & self ,
4388
- channel_state_lock : & mut MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > ,
4394
+ mut channel_state_lock : MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > ,
4389
4395
prev_hop : HTLCPreviousHopData , payment_preimage : PaymentPreimage , completion_action : ComplFunc )
4390
4396
-> ClaimFundsFromHop {
4391
4397
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
4392
4398
4393
4399
let chan_id = prev_hop. outpoint . to_channel_id ( ) ;
4394
- let channel_state = & mut * * channel_state_lock;
4400
+ let channel_state = & mut * channel_state_lock;
4395
4401
if let hash_map:: Entry :: Occupied ( mut chan) = channel_state. by_id . entry ( chan_id) {
4396
4402
match chan. get_mut ( ) . get_update_fulfill_htlc_and_commit ( prev_hop. htlc_id , payment_preimage, & self . logger ) {
4397
4403
Ok ( msgs_monitor_option) => {
@@ -4501,7 +4507,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4501
4507
}
4502
4508
}
4503
4509
4504
- 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 ] ) {
4510
+ 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 ] ) {
4505
4511
match source {
4506
4512
HTLCSource :: OutboundRoute { session_priv, payment_id, path, .. } => {
4507
4513
mem:: drop ( channel_state_lock) ;
@@ -4548,7 +4554,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4548
4554
} ,
4549
4555
HTLCSource :: PreviousHopData ( hop_data) => {
4550
4556
let prev_outpoint = hop_data. outpoint ;
4551
- let res = self . claim_funds_from_hop ( & mut channel_state_lock, hop_data, payment_preimage,
4557
+ let res = self . claim_funds_from_hop ( channel_state_lock, hop_data, payment_preimage,
4552
4558
|htlc_claim_value_msat| {
4553
4559
if let Some ( forwarded_htlc_value) = forwarded_htlc_value_msat {
4554
4560
let fee_earned_msat = if let Some ( claimed_htlc_value) = htlc_claim_value_msat {
@@ -4566,7 +4572,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4566
4572
} } )
4567
4573
} else { None }
4568
4574
} ) ;
4569
- mem:: drop ( channel_state_lock) ;
4570
4575
if let ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) = res {
4571
4576
let result: Result < ( ) , _ > = Err ( err) ;
4572
4577
let _ = handle_error ! ( self , result, pk) ;
0 commit comments