Skip to content

Commit

Permalink
Merge pull request bitcoin#2 from h4x3rotab/master
Browse files Browse the repository at this point in the history
PoW retargeting change for BTG hard fork permine proid.
  • Loading branch information
h4x3rotab committed Sep 12, 2017
2 parents a3a0d2a + 925606b commit 6b4e56d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class CMainParams : public CChainParams {
consensus.BIP34Hash = uint256S("0x000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8");
consensus.BIP65Height = 388381; // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
consensus.BIP66Height = 363725; // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
consensus.BTGHeight = 487427; // Around 10/1/2017 12:00 UTC
consensus.BTGPremineWindow = 16000;
consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
consensus.nPowTargetSpacing = 10 * 60;
Expand Down Expand Up @@ -183,6 +185,8 @@ class CTestNetParams : public CChainParams {
consensus.BIP34Hash = uint256S("0x0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8");
consensus.BIP65Height = 581885; // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
consensus.BIP66Height = 330776; // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
consensus.BTGHeight = 100000000; // Not activated yet.
consensus.BTGPremineWindow = 16000;
consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
consensus.nPowTargetSpacing = 10 * 60;
Expand Down Expand Up @@ -271,6 +275,8 @@ class CRegTestParams : public CChainParams {
consensus.BIP34Hash = uint256();
consensus.BIP65Height = 1351; // BIP65 activated on regtest (Used in rpc activation tests)
consensus.BIP66Height = 1251; // BIP66 activated on regtest (Used in rpc activation tests)
consensus.BTGHeight = 100000000; // Not activated yet.
consensus.BTGPremineWindow = 16000;
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
consensus.nPowTargetSpacing = 10 * 60;
Expand Down
4 changes: 4 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ struct Params {
int BIP65Height;
/** Block height at which BIP66 becomes active */
int BIP66Height;
/** Block height at which Bitcoin GPU hard fork becomes active */
int BTGHeight;
/** Premining blocks for Bitcoin GPU hard fork **/
int BTGPremineWindow;
/**
* Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
* (nPowTargetTimespan / nPowTargetSpacing) which is also used for BIP9 deployments.
Expand Down
10 changes: 8 additions & 2 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
assert(pindexLast != nullptr);
unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();

// Only change once per difficulty adjustment interval
if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
int nHeightNext = pindexLast->nHeight + 1;
if (nHeightNext >= params.BTGHeight && nHeightNext < params.BTGHeight + params.BTGPremineWindow)
{
// Lowest difficulty for Bitcoin GPU premining period.
return nProofOfWorkLimit;
}
else if (nHeightNext % params.DifficultyAdjustmentInterval() != 0)
{
// Difficulty adjustment interval is not finished. Keep the last value.
if (params.fPowAllowMinDifficultyBlocks)
{
// Special difficulty rule for testnet:
Expand Down
46 changes: 46 additions & 0 deletions src/test/pow_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,50 @@ BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
}
}

/* Tests the target change at the beginning of the hard fork. */
BOOST_AUTO_TEST_CASE(BitcoinGPUPreminingBegin_test)
{
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
CBlockIndex block1, block2;
CBlockHeader header;
// Check the block before the hard fork has a regular target.
block1.nHeight = 487425;
block1.nTime = 1501593084;
block1.nBits = 402736949;
BOOST_CHECK_EQUAL(GetNextWorkRequired(&block1, &header, chainParams->GetConsensus()), 0x18014735);
// Check the block after the hard fork has the max target.
block2.nHeight = 487426;
block2.nTime = 1501593374;
block2.nBits = 402736949;
BOOST_CHECK_EQUAL(GetNextWorkRequired(&block2, &header, chainParams->GetConsensus()), 0x1d00ffff);
}

/* Tests the block after premining window returns back to regular target. */
BOOST_AUTO_TEST_CASE(BitcoinGPUPreminingEnd_test)
{
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
const auto& params = chainParams->GetConsensus();
int nHeightPreminingEnd = 487427 + params.BTGPremineWindow;
int nHeightLast = nHeightPreminingEnd + params.DifficultyAdjustmentInterval();
int nHeightFirst = nHeightPreminingEnd - params.DifficultyAdjustmentInterval();
// Build a chain of blocks surrounding the first block after premining period.
CBlockHeader header;
std::vector<CBlockIndex> blocks(nHeightLast - nHeightFirst + 1);
for (size_t i = 0; i < blocks.size(); i++) {
blocks[i].pprev = (i == 0) ? nullptr : &blocks[i-1];
blocks[i].nHeight = nHeightFirst + i;
blocks[i].nTime = 1500000000 + i;
// Let the end of the premining period be a boundary:
// - The target before it is set as target limit;
// - The target after it is calculated regularly.
blocks[i].nBits = (blocks[i].nHeight < nHeightPreminingEnd)
? 0x1d00ffff
: GetNextWorkRequired(&blocks[i-1], &header, params);
}
// Since there must be at least one retargeting after the permining period,
// the last block must have different target with target limit (0x1d00ffff).
const auto& blockLast= blocks[blocks.size() - 1];
BOOST_CHECK(GetNextWorkRequired(&blockLast, &header, params) != 0x1d00ffff);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 6b4e56d

Please sign in to comment.