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.

pow_tests.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) 2015 The Bitcoin Core developers
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "main.h"
  5. #include "pow.h"
  6. #include "util.h"
  7. #include "test/test_bitcoin.h"
  8. #include <boost/test/unit_test.hpp>
  9. using namespace std;
  10. BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
  11. /* Test calculation of next difficulty target with no constraints applying */
  12. BOOST_AUTO_TEST_CASE(get_next_work)
  13. {
  14. SelectParams(CBaseChainParams::MAIN);
  15. const Consensus::Params& params = Params().GetConsensus();
  16. int64_t nLastRetargetTime = 1261130161; // Block #30240
  17. CBlockIndex pindexLast;
  18. pindexLast.nHeight = 32255;
  19. pindexLast.nTime = 1262152739; // Block #32255
  20. pindexLast.nBits = 0x1d00ffff;
  21. BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00d86a);
  22. }
  23. /* Test the constraint on the upper bound for next work */
  24. BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
  25. {
  26. SelectParams(CBaseChainParams::MAIN);
  27. const Consensus::Params& params = Params().GetConsensus();
  28. int64_t nLastRetargetTime = 1231006505; // Block #0
  29. CBlockIndex pindexLast;
  30. pindexLast.nHeight = 2015;
  31. pindexLast.nTime = 1233061996; // Block #2015
  32. pindexLast.nBits = 0x1d00ffff;
  33. BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00ffff);
  34. }
  35. /* Test the constraint on the lower bound for actual time taken */
  36. BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
  37. {
  38. SelectParams(CBaseChainParams::MAIN);
  39. const Consensus::Params& params = Params().GetConsensus();
  40. int64_t nLastRetargetTime = 1279008237; // Block #66528
  41. CBlockIndex pindexLast;
  42. pindexLast.nHeight = 68543;
  43. pindexLast.nTime = 1279297671; // Block #68543
  44. pindexLast.nBits = 0x1c05a3f4;
  45. BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1c0168fd);
  46. }
  47. /* Test the constraint on the upper bound for actual time taken */
  48. BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
  49. {
  50. SelectParams(CBaseChainParams::MAIN);
  51. const Consensus::Params& params = Params().GetConsensus();
  52. int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
  53. CBlockIndex pindexLast;
  54. pindexLast.nHeight = 46367;
  55. pindexLast.nTime = 1269211443; // Block #46367
  56. pindexLast.nBits = 0x1c387f6f;
  57. BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00e1fd);
  58. }
  59. BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
  60. {
  61. SelectParams(CBaseChainParams::MAIN);
  62. const Consensus::Params& params = Params().GetConsensus();
  63. std::vector<CBlockIndex> blocks(10000);
  64. for (int i = 0; i < 10000; i++) {
  65. blocks[i].pprev = i ? &blocks[i - 1] : NULL;
  66. blocks[i].nHeight = i;
  67. blocks[i].nTime = 1269211443 + i * params.nPowTargetSpacing;
  68. blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
  69. blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
  70. }
  71. for (int j = 0; j < 1000; j++) {
  72. CBlockIndex *p1 = &blocks[GetRand(10000)];
  73. CBlockIndex *p2 = &blocks[GetRand(10000)];
  74. CBlockIndex *p3 = &blocks[GetRand(10000)];
  75. int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, params);
  76. BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
  77. }
  78. }
  79. BOOST_AUTO_TEST_SUITE_END()