Simplify transaction builder tests

Requires impl PartialEq for Transaction, which is implemented as a TxId
comparison (relying on the invariant that Transaction is immutable).
This commit is contained in:
Jack Grigg
2019-08-13 15:24:08 +01:00
parent 3a73f946c5
commit 1760b275a7
2 changed files with 35 additions and 40 deletions

View File

@@ -131,6 +131,7 @@ impl SaplingOutput {
} }
/// Metadata about a transaction created by a [`Builder`]. /// Metadata about a transaction created by a [`Builder`].
#[derive(Debug, PartialEq)]
pub struct TransactionMetadata { pub struct TransactionMetadata {
spend_indices: Vec<usize>, spend_indices: Vec<usize>,
output_indices: Vec<usize>, output_indices: Vec<usize>,
@@ -554,22 +555,22 @@ mod tests {
let to = extfvk.default_address().unwrap().1; let to = extfvk.default_address().unwrap().1;
let mut builder = Builder::new(0); let mut builder = Builder::new(0);
match builder.add_sapling_output(ovk, to, Amount::from_i64(-1).unwrap(), None) { assert_eq!(
Err(e) => assert_eq!(e, Error::InvalidAmount), builder.add_sapling_output(ovk, to, Amount::from_i64(-1).unwrap(), None),
Ok(_) => panic!("Should have failed"), Err(Error::InvalidAmount)
} );
} }
#[test] #[test]
fn fails_on_negative_transparent_output() { fn fails_on_negative_transparent_output() {
let mut builder = Builder::new(0); let mut builder = Builder::new(0);
match builder.add_transparent_output( assert_eq!(
&TransparentAddress::PublicKey([0; 20]), builder.add_transparent_output(
Amount::from_i64(-1).unwrap(), &TransparentAddress::PublicKey([0; 20]),
) { Amount::from_i64(-1).unwrap(),
Err(e) => assert_eq!(e, Error::InvalidAmount), ),
Ok(_) => panic!("Should have failed"), Err(Error::InvalidAmount)
} );
} }
#[test] #[test]
@@ -583,13 +584,10 @@ mod tests {
// 0.0001 t-ZEC fee // 0.0001 t-ZEC fee
{ {
let builder = Builder::new(0); let builder = Builder::new(0);
match builder.build(1, MockTxProver) { assert_eq!(
Err(e) => assert_eq!( builder.build(1, MockTxProver),
e, Err(Error::ChangeIsNegative(Amount::from_i64(-10000).unwrap()))
Error::ChangeIsNegative(Amount::from_i64(-10000).unwrap()) );
),
Ok(_) => panic!("Should have failed"),
}
} }
let extfvk = ExtendedFullViewingKey::from(&extsk); let extfvk = ExtendedFullViewingKey::from(&extsk);
@@ -608,13 +606,10 @@ mod tests {
None, None,
) )
.unwrap(); .unwrap();
match builder.build(1, MockTxProver) { assert_eq!(
Err(e) => assert_eq!( builder.build(1, MockTxProver),
e, Err(Error::ChangeIsNegative(Amount::from_i64(-60000).unwrap()))
Error::ChangeIsNegative(Amount::from_i64(-60000).unwrap()) );
),
Ok(_) => panic!("Should have failed"),
}
} }
// Fail if there is only a transparent output // Fail if there is only a transparent output
@@ -627,13 +622,10 @@ mod tests {
Amount::from_u64(50000).unwrap(), Amount::from_u64(50000).unwrap(),
) )
.unwrap(); .unwrap();
match builder.build(1, MockTxProver) { assert_eq!(
Err(e) => assert_eq!( builder.build(1, MockTxProver),
e, Err(Error::ChangeIsNegative(Amount::from_i64(-60000).unwrap()))
Error::ChangeIsNegative(Amount::from_i64(-60000).unwrap()) );
),
Ok(_) => panic!("Should have failed"),
}
} }
let note1 = to let note1 = to
@@ -670,10 +662,10 @@ mod tests {
Amount::from_u64(20000).unwrap(), Amount::from_u64(20000).unwrap(),
) )
.unwrap(); .unwrap();
match builder.build(1, MockTxProver) { assert_eq!(
Err(e) => assert_eq!(e, Error::ChangeIsNegative(Amount::from_i64(-1).unwrap())), builder.build(1, MockTxProver),
Ok(_) => panic!("Should have failed"), Err(Error::ChangeIsNegative(Amount::from_i64(-1).unwrap()))
} );
} }
let note2 = to.create_note(1, Fs::random(&mut rng), &JUBJUB).unwrap(); let note2 = to.create_note(1, Fs::random(&mut rng), &JUBJUB).unwrap();
@@ -704,10 +696,7 @@ mod tests {
Amount::from_u64(20000).unwrap(), Amount::from_u64(20000).unwrap(),
) )
.unwrap(); .unwrap();
match builder.build(1, MockTxProver) { assert_eq!(builder.build(1, MockTxProver), Err(Error::BindingSig))
Err(e) => assert_eq!(e, Error::BindingSig),
Ok(_) => panic!("Should have failed"),
}
} }
} }
} }

View File

@@ -50,6 +50,12 @@ impl Deref for Transaction {
} }
} }
impl PartialEq for Transaction {
fn eq(&self, other: &Transaction) -> bool {
self.txid == other.txid
}
}
pub struct TransactionData { pub struct TransactionData {
pub overwintered: bool, pub overwintered: bool,
pub version: u32, pub version: u32,