Track local_commitment_tx at funding_created
authorAntoine Riard <ariard@student.42.fr>
Wed, 31 Oct 2018 02:49:19 +0000 (02:49 +0000)
committerAntoine Riard <ariard@student.42.fr>
Fri, 16 Nov 2018 00:07:34 +0000 (19:07 -0500)
Goal to claim sizeable push_msat and in event of
local commitment tx being broadcast without htlcs

src/ln/channel.rs

index 207eda4183f7a8993c5ff0278df605637f59c4a0..9b8db2fe3d37ab284ebc4a8f9265de986e82f7e4 100644 (file)
@@ -1371,22 +1371,25 @@ impl Channel {
                Ok(())
        }
 
-       fn funding_created_signature(&mut self, sig: &Signature) -> Result<(Transaction, Signature), HandleError> {
+       fn funding_created_signature(&mut self, sig: &Signature) -> Result<(Transaction, Transaction, Signature, TxCreationKeys), HandleError> {
                let funding_script = self.get_funding_redeemscript();
 
                let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
-               let local_initial_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, self.feerate_per_kw).0;
+               let mut local_initial_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, self.feerate_per_kw).0;
                let local_sighash = Message::from_slice(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
 
-               // They sign the "local" commitment transaction, allowing us to broadcast the tx if we wish.
+               // They sign the "local" commitment transaction...
                secp_call!(self.secp_ctx.verify(&local_sighash, &sig, &self.their_funding_pubkey.unwrap()), "Invalid funding_created signature from peer", self.channel_id());
 
+               // ...and we sign it, allowing us to broadcast the tx if we wish
+               self.sign_commitment_transaction(&mut local_initial_commitment_tx, sig);
+
                let remote_keys = self.build_remote_transaction_keys()?;
                let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false, self.feerate_per_kw).0;
                let remote_sighash = Message::from_slice(&bip143::SighashComponents::new(&remote_initial_commitment_tx).sighash_all(&remote_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
 
                // We sign the "remote" commitment transaction, allowing them to broadcast the tx if they wish.
-               Ok((remote_initial_commitment_tx, self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key)))
+               Ok((remote_initial_commitment_tx, local_initial_commitment_tx, self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key), local_keys))
        }
 
        pub fn funding_created(&mut self, msg: &msgs::FundingCreated) -> Result<(msgs::FundingSigned, ChannelMonitor), HandleError> {
@@ -1409,7 +1412,7 @@ impl Channel {
                let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
                self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
 
-               let (remote_initial_commitment_tx, our_signature) = match self.funding_created_signature(&msg.signature) {
+               let (remote_initial_commitment_tx, local_initial_commitment_tx, our_signature, local_keys) = match self.funding_created_signature(&msg.signature) {
                        Ok(res) => res,
                        Err(e) => {
                                self.channel_monitor.unset_funding_info();
@@ -1420,6 +1423,8 @@ impl Channel {
                // Now that we're past error-generating stuff, update our local state:
 
                self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number);
+               self.last_local_commitment_txn = vec![local_initial_commitment_tx.clone()];
+               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx, local_keys, self.feerate_per_kw, Vec::new());
                self.channel_state = ChannelState::FundingSent as u32;
                self.channel_id = funding_txo.to_channel_id();
                self.cur_remote_commitment_transaction_number -= 1;