Rename InputMaterial script to witness_script
authorAntoine Riard <ariard@student.42.fr>
Mon, 2 Mar 2020 23:12:18 +0000 (18:12 -0500)
committerAntoine Riard <ariard@student.42.fr>
Wed, 4 Mar 2020 21:06:31 +0000 (16:06 -0500)
lightning/src/ln/channelmonitor.rs
lightning/src/ln/onchaintx.rs

index 67e306b462897843374a88e7cdbb6b9976908fc2..ea9e7851e647afc385b3d789c300e18d72f8bace 100644 (file)
@@ -451,21 +451,21 @@ struct LocalSignedTx {
 #[derive(Clone, PartialEq)]
 pub(crate) enum InputMaterial {
        Revoked {
-               script: Script,
+               witness_script: Script,
                pubkey: Option<PublicKey>,
                key: SecretKey,
                is_htlc: bool,
                amount: u64,
        },
        RemoteHTLC {
-               script: Script,
+               witness_script: Script,
                key: SecretKey,
                preimage: Option<PaymentPreimage>,
                amount: u64,
                locktime: u32,
        },
        LocalHTLC {
-               script: Script,
+               witness_script: Script,
                sigs: (Signature, Signature),
                preimage: Option<PaymentPreimage>,
                amount: u64,
@@ -475,25 +475,25 @@ pub(crate) enum InputMaterial {
 impl Writeable for InputMaterial  {
        fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
                match self {
-                       &InputMaterial::Revoked { ref script, ref pubkey, ref key, ref is_htlc, ref amount} => {
+                       &InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref is_htlc, ref amount} => {
                                writer.write_all(&[0; 1])?;
-                               script.write(writer)?;
+                               witness_script.write(writer)?;
                                pubkey.write(writer)?;
                                writer.write_all(&key[..])?;
                                is_htlc.write(writer)?;
                                writer.write_all(&byte_utils::be64_to_array(*amount))?;
                        },
-                       &InputMaterial::RemoteHTLC { ref script, ref key, ref preimage, ref amount, ref locktime } => {
+                       &InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => {
                                writer.write_all(&[1; 1])?;
-                               script.write(writer)?;
+                               witness_script.write(writer)?;
                                key.write(writer)?;
                                preimage.write(writer)?;
                                writer.write_all(&byte_utils::be64_to_array(*amount))?;
                                writer.write_all(&byte_utils::be32_to_array(*locktime))?;
                        },
-                       &InputMaterial::LocalHTLC { ref script, ref sigs, ref preimage, ref amount } => {
+                       &InputMaterial::LocalHTLC { ref witness_script, ref sigs, ref preimage, ref amount } => {
                                writer.write_all(&[2; 1])?;
-                               script.write(writer)?;
+                               witness_script.write(writer)?;
                                sigs.0.write(writer)?;
                                sigs.1.write(writer)?;
                                preimage.write(writer)?;
@@ -508,13 +508,13 @@ impl Readable for InputMaterial {
        fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
                let input_material = match <u8 as Readable>::read(reader)? {
                        0 => {
-                               let script = Readable::read(reader)?;
+                               let witness_script = Readable::read(reader)?;
                                let pubkey = Readable::read(reader)?;
                                let key = Readable::read(reader)?;
                                let is_htlc = Readable::read(reader)?;
                                let amount = Readable::read(reader)?;
                                InputMaterial::Revoked {
-                                       script,
+                                       witness_script,
                                        pubkey,
                                        key,
                                        is_htlc,
@@ -522,13 +522,13 @@ impl Readable for InputMaterial {
                                }
                        },
                        1 => {
-                               let script = Readable::read(reader)?;
+                               let witness_script = Readable::read(reader)?;
                                let key = Readable::read(reader)?;
                                let preimage = Readable::read(reader)?;
                                let amount = Readable::read(reader)?;
                                let locktime = Readable::read(reader)?;
                                InputMaterial::RemoteHTLC {
-                                       script,
+                                       witness_script,
                                        key,
                                        preimage,
                                        amount,
@@ -536,13 +536,13 @@ impl Readable for InputMaterial {
                                }
                        },
                        2 => {
-                               let script = Readable::read(reader)?;
+                               let witness_script = Readable::read(reader)?;
                                let their_sig = Readable::read(reader)?;
                                let our_sig = Readable::read(reader)?;
                                let preimage = Readable::read(reader)?;
                                let amount = Readable::read(reader)?;
                                InputMaterial::LocalHTLC {
-                                       script,
+                                       witness_script,
                                        sigs: (their_sig, our_sig),
                                        preimage,
                                        amount
@@ -1461,7 +1461,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        // First, process non-htlc outputs (to_local & to_remote)
                        for (idx, outp) in tx.output.iter().enumerate() {
                                if outp.script_pubkey == revokeable_p2wsh {
-                                       let witness_data = InputMaterial::Revoked { script: revokeable_redeemscript.clone(), pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: false, amount: outp.value };
+                                       let witness_data = InputMaterial::Revoked { witness_script: revokeable_redeemscript.clone(), pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: false, amount: outp.value };
                                        claimable_outpoints.push(ClaimRequest { absolute_timelock: height + self.our_to_self_delay as u32, aggregable: true, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 }, witness_data});
                                } else if Some(&outp.script_pubkey) == local_payment_p2wpkh.as_ref() {
                                        spendable_outputs.push(SpendableOutputDescriptor::DynamicOutputP2WPKH {
@@ -1482,7 +1482,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                                                                tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
                                                        return (claimable_outpoints, (commitment_txid, watch_outputs), spendable_outputs); // Corrupted per_commitment_data, fuck this user
                                                }
-                                               let witness_data = InputMaterial::Revoked { script: expected_script, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: true, amount: tx.output[transaction_output_index as usize].value };
+                                               let witness_data = InputMaterial::Revoked { witness_script: expected_script, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: true, amount: tx.output[transaction_output_index as usize].value };
                                                claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable: true, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: transaction_output_index }, witness_data });
                                        }
                                }
@@ -1646,7 +1646,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                                                        let preimage = if htlc.offered { if let Some(p) = self.payment_preimages.get(&htlc.payment_hash) { Some(*p) } else { None } } else { None };
                                                        let aggregable = if !htlc.offered { false } else { true };
                                                        if preimage.is_some() || !htlc.offered {
-                                                               let witness_data = InputMaterial::RemoteHTLC { script: expected_script, key: htlc_privkey, preimage, amount: htlc.amount_msat / 1000, locktime: htlc.cltv_expiry };
+                                                               let witness_data = InputMaterial::RemoteHTLC { witness_script: expected_script, key: htlc_privkey, preimage, amount: htlc.amount_msat / 1000, locktime: htlc.cltv_expiry };
                                                                claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: transaction_output_index }, witness_data });
                                                        }
                                                }
@@ -1701,7 +1701,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                let htlc_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
 
                log_trace!(self, "Remote HTLC broadcast {}:{}", htlc_txid, 0);
-               let witness_data = InputMaterial::Revoked { script: redeemscript, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: false, amount: tx.output[0].value };
+               let witness_data = InputMaterial::Revoked { witness_script: redeemscript, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: false, amount: tx.output[0].value };
                let claimable_outpoints = vec!(ClaimRequest { absolute_timelock: height + self.our_to_self_delay as u32, aggregable: true, outpoint: BitcoinOutPoint { txid: htlc_txid, vout: 0}, witness_data });
                claimable_outpoints
        }
@@ -1749,7 +1749,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
 
                                                        add_dynamic_output!(htlc_timeout_tx, 0);
                                                        let mut per_input_material = HashMap::with_capacity(1);
-                                                       per_input_material.insert(htlc_timeout_tx.input[0].previous_output, InputMaterial::LocalHTLC { script: htlc_script, sigs: (*their_sig, our_sig), preimage: None, amount: htlc.amount_msat / 1000});
+                                                       per_input_material.insert(htlc_timeout_tx.input[0].previous_output, InputMaterial::LocalHTLC { witness_script: htlc_script, sigs: (*their_sig, our_sig), preimage: None, amount: htlc.amount_msat / 1000});
                                                        //TODO: with option_simplified_commitment track outpoint too
                                                        log_trace!(self, "Outpoint {}:{} is being being claimed", htlc_timeout_tx.input[0].previous_output.vout, htlc_timeout_tx.input[0].previous_output.txid);
                                                        res.push(htlc_timeout_tx);
@@ -1765,7 +1765,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
 
                                                                add_dynamic_output!(htlc_success_tx, 0);
                                                                let mut per_input_material = HashMap::with_capacity(1);
-                                                               per_input_material.insert(htlc_success_tx.input[0].previous_output, InputMaterial::LocalHTLC { script: htlc_script, sigs: (*their_sig, our_sig), preimage: Some(*payment_preimage), amount: htlc.amount_msat / 1000});
+                                                               per_input_material.insert(htlc_success_tx.input[0].previous_output, InputMaterial::LocalHTLC { witness_script: htlc_script, sigs: (*their_sig, our_sig), preimage: Some(*payment_preimage), amount: htlc.amount_msat / 1000});
                                                                //TODO: with option_simplified_commitment track outpoint too
                                                                log_trace!(self, "Outpoint {}:{} is being being claimed", htlc_success_tx.input[0].previous_output.vout, htlc_success_tx.input[0].previous_output.txid);
                                                                res.push(htlc_success_tx);
index bbab04ac8c711d1b05540897b57155de1db01061..1ec68f5eb4d5d1642c41916565fec7d9bc5a3fa1 100644 (file)
@@ -402,8 +402,8 @@ impl OnchainTxHandler {
                let mut amt = 0;
                for per_outp_material in cached_claim_datas.per_input_material.values() {
                        match per_outp_material {
-                               &InputMaterial::Revoked { ref script, ref is_htlc, ref amount, .. } => {
-                                       inputs_witnesses_weight += Self::get_witnesses_weight(if !is_htlc { &[InputDescriptors::RevokedOutput] } else if HTLCType::scriptlen_to_htlctype(script.len()) == Some(HTLCType::OfferedHTLC) { &[InputDescriptors::RevokedOfferedHTLC] } else if HTLCType::scriptlen_to_htlctype(script.len()) == Some(HTLCType::AcceptedHTLC) { &[InputDescriptors::RevokedReceivedHTLC] } else { unreachable!() });
+                               &InputMaterial::Revoked { ref witness_script, ref is_htlc, ref amount, .. } => {
+                                       inputs_witnesses_weight += Self::get_witnesses_weight(if !is_htlc { &[InputDescriptors::RevokedOutput] } else if HTLCType::scriptlen_to_htlctype(witness_script.len()) == Some(HTLCType::OfferedHTLC) { &[InputDescriptors::RevokedOfferedHTLC] } else if HTLCType::scriptlen_to_htlctype(witness_script.len()) == Some(HTLCType::AcceptedHTLC) { &[InputDescriptors::RevokedReceivedHTLC] } else { unreachable!() });
                                        amt += *amount;
                                },
                                &InputMaterial::RemoteHTLC { ref preimage, ref amount, .. } => {
@@ -436,9 +436,9 @@ impl OnchainTxHandler {
 
                for (i, (outp, per_outp_material)) in cached_claim_datas.per_input_material.iter().enumerate() {
                        match per_outp_material {
-                               &InputMaterial::Revoked { ref script, ref pubkey, ref key, ref is_htlc, ref amount } => {
+                               &InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref is_htlc, ref amount } => {
                                        let sighash_parts = bip143::SighashComponents::new(&bumped_tx);
-                                       let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[i], &script, *amount)[..]);
+                                       let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[i], &witness_script, *amount)[..]);
                                        let sig = self.secp_ctx.sign(&sighash, &key);
                                        bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
                                        bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
@@ -447,13 +447,13 @@ impl OnchainTxHandler {
                                        } else {
                                                bumped_tx.input[i].witness.push(vec!(1));
                                        }
-                                       bumped_tx.input[i].witness.push(script.clone().into_bytes());
-                                       log_trace!(self, "Going to broadcast Penalty Transaction {} claiming revoked {} output {} from {} with new feerate {}...", bumped_tx.txid(), if !is_htlc { "to_local" } else if HTLCType::scriptlen_to_htlctype(script.len()) == Some(HTLCType::OfferedHTLC) { "offered" } else if HTLCType::scriptlen_to_htlctype(script.len()) == Some(HTLCType::AcceptedHTLC) { "received" } else { "" }, outp.vout, outp.txid, new_feerate);
+                                       bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
+                                       log_trace!(self, "Going to broadcast Penalty Transaction {} claiming revoked {} output {} from {} with new feerate {}...", bumped_tx.txid(), if !is_htlc { "to_local" } else if HTLCType::scriptlen_to_htlctype(witness_script.len()) == Some(HTLCType::OfferedHTLC) { "offered" } else if HTLCType::scriptlen_to_htlctype(witness_script.len()) == Some(HTLCType::AcceptedHTLC) { "received" } else { "" }, outp.vout, outp.txid, new_feerate);
                                },
-                               &InputMaterial::RemoteHTLC { ref script, ref key, ref preimage, ref amount, ref locktime } => {
+                               &InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => {
                                        if !preimage.is_some() { bumped_tx.lock_time = *locktime }; // Right now we don't aggregate time-locked transaction, if we do we should set lock_time before to avoid breaking hash computation
                                        let sighash_parts = bip143::SighashComponents::new(&bumped_tx);
-                                       let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[i], &script, *amount)[..]);
+                                       let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[i], &witness_script, *amount)[..]);
                                        let sig = self.secp_ctx.sign(&sighash, &key);
                                        bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
                                        bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
@@ -462,7 +462,7 @@ impl OnchainTxHandler {
                                        } else {
                                                bumped_tx.input[i].witness.push(vec![0]);
                                        }
-                                       bumped_tx.input[i].witness.push(script.clone().into_bytes());
+                                       bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
                                        log_trace!(self, "Going to broadcast Claim Transaction {} claiming remote {} htlc output {} from {} with new feerate {}...", bumped_tx.txid(), if preimage.is_some() { "offered" } else { "received" }, outp.vout, outp.txid, new_feerate);
                                },
                                &InputMaterial::LocalHTLC { .. } => {