26 if (
Params().IsRegTestNet())
27 return pindexLast->
nBits;
44 int64_t nActualTimespan = 0;
45 int64_t LastBlockTime = 0;
46 int64_t PastBlocksMin = 24;
47 int64_t PastBlocksMax = 24;
48 int64_t CountBlocks = 0;
50 uint256 PastDifficultyAveragePrev;
52 if (BlockLastSolved == NULL || BlockLastSolved->
nHeight == 0 || BlockLastSolved->
nHeight < PastBlocksMin) {
58 int64_t nTargetSpacing = 60;
59 int64_t nTargetTimespan = 60 * 40;
64 pLastPoS = pLastPoS->
pprev;
66 int64_t nActualSpacing = 0;
71 if (nActualSpacing < 0)
87 int64_t nInterval = nTargetTimespan / nTargetSpacing;
88 bnNew *= ((nInterval - 1) * nTargetSpacing + nActualSpacing + nActualSpacing);
89 bnNew /= ((nInterval + 1) * nTargetSpacing);
91 if (bnNew <= 0 || bnNew > bnTargetLimit)
92 bnNew = bnTargetLimit;
97 for (
unsigned int i = 1; BlockReading && BlockReading->
nHeight > 0; i++) {
98 if (PastBlocksMax > 0 && i > PastBlocksMax) {
103 if (CountBlocks <= PastBlocksMin) {
104 if (CountBlocks == 1) {
107 PastDifficultyAverage = ((PastDifficultyAveragePrev * CountBlocks) + (
uint256().
SetCompact(BlockReading->
nBits))) / (CountBlocks + 1);
109 PastDifficultyAveragePrev = PastDifficultyAverage;
112 if (LastBlockTime > 0) {
113 int64_t Diff = (LastBlockTime - BlockReading->
GetBlockTime());
114 nActualTimespan += Diff;
118 if (BlockReading->
pprev == NULL) {
119 assert(BlockReading);
122 BlockReading = BlockReading->
pprev;
125 uint256 bnNew(PastDifficultyAverage);
129 if (nActualTimespan < _nTargetTimespan / 3)
130 nActualTimespan = _nTargetTimespan / 3;
131 if (nActualTimespan > _nTargetTimespan * 3)
132 nActualTimespan = _nTargetTimespan * 3;
135 bnNew *= nActualTimespan;
136 bnNew /= _nTargetTimespan;
138 if (bnNew >
Params().ProofOfWorkLimit()) {
151 if (
Params().SkipProofOfWorkCheck())
154 bnTarget.
SetCompact(nBits, &fNegative, &fOverflow);
157 if (fNegative || bnTarget.
IsNull() || fOverflow || bnTarget >
Params().ProofOfWorkLimit())
158 return error(
"CheckProofOfWork(): nBits below minimum work");
161 if (hash > bnTarget) {
162 if (
Params().MineBlocksOnDemand())
165 return error(
"CheckProofOfWork() : hash doesn't match nBits");
177 if (fNegative || fOverflow || bnTarget.
IsNull())
183 return (~bnTarget / (bnTarget + 1)) + 1;
187 if (!p || !p->
pprev)
return NULL;
198 return error(
"CheckPoAContainRecentHash(): Previous block not found");
207 pindex = pindex->
pprev;
217 return error(
"CheckPoAContainRecentHash(): Audited blocks not found");
242 throw std::runtime_error(
"Can't read block from disk");
244 uint256 lastAuditedPoSHash = lastAuditedPoSBlockInfo.
hash;
246 return error(
"CheckPoAContainRecentHash(): Audited blocks not found");
252 return error(
"CheckPoAContainRecentHash(): Being audited blocks not found");
260 return error(
"CheckPoAContainRecentHash(): PoA block is not on the same fork with the previous poa block");
266 pIndexLoop = pIndexLoop->
pprev;
269 return error(
"CheckPoAContainRecentHash(): Some PoS block between %s and %s is not audited\n", lastAuditedPoSHash.
GetHex(), currentFirstPoSAuditedHash.
GetHex());
276 return error(
"CheckPoAContainRecentHash(): PoS block %s not found\n", thisPoSAduditedHash.
GetHex());
280 if (!previousPoSIndex) {
281 return error(
"CheckPoAContainRecentHash(): Failed to find previous PoS block for block %s\n", thisPoSAduditedHash.
GetHex());
285 (uint32_t)previousPoSIndex->
nHeight != previousSummary.
height ||
287 return error(
"CheckPoAContainRecentHash(): PoS block info not matched for %s\n", thisPoSAduditedHash.
GetHex());
291 if (previousSummary.
nTime) {
348 if (minedHash > bnTarget) {
349 LogPrintf(
"Block mined hash not satisfied\n");
350 return error(
"CheckPoABlockMinedHash(): hash doesn't match nBits");
363 return error(
"CheckPrevPoABlockHash(): Previous block not found");
371 pindex = pindex->
pprev;
407 for (uint32_t i = 0; i < numOfPoSBlocks; i++) {
432 uint32_t prevPoATime = pindex->
nTime;
433 if (block.
nTime > prevPoATime && (block.
nTime - pindex->
nTime >= (uint32_t)
Params().POA_BLOCK_TIME())) {
436 LogPrint(
BCLog::POA,
"%s: PoA Block time: %d, Previous: %d, Current: %d, Distance: %d\n", __func__,
458 throw std::runtime_error(
"Can't read block from disk");
461 bool isAlreadyAudited =
false;
464 isAlreadyAudited =
true;
465 LogPrint(
BCLog::POA,
"%s: PoA Block Hash: %s, is already auditted by Block %s\n", __func__,
472 if (isAlreadyAudited) {
488 nReward = 0.25 * COIN;
490 nReward = 0.5 * COIN;
492 ret = block.
vtx.size() == 1;
493 ret = ret && block.
vtx[0].vout.size() == 1;
505 int prevPoAHeight = 0;
506 int lastPoSHeight = 0;
509 if (nHeight >=
Params().HardFork()) {
515 throw std::runtime_error(
"Can't read block from disk");
516 prevPoAHeight = pPrevPoAIndex->
nHeight;
521 padding = (nHeight - lastPoSHeight);
525 LogPrint(
BCLog::POA,
"%s: nHeight: %d, prevPoAHeight: %d, lastPoSHeight: %d, padding: %d\n", __func__, nHeight, prevPoAHeight, lastPoSHeight, padding);
537 return (txid ==
"9965850037f14dcb4abf1168016e9f96f53692322714e7fac92a2b8838544135" || txid ==
"dd3d1dccf8f39a220e3a83cfabaf1b567b6696af877073ec580d09af6198f098" || txid ==
"e8aafd0513a8b2da536d55d9efd788956d03c6a0baa8acc4251f8dc0f3f03e87" || txid ==
"2666169b99521f12b6c69454f66e23af465c63e4a4807a5a8ed45467846ebe93");
543 return (txid ==
"ef99f7882a681a075ebd51fa83be01685257ca66ccb736950fefc037f00e1538" || txid ==
"6514be1fad4d956a059924d5185a6f9db20a62f2f99e3e9b79257d6d3ca36065" || txid ==
"fd5a19a7a7df25774a6a030295f01bae6395be4229ebe2caf4974d536432e0dd");