diff --git a/src/gtest/test_pow.cpp b/src/gtest/test_pow.cpp index 998031295..ee05b69ec 100644 --- a/src/gtest/test_pow.cpp +++ b/src/gtest/test_pow.cpp @@ -22,7 +22,10 @@ TEST(PoW, DifficultyAveraging) { } // Result should be the same as if last difficulty was used - EXPECT_EQ(CalculateNextWorkRequired(&blocks[lastBlk], + arith_uint256 bnAvg; + bnAvg.SetCompact(blocks[lastBlk].nBits); + EXPECT_EQ(CalculateNextWorkRequired(bnAvg, + blocks[lastBlk].GetMedianTimePast(), blocks[firstBlk].GetMedianTimePast(), params), GetNextWorkRequired(&blocks[lastBlk], nullptr, params)); @@ -35,7 +38,9 @@ TEST(PoW, DifficultyAveraging) { blocks[lastBlk].nTime += GetRand(params.nPowTargetSpacing/2) + 1; // Result should be the same as if last difficulty was used - EXPECT_EQ(CalculateNextWorkRequired(&blocks[lastBlk], + bnAvg.SetCompact(blocks[lastBlk].nBits); + EXPECT_EQ(CalculateNextWorkRequired(bnAvg, + blocks[lastBlk].GetMedianTimePast(), blocks[firstBlk].GetMedianTimePast(), params), GetNextWorkRequired(&blocks[lastBlk], nullptr, params)); @@ -46,7 +51,9 @@ TEST(PoW, DifficultyAveraging) { blocks[lastBlk].nBits = 0x1e0fffff; // Result should not be the same as if last difficulty was used - EXPECT_NE(CalculateNextWorkRequired(&blocks[lastBlk], + bnAvg.SetCompact(blocks[lastBlk].nBits); + EXPECT_NE(CalculateNextWorkRequired(bnAvg, + blocks[lastBlk].GetMedianTimePast(), blocks[firstBlk].GetMedianTimePast(), params), GetNextWorkRequired(&blocks[lastBlk], nullptr, params)); diff --git a/src/pow.cpp b/src/pow.cpp index 0af573222..18873d4ee 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -43,14 +43,6 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead return CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params); } -// Left for testing purposes -unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params) -{ - arith_uint256 bnAvg; - bnAvg.SetCompact(pindexLast->nBits); - return CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), nFirstBlockTime, params); -} - unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, int64_t nLastBlockTime, int64_t nFirstBlockTime, const Consensus::Params& params) diff --git a/src/pow.h b/src/pow.h index 99f53c395..30d0f5e63 100644 --- a/src/pow.h +++ b/src/pow.h @@ -17,7 +17,6 @@ class uint256; class arith_uint256; unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params&); -unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params&); unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, int64_t nLastBlockTime, int64_t nFirstBlockTime, const Consensus::Params&); diff --git a/src/test/pow_tests.cpp b/src/test/pow_tests.cpp index 6ca9d5f7c..f780d313d 100644 --- a/src/test/pow_tests.cpp +++ b/src/test/pow_tests.cpp @@ -20,12 +20,11 @@ BOOST_AUTO_TEST_CASE(get_next_work) const Consensus::Params& params = Params().GetConsensus(); int64_t nLastRetargetTime = 1262149169; // NOTE: Not an actual block time - CBlockIndex pindexLast; - pindexLast.nHeight = 32255; - pindexLast.nTime = 1262152739; // Block #32255 of Bitcoin - // This represents an average difficulty in the current algorithm - pindexLast.nBits = 0x1d00ffff; - BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d011998); + int64_t nThisTime = 1262152739; // Block #32255 of Bitcoin + arith_uint256 bnAvg; + bnAvg.SetCompact(0x1d00ffff); + BOOST_CHECK_EQUAL(0x1d011998, + CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params)); } /* Test the constraint on the upper bound for next work */ @@ -35,13 +34,12 @@ BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) const Consensus::Params& params = Params().GetConsensus(); int64_t nLastRetargetTime = 1231006505; // Block #0 of Bitcoin - CBlockIndex pindexLast; - pindexLast.nHeight = 2015; - pindexLast.nTime = 1233061996; // Block #2015 of Bitcoin - // This represents an average difficulty in the current algorithm + int64_t nThisTime = 1233061996; // Block #2015 of Bitcoin + arith_uint256 bnAvg; // TODO change once the harder genesis block is generated - pindexLast.nBits = 0x200f0f0f; - BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x200f0f0f); + bnAvg.SetCompact(0x200f0f0f); + BOOST_CHECK_EQUAL(0x200f0f0f, + CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params)); } /* Test the constraint on the lower bound for actual time taken */ @@ -51,12 +49,11 @@ BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual) const Consensus::Params& params = Params().GetConsensus(); int64_t nLastRetargetTime = 1279296753; // NOTE: Not an actual block time - CBlockIndex pindexLast; - pindexLast.nHeight = 68543; - pindexLast.nTime = 1279297671; // Block #68543 of Bitcoin - // This represents an average difficulty in the current algorithm - pindexLast.nBits = 0x1c05a3f4; - BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1c04bceb); + int64_t nThisTime = 1279297671; // Block #68543 of Bitcoin + arith_uint256 bnAvg; + bnAvg.SetCompact(0x1c05a3f4); + BOOST_CHECK_EQUAL(0x1c04bceb, + CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params)); } /* Test the constraint on the upper bound for actual time taken */ @@ -66,12 +63,11 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual) const Consensus::Params& params = Params().GetConsensus(); int64_t nLastRetargetTime = 1269205629; // NOTE: Not an actual block time - CBlockIndex pindexLast; - pindexLast.nHeight = 46367; - pindexLast.nTime = 1269211443; // Block #46367 of Bitcoin - // This represents an average difficulty in the current algorithm - pindexLast.nBits = 0x1c387f6f; - BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1c4a93bb); + int64_t nThisTime = 1269211443; // Block #46367 of Bitcoin + arith_uint256 bnAvg; + bnAvg.SetCompact(0x1c387f6f); + BOOST_CHECK_EQUAL(0x1c4a93bb, + CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params)); } BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)