Skip to content

Commit

Permalink
Monacoin: 3rd hardfork 2/2 (DGWv3)
Browse files Browse the repository at this point in the history
  • Loading branch information
monacoinproject committed Jul 10, 2017
1 parent 45b2532 commit c29f3f8
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class CMainParams : public CChainParams {
// Hardfork params
nSwitchKGWblock = 80000;
nSwitchDIGIblock = 140000;
nSwitchLyra2REv2 = 450000;
nSwitchLyra2REv2_DGW = 450000;

/**
* The message start string is designed to be unlikely to occur in normal data.
Expand Down Expand Up @@ -219,7 +219,7 @@ class CTestNetParams : public CChainParams {
// Hardfork params
nSwitchKGWblock = 80000;
nSwitchDIGIblock = 140000;
nSwitchLyra2REv2 = 450000;
nSwitchLyra2REv2_DGW = 450000;

nDefaultPort = 19401;
nPruneAfterHeight = 1000;
Expand Down Expand Up @@ -306,7 +306,7 @@ class CRegTestParams : public CChainParams {
// Hardfork params
nSwitchKGWblock = 20;
nSwitchDIGIblock = 40;
nSwitchLyra2REv2 = 60;
nSwitchLyra2REv2_DGW = 60;

pchMessageStart[0] = 0xfa;
pchMessageStart[1] = 0xbf;
Expand Down
4 changes: 2 additions & 2 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class CChainParams

int SwitchKGWblock() const { return nSwitchKGWblock; }
int SwitchDIGIblock() const { return nSwitchDIGIblock; }
int SwitchLyra2REv2block() const { return nSwitchLyra2REv2; }
int SwitchLyra2REv2_DGWblock() const { return nSwitchLyra2REv2_DGW; }

protected:
CChainParams() {}
Expand All @@ -106,7 +106,7 @@ class CChainParams

int nSwitchKGWblock;
int nSwitchDIGIblock;
int nSwitchLyra2REv2;
int nSwitchLyra2REv2_DGW;
};

/**
Expand Down
63 changes: 62 additions & 1 deletion src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,62 @@ unsigned int static KimotoGravityWell(const CBlockIndex* pindexLast, const CBloc
}


unsigned int static DarkGravityWave(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) {
/* current difficulty formula, dash - DarkGravity v3, written by Evan Duffield - evan@dashpay.io */
const CBlockIndex *BlockLastSolved = pindexLast;
const CBlockIndex *BlockReading = pindexLast;
int64_t nActualTimespan = 0;
int64_t LastBlockTime = 0;
int64_t PastBlocksMin = 24;
int64_t PastBlocksMax = 24;
int64_t CountBlocks = 0;
CBigNum PastDifficultyAverage;
CBigNum PastDifficultyAveragePrev;

if (BlockLastSolved == NULL || BlockLastSolved->nHeight < Params().SwitchLyra2REv2_DGWblock() + PastBlocksMin) {
return UintToArith256(params.powLimit).GetCompact();
}

for (unsigned int i = 1; BlockReading && BlockReading->nHeight >= Params().SwitchLyra2REv2_DGWblock(); i++) {
if (PastBlocksMax > 0 && i > PastBlocksMax) { break; }
CountBlocks++;

if(CountBlocks <= PastBlocksMin) {
if (CountBlocks == 1) { PastDifficultyAverage.SetCompact(BlockReading->nBits); }
else { PastDifficultyAverage = ((PastDifficultyAveragePrev * CountBlocks)+(CBigNum().SetCompact(BlockReading->nBits))) / (CountBlocks+1); }
PastDifficultyAveragePrev = PastDifficultyAverage;
}

if(LastBlockTime > 0){
int64_t Diff = (LastBlockTime - BlockReading->GetBlockTime());
nActualTimespan += Diff;
}
LastBlockTime = BlockReading->GetBlockTime();

if (BlockReading->pprev == NULL) { assert(BlockReading); break; }
BlockReading = BlockReading->pprev;
}

CBigNum bnNew(PastDifficultyAverage);

int64_t _nTargetTimespan = CountBlocks*params.nPowTargetSpacing;

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

// Retarget
bnNew *= nActualTimespan;
bnNew /= _nTargetTimespan;

if (bnNew > CBigNum(params.powLimit)){
bnNew = CBigNum(params.powLimit);
}

return bnNew.GetCompact();
}

unsigned int static GetNextWorkRequired_V2(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
{
static const int64_t BlocksTargetSpacing = params.nPowTargetSpacing;
Expand All @@ -101,7 +157,12 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
if (pindexLast == NULL)
return nProofOfWorkLimit;

if(pindexLast->nHeight+1 >= Params().SwitchKGWblock() && pindexLast->nHeight+1 < Params().SwitchDIGIblock()){
if(pindexLast->nHeight+1 >= Params().SwitchLyra2REv2_DGWblock())
{
// DGWv3
return DarkGravityWave(pindexLast, pblock, params);
}
else if(pindexLast->nHeight+1 >= Params().SwitchKGWblock() && pindexLast->nHeight+1 < Params().SwitchDIGIblock()){
// KGW
return GetNextWorkRequired_V2(pindexLast, pblock, params);
}
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ UniValue generateBlocks(boost::shared_ptr<CReserveScript> coinbaseScript, int nG
LOCK(cs_main);
IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce);
}
while (nMaxTries > 0 && pblock->nNonce < nInnerLoopCount && !CheckProofOfWork(pblock->GetPoWHash(nHeight+1 >= Params().SwitchLyra2REv2block()), pblock->nBits, Params().GetConsensus())) {
while (nMaxTries > 0 && pblock->nNonce < nInnerLoopCount && !CheckProofOfWork(pblock->GetPoWHash(nHeight+1 >= Params().SwitchLyra2REv2_DGWblock()), pblock->nBits, Params().GetConsensus())) {
++pblock->nNonce;
--nMaxTries;
}
Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, int nHeight, con
}

// Check the header
if (!CheckProofOfWork(block.GetPoWHash(nHeight >= Params().SwitchLyra2REv2block()), block.nBits, consensusParams))
if (!CheckProofOfWork(block.GetPoWHash(nHeight >= Params().SwitchLyra2REv2_DGWblock()), block.nBits, consensusParams))
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());

return true;
Expand Down Expand Up @@ -2839,7 +2839,7 @@ bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const
}

// Check proof of work matches claimed amount
if (fCheckPOW && !CheckProofOfWork(block.GetPoWHash(nHeight >= Params().SwitchLyra2REv2block()), block.nBits, consensusParams))
if (fCheckPOW && !CheckProofOfWork(block.GetPoWHash(nHeight >= Params().SwitchLyra2REv2_DGWblock()), block.nBits, consensusParams))
return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");

return true;
Expand Down

0 comments on commit c29f3f8

Please sign in to comment.