- for htlc in self.context.pending_inbound_htlcs.iter_mut() {
- let new_forward = if let &InboundHTLCState::RemoteAnnounced(ref forward_info) = &htlc.state {
- Some(forward_info.clone())
- } else { None };
- if let Some(forward_info) = new_forward {
- log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToAnnounce due to commitment_signed in channel {}.",
- log_bytes!(htlc.payment_hash.0), log_bytes!(self.context.channel_id));
- htlc.state = InboundHTLCState::AwaitingRemoteRevokeToAnnounce(forward_info);
- need_commitment = true;
- }
- }
- let mut claimed_htlcs = Vec::new();
- for htlc in self.context.pending_outbound_htlcs.iter_mut() {
- if let &mut OutboundHTLCState::RemoteRemoved(ref mut outcome) = &mut htlc.state {
- log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToRemove due to commitment_signed in channel {}.",
- log_bytes!(htlc.payment_hash.0), log_bytes!(self.context.channel_id));
- // Grab the preimage, if it exists, instead of cloning
- let mut reason = OutboundHTLCOutcome::Success(None);
- mem::swap(outcome, &mut reason);
- if let OutboundHTLCOutcome::Success(Some(preimage)) = reason {
- // If a user (a) receives an HTLC claim using LDK 0.0.104 or before, then (b)
- // upgrades to LDK 0.0.114 or later before the HTLC is fully resolved, we could
- // have a `Success(None)` reason. In this case we could forget some HTLC
- // claims, but such an upgrade is unlikely and including claimed HTLCs here
- // fixes a bug which the user was exposed to on 0.0.104 when they started the
- // claim anyway.
- claimed_htlcs.push((SentHTLCId::from_source(&htlc.source), preimage));
- }
- htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove(reason);
- need_commitment = true;
- }
- }
-
- self.context.latest_monitor_update_id += 1;
- let mut monitor_update = ChannelMonitorUpdate {
- update_id: self.context.latest_monitor_update_id,
- updates: vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
- commitment_tx: holder_commitment_tx,
- htlc_outputs: htlcs_and_sigs,
- claimed_htlcs,
- nondust_htlc_sources,
- }]
- };
-
- self.context.cur_holder_commitment_transaction_number -= 1;
- // Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
- // build_commitment_no_status_check() next which will reset this to RAAFirst.
- self.context.resend_order = RAACommitmentOrder::CommitmentFirst;
-
- if (self.context.channel_state & ChannelState::MonitorUpdateInProgress as u32) != 0 {
- // In case we initially failed monitor updating without requiring a response, we need
- // to make sure the RAA gets sent first.
- self.context.monitor_pending_revoke_and_ack = true;
- if need_commitment && (self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
- // If we were going to send a commitment_signed after the RAA, go ahead and do all
- // the corresponding HTLC status updates so that get_last_commitment_update
- // includes the right HTLCs.
- self.context.monitor_pending_commitment_signed = true;
- let mut additional_update = self.build_commitment_no_status_check(logger);
- // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
- // strictly increasing by one, so decrement it here.
- self.context.latest_monitor_update_id = monitor_update.update_id;
- monitor_update.updates.append(&mut additional_update.updates);
- }
- log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updated HTLC state but awaiting a monitor update resolution to reply.",
- log_bytes!(self.context.channel_id));
- return Ok(self.push_ret_blockable_mon_update(monitor_update));
- }
-
- let need_commitment_signed = if need_commitment && (self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
- // If we're AwaitingRemoteRevoke we can't send a new commitment here, but that's ok -
- // we'll send one right away when we get the revoke_and_ack when we
- // free_holding_cell_htlcs().
- let mut additional_update = self.build_commitment_no_status_check(logger);
- // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
- // strictly increasing by one, so decrement it here.
- self.context.latest_monitor_update_id = monitor_update.update_id;
- monitor_update.updates.append(&mut additional_update.updates);
- true
- } else { false };
-
- log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updating HTLC state and responding with{} a revoke_and_ack.",
- log_bytes!(self.context.channel_id()), if need_commitment_signed { " our own commitment_signed and" } else { "" });
- self.monitor_updating_paused(true, need_commitment_signed, false, Vec::new(), Vec::new(), Vec::new());
- return Ok(self.push_ret_blockable_mon_update(monitor_update));