Bitcoin Core 32.1.0
P2P Digital Currency
Loading...
Searching...
No Matches
transaction.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <chainparams.h>
6#include <coins.h>
10#include <core_io.h>
11#include <core_memusage.h>
12#include <policy/policy.h>
13#include <policy/settings.h>
15#include <streams.h>
16#include <test/fuzz/fuzz.h>
17#include <test/util/random.h>
18#include <univalue.h>
19#include <util/chaintype.h>
20#include <util/rbf.h>
21#include <validation.h>
22
23#include <cassert>
24
29
31{
33 SpanReader ds{buffer};
34 bool valid_tx = true;
35 const CTransaction tx = [&] {
36 try {
38 } catch (const std::ios_base::failure&) {
39 valid_tx = false;
41 }
42 }();
43 bool valid_mutable_tx = true;
44 CMutableTransaction mutable_tx;
45 try {
46 SpanReader{buffer} >> TX_WITH_WITNESS(mutable_tx);
47 } catch (const std::ios_base::failure&) {
48 valid_mutable_tx = false;
49 }
50 assert(valid_tx == valid_mutable_tx);
51 if (!valid_tx) {
52 return;
53 }
54
55 {
56 TxValidationState state_with_dupe_check;
57 const bool res{CheckTransaction(tx, state_with_dupe_check)};
58 Assert(res == state_with_dupe_check.IsValid());
59 }
60
61 const CFeeRate dust_relay_fee{DUST_RELAY_TX_FEE};
62 std::string reason;
63 const bool is_standard_with_permit_bare_multisig = IsStandardTx(tx, std::nullopt, /* permit_bare_multisig= */ true, dust_relay_fee, reason);
64 const bool is_standard_without_permit_bare_multisig = IsStandardTx(tx, std::nullopt, /* permit_bare_multisig= */ false, dust_relay_fee, reason);
65 if (is_standard_without_permit_bare_multisig) {
66 assert(is_standard_with_permit_bare_multisig);
67 }
68
69 (void)tx.GetHash();
70 (void)tx.ComputeTotalSize();
71 try {
72 (void)tx.GetValueOut();
73 } catch (const std::runtime_error&) {
74 }
75 (void)tx.GetWitnessHash();
76 (void)tx.HasWitness();
77 (void)tx.IsCoinBase();
78 (void)tx.IsNull();
79 (void)tx.ToString();
80
81 (void)EncodeHexTx(tx);
82 (void)GetLegacySigOpCount(tx);
83 (void)GetTransactionWeight(tx);
85 (void)IsFinalTx(tx, /* nBlockHeight= */ 1024, /* nBlockTime= */ 1024);
86 (void)RecursiveDynamicUsage(tx);
87 (void)SignalsOptInRBF(tx);
88
89 const CCoinsViewCache coins_view_cache{&CoinsViewEmpty::Get()};
90 (void)ValidateInputsStandardness(tx, coins_view_cache);
91 (void)IsWitnessStandard(tx, coins_view_cache);
92
93 if (tx.ComputeTotalSize() < 250'000) { // Avoid high memory usage (with msan) due to json encoding
94 {
96 TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*entry=*/u);
97 }
98 {
100 TxToUniv(tx, /*block_hash=*/uint256::ONE, /*entry=*/u);
101 }
102 }
103}
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
#define Assert(val)
Identity function.
Definition check.h:116
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition coins.h:437
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition feerate.h:32
The basic transaction that is broadcasted on the network and contained in blocks.
bool HasWitness() const
bool IsNull() const
unsigned int ComputeTotalSize() const
Calculate the total transaction size in bytes, including witness data.
CTransaction(const CMutableTransaction &tx)
Convert a CMutableTransaction into a CTransaction.
std::string ToString() const
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
bool IsCoinBase() const
CAmount GetValueOut() const
const Txid & GetHash() const LIFETIMEBOUND
static CoinsViewEmpty & Get()
Definition coins.cpp:29
Minimal stream for reading from an existing byte array by std::span.
Definition streams.h:83
bool IsValid() const
Definition validation.h:112
static const uint256 ONE
Definition uint256.h:205
static const uint256 ZERO
Definition uint256.h:204
std::string EncodeHexTx(const CTransaction &tx)
Definition core_io.cpp:404
void TxToUniv(const CTransaction &tx, const uint256 &block_hash, UniValue &entry, bool include_hex, const CTxUndo *txundo, TxVerbosity verbosity, std::function< bool(const CTxOut &)> is_change_func)
Definition core_io.cpp:432
#define FUZZ_TARGET(...)
Definition fuzz.h:35
Definition basic.cpp:11
TxValidationState ValidateInputsStandardness(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs.
Definition policy.cpp:214
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition policy.cpp:395
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size,...
Definition policy.cpp:265
bool IsStandardTx(const CTransaction &tx, const std::optional< unsigned > &max_datacarrier_bytes, bool permit_bare_multisig, const CFeeRate &dust_relay_fee, std::string &reason)
Check for standard transaction types.
Definition policy.cpp:100
constexpr unsigned int DUST_RELAY_TX_FEE
Min feerate for defining dust.
Definition policy.h:68
constexpr TransactionSerParams TX_WITH_WITNESS
constexpr deserialize_type deserialize
Definition serialize.h:52
A mutable version of CTransaction.
SeedRandomStateForTest(SeedRand::ZEROS)
void initialize_transaction()
@ ZEROS
Seed with a compile time constant of zeros.
Definition random.h:19
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
Definition tx_check.cpp:19
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Auxiliary functions for transaction validation (ideally should not be exposed).
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
Check if transaction is final and can be included in a block with the specified height and time.
Definition tx_verify.cpp:23
bool SignalsOptInRBF(const CTransaction &tx)
Check whether the sequence numbers on this transaction are signaling opt-in to replace-by-fee,...
Definition rbf.cpp:11
assert(!tx.IsCoinBase())