Update to latest upstream rust-bitcoin
[rust-lightning] / lightning / src / ln / channel.rs
index a0879d4e7a554895a5636d57a4e6485bcc1bb086..ff7c60ad1f91b020fd2bba7b06e15653024fee2b 100644 (file)
@@ -1,8 +1,16 @@
+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
+// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
 use bitcoin::blockdata::block::BlockHeader;
 use bitcoin::blockdata::script::{Script,Builder};
 use bitcoin::blockdata::transaction::{TxIn, TxOut, Transaction, SigHashType};
 use bitcoin::blockdata::opcodes;
-use bitcoin::util::hash::BitcoinHash;
 use bitcoin::util::bip143;
 use bitcoin::consensus::encode;
 
@@ -19,7 +27,7 @@ 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};
+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;
 use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
 use chain::transaction::OutPoint;
@@ -377,9 +385,6 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
 
        their_shutdown_scriptpubkey: Option<Script>,
 
-       /// Used exclusively to broadcast the latest local state, mostly a historical quirk that this
-       /// is here:
-       channel_monitor: Option<ChannelMonitor<ChanSigner>>,
        commitment_secrets: CounterpartyCommitmentSecrets,
 
        network_sync: UpdateStatus,
@@ -458,6 +463,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        where K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
              F::Target: FeeEstimator,
        {
+               let our_to_self_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 {
@@ -467,8 +473,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 config.own_channel_config.our_to_self_delay < BREAKDOWN_TIMEOUT {
-                       return Err(APIError::APIMisuseError {err: format!("Configured with an unreasonable our_to_self_delay ({}) putting user funds at risks", config.own_channel_config.our_to_self_delay)});
+               if our_to_self_delay < BREAKDOWN_TIMEOUT {
+                       return Err(APIError::APIMisuseError {err: format!("Configured with an unreasonable our_to_self_delay ({}) putting user funds at risks", our_to_self_delay)});
                }
                let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
                if Channel::<ChanSigner>::get_remote_channel_reserve_satoshis(channel_value_satoshis) < Channel::<ChanSigner>::derive_our_dust_limit_satoshis(background_feerate) {
@@ -481,7 +487,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        user_id: user_id,
                        config: config.channel_options.clone(),
 
-                       channel_id: keys_provider.get_channel_id(),
+                       channel_id: keys_provider.get_secure_random_bytes(),
                        channel_state: ChannelState::OurInitSent as u32,
                        channel_outbound: true,
                        secp_ctx: Secp256k1::new(),
@@ -535,7 +541,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        their_htlc_minimum_msat: 0,
                        our_htlc_minimum_msat: if config.own_channel_config.our_htlc_minimum_msat == 0 { 1 } else { config.own_channel_config.our_htlc_minimum_msat },
                        their_to_self_delay: 0,
-                       our_to_self_delay: config.own_channel_config.our_to_self_delay,
+                       our_to_self_delay,
                        their_max_accepted_htlcs: 0,
                        minimum_depth: 0, // Filled in in accept_channel
 
@@ -547,7 +553,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
 
                        their_shutdown_scriptpubkey: None,
 
-                       channel_monitor: None,
                        commitment_secrets: CounterpartyCommitmentSecrets::new(),
 
                        network_sync: UpdateStatus::Fresh,
@@ -582,7 +587,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        delayed_payment_basepoint: msg.delayed_payment_basepoint,
                        htlc_basepoint: msg.htlc_basepoint
                };
-               chan_keys.set_remote_channel_pubkeys(&their_pubkeys);
+               chan_keys.on_accept(&their_pubkeys, msg.to_self_delay, config.own_channel_config.our_to_self_delay);
                let mut local_config = (*config).channel_options.clone();
 
                if config.own_channel_config.our_to_self_delay < BREAKDOWN_TIMEOUT {
@@ -776,7 +781,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
 
                        their_shutdown_scriptpubkey,
 
-                       channel_monitor: None,
                        commitment_secrets: CounterpartyCommitmentSecrets::new(),
 
                        network_sync: UpdateStatus::Fresh,
@@ -785,13 +789,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                Ok(chan)
        }
 
-       // Utilities to derive keys:
-
-       fn build_local_commitment_secret(&self, idx: u64) -> SecretKey {
-               let res = self.local_keys.commitment_secret(idx);
-               SecretKey::from_slice(&res).unwrap()
-       }
-
        // Utilities to build transactions:
 
        fn get_commitment_transaction_number_obscure_factor(&self) -> u64 {
@@ -1123,12 +1120,12 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        /// The result is a transaction which we can revoke ownership of (ie a "local" transaction)
        /// TODO Some magic rust shit to compile-time check this?
        fn build_local_transaction_keys(&self, commitment_number: u64) -> Result<TxCreationKeys, ChannelError> {
-               let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(commitment_number));
+               let per_commitment_point = self.local_keys.get_per_commitment_point(commitment_number, &self.secp_ctx);
                let delayed_payment_base = &self.local_keys.pubkeys().delayed_payment_basepoint;
                let htlc_basepoint = &self.local_keys.pubkeys().htlc_basepoint;
                let their_pubkeys = self.their_pubkeys.as_ref().unwrap();
 
-               Ok(secp_check!(TxCreationKeys::new(&self.secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &their_pubkeys.revocation_basepoint, &their_pubkeys.htlc_basepoint), "Local tx keys generation got bogus keys".to_owned()))
+               Ok(secp_check!(TxCreationKeys::derive_new(&self.secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &their_pubkeys.revocation_basepoint, &their_pubkeys.htlc_basepoint), "Local tx keys generation got bogus keys".to_owned()))
        }
 
        #[inline]
@@ -1142,7 +1139,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                let htlc_basepoint = &self.local_keys.pubkeys().htlc_basepoint;
                let their_pubkeys = self.their_pubkeys.as_ref().unwrap();
 
-               Ok(secp_check!(TxCreationKeys::new(&self.secp_ctx, &self.their_cur_commitment_point.unwrap(), &their_pubkeys.delayed_payment_basepoint, &their_pubkeys.htlc_basepoint, revocation_basepoint, htlc_basepoint), "Remote tx keys generation got bogus keys".to_owned()))
+               Ok(secp_check!(TxCreationKeys::derive_new(&self.secp_ctx, &self.their_cur_commitment_point.unwrap(), &their_pubkeys.delayed_payment_basepoint, &their_pubkeys.htlc_basepoint, revocation_basepoint, htlc_basepoint), "Remote tx keys generation got bogus keys".to_owned()))
        }
 
        /// Gets the redeemscript for the funding transaction output (ie the funding transaction output
@@ -1219,7 +1216,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                payment_preimage: payment_preimage_arg.clone(),
                        }],
                };
-               self.channel_monitor.as_mut().unwrap().update_monitor_ooo(monitor_update.clone(), logger).unwrap();
 
                if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32)) != 0 {
                        for pending_update in self.holding_cell_htlc_updates.iter() {
@@ -1464,7 +1460,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        htlc_basepoint: msg.htlc_basepoint
                };
 
-               self.local_keys.set_remote_channel_pubkeys(&their_pubkeys);
+               self.local_keys.on_accept(&their_pubkeys, msg.to_self_delay, self.our_to_self_delay);
                self.their_pubkeys = Some(their_pubkeys);
 
                self.their_cur_commitment_point = Some(msg.first_per_commitment_point);
@@ -1490,7 +1486,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
 
                let remote_keys = self.build_remote_transaction_keys()?;
                let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false, self.feerate_per_kw, logger).0;
-               let remote_signature = self.local_keys.sign_remote_commitment(self.feerate_per_kw, &remote_initial_commitment_tx, &remote_keys, &Vec::new(), self.our_to_self_delay, &self.secp_ctx)
+               let pre_remote_keys = PreCalculatedTxCreationKeys::new(remote_keys);
+               let remote_signature = self.local_keys.sign_remote_commitment(self.feerate_per_kw, &remote_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 the "remote" commitment transaction, allowing them to broadcast the tx if they wish.
@@ -1548,7 +1545,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        } }
                }
 
-               self.channel_monitor = Some(create_monitor!());
                let channel_monitor = create_monitor!();
 
                self.channel_state = ChannelState::FundingSent as u32;
@@ -1614,7 +1610,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        } }
                }
 
-               self.channel_monitor = Some(create_monitor!());
                let channel_monitor = create_monitor!();
 
                assert_eq!(self.channel_state & (ChannelState::MonitorUpdateFailed as u32), 0); // We have no had any monitor(s) yet to fail update!
@@ -1692,8 +1687,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        /// corner case properly.
        pub fn get_inbound_outbound_available_balance_msat(&self) -> (u64, u64) {
                // Note that we have to handle overflow due to the above case.
-               (cmp::min(self.channel_value_satoshis as i64 * 1000 - self.value_to_self_msat as i64 - self.get_inbound_pending_htlc_stats().1 as i64, 0) as u64,
-               cmp::min(self.value_to_self_msat as i64 - self.get_outbound_pending_htlc_stats().1 as i64, 0) as u64)
+               (cmp::max(self.channel_value_satoshis as i64 * 1000 - self.value_to_self_msat as i64 - self.get_inbound_pending_htlc_stats().1 as i64, 0) as u64,
+               cmp::max(self.value_to_self_msat as i64 - self.get_outbound_pending_htlc_stats().1 as i64, 0) as u64)
        }
 
        // Get the fee cost of a commitment tx with a given number of HTLC outputs.
@@ -2028,8 +2023,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        }
                }
 
-               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 = self.local_keys.commitment_secret(self.cur_local_commitment_transaction_number + 1);
+               let next_per_commitment_point = self.local_keys.get_per_commitment_point(self.cur_local_commitment_transaction_number - 1, &self.secp_ctx);
+               let per_commitment_secret = self.local_keys.release_commitment_secret(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;
@@ -2056,7 +2051,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                htlc_outputs: htlcs_and_sigs
                        }]
                };
-               self.channel_monitor.as_mut().unwrap().update_monitor_ooo(monitor_update.clone(), logger).unwrap();
 
                for htlc in self.pending_inbound_htlcs.iter_mut() {
                        let new_forward = if let &InboundHTLCState::RemoteAnnounced(ref forward_info) = &htlc.state {
@@ -2124,7 +2118,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
 
        /// 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<L: Deref>(&mut self, logger: &L) -> Result<Option<(msgs::CommitmentUpdate, ChannelMonitorUpdate)>, ChannelError> where L::Target: Logger {
+       fn free_holding_cell_htlcs<L: Deref>(&mut self, logger: &L) -> Result<(Option<(msgs::CommitmentUpdate, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash)>), ChannelError> where L::Target: Logger {
                assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, 0);
                if self.holding_cell_htlc_updates.len() != 0 || self.holding_cell_update_fee.is_some() {
                        log_trace!(logger, "Freeing holding cell with {} HTLC updates{}", self.holding_cell_htlc_updates.len(), if self.holding_cell_update_fee.is_some() { " and a fee update" } else { "" });
@@ -2139,110 +2133,94 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        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;
+                       let mut htlcs_to_fail = Vec::new();
                        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 it's less rare than I anticipate, we may want to revisit
                                // handling this case better and maybe fulfilling some of the HTLCs while attempting
                                // to rebalance channels.
-                               if err.is_some() { // We're back to AwaitingRemoteRevoke (or are about to fail the channel)
-                                       self.holding_cell_htlc_updates.push(htlc_update);
-                               } else {
-                                       match &htlc_update {
-                                               &HTLCUpdateAwaitingACK::AddHTLC {amount_msat, cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet, ..} => {
-                                                       match self.send_htlc(amount_msat, *payment_hash, cltv_expiry, source.clone(), onion_routing_packet.clone()) {
-                                                               Ok(update_add_msg_option) => update_add_htlcs.push(update_add_msg_option.unwrap()),
-                                                               Err(e) => {
-                                                                       match e {
-                                                                               ChannelError::Ignore(ref msg) => {
-                                                                                       log_info!(logger, "Failed to send HTLC with payment_hash {} due to {}", log_bytes!(payment_hash.0), msg);
-                                                                               },
-                                                                               _ => {
-                                                                                       log_info!(logger, "Failed to send HTLC with payment_hash {} resulting in a channel closure during holding_cell freeing", log_bytes!(payment_hash.0));
-                                                                               },
-                                                                       }
-                                                                       err = Some(e);
+                               match &htlc_update {
+                                       &HTLCUpdateAwaitingACK::AddHTLC {amount_msat, cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet, ..} => {
+                                               match self.send_htlc(amount_msat, *payment_hash, cltv_expiry, source.clone(), onion_routing_packet.clone()) {
+                                                       Ok(update_add_msg_option) => update_add_htlcs.push(update_add_msg_option.unwrap()),
+                                                       Err(e) => {
+                                                               match e {
+                                                                       ChannelError::Ignore(ref msg) => {
+                                                                               log_info!(logger, "Failed to send HTLC with payment_hash {} due to {}", log_bytes!(payment_hash.0), msg);
+                                                                               // If we fail to send here, then this HTLC should
+                                                                               // be failed backwards. Failing to send here
+                                                                               // indicates that this HTLC may keep being put back
+                                                                               // into the holding cell without ever being
+                                                                               // successfully forwarded/failed/fulfilled, causing
+                                                                               // our counterparty to eventually close on us.
+                                                                               htlcs_to_fail.push((source.clone(), *payment_hash));
+                                                                       },
+                                                                       _ => {
+                                                                               panic!("Got a non-IgnoreError action trying to send holding cell HTLC");
+                                                                       },
                                                                }
                                                        }
-                                               },
-                                               &HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, htlc_id, .. } => {
-                                                       match self.get_update_fulfill_htlc(htlc_id, *payment_preimage, logger) {
-                                                               Ok((update_fulfill_msg_option, additional_monitor_update_opt)) => {
-                                                                       update_fulfill_htlcs.push(update_fulfill_msg_option.unwrap());
-                                                                       if let Some(mut additional_monitor_update) = additional_monitor_update_opt {
-                                                                               monitor_update.updates.append(&mut additional_monitor_update.updates);
-                                                                       }
-                                                               },
-                                                               Err(e) => {
-                                                                       if let ChannelError::Ignore(_) = e {}
-                                                                       else {
-                                                                               panic!("Got a non-IgnoreError action trying to fulfill holding cell HTLC");
-                                                                       }
+                                               }
+                                       },
+                                       &HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, htlc_id, .. } => {
+                                               match self.get_update_fulfill_htlc(htlc_id, *payment_preimage, logger) {
+                                                       Ok((update_fulfill_msg_option, additional_monitor_update_opt)) => {
+                                                               update_fulfill_htlcs.push(update_fulfill_msg_option.unwrap());
+                                                               if let Some(mut additional_monitor_update) = additional_monitor_update_opt {
+                                                                       monitor_update.updates.append(&mut additional_monitor_update.updates);
+                                                               }
+                                                       },
+                                                       Err(e) => {
+                                                               if let ChannelError::Ignore(_) = e {}
+                                                               else {
+                                                                       panic!("Got a non-IgnoreError action trying to fulfill holding cell HTLC");
                                                                }
                                                        }
-                                               },
-                                               &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, ref err_packet } => {
-                                                       match self.get_update_fail_htlc(htlc_id, err_packet.clone()) {
-                                                               Ok(update_fail_msg_option) => update_fail_htlcs.push(update_fail_msg_option.unwrap()),
-                                                               Err(e) => {
-                                                                       if let ChannelError::Ignore(_) = e {}
-                                                                       else {
-                                                                               panic!("Got a non-IgnoreError action trying to fail holding cell HTLC");
-                                                                       }
+                                               }
+                                       },
+                                       &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, ref err_packet } => {
+                                               match self.get_update_fail_htlc(htlc_id, err_packet.clone()) {
+                                                       Ok(update_fail_msg_option) => update_fail_htlcs.push(update_fail_msg_option.unwrap()),
+                                                       Err(e) => {
+                                                               if let ChannelError::Ignore(_) = e {}
+                                                               else {
+                                                                       panic!("Got a non-IgnoreError action trying to fail holding cell HTLC");
                                                                }
                                                        }
-                                               },
-                                       }
-                                       if err.is_some() {
-                                               self.holding_cell_htlc_updates.push(htlc_update);
-                                               if let Some(ChannelError::Ignore(_)) = err {
-                                                       // If we failed to add the HTLC, but got an Ignore error, we should
-                                                       // still send the new commitment_signed, so reset the err to None.
-                                                       err = None;
                                                }
-                                       }
+                                       },
                                }
                        }
-                       //TODO: Need to examine the type of err - if it's a fee issue or similar we may want to
-                       //fail it back the route, if it's a temporary issue we can ignore it...
-                       match err {
-                               None => {
-                                       if update_add_htlcs.is_empty() && update_fulfill_htlcs.is_empty() && update_fail_htlcs.is_empty() && self.holding_cell_update_fee.is_none() {
-                                               // This should never actually happen and indicates we got some Errs back
-                                               // from update_fulfill_htlc/update_fail_htlc, but we handle it anyway in
-                                               // case there is some strange way to hit duplicate HTLC removes.
-                                               return Ok(None);
-                                       }
-                                       let update_fee = if let Some(feerate) = self.holding_cell_update_fee {
-                                                       self.pending_update_fee = self.holding_cell_update_fee.take();
-                                                       Some(msgs::UpdateFee {
-                                                               channel_id: self.channel_id,
-                                                               feerate_per_kw: feerate as u32,
-                                                       })
-                                               } else {
-                                                       None
-                                               };
+                       if update_add_htlcs.is_empty() && update_fulfill_htlcs.is_empty() && update_fail_htlcs.is_empty() && self.holding_cell_update_fee.is_none() {
+                               return Ok((None, htlcs_to_fail));
+                       }
+                       let update_fee = if let Some(feerate) = self.holding_cell_update_fee {
+                               self.pending_update_fee = self.holding_cell_update_fee.take();
+                               Some(msgs::UpdateFee {
+                                       channel_id: self.channel_id,
+                                       feerate_per_kw: feerate as u32,
+                               })
+                       } else {
+                               None
+                       };
 
-                                       let (commitment_signed, mut additional_update) = self.send_commitment_no_status_check(logger)?;
-                                       // send_commitment_no_status_check and get_update_fulfill_htlc may bump latest_monitor_id
-                                       // but we want them to be strictly increasing by one, so reset it here.
-                                       self.latest_monitor_update_id = monitor_update.update_id;
-                                       monitor_update.updates.append(&mut additional_update.updates);
+                       let (commitment_signed, mut additional_update) = self.send_commitment_no_status_check(logger)?;
+                       // send_commitment_no_status_check and get_update_fulfill_htlc may bump latest_monitor_id
+                       // but we want them to be strictly increasing by one, so reset it here.
+                       self.latest_monitor_update_id = monitor_update.update_id;
+                       monitor_update.updates.append(&mut additional_update.updates);
 
-                                       Ok(Some((msgs::CommitmentUpdate {
-                                               update_add_htlcs,
-                                               update_fulfill_htlcs,
-                                               update_fail_htlcs,
-                                               update_fail_malformed_htlcs: Vec::new(),
-                                               update_fee: update_fee,
-                                               commitment_signed,
-                                       }, monitor_update)))
-                               },
-                               Some(e) => Err(e)
-                       }
+                       Ok((Some((msgs::CommitmentUpdate {
+                               update_add_htlcs,
+                               update_fulfill_htlcs,
+                               update_fail_htlcs,
+                               update_fail_malformed_htlcs: Vec::new(),
+                               update_fee: update_fee,
+                               commitment_signed,
+                       }, monitor_update)), htlcs_to_fail))
                } else {
-                       Ok(None)
+                       Ok((None, Vec::new()))
                }
        }
 
@@ -2251,7 +2229,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        /// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
        /// generating an appropriate error *after* the channel state has been updated based on the
        /// revoke_and_ack message.
-       pub fn revoke_and_ack<F: Deref, L: Deref>(&mut self, msg: &msgs::RevokeAndACK, fee_estimator: &F, logger: &L) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, Option<msgs::ClosingSigned>, ChannelMonitorUpdate), ChannelError>
+       pub fn revoke_and_ack<F: Deref, L: Deref>(&mut self, msg: &msgs::RevokeAndACK, fee_estimator: &F, logger: &L) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, Option<msgs::ClosingSigned>, ChannelMonitorUpdate, Vec<(HTLCSource, PaymentHash)>), ChannelError>
                where F::Target: FeeEstimator,
                                        L::Target: Logger,
        {
@@ -2292,7 +2270,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                secret: msg.per_commitment_secret,
                        }],
                };
-               self.channel_monitor.as_mut().unwrap().update_monitor_ooo(monitor_update.clone(), logger).unwrap();
 
                // Update state now that we've passed all the can-fail calls...
                // (note that we may still fail to generate the new commitment_signed message, but that's
@@ -2426,11 +2403,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        }
                        self.monitor_pending_forwards.append(&mut to_forward_infos);
                        self.monitor_pending_failures.append(&mut revoked_htlcs);
-                       return Ok((None, Vec::new(), Vec::new(), None, monitor_update))
+                       return Ok((None, Vec::new(), Vec::new(), None, monitor_update, Vec::new()))
                }
 
                match self.free_holding_cell_htlcs(logger)? {
-                       Some((mut commitment_update, mut additional_update)) => {
+                       (Some((mut commitment_update, mut additional_update)), htlcs_to_fail) => {
                                commitment_update.update_fail_htlcs.reserve(update_fail_htlcs.len());
                                for fail_msg in update_fail_htlcs.drain(..) {
                                        commitment_update.update_fail_htlcs.push(fail_msg);
@@ -2445,9 +2422,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                self.latest_monitor_update_id = monitor_update.update_id;
                                monitor_update.updates.append(&mut additional_update.updates);
 
-                               Ok((Some(commitment_update), to_forward_infos, revoked_htlcs, None, monitor_update))
+                               Ok((Some(commitment_update), to_forward_infos, revoked_htlcs, None, monitor_update, htlcs_to_fail))
                        },
-                       None => {
+                       (None, htlcs_to_fail) => {
                                if require_commitment {
                                        let (commitment_signed, mut additional_update) = self.send_commitment_no_status_check(logger)?;
 
@@ -2463,9 +2440,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                                update_fail_malformed_htlcs,
                                                update_fee: None,
                                                commitment_signed
-                                       }), to_forward_infos, revoked_htlcs, None, monitor_update))
+                                       }), to_forward_infos, revoked_htlcs, None, monitor_update, htlcs_to_fail))
                                } else {
-                                       Ok((None, to_forward_infos, revoked_htlcs, self.maybe_propose_first_closing_signed(fee_estimator), monitor_update))
+                                       Ok((None, to_forward_infos, revoked_htlcs, self.maybe_propose_first_closing_signed(fee_estimator), monitor_update, htlcs_to_fail))
                                }
                        }
                }
@@ -2567,6 +2544,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
 
                self.holding_cell_htlc_updates.retain(|htlc_update| {
                        match htlc_update {
+                               // Note that currently on channel reestablish we assert that there are
+                               // no holding cell HTLC update_adds, so if in the future we stop
+                               // dropping added HTLCs here and failing them backwards, then there will
+                               // need to be corresponding changes made in the Channel's re-establish
+                               // logic.
                                &HTLCUpdateAwaitingACK::AddHTLC { ref payment_hash, ref source, .. } => {
                                        outbound_drops.push((source.clone(), payment_hash.clone()));
                                        false
@@ -2614,8 +2596,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                let funding_locked = if self.monitor_pending_funding_locked {
                        assert!(!self.channel_outbound, "Funding transaction broadcast without FundingBroadcastSafe!");
                        self.monitor_pending_funding_locked = false;
-                       let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
-                       let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
+                       let next_per_commitment_point = self.local_keys.get_per_commitment_point(self.cur_local_commitment_transaction_number, &self.secp_ctx);
                        Some(msgs::FundingLocked {
                                channel_id: self.channel_id(),
                                next_per_commitment_point: next_per_commitment_point,
@@ -2667,8 +2648,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        }
 
        fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
-               let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number));
-               let per_commitment_secret = self.local_keys.commitment_secret(self.cur_local_commitment_transaction_number + 2);
+               let next_per_commitment_point = self.local_keys.get_per_commitment_point(self.cur_local_commitment_transaction_number, &self.secp_ctx);
+               let per_commitment_secret = self.local_keys.release_commitment_secret(self.cur_local_commitment_transaction_number + 2);
                msgs::RevokeAndACK {
                        channel_id: self.channel_id,
                        per_commitment_secret,
@@ -2751,7 +2732,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                if msg.next_remote_commitment_number > 0 {
                        match msg.data_loss_protect {
                                OptionalField::Present(ref data_loss) => {
-                                       if self.local_keys.commitment_secret(INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1) != data_loss.your_last_per_commitment_secret {
+                                       let expected_point = self.local_keys.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1, &self.secp_ctx);
+                                       let given_secret = SecretKey::from_slice(&data_loss.your_last_per_commitment_secret)
+                                               .map_err(|_| ChannelError::Close("Peer sent a garbage channel_reestablish with unparseable secret key".to_owned()))?;
+                                       if expected_point != PublicKey::from_secret_key(&self.secp_ctx, &given_secret) {
                                                return Err(ChannelError::Close("Peer sent a garbage channel_reestablish with secret key not matching the commitment height provided".to_owned()));
                                        }
                                        if msg.next_remote_commitment_number > INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number {
@@ -2787,8 +2771,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        }
 
                        // We have OurFundingLocked set!
-                       let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
-                       let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
+                       let next_per_commitment_point = self.local_keys.get_per_commitment_point(self.cur_local_commitment_transaction_number, &self.secp_ctx);
                        return Ok((Some(msgs::FundingLocked {
                                channel_id: self.channel_id(),
                                next_per_commitment_point: next_per_commitment_point,
@@ -2818,8 +2801,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
 
                let resend_funding_locked = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number == 1 {
                        // We should never have to worry about MonitorUpdateFailed resending FundingLocked
-                       let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
-                       let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
+                       let next_per_commitment_point = self.local_keys.get_per_commitment_point(self.cur_local_commitment_transaction_number, &self.secp_ctx);
                        Some(msgs::FundingLocked {
                                channel_id: self.channel_id(),
                                next_per_commitment_point: next_per_commitment_point,
@@ -2834,6 +2816,14 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        }
 
                        if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::MonitorUpdateFailed as u32)) == 0 {
+                               // Note that if in the future we no longer drop holding cell update_adds on peer
+                               // disconnect, this logic will need to be updated.
+                               for htlc_update in self.holding_cell_htlc_updates.iter() {
+                                       if let &HTLCUpdateAwaitingACK::AddHTLC { .. } = htlc_update {
+                                               debug_assert!(false, "There shouldn't be any add-HTLCs in the holding cell now because they should have been dropped on peer disconnect. Panic here because said HTLCs won't be handled correctly.");
+                                       }
+                               }
+
                                // We're up-to-date and not waiting on a remote revoke (if we are our
                                // channel_reestablish should result in them sending a revoke_and_ack), but we may
                                // have received some updates while we were disconnected. Free the holding cell
@@ -2841,8 +2831,18 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                match self.free_holding_cell_htlcs(logger) {
                                        Err(ChannelError::Close(msg)) => return Err(ChannelError::Close(msg)),
                                        Err(ChannelError::Ignore(_)) | Err(ChannelError::CloseDelayBroadcast(_)) => panic!("Got non-channel-failing result from free_holding_cell_htlcs"),
-                                       Ok(Some((commitment_update, monitor_update))) => return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(monitor_update), self.resend_order.clone(), shutdown_msg)),
-                                       Ok(None) => return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), shutdown_msg)),
+                                       Ok((Some((commitment_update, monitor_update)), htlcs_to_fail)) => {
+                                               // If in the future we no longer drop holding cell update_adds on peer
+                                               // disconnect, we may be handed some HTLCs to fail backwards here.
+                                               assert!(htlcs_to_fail.is_empty());
+                                               return Ok((resend_funding_locked, required_revoke, Some(commitment_update), Some(monitor_update), self.resend_order.clone(), shutdown_msg));
+                                       },
+                                       Ok((None, htlcs_to_fail)) => {
+                                               // If in the future we no longer drop holding cell update_adds on peer
+                                               // disconnect, we may be handed some HTLCs to fail backwards here.
+                                               assert!(htlcs_to_fail.is_empty());
+                                               return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), shutdown_msg));
+                                       },
                                }
                        } else {
                                return Ok((resend_funding_locked, required_revoke, None, None, self.resend_order.clone(), shutdown_msg));
@@ -3104,14 +3104,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                self.user_id
        }
 
-       /// May only be called after funding has been initiated (ie is_funding_initiated() is true)
-       pub fn channel_monitor(&mut self) -> &mut ChannelMonitor<ChanSigner> {
-               if self.channel_state < ChannelState::FundingSent as u32 {
-                       panic!("Can't get a channel monitor until funding has been created");
-               }
-               self.channel_monitor.as_mut().unwrap()
-       }
-
        /// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus,
        /// is_usable() returns true).
        /// Allowed in any state (including after shutdown)
@@ -3135,6 +3127,18 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                self.our_htlc_minimum_msat
        }
 
+       /// Allowed in any state (including after shutdown)
+       pub fn get_announced_htlc_max_msat(&self) -> u64 {
+               return cmp::min(
+                       // Upper bound by capacity. We make it a bit less than full capacity to prevent attempts
+                       // to use full capacity. This is an effort to reduce routing failures, because in many cases
+                       // channel might have been used to route very small values (either by honest users or as DoS).
+                       self.channel_value_satoshis * 9 / 10,
+
+                       Channel::<ChanSigner>::get_our_max_htlc_value_in_flight_msat(self.channel_value_satoshis)
+               );
+       }
+
        /// Allowed in any state (including after shutdown)
        pub fn get_their_htlc_minimum_msat(&self) -> u64 {
                self.our_htlc_minimum_msat
@@ -3322,7 +3326,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        }
                });
                let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
-               if header.bitcoin_hash() != self.last_block_connected {
+               if header.block_hash() != self.last_block_connected {
                        if self.funding_tx_confirmations > 0 {
                                self.funding_tx_confirmations += 1;
                        }
@@ -3371,12 +3375,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                }
                        }
                }
-               if header.bitcoin_hash() != self.last_block_connected {
-                       self.last_block_connected = header.bitcoin_hash();
+               if header.block_hash() != self.last_block_connected {
+                       self.last_block_connected = header.block_hash();
                        self.update_time_counter = cmp::max(self.update_time_counter, header.time);
-                       if let Some(channel_monitor) = self.channel_monitor.as_mut() {
-                               channel_monitor.last_block_hash = self.last_block_connected;
-                       }
                        if self.funding_tx_confirmations > 0 {
                                if self.funding_tx_confirmations == self.minimum_depth as u64 {
                                        let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
@@ -3397,7 +3398,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                                // funding_tx_confirmed_in and return.
                                                false
                                        };
-                                       self.funding_tx_confirmed_in = Some(header.bitcoin_hash());
+                                       self.funding_tx_confirmed_in = Some(self.last_block_connected);
 
                                        //TODO: Note that this must be a duplicate of the previous commitment point they sent us,
                                        //as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
@@ -3405,8 +3406,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                        //a protocol oversight, but I assume I'm just missing something.
                                        if need_commitment_update {
                                                if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) == 0 {
-                                                       let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
-                                                       let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
+                                                       let next_per_commitment_point = self.local_keys.get_per_commitment_point(self.cur_local_commitment_transaction_number, &self.secp_ctx);
                                                        return Ok((Some(msgs::FundingLocked {
                                                                channel_id: self.channel_id,
                                                                next_per_commitment_point: next_per_commitment_point,
@@ -3432,13 +3432,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                return true;
                        }
                }
-               if Some(header.bitcoin_hash()) == self.funding_tx_confirmed_in {
+               self.last_block_connected = header.block_hash();
+               if Some(self.last_block_connected) == self.funding_tx_confirmed_in {
                        self.funding_tx_confirmations = self.minimum_depth as u64 - 1;
                }
-               self.last_block_connected = header.bitcoin_hash();
-               if let Some(channel_monitor) = self.channel_monitor.as_mut() {
-                       channel_monitor.last_block_hash = self.last_block_connected;
-               }
                false
        }
 
@@ -3457,7 +3454,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        panic!("Tried to send an open_channel for a channel that has already advanced");
                }
 
-               let local_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
+               let first_per_commitment_point = self.local_keys.get_per_commitment_point(self.cur_local_commitment_transaction_number, &self.secp_ctx);
                let local_keys = self.local_keys.pubkeys();
 
                msgs::OpenChannel {
@@ -3477,7 +3474,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        payment_point: local_keys.payment_point,
                        delayed_payment_basepoint: local_keys.delayed_payment_basepoint,
                        htlc_basepoint: local_keys.htlc_basepoint,
-                       first_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &local_commitment_secret),
+                       first_per_commitment_point,
                        channel_flags: if self.config.announced_channel {1} else {0},
                        shutdown_scriptpubkey: OptionalField::Present(if self.config.commit_upfront_shutdown_pubkey { self.get_closing_scriptpubkey() } else { Builder::new().into_script() })
                }
@@ -3494,7 +3491,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        panic!("Tried to send an accept_channel for a channel that has already advanced");
                }
 
-               let local_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
+               let first_per_commitment_point = self.local_keys.get_per_commitment_point(self.cur_local_commitment_transaction_number, &self.secp_ctx);
                let local_keys = self.local_keys.pubkeys();
 
                msgs::AcceptChannel {
@@ -3511,7 +3508,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        payment_point: local_keys.payment_point,
                        delayed_payment_basepoint: local_keys.delayed_payment_basepoint,
                        htlc_basepoint: local_keys.htlc_basepoint,
-                       first_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &local_commitment_secret),
+                       first_per_commitment_point,
                        shutdown_scriptpubkey: OptionalField::Present(if self.config.commit_upfront_shutdown_pubkey { self.get_closing_scriptpubkey() } else { Builder::new().into_script() })
                }
        }
@@ -3520,7 +3517,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        fn get_outbound_funding_created_signature<L: Deref>(&mut self, logger: &L) -> Result<Signature, ChannelError> where L::Target: Logger {
                let remote_keys = self.build_remote_transaction_keys()?;
                let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false, self.feerate_per_kw, logger).0;
-               Ok(self.local_keys.sign_remote_commitment(self.feerate_per_kw, &remote_initial_commitment_tx, &remote_keys, &Vec::new(), self.our_to_self_delay, &self.secp_ctx)
+               let pre_remote_keys = PreCalculatedTxCreationKeys::new(remote_keys);
+               Ok(self.local_keys.sign_remote_commitment(self.feerate_per_kw, &remote_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)
        }
 
@@ -3848,7 +3846,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                their_revocation_point: self.their_cur_commitment_point.unwrap()
                        }]
                };
-               self.channel_monitor.as_mut().unwrap().update_monitor_ooo(monitor_update.clone(), logger).unwrap();
                self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
                Ok((res, monitor_update))
        }
@@ -3873,10 +3870,12 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                htlcs.push(htlc);
                        }
 
-                       let res = self.local_keys.sign_remote_commitment(feerate_per_kw, &remote_commitment_tx.0, &remote_keys, &htlcs, self.our_to_self_delay, &self.secp_ctx)
+                       let pre_remote_keys = PreCalculatedTxCreationKeys::new(remote_keys);
+                       let res = self.local_keys.sign_remote_commitment(feerate_per_kw, &remote_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;
+                       let remote_keys = pre_remote_keys.trust_key_derivation();
 
                        log_trace!(logger, "Signed remote commitment tx {} with redeemscript {} -> {}",
                                encode::serialize_hex(&remote_commitment_tx.0),
@@ -3886,7 +3885,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(&remote_commitment_tx.0.txid(), feerate_per_kw, self.our_to_self_delay, htlc, &remote_keys.a_delayed_payment_key, &remote_keys.revocation_key)),
-                                       encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &remote_keys)),
+                                       encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, remote_keys)),
                                        log_bytes!(remote_keys.a_htlc_key.serialize()),
                                        log_bytes!(htlc_sig.serialize_compact()[..]));
                        }
@@ -4217,8 +4216,6 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
                self.their_shutdown_scriptpubkey.write(writer)?;
 
                self.commitment_secrets.write(writer)?;
-
-               self.channel_monitor.as_ref().unwrap().write_for_disk(writer)?;
                Ok(())
        }
 }
@@ -4373,13 +4370,6 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for Channel<ChanSigner> {
                let their_shutdown_scriptpubkey = Readable::read(reader)?;
                let commitment_secrets = Readable::read(reader)?;
 
-               let (monitor_last_block, channel_monitor) = Readable::read(reader)?;
-               // We drop the ChannelMonitor's last block connected hash cause we don't actually bother
-               // doing full block connection operations on the internal ChannelMonitor copies
-               if monitor_last_block != last_block_connected {
-                       return Err(DecodeError::InvalidValue);
-               }
-
                Ok(Channel {
                        user_id,
 
@@ -4451,7 +4441,6 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for Channel<ChanSigner> {
 
                        their_shutdown_scriptpubkey,
 
-                       channel_monitor: Some(channel_monitor),
                        commitment_secrets,
 
                        network_sync: UpdateStatus::Fresh,
@@ -4461,7 +4450,6 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for Channel<ChanSigner> {
 
 #[cfg(test)]
 mod tests {
-       use bitcoin::BitcoinHash;
        use bitcoin::util::bip143;
        use bitcoin::consensus::encode::serialize;
        use bitcoin::blockdata::script::{Script, Builder};
@@ -4530,8 +4518,7 @@ mod tests {
                fn get_channel_keys(&self, _inbound: bool, _channel_value_satoshis: u64) -> InMemoryChannelKeys {
                        self.chan_keys.clone()
                }
-               fn get_onion_rand(&self) -> (SecretKey, [u8; 32]) { panic!(); }
-               fn get_channel_id(&self) -> [u8; 32] { [0; 32] }
+               fn get_secure_random_bytes(&self) -> [u8; 32] { [0; 32] }
        }
 
        fn public_from_secret_hex(secp_ctx: &Secp256k1<All>, hex: &str) -> PublicKey {
@@ -4556,7 +4543,7 @@ mod tests {
                // Now change the fee so we can check that the fee in the open_channel message is the
                // same as the old fee.
                fee_est.fee_est = 500;
-               let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.bitcoin_hash());
+               let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
                assert_eq!(open_channel_msg.feerate_per_kw, original_fee);
        }
 
@@ -4577,7 +4564,7 @@ mod tests {
                let mut node_a_chan = Channel::<EnforcingChannelKeys>::new_outbound(&&feeest, &&keys_provider, node_a_node_id, 10000000, 100000, 42, &config).unwrap();
 
                // Create Node B's channel by receiving Node A's open_channel message
-               let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.bitcoin_hash());
+               let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
                let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
                let mut node_b_chan = Channel::<EnforcingChannelKeys>::new_from_req(&&feeest, &&keys_provider, node_b_node_id, InitFeatures::known(), &open_channel_msg, 7, &config).unwrap();
 
@@ -4666,7 +4653,7 @@ mod tests {
                        delayed_payment_basepoint: public_from_secret_hex(&secp_ctx, "1552dfba4f6cf29a62a0af13c8d6981d36d0ef8d61ba10fb0fe90da7634d7e13"),
                        htlc_basepoint: public_from_secret_hex(&secp_ctx, "4444444444444444444444444444444444444444444444444444444444444444")
                };
-               chan_keys.set_remote_channel_pubkeys(&their_pubkeys);
+               chan_keys.on_accept(&their_pubkeys, chan.their_to_self_delay, chan.our_to_self_delay);
 
                assert_eq!(their_pubkeys.payment_point.serialize()[..],
                           hex::decode("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]);
@@ -4684,7 +4671,7 @@ mod tests {
                let per_commitment_secret = SecretKey::from_slice(&hex::decode("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100").unwrap()[..]).unwrap();
                let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
                let htlc_basepoint = &chan.local_keys.pubkeys().htlc_basepoint;
-               let keys = TxCreationKeys::new(&secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &their_pubkeys.revocation_basepoint, &their_pubkeys.htlc_basepoint).unwrap();
+               let keys = TxCreationKeys::derive_new(&secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &their_pubkeys.revocation_basepoint, &their_pubkeys.htlc_basepoint).unwrap();
 
                chan.their_pubkeys = Some(their_pubkeys);
 
@@ -4722,7 +4709,7 @@ mod tests {
                                assert_eq!(serialize(&localtx.add_local_sig(&redeemscript, local_sig))[..],
                                                hex::decode($tx_hex).unwrap()[..]);
 
-                               let htlc_sigs = chan_keys.sign_local_commitment_htlc_transactions(&localtx, chan.their_to_self_delay, &chan.secp_ctx).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());
 
                                $({