Browse Source

Only enable min-difficulty blocks on testnet from a particular height

The min-difficulty change is a bilateral consensus rule change, and so
must be conditionally enabled in order for the earlier section of the
chain to synchronise.

Technically this could be implemented as a network upgrade, but as this will
never be deployed to mainnet, a targeted fork will suffice.
pull/4/head
Jack Grigg 6 years ago
parent
commit
b86dc98047
No known key found for this signature in database GPG Key ID: 1B8D649257DB0829
  1. 6
      src/chainparams.cpp
  2. 4
      src/consensus/params.h
  3. 5
      src/miner.cpp
  4. 3
      src/pow.cpp

6
src/chainparams.cpp

@ -95,7 +95,7 @@ public:
consensus.nPowMaxAdjustDown = 32; // 32% adjustment down
consensus.nPowMaxAdjustUp = 16; // 16% adjustment up
consensus.nPowTargetSpacing = 2.5 * 60;
consensus.fPowAllowMinDifficultyBlocks = false;
consensus.nPowAllowMinDifficultyBlocksFromHeight = boost::none;
consensus.vUpgrades[Consensus::BASE_SPROUT].nProtocolVersion = 170002;
consensus.vUpgrades[Consensus::BASE_SPROUT].nActivationHeight =
Consensus::NetworkUpgrade::ALWAYS_ACTIVE;
@ -272,7 +272,7 @@ public:
consensus.nPowMaxAdjustDown = 32; // 32% adjustment down
consensus.nPowMaxAdjustUp = 16; // 16% adjustment up
consensus.nPowTargetSpacing = 2.5 * 60;
consensus.fPowAllowMinDifficultyBlocks = true;
consensus.nPowAllowMinDifficultyBlocksFromHeight = 299187;
consensus.vUpgrades[Consensus::BASE_SPROUT].nProtocolVersion = 170002;
consensus.vUpgrades[Consensus::BASE_SPROUT].nActivationHeight =
Consensus::NetworkUpgrade::ALWAYS_ACTIVE;
@ -393,7 +393,7 @@ public:
consensus.nPowMaxAdjustDown = 0; // Turn off adjustment down
consensus.nPowMaxAdjustUp = 0; // Turn off adjustment up
consensus.nPowTargetSpacing = 2.5 * 60;
consensus.fPowAllowMinDifficultyBlocks = true;
consensus.nPowAllowMinDifficultyBlocksFromHeight = 0;
consensus.vUpgrades[Consensus::BASE_SPROUT].nProtocolVersion = 170002;
consensus.vUpgrades[Consensus::BASE_SPROUT].nActivationHeight =
Consensus::NetworkUpgrade::ALWAYS_ACTIVE;

4
src/consensus/params.h

@ -8,6 +8,8 @@
#include "uint256.h"
#include <boost/optional.hpp>
namespace Consensus {
/**
@ -91,7 +93,7 @@ struct Params {
NetworkUpgrade vUpgrades[MAX_NETWORK_UPGRADES];
/** Proof of work parameters */
uint256 powLimit;
bool fPowAllowMinDifficultyBlocks;
boost::optional<uint32_t> nPowAllowMinDifficultyBlocksFromHeight;
int64_t nPowAveragingWindow;
int64_t nPowMaxAdjustDown;
int64_t nPowMaxAdjustUp;

5
src/miner.cpp

@ -103,8 +103,9 @@ void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams,
pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
// Updating time can change work required on testnet:
if (consensusParams.fPowAllowMinDifficultyBlocks)
if (consensusParams.nPowAllowMinDifficultyBlocksFromHeight) {
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
}
}
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
@ -726,7 +727,7 @@ void static BitcoinMiner()
// Update nNonce and nTime
pblock->nNonce = ArithToUint256(UintToArith256(pblock->nNonce) + 1);
UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
if (chainparams.GetConsensus().fPowAllowMinDifficultyBlocks)
if (chainparams.GetConsensus().nPowAllowMinDifficultyBlocksFromHeight)
{
// Changing pblock->nTime can change work required on testnet:
hashTarget.SetCompact(pblock->nBits);

3
src/pow.cpp

@ -25,7 +25,8 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
return nProofOfWorkLimit;
{
if (params.fPowAllowMinDifficultyBlocks)
if (params.nPowAllowMinDifficultyBlocksFromHeight &&
pindexLast->nHeight >= params.nPowAllowMinDifficultyBlocksFromHeight.get())
{
// Special difficulty rule for testnet:
// If the new block's timestamp is more than 6 * 2.5 minutes

Loading…
Cancel
Save