Dont return an Err from Channel::get_accept_channel that can't fail
[rust-lightning] / src / ln / channel.rs
index f890f03968903b137b99e34db09f6f4e4f4890ee..8dcd23b785ec811e6ccda83078bd1165506bc35d 100644 (file)
@@ -1133,9 +1133,6 @@ impl Channel {
                let mut htlc_amount_msat = 0;
                for htlc in self.pending_htlcs.iter_mut() {
                        if !htlc.outbound && htlc.payment_hash == *payment_hash_arg {
-                               if htlc_id != 0 {
-                                       panic!("Duplicate HTLC payment_hash, you probably re-used payment preimages, NEVER DO THIS!");
-                               }
                                if htlc.state == HTLCState::Committed {
                                        htlc.state = HTLCState::LocalRemoved;
                                } else if htlc.state == HTLCState::RemoteAnnounced {
@@ -1152,6 +1149,9 @@ impl Channel {
                                } else {
                                        panic!("Have an inbound HTLC when not awaiting remote revoke that had a garbage state");
                                }
+                               if htlc_id != 0 {
+                                       panic!("Duplicate HTLC payment_hash, you probably re-used payment preimages, NEVER DO THIS!");
+                               }
                                htlc_id = htlc.htlc_id;
                                htlc_amount_msat += htlc.amount_msat;
                        }
@@ -2002,6 +2002,12 @@ impl Channel {
                self.channel_value_satoshis
        }
 
+       //TODO: Testing purpose only, should be changed in another way after #81
+       #[cfg(test)]
+       pub fn get_local_keys(&self) -> &ChannelKeys {
+               &self.local_keys
+       }
+
        /// Allowed in any state (including after shutdown)
        pub fn get_channel_update_count(&self) -> u32 {
                self.channel_update_count
@@ -2113,7 +2119,16 @@ impl Channel {
                                if tx.txid() == self.channel_monitor.get_funding_txo().unwrap().txid {
                                        let txo_idx = self.channel_monitor.get_funding_txo().unwrap().index as usize;
                                        if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.get_funding_redeemscript().to_v0_p2wsh() ||
-                                               tx.output[txo_idx].value != self.channel_value_satoshis {
+                                                       tx.output[txo_idx].value != self.channel_value_satoshis {
+                                               if self.channel_outbound {
+                                                       // If we generated the funding transaction and it doesn't match what it
+                                                       // should, the client is really broken and we should just panic and
+                                                       // tell them off. That said, because hash collisions happen with high
+                                                       // probability in fuzztarget mode, if we're fuzzing we just close the
+                                                       // channel and move on.
+                                                       #[cfg(not(feature = "fuzztarget"))]
+                                                       panic!("Client called ChannelManager::funding_transaction_generated with bogus transaction!");
+                                               }
                                                self.channel_state = ChannelState::ShutdownComplete as u32;
                                                self.channel_update_count += 1;
                                                return Err(HandleError{err: "funding tx had wrong script/value", action: Some(ErrorAction::DisconnectPeer{msg: None})});
@@ -2185,7 +2200,7 @@ impl Channel {
                })
        }
 
-       pub fn get_accept_channel(&self) -> Result<msgs::AcceptChannel, HandleError> {
+       pub fn get_accept_channel(&self) -> msgs::AcceptChannel {
                if self.channel_outbound {
                        panic!("Tried to send accept_channel for an outbound channel?");
                }
@@ -2198,7 +2213,7 @@ impl Channel {
 
                let local_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
 
-               Ok(msgs::AcceptChannel {
+               msgs::AcceptChannel {
                        temporary_channel_id: self.channel_id,
                        dust_limit_satoshis: self.our_dust_limit_satoshis,
                        max_htlc_value_in_flight_msat: Channel::get_our_max_htlc_value_in_flight_msat(self.channel_value_satoshis),
@@ -2214,7 +2229,7 @@ impl Channel {
                        htlc_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key),
                        first_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &local_commitment_secret),
                        shutdown_scriptpubkey: None,
-               })
+               }
        }
 
        fn get_outbound_funding_created_signature(&mut self) -> Result<(Signature, Transaction), HandleError> {