Fix max fee_satoshis constant to be 21 million, not 2.1 million.
[rust-lightning] / lightning / src / ln / channel.rs
index 5a55e3e79c4a10fb54b0de80a4a7e85f610ccd9a..407c9adbce55d8aa094736e545a2f81c5e1b448e 100644 (file)
@@ -25,12 +25,12 @@ use bitcoin::secp256k1;
 use ln::features::{ChannelFeatures, InitFeatures};
 use ln::msgs;
 use ln::msgs::{DecodeError, OptionalField, DataLossProtect};
-use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER};
 use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT};
-use ln::chan_utils::{CounterpartyCommitmentSecrets, LocalCommitmentTransaction, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys, PreCalculatedTxCreationKeys};
+use ln::chan_utils::{CounterpartyCommitmentSecrets, HolderCommitmentTransaction, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys, PreCalculatedTxCreationKeys};
 use ln::chan_utils;
 use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
-use chain::transaction::OutPoint;
+use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER};
+use chain::transaction::{OutPoint, TransactionData};
 use chain::keysinterface::{ChannelKeys, KeysInterface};
 use util::transaction_utils;
 use util::ser::{Readable, Writeable, Writer};
@@ -262,6 +262,9 @@ enum UpdateStatus {
 // has been completed, and then turn into a Channel to get compiler-time enforcement of things like
 // calling channel_id() before we're set up or things like get_outbound_funding_signed on an
 // inbound channel.
+//
+// Holder designates channel data owned for the benefice of the user client.
+// Counterparty designates channel data owned by the another channel participant entity.
 pub(super) struct Channel<ChanSigner: ChannelKeys> {
        config: ChannelConfig,
 
@@ -367,8 +370,8 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
        // get_holder_selected_channel_reserve_satoshis(channel_value_sats: u64): u64
        counterparty_htlc_minimum_msat: u64,
        holder_htlc_minimum_msat: u64,
-       counterparty_to_self_delay: u16,
-       to_self_delay: u16,
+       counterparty_selected_contest_delay: u16,
+       holder_selected_contest_delay: u16,
        #[cfg(test)]
        pub counterparty_max_accepted_htlcs: u16,
        #[cfg(not(test))]
@@ -463,7 +466,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        where K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
              F::Target: FeeEstimator,
        {
-               let to_self_delay = config.own_channel_config.our_to_self_delay;
+               let holder_selected_contest_delay = config.own_channel_config.our_to_self_delay;
                let chan_keys = keys_provider.get_channel_keys(false, channel_value_satoshis);
 
                if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
@@ -473,8 +476,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                if push_msat > channel_value_msat {
                        return Err(APIError::APIMisuseError { err: format!("Push value ({}) was larger than channel_value ({})", push_msat, channel_value_msat) });
                }
-               if to_self_delay < BREAKDOWN_TIMEOUT {
-                       return Err(APIError::APIMisuseError {err: format!("Configured with an unreasonable our_to_self_delay ({}) putting user funds at risks", to_self_delay)});
+               if holder_selected_contest_delay < BREAKDOWN_TIMEOUT {
+                       return Err(APIError::APIMisuseError {err: format!("Configured with an unreasonable our_to_self_delay ({}) putting user funds at risks", holder_selected_contest_delay)});
                }
                let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
                if Channel::<ChanSigner>::get_holder_selected_channel_reserve_satoshis(channel_value_satoshis) < Channel::<ChanSigner>::derive_holder_dust_limit_satoshis(background_feerate) {
@@ -540,8 +543,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        counterparty_selected_channel_reserve_satoshis: 0,
                        counterparty_htlc_minimum_msat: 0,
                        holder_htlc_minimum_msat: if config.own_channel_config.our_htlc_minimum_msat == 0 { 1 } else { config.own_channel_config.our_htlc_minimum_msat },
-                       counterparty_to_self_delay: 0,
-                       to_self_delay,
+                       counterparty_selected_contest_delay: 0,
+                       holder_selected_contest_delay,
                        counterparty_max_accepted_htlcs: 0,
                        minimum_depth: 0, // Filled in in accept_channel
 
@@ -768,8 +771,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        counterparty_selected_channel_reserve_satoshis: msg.channel_reserve_satoshis,
                        counterparty_htlc_minimum_msat: msg.htlc_minimum_msat,
                        holder_htlc_minimum_msat: if config.own_channel_config.our_htlc_minimum_msat == 0 { 1 } else { config.own_channel_config.our_htlc_minimum_msat },
-                       counterparty_to_self_delay: msg.to_self_delay,
-                       to_self_delay: config.own_channel_config.our_to_self_delay,
+                       counterparty_selected_contest_delay: msg.to_self_delay,
+                       holder_selected_contest_delay: config.own_channel_config.our_to_self_delay,
                        counterparty_max_accepted_htlcs: msg.max_accepted_htlcs,
                        minimum_depth: config.own_channel_config.minimum_depth,
 
@@ -989,8 +992,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        log_trace!(logger, "   ...including {} output with value {}", if local { "to_local" } else { "to_remote" }, value_to_a);
                        txouts.push((TxOut {
                                script_pubkey: chan_utils::get_revokeable_redeemscript(&keys.revocation_key,
-                                                                                      if local { self.counterparty_to_self_delay } else { self.to_self_delay },
-                                                                                      &keys.delayed_payment_key).to_v0_p2wsh(),
+                                                                                      if local { self.counterparty_selected_contest_delay } else { self.holder_selected_contest_delay },
+                                                                                      &keys.broadcaster_delayed_payment_key).to_v0_p2wsh(),
                                value: value_to_a as u64
                        }, None));
                }
@@ -1153,7 +1156,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        /// @local is used only to convert relevant internal structures which refer to remote vs local
        /// to decide value of outputs and direction of HTLCs.
        fn build_htlc_transaction(&self, prev_hash: &Txid, htlc: &HTLCOutputInCommitment, local: bool, keys: &TxCreationKeys, feerate_per_kw: u32) -> Transaction {
-               chan_utils::build_htlc_transaction(prev_hash, feerate_per_kw, if local { self.counterparty_to_self_delay } else { self.to_self_delay }, htlc, &keys.delayed_payment_key, &keys.revocation_key)
+               chan_utils::build_htlc_transaction(prev_hash, feerate_per_kw, if local { self.counterparty_selected_contest_delay } else { self.holder_selected_contest_delay }, htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key)
        }
 
        /// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
@@ -1448,7 +1451,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                self.counterparty_max_htlc_value_in_flight_msat = cmp::min(msg.max_htlc_value_in_flight_msat, self.channel_value_satoshis * 1000);
                self.counterparty_selected_channel_reserve_satoshis = msg.channel_reserve_satoshis;
                self.counterparty_htlc_minimum_msat = msg.htlc_minimum_msat;
-               self.counterparty_to_self_delay = msg.to_self_delay;
+               self.counterparty_selected_contest_delay = msg.to_self_delay;
                self.counterparty_max_accepted_htlcs = msg.max_accepted_htlcs;
                self.minimum_depth = msg.minimum_depth;
 
@@ -1460,7 +1463,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        htlc_basepoint: msg.htlc_basepoint
                };
 
-               self.holder_keys.on_accept(&counterparty_pubkeys, msg.to_self_delay, self.to_self_delay);
+               self.holder_keys.on_accept(&counterparty_pubkeys, msg.to_self_delay, self.holder_selected_contest_delay);
                self.counterparty_pubkeys = Some(counterparty_pubkeys);
 
                self.counterparty_cur_commitment_point = Some(msg.first_per_commitment_point);
@@ -1471,7 +1474,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                Ok(())
        }
 
-       fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(Transaction, LocalCommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
+       fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(Transaction, HolderCommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
                let funding_script = self.get_funding_redeemscript();
 
                let keys = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number)?;
@@ -1482,12 +1485,12 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                log_trace!(logger, "Checking funding_created tx signature {} by key {} against tx {} (sighash {}) with redeemscript {}", log_bytes!(sig.serialize_compact()[..]), log_bytes!(self.counterparty_funding_pubkey().serialize()), encode::serialize_hex(&initial_commitment_tx), log_bytes!(sighash[..]), encode::serialize_hex(&funding_script));
                secp_check!(self.secp_ctx.verify(&sighash, &sig, self.counterparty_funding_pubkey()), "Invalid funding_created signature from peer".to_owned());
 
-               let tx = LocalCommitmentTransaction::new_missing_local_sig(initial_commitment_tx, sig.clone(), &self.holder_keys.pubkeys().funding_pubkey, self.counterparty_funding_pubkey(), keys, self.feerate_per_kw, Vec::new());
+               let tx = HolderCommitmentTransaction::new_missing_holder_sig(initial_commitment_tx, sig.clone(), &self.holder_keys.pubkeys().funding_pubkey, self.counterparty_funding_pubkey(), keys, self.feerate_per_kw, Vec::new());
 
                let counterparty_keys = self.build_remote_transaction_keys()?;
                let counterparty_initial_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, self.feerate_per_kw, logger).0;
                let pre_remote_keys = PreCalculatedTxCreationKeys::new(counterparty_keys);
-               let counterparty_signature = self.holder_keys.sign_remote_commitment(self.feerate_per_kw, &counterparty_initial_commitment_tx, &pre_remote_keys, &Vec::new(), &self.secp_ctx)
+               let counterparty_signature = self.holder_keys.sign_counterparty_commitment(self.feerate_per_kw, &counterparty_initial_commitment_tx, &pre_remote_keys, &Vec::new(), &self.secp_ctx)
                                .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0;
 
                // We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
@@ -1533,14 +1536,14 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                macro_rules! create_monitor {
                        () => { {
                                let mut channel_monitor = ChannelMonitor::new(self.holder_keys.clone(),
-                                                                             &self.shutdown_pubkey, self.to_self_delay,
+                                                                             &self.shutdown_pubkey, self.holder_selected_contest_delay,
                                                                              &self.destination_script, (funding_txo, funding_txo_script.clone()),
                                                                              &counterparty_pubkeys.htlc_basepoint, &counterparty_pubkeys.delayed_payment_basepoint,
-                                                                             self.counterparty_to_self_delay, funding_redeemscript.clone(), self.channel_value_satoshis,
+                                                                             self.counterparty_selected_contest_delay, funding_redeemscript.clone(), self.channel_value_satoshis,
                                                                              self.get_commitment_transaction_number_obscure_factor(),
                                                                              initial_commitment_tx.clone());
 
-                               channel_monitor.provide_latest_remote_commitment_tx_info(&counterparty_initial_commitment_tx, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
+                               channel_monitor.provide_latest_counterparty_commitment_tx_info(&counterparty_initial_commitment_tx, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
                                channel_monitor
                        } }
                }
@@ -1578,8 +1581,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                let counterparty_keys = self.build_remote_transaction_keys()?;
                let counterparty_initial_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, self.feerate_per_kw, logger).0;
 
-               let keys = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number)?;
-               let initial_commitment_tx = self.build_commitment_transaction(self.cur_holder_commitment_transaction_number, &keys, true, false, self.feerate_per_kw, logger).0;
+               let holder_keys = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number)?;
+               let initial_commitment_tx = self.build_commitment_transaction(self.cur_holder_commitment_transaction_number, &holder_keys, true, false, self.feerate_per_kw, logger).0;
                let sighash = hash_to_message!(&bip143::SigHashCache::new(&initial_commitment_tx).signature_hash(0, &funding_script, self.channel_value_satoshis, SigHashType::All)[..]);
 
                let counterparty_funding_pubkey = &self.counterparty_pubkeys.as_ref().unwrap().funding_pubkey;
@@ -1595,16 +1598,16 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
                macro_rules! create_monitor {
                        () => { {
-                               let commitment_tx = LocalCommitmentTransaction::new_missing_local_sig(initial_commitment_tx.clone(), msg.signature.clone(), &self.holder_keys.pubkeys().funding_pubkey, counterparty_funding_pubkey, keys.clone(), self.feerate_per_kw, Vec::new());
+                               let commitment_tx = HolderCommitmentTransaction::new_missing_holder_sig(initial_commitment_tx.clone(), msg.signature.clone(), &self.holder_keys.pubkeys().funding_pubkey, counterparty_funding_pubkey, holder_keys.clone(), self.feerate_per_kw, Vec::new());
                                let mut channel_monitor = ChannelMonitor::new(self.holder_keys.clone(),
-                                                                             &self.shutdown_pubkey, self.to_self_delay,
+                                                                             &self.shutdown_pubkey, self.holder_selected_contest_delay,
                                                                              &self.destination_script, (funding_txo.clone(), funding_txo_script.clone()),
                                                                              &counterparty_pubkeys.htlc_basepoint, &counterparty_pubkeys.delayed_payment_basepoint,
-                                                                             self.counterparty_to_self_delay, funding_redeemscript.clone(), self.channel_value_satoshis,
+                                                                             self.counterparty_selected_contest_delay, funding_redeemscript.clone(), self.channel_value_satoshis,
                                                                              self.get_commitment_transaction_number_obscure_factor(),
                                                                              commitment_tx);
 
-                               channel_monitor.provide_latest_remote_commitment_tx_info(&counterparty_initial_commitment_tx, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
+                               channel_monitor.provide_latest_counterparty_commitment_tx_info(&counterparty_initial_commitment_tx, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
 
                                channel_monitor
                        } }
@@ -2045,8 +2048,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                self.latest_monitor_update_id += 1;
                let mut monitor_update = ChannelMonitorUpdate {
                        update_id: self.latest_monitor_update_id,
-                       updates: vec![ChannelMonitorUpdateStep::LatestLocalCommitmentTXInfo {
-                               commitment_tx: LocalCommitmentTransaction::new_missing_local_sig(commitment_tx.0, msg.signature.clone(), &self.holder_keys.pubkeys().funding_pubkey, &counterparty_funding_pubkey, keys, self.feerate_per_kw, htlcs_without_source),
+                       updates: vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
+                               commitment_tx: HolderCommitmentTransaction::new_missing_holder_sig(commitment_tx.0, msg.signature.clone(), &self.holder_keys.pubkeys().funding_pubkey, &counterparty_funding_pubkey, keys, self.feerate_per_kw, htlcs_without_source),
                                htlc_outputs: htlcs_and_sigs
                        }]
                };
@@ -3004,7 +3007,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                if !self.pending_inbound_htlcs.is_empty() || !self.pending_outbound_htlcs.is_empty() {
                        return Err(ChannelError::Close("Remote end sent us a closing_signed while there were still pending HTLCs".to_owned()));
                }
-               if msg.fee_satoshis > 21000000 * 10000000 { //this is required to stop potential overflow in build_closing_transaction
+               if msg.fee_satoshis > 21_000_000 * 1_0000_0000 { //this is required to stop potential overflow in build_closing_transaction
                        return Err(ChannelError::Close("Remote tried to send us a closing tx with > 21 million BTC fee".to_owned()));
                }
 
@@ -3122,6 +3125,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        }
 
        /// Allowed in any state (including after shutdown)
+       #[cfg(test)]
        pub fn get_holder_htlc_minimum_msat(&self) -> u64 {
                self.holder_htlc_minimum_msat
        }
@@ -3140,7 +3144,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
 
        /// Allowed in any state (including after shutdown)
        pub fn get_counterparty_htlc_minimum_msat(&self) -> u64 {
-               self.holder_htlc_minimum_msat
+               self.counterparty_htlc_minimum_msat
        }
 
        pub fn get_value_satoshis(&self) -> u64 {
@@ -3164,7 +3168,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                self.cur_counterparty_commitment_transaction_number + 1 - if self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32) != 0 { 1 } else { 0 }
        }
 
-       pub fn get_revoked_remote_commitment_transaction_number(&self) -> u64 {
+       pub fn get_revoked_counterparty_commitment_transaction_number(&self) -> u64 {
                self.cur_counterparty_commitment_transaction_number + 2
        }
 
@@ -3311,7 +3315,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        ///
        /// May return some HTLCs (and their payment_hash) which have timed out and should be failed
        /// back.
-       pub fn block_connected(&mut self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[usize]) -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> {
+       pub fn block_connected(&mut self, header: &BlockHeader, txdata: &TransactionData, height: u32) -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> {
                let mut timed_out_htlcs = Vec::new();
                self.holding_cell_htlc_updates.retain(|htlc_update| {
                        match htlc_update {
@@ -3331,7 +3335,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        }
                }
                if non_shutdown_state & !(ChannelState::TheirFundingLocked as u32) == ChannelState::FundingSent as u32 {
-                       for (ref tx, index_in_block) in txn_matched.iter().zip(indexes_of_txn_matched) {
+                       for &(index_in_block, tx) in txdata.iter() {
                                if tx.txid() == self.funding_txo.unwrap().txid {
                                        let txo_idx = self.funding_txo.unwrap().index as usize;
                                        if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.get_funding_redeemscript().to_v0_p2wsh() ||
@@ -3362,14 +3366,14 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                                                }
                                                        }
                                                }
-                                               if height > 0xff_ff_ff || (*index_in_block) > 0xff_ff_ff {
+                                               if height > 0xff_ff_ff || (index_in_block) > 0xff_ff_ff {
                                                        panic!("Block was bogus - either height 16 million or had > 16 million transactions");
                                                }
                                                assert!(txo_idx <= 0xffff); // txo_idx is a (u16 as usize), so this is just listed here for completeness
                                                self.funding_tx_confirmations = 1;
-                                               self.short_channel_id = Some(((height as u64)          << (5*8)) |
-                                                                            ((*index_in_block as u64) << (2*8)) |
-                                                                            ((txo_idx as u64)         << (0*8)));
+                                               self.short_channel_id = Some(((height as u64)         << (5*8)) |
+                                                                            ((index_in_block as u64) << (2*8)) |
+                                                                            ((txo_idx as u64)        << (0*8)));
                                        }
                                }
                        }
@@ -3466,7 +3470,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        channel_reserve_satoshis: Channel::<ChanSigner>::get_holder_selected_channel_reserve_satoshis(self.channel_value_satoshis),
                        htlc_minimum_msat: self.holder_htlc_minimum_msat,
                        feerate_per_kw: self.feerate_per_kw as u32,
-                       to_self_delay: self.to_self_delay,
+                       to_self_delay: self.holder_selected_contest_delay,
                        max_accepted_htlcs: OUR_MAX_HTLCS,
                        funding_pubkey: keys.funding_pubkey,
                        revocation_basepoint: keys.revocation_basepoint,
@@ -3500,7 +3504,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        channel_reserve_satoshis: Channel::<ChanSigner>::get_holder_selected_channel_reserve_satoshis(self.channel_value_satoshis),
                        htlc_minimum_msat: self.holder_htlc_minimum_msat,
                        minimum_depth: self.minimum_depth,
-                       to_self_delay: self.to_self_delay,
+                       to_self_delay: self.holder_selected_contest_delay,
                        max_accepted_htlcs: OUR_MAX_HTLCS,
                        funding_pubkey: keys.funding_pubkey,
                        revocation_basepoint: keys.revocation_basepoint,
@@ -3517,7 +3521,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                let counterparty_keys = self.build_remote_transaction_keys()?;
                let counterparty_initial_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, self.feerate_per_kw, logger).0;
                let pre_remote_keys = PreCalculatedTxCreationKeys::new(counterparty_keys);
-               Ok(self.holder_keys.sign_remote_commitment(self.feerate_per_kw, &counterparty_initial_commitment_tx, &pre_remote_keys, &Vec::new(), &self.secp_ctx)
+               Ok(self.holder_keys.sign_counterparty_commitment(self.feerate_per_kw, &counterparty_initial_commitment_tx, &pre_remote_keys, &Vec::new(), &self.secp_ctx)
                                .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0)
        }
 
@@ -3838,7 +3842,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                self.latest_monitor_update_id += 1;
                let monitor_update = ChannelMonitorUpdate {
                        update_id: self.latest_monitor_update_id,
-                       updates: vec![ChannelMonitorUpdateStep::LatestRemoteCommitmentTXInfo {
+                       updates: vec![ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo {
                                unsigned_commitment_tx: counterparty_commitment_tx.clone(),
                                htlc_outputs: htlcs.clone(),
                                commitment_number: self.cur_counterparty_commitment_transaction_number,
@@ -3870,7 +3874,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        }
 
                        let pre_remote_keys = PreCalculatedTxCreationKeys::new(counterparty_keys);
-                       let res = self.holder_keys.sign_remote_commitment(feerate_per_kw, &counterparty_commitment_tx.0, &pre_remote_keys, &htlcs, &self.secp_ctx)
+                       let res = self.holder_keys.sign_counterparty_commitment(feerate_per_kw, &counterparty_commitment_tx.0, &pre_remote_keys, &htlcs, &self.secp_ctx)
                                .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?;
                        signature = res.0;
                        htlc_signatures = res.1;
@@ -3883,7 +3887,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
 
                        for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
                                log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {}",
-                                       encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_tx.0.txid(), feerate_per_kw, self.to_self_delay, htlc, &counterparty_keys.delayed_payment_key, &counterparty_keys.revocation_key)),
+                                       encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_tx.0.txid(), feerate_per_kw, self.holder_selected_contest_delay, htlc, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
                                        encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, counterparty_keys)),
                                        log_bytes!(counterparty_keys.broadcaster_htlc_key.serialize()),
                                        log_bytes!(htlc_sig.serialize_compact()[..]));
@@ -4201,8 +4205,8 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
                self.counterparty_selected_channel_reserve_satoshis.write(writer)?;
                self.counterparty_htlc_minimum_msat.write(writer)?;
                self.holder_htlc_minimum_msat.write(writer)?;
-               self.counterparty_to_self_delay.write(writer)?;
-               self.to_self_delay.write(writer)?;
+               self.counterparty_selected_contest_delay.write(writer)?;
+               self.holder_selected_contest_delay.write(writer)?;
                self.counterparty_max_accepted_htlcs.write(writer)?;
                self.minimum_depth.write(writer)?;
 
@@ -4355,8 +4359,8 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for Channel<ChanSigner> {
                let counterparty_selected_channel_reserve_satoshis = Readable::read(reader)?;
                let counterparty_htlc_minimum_msat = Readable::read(reader)?;
                let holder_htlc_minimum_msat = Readable::read(reader)?;
-               let counterparty_to_self_delay = Readable::read(reader)?;
-               let to_self_delay = Readable::read(reader)?;
+               let counterparty_selected_contest_delay = Readable::read(reader)?;
+               let holder_selected_contest_delay = Readable::read(reader)?;
                let counterparty_max_accepted_htlcs = Readable::read(reader)?;
                let minimum_depth = Readable::read(reader)?;
 
@@ -4427,8 +4431,8 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for Channel<ChanSigner> {
                        counterparty_selected_channel_reserve_satoshis,
                        counterparty_htlc_minimum_msat,
                        holder_htlc_minimum_msat,
-                       counterparty_to_self_delay,
-                       to_self_delay,
+                       counterparty_selected_contest_delay,
+                       holder_selected_contest_delay,
                        counterparty_max_accepted_htlcs,
                        minimum_depth,
 
@@ -4464,7 +4468,7 @@ mod tests {
        use ln::features::InitFeatures;
        use ln::msgs::{OptionalField, DataLossProtect};
        use ln::chan_utils;
-       use ln::chan_utils::{LocalCommitmentTransaction, ChannelPublicKeys};
+       use ln::chan_utils::{HolderCommitmentTransaction, ChannelPublicKeys};
        use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
        use chain::keysinterface::{InMemoryChannelKeys, KeysInterface};
        use chain::transaction::OutPoint;
@@ -4639,7 +4643,7 @@ mod tests {
                let mut config = UserConfig::default();
                config.channel_options.announced_channel = false;
                let mut chan = Channel::<InMemoryChannelKeys>::new_outbound(&&feeest, &&keys_provider, counterparty_node_id, 10_000_000, 100000, 42, &config).unwrap(); // Nothing uses their network key in this test
-               chan.counterparty_to_self_delay = 144;
+               chan.counterparty_selected_contest_delay = 144;
                chan.holder_dust_limit_satoshis = 546;
 
                let funding_info = OutPoint{ txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(), index: 0 };
@@ -4652,7 +4656,7 @@ mod tests {
                        delayed_payment_basepoint: public_from_secret_hex(&secp_ctx, "1552dfba4f6cf29a62a0af13c8d6981d36d0ef8d61ba10fb0fe90da7634d7e13"),
                        htlc_basepoint: public_from_secret_hex(&secp_ctx, "4444444444444444444444444444444444444444444444444444444444444444")
                };
-               chan_keys.on_accept(&counterparty_pubkeys, chan.counterparty_to_self_delay, chan.to_self_delay);
+               chan_keys.on_accept(&counterparty_pubkeys, chan.counterparty_selected_contest_delay, chan.holder_selected_contest_delay);
 
                assert_eq!(counterparty_pubkeys.payment_point.serialize()[..],
                           hex::decode("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]);
@@ -4676,7 +4680,7 @@ mod tests {
 
                let mut unsigned_tx: (Transaction, Vec<HTLCOutputInCommitment>);
 
-               let mut localtx;
+               let mut holdertx;
                macro_rules! test_commitment {
                        ( $counterparty_sig_hex: expr, $sig_hex: expr, $tx_hex: expr, {
                                $( { $htlc_idx: expr, $counterparty_htlc_sig_hex: expr, $htlc_sig_hex: expr, $htlc_tx_hex: expr } ), *
@@ -4701,15 +4705,15 @@ mod tests {
                                })*
                                assert_eq!(unsigned_tx.1.len(), per_htlc.len());
 
-                               localtx = LocalCommitmentTransaction::new_missing_local_sig(unsigned_tx.0.clone(), counterparty_signature.clone(), &chan_keys.pubkeys().funding_pubkey, chan.counterparty_funding_pubkey(), keys.clone(), chan.feerate_per_kw, per_htlc);
-                               let local_sig = chan_keys.sign_local_commitment(&localtx, &chan.secp_ctx).unwrap();
-                               assert_eq!(Signature::from_der(&hex::decode($sig_hex).unwrap()[..]).unwrap(), local_sig);
+                               holdertx = HolderCommitmentTransaction::new_missing_holder_sig(unsigned_tx.0.clone(), counterparty_signature.clone(), &chan_keys.pubkeys().funding_pubkey, chan.counterparty_funding_pubkey(), keys.clone(), chan.feerate_per_kw, per_htlc);
+                               let holder_sig = chan_keys.sign_holder_commitment(&holdertx, &chan.secp_ctx).unwrap();
+                               assert_eq!(Signature::from_der(&hex::decode($sig_hex).unwrap()[..]).unwrap(), holder_sig);
 
-                               assert_eq!(serialize(&localtx.add_local_sig(&redeemscript, local_sig))[..],
+                               assert_eq!(serialize(&holdertx.add_holder_sig(&redeemscript, holder_sig))[..],
                                                hex::decode($tx_hex).unwrap()[..]);
 
-                               let htlc_sigs = chan_keys.sign_local_commitment_htlc_transactions(&localtx, &chan.secp_ctx).unwrap();
-                               let mut htlc_sig_iter = localtx.per_htlc.iter().zip(htlc_sigs.iter().enumerate());
+                               let htlc_sigs = chan_keys.sign_holder_commitment_htlc_transactions(&holdertx, &chan.secp_ctx).unwrap();
+                               let mut htlc_sig_iter = holdertx.per_htlc.iter().zip(htlc_sigs.iter().enumerate());
 
                                $({
                                        let remote_signature = Signature::from_der(&hex::decode($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
@@ -4738,7 +4742,7 @@ mod tests {
 
                                        let signature = Signature::from_der(&hex::decode($htlc_sig_hex).unwrap()[..]).unwrap();
                                        assert_eq!(Some(signature), *(htlc_sig.1).1);
-                                       assert_eq!(serialize(&localtx.get_signed_htlc_tx((htlc_sig.1).0, &(htlc_sig.1).1.unwrap(), &preimage, chan.counterparty_to_self_delay))[..],
+                                       assert_eq!(serialize(&holdertx.get_signed_htlc_tx((htlc_sig.1).0, &(htlc_sig.1).1.unwrap(), &preimage, chan.counterparty_selected_contest_delay))[..],
                                                        hex::decode($htlc_tx_hex).unwrap()[..]);
                                })*
                                loop {