PIVX Core  5.6.99
P2P Digital Currency
test_pivx.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2022 The PIVX 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 #ifndef PIVX_TEST_TEST_PIVX_H
6 #define PIVX_TEST_TEST_PIVX_H
7 
8 #include "fs.h"
9 #include "scheduler.h"
10 #include "txdb.h"
11 
12 #include <stdexcept>
13 
14 #include <boost/thread.hpp>
15 
17 
21 extern bool g_mock_deterministic_tests;
22 
23 enum class SeedRand {
24  ZEROS,
25  SEED,
26 };
27 
29 void Seed(FastRandomContext& ctx);
30 
31 static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
32 {
33  if (seed == SeedRand::ZEROS) {
34  g_insecure_rand_ctx = FastRandomContext(/* deterministic */ true);
35  } else {
37  }
38 }
39 
40 static inline uint16_t InsecureRand16() { return g_insecure_rand_ctx.rand16(); }
41 static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }
42 static inline uint256 InsecureRand256() { return g_insecure_rand_ctx.rand256(); }
43 static inline uint64_t InsecureRandBits(int bits) { return g_insecure_rand_ctx.randbits(bits); }
44 static inline uint64_t InsecureRandRange(uint64_t range) { return g_insecure_rand_ctx.randrange(range); }
45 static inline bool InsecureRandBool() { return g_insecure_rand_ctx.randbool(); }
46 static inline std::vector<unsigned char> InsecureRandBytes(size_t len) { return g_insecure_rand_ctx.randbytes(len); }
47 
53 
54  explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
56 
57  fs::path SetDataDir(const std::string& name);
58 
59 private:
60  const fs::path m_path_root;
61 };
62 
67 class CConnman;
71 {
72  boost::thread_group threadGroup;
76  std::unique_ptr<PeerLogicValidation> peerLogic;
77 
78  explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
79  ~TestingSetup();
80 };
81 
82 struct TestnetSetup : public TestingSetup
83 {
85 };
86 
88 {
90 };
91 
92 // Helper to finalize a modified PoW block.
93 std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock);
94 
95 class CBlock;
96 struct CMutableTransaction;
97 class CScript;
98 
99 // Test chain only available on regtest
101 {
102  explicit TestChainSetup(int blockCount);
103  ~TestChainSetup();
104 
105  // Create a new block with coinbase paying to scriptPubKey, and try to add it to the current chain.
106  // Include given transactions, and, if fNoMempoolTx=true, remove transactions coming from the mempool.
107  CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey, bool fNoMempoolTx = true);
108  CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CKey& scriptKey);
109  CBlock CreateBlock(const std::vector<CMutableTransaction>& txns,
110  const CScript& scriptPubKey,
111  bool fNoMempoolTx = true,
112  bool fTestBlockValidity = true,
113  bool fIncludeQfc = true,
114  CBlockIndex* customPrevBlock = nullptr);
115  CBlock CreateBlock(const std::vector<CMutableTransaction>& txns, const CKey& scriptKey, bool fTestBlockValidity = true);
116 
117  std::vector<CTransaction> coinbaseTxns; // For convenience, coinbase transactions
118  CKey coinbaseKey; // private/public key needed to spend coinbase transactions
119 };
120 
121 // Testing fixture that pre-creates a 100-block REGTEST-mode blockchain
123 {
125 };
126 
127 // Testing fixture that pre-creates a 400-block REGTEST-mode blockchain
128 // all 400 blocks are PoW. PoS starts at height 500
130 {
132 };
133 
134 class CTxMemPoolEntry;
135 
137 {
138  // Default values
140  int64_t nTime;
141  unsigned int nHeight;
143  unsigned int sigOpCount;
144 
146  nFee(0), nTime(0), nHeight(1),
148 
151 
152  // Change the default value
153  TestMemPoolEntryHelper &Fee(CAmount _fee) { nFee = _fee; return *this; }
154  TestMemPoolEntryHelper &Time(int64_t _time) { nTime = _time; return *this; }
155  TestMemPoolEntryHelper &Height(unsigned int _height) { nHeight = _height; return *this; }
157  TestMemPoolEntryHelper &SigOps(unsigned int _sigops) { sigOpCount = _sigops; return *this; }
158 };
159 
160 // define an implicit conversion here so that uint256 may be used directly in BOOST_CHECK_*
161 std::ostream& operator<<(std::ostream& os, const uint256& num);
162 
169 {
170 public:
171  explicit HasReason(const std::string& reason) : m_reason(reason) {}
172  bool operator()(const std::exception& e) const
173  {
174  return std::string(e.what()).find(m_reason) != std::string::npos;
175  };
176 
177 private:
178  const std::string m_reason;
179 };
180 
181 #endif // PIVX_TEST_TEST_PIVX_H
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
false
Definition: bls_dkg.cpp:151
CBaseChainParams defines the base parameters (shared between pivx-cli and pivxd) of a given instance ...
static const std::string MAIN
Chain name strings.
Definition: block.h:80
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:139
Definition: net.h:145
An encapsulated private key.
Definition: key.h:30
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:244
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:55
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:273
Fast randomness source.
Definition: random.h:107
uint32_t rand32() noexcept
Generate a random 32-bit integer.
Definition: random.h:191
uint16_t rand16()
Generate a random 16-bit integer.
Definition: random.h:188
uint256 rand256() noexcept
generate a random uint256.
Definition: random.cpp:621
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:197
std::vector< unsigned char > randbytes(size_t len)
Generate random bytes.
Definition: random.cpp:632
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
Definition: random.h:159
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:174
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: test_pivx.h:169
HasReason(const std::string &reason)
Definition: test_pivx.h:171
const std::string m_reason
Definition: test_pivx.h:175
bool operator()(const std::exception &e) const
Definition: test_pivx.h:172
256-bit opaque blob.
Definition: uint256.h:138
const char * name
Definition: rest.cpp:37
Basic testing setup.
Definition: test_pivx.h:51
BasicTestingSetup(const std::string &chainName=CBaseChainParams::MAIN)
Definition: test_pivx.cpp:67
fs::path SetDataDir(const std::string &name)
Definition: test_pivx.cpp:89
ECCVerifyHandle globalVerifyHandle
Definition: test_pivx.h:52
const fs::path m_path_root
Definition: test_pivx.h:60
A mutable version of CTransaction.
Definition: transaction.h:409
TestChainSetup(int blockCount)
Definition: test_pivx.cpp:163
std::vector< CTransaction > coinbaseTxns
Definition: test_pivx.h:117
CBlock CreateBlock(const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey, bool fNoMempoolTx=true, bool fTestBlockValidity=true, bool fIncludeQfc=true, CBlockIndex *customPrevBlock=nullptr)
Definition: test_pivx.cpp:197
CBlock CreateAndProcessBlock(const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey, bool fNoMempoolTx=true)
Definition: test_pivx.cpp:184
CKey coinbaseKey
Definition: test_pivx.h:118
Definition: test_pivx.h:137
TestMemPoolEntryHelper & SigOps(unsigned int _sigops)
Definition: test_pivx.h:157
TestMemPoolEntryHelper()
Definition: test_pivx.h:145
CAmount nFee
Definition: test_pivx.h:139
unsigned int sigOpCount
Definition: test_pivx.h:143
TestMemPoolEntryHelper & SpendsCoinbaseOrCoinstake(bool _flag)
Definition: test_pivx.h:156
bool spendsCoinbaseOrCoinstake
Definition: test_pivx.h:142
int64_t nTime
Definition: test_pivx.h:140
TestMemPoolEntryHelper & Height(unsigned int _height)
Definition: test_pivx.h:155
unsigned int nHeight
Definition: test_pivx.h:141
TestMemPoolEntryHelper & Time(int64_t _time)
Definition: test_pivx.h:154
TestMemPoolEntryHelper & Fee(CAmount _fee)
Definition: test_pivx.h:153
CTxMemPoolEntry FromTx(const CMutableTransaction &tx)
Definition: test_pivx.cpp:250
EvoNotificationInterface * pEvoNotificationInterface
Definition: test_pivx.h:74
CScheduler scheduler
Definition: test_pivx.h:75
CConnman * connman
Definition: test_pivx.h:73
TestingSetup(const std::string &chainName=CBaseChainParams::MAIN)
Definition: test_pivx.cpp:97
boost::thread_group threadGroup
Definition: test_pivx.h:72
std::unique_ptr< PeerLogicValidation > peerLogic
Definition: test_pivx.h:76
std::ostream & operator<<(std::ostream &os, const uint256 &num)
Definition: test_pivx.cpp:61
SeedRand
Definition: test_pivx.h:23
@ ZEROS
Seed with a compile time constant of zeros.
@ SEED
Call the Seed() helper.
bool g_mock_deterministic_tests
Flag to make GetRand in random.h return the same number.
Definition: random.cpp:584
std::shared_ptr< CBlock > FinalizeBlock(std::shared_ptr< CBlock > pblock)
Definition: test_pivx.cpp:239
void Seed(FastRandomContext &ctx)
Seed the given random ctx or use the seed passed in via an environment var.
Definition: test_pivx.cpp:47
FastRandomContext g_insecure_rand_ctx
Definition: test_pivx.cpp:35