Skip to content

Commit cb270ea

Browse files
committed
Make transaction::Version field private
This commit addresses rust-bitcoin#4041 by making the transaction::Version field private Changes: - Make the `Version` field private with `pub(crate)` - Rename `non_standard` to `maybe_non_standard` for clarity since it accepts both standard and non-standard versions - Add `#[inline]` attributes to small, frequently used methods: - `as_u32` - `maybe_non_standard` Users now must use either: - Constants (`Version::ONE/TWO/THREE`) for standard versions - `maybe_non_standard` method for any version (standard or non-standard)
1 parent 6c69b66 commit cb270ea

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

bitcoin/src/blockdata/transaction.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -627,26 +627,15 @@ impl std::error::Error for IndexOutOfBoundsError {
627627
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
628628
}
629629

630-
crate::internal_macros::define_extension_trait! {
631-
/// Extension functionality for the [`Version`] type.
632-
pub trait VersionExt impl for Version {
633-
/// Constructs a new non-standard transaction version.
634-
fn non_standard(version: u32) -> Version { Self(version) }
635-
636-
/// Returns true if this transaction version number is considered standard.
637-
fn is_standard(&self) -> bool { *self == Version::ONE || *self == Version::TWO || *self == Version::THREE }
638-
}
639-
}
640-
641630
impl Encodable for Version {
642631
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
643-
self.0.consensus_encode(w)
632+
self.to_u32().consensus_encode(w)
644633
}
645634
}
646635

647636
impl Decodable for Version {
648637
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
649-
Decodable::consensus_decode(r).map(Version)
638+
Decodable::consensus_decode(r).map(Version::maybe_non_standard)
650639
}
651640
}
652641

@@ -1373,7 +1362,7 @@ mod tests {
13731362
let tx: Result<Transaction, _> = deserialize(&tx_bytes);
13741363
assert!(tx.is_ok());
13751364
let realtx = tx.unwrap();
1376-
assert_eq!(realtx.version, Version::non_standard(u32::MAX));
1365+
assert_eq!(realtx.version, Version::maybe_non_standard(u32::MAX));
13771366
}
13781367

13791368
#[test]

primitives/src/transaction.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl Wtxid {
518518
/// [BIP-431]: https://github.com/bitcoin/bips/blob/master/bip-0431.mediawiki
519519
#[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord, Hash)]
520520
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
521-
pub struct Version(pub u32);
521+
pub struct Version(u32);
522522

523523
impl Version {
524524
/// The original Bitcoin transaction version (pre-BIP-68).
@@ -529,12 +529,35 @@ impl Version {
529529

530530
/// The third Bitcoin transaction version (post-BIP-431).
531531
pub const THREE: Self = Self(3);
532+
533+
/// Constructs a potentially non-standard transaction version.
534+
///
535+
/// This can accept both standard and non-standard versions.
536+
#[inline]
537+
pub fn maybe_non_standard(version: u32) -> Version { Self(version) }
538+
539+
/// Returns the inner `u32` value of this `Version`.
540+
#[inline]
541+
pub const fn to_u32(self) -> u32 { self.0 }
542+
543+
/// Returns true if this transaction version number is considered standard.
544+
///
545+
/// The behavior of this method matches whatever Bitcoin Core considers standard at the time
546+
/// of the release and may change in future versions to accommodate new standard versions.
547+
/// As of Bitcoin Core 28.0 ([release notes](https://bitcoincore.org/en/releases/28.0/)),
548+
/// versions 1, 2, and 3 are considered standard.
549+
#[inline]
550+
pub fn is_standard(&self) -> bool { *self == Version::ONE || *self == Version::TWO || *self == Version::THREE }
532551
}
533552

534553
impl fmt::Display for Version {
535554
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
536555
}
537556

557+
impl From<Version> for u32 {
558+
fn from(version: Version) -> Self { version.0 }
559+
}
560+
538561
#[cfg(feature = "arbitrary")]
539562
#[cfg(feature = "alloc")]
540563
impl<'a> Arbitrary<'a> for Transaction {

0 commit comments

Comments
 (0)