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); } }