diff --git a/src/init.cpp b/src/init.cpp index 51d6cb435..903ca8555 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1911,11 +1911,14 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) PruneAndFlush(); } } - if ( GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX) != 0 ) - nLocalServices |= NODE_ADDRINDEX; - if ( GetBoolArg("-spentindex", DEFAULT_SPENTINDEX) != 0 ) - nLocalServices |= NODE_SPENTINDEX; -fprintf(stderr,"nLocalServices %llx %d, %d\n",(long long)nLocalServices,GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX),GetBoolArg("-spentindex", DEFAULT_SPENTINDEX)); + if ( KOMODO_NSPV > 0 ) + { + if ( GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX) != 0 ) + nLocalServices |= NODE_ADDRINDEX; + if ( GetBoolArg("-spentindex", DEFAULT_SPENTINDEX) != 0 ) + nLocalServices |= NODE_SPENTINDEX; + fprintf(stderr,"nLocalServices %llx %d, %d\n",(long long)nLocalServices,GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX),GetBoolArg("-spentindex", DEFAULT_SPENTINDEX)); + } // ********************************************************* Step 10: import blocks if (mapArgs.count("-blocknotify")) 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 14c8e05ac..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() ) { @@ -5299,9 +5307,9 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta } else { - if ( block.GetBlockTime() <= pindexPrev->GetMedianTimePast() + 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME ) + if ( block.GetBlockTime() <= pindexPrev->nTime ) { - fprintf(stderr,"ht.%d too early2 %u vs %u\n",(int32_t)nHeight,(uint32_t)block.GetBlockTime(),(uint32_t)pindexPrev->GetMedianTimePast()); + fprintf(stderr,"ht.%d too early2 %u vs %u\n",(int32_t)nHeight,(uint32_t)block.GetBlockTime(),(uint32_t)pindexPrev->nTime); return state.Invalid(error("%s: block's timestamp is too early2", __func__), REJECT_INVALID, "time-too-old"); } diff --git a/src/miner.cpp b/src/miner.cpp index fe7ca068a..676a41b57 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -123,7 +123,9 @@ public: void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev) { - pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1 + 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME, GetAdjustedTime()); + if ( ASSETCHAINS_ADAPTIVEPOW == 0 ) + pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + else pblock->nTime = std::max((int64_t)(pindexPrev->nTime+1), GetAdjustedTime()); // Updating time can change work required on testnet: if (consensusParams.nPowAllowMinDifficultyBlocksAfterHeight != boost::none) { @@ -567,7 +569,9 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32 nLastBlockTx = nBlockTx; nLastBlockSize = nBlockSize; - blocktime = 1 + std::max(pindexPrev->GetMedianTimePast()+1+ 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME, GetAdjustedTime()); + if ( ASSETCHAINS_ADAPTIVEPOW == 0 ) + blocktime = 1 + std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + else blocktime = 1 + std::max((int64_t)(pindexPrev->nTime+1), GetAdjustedTime()); //pblock->nTime = blocktime + 1; pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus()); @@ -640,7 +644,10 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32 txNew.vout[0].nValue = GetBlockSubsidy(nHeight,consensusParams) + nFees; //fprintf(stderr,"mine ht.%d with %.8f\n",nHeight,(double)txNew.vout[0].nValue/COIN); txNew.nExpiryHeight = 0; - txNew.nLockTime = std::max(pindexPrev->GetMedianTimePast()+1+ 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME, GetAdjustedTime()); + if ( ASSETCHAINS_ADAPTIVEPOW == 0 ) + txNew.nLockTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + else txNew.nLockTime = std::max((int64_t)(pindexPrev->nTime+1), GetAdjustedTime()); + if ( ASSETCHAINS_SYMBOL[0] == 0 && IS_KOMODO_NOTARY != 0 && My_notaryid >= 0 ) txNew.vout[0].nValue += 5000; @@ -1445,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) { @@ -1480,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++) @@ -1818,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) { @@ -1847,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 @@ -2041,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..175137f06 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -73,11 +73,27 @@ 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; int32_t diff,mult = 0; + if ( pindexFirst != 0 && pblock != 0 ) + { + mult = pblock->nTime - pindexFirst->nTime - 7 * ASSETCHAINS_BLOCKTIME; + 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++) + { arith_uint256 bnTmp; bnTmp.SetCompact(pindexFirst->nBits); bnTot += bnTmp; + if ( ASSETCHAINS_ADAPTIVEPOW > 0 && i < 12 && pblock != 0 ) + { + diff = pblock->nTime - pindexFirst->nTime - (8+i)*ASSETCHAINS_BLOCKTIME; + if ( diff > mult ) + { + fprintf(stderr,"i.%d diff.%d (%u - %u - %dx)\n",i,(int32_t)diff,pblock->nTime,pindexFirst->nTime,(8+i)); + mult = diff; + } + } pindexFirst = pindexFirst->pprev; } @@ -85,9 +101,21 @@ 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 ) // jl777: this test of mult > 1 failed when it was int64_t??? + { + 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; + fprintf(stderr,"cmp.%d mult.%d ht.%d -> easy target\n",mult>1,(int32_t)mult,(int32_t)pindexLast->GetHeight()); + } else fprintf(stderr,"cmp.%d mult.%d for ht.%d\n",mult>1,(int32_t)mult,(int32_t)pindexLast->GetHeight()); + nbits = bnTarget.GetCompact(); + } + return(nbits); } unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, @@ -101,11 +129,13 @@ unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, nActualTimespan = params.AveragingWindowTimespan() + (nActualTimespan - params.AveragingWindowTimespan())/4; LogPrint("pow", " nActualTimespan = %d before bounds\n", nActualTimespan); - if (nActualTimespan < params.MinActualTimespan()) - nActualTimespan = params.MinActualTimespan(); - if (nActualTimespan > params.MaxActualTimespan()) - nActualTimespan = params.MaxActualTimespan(); - + if ( ASSETCHAINS_ADAPTIVEPOW <= 0 ) + { + if (nActualTimespan < params.MinActualTimespan()) + nActualTimespan = params.MinActualTimespan(); + if (nActualTimespan > params.MaxActualTimespan()) + nActualTimespan = params.MaxActualTimespan(); + } // Retarget arith_uint256 bnLimit; if (ASSETCHAINS_ALGO == ASSETCHAINS_EQUIHASH) @@ -453,8 +483,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"));