Bitcoin Core  29.1.0
P2P Digital Currency
transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2022 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>
7 #include <consensus/tx_check.h>
8 #include <consensus/tx_verify.h>
9 #include <consensus/validation.h>
10 #include <core_io.h>
11 #include <core_memusage.h>
12 #include <policy/policy.h>
13 #include <policy/settings.h>
14 #include <primitives/transaction.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 
26 {
28 }
29 
31 {
33  DataStream 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  DataStream ds_mtx{buffer};
45  CMutableTransaction mutable_tx;
46  try {
47  ds_mtx >> TX_WITH_WITNESS(mutable_tx);
48  } catch (const std::ios_base::failure&) {
49  valid_mutable_tx = false;
50  }
51  assert(valid_tx == valid_mutable_tx);
52  if (!valid_tx) {
53  return;
54  }
55 
56  {
57  TxValidationState state_with_dupe_check;
58  const bool res{CheckTransaction(tx, state_with_dupe_check)};
59  Assert(res == state_with_dupe_check.IsValid());
60  }
61 
62  const CFeeRate dust_relay_fee{DUST_RELAY_TX_FEE};
63  std::string reason;
64  const bool is_standard_with_permit_bare_multisig = IsStandardTx(tx, std::nullopt, /* permit_bare_multisig= */ true, dust_relay_fee, reason);
65  const bool is_standard_without_permit_bare_multisig = IsStandardTx(tx, std::nullopt, /* permit_bare_multisig= */ false, dust_relay_fee, reason);
66  if (is_standard_without_permit_bare_multisig) {
67  assert(is_standard_with_permit_bare_multisig);
68  }
69 
70  (void)tx.GetHash();
71  (void)tx.GetTotalSize();
72  try {
73  (void)tx.GetValueOut();
74  } catch (const std::runtime_error&) {
75  }
76  (void)tx.GetWitnessHash();
77  (void)tx.HasWitness();
78  (void)tx.IsCoinBase();
79  (void)tx.IsNull();
80  (void)tx.ToString();
81 
82  (void)EncodeHexTx(tx);
83  (void)GetLegacySigOpCount(tx);
84  (void)GetTransactionWeight(tx);
85  (void)GetVirtualTransactionSize(tx);
86  (void)IsFinalTx(tx, /* nBlockHeight= */ 1024, /* nBlockTime= */ 1024);
87  (void)RecursiveDynamicUsage(tx);
88  (void)SignalsOptInRBF(tx);
89 
90  CCoinsView coins_view;
91  const CCoinsViewCache coins_view_cache(&coins_view);
92  (void)AreInputsStandard(tx, coins_view_cache);
93  (void)IsWitnessStandard(tx, coins_view_cache);
94 
95  if (tx.GetTotalSize() < 250'000) { // Avoid high memory usage (with msan) due to json encoding
96  {
97  UniValue u{UniValue::VOBJ};
98  TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*entry=*/u);
99  }
100  {
101  UniValue u{UniValue::VOBJ};
102  TxToUniv(tx, /*block_hash=*/uint256::ONE, /*entry=*/u);
103  }
104  }
105 }
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:103
assert(!tx.IsCoinBase())
void initialize_transaction()
Definition: transaction.cpp:25
constexpr deserialize_type deserialize
Definition: serialize.h:49
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:347
bool SignalsOptInRBF(const CTransaction &tx)
Check whether the sequence numbers on this transaction are signaling opt-in to replace-by-fee, according to BIP 125.
Definition: rbf.cpp:9
static int32_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:133
bool IsCoinBase() const
Definition: transaction.h:356
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size...
Definition: policy.cpp:258
CAmount GetValueOut() const
Definition: transaction.cpp:98
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:220
bool IsValid() const
Definition: validation.h:106
FUZZ_TARGET(transaction,.init=initialize_transaction)
Definition: transaction.cpp:30
Abstract view on the open txout dataset.
Definition: coins.h:309
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:146
bool IsNull() const
Definition: transaction.h:339
static constexpr unsigned int DUST_RELAY_TX_FEE
Min feerate for defining dust.
Definition: policy.h:64
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
bool HasWitness() const
Definition: transaction.h:373
std::string EncodeHexTx(const CTransaction &tx)
Definition: core_write.cpp:143
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Auxiliary functions for transaction validation (ideally should not be exposed)
Definition: tx_verify.cpp:112
std::string ToString() const
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:32
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
A mutable version of CTransaction.
Definition: transaction.h:377
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
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:17
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:295
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:362
Seed with a compile time constant of zeros.
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:11
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
Definition: transaction.h:344
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:85
const Txid & GetHash() const LIFETIMEBOUND
Definition: transaction.h:343
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195