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