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