From 1fde868f5162624ec852a138037c9bf9572922eb Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 3 Sep 2024 10:54:48 +1000 Subject: [PATCH 1/5] Separate Version impl blocks In preparation for adding an extension trait; separate the `transaction::Version` impl blocks into stuff that will stay here and stuff that will go to `primitives`. Refactor only, no logic changes. --- bitcoin/src/blockdata/transaction.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 63858e6d8..d1a2753fe 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -944,7 +944,9 @@ impl Version { /// The second Bitcoin transaction version (post-BIP-68). pub const TWO: Self = Self(2); +} +impl Version { /// Creates a non-standard transaction version. pub fn non_standard(version: i32) -> Version { Self(version) } From bb3a3ecbaa171dd003faa968bbbefe8fc662e99a Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 3 Sep 2024 10:56:06 +1000 Subject: [PATCH 2/5] Introduce temporary module for Version In preparation for using the formatter to indent add a temporary module. Done as part of adding an extension trait. --- bitcoin/src/blockdata/transaction.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index d1a2753fe..071009808 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -946,6 +946,8 @@ impl Version { pub const TWO: Self = Self(2); } +mod tmp { +use super::*; impl Version { /// Creates a non-standard transaction version. pub fn non_standard(version: i32) -> Version { Self(version) } @@ -953,6 +955,7 @@ impl Version { /// Returns true if this transaction version number is considered standard. pub fn is_standard(&self) -> bool { *self == Version::ONE || *self == Version::TWO } } +} impl Encodable for Version { fn consensus_encode(&self, w: &mut W) -> Result { From fb89974b8233145892c517b79870c8195cdaa9b7 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 3 Sep 2024 10:56:59 +1000 Subject: [PATCH 3/5] Run the formatter Run `just fmt`. No manual changes. --- bitcoin/src/blockdata/transaction.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 071009808..cfa1533bb 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -947,14 +947,14 @@ impl Version { } mod tmp { -use super::*; -impl Version { - /// Creates a non-standard transaction version. - pub fn non_standard(version: i32) -> Version { Self(version) } + use super::*; + impl Version { + /// Creates a non-standard transaction version. + pub fn non_standard(version: i32) -> Version { Self(version) } - /// Returns true if this transaction version number is considered standard. - pub fn is_standard(&self) -> bool { *self == Version::ONE || *self == Version::TWO } -} + /// Returns true if this transaction version number is considered standard. + pub fn is_standard(&self) -> bool { *self == Version::ONE || *self == Version::TWO } + } } impl Encodable for Version { From f490222068a59bdce0a36f4bb99af5bcc1661c0c Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 3 Sep 2024 10:57:58 +1000 Subject: [PATCH 4/5] Introduce the VersionExt trait In preparation for moving the `transaction::Version` type to `primitives`; add a `VersionExt` trait using our macro. --- bitcoin/src/blockdata/transaction.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index cfa1533bb..d77022a99 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -946,14 +946,14 @@ impl Version { pub const TWO: Self = Self(2); } -mod tmp { - use super::*; - impl Version { +crate::internal_macros::define_extension_trait! { + /// Extension functionality for the [`Version`] type. + pub trait VersionExt impl for 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. - pub fn is_standard(&self) -> bool { *self == Version::ONE || *self == Version::TWO } + fn is_standard(&self) -> bool { *self == Version::ONE || *self == Version::TWO } } } From c48d9d6523425aad1e7e4bc9b62492ad04a26a66 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 3 Sep 2024 11:01:29 +1000 Subject: [PATCH 5/5] Move transaction::Version to primitives We would like to move the `Transaction` type to `primitives`, as a step towards this move the `transaction::Version` and its trait imps (just `Display`) over there. --- bitcoin/src/blockdata/transaction.rs | 29 +++------------------ primitives/src/lib.rs | 1 + primitives/src/transaction.rs | 38 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 primitives/src/transaction.rs diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index d77022a99..ef84cb67c 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -31,6 +31,10 @@ use crate::sighash::{EcdsaSighashType, TapSighashType}; use crate::witness::Witness; use crate::{Amount, FeeRate, SignedAmount, VarInt}; +#[rustfmt::skip] // Keep public re-exports separate. +#[doc(inline)] +pub use primitives::transaction::*; + hashes::hash_newtype! { /// A bitcoin transaction hash/transaction ID. /// @@ -925,27 +929,6 @@ impl std::error::Error for IndexOutOfBoundsError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } } -/// 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); -} - crate::internal_macros::define_extension_trait! { /// Extension functionality for the [`Version`] type. pub trait VersionExt impl for Version { @@ -969,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 Encodable for OutPoint { diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 3b90a8507..9e27c72fb 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -33,6 +33,7 @@ pub mod locktime; pub mod opcodes; pub mod pow; pub mod sequence; +pub mod transaction; #[doc(inline)] pub use units::*; diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs new file mode 100644 index 000000000..7b0701002 --- /dev/null +++ b/primitives/src/transaction.rs @@ -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) } +}