diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index d34b626c6..0533192a8 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1416,6 +1416,29 @@ uint32_t komodo_stakehash(uint256 *hashp,char *address,uint8_t *hashbuf,uint256 return(addrhash.uints[0]); } +arith_uint256 komodo_adaptivepow_target(int32_t height,arith_uint256 bnTarget,uint32_t nTime) +{ + arith_uint256 origtarget,easy; int32_t diff,mult; bool fNegative,fOverflow; CBlockIndex *tipindex; + if ( height > 10 && (tipindex= komodo_chainactive(height - 1)) != 0 ) + { + diff = (nTime - tipindex->GetMedianTimePast()); + if ( diff > 20 * ASSETCHAINS_BLOCKTIME ) + { + mult = diff - 19 * ASSETCHAINS_BLOCKTIME; + mult = (mult / ASSETCHAINS_BLOCKTIME) * ASSETCHAINS_BLOCKTIME + ASSETCHAINS_BLOCKTIME / 3; + origtarget = bnTarget; + bnTarget = bnTarget * arith_uint256(mult * mult); + easy.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); + if ( bnTarget < origtarget || bnTarget > easy ) // deal with overflow + { + bnTarget = easy; + fprintf(stderr,"miner overflowed, set to mindiff\n"); + } else fprintf(stderr,"miner elapsed %d, adjust by factor of %d\n",diff,mult); + } + } + return(bnTarget); +} + arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t height,int32_t goalperc) { int32_t oldflag = 0,dispflag = 0; diff --git a/src/komodo_defs.h b/src/komodo_defs.h index d1ee2fb46..9d891eeda 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -337,6 +337,7 @@ int32_t komodo_priceget(int64_t *buf64,int32_t ind,int32_t height,int32_t numblo uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue,int32_t tipheight); int32_t komodo_currentheight(); int32_t komodo_notarized_bracket(struct notarized_checkpoint *nps[2],int32_t height); +arith_uint256 komodo_adaptivepow_target(int32_t height,arith_uint256 bnTarget,uint32_t nTime); uint256 Parseuint256(const char *hexstr); diff --git a/src/miner.cpp b/src/miner.cpp index 16500b60d..04d93fd9c 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -1448,22 +1448,8 @@ void static BitcoinMiner_noeq() LogPrintf("Block %d : PoS %d%% vs target %d%%\n", Mining_height, percPoS, (int32_t)ASSETCHAINS_STAKED); } else if ( ASSETCHAINS_ADAPTIVEPOW != 0 && ASSETCHAINS_STAKED == 0 ) - { - arith_uint256 origtarget; bool fNegative,fOverflow; - uint32_t elapsed = (pblock->nTime - komodo_heightstamp(Mining_height-1)); - if ( elapsed > 777 ) - { - elapsed -= 777; - origtarget = HASHTarget; - HASHTarget_POW = HASHTarget * arith_uint256(elapsed * elapsed); - if ( HASHTarget_POW < origtarget ) // deal with underflow - { - HASHTarget_POW.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); - fprintf(stderr,"miner underflowed, set to mindiff\n"); - } else fprintf(stderr,"miner elapsed %d, adjust by factor of %d\n",elapsed+777,elapsed*elapsed); - } - } - + HASHTarget_POW = komodo_adaptivepow_target(height,HASHTarget,pblock->nTime); + while (true) { arith_uint256 arNonce = UintToArith256(pblock->nNonce); diff --git a/src/pow.cpp b/src/pow.cpp index 47d538031..bc862b835 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -454,21 +454,7 @@ bool CheckProofOfWork(const CBlockHeader &blkHeader, uint8_t *pubkey33, int32_t bnTarget.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); } else if ( ASSETCHAINS_ADAPTIVEPOW != 0 && ASSETCHAINS_STAKED == 0 ) - { - arith_uint256 origtarget; - uint32_t elapsed = (blkHeader.nTime - komodo_heightstamp(height-1)); - if ( elapsed > 777 ) - { - elapsed -= 777; - origtarget = bnTarget; - bnTarget = bnTarget * arith_uint256(elapsed * elapsed); - if ( bnTarget < origtarget ) // deal with underflow - { - bnTarget.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); - fprintf(stderr,"underflowed, set to mindiff\n"); - } else fprintf(stderr,"elapsed %d, adjust by factor of %d\n",elapsed+777,elapsed*elapsed); - } - } + bnTarget = komodo_adaptivepow_target(height,bnTarget,blkHeader.nTime); // Check proof of work matches claimed amount if ( UintToArith256(hash = blkHeader.GetHash()) > bnTarget && !blkHeader.IsVerusPOSBlock() ) {