Check transparent input for correctness before modifying vin.

This commit is contained in:
Kris Nuttycombe 2020-06-02 16:20:44 -06:00
parent 4a954c7f8f
commit fe6bea1fce
3 changed files with 21 additions and 16 deletions

View File

@ -419,17 +419,14 @@ impl<'a, B: ExtensionTxBuilder<'a>> DemoBuilder<&mut B> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use ff::{Field, PrimeField};
use blake2b_simd::Params; use blake2b_simd::Params;
use ff::{Field, PrimeField};
use rand_core::OsRng; use rand_core::OsRng;
use zcash_proofs::prover::LocalTxProver; use zcash_proofs::prover::LocalTxProver;
use zcash_primitives::{ use zcash_primitives::{
consensus::{ consensus::{BranchId, TestNetwork},
BranchId,
TestNetwork,
},
extensions::transparent::{self as tze, Extension, FromPayload, ToPayload}, extensions::transparent::{self as tze, Extension, FromPayload, ToPayload},
legacy::TransparentAddress, legacy::TransparentAddress,
merkle_tree::{CommitmentTree, IncrementalWitness}, merkle_tree::{CommitmentTree, IncrementalWitness},
@ -445,7 +442,6 @@ mod tests {
use super::{close, open, Context, DemoBuilder, Precondition, Program, Witness}; use super::{close, open, Context, DemoBuilder, Precondition, Program, Witness};
#[test] #[test]
fn precondition_open_round_trip() { fn precondition_open_round_trip() {
let data = vec![7; 32]; let data = vec![7; 32];
@ -673,7 +669,10 @@ mod tests {
txn_builder: &mut builder_b, txn_builder: &mut builder_b,
extension_id: 0, extension_id: 0,
}; };
let prevout_a = (OutPoint::new(tx_a.txid().0, 0), tx_a.data.tze_outputs[0].clone()); let prevout_a = (
OutPoint::new(tx_a.txid().0, 0),
tx_a.data.tze_outputs[0].clone(),
);
let value_xfr = Amount::from_u64(90000).unwrap(); let value_xfr = Amount::from_u64(90000).unwrap();
db_b.demo_transfer_to_close(prevout_a, value_xfr, preimage_1, preimage_2) db_b.demo_transfer_to_close(prevout_a, value_xfr, preimage_1, preimage_2)
.map_err(|e| format!("transfer failure: {:?}", e)) .map_err(|e| format!("transfer failure: {:?}", e))
@ -692,13 +691,20 @@ mod tests {
txn_builder: &mut builder_c, txn_builder: &mut builder_c,
extension_id: 0, extension_id: 0,
}; };
let prevout_b = (OutPoint::new(tx_a.txid().0, 0), tx_b.data.tze_outputs[0].clone()); let prevout_b = (
OutPoint::new(tx_a.txid().0, 0),
tx_b.data.tze_outputs[0].clone(),
);
db_c.demo_close(prevout_b, preimage_2) db_c.demo_close(prevout_b, preimage_2)
.map_err(|e| format!("close failure: {:?}", e)) .map_err(|e| format!("close failure: {:?}", e))
.unwrap(); .unwrap();
builder_c.add_transparent_output(&TransparentAddress::PublicKey([0; 20]), Amount::from_u64(80000).unwrap()) builder_c
.unwrap(); .add_transparent_output(
&TransparentAddress::PublicKey([0; 20]),
Amount::from_u64(80000).unwrap(),
)
.unwrap();
let (tx_c, _) = builder_c let (tx_c, _) = builder_c
.build(BranchId::Canopy, &prover) .build(BranchId::Canopy, &prover)

View File

@ -478,8 +478,9 @@ impl<'a, P: consensus::Parameters, R: RngCore + CryptoRng> Builder<'a, P, R> {
utxo: OutPoint, utxo: OutPoint,
coin: TxOut, coin: TxOut,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.transparent_inputs.push(sk, coin)?;
self.mtx.vin.push(TxIn::new(utxo)); self.mtx.vin.push(TxIn::new(utxo));
self.transparent_inputs.push(sk, coin) Ok(());
} }
/// Adds a transparent address to send funds to. /// Adds a transparent address to send funds to.
@ -529,9 +530,7 @@ impl<'a, P: consensus::Parameters, R: RngCore + CryptoRng> Builder<'a, P, R> {
// //
// Valid change // Valid change
let change = self.mtx.value_balance let change = self.mtx.value_balance - self.fee + self.transparent_inputs.value_sum()
- self.fee
+ self.transparent_inputs.value_sum()
- self.mtx.vout.iter().map(|vo| vo.value).sum::<Amount>() - self.mtx.vout.iter().map(|vo| vo.value).sum::<Amount>()
+ self + self
.tze_inputs .tze_inputs

View File

@ -5,8 +5,8 @@ use group::GroupEncoding;
use super::{ use super::{
components::{Amount, TxOut}, components::{Amount, TxOut},
Transaction, TransactionData, OVERWINTER_VERSION_GROUP_ID, SAPLING_TX_VERSION, Transaction, TransactionData, FUTURE_VERSION_GROUP_ID, OVERWINTER_VERSION_GROUP_ID,
SAPLING_VERSION_GROUP_ID, FUTURE_VERSION_GROUP_ID, SAPLING_TX_VERSION, SAPLING_VERSION_GROUP_ID,
}; };
use crate::{consensus, legacy::Script}; use crate::{consensus, legacy::Script};