fuzz: check that transaction deserialization roundtrips

This commit is contained in:
Andrew Poelstra 2018-10-01 20:38:50 +00:00
parent a181a523c6
commit 7f7013db9c
1 changed files with 12 additions and 3 deletions

View File

@ -5,13 +5,22 @@ fn do_test(data: &[u8]) {
match tx_result { match tx_result {
Err(_) => {}, Err(_) => {},
Ok(mut tx) => { Ok(mut tx) => {
let len = bitcoin::consensus::encode::serialize(&tx).len() as u64; let ser = bitcoin::consensus::encode::serialize(&tx);
assert_eq!(&ser[..], data);
let len = ser.len() as u64;
let calculated_weight = tx.get_weight(); let calculated_weight = tx.get_weight();
for input in &mut tx.input { for input in &mut tx.input {
input.witness = vec![]; input.witness = vec![];
} }
let no_witness_len = bitcoin::consensus::encode::serialize(&tx).len() as u64; let no_witness_len = bitcoin::consensus::encode::serialize(&tx).len() as u64;
// For 0-input transactions, `no_witness_len` will be incorrect because
// we serialize as segwit even after "stripping the witnesses". We need
// to drop two bytes (i.e. eight weight)
if tx.input.is_empty() {
assert_eq!(no_witness_len * 3 + len - 8, calculated_weight);
} else {
assert_eq!(no_witness_len * 3 + len, calculated_weight); assert_eq!(no_witness_len * 3 + len, calculated_weight);
}
}, },
} }
} }
@ -58,7 +67,7 @@ mod tests {
#[test] #[test]
fn duplicate_crash() { fn duplicate_crash() {
let mut a = Vec::new(); let mut a = Vec::new();
extend_vec_from_hex("00", &mut a); extend_vec_from_hex("000700000001000000010000", &mut a);
super::do_test(&a); super::do_test(&a);
} }
} }