diff --git a/src/pow.cpp b/src/pow.cpp index 406fca63c..a84db0015 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -42,13 +42,13 @@ uint32_t komodo_chainactive_timestamp(); unsigned int lwmaGetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params); unsigned int lwmaCalculateNextWorkRequired(const CBlockIndex* pindexLast, const Consensus::Params& params); -arith_uint256 zawy_targetMA(arith_uint256 easy,arith_uint256 bnTarget,int32_t divisor) +arith_uint256 zawy_targetMA(arith_uint256 easy,arith_uint256 bnSum,int32_t num,int32_t numerator,int32_t divisor) { - bnTarget /= arith_uint256(divisor); - bnTarget *= arith_uint256(ASSETCHAINS_BLOCKTIME); - if ( bnTarget > easy ) - bnTarget = easy; - return(bnTarget); + bnSum /= arith_uint256(ASSETCHAINS_BLOCKTIME * num * num * divisor); + bnSum *= arith_uint256(numerator); + if ( bnSum > easy ) + bnSum = easy; + return(bnSum); } arith_uint256 zawy_exponential(arith_uint256 bnTarget,int32_t mult) @@ -100,11 +100,12 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead // Find the first block in the averaging interval const CBlockIndex* pindexFirst = pindexLast; - arith_uint256 bnTmp,bnTarget,bnSum4 {0},bnSum7 {0},bnSum12 {0},bnTot {0}; + arith_uint256 bnTmp,bnTarget,bnPrev {0},bnSum4 {0},bnSum7 {0},bnSum12 {0},bnTot {0}; uint32_t nbits,blocktime,block4diff=0,block7diff=0,block12diff=0; int32_t diff,mult = 0; if ( ASSETCHAINS_ADAPTIVEPOW > 0 && pindexFirst != 0 && pblock != 0 ) { mult = pblock->nTime - pindexFirst->nTime - 7 * ASSETCHAINS_BLOCKTIME; + bnPrev.SetCompact(pindexFirst->nBits); //fprintf(stderr,"ht.%d mult.%d = (%u - %u - 7x)\n",pindexLast->GetHeight(),(int32_t)mult,pblock->nTime, pindexFirst->nTime); } for (int i = 0; pindexFirst && i < params.nPowAveragingWindow; i++) @@ -148,15 +149,30 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead if (pindexFirst == NULL) return nProofOfWorkLimit; - bool fNegative,fOverflow; arith_uint256 easy,origtarget,bnAvg {bnTot / params.nPowAveragingWindow}; + bool fNegative,fOverflow; int32_t flag = 0; arith_uint256 easy,origtarget,bnAvg {bnTot / params.nPowAveragingWindow}; nbits = CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params); - if ( ASSETCHAINS_ADAPTIVEPOW > 0 ) + if ( ASSETCHAINS_ADAPTIVEPOW > 0 && block12diff != 0 && block7diff != 0 && block4diff != 0 ) { origtarget = bnTarget = arith_uint256().SetCompact(nbits); easy.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); - if ( mult > 1 ) // chain is stuck case, jl777: test of mult > 1 failed when it was int64_t??? + bnSum4 = zawy_targetMA(easy,bnSum4,4,block4diff * 5,1); + bnSum7 = zawy_targetMA(easy,bnSum7,7,block7diff * 3,1); + bnSum12 = zawy_targetMA(easy,bnSum12,12,block12diff * 2,1); + if ( bnSum4 < bnSum7 ) + bnTmp = bnSum4; + else bnTmp = bnSum7; + if ( bnSum12 < bnTmp ) + bnTmp = bnSum12; + if ( bnTmp < bnTarget ) { - bnTarget = zawy_exponential(bnTarget,mult); //replaces: bnTarget * arith_uint256(mult * mult); + fprintf(stderr,"ht.%d block12diff %d vs %d, make harder\n",(int32_t)pindexLast->GetHeight()+1,block12diff,ASSETCHAINS_BLOCKTIME*11); + bnTarget = (bnTmp + bnPrev) / arith_uint256(2); + flag = 1; + } + else if ( flag == 0 && mult > 1 ) // e^mult case, jl777: test of mult > 1 failed when it was int64_t??? + { + flag = 1; + bnTarget = zawy_exponential(bnTarget,mult); if ( bnTarget < origtarget || bnTarget > easy ) { bnTarget = easy; @@ -164,45 +180,23 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead return(KOMODO_MINDIFF_NBITS); } else fprintf(stderr,"cmp.%d mult.%d for ht.%d\n",mult>1,(int32_t)mult,(int32_t)pindexLast->GetHeight()); } - else if ( block12diff != 0 && block7diff != 0 && block4diff != 0 ) + if ( flag == 0 ) { - bnSum4 = zawy_targetMA(easy,bnSum4,block4diff * 5); - bnSum7 = zawy_targetMA(easy,bnSum7,block7diff * 3); - bnSum12 = zawy_targetMA(easy,bnSum12,block12diff * 2); - if ( block12diff < ASSETCHAINS_BLOCKTIME*11 ) + bnSum4 = zawy_targetMA(easy,bnSum4,4,block4diff * 3,10); + bnSum7 = zawy_targetMA(easy,bnSum7,7,block7diff * 5,10); + bnSum12 = zawy_targetMA(easy,bnSum12,12,block12diff * 6,10); + if ( bnSum4 > bnSum7 ) + bnTmp = bnSum4; + else bnTmp = bnSum7; + if ( bnSum12 > bnTmp ) + bnTmp = bnSum12; + if ( bnTmp > bnTarget ) { - if ( bnSum4 < bnSum7 ) - bnTmp = bnSum4; - else bnTmp = bnSum7; - if ( bnSum12 < bnTmp ) - bnTmp = bnSum12; - if ( bnTmp < bnTarget ) - { - fprintf(stderr,"ht.%d block12diff %d < %d, make harder\n",(int32_t)pindexLast->GetHeight()+1,block12diff,ASSETCHAINS_BLOCKTIME*11); - bnTarget = bnTmp; - } //else fprintf(stderr,"nothing smaller\n"); + fprintf(stderr,"ht.%d block12diff %d > %d, make easier\n",(int32_t)pindexLast->GetHeight()+1,block12diff,ASSETCHAINS_BLOCKTIME*13); + bnTarget = (bnTmp + bnPrev) / arith_uint256(2); + flag = 1; } - else if ( block12diff > ASSETCHAINS_BLOCKTIME*13 ) - { - if ( bnSum4 > bnSum7 ) - bnTmp = bnSum4; - else bnTmp = bnSum7; - if ( bnSum12 > bnTmp ) - bnTmp = bnSum12; - if ( bnTmp > bnTarget ) - { - fprintf(stderr,"ht.%d block12diff %d > %d, make easier\n",(int32_t)pindexLast->GetHeight()+1,block12diff,ASSETCHAINS_BLOCKTIME*13); - bnTarget = bnTmp; - } //else fprintf(stderr,"nothing bigger\n"); - } - /*if ( block4diff > 4 && block4diff < ASSETCHAINS_BLOCKTIME ) // for 10x and higher hashrate increases - { - block4diff += (2 * ASSETCHAINS_BLOCKTIME) / 3; - bnTarget = bnTarget * arith_uint256(block4diff) / arith_uint256(ASSETCHAINS_BLOCKTIME * 2); - fprintf(stderr,"ht.%d 4 blocks happened in %d adjust by %.4f\n",(int32_t)pindexLast->GetHeight(),block4diff-((2 * ASSETCHAINS_BLOCKTIME) / 3),(double)block4diff/(ASSETCHAINS_BLOCKTIME*2)); - } - */ - } // else fprintf(stderr,"null diff %d %d %d\n",block4diff,block7diff,block12diff); + } nbits = bnTarget.GetCompact(); } return(nbits);