3 static const int64_t nTargetTimespan = 86184;
4 static const int64_t nTargetSpacing = 205;
5 static const int64_t nInterval = nTargetTimespan / nTargetSpacing;
7 static const int nDifficultySwitchHeight = 15420;
8 static const int nDifficultySwitchHeight2 = 77777;
11 static const int nDifficultyProtocol3 = 87777;
16 int64_t nSubsidy = 5 * COIN;
18 if (nHeight < 42000) {
19 nSubsidy = 4.2 * COIN;
20 }
else if (nHeight < 77777) {
22 }
else if (nHeight == 77778) {
25 nSubsidy >>= (nHeight / 306600);
27 return nSubsidy + nFees;
42 if (
TestNet() && nTime > nTargetSpacing*2)
47 while (nTime > 0 && bnResult < bnLimit)
50 bnResult = (bnResult * 99) / 70;
52 nTime -= nTargetTimespan*4;
54 if (bnResult > bnLimit)
63 unsigned int static KimotoGravityWell(
const CBlockIndex* pindexLast,
65 uint64_t TargetBlocksSpacingSeconds,
66 uint64_t PastBlocksMin,
67 uint64_t PastBlocksMax) {
73 uint64_t PastBlocksMass = 0;
74 int64_t PastRateActualSeconds = 0;
75 int64_t PastRateTargetSeconds = 0;
76 double PastRateAdjustmentRatio = double(1);
78 CBigNum PastDifficultyAveragePrev;
79 double EventHorizonDeviation;
80 double EventHorizonDeviationFast;
81 double EventHorizonDeviationSlow;
84 if (BlockLastSolved == NULL || BlockLastSolved->
nHeight == 0 || (uint64_t)BlockLastSolved->
nHeight < PastBlocksMin) {
return bnProofOfWorkLimit.
GetCompact(); }
86 for (
unsigned int i = 1; BlockReading && BlockReading->
nHeight > 0; i++) {
87 if (PastBlocksMax > 0 && i > PastBlocksMax) {
break; }
91 else { PastDifficultyAverage = ((
CBigNum().
SetCompact(BlockReading->
nBits) - PastDifficultyAveragePrev) / i) + PastDifficultyAveragePrev; }
92 PastDifficultyAveragePrev = PastDifficultyAverage;
95 PastRateTargetSeconds = TargetBlocksSpacingSeconds * PastBlocksMass;
96 PastRateAdjustmentRatio = double(1);
97 if (PastRateActualSeconds < 0) { PastRateActualSeconds = 0; }
98 if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
99 PastRateAdjustmentRatio = double(PastRateTargetSeconds) / double(PastRateActualSeconds);
101 EventHorizonDeviation = 1 + (0.7084 * pow((
double(PastBlocksMass)/
double(144)), -1.228));
102 EventHorizonDeviationFast = EventHorizonDeviation;
103 EventHorizonDeviationSlow = 1 / EventHorizonDeviation;
105 if (PastBlocksMass >= PastBlocksMin) {
106 if ((PastRateAdjustmentRatio <= EventHorizonDeviationSlow) || (PastRateAdjustmentRatio >= EventHorizonDeviationFast)) { assert(BlockReading);
break; }
108 if (BlockReading->
pprev == NULL) { assert(BlockReading);
break; }
109 BlockReading = BlockReading->
pprev;
112 CBigNum bnNew(PastDifficultyAverage);
113 if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
114 bnNew *= PastRateActualSeconds;
115 bnNew /= PastRateTargetSeconds;
117 if (bnNew > bnProofOfWorkLimit) { bnNew = bnProofOfWorkLimit; }
131 static const int64_t BlocksTargetSpacing = 3 * 60;
132 unsigned int TimeDaySeconds = 60 * 60 * 24;
133 int64_t PastSecondsMin = TimeDaySeconds * 0.25;
134 int64_t PastSecondsMax = TimeDaySeconds * 7;
135 uint64_t PastBlocksMin = PastSecondsMin / BlocksTargetSpacing;
136 uint64_t PastBlocksMax = PastSecondsMax / BlocksTargetSpacing;
138 return KimotoGravityWell(pindexLast, pblock, BlocksTargetSpacing, PastBlocksMin, PastBlocksMax);
146 unsigned int nProofOfWorkLimit = bnProofOfWorkLimit.
GetCompact();
149 if (pindexLast == NULL)
150 return nProofOfWorkLimit;
154 static const int newTargetTimespan = 2050;
155 int nHeight = pindexLast->
nHeight + 1;
156 bool fNewDifficultyProtocol = (nHeight >= nDifficultySwitchHeight ||
TestNet());
157 bool fNewDifficultyProtocol2 = (nHeight >= nDifficultySwitchHeight2 ||
TestNet());
158 if (fNewDifficultyProtocol2) {
160 fNewDifficultyProtocol =
false;
163 int64_t nTargetTimespanCurrent = fNewDifficultyProtocol ? (nTargetTimespan*4) : nTargetTimespan;
164 int64_t nInterval = nTargetTimespanCurrent / nTargetSpacing;
166 if (fNewDifficultyProtocol2) nTargetTimespanCurrent = newTargetTimespan;
167 if (
TestNet() && nHeight < (newTargetTimespan/205)+1)
return pindexLast->
nBits;
170 if ((nHeight % nInterval != 0 && !fNewDifficultyProtocol2) &&
171 (nHeight != nDifficultySwitchHeight) && !
TestNet())
173 return pindexLast->
nBits;
178 int blockstogoback = nInterval-1;
179 if ((pindexLast->
nHeight+1) != nInterval)
180 blockstogoback = nInterval;
184 blockstogoback = fNewDifficultyProtocol2 ? (newTargetTimespan/205) : blockstogoback;
185 for (
int i = 0; pindexFirst && i < blockstogoback; i++)
186 pindexFirst = pindexFirst->
pprev;
191 int64_t nActualTimespanMax = fNewDifficultyProtocol ? (nTargetTimespanCurrent*4) : ((nTargetTimespanCurrent*99)/70);
192 int64_t nActualTimespanMin = fNewDifficultyProtocol ? (nTargetTimespanCurrent/4) : ((nTargetTimespanCurrent*70)/99);
193 LogPrintf(
" nActualTimespan = %lld before bounds\n", nActualTimespan);
194 if (pindexLast->
nHeight+1 >= nDifficultySwitchHeight2) {
195 if (nActualTimespan < nActualTimespanMin)
196 nActualTimespan = nActualTimespanMin;
197 if (nActualTimespan > nActualTimespanMax)
198 nActualTimespan = nActualTimespanMax;
199 }
else if (pindexLast->
nHeight+1 > nDifficultySwitchHeight) {
200 if (nActualTimespan < nActualTimespanMin/4)
201 nActualTimespan = nActualTimespanMin/4;
202 if (nActualTimespan > nActualTimespanMax)
203 nActualTimespan = nActualTimespanMax;
205 if (nActualTimespan < nActualTimespanMin)
206 nActualTimespan = nActualTimespanMin;
207 if (nActualTimespan > nActualTimespanMax)
208 nActualTimespan = nActualTimespanMax;
214 bnNew *= nActualTimespan;
215 if (fNewDifficultyProtocol2) {
216 bnNew /= nTargetTimespanCurrent;
218 bnNew /= nTargetTimespan;
221 if (bnNew > bnProofOfWorkLimit) bnNew = bnProofOfWorkLimit;
224 LogPrintf(
"GetNextWorkRequired RETARGET\n");
225 LogPrintf(
"nTargetTimespan = %lld nActualTimespan = %lld\n", nTargetTimespan, nActualTimespan);
236 return NeoGetNextWorkRequired(pindexLast, pblock);
238 return OldGetNextWorkRequired(pindexLast, pblock);
251 return error(
"CheckProofOfWork() : nBits below minimum work");
255 return error(
"CheckProofOfWork() : hash doesn't match nBits");
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock)
uint256 getuint256() const
bool CheckProofOfWork(uint256 hash, unsigned int nBits)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime)
Calculate the minimum amount of work a received block needs, without knowing its direct parent...
CBigNum & SetCompact(unsigned int nCompact)
int64_t GetBlockValue(int nHeight, int64_t nFees)
C++ wrapper for BIGNUM (OpenSSL bignum)
The block chain is a tree shaped structure starting with the genesis block at the root...
const CChainParams & Params()
Return the currently selected parameters.
std::string ToString() const
unsigned int GetCompact() const
int64_t GetBlockTime() const
const CBigNum & ProofOfWorkLimit(MinedWithAlgo mwa=SCRYPT_ANC) const