From 7f7013db9c481f2b94185908a40bc7816d11a9e8 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Mon, 1 Oct 2018 20:38:50 +0000 Subject: [PATCH] fuzz: check that transaction deserialization roundtrips --- fuzz/fuzz_targets/deserialize_transaction.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/fuzz/fuzz_targets/deserialize_transaction.rs b/fuzz/fuzz_targets/deserialize_transaction.rs index a3b251d6..7f4a5d04 100644 --- a/fuzz/fuzz_targets/deserialize_transaction.rs +++ b/fuzz/fuzz_targets/deserialize_transaction.rs @@ -5,13 +5,22 @@ fn do_test(data: &[u8]) { match tx_result { Err(_) => {}, 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(); for input in &mut tx.input { input.witness = vec![]; } let no_witness_len = bitcoin::consensus::encode::serialize(&tx).len() as u64; - assert_eq!(no_witness_len * 3 + len, calculated_weight); + // 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); + } }, } } @@ -58,7 +67,7 @@ mod tests { #[test] fn duplicate_crash() { let mut a = Vec::new(); - extend_vec_from_hex("00", &mut a); + extend_vec_from_hex("000700000001000000010000", &mut a); super::do_test(&a); } }