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

View File

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

View File

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