From e9f1f11c2ce6512aae7f9b5918179e48970f6542 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 17 Aug 2020 10:28:33 -0700 Subject: [PATCH 1/2] Transaction and header version is signed int --- src/blockdata/block.rs | 3 +-- src/blockdata/transaction.rs | 7 +++---- src/util/bip143.rs | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/blockdata/block.rs b/src/blockdata/block.rs index 5a415594..989201db 100644 --- a/src/blockdata/block.rs +++ b/src/blockdata/block.rs @@ -37,7 +37,7 @@ use VarInt; #[derive(Copy, PartialEq, Eq, Clone, Debug)] pub struct BlockHeader { /// The protocol version. Should always be 1. - pub version: u32, + pub version: i32, /// Reference to the previous block in the chain pub prev_blockhash: BlockHash, /// The root hash of the merkle tree of transactions in the block @@ -320,4 +320,3 @@ mod tests { assert_eq!(header.bits, BlockHeader::compact_target_from_u256(&header.target())); } } - diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index 6b091f82..bc9cd2fb 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -260,7 +260,7 @@ impl Default for TxOut { #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct Transaction { /// The protocol version, is currently expected to be 1 or 2 (BIP 68). - pub version: u32, + pub version: i32, /// Block number before which this transaction is valid, or 0 for /// valid immediately. pub lock_time: u32, @@ -531,7 +531,7 @@ impl Encodable for Transaction { impl Decodable for Transaction { fn consensus_decode(mut d: D) -> Result { - let version = u32::consensus_decode(&mut d)?; + let version = i32::consensus_decode(&mut d)?; let input = Vec::::consensus_decode(&mut d)?; // segwit if input.is_empty() { @@ -662,7 +662,7 @@ mod tests { Err(ParseOutPointError::Txid(Txid::from_hex("5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c945X").unwrap_err()))); assert_eq!(OutPoint::from_str("5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456:lol"), Err(ParseOutPointError::Vout(u32::from_str("lol").unwrap_err()))); - + assert_eq!(OutPoint::from_str("5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456:42"), Ok(OutPoint{ txid: Txid::from_hex("5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456").unwrap(), @@ -1229,4 +1229,3 @@ mod tests { } } } - diff --git a/src/util/bip143.rs b/src/util/bip143.rs index 8b805e61..59e36961 100644 --- a/src/util/bip143.rs +++ b/src/util/bip143.rs @@ -30,7 +30,7 @@ use consensus::encode::Encodable; #[derive(Clone, PartialEq, Eq, Debug)] #[deprecated(since="0.24.0", note="please use `SigHashCache` instead")] pub struct SighashComponents { - tx_version: u32, + tx_version: i32, tx_locktime: u32, /// Hash of all the previous outputs pub hash_prevouts: SigHash, From 945db009b2638151ac80e3750a11ade455c2a034 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 19 Aug 2020 13:46:07 -0700 Subject: [PATCH 2/2] Add test for transaction and block version serialization --- src/blockdata/block.rs | 15 +++++++++++++++ src/blockdata/transaction.rs | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/blockdata/block.rs b/src/blockdata/block.rs index 989201db..93d7c591 100644 --- a/src/blockdata/block.rs +++ b/src/blockdata/block.rs @@ -311,6 +311,21 @@ mod tests { assert_eq!(serialize(&real_decode), segwit_block); } + #[test] + fn block_version_test() { + let block = Vec::from_hex("ffffff7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let decode: Result = deserialize(&block); + assert!(decode.is_ok()); + let real_decode = decode.unwrap(); + assert_eq!(real_decode.header.version, 2147483647); + + let block2 = Vec::from_hex("000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let decode2: Result = deserialize(&block2); + assert!(decode2.is_ok()); + let real_decode2 = decode2.unwrap(); + assert_eq!(real_decode2.header.version, -2147483648); + } + #[test] fn compact_roundrtip_test() { let some_header = Vec::from_hex("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b").unwrap(); diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index bc9cd2fb..8cc37d84 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -761,6 +761,21 @@ mod tests { assert_eq!(realtx.get_size(), tx_bytes.len()); } + #[test] + fn test_transaction_version() { + let tx_bytes = Vec::from_hex("ffffff7f0100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000").unwrap(); + let tx: Result = deserialize(&tx_bytes); + assert!(tx.is_ok()); + let realtx = tx.unwrap(); + assert_eq!(realtx.version, 2147483647); + + let tx2_bytes = Vec::from_hex("000000800100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000").unwrap(); + let tx2: Result = deserialize(&tx2_bytes); + assert!(tx2.is_ok()); + let realtx2 = tx2.unwrap(); + assert_eq!(realtx2.version, -2147483648); + } + #[test] fn tx_no_input_deserialization() { let tx_bytes = Vec::from_hex(