pub struct Transaction {
    pub version: Version,
    pub lock_time: LockTime,
    pub input: Vec<TxIn>,
    pub output: Vec<TxOut>,
}
Expand description

Bitcoin transaction.

An authenticated movement of coins.

§Groestlcoin Core References

§Serialization notes

If any inputs have nonempty witnesses, the entire transaction is serialized in the post-BIP141 Segwit format which includes a list of witnesses. If all inputs have empty witnesses, the transaction is serialized in the pre-BIP141 format.

There is one major exception to this: to avoid deserialization ambiguity, if the transaction has no inputs, it is serialized in the BIP141 style. Be aware that this differs from the transaction format in PSBT, which never uses BIP141. (Ordinarily there is no conflict, since in PSBT transactions are always unsigned and therefore their inputs have empty witnesses.)

The specific ambiguity is that Segwit uses the flag bytes 0001 where an old serializer would read the number of transaction inputs. The old serializer would interpret this as “no inputs, one output”, which means the transaction is invalid, and simply reject it. Segwit further specifies that this encoding should only be used when some input has a nonempty witness; that is, witness-less transactions should be encoded in the traditional format.

However, in protocols where transactions may legitimately have 0 inputs, e.g. when parties are cooperatively funding a transaction, the “00 means Segwit” heuristic does not work. Since Segwit requires such a transaction be encoded in the original transaction format (since it has no inputs and therefore no input witnesses), a traditionally encoded transaction may have the 0001 Segwit flag in it, which confuses most Segwit parsers including the one in Groestlcoin Core.

We therefore deviate from the spec by always using the Segwit witness encoding for 0-input transactions, which results in unambiguously parseable transactions.

§A note on ordering

This type implements Ord, even though it contains a locktime, which is not itself Ord. This was done to simplify applications that may need to hold transactions inside a sorted container. We have ordered the locktimes based on their representation as a u32, which is not a semantically meaningful order, and therefore the ordering on Transaction itself is not semantically meaningful either.

The ordering is, however, consistent with the ordering present in this library before this change, so users should not notice any breakage (here) when transitioning from 0.29 to 0.30.

Fields§

§version: Version

The protocol version, is currently expected to be 1 or 2 (BIP 68).

§lock_time: LockTime

Block height or timestamp. Transaction cannot be included in a block until this height/time.

§Relevant BIPs
§input: Vec<TxIn>

List of transaction inputs.

§output: Vec<TxOut>

List of transaction outputs.

Implementations§

source§

impl Transaction

source

pub const MAX_STANDARD_WEIGHT: Weight = _

Maximum transaction weight for Bitcoin Core 25.0.

source

pub fn ntxid(&self) -> Hash

Computes a “normalized TXID” which does not include any signatures.

This gives a way to identify a transaction that is “the same” as another in the sense of having same inputs and outputs.

source

pub fn txid(&self) -> Txid

Computes the Txid.

Hashes the transaction excluding the segwit data (i.e. the marker, flag bytes, and the witness fields themselves). For non-segwit transactions which do not have any segwit data, this will be equal to Transaction::wtxid().

source

pub fn wtxid(&self) -> Wtxid

Computes the segwit version of the transaction id.

Hashes the transaction including all segwit data (i.e. the marker, flag bytes, and the witness fields themselves). For non-segwit transactions which do not have any segwit data, this will be equal to Transaction::txid().

source

pub fn weight(&self) -> Weight

Returns the weight of this transaction, as defined by BIP-141.

Transaction weight is defined as Base transaction size * 3 + Total transaction size (ie. the same method as calculating Block weight from Base size and Total size).

For transactions with an empty witness, this is simply the consensus-serialized size times four. For transactions with a witness, this is the non-witness consensus-serialized size multiplied by three plus the with-witness consensus-serialized size.

For transactions with no inputs, this function will return a value 2 less than the actual weight of the serialized transaction. The reason is that zero-input transactions, post-segwit, cannot be unambiguously serialized; we make a choice that adds two extra bytes. For more details see BIP 141 which uses a “input count” of 0x00 as a marker for a Segwit-encoded transaction.

If you need to use 0-input transactions, we strongly recommend you do so using the PSBT API. The unsigned transaction encoded within PSBT is always a non-segwit transaction and can therefore avoid this ambiguity.

source

pub fn base_size(&self) -> usize

Returns the base transaction size.

Base transaction size is the size of the transaction serialised with the witness data stripped.

source

pub fn total_size(&self) -> usize

Returns the total transaction size.

Total transaction size is the transaction size in bytes serialized as described in BIP144, including base data and witness data.

source

pub fn vsize(&self) -> usize

Returns the “virtual size” (vsize) of this transaction.

Will be ceil(weight / 4.0). Note this implements the virtual size as per BIP141, which is different to what is implemented in Bitcoin Core. The computation should be the same for any remotely sane transaction, and a standardness-rule-correct version is available in the policy module.

Virtual transaction size is defined as Transaction weight / 4 (rounded up to the next integer).

source

pub fn strippedsize(&self) -> usize

👎Deprecated since 0.31.0: Use Transaction::base_size() instead

Returns the size of this transaction excluding the witness data.

source

pub fn is_coinbase(&self) -> bool

Checks if this is a coinbase transaction.

The first transaction in the block distributes the mining reward and is called the coinbase transaction. It is impossible to check if the transaction is first in the block, so this function checks the structure of the transaction instead - the previous output must be all-zeros (creates satoshis “out of thin air”).

source

pub fn is_coin_base(&self) -> bool

👎Deprecated since 0.31.0: use is_coinbase instead

Checks if this is a coinbase transaction.

source

pub fn is_explicitly_rbf(&self) -> bool

Returns true if the transaction itself opted in to be BIP-125-replaceable (RBF).

§Warning

Incorrectly relying on RBF may lead to monetary loss!

This does not cover the case where a transaction becomes replaceable due to ancestors being RBF. Please note that transactions may be replaced even if they do not include the RBF signal: https://bitcoinops.org/en/newsletters/2022/10/19/#transaction-replacement-option.

source

pub fn is_absolute_timelock_satisfied(&self, height: Height, time: Time) -> bool

Returns true if this Transaction’s absolute timelock is satisfied at height/time.

§Returns

By definition if the lock time is not enabled the transaction’s absolute timelock is considered to be satisfied i.e., there are no timelock constraints restricting this transaction from being mined immediately.

source

pub fn is_lock_time_enabled(&self) -> bool

Returns true if this transactions nLockTime is enabled (BIP-65).

source

pub fn script_pubkey_lens(&self) -> impl Iterator<Item = usize> + '_

Returns an iterator over lengths of script_pubkeys in the outputs.

This is useful in combination with predict_weight if you have the transaction already constructed with a dummy value in the fee output which you’ll adjust after calculating the weight.

source

pub fn total_sigop_cost<S>(&self, spent: S) -> usize
where S: FnMut(&OutPoint) -> Option<TxOut>,

Counts the total number of sigops.

This value is for pre-taproot transactions only.

In taproot, a different mechanism is used. Instead of having a global per-block limit, there is a per-transaction-input limit, proportional to the size of that input. ref: https://bitcoin.stackexchange.com/questions/117356/what-is-sigop-signature-operation#117359

The spent parameter is a closure/function that looks up the output being spent by each input It takes in an OutPoint and returns a TxOut. If you can’t provide this, a placeholder of |_| None can be used. Without access to the previous TxOut, any sigops in a redeemScript (P2SH) as well as any segwit sigops will not be counted for that input.

source§

impl Transaction

source

pub fn verify<S>(&self, spent: S) -> Result<(), TxVerifyError>
where S: FnMut(&OutPoint) -> Option<TxOut>,

Available on crate feature groestlcoinconsensus only.

Verifies that this transaction is able to spend its inputs.

Shorthand for Self::verify_with_flags with flag groestlcoinconsensus::VERIFY_ALL.

The spent closure should not return the same TxOut twice!

source

pub fn verify_with_flags<S, F>( &self, spent: S, flags: F ) -> Result<(), TxVerifyError>
where S: FnMut(&OutPoint) -> Option<TxOut>, F: Into<u32>,

Available on crate feature groestlcoinconsensus only.

Verifies that this transaction is able to spend its inputs.

The spent closure should not return the same TxOut twice!

Trait Implementations§

source§

impl AsRef<Transaction> for PrefilledTransaction

source§

fn as_ref(&self) -> &Transaction

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for Transaction

source§

fn clone(&self) -> Transaction

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Transaction

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Decodable for Transaction

source§

fn consensus_decode_from_finite_reader<R: Read + ?Sized>( r: &mut R ) -> Result<Self, Error>

Decode Self from a size-limited reader. Read more
source§

fn consensus_decode<R: Read + ?Sized>(reader: &mut R) -> Result<Self, Error>

Decode an object with a well-defined format. Read more
source§

impl<'de> Deserialize<'de> for Transaction

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Encodable for Transaction

source§

fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, Error>

Encodes an object with a well-defined format. Read more
source§

impl From<&Transaction> for Txid

source§

fn from(tx: &Transaction) -> Txid

Converts to this type from the input type.
source§

impl From<&Transaction> for Wtxid

source§

fn from(tx: &Transaction) -> Wtxid

Converts to this type from the input type.
source§

impl From<Transaction> for Txid

source§

fn from(tx: Transaction) -> Txid

Converts to this type from the input type.
source§

impl From<Transaction> for Wtxid

source§

fn from(tx: Transaction) -> Wtxid

Converts to this type from the input type.
source§

impl Hash for Transaction

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Transaction

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Transaction

source§

fn eq(&self, other: &Transaction) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Transaction

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Transaction

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for Transaction

source§

impl StructuralPartialEq for Transaction

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,