2015-02-21 13:57:44 +01:00
|
|
|
// 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.
|
|
|
|
|
2015-07-05 14:17:46 +02:00
|
|
|
#include "chain.h"
|
|
|
|
#include "chainparams.h"
|
2015-02-21 13:57:44 +01:00
|
|
|
#include "pow.h"
|
2015-07-05 14:17:46 +02:00
|
|
|
#include "random.h"
|
2015-02-21 13:57:44 +01:00
|
|
|
#include "util.h"
|
2015-03-12 09:34:42 +01:00
|
|
|
#include "test/test_bitcoin.h"
|
2015-02-21 13:57:44 +01:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
|
2015-02-21 13:57:44 +01:00
|
|
|
|
|
|
|
/* Test calculation of next difficulty target with no constraints applying */
|
|
|
|
BOOST_AUTO_TEST_CASE(get_next_work)
|
|
|
|
{
|
2016-10-21 01:33:08 +02:00
|
|
|
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
|
2015-02-21 13:57:44 +01:00
|
|
|
int64_t nLastRetargetTime = 1261130161; // Block #30240
|
|
|
|
CBlockIndex pindexLast;
|
|
|
|
pindexLast.nHeight = 32255;
|
|
|
|
pindexLast.nTime = 1262152739; // Block #32255
|
|
|
|
pindexLast.nBits = 0x1d00ffff;
|
2016-10-21 01:33:08 +02:00
|
|
|
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00d86a);
|
2015-02-21 13:57:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test the constraint on the upper bound for next work */
|
|
|
|
BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
|
|
|
|
{
|
2016-10-21 01:33:08 +02:00
|
|
|
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
|
2015-02-21 13:57:44 +01:00
|
|
|
int64_t nLastRetargetTime = 1231006505; // Block #0
|
|
|
|
CBlockIndex pindexLast;
|
|
|
|
pindexLast.nHeight = 2015;
|
|
|
|
pindexLast.nTime = 1233061996; // Block #2015
|
|
|
|
pindexLast.nBits = 0x1d00ffff;
|
2016-10-21 01:33:08 +02:00
|
|
|
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00ffff);
|
2015-02-21 13:57:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test the constraint on the lower bound for actual time taken */
|
|
|
|
BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
|
|
|
|
{
|
2016-10-21 01:33:08 +02:00
|
|
|
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
|
2015-02-21 13:57:44 +01:00
|
|
|
int64_t nLastRetargetTime = 1279008237; // Block #66528
|
|
|
|
CBlockIndex pindexLast;
|
|
|
|
pindexLast.nHeight = 68543;
|
|
|
|
pindexLast.nTime = 1279297671; // Block #68543
|
|
|
|
pindexLast.nBits = 0x1c05a3f4;
|
2016-10-21 01:33:08 +02:00
|
|
|
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1c0168fd);
|
2015-02-21 13:57:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test the constraint on the upper bound for actual time taken */
|
|
|
|
BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
|
|
|
|
{
|
2016-10-21 01:33:08 +02:00
|
|
|
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
|
2015-02-21 13:57:44 +01:00
|
|
|
int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
|
|
|
|
CBlockIndex pindexLast;
|
|
|
|
pindexLast.nHeight = 46367;
|
|
|
|
pindexLast.nTime = 1269211443; // Block #46367
|
|
|
|
pindexLast.nBits = 0x1c387f6f;
|
2016-10-21 01:33:08 +02:00
|
|
|
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00e1fd);
|
2015-02-21 13:57:44 +01:00
|
|
|
}
|
|
|
|
|
2015-03-17 14:35:59 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
|
|
|
|
{
|
2016-10-21 01:33:08 +02:00
|
|
|
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
|
2015-03-17 14:35:59 +01:00
|
|
|
std::vector<CBlockIndex> blocks(10000);
|
|
|
|
for (int i = 0; i < 10000; i++) {
|
|
|
|
blocks[i].pprev = i ? &blocks[i - 1] : NULL;
|
|
|
|
blocks[i].nHeight = i;
|
2016-10-21 01:33:08 +02:00
|
|
|
blocks[i].nTime = 1269211443 + i * chainParams->GetConsensus().nPowTargetSpacing;
|
2015-03-17 14:35:59 +01:00
|
|
|
blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
|
|
|
|
blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < 1000; j++) {
|
|
|
|
CBlockIndex *p1 = &blocks[GetRand(10000)];
|
|
|
|
CBlockIndex *p2 = &blocks[GetRand(10000)];
|
|
|
|
CBlockIndex *p3 = &blocks[GetRand(10000)];
|
|
|
|
|
2016-10-21 01:33:08 +02:00
|
|
|
int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, chainParams->GetConsensus());
|
2015-03-17 14:35:59 +01:00
|
|
|
BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 13:57:44 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|