Merge pull request #1717 from TheBlueMatt/2022-09-req-features-in-handlers
[rust-lightning] / lightning / src / ln / channel.rs
index fdd073f632a8d4b978453a55dc6c98b7f277e141..c8209a9eac6f984f76514987729d9ca04dde192f 100644 (file)
@@ -3550,6 +3550,12 @@ impl<Signer: Sign> Channel<Signer> {
                        return;
                }
 
+               if self.channel_state & (ChannelState::PeerDisconnected as u32) == (ChannelState::PeerDisconnected as u32) {
+                       // While the below code should be idempotent, it's simpler to just return early, as
+                       // redundant disconnect events can fire, though they should be rare.
+                       return;
+               }
+
                if self.announcement_sigs_state == AnnouncementSigsState::MessageSent || self.announcement_sigs_state == AnnouncementSigsState::Committed {
                        self.announcement_sigs_state = AnnouncementSigsState::NotSent;
                }
@@ -4771,6 +4777,9 @@ impl<Signer: Sign> Channel<Signer> {
        }
 
        fn check_get_channel_ready(&mut self, height: u32) -> Option<msgs::ChannelReady> {
+               // Called:
+               //  * always when a new block/transactions are confirmed with the new height
+               //  * when funding is signed with a height of 0
                if self.funding_tx_confirmation_height == 0 && self.minimum_depth != Some(0) {
                        return None;
                }
@@ -4796,7 +4805,7 @@ impl<Signer: Sign> Channel<Signer> {
                        // We got a reorg but not enough to trigger a force close, just ignore.
                        false
                } else {
-                       if self.channel_state < ChannelState::ChannelFunded as u32 {
+                       if self.funding_tx_confirmation_height != 0 && self.channel_state < ChannelState::ChannelFunded as u32 {
                                // We should never see a funding transaction on-chain until we've received
                                // funding_signed (if we're an outbound channel), or seen funding_generated (if we're
                                // an inbound channel - before that we have no known funding TXID). The fuzzer,
@@ -6603,7 +6612,7 @@ mod tests {
        use util::errors::APIError;
        use util::test_utils;
        use util::test_utils::OnGetShutdownScriptpubkey;
-       use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
+       use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature, Scalar};
        use bitcoin::secp256k1::ffi::Signature as FFISignature;
        use bitcoin::secp256k1::{SecretKey,PublicKey};
        use bitcoin::secp256k1::ecdh::SharedSecret;
@@ -6612,6 +6621,7 @@ mod tests {
        use bitcoin::hashes::Hash;
        use bitcoin::hash_types::WPubkeyHash;
        use bitcoin::bech32::u5;
+       use bitcoin::PackedLockTime;
        use bitcoin::util::address::WitnessVersion;
        use prelude::*;
 
@@ -6647,7 +6657,7 @@ mod tests {
                type Signer = InMemorySigner;
 
                fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> { panic!(); }
-               fn ecdh(&self, _recipient: Recipient, _other_key: &PublicKey, _tweak: Option<&[u8; 32]>) -> Result<SharedSecret, ()> { panic!(); }
+               fn ecdh(&self, _recipient: Recipient, _other_key: &PublicKey, _tweak: Option<&Scalar>) -> Result<SharedSecret, ()> { panic!(); }
                fn get_inbound_payment_key_material(&self) -> KeyMaterial { panic!(); }
                fn get_destination_script(&self) -> Script {
                        let secp_ctx = Secp256k1::signing_only();
@@ -6872,7 +6882,7 @@ mod tests {
 
                // Node A --> Node B: funding created
                let output_script = node_a_chan.get_funding_redeemscript();
-               let tx = Transaction { version: 1, lock_time: 0, input: Vec::new(), output: vec![TxOut {
+               let tx = Transaction { version: 1, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: vec![TxOut {
                        value: 10000000, script_pubkey: output_script.clone(),
                }]};
                let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };