PIVX Core  5.6.99
P2P Digital Currency
transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 The Bitcoin Core developers
2 // Copyright (c) 2021 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
5 
6 #include "consensus/tx_verify.h"
7 #include "consensus/validation.h"
8 #include "core_io.h"
9 #include "policy/policy.h"
10 #include "primitives/transaction.h"
11 #include "streams.h"
12 #include "test/fuzz/fuzz.h"
13 #include "version.h"
14 
15 #include <cassert>
16 
19 
20 void test_one_input(std::vector<uint8_t> buffer)
21 {
22  CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION);
23  try {
24  int nVersion;
25  ds >> nVersion;
26  ds.SetVersion(nVersion);
27  } catch (const std::ios_base::failure&) {
28  return;
29  }
30  unsigned int nBlockHeight = 0;
31  try {
32  ds >> nBlockHeight;
33  } catch (const std::ios_base::failure& e) {
34  return;
35  }
36  bool valid_tx = true;
37  const CTransaction tx = [&] {
38  try {
39  return CTransaction(deserialize, ds);
40  } catch (const std::ios_base::failure&) {
41  valid_tx = false;
42  return CTransaction();
43  }
44  }();
45  bool valid_mutable_tx = true;
46  CDataStream ds_mtx(buffer, SER_NETWORK, INIT_PROTO_VERSION);
47  CMutableTransaction mutable_tx;
48  try {
49  int nVersion;
50  ds_mtx >> nVersion;
51  ds_mtx.SetVersion(nVersion);
52  unsigned int nBlockHeight2 = 0;
53  ds_mtx >> nBlockHeight2;
54  ds_mtx >> mutable_tx;
55  } catch (const std::ios_base::failure&) {
56  valid_mutable_tx = false;
57  }
58  assert(valid_tx == valid_mutable_tx);
59  if (!valid_tx) {
60  return;
61  }
62 
63  // Cold staking flag disabled
64  CValidationState state_with_all_flags_disabled;
65  CheckTransactionAllFlagsDisabled(tx, state_with_all_flags_disabled);
66 
67  // Cold Staking enabled
68  CValidationState state_with_cold_staking_enabled;
69  CheckTransactionColdStakingEnabled(tx, state_with_cold_staking_enabled);
70 
71  CTransactionRef ptx = MakeTransactionRef(tx);
72  std::string reason;
73  IsStandardTx(ptx, nBlockHeight,reason);
74 
75  CValidationState contextualCheckState;
76  ContextualCheckTransaction(ptx, contextualCheckState, Params(), nBlockHeight, true, false);
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.IsCoinBase();
85  (void)tx.IsCoinStake();
86  (void)tx.IsShieldedTx();
87  (void)tx.IsSpecialTx();
88  (void)tx.IsNormalType();
89  (void)tx.isSaplingVersion();
90  (void)tx.hasSaplingData();
91  (void)tx.HasP2CSOutputs();
92  (void)tx.ContainsZerocoins();
93  (void)tx.IsNull();
94  (void)tx.ToString();
95 
96 
97  (void)EncodeHexTx(tx);
98  (void)GetLegacySigOpCount(tx);
99  (void)IsFinalTx(ptx, nBlockHeight, /* nBlockTime= */ 1024);
100  (void)IsStandardTx(ptx, nBlockHeight, reason);
101 }
102 
104  return CheckTransaction(tx, state, false);
105 }
106 
108  return CheckTransaction(tx, state, true);
109 }
110 
const CChainParams & Params()
Return the currently selected parameters.
void SetVersion(int n)
Definition: streams.h:259
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:244
bool IsNormalType() const
Definition: transaction.h:334
bool IsNull() const
Definition: transaction.h:297
bool IsShieldedTx() const
Definition: transaction.h:319
bool ContainsZerocoins() const
Definition: transaction.h:371
const uint256 & GetHash() const
Definition: transaction.h:301
std::string ToString() const
unsigned int GetTotalSize() const
bool IsCoinBase() const
Definition: transaction.h:376
bool IsSpecialTx() const
Definition: transaction.h:329
CAmount GetValueOut() const
bool HasP2CSOutputs() const
bool isSaplingVersion() const
Definition: transaction.h:314
bool IsCoinStake() const
bool hasSaplingData() const
Definition: transaction.h:305
Capture information about block/transaction validation.
Definition: validation.h:24
std::string EncodeHexTx(const CTransaction &tx)
Definition: core_write.cpp:116
std::vector< uint8_t > buffer
Definition: fuzz.cpp:71
bool IsStandardTx(const CTransactionRef &tx, int nBlockHeight, std::string &reason)
Check for standard transaction types.
Definition: policy.cpp:100
@ SER_NETWORK
Definition: serialize.h:174
constexpr deserialize_type deserialize
Definition: serialize.h:57
A mutable version of CTransaction.
Definition: transaction.h:409
void test_one_input(std::vector< uint8_t > buffer)
Definition: transaction.cpp:20
bool CheckTransactionColdStakingEnabled(const CTransaction &tx, CValidationState state)
bool CheckTransactionAllFlagsDisabled(const CTransaction &tx, CValidationState state)
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:456
bool IsFinalTx(const CTransactionRef &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:12
bool CheckTransaction(const CTransaction &tx, CValidationState &state, bool fColdStakingActive)
Transaction validation functions.
Definition: tx_verify.cpp:54
bool ContextualCheckTransaction(const CTransactionRef &tx, CValidationState &state, const CChainParams &chainparams, int nHeight, bool isMined, bool fIBD)
Context-dependent validity checks.
Definition: tx_verify.cpp:134
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Count ECDSA signature operations the old-fashioned (pre-0.6) way.
Definition: tx_verify.cpp:27