FakeTransaction test helper

This commit is contained in:
Aditya Kulkarni
2019-09-23 10:48:40 -07:00
parent 3b1f46e533
commit cd113749a5

View File

@@ -1111,6 +1111,7 @@ pub mod tests {
use super::LightWallet; use super::LightWallet;
use crate::LightClientConfig; use crate::LightClientConfig;
use secp256k1::PublicKey;
struct FakeCompactBlock { struct FakeCompactBlock {
block: CompactBlock, block: CompactBlock,
@@ -1265,6 +1266,55 @@ pub mod tests {
} }
} }
struct FakeTransaction {
tx: Transaction,
}
impl FakeTransaction {
// New FakeTransaction with random txid
fn new<R: RngCore>(rng: &mut R) -> Self {
let mut txid = [0u8; 32];
rng.fill_bytes(&mut txid);
FakeTransaction::new_with_txid(TxId(txid))
}
fn new_with_txid(txid: TxId) -> Self {
FakeTransaction {
tx: Transaction {
txid,
data: TransactionData::new()
}
}
}
fn get_tx(&self) -> &Transaction {
&self.tx
}
fn add_t_output(&mut self, pk: &PublicKey, value: u64) {
let mut hash160 = ripemd160::Ripemd160::new();
hash160.input(Sha256::digest(&pk.serialize()[..].to_vec()));
let taddr_bytes = hash160.result();
self.tx.data.vout.push(TxOut {
value: Amount::from_u64(value).unwrap(),
script_pubkey: TransparentAddress::PublicKey(taddr_bytes.try_into().unwrap()).script(),
});
}
fn add_t_input(&mut self, txid: TxId, n: u32) {
self.tx.data.vin.push(TxIn {
prevout: OutPoint{
hash: txid.0,
n
},
script_sig: Script{0: vec![]},
sequence: 0,
});
}
}
#[test] #[test]
fn z_balances() { fn z_balances() {
let wallet = LightWallet::new(None, &LightClientConfig { let wallet = LightWallet::new(None, &LightClientConfig {
@@ -1369,31 +1419,16 @@ pub mod tests {
sapling_activation_height: 0 sapling_activation_height: 0
}).unwrap(); }).unwrap();
let pk = secp256k1::PublicKey::from_secret_key(&secp, &wallet.tkeys[0]); let pk = PublicKey::from_secret_key(&secp, &wallet.tkeys[0]);
let mut hash160 = ripemd160::Ripemd160::new();
hash160.input(Sha256::digest(&pk.serialize()[..].to_vec()));
let taddr_bytes = hash160.result();
let taddr = wallet.address_from_sk(&wallet.tkeys[0]); let taddr = wallet.address_from_sk(&wallet.tkeys[0]);
let mut txid = vec![0; 32];
rng.fill_bytes(&mut txid);
let txid1 = TxId{0: txid[..].try_into().unwrap()};
const AMOUNT1: u64 = 20; const AMOUNT1: u64 = 20;
let mut td = TransactionData::new(); let mut tx = FakeTransaction::new(&mut rng);
td.vout.push(TxOut{ tx.add_t_output(&pk, AMOUNT1);
value: Amount::from_u64(AMOUNT1).unwrap(), let txid1 = tx.get_tx().txid();
script_pubkey: TransparentAddress::PublicKey(taddr_bytes.try_into().unwrap()).script(),
});
let tx = Transaction{ wallet.scan_full_tx(&tx.get_tx(), 100); // Pretend it is at height 100
txid: txid1,
data: td,
};
wallet.scan_full_tx(&tx, 100); // Pretent it is at height 100
{ {
let txs = wallet.txs.read().unwrap(); let txs = wallet.txs.read().unwrap();
@@ -1414,27 +1449,11 @@ pub mod tests {
} }
// Create a new Tx, spending this taddr // Create a new Tx, spending this taddr
let mut td = TransactionData::new(); let mut tx = FakeTransaction::new(&mut rng);
td.vin.push(TxIn{ tx.add_t_input(txid1, 0);
prevout: OutPoint{ let txid2 = tx.get_tx().txid();
hash: txid1.0,
n: 0
},
script_sig: Script{0: vec![]},
sequence: 0,
});
// Create a new txid
let mut txid = vec![0; 32];
rng.fill_bytes(&mut txid);
let txid2 = TxId{0: txid[..].try_into().unwrap()};
let tx = Transaction{ wallet.scan_full_tx(&tx.get_tx(), 101); // Pretent it is at height 101
txid: txid2,
data: td,
};
wallet.scan_full_tx(&tx, 101); // Pretent it is at height 101
{ {
// Make sure the txid was spent // Make sure the txid was spent
@@ -1455,6 +1474,5 @@ pub mod tests {
// Make sure there is no t-ZEC left // Make sure there is no t-ZEC left
assert_eq!(wallet.tbalance(None), 0); assert_eq!(wallet.tbalance(None), 0);
} }
} }
} }