Update to rust-secp256k1 v0.11 and rust-bitcoin v0.14
[rust-lightning] / src / ln / channelmanager.rs
index f39d0c093ceac2055c7105821a8396c7be431fd8..76c7bb18ff30be84e8b8a491622efd244e221577 100644 (file)
@@ -19,13 +19,15 @@ use ln::msgs;
 use ln::msgs::{HandleError,ChannelMessageHandler,MsgEncodable,MsgDecodable};
 use util::{byte_utils, events, internal_traits, rng};
 use util::sha2::Sha256;
+use util::chacha20poly1305rfc::ChaCha20;
+use util::logger::Logger;
+use util::errors::APIError;
 
 use crypto;
 use crypto::mac::{Mac,MacResult};
 use crypto::hmac::Hmac;
 use crypto::digest::Digest;
 use crypto::symmetriccipher::SynchronousStreamCipher;
-use crypto::chacha20::ChaCha20;
 
 use std::{ptr, mem};
 use std::collections::HashMap;
@@ -160,12 +162,14 @@ pub struct ChannelManager {
        announce_channels_publicly: bool,
        fee_proportional_millionths: u32,
        latest_block_height: AtomicUsize,
-       secp_ctx: Secp256k1,
+       secp_ctx: Secp256k1<secp256k1::All>,
 
        channel_state: Mutex<ChannelHolder>,
        our_network_key: SecretKey,
 
        pending_events: Mutex<Vec<events::Event>>,
+
+       logger: Arc<Logger>,
 }
 
 const CLTV_EXPIRY_DELTA: u16 = 6 * 24 * 2; //TODO?
@@ -211,7 +215,7 @@ impl ChannelManager {
        /// fee_proportional_millionths is an optional fee to charge any payments routed through us.
        /// Non-proportional fees are fixed according to our risk using the provided fee estimator.
        /// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`!
-       pub fn new(our_network_key: SecretKey, fee_proportional_millionths: u32, announce_channels_publicly: bool, network: Network, feeest: Arc<FeeEstimator>, monitor: Arc<ManyChannelMonitor>, chain_monitor: Arc<ChainWatchInterface>, tx_broadcaster: Arc<BroadcasterInterface>) -> Result<Arc<ChannelManager>, secp256k1::Error> {
+       pub fn new(our_network_key: SecretKey, fee_proportional_millionths: u32, announce_channels_publicly: bool, network: Network, feeest: Arc<FeeEstimator>, monitor: Arc<ManyChannelMonitor>, chain_monitor: Arc<ChainWatchInterface>, tx_broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>) -> Result<Arc<ChannelManager>, secp256k1::Error> {
                let secp_ctx = Secp256k1::new();
 
                let res = Arc::new(ChannelManager {
@@ -236,6 +240,8 @@ impl ChannelManager {
                        our_network_key,
 
                        pending_events: Mutex::new(Vec::new()),
+
+                       logger,
                });
                let weak_res = Arc::downgrade(&res);
                res.chain_monitor.register_listener(weak_res);
@@ -249,7 +255,8 @@ impl ChannelManager {
        /// may wish to avoid using 0 for user_id here.
        /// If successful, will generate a SendOpenChannel event, so you should probably poll
        /// PeerManager::process_events afterwards.
-       pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, user_id: u64) -> Result<(), HandleError> {
+       /// Raises APIError::APIMisuseError when channel_value_satoshis > 2**24 or push_msat being greater than channel_value_satoshis * 1k
+       pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64) -> Result<(), APIError> {
                let chan_keys = if cfg!(feature = "fuzztarget") {
                        ChannelKeys {
                                funding_key:               SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap(),
@@ -270,7 +277,7 @@ impl ChannelManager {
                        }
                };
 
-               let channel = Channel::new_outbound(&*self.fee_estimator, chan_keys, their_network_key, channel_value_satoshis, self.announce_channels_publicly, user_id);
+               let channel = Channel::new_outbound(&*self.fee_estimator, chan_keys, their_network_key, channel_value_satoshis, push_msat, self.announce_channels_publicly, user_id, Arc::clone(&self.logger))?;
                let res = channel.get_open_channel(self.genesis_hash.clone(), &*self.fee_estimator)?;
                let mut channel_state = self.channel_state.lock().unwrap();
                match channel_state.by_id.insert(channel.channel_id(), channel) {
@@ -408,6 +415,14 @@ impl ChannelManager {
                }
        }
 
+       /// Force close all channels, immediately broadcasting the latest local commitment transaction
+       /// for each to the chain and rejecting new HTLCs on each.
+       pub fn force_close_all_channels(&self) {
+               for chan in self.list_channels() {
+                       self.force_close_channel(&chan.channel_id);
+               }
+       }
+
        #[inline]
        fn gen_rho_mu_from_shared_secret(shared_secret: &SharedSecret) -> ([u8; 32], [u8; 32]) {
                ({
@@ -446,9 +461,9 @@ impl ChannelManager {
 
        // can only fail if an intermediary hop has an invalid public key or session_priv is invalid
        #[inline]
-       fn construct_onion_keys_callback<FType: FnMut(SharedSecret, [u8; 32], PublicKey, &RouteHop)> (secp_ctx: &Secp256k1, route: &Route, session_priv: &SecretKey, mut callback: FType) -> Result<(), HandleError> {
+       fn construct_onion_keys_callback<T: secp256k1::Signing, FType: FnMut(SharedSecret, [u8; 32], PublicKey, &RouteHop)> (secp_ctx: &Secp256k1<T>, route: &Route, session_priv: &SecretKey, mut callback: FType) -> Result<(), HandleError> {
                let mut blinded_priv = session_priv.clone();
-               let mut blinded_pub = secp_call!(PublicKey::from_secret_key(secp_ctx, &blinded_priv));
+               let mut blinded_pub = PublicKey::from_secret_key(secp_ctx, &blinded_priv);
                let mut first_iteration = true;
 
                for hop in route.hops.iter() {
@@ -461,13 +476,13 @@ impl ChannelManager {
                        sha.result(&mut blinding_factor);
 
                        if first_iteration {
-                               blinded_pub = secp_call!(PublicKey::from_secret_key(secp_ctx, &blinded_priv));
+                               blinded_pub = PublicKey::from_secret_key(secp_ctx, &blinded_priv);
                                first_iteration = false;
                        }
                        let ephemeral_pubkey = blinded_pub;
 
                        secp_call!(blinded_priv.mul_assign(secp_ctx, &secp_call!(SecretKey::from_slice(secp_ctx, &blinding_factor))));
-                       blinded_pub = secp_call!(PublicKey::from_secret_key(secp_ctx, &blinded_priv));
+                       blinded_pub = PublicKey::from_secret_key(secp_ctx, &blinded_priv);
 
                        callback(shared_secret, blinding_factor, ephemeral_pubkey, hop);
                }
@@ -476,7 +491,7 @@ impl ChannelManager {
        }
 
        // can only fail if an intermediary hop has an invalid public key or session_priv is invalid
-       fn construct_onion_keys(secp_ctx: &Secp256k1, route: &Route, session_priv: &SecretKey) -> Result<Vec<OnionKeys>, HandleError> {
+       fn construct_onion_keys<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, route: &Route, session_priv: &SecretKey) -> Result<Vec<OnionKeys>, HandleError> {
                let mut res = Vec::with_capacity(route.hops.len());
 
                Self::construct_onion_keys_callback(secp_ctx, route, session_priv, |shared_secret, _blinding_factor, ephemeral_pubkey, _| {
@@ -659,7 +674,7 @@ impl ChannelManager {
                        Some(id) => id,
                };
 
-               let were_node_one = PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key).unwrap().serialize()[..] < chan.get_their_node_id().serialize()[..];
+               let were_node_one = PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key).serialize()[..] < chan.get_their_node_id().serialize()[..];
 
                let unsigned = msgs::UnsignedChannelUpdate {
                        chain_hash: self.genesis_hash,
@@ -673,7 +688,7 @@ impl ChannelManager {
                };
 
                let msg_hash = Sha256dHash::from_data(&unsigned.encode()[..]);
-               let sig = self.secp_ctx.sign(&Message::from_slice(&msg_hash[..]).unwrap(), &self.our_network_key).unwrap(); //TODO Can we unwrap here?
+               let sig = self.secp_ctx.sign(&Message::from_slice(&msg_hash[..]).unwrap(), &self.our_network_key); //TODO Can we unwrap here?
 
                Ok(msgs::ChannelUpdate {
                        signature: sig,
@@ -765,7 +780,10 @@ impl ChannelManager {
 
        /// Call this upon creation of a funding transaction for the given channel.
        /// Panics if a funding transaction has already been provided for this channel.
+       /// May panic if the funding_txo is duplicative with some other channel (note that this should
+       /// be trivially prevented by using unique funding transaction keys per-channel).
        pub fn funding_transaction_generated(&self, temporary_channel_id: &[u8; 32], funding_txo: OutPoint) {
+
                macro_rules! add_pending_event {
                        ($event: expr) => {
                                {
@@ -784,12 +802,12 @@ impl ChannelManager {
                                                        (chan, funding_msg.0, funding_msg.1)
                                                },
                                                Err(e) => {
+                                                       log_error!(self, "Got bad signatures: {}!", e.err);
                                                        mem::drop(channel_state);
-                                                       add_pending_event!(events::Event::DisconnectPeer {
+                                                       add_pending_event!(events::Event::HandleError {
                                                                node_id: chan.get_their_node_id(),
-                                                               msg: if let Some(msgs::ErrorAction::DisconnectPeer { msg } ) = e.action { msg } else { None },
+                                                               action: e.action,
                                                        });
-
                                                        return;
                                                },
                                        }
@@ -806,7 +824,14 @@ impl ChannelManager {
                });
 
                let mut channel_state = self.channel_state.lock().unwrap();
-               channel_state.by_id.insert(chan.channel_id(), chan);
+               match channel_state.by_id.entry(chan.channel_id()) {
+                       hash_map::Entry::Occupied(_) => {
+                               panic!("Generated duplicate funding txid?");
+                       },
+                       hash_map::Entry::Vacant(e) => {
+                               e.insert(chan);
+                       }
+               }
        }
 
        fn get_announcement_sigs(&self, chan: &Channel) -> Result<Option<msgs::AnnouncementSignatures>, HandleError> {
@@ -814,7 +839,7 @@ impl ChannelManager {
 
                let (announcement, our_bitcoin_sig) = chan.get_channel_announcement(self.get_our_node_id(), self.genesis_hash.clone())?;
                let msghash = Message::from_slice(&Sha256dHash::from_data(&announcement.encode()[..])[..]).unwrap();
-               let our_node_sig = secp_call!(self.secp_ctx.sign(&msghash, &self.our_network_key));
+               let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
 
                Ok(Some(msgs::AnnouncementSignatures {
                        channel_id: chan.channel_id(),
@@ -880,7 +905,7 @@ impl ChannelManager {
                                        if !add_htlc_msgs.is_empty() {
                                                let (commitment_msg, monitor) = match forward_chan.send_commitment() {
                                                        Ok(res) => res,
-                                                       Err(_) => {
+                                                       Err(_e) => {
                                                                //TODO: Handle...this is bad!
                                                                continue;
                                                        },
@@ -1004,6 +1029,7 @@ impl ChannelManager {
                                                }
 
                                                let mut pending_events = self.pending_events.lock().unwrap();
+                                               //TODO: replace by HandleError ? UpdateFailHTLC in handle_update_add_htlc need also to build a CommitmentSigned
                                                pending_events.push(events::Event::SendFailHTLC {
                                                        node_id,
                                                        msg: msg,
@@ -1112,7 +1138,7 @@ impl ChannelManager {
 
        /// Gets the node_id held by this ChannelManager
        pub fn get_our_node_id(&self) -> PublicKey {
-               PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key).unwrap()
+               PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key)
        }
 
        /// Used to restore channels to normal operation after a
@@ -1145,7 +1171,8 @@ impl ChainListener for ChannelManager {
                                if let Ok(Some(funding_locked)) = chan_res {
                                        let announcement_sigs = match self.get_announcement_sigs(channel) {
                                                Ok(res) => res,
-                                               Err(_e) => {
+                                               Err(e) => {
+                                                       log_error!(self, "Got error handling message: {}!", e.err);
                                                        //TODO: push e on events and blow up the channel (it has bad keys)
                                                        return true;
                                                }
@@ -1157,12 +1184,10 @@ impl ChainListener for ChannelManager {
                                        });
                                        short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
                                } else if let Err(e) = chan_res {
-                                       if let Some(msgs::ErrorAction::DisconnectPeer { msg }) = e.action {
-                                               new_events.push(events::Event::DisconnectPeer {
-                                                       node_id: channel.get_their_node_id(),
-                                                       msg: msg
-                                               });
-                                       } else { unreachable!(); }
+                                       new_events.push(events::Event::HandleError {
+                                               node_id: channel.get_their_node_id(),
+                                               action: e.action,
+                                       });
                                        if channel.is_shutdown() {
                                                return false;
                                        }
@@ -1170,7 +1195,7 @@ impl ChainListener for ChannelManager {
                                if let Some(funding_txo) = channel.get_funding_txo() {
                                        for tx in txn_matched {
                                                for inp in tx.input.iter() {
-                                                       if inp.prev_hash == funding_txo.txid && inp.prev_index == funding_txo.index as u32 {
+                                                       if inp.previous_output == funding_txo.into_bitcoin_outpoint() {
                                                                if let Some(short_id) = channel.get_short_channel_id() {
                                                                        short_to_id.remove(&short_id);
                                                                }
@@ -1188,7 +1213,7 @@ impl ChainListener for ChannelManager {
                                                }
                                        }
                                }
-                               if channel.channel_monitor().would_broadcast_at_height(height) {
+                               if channel.is_funding_initiated() && channel.channel_monitor().would_broadcast_at_height(height) {
                                        if let Some(short_id) = channel.get_short_channel_id() {
                                                short_to_id.remove(&short_id);
                                        }
@@ -1268,13 +1293,13 @@ impl ChannelMessageHandler for ChannelManager {
 
                let chan_keys = if cfg!(feature = "fuzztarget") {
                        ChannelKeys {
-                               funding_key:               SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap(),
-                               revocation_base_key:       SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap(),
-                               payment_base_key:          SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap(),
-                               delayed_payment_base_key:  SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap(),
-                               htlc_base_key:             SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap(),
-                               channel_close_key:         SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap(),
-                               channel_monitor_claim_key: SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap(),
+                               funding_key:               SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]).unwrap(),
+                               revocation_base_key:       SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0]).unwrap(),
+                               payment_base_key:          SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0]).unwrap(),
+                               delayed_payment_base_key:  SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0]).unwrap(),
+                               htlc_base_key:             SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0]).unwrap(),
+                               channel_close_key:         SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0]).unwrap(),
+                               channel_monitor_claim_key: SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0]).unwrap(),
                                commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                        }
                } else {
@@ -1286,7 +1311,7 @@ impl ChannelMessageHandler for ChannelManager {
                        }
                };
 
-               let channel = Channel::new_from_req(&*self.fee_estimator, chan_keys, their_node_id.clone(), msg, 0, false, self.announce_channels_publicly)?;
+               let channel = Channel::new_from_req(&*self.fee_estimator, chan_keys, their_node_id.clone(), msg, 0, false, self.announce_channels_publicly, Arc::clone(&self.logger))?;
                let accept_msg = channel.get_accept_channel()?;
                channel_state.by_id.insert(channel.channel_id(), channel);
                Ok(accept_msg)
@@ -1317,26 +1342,24 @@ impl ChannelMessageHandler for ChannelManager {
        }
 
        fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<msgs::FundingSigned, HandleError> {
-               //TODO: broke this - a node shouldn't be able to get their channel removed by sending a
-               //funding_created a second time, or long after the first, or whatever (note this also
-               //leaves the short_to_id map in a busted state.
                let (chan, funding_msg, monitor_update) = {
                        let mut channel_state = self.channel_state.lock().unwrap();
-                       match channel_state.by_id.remove(&msg.temporary_channel_id) {
-                               Some(mut chan) => {
-                                       if chan.get_their_node_id() != *their_node_id {
+                       match channel_state.by_id.entry(msg.temporary_channel_id.clone()) {
+                               hash_map::Entry::Occupied(mut chan) => {
+                                       if chan.get().get_their_node_id() != *their_node_id {
                                                return Err(HandleError{err: "Got a message for a channel from the wrong node!", action: None})
                                        }
-                                       match chan.funding_created(msg) {
+                                       match chan.get_mut().funding_created(msg) {
                                                Ok((funding_msg, monitor_update)) => {
-                                                       (chan, funding_msg, monitor_update)
+                                                       (chan.remove(), funding_msg, monitor_update)
                                                },
                                                Err(e) => {
+                                                       //TODO: Possibly remove the channel depending on e.action
                                                        return Err(e);
                                                }
                                        }
                                },
-                               None => return Err(HandleError{err: "Failed to find corresponding channel", action: None})
+                               hash_map::Entry::Vacant(_) => return Err(HandleError{err: "Failed to find corresponding channel", action: None})
                        }
                }; // Release channel lock for install_watch_outpoint call,
                   // note that this means if the remote end is misbehaving and sends a message for the same
@@ -1346,7 +1369,17 @@ impl ChannelMessageHandler for ChannelManager {
                        unimplemented!();
                }
                let mut channel_state = self.channel_state.lock().unwrap();
-               channel_state.by_id.insert(funding_msg.channel_id, chan);
+               match channel_state.by_id.entry(funding_msg.channel_id) {
+                       hash_map::Entry::Occupied(_) => {
+                               return Err(HandleError {
+                                       err: "Duplicate channel_id!",
+                                       action: Some(msgs::ErrorAction::SendErrorMessage { msg: msgs::ErrorMessage { channel_id: funding_msg.channel_id, data: "Already had channel with the new channel_id".to_owned() } })
+                               });
+                       },
+                       hash_map::Entry::Vacant(e) => {
+                               e.insert(chan);
+                       }
+               }
                Ok(funding_msg)
        }
 
@@ -1600,8 +1633,6 @@ impl ChannelMessageHandler for ChannelManager {
                                        hmac: next_hop_data.hmac.clone(),
                                };
 
-                               //TODO: Check amt_to_forward and outgoing_cltv_value are within acceptable ranges!
-
                                PendingForwardHTLCInfo {
                                        onion_packet: Some(outgoing_packet),
                                        payment_hash: msg.payment_hash.clone(),
@@ -1623,6 +1654,17 @@ impl ChannelMessageHandler for ChannelManager {
                                Some(id) => id.clone(),
                        };
                        let chan = channel_state.by_id.get_mut(&forwarding_id).unwrap();
+                       let fee = chan.get_our_fee_base_msat(&*self.fee_estimator) + (pending_forward_info.amt_to_forward * self.fee_proportional_millionths as u64 / 1000000) as u32;
+                       if msg.amount_msat < fee as u64 || (msg.amount_msat - fee as u64) < pending_forward_info.amt_to_forward {
+                               log_debug!(self, "HTLC {} incorrect amount: in {} out {} fee required {}", msg.htlc_id, msg.amount_msat, pending_forward_info.amt_to_forward, fee);
+                               let chan_update = self.get_channel_update(chan).unwrap();
+                               return_err!("Prior hop has deviated from specified fees parameters or origin node has obsolete ones", 0x1000 | 12, &chan_update.encode_with_len()[..]);
+                       }
+                       if (msg.cltv_expiry as u64) < pending_forward_info.outgoing_cltv_value as u64 + CLTV_EXPIRY_DELTA as u64 {
+                               log_debug!(self, "HTLC {} incorrect CLTV: in {} out {} delta required {}", msg.htlc_id, msg.cltv_expiry, pending_forward_info.outgoing_cltv_value, CLTV_EXPIRY_DELTA);
+                               let chan_update = self.get_channel_update(chan).unwrap();
+                               return_err!("Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta", 0x1000 | 13, &chan_update.encode_with_len()[..]);
+                       }
                        if !chan.is_live() {
                                let chan_update = self.get_channel_update(chan).unwrap();
                                return_err!("Forwarding channel is not in a ready state.", 0x1000 | 7, &chan_update.encode_with_len()[..]);
@@ -1897,7 +1939,7 @@ impl ChannelMessageHandler for ChannelManager {
                                        secp_call!(self.secp_ctx.verify(&msghash, &msg.node_signature, if were_node_one { &announcement.node_id_2 } else { &announcement.node_id_1 }));
                                        secp_call!(self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, if were_node_one { &announcement.bitcoin_key_2 } else { &announcement.bitcoin_key_1 }));
 
-                                       let our_node_sig = secp_call!(self.secp_ctx.sign(&msghash, &self.our_network_key));
+                                       let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
 
                                        (msgs::ChannelAnnouncement {
                                                node_signature_1: if were_node_one { our_node_sig } else { msg.node_signature },
@@ -1972,6 +2014,7 @@ mod tests {
        use ln::msgs::{MsgEncodable,ChannelMessageHandler,RoutingMessageHandler};
        use util::test_utils;
        use util::events::{Event, EventsProvider};
+       use util::logger::Logger;
 
        use bitcoin::util::hash::Sha256dHash;
        use bitcoin::blockdata::block::{Block, BlockHeader};
@@ -2168,7 +2211,7 @@ mod tests {
 
        static mut CHAN_COUNT: u32 = 0;
        fn create_chan_between_nodes(node_a: &Node, node_b: &Node) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
-               node_a.node.create_channel(node_b.node.get_our_node_id(), 100000, 42).unwrap();
+               node_a.node.create_channel(node_b.node.get_our_node_id(), 100000, 10001, 42).unwrap();
 
                let events_1 = node_a.node.get_and_clear_pending_events();
                assert_eq!(events_1.len(), 1);
@@ -2686,10 +2729,11 @@ mod tests {
                let mut nodes = Vec::new();
                let mut rng = thread_rng();
                let secp_ctx = Secp256k1::new();
+               let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
 
                for _ in 0..node_count {
                        let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
-                       let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new());
+                       let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Arc::clone(&logger)));
                        let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
                        let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone()));
                        let node_id = {
@@ -2697,8 +2741,8 @@ mod tests {
                                rng.fill_bytes(&mut key_slice);
                                SecretKey::from_slice(&secp_ctx, &key_slice).unwrap()
                        };
-                       let node = ChannelManager::new(node_id.clone(), 0, true, Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone()).unwrap();
-                       let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &node_id).unwrap());
+                       let node = ChannelManager::new(node_id.clone(), 0, true, Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger)).unwrap();
+                       let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &node_id), Arc::clone(&logger));
                        nodes.push(Node { feeest, chain_monitor, tx_broadcaster, chan_monitor, node_id, node, router });
                }
 
@@ -2835,7 +2879,7 @@ mod tests {
                        res.push(explicit_tx.clone());
                } else {
                        for tx in node_txn.iter() {
-                               if tx.input.len() == 1 && tx.input[0].prev_hash == chan.3.txid() {
+                               if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
                                        let mut funding_tx_map = HashMap::new();
                                        funding_tx_map.insert(chan.3.txid(), chan.3.clone());
                                        tx.verify(&funding_tx_map).unwrap();
@@ -2847,7 +2891,7 @@ mod tests {
 
                if has_htlc_tx != HTLCType::NONE {
                        for tx in node_txn.iter() {
-                               if tx.input.len() == 1 && tx.input[0].prev_hash == res[0].txid() {
+                               if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
                                        let mut funding_tx_map = HashMap::new();
                                        funding_tx_map.insert(res[0].txid(), res[0].clone());
                                        tx.verify(&funding_tx_map).unwrap();
@@ -2874,7 +2918,7 @@ mod tests {
                let mut found_prev = false;
 
                for tx in prev_txn {
-                       if node_txn[0].input[0].prev_hash == tx.txid() {
+                       if node_txn[0].input[0].previous_output.txid == tx.txid() {
                                let mut funding_tx_map = HashMap::new();
                                funding_tx_map.insert(tx.txid(), tx.clone());
                                node_txn[0].verify(&funding_tx_map).unwrap();