Implement new difficulty algorithm (#931)
The algorithm is based on DigiShield v3/v4.
This commit is contained in:
@@ -39,7 +39,9 @@ public:
|
|||||||
// TODO generate harder genesis block
|
// TODO generate harder genesis block
|
||||||
//consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
//consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
||||||
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
||||||
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
|
consensus.nPowAveragingWindow = 17;
|
||||||
|
consensus.nPowMaxAdjustDown = 16; // 16% adjustment down
|
||||||
|
consensus.nPowMaxAdjustUp = 8; // 8% adjustment up
|
||||||
consensus.nPowTargetSpacing = 2.5 * 60;
|
consensus.nPowTargetSpacing = 2.5 * 60;
|
||||||
consensus.fPowAllowMinDifficultyBlocks = false;
|
consensus.fPowAllowMinDifficultyBlocks = false;
|
||||||
/**
|
/**
|
||||||
@@ -216,6 +218,8 @@ public:
|
|||||||
consensus.nMajorityRejectBlockOutdated = 950;
|
consensus.nMajorityRejectBlockOutdated = 950;
|
||||||
consensus.nMajorityWindow = 1000;
|
consensus.nMajorityWindow = 1000;
|
||||||
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
||||||
|
consensus.nPowMaxAdjustDown = 0; // Turn off adjustment down
|
||||||
|
consensus.nPowMaxAdjustUp = 0; // Turn off adjustment up
|
||||||
pchMessageStart[0] = 0xaa;
|
pchMessageStart[0] = 0xaa;
|
||||||
pchMessageStart[1] = 0xe8;
|
pchMessageStart[1] = 0xe8;
|
||||||
pchMessageStart[2] = 0x3f;
|
pchMessageStart[2] = 0x3f;
|
||||||
|
|||||||
@@ -36,9 +36,13 @@ struct Params {
|
|||||||
/** Proof of work parameters */
|
/** Proof of work parameters */
|
||||||
uint256 powLimit;
|
uint256 powLimit;
|
||||||
bool fPowAllowMinDifficultyBlocks;
|
bool fPowAllowMinDifficultyBlocks;
|
||||||
|
int64_t nPowAveragingWindow;
|
||||||
|
int64_t nPowMaxAdjustDown;
|
||||||
|
int64_t nPowMaxAdjustUp;
|
||||||
int64_t nPowTargetSpacing;
|
int64_t nPowTargetSpacing;
|
||||||
int64_t nPowTargetTimespan;
|
int64_t AveragingWindowTimespan() const { return nPowAveragingWindow * nPowTargetSpacing; }
|
||||||
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
|
int64_t MinActualTimespan() const { return (AveragingWindowTimespan() * (100 - nPowMaxAdjustUp )) / 100; }
|
||||||
|
int64_t MaxActualTimespan() const { return (AveragingWindowTimespan() * (100 + nPowMaxAdjustDown)) / 100; }
|
||||||
};
|
};
|
||||||
} // namespace Consensus
|
} // namespace Consensus
|
||||||
|
|
||||||
|
|||||||
53
src/pow.cpp
53
src/pow.cpp
@@ -24,8 +24,6 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
|
|||||||
if (pindexLast == NULL)
|
if (pindexLast == NULL)
|
||||||
return nProofOfWorkLimit;
|
return nProofOfWorkLimit;
|
||||||
|
|
||||||
// Only change once per difficulty adjustment interval
|
|
||||||
if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
|
|
||||||
{
|
{
|
||||||
if (params.fPowAllowMinDifficultyBlocks)
|
if (params.fPowAllowMinDifficultyBlocks)
|
||||||
{
|
{
|
||||||
@@ -34,36 +32,35 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
|
|||||||
// then allow mining of a min-difficulty block.
|
// then allow mining of a min-difficulty block.
|
||||||
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
|
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
|
||||||
return nProofOfWorkLimit;
|
return nProofOfWorkLimit;
|
||||||
else
|
|
||||||
{
|
|
||||||
// Return the last non-special-min-difficulty-rules-block
|
|
||||||
const CBlockIndex* pindex = pindexLast;
|
|
||||||
while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)
|
|
||||||
pindex = pindex->pprev;
|
|
||||||
return pindex->nBits;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return pindexLast->nBits;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Go back by what we want to be 14 days worth of blocks
|
// Find the first block in the averaging interval
|
||||||
int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);
|
const CBlockIndex* pindexFirst = pindexLast;
|
||||||
assert(nHeightFirst >= 0);
|
for (int i = 0; pindexFirst && i < params.nPowAveragingWindow; i++) {
|
||||||
const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);
|
pindexFirst = pindexFirst->pprev;
|
||||||
assert(pindexFirst);
|
}
|
||||||
|
|
||||||
return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
|
// Check we have enough blocks
|
||||||
|
if (pindexFirst == NULL)
|
||||||
|
return nProofOfWorkLimit;
|
||||||
|
|
||||||
|
return CalculateNextWorkRequired(pindexLast, pindexFirst->GetMedianTimePast(), params);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
|
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
|
||||||
{
|
{
|
||||||
// Limit adjustment step
|
// Limit adjustment step
|
||||||
int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
|
// Use medians to prevent time-warp attacks
|
||||||
LogPrintf(" nActualTimespan = %d before bounds\n", nActualTimespan);
|
int64_t nActualTimespan = pindexLast->GetMedianTimePast() - nFirstBlockTime;
|
||||||
if (nActualTimespan < params.nPowTargetTimespan/4)
|
LogPrint("pow", " nActualTimespan = %d before dampening\n", nActualTimespan);
|
||||||
nActualTimespan = params.nPowTargetTimespan/4;
|
nActualTimespan = params.AveragingWindowTimespan() + (nActualTimespan - params.AveragingWindowTimespan())/4;
|
||||||
if (nActualTimespan > params.nPowTargetTimespan*4)
|
LogPrint("pow", " nActualTimespan = %d before bounds\n", nActualTimespan);
|
||||||
nActualTimespan = params.nPowTargetTimespan*4;
|
|
||||||
|
if (nActualTimespan < params.MinActualTimespan())
|
||||||
|
nActualTimespan = params.MinActualTimespan();
|
||||||
|
if (nActualTimespan > params.MaxActualTimespan())
|
||||||
|
nActualTimespan = params.MaxActualTimespan();
|
||||||
|
|
||||||
// Retarget
|
// Retarget
|
||||||
const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
|
const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
|
||||||
@@ -71,17 +68,17 @@ unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nF
|
|||||||
arith_uint256 bnOld;
|
arith_uint256 bnOld;
|
||||||
bnNew.SetCompact(pindexLast->nBits);
|
bnNew.SetCompact(pindexLast->nBits);
|
||||||
bnOld = bnNew;
|
bnOld = bnNew;
|
||||||
bnNew /= params.nPowTargetTimespan;
|
bnNew /= params.AveragingWindowTimespan();
|
||||||
bnNew *= nActualTimespan;
|
bnNew *= nActualTimespan;
|
||||||
|
|
||||||
if (bnNew > bnPowLimit)
|
if (bnNew > bnPowLimit)
|
||||||
bnNew = bnPowLimit;
|
bnNew = bnPowLimit;
|
||||||
|
|
||||||
/// debug print
|
/// debug print
|
||||||
LogPrintf("GetNextWorkRequired RETARGET\n");
|
LogPrint("pow", "GetNextWorkRequired RETARGET\n");
|
||||||
LogPrintf("params.nPowTargetTimespan = %d nActualTimespan = %d\n", params.nPowTargetTimespan, nActualTimespan);
|
LogPrint("pow", "params.AveragingWindowTimespan() = %d nActualTimespan = %d\n", params.AveragingWindowTimespan(), nActualTimespan);
|
||||||
LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString());
|
LogPrint("pow", "Before: %08x %s\n", pindexLast->nBits, bnOld.ToString());
|
||||||
LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString());
|
LogPrint("pow", "After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString());
|
||||||
|
|
||||||
return bnNew.GetCompact();
|
return bnNew.GetCompact();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ using namespace std;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return average network hashes per second based on the last 'lookup' blocks,
|
* Return average network hashes per second based on the last 'lookup' blocks,
|
||||||
* or from the last difficulty change if 'lookup' is nonpositive.
|
* or over the difficulty averaging window if 'lookup' is nonpositive.
|
||||||
* If 'height' is nonnegative, compute the estimate at the time when a given block was found.
|
* If 'height' is nonnegative, compute the estimate at the time when a given block was found.
|
||||||
*/
|
*/
|
||||||
Value GetNetworkHashPS(int lookup, int height) {
|
Value GetNetworkHashPS(int lookup, int height) {
|
||||||
@@ -45,9 +45,13 @@ Value GetNetworkHashPS(int lookup, int height) {
|
|||||||
if (pb == NULL || !pb->nHeight)
|
if (pb == NULL || !pb->nHeight)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
// If lookup is -1, then use blocks since last difficulty change.
|
// If lookup is -1, then use difficulty averaging window.
|
||||||
if (lookup <= 0)
|
if (lookup <= 0)
|
||||||
lookup = pb->nHeight % Params().GetConsensus().DifficultyAdjustmentInterval() + 1;
|
lookup = pb->nHeight - Params().GetConsensus().nPowAveragingWindow;
|
||||||
|
|
||||||
|
// If lookup is still negative, then use blocks since genesis.
|
||||||
|
if (lookup <= 0)
|
||||||
|
lookup = pb->nHeight;
|
||||||
|
|
||||||
// If lookup is larger than chain, then set it to chain length.
|
// If lookup is larger than chain, then set it to chain length.
|
||||||
if (lookup > pb->nHeight)
|
if (lookup > pb->nHeight)
|
||||||
@@ -79,10 +83,10 @@ Value getnetworkhashps(const Array& params, bool fHelp)
|
|||||||
throw runtime_error(
|
throw runtime_error(
|
||||||
"getnetworkhashps ( blocks height )\n"
|
"getnetworkhashps ( blocks height )\n"
|
||||||
"\nReturns the estimated network hashes per second based on the last n blocks.\n"
|
"\nReturns the estimated network hashes per second based on the last n blocks.\n"
|
||||||
"Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
|
"Pass in [blocks] to override # of blocks, -1 specifies over difficulty averaging window.\n"
|
||||||
"Pass in [height] to estimate the network speed at the time when a certain block was found.\n"
|
"Pass in [height] to estimate the network speed at the time when a certain block was found.\n"
|
||||||
"\nArguments:\n"
|
"\nArguments:\n"
|
||||||
"1. blocks (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change.\n"
|
"1. blocks (numeric, optional, default=120) The number of blocks, or -1 for blocks over difficulty averaging window.\n"
|
||||||
"2. height (numeric, optional, default=-1) To estimate at the time of the given height.\n"
|
"2. height (numeric, optional, default=-1) To estimate at the time of the given height.\n"
|
||||||
"\nResult:\n"
|
"\nResult:\n"
|
||||||
"x (numeric) Hashes per second estimated\n"
|
"x (numeric) Hashes per second estimated\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user