// Copyright (c) 2015 The Bitcoin Core developers
-// Distributed under the MIT/X11 software license, see the accompanying
-// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or https://www.opensource.org/licenses/mit-license.php .
#include "main.h"
#include "pow.h"
#include "util.h"
+#include "utiltest.h"
#include "test/test_bitcoin.h"
#include <boost/test/unit_test.hpp>
{
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus();
+ BOOST_CHECK_EQUAL(150, params.PoWTargetSpacing(0));
+
+ int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
+ int64_t nThisTime = 1000003570;
+ arith_uint256 bnAvg;
+ bnAvg.SetCompact(0x1d00ffff);
+ BOOST_CHECK_EQUAL(0x1d011998,
+ CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
+}
+
+
+BOOST_AUTO_TEST_CASE(get_next_work_blossom)
+{
+ const Consensus::Params& params = RegtestActivateBlossom(true);
+ BOOST_CHECK_EQUAL(75, params.PoWTargetSpacing(0));
+
+ int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
+ int64_t nThisTime = 1000001445;
+ arith_uint256 bnAvg;
+ bnAvg.SetCompact(0x1d00ffff);
+ BOOST_CHECK_GT(0x1d011998,
+ CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
- int64_t nLastRetargetTime = 1261130161; // Block #30240
- CBlockIndex pindexLast;
- pindexLast.nHeight = 32255;
- pindexLast.nTime = 1262152739; // Block #32255
- pindexLast.nBits = 0x1d00ffff;
- BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00d86a);
+ RegtestDeactivateBlossom();
}
/* Test the constraint on the upper bound for next work */
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus();
- int64_t nLastRetargetTime = 1231006505; // Block #0
- CBlockIndex pindexLast;
- pindexLast.nHeight = 2015;
- pindexLast.nTime = 1233061996; // Block #2015
- pindexLast.nBits = 0x1d00ffff;
- BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00ffff);
+ int64_t nLastRetargetTime = 1231006505;
+ int64_t nThisTime = 1233061996;
+ arith_uint256 bnAvg;
+ // TODO change once the harder genesis block is generated
+ bnAvg.SetCompact(KOMODO_MINDIFF_NBITS);
+ BOOST_CHECK_EQUAL(KOMODO_MINDIFF_NBITS,
+ CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params));
}
/* Test the constraint on the lower bound for actual time taken */
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus();
- int64_t nLastRetargetTime = 1279008237; // Block #66528
- CBlockIndex pindexLast;
- pindexLast.nHeight = 68543;
- pindexLast.nTime = 1279297671; // Block #68543
- pindexLast.nBits = 0x1c05a3f4;
- BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1c0168fd);
+ int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
+ // 17*150*(1 - PoWMaxAdjustUp*PoWDampingFactor) = 918
+ // so we pick 917 to be outside of this window
+ int64_t nThisTime = 100000917;
+ arith_uint256 bnAvg;
+ bnAvg.SetCompact(0x1c05a3f4);
+ BOOST_CHECK_EQUAL(0x1c04bceb,
+ CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
+}
+
+BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual_blossom)
+{
+ const Consensus::Params& params = RegtestActivateBlossom(true);
+
+ int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
+ int64_t nThisTime = 1000000458;
+ arith_uint256 bnAvg;
+ bnAvg.SetCompact(0x1c05a3f4);
+ BOOST_CHECK_EQUAL(0x1c04bceb,
+ CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
+
+ RegtestDeactivateBlossom();
}
/* Test the constraint on the upper bound for actual time taken */
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params& params = Params().GetConsensus();
- int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
- CBlockIndex pindexLast;
- pindexLast.nHeight = 46367;
- pindexLast.nTime = 1269211443; // Block #46367
- pindexLast.nBits = 0x1c387f6f;
- BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00e1fd);
+ int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
+ // 17*150*(1 + maxAdjustDown*PoWDampingFactor) = 5814
+ int64_t nThisTime = 1000005815;
+ arith_uint256 bnAvg;
+ bnAvg.SetCompact(0x1c387f6f);
+ BOOST_CHECK_EQUAL(0x1c4a93bb,
+ CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
}
-BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
+BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual_blossom)
{
- SelectParams(CBaseChainParams::MAIN);
- const Consensus::Params& params = Params().GetConsensus();
+ const Consensus::Params& params = RegtestActivateBlossom(true);
+
+ int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
+ int64_t nThisTime = 1000002908;
+ arith_uint256 bnAvg;
+ bnAvg.SetCompact(0x1c387f6f);
+ BOOST_CHECK_EQUAL(0x1c4a93bb,
+ CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
+
+ RegtestDeactivateBlossom();
+}
+void GetBlockProofEquivalentTimeImpl(const Consensus::Params& params) {
std::vector<CBlockIndex> blocks(10000);
for (int i = 0; i < 10000; i++) {
blocks[i].pprev = i ? &blocks[i - 1] : NULL;
- blocks[i].nHeight = i;
+ blocks[i].SetHeight(i);
blocks[i].nTime = 1269211443 + i * params.nPowTargetSpacing;
blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
- blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
+ blocks[i].chainPower = CChainPower(&blocks[i]);
+ if (i != 0)
+ blocks[i].chainPower += GetBlockProof(blocks[i - 1]);
}
for (int j = 0; j < 1000; j++) {
}
}
+BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
+{
+ SelectParams(CBaseChainParams::MAIN);
+ GetBlockProofEquivalentTimeImpl(Params().GetConsensus());
+}
+
+BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test_blossom)
+{
+ GetBlockProofEquivalentTimeImpl(RegtestActivateBlossom(true));
+ RegtestDeactivateBlossom();
+}
+
BOOST_AUTO_TEST_SUITE_END()