You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_bitcoin.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) 2015-2017 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. #ifndef BITCOIN_TEST_TEST_BITCOIN_H
  5. #define BITCOIN_TEST_TEST_BITCOIN_H
  6. #include <chainparamsbase.h>
  7. #include <fs.h>
  8. #include <key.h>
  9. #include <pubkey.h>
  10. #include <random.h>
  11. #include <scheduler.h>
  12. #include <txdb.h>
  13. #include <txmempool.h>
  14. #include <boost/thread.hpp>
  15. extern uint256 insecure_rand_seed;
  16. extern FastRandomContext insecure_rand_ctx;
  17. static inline void SeedInsecureRand(bool fDeterministic = false)
  18. {
  19. if (fDeterministic) {
  20. insecure_rand_seed = uint256();
  21. } else {
  22. insecure_rand_seed = GetRandHash();
  23. }
  24. insecure_rand_ctx = FastRandomContext(insecure_rand_seed);
  25. }
  26. static inline uint32_t InsecureRand32() { return insecure_rand_ctx.rand32(); }
  27. static inline uint256 InsecureRand256() { return insecure_rand_ctx.rand256(); }
  28. static inline uint64_t InsecureRandBits(int bits) { return insecure_rand_ctx.randbits(bits); }
  29. static inline uint64_t InsecureRandRange(uint64_t range) { return insecure_rand_ctx.randrange(range); }
  30. static inline bool InsecureRandBool() { return insecure_rand_ctx.randbool(); }
  31. /** Basic testing setup.
  32. * This just configures logging and chain parameters.
  33. */
  34. struct BasicTestingSetup {
  35. ECCVerifyHandle globalVerifyHandle;
  36. explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
  37. ~BasicTestingSetup();
  38. };
  39. /** Testing setup that configures a complete environment.
  40. * Included are data directory, coins database, script check threads setup.
  41. */
  42. class CConnman;
  43. class CNode;
  44. struct CConnmanTest {
  45. static void AddNode(CNode& node);
  46. static void ClearNodes();
  47. };
  48. class PeerLogicValidation;
  49. struct TestingSetup: public BasicTestingSetup {
  50. fs::path pathTemp;
  51. boost::thread_group threadGroup;
  52. CConnman* connman;
  53. CScheduler scheduler;
  54. std::unique_ptr<PeerLogicValidation> peerLogic;
  55. explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
  56. ~TestingSetup();
  57. };
  58. class CBlock;
  59. struct CMutableTransaction;
  60. class CScript;
  61. //
  62. // Testing fixture that pre-creates a
  63. // 100-block REGTEST-mode block chain
  64. //
  65. struct TestChain100Setup : public TestingSetup {
  66. TestChain100Setup();
  67. // Create a new block with just given transactions, coinbase paying to
  68. // scriptPubKey, and try to add it to the current chain.
  69. CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
  70. const CScript& scriptPubKey);
  71. ~TestChain100Setup();
  72. std::vector<CTransaction> coinbaseTxns; // For convenience, coinbase transactions
  73. CKey coinbaseKey; // private/public key needed to spend coinbase transactions
  74. };
  75. class CTxMemPoolEntry;
  76. struct TestMemPoolEntryHelper
  77. {
  78. // Default values
  79. CAmount nFee;
  80. int64_t nTime;
  81. unsigned int nHeight;
  82. bool spendsCoinbase;
  83. unsigned int sigOpCost;
  84. LockPoints lp;
  85. TestMemPoolEntryHelper() :
  86. nFee(0), nTime(0), nHeight(1),
  87. spendsCoinbase(false), sigOpCost(4) { }
  88. CTxMemPoolEntry FromTx(const CMutableTransaction &tx);
  89. CTxMemPoolEntry FromTx(const CTransaction &tx);
  90. // Change the default value
  91. TestMemPoolEntryHelper &Fee(CAmount _fee) { nFee = _fee; return *this; }
  92. TestMemPoolEntryHelper &Time(int64_t _time) { nTime = _time; return *this; }
  93. TestMemPoolEntryHelper &Height(unsigned int _height) { nHeight = _height; return *this; }
  94. TestMemPoolEntryHelper &SpendsCoinbase(bool _flag) { spendsCoinbase = _flag; return *this; }
  95. TestMemPoolEntryHelper &SigOpsCost(unsigned int _sigopsCost) { sigOpCost = _sigopsCost; return *this; }
  96. };
  97. CBlock getBlock13b8a();
  98. // define an implicit conversion here so that uint256 may be used directly in BOOST_CHECK_*
  99. std::ostream& operator<<(std::ostream& os, const uint256& num);
  100. #endif