Skip to content

Commit

Permalink
Merge rust-bitcoin#4040: primitives: store transaction::Version as u3…
Browse files Browse the repository at this point in the history
…2 instead of i32

0acd2b5 primitives: store transaction::Version as u32 instead of i32 (Andrew Toth)

Pull request description:

  Closes rust-bitcoin#4039.

ACKs for top commit:
  Kixunil:
    ACK 0acd2b5
  apoelstra:
    ACK 0acd2b5; successfully ran local tests

Tree-SHA512: 8e207c77eec3db3ab85de842ab03ac8692ac66f0359121fbc736068b8f7ce66ef3049ad0be53a534cba08d32ea62e76a624ca2713dd5b8b6f115fd51826a81ce
  • Loading branch information
apoelstra committed Feb 12, 2025
2 parents 2c4e796 + 0acd2b5 commit 58bb2b1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
12 changes: 3 additions & 9 deletions bitcoin/src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ crate::internal_macros::define_extension_trait! {
/// Extension functionality for the [`Version`] type.
pub trait VersionExt impl for Version {
/// Constructs a new non-standard transaction version.
fn non_standard(version: i32) -> Version { Self(version) }
fn non_standard(version: u32) -> Version { Self(version) }

/// Returns true if this transaction version number is considered standard.
fn is_standard(&self) -> bool { *self == Version::ONE || *self == Version::TWO || *self == Version::THREE }
Expand Down Expand Up @@ -1369,17 +1369,11 @@ mod tests {

#[test]
fn transaction_version() {
let tx_bytes = hex!("ffffff7f0100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000");
let tx_bytes = hex!("ffffffff0100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000");
let tx: Result<Transaction, _> = deserialize(&tx_bytes);
assert!(tx.is_ok());
let realtx = tx.unwrap();
assert_eq!(realtx.version, Version::non_standard(2147483647));

let tx2_bytes = hex!("000000800100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000");
let tx2: Result<Transaction, _> = deserialize(&tx2_bytes);
assert!(tx2.is_ok());
let realtx2 = tx2.unwrap();
assert_eq!(realtx2.version, Version::non_standard(-2147483648));
assert_eq!(realtx.version, Version::non_standard(u32::MAX));
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,15 @@ impl Wtxid {
///
/// Currently, as specified by [BIP-68] and [BIP-431], version 1, 2, and 3 are considered standard.
///
/// Standardness of the inner `i32` is not an invariant because you are free to create transactions
/// Standardness of the inner `u32` is not an invariant because you are free to create transactions
/// of any version, transactions with non-standard version numbers will not be relayed by the
/// Bitcoin network.
///
/// [BIP-68]: https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki
/// [BIP-431]: https://github.com/bitcoin/bips/blob/master/bip-0431.mediawiki
#[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Version(pub i32);
pub struct Version(pub u32);

impl Version {
/// The original Bitcoin transaction version (pre-BIP-68).
Expand Down

0 comments on commit 58bb2b1

Please sign in to comment.