@@ -4147,8 +4147,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4147
4147
let mut expected_amt_msat = None ;
4148
4148
let mut valid_mpp = true ;
4149
4149
let mut errs = Vec :: new ( ) ;
4150
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4151
- let channel_state = & mut * channel_state_lock;
4150
+ let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
4152
4151
for htlc in sources. iter ( ) {
4153
4152
let chan_id = match self . short_to_chan_info . read ( ) . unwrap ( ) . get ( & htlc. prev_hop . short_channel_id ) {
4154
4153
Some ( ( _cp_id, chan_id) ) => chan_id. clone ( ) ,
@@ -4158,7 +4157,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4158
4157
}
4159
4158
} ;
4160
4159
4161
- if let None = channel_state. by_id . get ( & chan_id) {
4160
+ if let None = channel_state. as_ref ( ) . unwrap ( ) . by_id . get ( & chan_id) {
4162
4161
valid_mpp = false ;
4163
4162
break ;
4164
4163
}
@@ -4196,7 +4195,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4196
4195
}
4197
4196
if valid_mpp {
4198
4197
for htlc in sources. drain ( ..) {
4199
- match self . claim_funds_from_hop ( & mut channel_state_lock, htlc. prev_hop , payment_preimage,
4198
+ if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
4199
+ match self . claim_funds_from_hop ( channel_state. take ( ) . unwrap ( ) , htlc. prev_hop ,
4200
+ payment_preimage,
4200
4201
|_| Some ( MonitorUpdateCompletionAction :: PaymentClaimed { payment_hash } ) )
4201
4202
{
4202
4203
ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) => {
@@ -4206,7 +4207,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4206
4207
log_error ! ( self . logger, "Temporary failure claiming HTLC, treating as success: {}" , err. err. err) ;
4207
4208
} else { errs. push ( ( pk, err) ) ; }
4208
4209
} ,
4209
- ClaimFundsFromHop :: PrevHopForceClosed => unreachable ! ( "We already checked for channel existence, we can't fail here!" ) ,
4210
+ ClaimFundsFromHop :: PrevHopForceClosed => {
4211
+ // This should be incredibly rare - we checked that all the channels were
4212
+ // open above, though as we release the lock at each loop iteration it's
4213
+ // still possible. We should still claim the HTLC on-chain through the
4214
+ // closed-channel-update generated in claim_funds_from_hop.
4215
+ } ,
4210
4216
ClaimFundsFromHop :: DuplicateClaim => {
4211
4217
// While we should never get here in most cases, if we do, it likely
4212
4218
// indicates that the HTLC was timed out some time ago and is no longer
@@ -4217,7 +4223,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4217
4223
}
4218
4224
}
4219
4225
}
4220
- mem:: drop ( channel_state_lock ) ;
4226
+ mem:: drop ( channel_state ) ;
4221
4227
if !valid_mpp {
4222
4228
for htlc in sources. drain ( ..) {
4223
4229
let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
@@ -4238,13 +4244,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4238
4244
}
4239
4245
4240
4246
fn claim_funds_from_hop < ComplFunc : FnOnce ( Option < u64 > ) -> Option < MonitorUpdateCompletionAction > > ( & self ,
4241
- channel_state_lock : & mut MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > ,
4247
+ mut channel_state_lock : MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > ,
4242
4248
prev_hop : HTLCPreviousHopData , payment_preimage : PaymentPreimage , completion_action : ComplFunc )
4243
4249
-> ClaimFundsFromHop {
4244
4250
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
4245
4251
4246
4252
let chan_id = prev_hop. outpoint . to_channel_id ( ) ;
4247
- let channel_state = & mut * * channel_state_lock;
4253
+ let channel_state = & mut * channel_state_lock;
4248
4254
if let hash_map:: Entry :: Occupied ( mut chan) = channel_state. by_id . entry ( chan_id) {
4249
4255
match chan. get_mut ( ) . get_update_fulfill_htlc_and_commit ( prev_hop. htlc_id , payment_preimage, & self . logger ) {
4250
4256
Ok ( msgs_monitor_option) => {
@@ -4354,7 +4360,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4354
4360
}
4355
4361
}
4356
4362
4357
- 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 ] ) {
4363
+ 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 ] ) {
4358
4364
match source {
4359
4365
HTLCSource :: OutboundRoute { session_priv, payment_id, path, .. } => {
4360
4366
mem:: drop ( channel_state_lock) ;
@@ -4401,7 +4407,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4401
4407
} ,
4402
4408
HTLCSource :: PreviousHopData ( hop_data) => {
4403
4409
let prev_outpoint = hop_data. outpoint ;
4404
- let res = self . claim_funds_from_hop ( & mut channel_state_lock, hop_data, payment_preimage,
4410
+ let res = self . claim_funds_from_hop ( channel_state_lock, hop_data, payment_preimage,
4405
4411
|htlc_claim_value_msat| {
4406
4412
if let Some ( forwarded_htlc_value) = forwarded_htlc_value_msat {
4407
4413
let fee_earned_msat = if let Some ( claimed_htlc_value) = htlc_claim_value_msat {
@@ -4419,7 +4425,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4419
4425
} } )
4420
4426
} else { None }
4421
4427
} ) ;
4422
- mem:: drop ( channel_state_lock) ;
4423
4428
if let ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) = res {
4424
4429
let result: Result < ( ) , _ > = Err ( err) ;
4425
4430
let _ = handle_error ! ( self , result, pk) ;
0 commit comments