Struct bitcoin::blockdata::transaction::Transaction
source · 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.
See Bitcoin Wiki: Transaction for more information.
Bitcoin 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
Bitcoin 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
impl Transaction
sourcepub const MAX_STANDARD_WEIGHT: Weight = _
pub const MAX_STANDARD_WEIGHT: Weight = _
Maximum transaction weight for Bitcoin Core 25.0.
sourcepub fn ntxid(&self) -> Hash
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.
sourcepub fn txid(&self) -> Txid
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()
.
sourcepub fn wtxid(&self) -> Wtxid
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()
.
sourcepub fn weight(&self) -> Weight
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.
sourcepub fn base_size(&self) -> usize
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.
sourcepub fn total_size(&self) -> usize
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.
sourcepub fn vsize(&self) -> usize
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).
sourcepub fn strippedsize(&self) -> usize
👎Deprecated since 0.31.0: Use Transaction::base_size() instead
pub fn strippedsize(&self) -> usize
Returns the size of this transaction excluding the witness data.
sourcepub fn is_coinbase(&self) -> bool
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”).
sourcepub fn is_coin_base(&self) -> bool
👎Deprecated since 0.31.0: use is_coinbase instead
pub fn is_coin_base(&self) -> bool
Checks if this is a coinbase transaction.
sourcepub fn is_explicitly_rbf(&self) -> bool
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.
sourcepub fn is_absolute_timelock_satisfied(&self, height: Height, time: Time) -> bool
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.
sourcepub fn is_lock_time_enabled(&self) -> bool
pub fn is_lock_time_enabled(&self) -> bool
Returns true
if this transactions nLockTime is enabled (BIP-65).
sourcepub fn script_pubkey_lens(&self) -> impl Iterator<Item = usize> + '_
pub fn script_pubkey_lens(&self) -> impl Iterator<Item = usize> + '_
Returns an iterator over lengths of script_pubkey
s 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.
sourcepub fn total_sigop_cost<S>(&self, spent: S) -> usizewhere
S: FnMut(&OutPoint) -> Option<TxOut>,
pub fn total_sigop_cost<S>(&self, spent: S) -> usizewhere 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
impl Transaction
sourcepub fn verify<S>(&self, spent: S) -> Result<(), TxVerifyError>where
S: FnMut(&OutPoint) -> Option<TxOut>,
pub fn verify<S>(&self, spent: S) -> Result<(), TxVerifyError>where S: FnMut(&OutPoint) -> Option<TxOut>,
Verifies that this transaction is able to spend its inputs.
Shorthand for Self::verify_with_flags
with flag bitcoinconsensus::VERIFY_ALL
.
The spent
closure should not return the same TxOut
twice!
Trait Implementations§
source§impl AsRef<Transaction> for PrefilledTransaction
impl AsRef<Transaction> for PrefilledTransaction
source§fn as_ref(&self) -> &Transaction
fn as_ref(&self) -> &Transaction
source§impl Clone for Transaction
impl Clone for Transaction
source§fn clone(&self) -> Transaction
fn clone(&self) -> Transaction
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for Transaction
impl Debug for Transaction
source§impl Decodable for Transaction
impl Decodable for Transaction
source§impl<'de> Deserialize<'de> for Transaction
impl<'de> Deserialize<'de> for Transaction
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl Encodable for Transaction
impl Encodable for Transaction
source§impl From<&Transaction> for Txid
impl From<&Transaction> for Txid
source§fn from(tx: &Transaction) -> Txid
fn from(tx: &Transaction) -> Txid
source§impl From<&Transaction> for Wtxid
impl From<&Transaction> for Wtxid
source§fn from(tx: &Transaction) -> Wtxid
fn from(tx: &Transaction) -> Wtxid
source§impl From<Transaction> for Txid
impl From<Transaction> for Txid
source§fn from(tx: Transaction) -> Txid
fn from(tx: Transaction) -> Txid
source§impl From<Transaction> for Wtxid
impl From<Transaction> for Wtxid
source§fn from(tx: Transaction) -> Wtxid
fn from(tx: Transaction) -> Wtxid
source§impl Hash for Transaction
impl Hash for Transaction
source§impl Ord for Transaction
impl Ord for Transaction
source§impl PartialEq<Transaction> for Transaction
impl PartialEq<Transaction> for Transaction
source§fn eq(&self, other: &Transaction) -> bool
fn eq(&self, other: &Transaction) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<Transaction> for Transaction
impl PartialOrd<Transaction> for Transaction
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more