Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow minimum-difficulty blocks on testnet #3559

Merged
merged 7 commits into from
Oct 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class CMainParams : public CChainParams {
consensus.nPowMaxAdjustDown = 32; // 32% adjustment down
consensus.nPowMaxAdjustUp = 16; // 16% adjustment up
consensus.nPowTargetSpacing = 2.5 * 60;
consensus.nPowAllowMinDifficultyBlocksAfterHeight = boost::none;
consensus.vUpgrades[Consensus::BASE_SPROUT].nProtocolVersion = 170002;
consensus.vUpgrades[Consensus::BASE_SPROUT].nActivationHeight =
Consensus::NetworkUpgrade::ALWAYS_ACTIVE;
Expand Down Expand Up @@ -271,6 +272,7 @@ class CTestNetParams : public CChainParams {
consensus.nPowMaxAdjustDown = 32; // 32% adjustment down
consensus.nPowMaxAdjustUp = 16; // 16% adjustment up
consensus.nPowTargetSpacing = 2.5 * 60;
consensus.nPowAllowMinDifficultyBlocksAfterHeight = 299187;
consensus.vUpgrades[Consensus::BASE_SPROUT].nProtocolVersion = 170002;
consensus.vUpgrades[Consensus::BASE_SPROUT].nActivationHeight =
Consensus::NetworkUpgrade::ALWAYS_ACTIVE;
Expand Down Expand Up @@ -391,6 +393,7 @@ class CRegTestParams : public CChainParams {
consensus.nPowMaxAdjustDown = 0; // Turn off adjustment down
consensus.nPowMaxAdjustUp = 0; // Turn off adjustment up
consensus.nPowTargetSpacing = 2.5 * 60;
consensus.nPowAllowMinDifficultyBlocksAfterHeight = 0;
daira marked this conversation as resolved.
Show resolved Hide resolved
consensus.vUpgrades[Consensus::BASE_SPROUT].nProtocolVersion = 170002;
consensus.vUpgrades[Consensus::BASE_SPROUT].nActivationHeight =
Consensus::NetworkUpgrade::ALWAYS_ACTIVE;
Expand Down
3 changes: 3 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "uint256.h"

#include <boost/optional.hpp>

namespace Consensus {

/**
Expand Down Expand Up @@ -91,6 +93,7 @@ struct Params {
NetworkUpgrade vUpgrades[MAX_NETWORK_UPGRADES];
/** Proof of work parameters */
uint256 powLimit;
boost::optional<uint32_t> nPowAllowMinDifficultyBlocksAfterHeight;
int64_t nPowAveragingWindow;
int64_t nPowMaxAdjustDown;
int64_t nPowMaxAdjustUp;
Expand Down
41 changes: 41 additions & 0 deletions src/gtest/test_pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,44 @@ TEST(PoW, DifficultyAveraging) {
params),
GetNextWorkRequired(&blocks[lastBlk], nullptr, params));
}

TEST(PoW, MinDifficultyRules) {
SelectParams(CBaseChainParams::TESTNET);
daira marked this conversation as resolved.
Show resolved Hide resolved
const Consensus::Params& params = Params().GetConsensus();
size_t lastBlk = 2*params.nPowAveragingWindow;
size_t firstBlk = lastBlk - params.nPowAveragingWindow;

// Start with blocks evenly-spaced and equal difficulty
std::vector<CBlockIndex> blocks(lastBlk+1);
for (int i = 0; i <= lastBlk; i++) {
blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
blocks[i].nHeight = params.nPowAllowMinDifficultyBlocksAfterHeight.get() + i;
blocks[i].nTime = 1269211443 + i * params.nPowTargetSpacing;
blocks[i].nBits = 0x1e7fffff; /* target 0x007fffff000... */
blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
}

// Create a new block at the target spacing
CBlockHeader next;
next.nTime = blocks[lastBlk].nTime + params.nPowTargetSpacing;

// Result should be unchanged, modulo integer division precision loss
arith_uint256 bnRes;
bnRes.SetCompact(0x1e7fffff);
bnRes /= params.AveragingWindowTimespan();
bnRes *= params.AveragingWindowTimespan();
EXPECT_EQ(GetNextWorkRequired(&blocks[lastBlk], &next, params), bnRes.GetCompact());

// Delay last block up to the edge of the min-difficulty limit
next.nTime += params.nPowTargetSpacing * 5;

// Result should be unchanged, modulo integer division precision loss
EXPECT_EQ(GetNextWorkRequired(&blocks[lastBlk], &next, params), bnRes.GetCompact());

// Delay last block over the min-difficulty limit
next.nTime += 1;

// Result should be the minimum difficulty
EXPECT_EQ(GetNextWorkRequired(&blocks[lastBlk], &next, params),
UintToArith256(params.powLimit).GetCompact());
}
10 changes: 10 additions & 0 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ class TxPriorityCompare
void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
{
pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());

// Updating time can change work required on testnet:
if (consensusParams.nPowAllowMinDifficultyBlocksAfterHeight != boost::none) {
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
daira marked this conversation as resolved.
Show resolved Hide resolved
}
}

CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
Expand Down Expand Up @@ -722,6 +727,11 @@ void static BitcoinMiner()
// Update nNonce and nTime
pblock->nNonce = ArithToUint256(UintToArith256(pblock->nNonce) + 1);
UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
if (chainparams.GetConsensus().nPowAllowMinDifficultyBlocksAfterHeight != boost::none)
{
// Changing pblock->nTime can change work required on testnet:
hashTarget.SetCompact(pblock->nBits);
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
if (pindexLast == NULL)
return nProofOfWorkLimit;

{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't necessary for this to be a block.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, also braces might help at line 25 and line 34-35 of this method.

// Comparing to pindexLast->nHeight with >= because this function
// returns the work required for the block after pindexLast.
if (params.nPowAllowMinDifficultyBlocksAfterHeight != boost::none &&
pindexLast->nHeight >= params.nPowAllowMinDifficultyBlocksAfterHeight.get())
{
// Special difficulty rule for testnet:
// If the new block's timestamp is more than 6 * 2.5 minutes
// then allow mining of a min-difficulty block.
if (pblock && pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing * 6)
return nProofOfWorkLimit;
}
}

// Find the first block in the averaging interval
const CBlockIndex* pindexFirst = pindexLast;
arith_uint256 bnTot {0};
Expand Down