Store+expose bits of Channel[Monitor] to figure out local state
[rust-lightning] / src / ln / channel.rs
index 2f73cd3c5d4f5cf89d343507b5da28d1fb37386b..d4b30f0c456a8636665d5bff1c212194f186cd1f 100644 (file)
@@ -13,9 +13,9 @@ use secp256k1;
 use crypto::digest::Digest;
 
 use ln::msgs;
-use ln::msgs::{ErrorAction, HandleError, RAACommitmentOrder};
+use ln::msgs::{ErrorAction, HandleError};
 use ln::channelmonitor::ChannelMonitor;
-use ln::channelmanager::{PendingHTLCStatus, HTLCSource, PendingForwardHTLCInfo, HTLCFailReason, HTLCFailureMsg};
+use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingForwardHTLCInfo, RAACommitmentOrder};
 use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGHT,HTLC_TIMEOUT_TX_WEIGHT};
 use ln::chan_utils;
 use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
@@ -436,11 +436,9 @@ impl Channel {
                let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
 
                let secp_ctx = Secp256k1::new();
-               let our_channel_monitor_claim_key_hash = Hash160::from_data(&PublicKey::from_secret_key(&secp_ctx, &chan_keys.channel_monitor_claim_key).serialize());
-               let our_channel_monitor_claim_script = Builder::new().push_opcode(opcodes::All::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script();
                let channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key, &chan_keys.delayed_payment_base_key,
-                                                         &chan_keys.htlc_base_key,
-                                                         BREAKDOWN_TIMEOUT, our_channel_monitor_claim_script);
+                                                         &chan_keys.htlc_base_key, BREAKDOWN_TIMEOUT,
+                                                         keys_provider.get_destination_script(), logger.clone());
 
                Ok(Channel {
                        user_id: user_id,
@@ -600,11 +598,9 @@ impl Channel {
                }
 
                let secp_ctx = Secp256k1::new();
-               let our_channel_monitor_claim_key_hash = Hash160::from_data(&PublicKey::from_secret_key(&secp_ctx, &chan_keys.channel_monitor_claim_key).serialize());
-               let our_channel_monitor_claim_script = Builder::new().push_opcode(opcodes::All::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script();
                let mut channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key, &chan_keys.delayed_payment_base_key,
-                                                             &chan_keys.htlc_base_key,
-                                                             BREAKDOWN_TIMEOUT, our_channel_monitor_claim_script);
+                                                             &chan_keys.htlc_base_key, BREAKDOWN_TIMEOUT,
+                                                             keys_provider.get_destination_script(), logger.clone());
                channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
                channel_monitor.set_their_to_self_delay(msg.to_self_delay);
 
@@ -2048,6 +2044,7 @@ impl Channel {
                                },
                        }
                });
+               self.next_remote_htlc_id -= inbound_drop_count;
 
                for htlc in self.pending_outbound_htlcs.iter_mut() {
                        if let OutboundHTLCState::RemoteRemoved = htlc.state {
@@ -2563,6 +2560,18 @@ impl Channel {
                self.feerate_per_kw
        }
 
+       pub fn get_cur_local_commitment_transaction_number(&self) -> u64 {
+               self.cur_local_commitment_transaction_number + 1
+       }
+
+       pub fn get_cur_remote_commitment_transaction_number(&self) -> u64 {
+               self.cur_remote_commitment_transaction_number + 1 - if self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32) != 0 { 1 } else { 0 }
+       }
+
+       pub fn get_revoked_remote_commitment_transaction_number(&self) -> u64 {
+               self.cur_remote_commitment_transaction_number + 2
+       }
+
        //TODO: Testing purpose only, should be changed in another way after #81
        #[cfg(test)]
        pub fn get_local_keys(&self) -> &ChannelKeys {
@@ -2677,6 +2686,7 @@ impl Channel {
                if self.funding_tx_confirmations > 0 {
                        if header.bitcoin_hash() != self.last_block_connected {
                                self.last_block_connected = header.bitcoin_hash();
+                               self.channel_monitor.last_block_hash = self.last_block_connected;
                                self.funding_tx_confirmations += 1;
                                if self.funding_tx_confirmations == Channel::derive_minimum_depth(self.channel_value_satoshis*1000, self.value_to_self_msat) as u64 {
                                        let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
@@ -3232,11 +3242,12 @@ impl Channel {
 
 #[cfg(test)]
 mod tests {
-       use bitcoin::util::hash::Sha256dHash;
+       use bitcoin::util::hash::{Sha256dHash, Hash160};
        use bitcoin::util::bip143;
        use bitcoin::network::serialize::serialize;
-       use bitcoin::blockdata::script::Script;
+       use bitcoin::blockdata::script::{Script, Builder};
        use bitcoin::blockdata::transaction::Transaction;
+       use bitcoin::blockdata::opcodes;
        use hex;
        use ln::channelmanager::HTLCSource;
        use ln::channel::{Channel,ChannelKeys,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,TxCreationKeys};
@@ -3273,7 +3284,12 @@ mod tests {
        }
        impl KeysInterface for Keys {
                fn get_node_secret(&self) -> SecretKey { panic!(); }
-               fn get_destination_script(&self) -> Script { panic!(); }
+               fn get_destination_script(&self) -> Script {
+                       let secp_ctx = Secp256k1::signing_only();
+                       let channel_monitor_claim_key = SecretKey::from_slice(&secp_ctx, &hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap();
+                       let our_channel_monitor_claim_key_hash = Hash160::from_data(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
+                       Builder::new().push_opcode(opcodes::All::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
+               }
 
                fn get_shutdown_pubkey(&self) -> PublicKey {
                        let secp_ctx = Secp256k1::signing_only();
@@ -3299,7 +3315,6 @@ mod tests {
 
                        // These aren't set in the test vectors:
                        revocation_base_key: SecretKey::from_slice(&secp_ctx, &hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(),
-                       channel_monitor_claim_key: SecretKey::from_slice(&secp_ctx, &hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(),
                        commitment_seed: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
                };
                assert_eq!(PublicKey::from_secret_key(&secp_ctx, &chan_keys.funding_key).serialize()[..],