Merge rust-bitcoin/rust-bitcoin#3287: Move `transaction::Version` to `primitives`

c48d9d6523 Move transaction::Version to primitives (Tobin C. Harding)
f490222068 Introduce the VersionExt trait (Tobin C. Harding)
fb89974b82 Run the formatter (Tobin C. Harding)
bb3a3ecbaa Introduce temporary module for Version (Tobin C. Harding)
1fde868f51 Separate Version impl blocks (Tobin C. Harding)

Pull request description:

  As per title, in tiny small chunks, move the `transaction::Version` over to `primitives`. Only the type, its associated consts, and its `Display` impl are moved. The two methods are left in an extension trait.

  Was originally attempted in #3253

ACKs for top commit:
  Kixunil:
    ACK c48d9d6523
  apoelstra:
    ACK c48d9d6523 successfully ran local tests

Tree-SHA512: 83415cf0762dca5c263deb743734fc7abede804a6daac31df3d0101b51c6261e6d54452eb744727ae680cacce9e4ef726a6fa253d86c4e7a5d8ec789b137566c
This commit is contained in:
merge-script 2024-09-04 01:21:43 +00:00
commit f6287fb445
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 51 additions and 28 deletions

View File

@ -31,6 +31,10 @@ use crate::sighash::{EcdsaSighashType, TapSighashType};
use crate::witness::Witness; use crate::witness::Witness;
use crate::{Amount, FeeRate, SignedAmount, VarInt}; use crate::{Amount, FeeRate, SignedAmount, VarInt};
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)]
pub use primitives::transaction::*;
hashes::hash_newtype! { hashes::hash_newtype! {
/// A bitcoin transaction hash/transaction ID. /// A bitcoin transaction hash/transaction ID.
/// ///
@ -925,31 +929,15 @@ impl std::error::Error for IndexOutOfBoundsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
} }
/// The transaction version. crate::internal_macros::define_extension_trait! {
/// /// Extension functionality for the [`Version`] type.
/// Currently, as specified by [BIP-68], only version 1 and 2 are considered standard. pub trait VersionExt impl for Version {
///
/// Standardness of the inner `i32` 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
#[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Version(pub i32);
impl Version {
/// The original Bitcoin transaction version (pre-BIP-68).
pub const ONE: Self = Self(1);
/// The second Bitcoin transaction version (post-BIP-68).
pub const TWO: Self = Self(2);
/// Creates a non-standard transaction version. /// Creates a non-standard transaction version.
pub fn non_standard(version: i32) -> Version { Self(version) } fn non_standard(version: i32) -> Version { Self(version) }
/// Returns true if this transaction version number is considered standard. /// Returns true if this transaction version number is considered standard.
pub fn is_standard(&self) -> bool { *self == Version::ONE || *self == Version::TWO } fn is_standard(&self) -> bool { *self == Version::ONE || *self == Version::TWO }
}
} }
impl Encodable for Version { impl Encodable for Version {
@ -964,10 +952,6 @@ impl Decodable for Version {
} }
} }
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
}
impl_consensus_encoding!(TxOut, value, script_pubkey); impl_consensus_encoding!(TxOut, value, script_pubkey);
impl Encodable for OutPoint { impl Encodable for OutPoint {

View File

@ -33,6 +33,7 @@ pub mod locktime;
pub mod opcodes; pub mod opcodes;
pub mod pow; pub mod pow;
pub mod sequence; pub mod sequence;
pub mod transaction;
#[doc(inline)] #[doc(inline)]
pub use units::*; pub use units::*;

View File

@ -0,0 +1,38 @@
// SPDX-License-Identifier: CC0-1.0
//! Bitcoin transactions.
//!
//! A transaction describes a transfer of money. It consumes previously-unspent
//! transaction outputs and produces new ones, satisfying the condition to spend
//! the old outputs (typically a digital signature with a specific key must be
//! provided) and defining the condition to spend the new ones. The use of digital
//! signatures ensures that coins cannot be spent by unauthorized parties.
//!
//! This module provides the structures and functions needed to support transactions.
use core::fmt;
/// The transaction version.
///
/// Currently, as specified by [BIP-68], only version 1 and 2 are considered standard.
///
/// Standardness of the inner `i32` 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
#[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Version(pub i32);
impl Version {
/// The original Bitcoin transaction version (pre-BIP-68).
pub const ONE: Self = Self(1);
/// The second Bitcoin transaction version (post-BIP-68).
pub const TWO: Self = Self(2);
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
}