From 23ca86df3a7031e0cfee52b364aa137b8219ba3f Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Aug 2019 09:14:07 -1100 Subject: [PATCH] Zawys new algo 1) FTL 4 seconds 2) MTP from 11 -> 1 3) must calc MAX(diff[I]) for past 11 blocks 4) bnTarget *= mult*mult 5) bnTarget.nBits -> onchain --- src/komodo_bitcoind.h | 8 ++++---- src/main.cpp | 12 ++++++++++-- src/miner.cpp | 19 ++++++++++--------- src/pow.cpp | 31 ++++++++++++++++++++++++------- src/rpc/mining.cpp | 5 +++-- 5 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index e2bc2b7f2..c0850006b 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1419,13 +1419,13 @@ uint32_t komodo_stakehash(uint256 *hashp,char *address,uint8_t *hashbuf,uint256 arith_uint256 komodo_adaptivepow_target(int32_t height,arith_uint256 bnTarget,uint32_t nTime) { arith_uint256 origtarget,easy; int32_t diff,tipdiff; int64_t mult; bool fNegative,fOverflow; CBlockIndex *tipindex; - if ( height > 10 && (tipindex= komodo_chainactive(height - 1)) != 0 ) + if ( height > 10 && (tipindex= komodo_chainactive(height - 1)) != 0 ) // disable offchain diffchange { diff = (nTime - tipindex->GetMedianTimePast()); tipdiff = (nTime - tipindex->nTime); if ( tipdiff > 13*ASSETCHAINS_BLOCKTIME ) diff = tipdiff; - if ( diff >= 13 * ASSETCHAINS_BLOCKTIME && (height < 3000 || tipdiff > 2*ASSETCHAINS_BLOCKTIME) ) + if ( diff >= 13 * ASSETCHAINS_BLOCKTIME && (height < 30 || tipdiff > 2*ASSETCHAINS_BLOCKTIME) ) { mult = diff - 12 * ASSETCHAINS_BLOCKTIME; mult = (mult / ASSETCHAINS_BLOCKTIME) * ASSETCHAINS_BLOCKTIME + ASSETCHAINS_BLOCKTIME / 2; @@ -2286,8 +2286,8 @@ int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) if ( height == 0 ) return(0); } - if ( ASSETCHAINS_ADAPTIVEPOW > 0 ) - bnTarget = komodo_adaptivepow_target(height,bnTarget,pblock->nTime); + //if ( ASSETCHAINS_ADAPTIVEPOW > 0 ) + // bnTarget = komodo_adaptivepow_target(height,bnTarget,pblock->nTime); if ( ASSETCHAINS_LWMAPOS != 0 && bhash > bnTarget ) { // if proof of stake is active, check if this is a valid PoS block before we fail diff --git a/src/main.cpp b/src/main.cpp index aeab7cadc..9cffe8a76 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5043,7 +5043,15 @@ bool CheckBlockHeader(int32_t *futureblockp,int32_t height,CBlockIndex *pindex, } } *futureblockp = 0; - if (blockhdr.GetBlockTime() > GetAdjustedTime() + 60) + if ( ASSETCHAINS_ADAPTIVEPOW > 0 ) + { + if (blockhdr.GetBlockTime() > GetAdjustedTime() + 4) + { + //LogPrintf("CheckBlockHeader block from future %d error",blockhdr.GetBlockTime() - GetAdjustedTime()); + return false; + } + } + else if (blockhdr.GetBlockTime() > GetAdjustedTime() + 60) { /*CBlockIndex *tipindex; //fprintf(stderr,"ht.%d future block %u vs time.%u + 60\n",height,(uint32_t)blockhdr.GetBlockTime(),(uint32_t)GetAdjustedTime()); @@ -5288,7 +5296,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta } // Check timestamp against prev - if ( ASSETCHAINS_ADAPTIVEPOW == 0 || nHeight < 3000 ) + if ( ASSETCHAINS_ADAPTIVEPOW == 0 || nHeight < 30 ) { if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast() ) { diff --git a/src/miner.cpp b/src/miner.cpp index d19faee7d..676a41b57 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -1452,8 +1452,8 @@ void static BitcoinMiner_noeq() HASHTarget.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); LogPrintf("Block %d : PoS %d%% vs target %d%%\n", Mining_height, percPoS, (int32_t)ASSETCHAINS_STAKED); } - else if ( ASSETCHAINS_ADAPTIVEPOW > 0 && ASSETCHAINS_STAKED == 0 ) - HASHTarget_POW = komodo_adaptivepow_target(Mining_height,HASHTarget,pblock->nTime); + //else if ( ASSETCHAINS_ADAPTIVEPOW > 0 && ASSETCHAINS_STAKED == 0 ) + // HASHTarget_POW = komodo_adaptivepow_target(Mining_height,HASHTarget,pblock->nTime); while (true) { @@ -1487,8 +1487,8 @@ void static BitcoinMiner_noeq() } else if ( ASSETCHAINS_STAKED == 100 && Mining_height > 100 ) hashTarget = HASHTarget; - else if ( ASSETCHAINS_ADAPTIVEPOW > 0 && ASSETCHAINS_STAKED == 0 ) - hashTarget = HASHTarget_POW; + //else if ( ASSETCHAINS_ADAPTIVEPOW > 0 && ASSETCHAINS_STAKED == 0 ) + // hashTarget = HASHTarget_POW; // for speed check NONCEMASK at a time for (i = 0; i < count; i++) @@ -1825,8 +1825,8 @@ void static BitcoinMiner() if ( ASSETCHAINS_STAKED < 100 ) LogPrintf("Block %d : PoS %d%% vs target %d%% \n",Mining_height,percPoS,(int32_t)ASSETCHAINS_STAKED); } - else if ( ASSETCHAINS_ADAPTIVEPOW > 0 ) - HASHTarget_POW = komodo_adaptivepow_target(Mining_height,HASHTarget,pblock->nTime); + //else if ( ASSETCHAINS_ADAPTIVEPOW > 0 ) + // HASHTarget_POW = komodo_adaptivepow_target(Mining_height,HASHTarget,pblock->nTime); gotinvalid = 0; while (true) { @@ -1854,8 +1854,8 @@ void static BitcoinMiner() arith_uint256 hashTarget; if ( KOMODO_MININGTHREADS > 0 && ASSETCHAINS_STAKED > 0 && ASSETCHAINS_STAKED < 100 && Mining_height > 10 ) hashTarget = HASHTarget_POW; - else if ( ASSETCHAINS_ADAPTIVEPOW > 0 ) - hashTarget = HASHTarget_POW; + //else if ( ASSETCHAINS_ADAPTIVEPOW > 0 ) + // hashTarget = HASHTarget_POW; else hashTarget = HASHTarget; std::function)> validBlock = #ifdef ENABLE_WALLET @@ -2048,7 +2048,8 @@ void static BitcoinMiner() if ( ASSETCHAINS_ADAPTIVEPOW > 0 ) { UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev); - hashTarget = HASHTarget_POW = komodo_adaptivepow_target(Mining_height,HASHTarget,pblock->nTime); + HASHTarget.SetCompact(pblock->nBits); + //hashTarget = HASHTarget_POW = komodo_adaptivepow_target(Mining_height,HASHTarget,pblock->nTime); } /*if ( NOTARY_PUBKEY33[0] == 0 ) { diff --git a/src/pow.cpp b/src/pow.cpp index 18fda549f..19eb28629 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -73,11 +73,19 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead // Find the first block in the averaging interval const CBlockIndex* pindexFirst = pindexLast; - arith_uint256 bnTot {0}; - for (int i = 0; pindexFirst && i < params.nPowAveragingWindow; i++) { + arith_uint256 bnTarget,bnTot {0}; + uint32_t nbits; int64_t diff,mult = pblock->nTime - pindexFirst->nTime - 7 * ASSETCHAINS_BLOCKTIME; + for (int i = 0; pindexFirst && i < params.nPowAveragingWindow; i++) + { arith_uint256 bnTmp; bnTmp.SetCompact(pindexFirst->nBits); bnTot += bnTmp; + if ( ASSETCHAINS_ADAPTIVEPOW > 0 && i < 12 ) + { + diff = pblock->nTime - pindexFirst->nTime - (8+i)*ASSETCHAINS_BLOCKTIME; + if ( diff > mult ) + mult = diff; + } pindexFirst = pindexFirst->pprev; } @@ -85,9 +93,18 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead if (pindexFirst == NULL) return nProofOfWorkLimit; - arith_uint256 bnAvg {bnTot / params.nPowAveragingWindow}; - - return CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params); + bool fNegative,fOverflow; arith_uint256 easy,origtarget,bnAvg {bnTot / params.nPowAveragingWindow}; + nbits = CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params); + if ( ASSETCHAINS_ADAPTIVEPOW > 0 && mult > 1 ) + { + origtarget = bnTarget = arith_uint256().SetCompact(nbits); + bnTarget = bnTarget * arith_uint256(mult * mult); + easy.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); + if ( bnTarget < origtarget || bnTarget > easy ) + bnTarget = easy; + nbits = bnTarget.GetCompact(); + } + return(nbits); } unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, @@ -453,8 +470,8 @@ bool CheckProofOfWork(const CBlockHeader &blkHeader, uint8_t *pubkey33, int32_t arith_uint256 bnMaxPoSdiff; bnTarget.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); } - else if ( ASSETCHAINS_ADAPTIVEPOW > 0 && ASSETCHAINS_STAKED == 0 ) - bnTarget = komodo_adaptivepow_target(height,bnTarget,blkHeader.nTime); + //else if ( ASSETCHAINS_ADAPTIVEPOW > 0 && ASSETCHAINS_STAKED == 0 ) + // bnTarget = komodo_adaptivepow_target(height,bnTarget,blkHeader.nTime); // Check proof of work matches claimed amount if ( UintToArith256(hash = blkHeader.GetHash()) > bnTarget && !blkHeader.IsVerusPOSBlock() ) { diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index e31dbd396..d70f198f1 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -838,9 +838,10 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp) result.push_back(Pair("ac_staked", (int64_t)ASSETCHAINS_STAKED)); result.push_back(Pair("origtarget", hashTarget.GetHex())); } - else if ( ASSETCHAINS_ADAPTIVEPOW != 0 ) + /*else if ( ASSETCHAINS_ADAPTIVEPOW > 0 ) result.push_back(Pair("target",komodo_adaptivepow_target((int32_t)(pindexPrev->GetHeight()+1),hashTarget,pblock->nTime).GetHex())); - else result.push_back(Pair("target", hashTarget.GetHex())); + else*/ + result.push_back(Pair("target", hashTarget.GetHex())); result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1)); result.push_back(Pair("mutable", aMutable)); result.push_back(Pair("noncerange", "00000000ffffffff"));