Skip to content

Commit

Permalink
change to dgw algo, update fork height at 3450
Browse files Browse the repository at this point in the history
  • Loading branch information
vnetizen committed Feb 19, 2020
1 parent 5bbc469 commit 0cef3af
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 16 deletions.
26 changes: 16 additions & 10 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class CMainParams : public CChainParams {
consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false;
consensus.nStartMiningTime = 1581519900;
consensus.nDgwPastBlocks = 30;
consensus.nDGWStartHeight = 3450;

// The best chain should have at least this much work.
consensus.nMinimumChainWork = uint256S("0x00"); // 350000
Expand Down Expand Up @@ -106,9 +108,9 @@ class CMainParams : public CChainParams {
fMineBlocksOnDemand = false;

checkpointData = {
{
{0, uint256S("854d7df4269fab0078483472fa0291affc0a767e51194021e04810fa8476be7e")},
}
{
{0, uint256S("854d7df4269fab0078483472fa0291affc0a767e51194021e04810fa8476be7e")},
}
};

chainTxData = ChainTxData{
Expand Down Expand Up @@ -152,6 +154,8 @@ class CTestNetParams : public CChainParams {
consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false;
consensus.nStartMiningTime = 1581440700;
consensus.nDgwPastBlocks = 30;
consensus.nDGWStartHeight = 2000;

// The best chain should have at least this much work.
consensus.nMinimumChainWork = uint256S("0x00");
Expand Down Expand Up @@ -200,7 +204,7 @@ class CTestNetParams : public CChainParams {
checkpointData = {
{
{0, uint256S("fb6945d0c081953e96fb3e173b844e459d950e560840015b7f303b8fdf996332")},
}
}
};

chainTxData = ChainTxData{
Expand Down Expand Up @@ -245,6 +249,8 @@ class CRegTestParams : public CChainParams {
consensus.fPowAllowMinDifficultyBlocks = true;
consensus.fPowNoRetargeting = true;
consensus.nStartMiningTime = 1581514320;
consensus.nDgwPastBlocks = 30;
consensus.nDGWStartHeight = 2000;

// The best chain should have at least this much work.
consensus.nMinimumChainWork = uint256S("0x00");
Expand All @@ -270,15 +276,15 @@ class CRegTestParams : public CChainParams {
vSeeds.clear(); //!< Regtest mode doesn't have any DNS seeds.

checkpointData = {
{
{0, uint256S("123d7466cd1a465d0f18e1c34c967733e4827fd01bbc61144b86cca75613e1bc")},
}
{
{0, uint256S("123d7466cd1a465d0f18e1c34c967733e4827fd01bbc61144b86cca75613e1bc")},
}
};

chainTxData = ChainTxData{
0,
0,
0
0,
0,
0
};

base58Prefixes[PUBKEY_ADDRESS] = {0x80}; // t
Expand Down
2 changes: 2 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ struct Params {
CAmount powBlockRewardAtEpoch[4];
int nCoinbaseMaturity; // Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
int nStartMiningTime;
int nDgwPastBlocks; // number of blocks to average in Dark Gravity Wave
int nDGWStartHeight;
};
} // namespace Consensus

Expand Down
87 changes: 84 additions & 3 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,92 @@ const CBlockIndex *GetLastBlockIndex(const CBlockIndex *pindex, bool fProofOfSta
return pindex;
}

double GetBlockDifficulty(uint32_t nBits) {
int nShift = (nBits >> 24) & 0xff;

double dDiff =
(double) 0x0000ffff / (double) (nBits & 0x00ffffff);

while (nShift < 31) {
dDiff *= 256.0;
nShift++;
}
while (nShift > 31) {
dDiff /= 256.0;
nShift--;
}

return dDiff;
}

unsigned int DarkGravityWave(const CBlockIndex *pindexLast, bool fProofOfStake, const Consensus::Params &params) {
/* current difficulty formula, veil - DarkGravity v3, written by Evan Duffield - evan@dash.org */
const arith_uint256 bnProofOfWorkLimit = UintToArith256(params.powLimit);
unsigned int nProofOfWorkLimit = bnProofOfWorkLimit.GetCompact();

int nCountBlocks = 0;
const CBlockIndex *pindex = pindexLast;
const CBlockIndex *pindexLastMatchingProof = nullptr;
arith_uint256 bnPastTargetAvg = 0;
while (nCountBlocks < params.nDgwPastBlocks) {
// Ran out of blocks, return pow limit
if (!pindex)
return nProofOfWorkLimit;

// Only consider PoW or PoS blocks but not both
if (pindex->IsProofOfStake() != fProofOfStake) {
pindex = pindex->pprev;
continue;
} else if (!pindexLastMatchingProof) {
pindexLastMatchingProof = pindex;
}

arith_uint256 bnTarget = arith_uint256().SetCompact(pindex->nBits);
bnPastTargetAvg = (bnPastTargetAvg * nCountBlocks + bnTarget) / (nCountBlocks + 1);

if (++nCountBlocks != params.nDgwPastBlocks)
pindex = pindex->pprev;
}

LogPrint(BCLog::ALL, "DarkGravityWave fProofOfStake %s pindexDGWHeight=%d, pindexLastMatchingProof=%d, nDgwPastBlocks=%d \n", fProofOfStake, pindex->nHeight, pindexLastMatchingProof->nHeight, params.nDgwPastBlocks);

arith_uint256 bnNew(bnPastTargetAvg);
LogPrint(BCLog::ALL, "-- avg diff %d\n", GetBlockDifficulty(bnNew.GetCompact()));

int64_t nActualTimespan = pindexLast->GetBlockTime() - pindex->GetBlockTime();
int64_t nTargetTimespan = (pindexLast->nHeight - pindex->nHeight) * params.nStakeTargetSpacing;
LogPrint(BCLog::ALL, "-- nActualTimespan=%d nTargetTimespan=%d nDgwPastBlocks=%d\n", nActualTimespan, nTargetTimespan, params.nDgwPastBlocks);

if (nActualTimespan < nTargetTimespan / 3)
nActualTimespan = nTargetTimespan / 3;
if (nActualTimespan > nTargetTimespan * 3)
nActualTimespan = nTargetTimespan * 3;

// Retarget
bnNew *= nActualTimespan;
bnNew /= nTargetTimespan;

if (bnNew > bnProofOfWorkLimit) {
bnNew = bnProofOfWorkLimit;
}
LogPrint(BCLog::ALL, "-- next diff %d\n", GetBlockDifficulty(bnNew.GetCompact()));
return bnNew.GetCompact();
}

unsigned int GetNextTargetRequired(const CBlockIndex *pindexLast, bool fProofOfStake, const Consensus::Params &params) {
const arith_uint256 bnProofOfWorkLimit = UintToArith256(params.powLimit);
unsigned int nProofOfWorkLimit = bnProofOfWorkLimit.GetCompact();

if (params.fPowNoRetargeting) // regtest only
return nProofOfWorkLimit;
if (pindexLast == nullptr)
return nProofOfWorkLimit; // genesis block

if (pindexLast->nHeight >= params.nDGWStartHeight) {
// Retarget every block with DarkGravityWave
return DarkGravityWave(pindexLast, fProofOfStake, params);
}

const CBlockIndex *pindexPrev = GetLastBlockIndex(pindexLast, fProofOfStake);
if (pindexPrev->pprev == nullptr)
return nProofOfWorkLimit; // first block
Expand All @@ -51,22 +130,24 @@ unsigned int GetNextTargetRequired(const CBlockIndex *pindexLast, bool fProofOfS
return bnNew.GetCompact();
}


bool CheckProofOfWork(const CBlockHeader *pblock, const Consensus::Params &params) {
bool fNegative;
bool fOverflow;
arith_uint256 bnTarget;

bnTarget.SetCompact(pblock->nBits, &fNegative, &fOverflow);

// Check range
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit)) {
return false;
}

// Check proof of work matches claimed amount
uint256 PoWHash;
pblock->GetPoWHash(&PoWHash);
if (UintToArith256(PoWHash) > bnTarget)
if (UintToArith256(PoWHash) > bnTarget) {
return false;
}

return true;
}
2 changes: 1 addition & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2953,7 +2953,7 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, bool fProofOfS

const Consensus::Params& consensusParams = params.GetConsensus();
//check block time (start)
if (block.GetBlockTime() < consensusParams.nStartMiningTime) {
if (GetTime() < consensusParams.nStartMiningTime) {
return state.Invalid(false, REJECT_INVALID, "time-too-new", "it's not good time to start mining");
}
// Check proof of work or proof-of-stake
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* network protocol versioning
*/

static const int PROTOCOL_VERSION = 80019;
static const int PROTOCOL_VERSION = 80020;
static const int OLD_VERSION = 80005; // trigger: used to communicate with clients that don't know how to send PoS information in headers

//! initial proto version, to be increased after version/verack negotiation
Expand Down
1 change: 0 additions & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4425,7 +4425,6 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, CMu
LogPrint(BCLog::ALERT, "-- CreateCoinStake: Have no coin to stake\n");
return false;
}
int count = 0;
for (const auto &output : setCoins) {
sortedCoins.push_back(output);
}
Expand Down

0 comments on commit 0cef3af

Please sign in to comment.