Merge pull request #458 from braydonf/version

Transaction and header version is signed int
This commit is contained in:
Andrew Poelstra 2020-09-09 17:06:31 +00:00 committed by GitHub
commit addb54ffc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 7 deletions

View File

@ -37,7 +37,7 @@ use VarInt;
#[derive(Copy, PartialEq, Eq, Clone, Debug)] #[derive(Copy, PartialEq, Eq, Clone, Debug)]
pub struct BlockHeader { pub struct BlockHeader {
/// The protocol version. Should always be 1. /// The protocol version. Should always be 1.
pub version: u32, pub version: i32,
/// Reference to the previous block in the chain /// Reference to the previous block in the chain
pub prev_blockhash: BlockHash, pub prev_blockhash: BlockHash,
/// The root hash of the merkle tree of transactions in the block /// The root hash of the merkle tree of transactions in the block
@ -311,6 +311,21 @@ mod tests {
assert_eq!(serialize(&real_decode), segwit_block); assert_eq!(serialize(&real_decode), segwit_block);
} }
#[test]
fn block_version_test() {
let block = Vec::from_hex("ffffff7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
let decode: Result<Block, _> = 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<Block, _> = deserialize(&block2);
assert!(decode2.is_ok());
let real_decode2 = decode2.unwrap();
assert_eq!(real_decode2.header.version, -2147483648);
}
#[test] #[test]
fn compact_roundrtip_test() { fn compact_roundrtip_test() {
let some_header = Vec::from_hex("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b").unwrap(); let some_header = Vec::from_hex("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b").unwrap();
@ -320,4 +335,3 @@ mod tests {
assert_eq!(header.bits, BlockHeader::compact_target_from_u256(&header.target())); assert_eq!(header.bits, BlockHeader::compact_target_from_u256(&header.target()));
} }
} }

View File

@ -260,7 +260,7 @@ impl Default for TxOut {
#[derive(Clone, PartialEq, Eq, Debug, Hash)] #[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct Transaction { pub struct Transaction {
/// The protocol version, is currently expected to be 1 or 2 (BIP 68). /// 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 /// Block number before which this transaction is valid, or 0 for
/// valid immediately. /// valid immediately.
pub lock_time: u32, pub lock_time: u32,
@ -531,7 +531,7 @@ impl Encodable for Transaction {
impl Decodable for Transaction { impl Decodable for Transaction {
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> { fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
let version = u32::consensus_decode(&mut d)?; let version = i32::consensus_decode(&mut d)?;
let input = Vec::<TxIn>::consensus_decode(&mut d)?; let input = Vec::<TxIn>::consensus_decode(&mut d)?;
// segwit // segwit
if input.is_empty() { if input.is_empty() {
@ -662,7 +662,7 @@ mod tests {
Err(ParseOutPointError::Txid(Txid::from_hex("5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c945X").unwrap_err()))); Err(ParseOutPointError::Txid(Txid::from_hex("5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c945X").unwrap_err())));
assert_eq!(OutPoint::from_str("5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456:lol"), assert_eq!(OutPoint::from_str("5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456:lol"),
Err(ParseOutPointError::Vout(u32::from_str("lol").unwrap_err()))); Err(ParseOutPointError::Vout(u32::from_str("lol").unwrap_err())));
assert_eq!(OutPoint::from_str("5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456:42"), assert_eq!(OutPoint::from_str("5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456:42"),
Ok(OutPoint{ Ok(OutPoint{
txid: Txid::from_hex("5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456").unwrap(), txid: Txid::from_hex("5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456").unwrap(),
@ -761,6 +761,21 @@ mod tests {
assert_eq!(realtx.get_size(), tx_bytes.len()); assert_eq!(realtx.get_size(), tx_bytes.len());
} }
#[test]
fn test_transaction_version() {
let tx_bytes = Vec::from_hex("ffffff7f0100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000").unwrap();
let tx: Result<Transaction, _> = 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<Transaction, _> = deserialize(&tx2_bytes);
assert!(tx2.is_ok());
let realtx2 = tx2.unwrap();
assert_eq!(realtx2.version, -2147483648);
}
#[test] #[test]
fn tx_no_input_deserialization() { fn tx_no_input_deserialization() {
let tx_bytes = Vec::from_hex( let tx_bytes = Vec::from_hex(
@ -1229,4 +1244,3 @@ mod tests {
} }
} }
} }

View File

@ -30,7 +30,7 @@ use consensus::encode::Encodable;
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
#[deprecated(since="0.24.0", note="please use `SigHashCache` instead")] #[deprecated(since="0.24.0", note="please use `SigHashCache` instead")]
pub struct SighashComponents { pub struct SighashComponents {
tx_version: u32, tx_version: i32,
tx_locktime: u32, tx_locktime: u32,
/// Hash of all the previous outputs /// Hash of all the previous outputs
pub hash_prevouts: SigHash, pub hash_prevouts: SigHash,