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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2015-2016 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. 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. CCoinsViewDB *pcoinsdbview;
  51. fs::path pathTemp;
  52. boost::thread_group threadGroup;
  53. CConnman* connman;
  54. CScheduler scheduler;
  55. std::unique_ptr<PeerLogicValidation> peerLogic;
  56. TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
  57. ~TestingSetup();
  58. };
  59. class CBlock;
  60. struct CMutableTransaction;
  61. class CScript;
  62. //
  63. // Testing fixture that pre-creates a
  64. // 100-block REGTEST-mode block chain
  65. //
  66. struct TestChain100Setup : public TestingSetup {
  67. TestChain100Setup();
  68. // Create a new block with just given transactions, coinbase paying to
  69. // scriptPubKey, and try to add it to the current chain.
  70. CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
  71. const CScript& scriptPubKey);
  72. ~TestChain100Setup();
  73. std::vector<CTransaction> coinbaseTxns; // For convenience, coinbase transactions
  74. CKey coinbaseKey; // private/public key needed to spend coinbase transactions
  75. };
  76. class CTxMemPoolEntry;
  77. struct TestMemPoolEntryHelper
  78. {
  79. // Default values
  80. CAmount nFee;
  81. int64_t nTime;
  82. unsigned int nHeight;
  83. bool spendsCoinbase;
  84. unsigned int sigOpCost;
  85. LockPoints lp;
  86. TestMemPoolEntryHelper() :
  87. nFee(0), nTime(0), nHeight(1),
  88. spendsCoinbase(false), sigOpCost(4) { }
  89. CTxMemPoolEntry FromTx(const CMutableTransaction &tx);
  90. CTxMemPoolEntry FromTx(const CTransaction &tx);
  91. // Change the default value
  92. TestMemPoolEntryHelper &Fee(CAmount _fee) { nFee = _fee; return *this; }
  93. TestMemPoolEntryHelper &Time(int64_t _time) { nTime = _time; return *this; }
  94. TestMemPoolEntryHelper &Height(unsigned int _height) { nHeight = _height; return *this; }
  95. TestMemPoolEntryHelper &SpendsCoinbase(bool _flag) { spendsCoinbase = _flag; return *this; }
  96. TestMemPoolEntryHelper &SigOpsCost(unsigned int _sigopsCost) { sigOpCost = _sigopsCost; return *this; }
  97. };
  98. #endif