shohu / c0ban

c0ban source tree
MIT License
0 stars 0 forks source link

Change Difficultiy #4

Closed shohu closed 6 years ago

shohu commented 6 years ago

If you change difficulty, change nPowTargetSpacing or nPowTargetTimespan.

class CMainParams : public CChainParams {
public:
    CMainParams() {

        consensus.nPowTargetSpacing = 32;
        consensus.nPowTargetTimespan = 24 * 60 * 60; // one day

source

unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
{
    unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();

    // Genesis block
    if (pindexLast == NULL)
        return nProofOfWorkLimit;

    // Only change once per difficulty adjustment interval
    if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
    :
    // Go back by what we want to be 14 days worth of blocks
    int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);
    assert(nHeightFirst >= 0);
    const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);
    assert(pindexFirst);

    return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
}
params.h 
 struct Params {
    int64_t nPowTargetSpacing;
    int64_t nPowTargetTimespan;
    int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }

above result is DifficultyAdjustmentInterval = 2700

By the way. bitcoin 0.15.1 is 2016

consensus.nPowTargetTimespan = 14 24 60 60; // two weeks consensus.nPowTargetSpacing = 10 60;

DifficultyAdjustmentInterval = 2016

shohu commented 6 years ago

If I change difficulty, it's hard fork. So pending change difficulty