Update to latest upstream rust-bitcoin
[rust-lightning] / lightning / src / ln / channelmonitor.rs
index b054258319485be03ee5ddac0aaa4b8500f6d4e5..49168627f5448647f8bc150fdce7aa72a862b738 100644 (file)
@@ -1,3 +1,12 @@
+// 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.
+
 //! The logic to monitor for on-chain transactions and create the relevant claim responses lives
 //! here.
 //!
@@ -17,7 +26,6 @@ use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
 use bitcoin::blockdata::script::{Script, Builder};
 use bitcoin::blockdata::opcodes;
 use bitcoin::consensus::encode;
-use bitcoin::util::hash::BitcoinHash;
 
 use bitcoin::hashes::Hash;
 use bitcoin::hashes::sha256::Hash as Sha256;
@@ -31,18 +39,20 @@ use ln::msgs::DecodeError;
 use ln::chan_utils;
 use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, LocalCommitmentTransaction, HTLCType};
 use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
-use ln::onchaintx::OnchainTxHandler;
+use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
 use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator};
 use chain::transaction::OutPoint;
 use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
 use util::logger::Logger;
 use util::ser::{Readable, MaybeReadable, Writer, Writeable, U48};
 use util::{byte_utils, events};
+use util::events::Event;
 
 use std::collections::{HashMap, hash_map};
 use std::sync::Mutex;
 use std::{hash,cmp, mem};
 use std::ops::Deref;
+use std::io::Error;
 
 /// An update generated by the underlying Channel itself which contains some new information the
 /// ChannelMonitor should be made aware of.
@@ -139,6 +149,16 @@ pub enum ChannelMonitorUpdateErr {
 #[derive(Debug)]
 pub struct MonitorUpdateError(pub &'static str);
 
+/// An event to be processed by the ChannelManager.
+#[derive(PartialEq)]
+pub enum MonitorEvent {
+       /// A monitor event containing an HTLCUpdate.
+       HTLCEvent(HTLCUpdate),
+
+       /// A monitor event that the Channel's commitment transaction was broadcasted.
+       CommitmentTxBroadcasted(OutPoint),
+}
+
 /// Simple structure send back by ManyChannelMonitor in case of HTLC detected onchain from a
 /// forward channel and from which info are needed to update HTLC in a backward channel.
 #[derive(Clone, PartialEq)]
@@ -166,10 +186,8 @@ pub struct SimpleManyChannelMonitor<Key, ChanSigner: ChannelKeys, T: Deref, F: D
         L::Target: Logger,
         C::Target: ChainWatchInterface,
 {
-       #[cfg(test)] // Used in ChannelManager tests to manipulate channels directly
+       /// The monitors
        pub monitors: Mutex<HashMap<Key, ChannelMonitor<ChanSigner>>>,
-       #[cfg(not(test))]
-       monitors: Mutex<HashMap<Key, ChannelMonitor<ChanSigner>>>,
        chain_monitor: C,
        broadcaster: T,
        logger: L,
@@ -183,8 +201,8 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref + Sync
              L::Target: Logger,
         C::Target: ChainWatchInterface,
 {
-       fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[u32]) {
-               let block_hash = header.bitcoin_hash();
+       fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[usize]) {
+               let block_hash = header.block_hash();
                {
                        let mut monitors = self.monitors.lock().unwrap();
                        for monitor in monitors.values_mut() {
@@ -200,7 +218,7 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref + Sync
        }
 
        fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
-               let block_hash = header.bitcoin_hash();
+               let block_hash = header.block_hash();
                let mut monitors = self.monitors.lock().unwrap();
                for monitor in monitors.values_mut() {
                        monitor.block_disconnected(disconnected_height, &block_hash, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
@@ -235,12 +253,15 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
                        hash_map::Entry::Occupied(_) => return Err(MonitorUpdateError("Channel monitor for given key is already present")),
                        hash_map::Entry::Vacant(e) => e,
                };
-               log_trace!(self.logger, "Got new Channel Monitor for channel {}", log_bytes!(monitor.funding_info.0.to_channel_id()[..]));
-               self.chain_monitor.install_watch_tx(&monitor.funding_info.0.txid, &monitor.funding_info.1);
-               self.chain_monitor.install_watch_outpoint((monitor.funding_info.0.txid, monitor.funding_info.0.index as u32), &monitor.funding_info.1);
-               for (txid, outputs) in monitor.get_outputs_to_watch().iter() {
-                       for (idx, script) in outputs.iter().enumerate() {
-                               self.chain_monitor.install_watch_outpoint((*txid, idx as u32), script);
+               {
+                       let funding_txo = monitor.get_funding_txo();
+                       log_trace!(self.logger, "Got new Channel Monitor for channel {}", log_bytes!(funding_txo.0.to_channel_id()[..]));
+                       self.chain_monitor.install_watch_tx(&funding_txo.0.txid, &funding_txo.1);
+                       self.chain_monitor.install_watch_outpoint((funding_txo.0.txid, funding_txo.0.index as u32), &funding_txo.1);
+                       for (txid, outputs) in monitor.get_outputs_to_watch().iter() {
+                               for (idx, script) in outputs.iter().enumerate() {
+                                       self.chain_monitor.install_watch_outpoint((*txid, idx as u32), script);
+                               }
                        }
                }
                entry.insert(monitor);
@@ -282,12 +303,12 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
                }
        }
 
-       fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate> {
-               let mut pending_htlcs_updated = Vec::new();
+       fn get_and_clear_pending_monitor_events(&self) -> Vec<MonitorEvent> {
+               let mut pending_monitor_events = Vec::new();
                for chan in self.monitors.lock().unwrap().values_mut() {
-                       pending_htlcs_updated.append(&mut chan.get_and_clear_pending_htlcs_updated());
+                       pending_monitor_events.append(&mut chan.get_and_clear_pending_monitor_events());
                }
-               pending_htlcs_updated
+               pending_monitor_events
        }
 }
 
@@ -297,7 +318,7 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref, F: De
              L::Target: Logger,
         C::Target: ChainWatchInterface,
 {
-       fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
+       fn get_and_clear_pending_events(&self) -> Vec<Event> {
                let mut pending_events = Vec::new();
                for chan in self.monitors.lock().unwrap().values_mut() {
                        pending_events.append(&mut chan.get_and_clear_pending_events());
@@ -366,28 +387,88 @@ struct LocalSignedTx {
        b_htlc_key: PublicKey,
        delayed_payment_key: PublicKey,
        per_commitment_point: PublicKey,
-       feerate_per_kw: u64,
+       feerate_per_kw: u32,
        htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>,
 }
 
+/// We use this to track remote commitment transactions and htlcs outputs and
+/// use it to generate any justice or 2nd-stage preimage/timeout transactions.
+#[derive(PartialEq)]
+struct RemoteCommitmentTransaction {
+       remote_delayed_payment_base_key: PublicKey,
+       remote_htlc_base_key: PublicKey,
+       on_remote_tx_csv: u16,
+       per_htlc: HashMap<Txid, Vec<HTLCOutputInCommitment>>
+}
+
+impl Writeable for RemoteCommitmentTransaction {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               self.remote_delayed_payment_base_key.write(w)?;
+               self.remote_htlc_base_key.write(w)?;
+               w.write_all(&byte_utils::be16_to_array(self.on_remote_tx_csv))?;
+               w.write_all(&byte_utils::be64_to_array(self.per_htlc.len() as u64))?;
+               for (ref txid, ref htlcs) in self.per_htlc.iter() {
+                       w.write_all(&txid[..])?;
+                       w.write_all(&byte_utils::be64_to_array(htlcs.len() as u64))?;
+                       for &ref htlc in htlcs.iter() {
+                               htlc.write(w)?;
+                       }
+               }
+               Ok(())
+       }
+}
+impl Readable for RemoteCommitmentTransaction {
+       fn read<R: ::std::io::Read>(r: &mut R) -> Result<Self, DecodeError> {
+               let remote_commitment_transaction = {
+                       let remote_delayed_payment_base_key = Readable::read(r)?;
+                       let remote_htlc_base_key = Readable::read(r)?;
+                       let on_remote_tx_csv: u16 = Readable::read(r)?;
+                       let per_htlc_len: u64 = Readable::read(r)?;
+                       let mut per_htlc = HashMap::with_capacity(cmp::min(per_htlc_len as usize, MAX_ALLOC_SIZE / 64));
+                       for _  in 0..per_htlc_len {
+                               let txid: Txid = Readable::read(r)?;
+                               let htlcs_count: u64 = Readable::read(r)?;
+                               let mut htlcs = Vec::with_capacity(cmp::min(htlcs_count as usize, MAX_ALLOC_SIZE / 32));
+                               for _ in 0..htlcs_count {
+                                       let htlc = Readable::read(r)?;
+                                       htlcs.push(htlc);
+                               }
+                               if let Some(_) = per_htlc.insert(txid, htlcs) {
+                                       return Err(DecodeError::InvalidValue);
+                               }
+                       }
+                       RemoteCommitmentTransaction {
+                               remote_delayed_payment_base_key,
+                               remote_htlc_base_key,
+                               on_remote_tx_csv,
+                               per_htlc,
+                       }
+               };
+               Ok(remote_commitment_transaction)
+       }
+}
+
 /// When ChannelMonitor discovers an onchain outpoint being a step of a channel and that it needs
 /// to generate a tx to push channel state forward, we cache outpoint-solving tx material to build
 /// a new bumped one in case of lenghty confirmation delay
 #[derive(Clone, PartialEq)]
 pub(crate) enum InputMaterial {
        Revoked {
-               witness_script: Script,
-               pubkey: Option<PublicKey>,
-               key: SecretKey,
-               is_htlc: bool,
+               per_commitment_point: PublicKey,
+               remote_delayed_payment_base_key: PublicKey,
+               remote_htlc_base_key: PublicKey,
+               per_commitment_key: SecretKey,
+               input_descriptor: InputDescriptors,
                amount: u64,
+               htlc: Option<HTLCOutputInCommitment>,
+               on_remote_tx_csv: u16,
        },
        RemoteHTLC {
-               witness_script: Script,
-               key: SecretKey,
+               per_commitment_point: PublicKey,
+               remote_delayed_payment_base_key: PublicKey,
+               remote_htlc_base_key: PublicKey,
                preimage: Option<PaymentPreimage>,
-               amount: u64,
-               locktime: u32,
+               htlc: HTLCOutputInCommitment
        },
        LocalHTLC {
                preimage: Option<PaymentPreimage>,
@@ -401,21 +482,24 @@ pub(crate) enum InputMaterial {
 impl Writeable for InputMaterial  {
        fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
                match self {
-                       &InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref is_htlc, ref amount} => {
+                       &InputMaterial::Revoked { ref per_commitment_point, ref remote_delayed_payment_base_key, ref remote_htlc_base_key, ref per_commitment_key, ref input_descriptor, ref amount, ref htlc, ref on_remote_tx_csv} => {
                                writer.write_all(&[0; 1])?;
-                               witness_script.write(writer)?;
-                               pubkey.write(writer)?;
-                               writer.write_all(&key[..])?;
-                               is_htlc.write(writer)?;
+                               per_commitment_point.write(writer)?;
+                               remote_delayed_payment_base_key.write(writer)?;
+                               remote_htlc_base_key.write(writer)?;
+                               writer.write_all(&per_commitment_key[..])?;
+                               input_descriptor.write(writer)?;
                                writer.write_all(&byte_utils::be64_to_array(*amount))?;
+                               htlc.write(writer)?;
+                               on_remote_tx_csv.write(writer)?;
                        },
-                       &InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => {
+                       &InputMaterial::RemoteHTLC { ref per_commitment_point, ref remote_delayed_payment_base_key, ref remote_htlc_base_key, ref preimage, ref htlc} => {
                                writer.write_all(&[1; 1])?;
-                               witness_script.write(writer)?;
-                               key.write(writer)?;
+                               per_commitment_point.write(writer)?;
+                               remote_delayed_payment_base_key.write(writer)?;
+                               remote_htlc_base_key.write(writer)?;
                                preimage.write(writer)?;
-                               writer.write_all(&byte_utils::be64_to_array(*amount))?;
-                               writer.write_all(&byte_utils::be32_to_array(*locktime))?;
+                               htlc.write(writer)?;
                        },
                        &InputMaterial::LocalHTLC { ref preimage, ref amount } => {
                                writer.write_all(&[2; 1])?;
@@ -435,31 +519,37 @@ impl Readable for InputMaterial {
        fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
                let input_material = match <u8 as Readable>::read(reader)? {
                        0 => {
-                               let witness_script = Readable::read(reader)?;
-                               let pubkey = Readable::read(reader)?;
-                               let key = Readable::read(reader)?;
-                               let is_htlc = Readable::read(reader)?;
+                               let per_commitment_point = Readable::read(reader)?;
+                               let remote_delayed_payment_base_key = Readable::read(reader)?;
+                               let remote_htlc_base_key = Readable::read(reader)?;
+                               let per_commitment_key = Readable::read(reader)?;
+                               let input_descriptor = Readable::read(reader)?;
                                let amount = Readable::read(reader)?;
+                               let htlc = Readable::read(reader)?;
+                               let on_remote_tx_csv = Readable::read(reader)?;
                                InputMaterial::Revoked {
-                                       witness_script,
-                                       pubkey,
-                                       key,
-                                       is_htlc,
-                                       amount
+                                       per_commitment_point,
+                                       remote_delayed_payment_base_key,
+                                       remote_htlc_base_key,
+                                       per_commitment_key,
+                                       input_descriptor,
+                                       amount,
+                                       htlc,
+                                       on_remote_tx_csv
                                }
                        },
                        1 => {
-                               let witness_script = Readable::read(reader)?;
-                               let key = Readable::read(reader)?;
+                               let per_commitment_point = Readable::read(reader)?;
+                               let remote_delayed_payment_base_key = Readable::read(reader)?;
+                               let remote_htlc_base_key = Readable::read(reader)?;
                                let preimage = Readable::read(reader)?;
-                               let amount = Readable::read(reader)?;
-                               let locktime = Readable::read(reader)?;
+                               let htlc = Readable::read(reader)?;
                                InputMaterial::RemoteHTLC {
-                                       witness_script,
-                                       key,
+                                       per_commitment_point,
+                                       remote_delayed_payment_base_key,
+                                       remote_htlc_base_key,
                                        preimage,
-                                       amount,
-                                       locktime
+                                       htlc
                                }
                        },
                        2 => {
@@ -650,7 +740,7 @@ impl Readable for ChannelMonitorUpdateStep {
 /// information and are actively monitoring the chain.
 ///
 /// Pending Events or updated HTLCs which have not yet been read out by
-/// get_and_clear_pending_htlcs_updated or get_and_clear_pending_events are serialized to disk and
+/// get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and
 /// reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
 /// gotten are fully handled before re-serializing the new state.
 pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
@@ -658,7 +748,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
        commitment_transaction_number_obscure_factor: u64,
 
        destination_script: Script,
-       broadcasted_local_revokable_script: Option<(Script, SecretKey, Script)>,
+       broadcasted_local_revokable_script: Option<(Script, PublicKey, PublicKey)>,
        remote_payment_script: Script,
        shutdown_script: Script,
 
@@ -667,15 +757,13 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
        current_remote_commitment_txid: Option<Txid>,
        prev_remote_commitment_txid: Option<Txid>,
 
-       their_htlc_base_key: PublicKey,
-       their_delayed_payment_base_key: PublicKey,
+       remote_tx_cache: RemoteCommitmentTransaction,
        funding_redeemscript: Script,
        channel_value_satoshis: u64,
        // first is the idx of the first of the two revocation points
        their_cur_revocation_points: Option<(u64, PublicKey, Option<PublicKey>)>,
 
-       our_to_self_delay: u16,
-       their_to_self_delay: u16,
+       on_local_tx_csv: u16,
 
        commitment_secrets: CounterpartyCommitmentSecrets,
        remote_claimable_outpoints: HashMap<Txid, Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>>,
@@ -707,8 +795,8 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
 
        payment_preimages: HashMap<PaymentHash, PaymentPreimage>,
 
-       pending_htlcs_updated: Vec<HTLCUpdate>,
-       pending_events: Vec<events::Event>,
+       pending_monitor_events: Vec<MonitorEvent>,
+       pending_events: Vec<Event>,
 
        // Used to track onchain events, i.e transactions parts of channels confirmed on chain, on which
        // we have to take actions once they reach enough confs. Key is a block height timer, i.e we enforce
@@ -741,7 +829,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
        // (we do *not*, however, update them in update_monitor to ensure any local user copies keep
        // their last_block_hash from its state and not based on updated copies that didn't run through
        // the full block_connected).
-       pub(crate) last_block_hash: BlockHash,
+       last_block_hash: BlockHash,
        secp_ctx: Secp256k1<secp256k1::All>, //TODO: dedup this a bit...
 }
 
@@ -804,9 +892,9 @@ pub trait ManyChannelMonitor: Send + Sync {
        /// with success or failure.
        ///
        /// You should probably just call through to
-       /// ChannelMonitor::get_and_clear_pending_htlcs_updated() for each ChannelMonitor and return
+       /// ChannelMonitor::get_and_clear_pending_monitor_events() for each ChannelMonitor and return
        /// the full list.
-       fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate>;
+       fn get_and_clear_pending_monitor_events(&self) -> Vec<MonitorEvent>;
 }
 
 #[cfg(any(test, feature = "fuzztarget"))]
@@ -823,13 +911,11 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
                        self.funding_info != other.funding_info ||
                        self.current_remote_commitment_txid != other.current_remote_commitment_txid ||
                        self.prev_remote_commitment_txid != other.prev_remote_commitment_txid ||
-                       self.their_htlc_base_key != other.their_htlc_base_key ||
-                       self.their_delayed_payment_base_key != other.their_delayed_payment_base_key ||
+                       self.remote_tx_cache != other.remote_tx_cache ||
                        self.funding_redeemscript != other.funding_redeemscript ||
                        self.channel_value_satoshis != other.channel_value_satoshis ||
                        self.their_cur_revocation_points != other.their_cur_revocation_points ||
-                       self.our_to_self_delay != other.our_to_self_delay ||
-                       self.their_to_self_delay != other.their_to_self_delay ||
+                       self.on_local_tx_csv != other.on_local_tx_csv ||
                        self.commitment_secrets != other.commitment_secrets ||
                        self.remote_claimable_outpoints != other.remote_claimable_outpoints ||
                        self.remote_commitment_txn_on_chain != other.remote_commitment_txn_on_chain ||
@@ -839,7 +925,7 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
                        self.current_local_commitment_number != other.current_local_commitment_number ||
                        self.current_local_commitment_tx != other.current_local_commitment_tx ||
                        self.payment_preimages != other.payment_preimages ||
-                       self.pending_htlcs_updated != other.pending_htlcs_updated ||
+                       self.pending_monitor_events != other.pending_monitor_events ||
                        self.pending_events.len() != other.pending_events.len() || // We trust events to round-trip properly
                        self.onchain_events_waiting_threshold_conf != other.onchain_events_waiting_threshold_conf ||
                        self.outputs_to_watch != other.outputs_to_watch ||
@@ -861,7 +947,7 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
        /// the "reorg path" (ie disconnecting blocks until you find a common ancestor from both the
        /// returned block hash and the the current chain and then reconnecting blocks to get to the
        /// best chain) upon deserializing the object!
-       pub fn write_for_disk<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+       pub fn write_for_disk<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
                //TODO: We still write out all the serialization here manually instead of using the fancy
                //serialization framework we have, we should migrate things over to it.
                writer.write_all(&[SERIALIZATION_VERSION; 1])?;
@@ -892,8 +978,7 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
                self.current_remote_commitment_txid.write(writer)?;
                self.prev_remote_commitment_txid.write(writer)?;
 
-               writer.write_all(&self.their_htlc_base_key.serialize())?;
-               writer.write_all(&self.their_delayed_payment_base_key.serialize())?;
+               self.remote_tx_cache.write(writer)?;
                self.funding_redeemscript.write(writer)?;
                self.channel_value_satoshis.write(writer)?;
 
@@ -915,8 +1000,7 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
                        },
                }
 
-               writer.write_all(&byte_utils::be16_to_array(self.our_to_self_delay))?;
-               writer.write_all(&byte_utils::be16_to_array(self.their_to_self_delay))?;
+               writer.write_all(&byte_utils::be16_to_array(self.on_local_tx_csv))?;
 
                self.commitment_secrets.write(writer)?;
 
@@ -965,7 +1049,7 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
                                writer.write_all(&$local_tx.delayed_payment_key.serialize())?;
                                writer.write_all(&$local_tx.per_commitment_point.serialize())?;
 
-                               writer.write_all(&byte_utils::be64_to_array($local_tx.feerate_per_kw))?;
+                               writer.write_all(&byte_utils::be32_to_array($local_tx.feerate_per_kw))?;
                                writer.write_all(&byte_utils::be64_to_array($local_tx.htlc_outputs.len() as u64))?;
                                for &(ref htlc_output, ref sig, ref htlc_source) in $local_tx.htlc_outputs.iter() {
                                        serialize_htlc_in_commitment!(htlc_output);
@@ -997,9 +1081,15 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
                        writer.write_all(&payment_preimage.0[..])?;
                }
 
-               writer.write_all(&byte_utils::be64_to_array(self.pending_htlcs_updated.len() as u64))?;
-               for data in self.pending_htlcs_updated.iter() {
-                       data.write(writer)?;
+               writer.write_all(&byte_utils::be64_to_array(self.pending_monitor_events.len() as u64))?;
+               for event in self.pending_monitor_events.iter() {
+                       match event {
+                               MonitorEvent::HTLCEvent(upd) => {
+                                       0u8.write(writer)?;
+                                       upd.write(writer)?;
+                               },
+                               MonitorEvent::CommitmentTxBroadcasted(_) => 1u8.write(writer)?
+                       }
                }
 
                writer.write_all(&byte_utils::be64_to_array(self.pending_events.len() as u64))?;
@@ -1047,9 +1137,9 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
 
 impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
        pub(super) fn new(keys: ChanSigner, shutdown_pubkey: &PublicKey,
-                       our_to_self_delay: u16, destination_script: &Script, funding_info: (OutPoint, Script),
-                       their_htlc_base_key: &PublicKey, their_delayed_payment_base_key: &PublicKey,
-                       their_to_self_delay: u16, funding_redeemscript: Script, channel_value_satoshis: u64,
+                       on_remote_tx_csv: u16, destination_script: &Script, funding_info: (OutPoint, Script),
+                       remote_htlc_base_key: &PublicKey, remote_delayed_payment_base_key: &PublicKey,
+                       on_local_tx_csv: u16, funding_redeemscript: Script, channel_value_satoshis: u64,
                        commitment_transaction_number_obscure_factor: u64,
                        initial_local_commitment_tx: LocalCommitmentTransaction) -> ChannelMonitor<ChanSigner> {
 
@@ -1059,7 +1149,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                let payment_key_hash = WPubkeyHash::hash(&keys.pubkeys().payment_point.serialize());
                let remote_payment_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&payment_key_hash[..]).into_script();
 
-               let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys.clone(), their_to_self_delay);
+               let remote_tx_cache = RemoteCommitmentTransaction { remote_delayed_payment_base_key: *remote_delayed_payment_base_key, remote_htlc_base_key: *remote_htlc_base_key, on_remote_tx_csv, per_htlc: HashMap::new() };
+
+               let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys.clone(), on_local_tx_csv);
 
                let local_tx_sequence = initial_local_commitment_tx.unsigned_tx.input[0].sequence as u64;
                let local_tx_locktime = initial_local_commitment_tx.unsigned_tx.lock_time as u64;
@@ -1094,14 +1186,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        current_remote_commitment_txid: None,
                        prev_remote_commitment_txid: None,
 
-                       their_htlc_base_key: their_htlc_base_key.clone(),
-                       their_delayed_payment_base_key: their_delayed_payment_base_key.clone(),
+                       remote_tx_cache,
                        funding_redeemscript,
                        channel_value_satoshis: channel_value_satoshis,
                        their_cur_revocation_points: None,
 
-                       our_to_self_delay,
-                       their_to_self_delay,
+                       on_local_tx_csv,
 
                        commitment_secrets: CounterpartyCommitmentSecrets::new(),
                        remote_claimable_outpoints: HashMap::new(),
@@ -1114,7 +1204,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        current_local_commitment_number: 0xffff_ffff_ffff - ((((local_tx_sequence & 0xffffff) << 3*8) | (local_tx_locktime as u64 & 0xffffff)) ^ commitment_transaction_number_obscure_factor),
 
                        payment_preimages: HashMap::new(),
-                       pending_htlcs_updated: Vec::new(),
+                       pending_monitor_events: Vec::new(),
                        pending_events: Vec::new(),
 
                        onchain_events_waiting_threshold_conf: HashMap::new(),
@@ -1199,7 +1289,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                log_trace!(logger, "New potential remote commitment transaction: {}", encode::serialize_hex(unsigned_commitment_tx));
                self.prev_remote_commitment_txid = self.current_remote_commitment_txid.take();
                self.current_remote_commitment_txid = Some(new_txid);
-               self.remote_claimable_outpoints.insert(new_txid, htlc_outputs);
+               self.remote_claimable_outpoints.insert(new_txid, htlc_outputs.clone());
                self.current_remote_commitment_number = commitment_number;
                //TODO: Merge this into the other per-remote-transaction output storage stuff
                match self.their_cur_revocation_points {
@@ -1220,13 +1310,20 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                                self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
                        }
                }
+               let mut htlcs = Vec::with_capacity(htlc_outputs.len());
+               for htlc in htlc_outputs {
+                       if htlc.0.transaction_output_index.is_some() {
+                               htlcs.push(htlc.0);
+                       }
+               }
+               self.remote_tx_cache.per_htlc.insert(new_txid, htlcs);
        }
 
        /// Informs this monitor of the latest local (ie broadcastable) commitment transaction. The
        /// monitor watches for timeouts and may broadcast it if we approach such a timeout. Thus, it
        /// is important that any clones of this channel monitor (including remote clones) by kept
        /// up-to-date as our local commitment transaction is updated.
-       /// Panics if set_their_to_self_delay has never been called.
+       /// Panics if set_on_local_tx_csv has never been called.
        pub(super) fn provide_latest_local_commitment_tx_info(&mut self, commitment_tx: LocalCommitmentTransaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
                if self.local_tx_signed {
                        return Err(MonitorUpdateError("A local commitment tx has already been signed, no new local commitment txn can be sent to our counterparty"));
@@ -1271,27 +1368,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                for tx in self.get_latest_local_commitment_txn(logger).iter() {
                        broadcaster.broadcast_transaction(tx);
                }
-       }
-
-       /// Used in Channel to cheat wrt the update_ids since it plays games, will be removed soon!
-       pub(super) fn update_monitor_ooo<L: Deref>(&mut self, mut updates: ChannelMonitorUpdate, logger: &L) -> Result<(), MonitorUpdateError> where L::Target: Logger {
-               for update in updates.updates.drain(..) {
-                       match update {
-                               ChannelMonitorUpdateStep::LatestLocalCommitmentTXInfo { commitment_tx, htlc_outputs } => {
-                                       if self.lockdown_from_offchain { panic!(); }
-                                       self.provide_latest_local_commitment_tx_info(commitment_tx, htlc_outputs)?
-                               },
-                               ChannelMonitorUpdateStep::LatestRemoteCommitmentTXInfo { unsigned_commitment_tx, htlc_outputs, commitment_number, their_revocation_point } =>
-                                       self.provide_latest_remote_commitment_tx_info(&unsigned_commitment_tx, htlc_outputs, commitment_number, their_revocation_point, logger),
-                               ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage } =>
-                                       self.provide_payment_preimage(&PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner()), &payment_preimage),
-                               ChannelMonitorUpdateStep::CommitmentSecret { idx, secret } =>
-                                       self.provide_secret(idx, secret)?,
-                               ChannelMonitorUpdateStep::ChannelForceClosed { .. } => {},
-                       }
-               }
-               self.latest_update_id = updates.update_id;
-               Ok(())
+               self.pending_monitor_events.push(MonitorEvent::CommitmentTxBroadcasted(self.funding_info.0));
        }
 
        /// Updates a ChannelMonitor on the basis of some new information provided by the Channel
@@ -1338,8 +1415,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
        }
 
        /// Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
-       pub fn get_funding_txo(&self) -> OutPoint {
-               self.funding_info.0
+       pub fn get_funding_txo(&self) -> &(OutPoint, Script) {
+               &self.funding_info
        }
 
        /// Gets a list of txids, with their output scripts (in the order they appear in the
@@ -1363,10 +1440,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
        }
 
        /// Get the list of HTLCs who's status has been updated on chain. This should be called by
-       /// ChannelManager via ManyChannelMonitor::get_and_clear_pending_htlcs_updated().
-       pub fn get_and_clear_pending_htlcs_updated(&mut self) -> Vec<HTLCUpdate> {
+       /// ChannelManager via ManyChannelMonitor::get_and_clear_pending_monitor_events().
+       pub fn get_and_clear_pending_monitor_events(&mut self) -> Vec<MonitorEvent> {
                let mut ret = Vec::new();
-               mem::swap(&mut ret, &mut self.pending_htlcs_updated);
+               mem::swap(&mut ret, &mut self.pending_monitor_events);
                ret
        }
 
@@ -1376,7 +1453,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
        /// This is called by ManyChannelMonitor::get_and_clear_pending_events() and is equivalent to
        /// EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
        /// no internal locking in ChannelMonitors.
-       pub fn get_and_clear_pending_events(&mut self) -> Vec<events::Event> {
+       pub fn get_and_clear_pending_events(&mut self) -> Vec<Event> {
                let mut ret = Vec::new();
                mem::swap(&mut ret, &mut self.pending_events);
                ret
@@ -1429,19 +1506,16 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
                        let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
                        let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.keys.pubkeys().revocation_basepoint));
-                       let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &self.keys.revocation_base_key()));
-                       let b_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &self.keys.pubkeys().htlc_basepoint));
-                       let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.their_delayed_payment_base_key));
-                       let a_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.their_htlc_base_key));
+                       let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.remote_tx_cache.remote_delayed_payment_base_key));
 
-                       let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.our_to_self_delay, &delayed_key);
+                       let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.remote_tx_cache.on_remote_tx_csv, &delayed_key);
                        let revokeable_p2wsh = revokeable_redeemscript.to_v0_p2wsh();
 
                        // First, process non-htlc outputs (to_local & to_remote)
                        for (idx, outp) in tx.output.iter().enumerate() {
                                if outp.script_pubkey == revokeable_p2wsh {
-                                       let witness_data = InputMaterial::Revoked { witness_script: revokeable_redeemscript.clone(), pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: false, amount: outp.value };
-                                       claimable_outpoints.push(ClaimRequest { absolute_timelock: height + self.our_to_self_delay as u32, aggregable: true, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 }, witness_data});
+                                       let witness_data = InputMaterial::Revoked { per_commitment_point, remote_delayed_payment_base_key: self.remote_tx_cache.remote_delayed_payment_base_key, remote_htlc_base_key: self.remote_tx_cache.remote_htlc_base_key, per_commitment_key, input_descriptor: InputDescriptors::RevokedOutput, amount: outp.value, htlc: None, on_remote_tx_csv: self.remote_tx_cache.on_remote_tx_csv};
+                                       claimable_outpoints.push(ClaimRequest { absolute_timelock: height + self.remote_tx_cache.on_remote_tx_csv as u32, aggregable: true, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 }, witness_data});
                                }
                        }
 
@@ -1449,13 +1523,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        if let Some(ref per_commitment_data) = per_commitment_option {
                                for (_, &(ref htlc, _)) in per_commitment_data.iter().enumerate() {
                                        if let Some(transaction_output_index) = htlc.transaction_output_index {
-                                               let expected_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
                                                if transaction_output_index as usize >= tx.output.len() ||
-                                                               tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
-                                                               tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
+                                                               tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 {
                                                        return (claimable_outpoints, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
                                                }
-                                               let witness_data = InputMaterial::Revoked { witness_script: expected_script, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: true, amount: tx.output[transaction_output_index as usize].value };
+                                               let witness_data = InputMaterial::Revoked { per_commitment_point, remote_delayed_payment_base_key: self.remote_tx_cache.remote_delayed_payment_base_key, remote_htlc_base_key: self.remote_tx_cache.remote_htlc_base_key, per_commitment_key, input_descriptor: if htlc.offered { InputDescriptors::RevokedOfferedHTLC } else { InputDescriptors::RevokedReceivedHTLC }, amount: tx.output[transaction_output_index as usize].value, htlc: Some(htlc.clone()), on_remote_tx_csv: self.remote_tx_cache.on_remote_tx_csv};
                                                claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable: true, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: transaction_output_index }, witness_data });
                                        }
                                }
@@ -1573,24 +1645,24 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                                                if revocation_points.0 == commitment_number + 1 { Some(point) } else { None }
                                        } else { None };
                                if let Some(revocation_point) = revocation_point_option {
-                                       let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &self.keys.pubkeys().revocation_basepoint));
-                                       let b_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &self.keys.pubkeys().htlc_basepoint));
-                                       let htlc_privkey = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.keys.htlc_base_key()));
-                                       let a_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &self.their_htlc_base_key));
+                                       self.remote_payment_script = {
+                                               // Note that the Network here is ignored as we immediately drop the address for the
+                                               // script_pubkey version
+                                               let payment_hash160 = WPubkeyHash::hash(&self.keys.pubkeys().payment_point.serialize());
+                                               Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&payment_hash160[..]).into_script()
+                                       };
 
                                        // Then, try to find htlc outputs
                                        for (_, &(ref htlc, _)) in per_commitment_data.iter().enumerate() {
                                                if let Some(transaction_output_index) = htlc.transaction_output_index {
-                                                       let expected_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
                                                        if transaction_output_index as usize >= tx.output.len() ||
-                                                                       tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
-                                                                       tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
+                                                                       tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 {
                                                                return (claimable_outpoints, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
                                                        }
                                                        let preimage = if htlc.offered { if let Some(p) = self.payment_preimages.get(&htlc.payment_hash) { Some(*p) } else { None } } else { None };
                                                        let aggregable = if !htlc.offered { false } else { true };
                                                        if preimage.is_some() || !htlc.offered {
-                                                               let witness_data = InputMaterial::RemoteHTLC { witness_script: expected_script, key: htlc_privkey, preimage, amount: htlc.amount_msat / 1000, locktime: htlc.cltv_expiry };
+                                                               let witness_data = InputMaterial::RemoteHTLC { per_commitment_point: *revocation_point, remote_delayed_payment_base_key: self.remote_tx_cache.remote_delayed_payment_base_key, remote_htlc_base_key: self.remote_tx_cache.remote_htlc_base_key, preimage, htlc: htlc.clone() };
                                                                claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: transaction_output_index }, witness_data });
                                                        }
                                                }
@@ -1620,25 +1692,19 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                let secret = if let Some(secret) = self.get_secret(commitment_number) { secret } else { return (Vec::new(), None); };
                let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
                let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
-               let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.keys.pubkeys().revocation_basepoint));
-               let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &self.keys.revocation_base_key()));
-               let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &self.their_delayed_payment_base_key));
-               let redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.our_to_self_delay, &delayed_key);
 
                log_trace!(logger, "Remote HTLC broadcast {}:{}", htlc_txid, 0);
-               let witness_data = InputMaterial::Revoked { witness_script: redeemscript, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: false, amount: tx.output[0].value };
-               let claimable_outpoints = vec!(ClaimRequest { absolute_timelock: height + self.our_to_self_delay as u32, aggregable: true, outpoint: BitcoinOutPoint { txid: htlc_txid, vout: 0}, witness_data });
+               let witness_data = InputMaterial::Revoked { per_commitment_point, remote_delayed_payment_base_key: self.remote_tx_cache.remote_delayed_payment_base_key, remote_htlc_base_key: self.remote_tx_cache.remote_htlc_base_key,  per_commitment_key, input_descriptor: InputDescriptors::RevokedOutput, amount: tx.output[0].value, htlc: None, on_remote_tx_csv: self.remote_tx_cache.on_remote_tx_csv };
+               let claimable_outpoints = vec!(ClaimRequest { absolute_timelock: height + self.remote_tx_cache.on_remote_tx_csv as u32, aggregable: true, outpoint: BitcoinOutPoint { txid: htlc_txid, vout: 0}, witness_data });
                (claimable_outpoints, Some((htlc_txid, tx.output.clone())))
        }
 
-       fn broadcast_by_local_state(&self, commitment_tx: &Transaction, local_tx: &LocalSignedTx) -> (Vec<ClaimRequest>, Vec<TxOut>, Option<(Script, SecretKey, Script)>) {
+       fn broadcast_by_local_state(&self, commitment_tx: &Transaction, local_tx: &LocalSignedTx) -> (Vec<ClaimRequest>, Vec<TxOut>, Option<(Script, PublicKey, PublicKey)>) {
                let mut claim_requests = Vec::with_capacity(local_tx.htlc_outputs.len());
                let mut watch_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
 
-               let redeemscript = chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.their_to_self_delay, &local_tx.delayed_payment_key);
-               let broadcasted_local_revokable_script = if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, &local_tx.per_commitment_point, self.keys.delayed_payment_base_key()) {
-                       Some((redeemscript.to_v0_p2wsh(), local_delayedkey, redeemscript))
-               } else { None };
+               let redeemscript = chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.on_local_tx_csv, &local_tx.delayed_payment_key);
+               let broadcasted_local_revokable_script = Some((redeemscript.to_v0_p2wsh(), local_tx.per_commitment_point.clone(), local_tx.revocation_key.clone()));
 
                for &(ref htlc, _, _) in local_tx.htlc_outputs.iter() {
                        if let Some(transaction_output_index) = htlc.transaction_output_index {
@@ -1778,7 +1844,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
        /// Unsafe test-only version of get_latest_local_commitment_txn used by our test framework
        /// to bypass LocalCommitmentTransaction state update lockdown after signature and generate
        /// revoked commitment transaction.
-       #[cfg(test)]
+       #[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
        pub fn unsafe_get_latest_local_commitment_txn<L: Deref>(&mut self, logger: &L) -> Vec<Transaction> where L::Target: Logger {
                log_trace!(logger, "Getting signed copy of latest local commitment transaction!");
                if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_copy_local_tx(&self.funding_redeemscript) {
@@ -1869,7 +1935,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        claimable_outpoints.push(ClaimRequest { absolute_timelock: height, aggregable: false, outpoint: BitcoinOutPoint { txid: self.funding_info.0.txid.clone(), vout: self.funding_info.0.index as u32 }, witness_data: InputMaterial::Funding { funding_redeemscript: self.funding_redeemscript.clone() }});
                }
                if should_broadcast {
+                       self.pending_monitor_events.push(MonitorEvent::CommitmentTxBroadcasted(self.funding_info.0));
                        if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_local_tx(&self.funding_redeemscript) {
+                               self.local_tx_signed = true;
                                let (mut new_outpoints, new_outputs, _) = self.broadcast_by_local_state(&commitment_tx, &self.current_local_commitment_tx);
                                if !new_outputs.is_empty() {
                                        watch_outputs.push((self.current_local_commitment_tx.txid.clone(), new_outputs));
@@ -1882,21 +1950,22 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                                match ev {
                                        OnchainEvent::HTLCUpdate { htlc_update } => {
                                                log_trace!(logger, "HTLC {} failure update has got enough confirmations to be passed upstream", log_bytes!((htlc_update.1).0));
-                                               self.pending_htlcs_updated.push(HTLCUpdate {
+                                               self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
                                                        payment_hash: htlc_update.1,
                                                        payment_preimage: None,
                                                        source: htlc_update.0,
-                                               });
+                                               }));
                                        },
                                        OnchainEvent::MaturingOutput { descriptor } => {
                                                log_trace!(logger, "Descriptor {} has got enough confirmations to be passed upstream", log_spendable!(descriptor));
-                                               self.pending_events.push(events::Event::SpendableOutputs {
+                                               self.pending_events.push(Event::SpendableOutputs {
                                                        outputs: vec![descriptor]
                                                });
                                        }
                                }
                        }
                }
+
                self.onchain_tx_handler.block_connected(txn_matched, claimable_outpoints, height, &*broadcaster, &*fee_estimator, &*logger);
 
                self.last_block_hash = block_hash.clone();
@@ -1924,7 +1993,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                self.last_block_hash = block_hash.clone();
        }
 
-       pub(super) fn would_broadcast_at_height<L: Deref>(&self, height: u32, logger: &L) -> bool where L::Target: Logger {
+       fn would_broadcast_at_height<L: Deref>(&self, height: u32, logger: &L) -> bool where L::Target: Logger {
                // We need to consider all HTLCs which are:
                //  * in any unrevoked remote commitment transaction, as they could broadcast said
                //    transactions and we'd end up in a race, or
@@ -2082,22 +2151,26 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        if let Some((source, payment_hash)) = payment_data {
                                let mut payment_preimage = PaymentPreimage([0; 32]);
                                if accepted_preimage_claim {
-                                       if !self.pending_htlcs_updated.iter().any(|update| update.source == source) {
+                                       if !self.pending_monitor_events.iter().any(
+                                               |update| if let &MonitorEvent::HTLCEvent(ref upd) = update { upd.source == source } else { false }) {
                                                payment_preimage.0.copy_from_slice(&input.witness[3]);
-                                               self.pending_htlcs_updated.push(HTLCUpdate {
+                                               self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
                                                        source,
                                                        payment_preimage: Some(payment_preimage),
                                                        payment_hash
-                                               });
+                                               }));
                                        }
                                } else if offered_preimage_claim {
-                                       if !self.pending_htlcs_updated.iter().any(|update| update.source == source) {
+                                       if !self.pending_monitor_events.iter().any(
+                                               |update| if let &MonitorEvent::HTLCEvent(ref upd) = update {
+                                                       upd.source == source
+                                               } else { false }) {
                                                payment_preimage.0.copy_from_slice(&input.witness[1]);
-                                               self.pending_htlcs_updated.push(HTLCUpdate {
+                                               self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
                                                        source,
                                                        payment_preimage: Some(payment_preimage),
                                                        payment_hash
-                                               });
+                                               }));
                                        }
                                } else {
                                        log_info!(logger, "Failing HTLC with payment_hash {} timeout by a spend tx, waiting for confirmation (at height{})", log_bytes!(payment_hash.0), height + ANTI_REORG_DELAY - 1);
@@ -2127,33 +2200,48 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
        fn is_paying_spendable_output<L: Deref>(&mut self, tx: &Transaction, height: u32, logger: &L) where L::Target: Logger {
                let mut spendable_output = None;
                for (i, outp) in tx.output.iter().enumerate() { // There is max one spendable output for any channel tx, including ones generated by us
+                       if i > ::std::u16::MAX as usize {
+                               // While it is possible that an output exists on chain which is greater than the
+                               // 2^16th output in a given transaction, this is only possible if the output is not
+                               // in a lightning transaction and was instead placed there by some third party who
+                               // wishes to give us money for no reason.
+                               // Namely, any lightning transactions which we pre-sign will never have anywhere
+                               // near 2^16 outputs both because such transactions must have ~2^16 outputs who's
+                               // scripts are not longer than one byte in length and because they are inherently
+                               // non-standard due to their size.
+                               // Thus, it is completely safe to ignore such outputs, and while it may result in
+                               // us ignoring non-lightning fund to us, that is only possible if someone fills
+                               // nearly a full block with garbage just to hit this case.
+                               continue;
+                       }
                        if outp.script_pubkey == self.destination_script {
                                spendable_output =  Some(SpendableOutputDescriptor::StaticOutput {
-                                       outpoint: BitcoinOutPoint { txid: tx.txid(), vout: i as u32 },
+                                       outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
                                        output: outp.clone(),
                                });
                                break;
                        } else if let Some(ref broadcasted_local_revokable_script) = self.broadcasted_local_revokable_script {
                                if broadcasted_local_revokable_script.0 == outp.script_pubkey {
                                        spendable_output =  Some(SpendableOutputDescriptor::DynamicOutputP2WSH {
-                                               outpoint: BitcoinOutPoint { txid: tx.txid(), vout: i as u32 },
-                                               key: broadcasted_local_revokable_script.1,
-                                               witness_script: broadcasted_local_revokable_script.2.clone(),
-                                               to_self_delay: self.their_to_self_delay,
+                                               outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
+                                               per_commitment_point: broadcasted_local_revokable_script.1,
+                                               to_self_delay: self.on_local_tx_csv,
                                                output: outp.clone(),
+                                               key_derivation_params: self.keys.key_derivation_params(),
+                                               remote_revocation_pubkey: broadcasted_local_revokable_script.2.clone(),
                                        });
                                        break;
                                }
                        } else if self.remote_payment_script == outp.script_pubkey {
-                               spendable_output = Some(SpendableOutputDescriptor::DynamicOutputP2WPKH {
-                                       outpoint: BitcoinOutPoint { txid: tx.txid(), vout: i as u32 },
-                                       key: self.keys.payment_key().clone(),
+                               spendable_output = Some(SpendableOutputDescriptor::StaticOutputRemotePayment {
+                                       outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
                                        output: outp.clone(),
+                                       key_derivation_params: self.keys.key_derivation_params(),
                                });
                                break;
                        } else if outp.script_pubkey == self.shutdown_script {
                                spendable_output = Some(SpendableOutputDescriptor::StaticOutput {
-                                       outpoint: BitcoinOutPoint { txid: tx.txid(), vout: i as u32 },
+                                       outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
                                        output: outp.clone(),
                                });
                        }
@@ -2199,9 +2287,9 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
                let broadcasted_local_revokable_script = match <u8 as Readable>::read(reader)? {
                        0 => {
                                let revokable_address = Readable::read(reader)?;
-                               let local_delayedkey = Readable::read(reader)?;
+                               let per_commitment_point = Readable::read(reader)?;
                                let revokable_script = Readable::read(reader)?;
-                               Some((revokable_address, local_delayedkey, revokable_script))
+                               Some((revokable_address, per_commitment_point, revokable_script))
                        },
                        1 => { None },
                        _ => return Err(DecodeError::InvalidValue),
@@ -2220,8 +2308,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
                let current_remote_commitment_txid = Readable::read(reader)?;
                let prev_remote_commitment_txid = Readable::read(reader)?;
 
-               let their_htlc_base_key = Readable::read(reader)?;
-               let their_delayed_payment_base_key = Readable::read(reader)?;
+               let remote_tx_cache = Readable::read(reader)?;
                let funding_redeemscript = Readable::read(reader)?;
                let channel_value_satoshis = Readable::read(reader)?;
 
@@ -2240,8 +2327,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
                        }
                };
 
-               let our_to_self_delay: u16 = Readable::read(reader)?;
-               let their_to_self_delay: u16 = Readable::read(reader)?;
+               let on_local_tx_csv: u16 = Readable::read(reader)?;
 
                let commitment_secrets = Readable::read(reader)?;
 
@@ -2309,7 +2395,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
                                        let b_htlc_key = Readable::read(reader)?;
                                        let delayed_payment_key = Readable::read(reader)?;
                                        let per_commitment_point = Readable::read(reader)?;
-                                       let feerate_per_kw: u64 = Readable::read(reader)?;
+                                       let feerate_per_kw: u32 = Readable::read(reader)?;
 
                                        let htlcs_len: u64 = Readable::read(reader)?;
                                        let mut htlcs = Vec::with_capacity(cmp::min(htlcs_len as usize, MAX_ALLOC_SIZE / 128));
@@ -2354,14 +2440,19 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
                        }
                }
 
-               let pending_htlcs_updated_len: u64 = Readable::read(reader)?;
-               let mut pending_htlcs_updated = Vec::with_capacity(cmp::min(pending_htlcs_updated_len as usize, MAX_ALLOC_SIZE / (32 + 8*3)));
-               for _ in 0..pending_htlcs_updated_len {
-                       pending_htlcs_updated.push(Readable::read(reader)?);
+               let pending_monitor_events_len: u64 = Readable::read(reader)?;
+               let mut pending_monitor_events = Vec::with_capacity(cmp::min(pending_monitor_events_len as usize, MAX_ALLOC_SIZE / (32 + 8*3)));
+               for _ in 0..pending_monitor_events_len {
+                       let ev = match <u8 as Readable>::read(reader)? {
+                               0 => MonitorEvent::HTLCEvent(Readable::read(reader)?),
+                               1 => MonitorEvent::CommitmentTxBroadcasted(funding_info.0),
+                               _ => return Err(DecodeError::InvalidValue)
+                       };
+                       pending_monitor_events.push(ev);
                }
 
                let pending_events_len: u64 = Readable::read(reader)?;
-               let mut pending_events = Vec::with_capacity(cmp::min(pending_events_len as usize, MAX_ALLOC_SIZE / mem::size_of::<events::Event>()));
+               let mut pending_events = Vec::with_capacity(cmp::min(pending_events_len as usize, MAX_ALLOC_SIZE / mem::size_of::<Event>()));
                for _ in 0..pending_events_len {
                        if let Some(event) = MaybeReadable::read(reader)? {
                                pending_events.push(event);
@@ -2430,14 +2521,12 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
                        current_remote_commitment_txid,
                        prev_remote_commitment_txid,
 
-                       their_htlc_base_key,
-                       their_delayed_payment_base_key,
+                       remote_tx_cache,
                        funding_redeemscript,
                        channel_value_satoshis,
                        their_cur_revocation_points,
 
-                       our_to_self_delay,
-                       their_to_self_delay,
+                       on_local_tx_csv,
 
                        commitment_secrets,
                        remote_claimable_outpoints,
@@ -2450,7 +2539,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
                        current_local_commitment_number,
 
                        payment_preimages,
-                       pending_htlcs_updated,
+                       pending_monitor_events,
                        pending_events,
 
                        onchain_events_waiting_threshold_conf,
@@ -2488,7 +2577,6 @@ mod tests {
        use util::test_utils::TestLogger;
        use bitcoin::secp256k1::key::{SecretKey,PublicKey};
        use bitcoin::secp256k1::Secp256k1;
-       use rand::{thread_rng,Rng};
        use std::sync::Arc;
        use chain::keysinterface::InMemoryChannelKeys;
 
@@ -2502,10 +2590,8 @@ mod tests {
 
                let mut preimages = Vec::new();
                {
-                       let mut rng  = thread_rng();
-                       for _ in 0..20 {
-                               let mut preimage = PaymentPreimage([0; 32]);
-                               rng.fill_bytes(&mut preimage.0[..]);
+                       for i in 0..20 {
+                               let preimage = PaymentPreimage([i; 32]);
                                let hash = PaymentHash(Sha256::hash(&preimage.0[..]).into_inner());
                                preimages.push((preimage, hash));
                        }
@@ -2555,6 +2641,7 @@ mod tests {
                        SecretKey::from_slice(&[41; 32]).unwrap(),
                        [41; 32],
                        0,
+                       (0, 0)
                );
 
                // Prune with one old state and a local commitment tx holding a few overlaps with the