diff --git a/chain/src/indexed_block.rs b/chain/src/indexed_block.rs index 22ac74b0..7297bcf5 100644 --- a/chain/src/indexed_block.rs +++ b/chain/src/indexed_block.rs @@ -84,3 +84,18 @@ impl From<&'static str> for IndexedBlock { deserialize(&s.from_hex().unwrap() as &[u8]).unwrap() } } + +#[cfg(test)] +mod tests { + use super::IndexedBlock; + + #[test] + fn size_with_witness_not_equal_to_size() { + let block_without_witness: IndexedBlock = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".into(); + assert_eq!(block_without_witness.size(), block_without_witness.size_with_witness()); + + // bip143 block + let block_with_witness: IndexedBlock = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000010100000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000000000".into(); + assert!(block_with_witness.size() != block_with_witness.size_with_witness()); + } +} diff --git a/chain/src/transaction.rs b/chain/src/transaction.rs index 2fcf87c7..b84dd0ed 100644 --- a/chain/src/transaction.rs +++ b/chain/src/transaction.rs @@ -262,7 +262,7 @@ impl Deserializable for Transaction { #[cfg(test)] mod tests { use hash::H256; - use ser::Serializable; + use ser::{Serializable, serialize_with_flags, SERIALIZE_TRANSACTION_WITNESS}; use super::{Transaction, TransactionInput, OutPoint, TransactionOutput}; // real transaction from block 80000 @@ -335,4 +335,13 @@ mod tests { }; assert_eq!(actual, expected); } + + #[test] + fn test_serialization_with_flags() { + let transaction_without_witness: Transaction = "000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".into(); + assert_eq!(serialize_with_flags(&transaction_without_witness, 0), serialize_with_flags(&transaction_without_witness, SERIALIZE_TRANSACTION_WITNESS)); + + let transaction_with_witness: Transaction = "0000000000010100000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000000000".into(); + assert!(serialize_with_flags(&transaction_with_witness, 0) != serialize_with_flags(&transaction_with_witness, SERIALIZE_TRANSACTION_WITNESS)); + } }