Bitcoin Core  24.99.0
P2P Digital Currency
transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2021 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 <univalue.h>
18 #include <util/rbf.h>
19 #include <validation.h>
20 #include <version.h>
21 
22 #include <cassert>
23 
25 {
27 }
28 
30 {
32  try {
33  int nVersion;
34  ds >> nVersion;
35  ds.SetVersion(nVersion);
36  } catch (const std::ios_base::failure&) {
37  return;
38  }
39  bool valid_tx = true;
40  const CTransaction tx = [&] {
41  try {
42  return CTransaction(deserialize, ds);
43  } catch (const std::ios_base::failure&) {
44  valid_tx = false;
46  }
47  }();
48  bool valid_mutable_tx = true;
50  CMutableTransaction mutable_tx;
51  try {
52  int nVersion;
53  ds_mtx >> nVersion;
54  ds_mtx.SetVersion(nVersion);
55  ds_mtx >> mutable_tx;
56  } catch (const std::ios_base::failure&) {
57  valid_mutable_tx = false;
58  }
59  assert(valid_tx == valid_mutable_tx);
60  if (!valid_tx) {
61  return;
62  }
63 
64  {
65  TxValidationState state_with_dupe_check;
66  const bool res{CheckTransaction(tx, state_with_dupe_check)};
67  Assert(res == state_with_dupe_check.IsValid());
68  }
69 
70  const CFeeRate dust_relay_fee{DUST_RELAY_TX_FEE};
71  std::string reason;
72  const bool is_standard_with_permit_bare_multisig = IsStandardTx(tx, std::nullopt, /* permit_bare_multisig= */ true, dust_relay_fee, reason);
73  const bool is_standard_without_permit_bare_multisig = IsStandardTx(tx, std::nullopt, /* permit_bare_multisig= */ false, dust_relay_fee, reason);
74  if (is_standard_without_permit_bare_multisig) {
75  assert(is_standard_with_permit_bare_multisig);
76  }
77 
78  (void)tx.GetHash();
79  (void)tx.GetTotalSize();
80  try {
81  (void)tx.GetValueOut();
82  } catch (const std::runtime_error&) {
83  }
84  (void)tx.GetWitnessHash();
85  (void)tx.HasWitness();
86  (void)tx.IsCoinBase();
87  (void)tx.IsNull();
88  (void)tx.ToString();
89 
90  (void)EncodeHexTx(tx);
91  (void)GetLegacySigOpCount(tx);
92  (void)GetTransactionWeight(tx);
93  (void)GetVirtualTransactionSize(tx);
94  (void)IsFinalTx(tx, /* nBlockHeight= */ 1024, /* nBlockTime= */ 1024);
95  (void)RecursiveDynamicUsage(tx);
96  (void)SignalsOptInRBF(tx);
97 
98  CCoinsView coins_view;
99  const CCoinsViewCache coins_view_cache(&coins_view);
100  (void)AreInputsStandard(tx, coins_view_cache);
101  (void)IsWitnessStandard(tx, coins_view_cache);
102 
104  TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*entry=*/u);
105  TxToUniv(tx, /*block_hash=*/uint256::ONE, /*entry=*/u);
106 }
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
#define Assert(val)
Identity function.
Definition: check.h:74
static const std::string REGTEST
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:213
Abstract view on the open txout dataset.
Definition: coins.h:157
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:186
void SetVersion(int n)
Definition: streams.h:277
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:288
bool HasWitness() const
Definition: transaction.h:360
bool IsNull() const
Definition: transaction.h:326
const uint256 & GetHash() const
Definition: transaction.h:330
std::string ToString() const
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
bool IsCoinBase() const
Definition: transaction.h:343
CAmount GetValueOut() const
Definition: transaction.cpp:89
const uint256 & GetWitnessHash() const
Definition: transaction.h:331
@ VOBJ
Definition: univalue.h:20
bool IsValid() const
Definition: validation.h:121
static const uint256 ONE
Definition: uint256.h:125
static const uint256 ZERO
Definition: uint256.h:124
static int64_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:148
std::string EncodeHexTx(const CTransaction &tx, const int serializeFlags=0)
Definition: core_write.cpp:143
void TxToUniv(const CTransaction &tx, const uint256 &block_hash, UniValue &entry, bool include_hex=true, int serialize_flags=0, const CTxUndo *txundo=nullptr, TxVerbosity verbosity=TxVerbosity::SHOW_DETAILS)
Definition: core_write.cpp:171
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:177
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:295
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size,...
Definition: policy.cpp:211
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:94
static constexpr unsigned int DUST_RELAY_TX_FEE
Min feerate for defining dust.
Definition: policy.h:55
@ SER_NETWORK
Definition: serialize.h:131
constexpr deserialize_type deserialize
Definition: serialize.h:48
A mutable version of CTransaction.
Definition: transaction.h:373
FUZZ_TARGET_INIT(transaction, initialize_transaction)
Definition: transaction.cpp:29
void initialize_transaction()
Definition: transaction.cpp:24
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:11
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Auxiliary functions for transaction validation (ideally should not be exposed)
Definition: tx_verify.cpp:116
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
bool SignalsOptInRBF(const CTransaction &tx)
Check whether the sequence numbers on this transaction are signaling opt-in to replace-by-fee,...
Definition: rbf.cpp:9
assert(!tx.IsCoinBase())
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15