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

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 overwintered: bool,
pub version: u32,