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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) 2015 The Starwels 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 "chain.h"
  5. #include "chainparams.h"
  6. #include "pow.h"
  7. #include "random.h"
  8. #include "util.h"
  9. #include "test/test_starwels.h"
  10. #include <boost/test/unit_test.hpp>
  11. BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
  12. /* Test calculation of next difficulty target with no constraints applying */
  13. BOOST_AUTO_TEST_CASE(get_next_work)
  14. {
  15. const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
  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, chainParams->GetConsensus()), 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. const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
  27. int64_t nLastRetargetTime = 1484870400; // Block #0
  28. CBlockIndex pindexLast;
  29. pindexLast.nHeight = 2015;
  30. pindexLast.nTime = 1517408676; // Block #2015
  31. pindexLast.nBits = 0x1d00ffff;
  32. BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00ffff);
  33. }
  34. /* Test the constraint on the lower bound for actual time taken */
  35. BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
  36. {
  37. const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
  38. int64_t nLastRetargetTime = 1279008237; // Block #66528
  39. CBlockIndex pindexLast;
  40. pindexLast.nHeight = 68543;
  41. pindexLast.nTime = 1279297671; // Block #68543
  42. pindexLast.nBits = 0x1c05a3f4;
  43. BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1c0168fd);
  44. }
  45. /* Test the constraint on the upper bound for actual time taken */
  46. BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
  47. {
  48. const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
  49. int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
  50. CBlockIndex pindexLast;
  51. pindexLast.nHeight = 46367;
  52. pindexLast.nTime = 1269211443; // Block #46367
  53. pindexLast.nBits = 0x1c387f6f;
  54. BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00e1fd);
  55. }
  56. BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
  57. {
  58. const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
  59. std::vector<CBlockIndex> blocks(10000);
  60. for (int i = 0; i < 10000; i++) {
  61. blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
  62. blocks[i].nHeight = i;
  63. blocks[i].nTime = 1269211443 + i * chainParams->GetConsensus().nPowTargetSpacing;
  64. blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
  65. blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
  66. }
  67. for (int j = 0; j < 1000; j++) {
  68. CBlockIndex *p1 = &blocks[InsecureRandRange(10000)];
  69. CBlockIndex *p2 = &blocks[InsecureRandRange(10000)];
  70. CBlockIndex *p3 = &blocks[InsecureRandRange(10000)];
  71. int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, chainParams->GetConsensus());
  72. BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
  73. }
  74. }
  75. BOOST_AUTO_TEST_SUITE_END()