Skip to content

Commit

Permalink
[Consensus] Change difficulty computation for V2 time protocol
Browse files Browse the repository at this point in the history
- 16 seconds granularity
- 64 seconds blocktime
- 16 minutes retargetting timespan
- 2^(256-48)-1 target limit
  • Loading branch information
random-zebra committed Nov 20, 2019
1 parent f892e45 commit 8c3546f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,18 @@ class CMainParams : public CChainParams
vAlertPubKey = ParseHex("0000098d3ba6ba6e7423fa5cbd6a89e0a9a5348f88d332b44a5cb1a8b7ed2c1eaa335fc8dc4f012cb8241cc0bdafd6ca70c5f5448916e4e6f511bcd746ed57dc50");
nDefaultPort = 51472;
bnProofOfWorkLimit = ~uint256(0) >> 20; // PIVX starting difficulty is 1 / 2^12
bnProofOfStakeLimit = ~uint256(0) >> 24;
bnProofOfStakeLimit_V2 = ~uint256(0) >> 48;
nSubsidyHalvingInterval = 210000;
nMaxReorganizationDepth = 100;
nEnforceBlockUpgradeMajority = 8100; // 75%
nRejectBlockOutdatedMajority = 10260; // 95%
nToCheckBlockUpgradeMajority = 10800; // Approximate expected amount of blocks in 7 days (1440*7.5)
nMinerThreads = 0;
nTargetSpacing = 1 * 60; // 1 minute
nTargetSpacing_V2 = 64; // 64 seconds
nTargetTimespan = 40 * 60; // 40 minutes
nTargetTimespan_V2 = 16 * 60; // 16 minutes
nMaturity = 100;
nStakeMinDepth = 600;
nFutureTimeDriftPoW = 7200;
Expand Down Expand Up @@ -329,7 +334,6 @@ class CTestNetParams : public CMainParams
nRejectBlockOutdatedMajority = 5472; // 95%
nToCheckBlockUpgradeMajority = 5760; // 4 days
nMinerThreads = 0;
nTargetSpacing = 1 * 60; // PIVX: 1 minute
nLastPOWBlock = 200;
nPivxBadBlockTime = 1489001494; // Skip nBit validation of Block 259201 per PR #915
nPivxBadBlocknBits = 0x1e0a20bd; // Skip nBit validation of Block 201 per PR #915
Expand Down Expand Up @@ -434,7 +438,6 @@ class CRegTestParams : public CTestNetParams
nRejectBlockOutdatedMajority = 950;
nToCheckBlockUpgradeMajority = 1000;
nMinerThreads = 1;
nTargetSpacing = 1 * 60; // PIVX: 1 minutes
bnProofOfWorkLimit = ~uint256(0) >> 1;
nLastPOWBlock = 250;
nMaturity = 100;
Expand Down
9 changes: 8 additions & 1 deletion src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class CChainParams
const std::vector<unsigned char>& AlertKey() const { return vAlertPubKey; }
int GetDefaultPort() const { return nDefaultPort; }
const uint256& ProofOfWorkLimit() const { return bnProofOfWorkLimit; }
const uint256& ProofOfStakeLimit(const bool fV2) const { return fV2 ? bnProofOfStakeLimit_V2 : bnProofOfStakeLimit; }
int SubsidyHalvingInterval() const { return nSubsidyHalvingInterval; }
/** Used to check majorities for block version upgrade */
int EnforceBlockUpgradeMajority() const { return nEnforceBlockUpgradeMajority; }
Expand All @@ -73,7 +74,8 @@ class CChainParams
bool SkipProofOfWorkCheck() const { return fSkipProofOfWorkCheck; }
/** Make standard checks */
bool RequireStandard() const { return fRequireStandard; }
int64_t TargetSpacing() const { return nTargetSpacing; }
int64_t TargetSpacing(const bool fV2 = true) const { return fV2 ? nTargetSpacing_V2 : nTargetSpacing; }
int64_t TargetTimespan(const bool fV2 = true) const { return fV2 ? nTargetTimespan_V2 : nTargetTimespan; }

/** returns the coinbase maturity **/
int COINBASE_MATURITY() const { return nMaturity; }
Expand Down Expand Up @@ -166,12 +168,17 @@ class CChainParams
std::vector<unsigned char> vAlertPubKey;
int nDefaultPort;
uint256 bnProofOfWorkLimit;
uint256 bnProofOfStakeLimit;
uint256 bnProofOfStakeLimit_V2;
int nMaxReorganizationDepth;
int nSubsidyHalvingInterval;
int nEnforceBlockUpgradeMajority;
int nRejectBlockOutdatedMajority;
int nToCheckBlockUpgradeMajority;
int64_t nTargetSpacing;
int64_t nTargetSpacing_V2;
int64_t nTargetTimespan;
int64_t nTargetTimespan_V2;
int nLastPOWBlock;
int64_t nPivxBadBlockTime;
unsigned int nPivxBadBlocknBits;
Expand Down
2 changes: 1 addition & 1 deletion src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ bool GetKernelStakeModifier(uint256 hashBlockFrom, uint64_t& nStakeModifier, int
return true;
}
const CBlockIndex* pindex = pindexFrom;
CBlockIndex* pindexNext = chainActive[pindex->nHeight + 1];;
CBlockIndex* pindexNext = chainActive[pindex->nHeight + 1];

// loop to find the stake modifier later by a selection interval
do {
Expand Down
14 changes: 10 additions & 4 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,22 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
}

if (pindexLast->nHeight >= Params().LAST_POW_BLOCK()) {
uint256 bnTargetLimit = (~uint256(0) >> 24);
int64_t nTargetSpacing = 60;
int64_t nTargetTimespan = 60 * 40;
const bool fTimeV2 = Params().IsTimeProtocolV2(pindexLast->nHeight+1);
const uint256 bnTargetLimit = Params().ProofOfStakeLimit(fTimeV2);
const int64_t nTargetSpacing = Params().TargetSpacing(fTimeV2);
const int64_t nTargetTimespan = Params().TargetTimespan(fTimeV2);

// on first block with V2 time protocol, return the limit
if (fTimeV2 && !Params().IsTimeProtocolV2(pindexLast->nHeight))
return bnTargetLimit.GetCompact();

int64_t nActualSpacing = 0;
if (pindexLast->nHeight != 0)
nActualSpacing = pindexLast->GetBlockTime() - pindexLast->pprev->GetBlockTime();

if (nActualSpacing < 0)
nActualSpacing = 1;
if (fTimeV2 && nActualSpacing > nTargetSpacing*10)
nActualSpacing = nTargetSpacing*10;

// ppcoin: target change every block
// ppcoin: retarget with exponential moving toward target spacing
Expand Down

0 comments on commit 8c3546f

Please sign in to comment.