Bitcoin Core  22.99.0
P2P Digital Currency
transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 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, /* permit_bare_multisig= */ true, dust_relay_fee, reason);
73  const bool is_standard_without_permit_bare_multisig = IsStandardTx(tx, /* 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)IsStandardTx(tx, reason);
96  (void)RecursiveDynamicUsage(tx);
97  (void)SignalsOptInRBF(tx);
98 
99  CCoinsView coins_view;
100  const CCoinsViewCache coins_view_cache(&coins_view);
101  (void)AreInputsStandard(tx, coins_view_cache, false);
102  (void)AreInputsStandard(tx, coins_view_cache, true);
103  (void)IsWitnessStandard(tx, coins_view_cache);
104 
106  TxToUniv(tx, /* hashBlock */ uint256::ZERO, /* include_addresses */ true, u);
107  TxToUniv(tx, /* hashBlock */ uint256::ONE, /* include_addresses */ false, u);
108 }
policy.h
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(transaction, initialize_transaction)
Definition: transaction.cpp:29
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:21
assert
assert(!tx.IsCoinBase())
TxToUniv
void TxToUniv(const CTransaction &tx, const uint256 &hashBlock, bool include_addresses, UniValue &entry, bool include_hex=true, int serialize_flags=0, const CTxUndo *txundo=nullptr)
Definition: core_write.cpp:193
settings.h
streams.h
IsWitnessStandard
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size,...
Definition: policy.cpp:196
transaction.h
validation.h
IsStandardTx
bool IsStandardTx(const CTransaction &tx, bool permit_bare_multisig, const CFeeRate &dust_relay_fee, std::string &reason)
Check for standard transaction types.
Definition: policy.cpp:76
CheckTransaction
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:10
CTransaction::GetValueOut
CAmount GetValueOut() const
Definition: transaction.cpp:83
GetLegacySigOpCount
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Auxiliary functions for transaction validation (ideally should not be exposed)
Definition: tx_verify.cpp:116
chainparams.h
IsFinalTx
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
AreInputsStandard
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs, bool taproot_active)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:159
version.h
core_io.h
Assert
#define Assert(val)
Identity function.
Definition: check.h:57
CFeeRate
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
UniValue
Definition: univalue.h:19
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
rbf.h
CCoinsView
Abstract view on the open txout dataset.
Definition: coins.h:157
ValidationState::IsValid
bool IsValid() const
Definition: validation.h:119
TxValidationState
Definition: validation.h:139
CTransaction::IsCoinBase
bool IsCoinBase() const
Definition: transaction.h:315
RecursiveDynamicUsage
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
tx_verify.h
univalue.h
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:25
CTransaction::GetTotalSize
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Definition: transaction.cpp:95
SelectParams
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
Definition: chainparams.cpp:555
uint256::ZERO
static const uint256 ZERO
Definition: uint256.h:129
coins.h
CDataStream::SetVersion
void SetVersion(int n)
Definition: streams.h:362
EncodeHexTx
std::string EncodeHexTx(const CTransaction &tx, const int serializeFlags=0)
Definition: core_write.cpp:137
CTransaction::HasWitness
bool HasWitness() const
Definition: transaction.h:332
core_memusage.h
CTransaction::ToString
std::string ToString() const
Definition: transaction.cpp:100
GetTransactionWeight
static int64_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:146
CCoinsViewCache
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:213
fuzz.h
CTransaction::IsNull
bool IsNull() const
Definition: transaction.h:298
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
CTransaction::GetWitnessHash
const uint256 & GetWitnessHash() const
Definition: transaction.h:303
DUST_RELAY_TX_FEE
static const unsigned int DUST_RELAY_TX_FEE
Min feerate for defining dust.
Definition: policy.h:54
CTransaction::GetHash
const uint256 & GetHash() const
Definition: transaction.h:302
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
tx_check.h
GetVirtualTransactionSize
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:280
deserialize
constexpr deserialize_type deserialize
Definition: serialize.h:48
initialize_transaction
void initialize_transaction()
Definition: transaction.cpp:24
SignalsOptInRBF
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
INIT_PROTO_VERSION
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15
uint256::ONE
static const uint256 ONE
Definition: uint256.h:130