+ self.received_commitment_while_awaiting_raa = false;
+ if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) == 0 {
+ // This is a response to our post-monitor-failed unfreeze messages, so we can clear the
+ // monitor_pending_order requirement as we won't re-send the monitor_pending messages.
+ self.monitor_pending_order = None;
+ }
+
+ let mut to_forward_infos = Vec::new();
+ let mut revoked_htlcs = Vec::new();
+ let mut update_fail_htlcs = Vec::new();
+ let mut update_fail_malformed_htlcs = Vec::new();
+ let mut require_commitment = false;
+ let mut value_to_self_msat_diff: i64 = 0;
+ // We really shouldnt have two passes here, but retain gives a non-mutable ref (Rust bug)
+ self.pending_inbound_htlcs.retain(|htlc| {
+ if let &InboundHTLCState::LocalRemoved(ref reason) = &htlc.state {
+ if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
+ value_to_self_msat_diff += htlc.amount_msat as i64;
+ }
+ false
+ } else { true }
+ });
+ self.pending_outbound_htlcs.retain(|htlc| {
+ if let OutboundHTLCState::AwaitingRemovedRemoteRevoke = htlc.state {
+ if let Some(reason) = htlc.fail_reason.clone() { // We really want take() here, but, again, non-mut ref :(
+ revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
+ } else {
+ // They fulfilled, so we sent them money
+ value_to_self_msat_diff -= htlc.amount_msat as i64;
+ }
+ false
+ } else { true }
+ });
+ for htlc in self.pending_inbound_htlcs.iter_mut() {
+ let swap = if let &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(_) = &htlc.state {
+ true
+ } else if let &InboundHTLCState::AwaitingAnnouncedRemoteRevoke(_) = &htlc.state {
+ true
+ } else { false };
+ if swap {
+ let mut state = InboundHTLCState::Committed;
+ mem::swap(&mut state, &mut htlc.state);
+
+ if let InboundHTLCState::AwaitingRemoteRevokeToAnnounce(forward_info) = state {
+ htlc.state = InboundHTLCState::AwaitingAnnouncedRemoteRevoke(forward_info);
+ require_commitment = true;
+ } else if let InboundHTLCState::AwaitingAnnouncedRemoteRevoke(forward_info) = state {
+ match forward_info {
+ PendingHTLCStatus::Fail(fail_msg) => {
+ require_commitment = true;
+ match fail_msg {
+ HTLCFailureMsg::Relay(msg) => {
+ htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(msg.reason.clone()));
+ update_fail_htlcs.push(msg)
+ },
+ HTLCFailureMsg::Malformed(msg) => {
+ htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed((msg.sha256_of_onion, msg.failure_code)));
+ update_fail_malformed_htlcs.push(msg)
+ },
+ }
+ },
+ PendingHTLCStatus::Forward(forward_info) => {
+ to_forward_infos.push((forward_info, htlc.htlc_id));
+ htlc.state = InboundHTLCState::Committed;
+ }
+ }
+ }
+ }
+ }
+ for htlc in self.pending_outbound_htlcs.iter_mut() {
+ if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
+ htlc.state = OutboundHTLCState::Committed;
+ } else if let OutboundHTLCState::AwaitingRemoteRevokeToRemove = htlc.state {
+ htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke;
+ require_commitment = true;
+ }
+ }
+ self.value_to_self_msat = (self.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
+
+ if self.channel_outbound {
+ if let Some(feerate) = self.pending_update_fee.take() {
+ self.feerate_per_kw = feerate;
+ }
+ } else {
+ if let Some(feerate) = self.pending_update_fee {
+ // Because a node cannot send two commitment_signed's in a row without getting a
+ // revoke_and_ack from us (as it would otherwise not know the per_commitment_point
+ // it should use to create keys with) and because a node can't send a
+ // commitment_signed without changes, checking if the feerate is equal to the
+ // pending feerate update is sufficient to detect require_commitment.
+ if feerate == self.feerate_per_kw {
+ require_commitment = true;
+ self.pending_update_fee = None;
+ }
+ }
+ }
+
+ if (self.channel_state & ChannelState::MonitorUpdateFailed as u32) == ChannelState::MonitorUpdateFailed as u32 {
+ // We can't actually generate a new commitment transaction (incl by freeing holding
+ // cells) while we can't update the monitor, so we just return what we have.
+ if require_commitment {
+ self.monitor_pending_commitment_signed = true;
+ }
+ self.monitor_pending_forwards.append(&mut to_forward_infos);
+ self.monitor_pending_failures.append(&mut revoked_htlcs);
+ return Ok((None, Vec::new(), Vec::new(), self.channel_monitor.clone()));
+ }
+
+ match self.free_holding_cell_htlcs()? {
+ Some(mut commitment_update) => {
+ commitment_update.0.update_fail_htlcs.reserve(update_fail_htlcs.len());
+ for fail_msg in update_fail_htlcs.drain(..) {
+ commitment_update.0.update_fail_htlcs.push(fail_msg);
+ }
+ commitment_update.0.update_fail_malformed_htlcs.reserve(update_fail_malformed_htlcs.len());
+ for fail_msg in update_fail_malformed_htlcs.drain(..) {
+ commitment_update.0.update_fail_malformed_htlcs.push(fail_msg);
+ }
+ Ok((Some(commitment_update.0), to_forward_infos, revoked_htlcs, commitment_update.1))
+ },
+ None => {
+ if require_commitment {
+ let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
+ Ok((Some(msgs::CommitmentUpdate {
+ update_add_htlcs: Vec::new(),
+ update_fulfill_htlcs: Vec::new(),
+ update_fail_htlcs,
+ update_fail_malformed_htlcs,
+ update_fee: None,
+ commitment_signed
+ }), to_forward_infos, revoked_htlcs, monitor_update))
+ } else {
+ Ok((None, to_forward_infos, revoked_htlcs, self.channel_monitor.clone()))
+ }
+ }
+ }
+
+ }
+
+ /// Adds a pending update to this channel. See the doc for send_htlc for
+ /// further details on the optionness of the return value.
+ /// You MUST call send_commitment prior to any other calls on this Channel
+ fn send_update_fee(&mut self, feerate_per_kw: u64) -> Option<msgs::UpdateFee> {
+ if !self.channel_outbound {
+ panic!("Cannot send fee from inbound channel");
+ }
+ if !self.is_usable() {
+ panic!("Cannot update fee until channel is fully established and we haven't started shutting down");
+ }
+ if !self.is_live() {
+ panic!("Cannot update fee while peer is disconnected/we're awaiting a monitor update (ChannelManager should have caught this)");
+ }
+
+ if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
+ self.holding_cell_update_fee = Some(feerate_per_kw);
+ return None;
+ }
+
+ debug_assert!(self.pending_update_fee.is_none());
+ self.pending_update_fee = Some(feerate_per_kw);
+
+ Some(msgs::UpdateFee {
+ channel_id: self.channel_id,
+ feerate_per_kw: feerate_per_kw as u32,
+ })
+ }
+
+ pub fn send_update_fee_and_commit(&mut self, feerate_per_kw: u64) -> Result<Option<(msgs::UpdateFee, msgs::CommitmentSigned, ChannelMonitor)>, HandleError> {
+ match self.send_update_fee(feerate_per_kw) {
+ Some(update_fee) => {
+ let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
+ Ok(Some((update_fee, commitment_signed, monitor_update)))
+ },
+ None => Ok(None)
+ }
+ }
+
+ /// Removes any uncommitted HTLCs, to be used on peer disconnection, including any pending
+ /// HTLCs that we intended to add but haven't as we were waiting on a remote revoke.
+ /// Returns the set of PendingHTLCStatuses from remote uncommitted HTLCs (which we're
+ /// implicitly dropping) and the payment_hashes of HTLCs we tried to add but are dropping.
+ /// No further message handling calls may be made until a channel_reestablish dance has
+ /// completed.
+ pub fn remove_uncommitted_htlcs_and_mark_paused(&mut self) -> Vec<(HTLCSource, [u8; 32])> {
+ let mut outbound_drops = Vec::new();
+
+ assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
+ if self.channel_state < ChannelState::FundingSent as u32 {
+ self.channel_state = ChannelState::ShutdownComplete as u32;
+ return outbound_drops;
+ }
+
+ let mut inbound_drop_count = 0;
+ self.pending_inbound_htlcs.retain(|htlc| {
+ match htlc.state {
+ InboundHTLCState::RemoteAnnounced(_) => {
+ // They sent us an update_add_htlc but we never got the commitment_signed.
+ // We'll tell them what commitment_signed we're expecting next and they'll drop
+ // this HTLC accordingly
+ inbound_drop_count += 1;
+ false
+ },
+ InboundHTLCState::AwaitingRemoteRevokeToAnnounce(_)|InboundHTLCState::AwaitingAnnouncedRemoteRevoke(_) => {
+ // We received a commitment_signed updating this HTLC and (at least hopefully)
+ // sent a revoke_and_ack (which we can re-transmit) and have heard nothing
+ // in response to it yet, so don't touch it.
+ true
+ },
+ InboundHTLCState::Committed => true,
+ InboundHTLCState::LocalRemoved(_) => {
+ // We (hopefully) sent a commitment_signed updating this HTLC (which we can
+ // re-transmit if needed) and they may have even sent a revoke_and_ack back
+ // (that we missed). Keep this around for now and if they tell us they missed
+ // the commitment_signed we can re-transmit the update then.
+ true
+ },