- let res = self.claim_funds_from_hop(&mut channel_state_lock, hop_data, payment_preimage);
- let claimed_htlc = if let ClaimFundsFromHop::DuplicateClaim = res { false } else { true };
- let htlc_claim_value_msat = match res {
- ClaimFundsFromHop::MonitorUpdateFail(_, _, amt_opt) => amt_opt,
- ClaimFundsFromHop::Success(amt) => Some(amt),
- _ => None,
- };
- if let ClaimFundsFromHop::PrevHopForceClosed = res {
- let preimage_update = ChannelMonitorUpdate {
- update_id: CLOSED_CHANNEL_UPDATE_ID,
- updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
- payment_preimage: payment_preimage.clone(),
- }],
- };
- // We update the ChannelMonitor on the backward link, after
- // receiving an offchain preimage event from the forward link (the
- // event being update_fulfill_htlc).
- let update_res = self.chain_monitor.update_channel(prev_outpoint, preimage_update);
- if update_res != ChannelMonitorUpdateStatus::Completed {
- // TODO: This needs to be handled somehow - if we receive a monitor update
- // with a preimage we *must* somehow manage to propagate it to the upstream
- // channel, or we must have an ability to receive the same event and try
- // again on restart.
- log_error!(self.logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}",
- payment_preimage, update_res);
- }
- // Note that we do *not* set `claimed_htlc` to false here. In fact, this
- // totally could be a duplicate claim, but we have no way of knowing
- // without interrogating the `ChannelMonitor` we've provided the above
- // update to. Instead, we simply document in `PaymentForwarded` that this
- // can happen.
- }
- mem::drop(channel_state_lock);
- if let ClaimFundsFromHop::MonitorUpdateFail(pk, err, _) = res {
+ let res = self.claim_funds_from_hop(channel_state_lock, hop_data, payment_preimage,
+ |htlc_claim_value_msat| {
+ if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
+ let fee_earned_msat = if let Some(claimed_htlc_value) = htlc_claim_value_msat {
+ Some(claimed_htlc_value - forwarded_htlc_value)
+ } else { None };
+
+ let prev_channel_id = Some(prev_outpoint.to_channel_id());
+ let next_channel_id = Some(next_channel_id);
+
+ Some(MonitorUpdateCompletionAction::EmitEvent { event: events::Event::PaymentForwarded {
+ fee_earned_msat,
+ claim_from_onchain_tx: from_onchain,
+ prev_channel_id,
+ next_channel_id,
+ }})
+ } else { None }
+ });
+ if let Err((pk, err)) = res {