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