@@ -4303,7 +4303,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4303
4303
let mut expected_amt_msat = None ;
4304
4304
let mut valid_mpp = true ;
4305
4305
let mut errs = Vec :: new ( ) ;
4306
- let mut claimed_any_htlcs = false ;
4307
4306
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
4308
4307
for htlc in sources. iter ( ) {
4309
4308
let chan_id = match self . short_to_chan_info . read ( ) . unwrap ( ) . get ( & htlc. prev_hop . short_channel_id ) {
@@ -4353,13 +4352,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4353
4352
if valid_mpp {
4354
4353
for htlc in sources. drain ( ..) {
4355
4354
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
4356
- match self . claim_funds_from_hop ( channel_state. take ( ) . unwrap ( ) , htlc. prev_hop , payment_preimage) {
4355
+ match self . claim_funds_from_hop ( channel_state. take ( ) . unwrap ( ) , htlc. prev_hop , payment_preimage,
4356
+ |_| Some ( MonitorUpdateCompletionAction :: PaymentClaimed { payment_hash } ) )
4357
+ {
4357
4358
ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) => {
4358
4359
if let msgs:: ErrorAction :: IgnoreError = err. err . action {
4359
4360
// We got a temporary failure updating monitor, but will claim the
4360
4361
// HTLC when the monitor updating is restored (or on chain).
4361
4362
log_error ! ( self . logger, "Temporary failure claiming HTLC, treating as success: {}" , err. err. err) ;
4362
- claimed_any_htlcs = true ;
4363
4363
} else { errs. push ( ( pk, err) ) ; }
4364
4364
} ,
4365
4365
ClaimFundsFromHop :: PrevHopForceClosed => {
@@ -4374,7 +4374,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4374
4374
// available to be claimed. Thus, it does not make sense to set
4375
4375
// `claimed_any_htlcs`.
4376
4376
} ,
4377
- ClaimFundsFromHop :: Success ( _) => claimed_any_htlcs = true ,
4377
+ ClaimFundsFromHop :: Success ( _) => { } ,
4378
4378
}
4379
4379
}
4380
4380
}
@@ -4388,14 +4388,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4388
4388
let receiver = HTLCDestination :: FailedPayment { payment_hash } ;
4389
4389
self . fail_htlc_backwards_internal ( & source, & payment_hash, & reason, receiver) ;
4390
4390
}
4391
- }
4392
-
4393
- let ClaimingPayment { amount_msat, payment_purpose : purpose, receiver_node_id } =
4394
- self . claimable_payments . lock ( ) . unwrap ( ) . pending_claimed_payments . remove ( & payment_hash) . unwrap ( ) ;
4395
- if claimed_any_htlcs {
4396
- self . pending_events . lock ( ) . unwrap ( ) . push ( events:: Event :: PaymentClaimed {
4397
- payment_hash, purpose, amount_msat, receiver_node_id : Some ( receiver_node_id) ,
4398
- } ) ;
4391
+ self . claimable_payments . lock ( ) . unwrap ( ) . pending_claimed_payments . remove ( & payment_hash) ;
4399
4392
}
4400
4393
4401
4394
// Now we can handle any errors which were generated.
@@ -4405,12 +4398,16 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4405
4398
}
4406
4399
}
4407
4400
4408
- fn claim_funds_from_hop ( & self , mut channel_state_lock : MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > , prev_hop : HTLCPreviousHopData , payment_preimage : PaymentPreimage ) -> ClaimFundsFromHop {
4401
+ fn claim_funds_from_hop < ComplFunc : FnOnce ( Option < u64 > ) -> Option < MonitorUpdateCompletionAction > > ( & self ,
4402
+ mut channel_state_lock : MutexGuard < ChannelHolder < <K :: Target as KeysInterface >:: Signer > > ,
4403
+ prev_hop : HTLCPreviousHopData , payment_preimage : PaymentPreimage , completion_action : ComplFunc )
4404
+ -> ClaimFundsFromHop {
4409
4405
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
4410
4406
4411
4407
let chan_id = prev_hop. outpoint . to_channel_id ( ) ;
4412
4408
let channel_state = & mut * channel_state_lock;
4413
4409
if let hash_map:: Entry :: Occupied ( mut chan) = channel_state. by_id . entry ( chan_id) {
4410
+ let counterparty_node_id = chan. get ( ) . get_counterparty_node_id ( ) ;
4414
4411
match chan. get_mut ( ) . get_update_fulfill_htlc_and_commit ( prev_hop. htlc_id , payment_preimage, & self . logger ) {
4415
4412
Ok ( msgs_monitor_option) => {
4416
4413
if let UpdateFulfillCommitFetch :: NewClaim { msgs, htlc_value_msat, monitor_update } = msgs_monitor_option {
@@ -4420,10 +4417,11 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4420
4417
log_given_level ! ( self . logger, if e == ChannelMonitorUpdateStatus :: PermanentFailure { Level :: Error } else { Level :: Debug } ,
4421
4418
"Failed to update channel monitor with preimage {:?}: {:?}" ,
4422
4419
payment_preimage, e) ;
4420
+ let err = handle_monitor_update_res ! ( self , e, chan, RAACommitmentOrder :: CommitmentFirst , false , msgs. is_some( ) ) . unwrap_err ( ) ;
4421
+ mem:: drop ( channel_state_lock) ;
4422
+ self . handle_monitor_update_completion_actions ( completion_action ( Some ( htlc_value_msat) ) ) ;
4423
4423
return ClaimFundsFromHop :: MonitorUpdateFail (
4424
- chan. get ( ) . get_counterparty_node_id ( ) ,
4425
- handle_monitor_update_res ! ( self , e, chan, RAACommitmentOrder :: CommitmentFirst , false , msgs. is_some( ) ) . unwrap_err ( ) ,
4426
- Some ( htlc_value_msat)
4424
+ counterparty_node_id, err, Some ( htlc_value_msat)
4427
4425
) ;
4428
4426
}
4429
4427
}
@@ -4442,6 +4440,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4442
4440
}
4443
4441
} ) ;
4444
4442
}
4443
+ mem:: drop ( channel_state_lock) ;
4444
+ self . handle_monitor_update_completion_actions ( completion_action ( Some ( htlc_value_msat) ) ) ;
4445
4445
return ClaimFundsFromHop :: Success ( htlc_value_msat) ;
4446
4446
} else {
4447
4447
return ClaimFundsFromHop :: DuplicateClaim ;
@@ -4456,11 +4456,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4456
4456
payment_preimage, e) ;
4457
4457
} ,
4458
4458
}
4459
- let counterparty_node_id = chan. get ( ) . get_counterparty_node_id ( ) ;
4460
4459
let ( drop, res) = convert_chan_err ! ( self , e, chan. get_mut( ) , & chan_id) ;
4461
4460
if drop {
4462
4461
chan. remove_entry ( ) ;
4463
4462
}
4463
+ mem:: drop ( channel_state_lock) ;
4464
+ self . handle_monitor_update_completion_actions ( completion_action ( None ) ) ;
4464
4465
return ClaimFundsFromHop :: MonitorUpdateFail ( counterparty_node_id, res, None ) ;
4465
4466
} ,
4466
4467
}
@@ -4482,6 +4483,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4482
4483
log_error ! ( self . logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}" ,
4483
4484
payment_preimage, update_res) ;
4484
4485
}
4486
+ mem:: drop ( channel_state_lock) ;
4487
+ // Note that we do process the completion action here. This totally could be a
4488
+ // duplicate claim, but we have no way of knowing without interrogating the
4489
+ // `ChannelMonitor` we've provided the above update to. Instead, note that `Event`s are
4490
+ // generally always allowed to be duplicative (and it's specifically noted in
4491
+ // `PaymentForwarded`).
4492
+ self . handle_monitor_update_completion_actions ( completion_action ( None ) ) ;
4485
4493
return ClaimFundsFromHop :: PrevHopForceClosed
4486
4494
}
4487
4495
}
@@ -4556,43 +4564,28 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4556
4564
} ,
4557
4565
HTLCSource :: PreviousHopData ( hop_data) => {
4558
4566
let prev_outpoint = hop_data. outpoint ;
4559
- let res = self . claim_funds_from_hop ( channel_state_lock, hop_data, payment_preimage) ;
4560
- let claimed_htlc = if let ClaimFundsFromHop :: DuplicateClaim = res { false } else { true } ;
4561
- let htlc_claim_value_msat = match res {
4562
- ClaimFundsFromHop :: MonitorUpdateFail ( _, _, amt_opt) => amt_opt,
4563
- ClaimFundsFromHop :: Success ( amt) => Some ( amt) ,
4564
- _ => None ,
4565
- } ;
4566
- if let ClaimFundsFromHop :: PrevHopForceClosed = res {
4567
- // Note that we do *not* set `claimed_htlc` to false here. In fact, this
4568
- // totally could be a duplicate claim, but we have no way of knowing
4569
- // without interrogating the `ChannelMonitor` we've provided the above
4570
- // update to. Instead, we simply document in `PaymentForwarded` that this
4571
- // can happen.
4572
- }
4567
+ let res = self . claim_funds_from_hop ( channel_state_lock, hop_data, payment_preimage,
4568
+ |htlc_claim_value_msat| {
4569
+ if let Some ( forwarded_htlc_value) = forwarded_htlc_value_msat {
4570
+ let fee_earned_msat = if let Some ( claimed_htlc_value) = htlc_claim_value_msat {
4571
+ Some ( claimed_htlc_value - forwarded_htlc_value)
4572
+ } else { None } ;
4573
+
4574
+ let prev_channel_id = Some ( prev_outpoint. to_channel_id ( ) ) ;
4575
+ let next_channel_id = Some ( next_channel_id) ;
4576
+
4577
+ Some ( MonitorUpdateCompletionAction :: EmitEvent { event : events:: Event :: PaymentForwarded {
4578
+ fee_earned_msat,
4579
+ claim_from_onchain_tx : from_onchain,
4580
+ prev_channel_id,
4581
+ next_channel_id,
4582
+ } } )
4583
+ } else { None }
4584
+ } ) ;
4573
4585
if let ClaimFundsFromHop :: MonitorUpdateFail ( pk, err, _) = res {
4574
4586
let result: Result < ( ) , _ > = Err ( err) ;
4575
4587
let _ = handle_error ! ( self , result, pk) ;
4576
4588
}
4577
-
4578
- if claimed_htlc {
4579
- if let Some ( forwarded_htlc_value) = forwarded_htlc_value_msat {
4580
- let fee_earned_msat = if let Some ( claimed_htlc_value) = htlc_claim_value_msat {
4581
- Some ( claimed_htlc_value - forwarded_htlc_value)
4582
- } else { None } ;
4583
-
4584
- let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
4585
- let prev_channel_id = Some ( prev_outpoint. to_channel_id ( ) ) ;
4586
- let next_channel_id = Some ( next_channel_id) ;
4587
-
4588
- pending_events. push ( events:: Event :: PaymentForwarded {
4589
- fee_earned_msat,
4590
- claim_from_onchain_tx : from_onchain,
4591
- prev_channel_id,
4592
- next_channel_id,
4593
- } ) ;
4594
- }
4595
- }
4596
4589
} ,
4597
4590
}
4598
4591
}
0 commit comments