+ pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<&HTLCSource, ChannelError> {
+ if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
+ return Err(ChannelError::Close("Got fail HTLC message when channel was not in an operational state"));
+ }
+ if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+ return Err(ChannelError::Close("Peer sent update_fail_htlc when we needed a channel_reestablish"));
+ }
+
+ self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))
+ }
+
+ pub fn update_fail_malformed_htlc<'a>(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<&HTLCSource, ChannelError> {
+ if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
+ return Err(ChannelError::Close("Got fail malformed HTLC message when channel was not in an operational state"));
+ }
+ if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+ return Err(ChannelError::Close("Peer sent update_fail_malformed_htlc when we needed a channel_reestablish"));
+ }
+
+ self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))
+ }
+
+ pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>, ChannelMonitor), HandleError> {
+ if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
+ return Err(HandleError{err: "Got commitment signed message when channel was not in an operational state", action: None});
+ }
+ if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
+ return Err(HandleError{err: "Peer sent commitment_signed when we needed a channel_reestablish", action: Some(msgs::ErrorAction::SendErrorMessage{msg: msgs::ErrorMessage{data: "Peer sent commitment_signed when we needed a channel_reestablish".to_string(), channel_id: msg.channel_id}})});
+ }
+
+ let funding_script = self.get_funding_redeemscript();
+
+ let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
+
+ let feerate_per_kw = if !self.channel_outbound && self.pending_update_fee.is_some() {
+ self.pending_update_fee.unwrap()
+ } else {
+ self.feerate_per_kw
+ };
+
+ let mut local_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, feerate_per_kw);
+ let local_commitment_txid = local_commitment_tx.0.txid();
+ let local_sighash = Message::from_slice(&bip143::SighashComponents::new(&local_commitment_tx.0).sighash_all(&local_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
+ secp_call!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid commitment tx signature from peer", self.channel_id());
+
+ if msg.htlc_signatures.len() != local_commitment_tx.1.len() {
+ return Err(HandleError{err: "Got wrong number of HTLC signatures from remote", action: None});
+ }
+
+ let mut new_local_commitment_txn = Vec::with_capacity(local_commitment_tx.1.len() + 1);
+ self.sign_commitment_transaction(&mut local_commitment_tx.0, &msg.signature);
+ new_local_commitment_txn.push(local_commitment_tx.0.clone());
+
+ let mut htlcs_and_sigs = Vec::with_capacity(local_commitment_tx.1.len());
+ for (idx, ref htlc) in local_commitment_tx.1.iter().enumerate() {
+ let mut htlc_tx = self.build_htlc_transaction(&local_commitment_txid, htlc, true, &local_keys, feerate_per_kw);
+ let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &local_keys);
+ let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
+ secp_call!(self.secp_ctx.verify(&htlc_sighash, &msg.htlc_signatures[idx], &local_keys.b_htlc_key), "Invalid HTLC tx siganture from peer", self.channel_id());
+ let htlc_sig = if htlc.offered {
+ let htlc_sig = self.sign_htlc_transaction(&mut htlc_tx, &msg.htlc_signatures[idx], &None, htlc, &local_keys)?;
+ new_local_commitment_txn.push(htlc_tx);
+ htlc_sig
+ } else {
+ self.create_htlc_tx_signature(&htlc_tx, htlc, &local_keys)?.1
+ };
+ htlcs_and_sigs.push(((*htlc).clone(), msg.htlc_signatures[idx], htlc_sig));
+ }
+
+ let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number - 1));
+ let per_commitment_secret = chan_utils::build_commitment_secret(self.local_keys.commitment_seed, self.cur_local_commitment_transaction_number + 1);
+
+ // Update state now that we've passed all the can-fail calls...
+ let mut need_our_commitment = false;
+ if !self.channel_outbound {
+ if let Some(fee_update) = self.pending_update_fee {
+ self.feerate_per_kw = fee_update;
+ // We later use the presence of pending_update_fee to indicate we should generate a
+ // commitment_signed upon receipt of revoke_and_ack, so we can only set it to None
+ // if we're not awaiting a revoke (ie will send a commitment_signed now).
+ if (self.channel_state & ChannelState::AwaitingRemoteRevoke as u32) == 0 {
+ need_our_commitment = true;
+ self.pending_update_fee = None;
+ }
+ }
+ }
+
+ self.channel_monitor.provide_latest_local_commitment_tx_info(local_commitment_tx.0, local_keys, self.feerate_per_kw, htlcs_and_sigs);
+
+ for htlc in self.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 {
+ htlc.state = InboundHTLCState::AwaitingRemoteRevokeToAnnounce(forward_info);
+ need_our_commitment = true;
+ }
+ }
+ for htlc in self.pending_outbound_htlcs.iter_mut() {
+ if let OutboundHTLCState::RemoteRemoved = htlc.state {
+ htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove;
+ need_our_commitment = true;
+ }
+ }
+
+ self.cur_local_commitment_transaction_number -= 1;
+ self.last_local_commitment_txn = new_local_commitment_txn;
+
+ let (our_commitment_signed, monitor_update) = if need_our_commitment && (self.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 (msg, monitor) = self.send_commitment_no_status_check()?;
+ (Some(msg), monitor)
+ } else { (None, self.channel_monitor.clone()) };
+
+ Ok((msgs::RevokeAndACK {
+ channel_id: self.channel_id,
+ per_commitment_secret: per_commitment_secret,
+ next_per_commitment_point: next_per_commitment_point,
+ }, our_commitment_signed, monitor_update))
+ }
+
+ /// Used to fulfill holding_cell_htlcs when we get a remote ack (or implicitly get it by them
+ /// fulfilling or failing the last pending HTLC)
+ fn free_holding_cell_htlcs(&mut self) -> Result<Option<(msgs::CommitmentUpdate, ChannelMonitor)>, HandleError> {
+ if self.holding_cell_htlc_updates.len() != 0 || self.holding_cell_update_fee.is_some() {
+ let mut htlc_updates = Vec::new();
+ mem::swap(&mut htlc_updates, &mut self.holding_cell_htlc_updates);
+ let mut update_add_htlcs = Vec::with_capacity(htlc_updates.len());
+ let mut update_fulfill_htlcs = Vec::with_capacity(htlc_updates.len());
+ let mut update_fail_htlcs = Vec::with_capacity(htlc_updates.len());
+ let mut err = None;
+ for htlc_update in htlc_updates.drain(..) {
+ // Note that this *can* fail, though it should be due to rather-rare conditions on
+ // fee races with adding too many outputs which push our total payments just over
+ // the limit. In case its less rare than I anticipate, we may want to revisit
+ // handling this case better and maybe fufilling some of the HTLCs while attempting