diff --git a/src/komodo_bitcoind.cpp b/src/komodo_bitcoind.cpp index 43f06c2200d..5d64604d877 100644 --- a/src/komodo_bitcoind.cpp +++ b/src/komodo_bitcoind.cpp @@ -2247,35 +2247,36 @@ int64_t komodo_coinsupply(int64_t *zfundsp,int64_t *sproutfundsp,int32_t height) return(supply); } -struct komodo_staking *komodo_addutxo(struct komodo_staking *array,int32_t *numkp,int32_t *maxkp,uint32_t txtime,uint64_t nValue,uint256 txid,int32_t vout,char *address,uint8_t *hashbuf,CScript pk) +void komodo_addutxo(std::vector &array,uint32_t txtime,uint64_t nValue,uint256 txid,int32_t vout,char *address,uint8_t *hashbuf,CScript pk) { - uint256 hash; uint32_t segid32; struct komodo_staking *kp; + uint256 hash; uint32_t segid32; komodo_staking kp; segid32 = komodo_stakehash(&hash,address,hashbuf,txid,vout); - if ( *numkp >= *maxkp ) - { - *maxkp += 1000; - array = (struct komodo_staking *)realloc(array,sizeof(*array) * (*maxkp)); - //fprintf(stderr,"realloc max.%d array.%p\n",*maxkp,array); - } - kp = &array[(*numkp)++]; - //fprintf(stderr,"kp.%p num.%d\n",kp,*numkp); - memset(kp,0,sizeof(*kp)); - strcpy(kp->address,address); - kp->txid = txid; - kp->vout = vout; - kp->hashval = UintToArith256(hash); - kp->txtime = txtime; - kp->segid32 = segid32; - kp->nValue = nValue; - kp->scriptPubKey = pk; - return(array); + if ( array.size() >= array.capacity() ) + { + array.reserve(array.capacity() + 1000); + //fprintf(stderr,"%s realloc array.size().%d array.capacity().%d\n", __func__, array.size(), array.capacity()); + } + //memset(&kp,0,sizeof(kp)); + strcpy(kp.address, address); + kp.txid = txid; + kp.vout = vout; + kp.hashval = UintToArith256(hash); + kp.txtime = txtime; + kp.segid32 = segid32; + kp.nValue = nValue; + kp.scriptPubKey = pk; + array.push_back(kp); + //fprintf(stderr,"kp.%p array.size().%d\n",kp,array.size()); } int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig, uint256 merkleroot) { - static struct komodo_staking *array; static int32_t numkp,maxkp; static uint32_t lasttime; + // use thread_local to prevent crash in case of accidental thread overlapping + thread_local std::vector array; + thread_local uint32_t lasttime; + int32_t PoSperc = 0, newStakerActive; - std::set setAddress; struct komodo_staking *kp; int32_t winners,segid,minage,nHeight,counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; std::vector vecOutputs; uint32_t block_from_future_rejecttime,besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 mindiff,ratio,bnTarget,tmpTarget; CBlockIndex *tipindex,*pindex; CTxDestination address; bool fNegative,fOverflow; uint8_t hashbuf[256]; CTransaction tx; uint256 hashBlock; + std::set setAddress; int32_t winners,segid,minage,nHeight,counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; std::vector vecOutputs; uint32_t block_from_future_rejecttime,besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 mindiff,ratio,bnTarget,tmpTarget; CBlockIndex *tipindex,*pindex; CTxDestination address; bool fNegative,fOverflow; uint8_t hashbuf[256]; CTransaction tx; uint256 hashBlock; uint64_t cbPerc = *utxovaluep, tocoinbase = 0; if (!EnsureWalletIsAvailable(0)) return 0; @@ -2297,7 +2298,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt // this was for VerusHash PoS64 //tmpTarget = komodo_PoWtarget(&PoSperc,bnTarget,nHeight,ASSETCHAINS_STAKED); bool resetstaker = false; - if ( array != 0 ) + if ( array.size() != 0 ) { LOCK(cs_main); CBlockIndex* pblockindex = chainActive[tipindex->nHeight]; @@ -2309,15 +2310,13 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt } } - if ( resetstaker || array == 0 || time(NULL) > lasttime+600 ) + if ( resetstaker || array.size() == 0 || time(NULL) > lasttime+600 ) { LOCK2(cs_main, pwalletMain->cs_wallet); pwalletMain->AvailableCoins(vecOutputs, false, NULL, true); - if ( array != 0 ) + if ( array.size() != 0 ) { - free(array); - array = 0; - maxkp = numkp = 0; + array.clear(); lasttime = 0; } BOOST_FOREACH(const COutput& out, vecOutputs) @@ -2343,16 +2342,16 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt continue; if ( myGetTransaction(out.tx->GetHash(),tx,hashBlock) != 0 && (pindex= komodo_getblockindex(hashBlock)) != 0 ) { - array = komodo_addutxo(array,&numkp,&maxkp,(uint32_t)pindex->nTime,(uint64_t)nValue,out.tx->GetHash(),out.i,(char *)CBitcoinAddress(address).ToString().c_str(),hashbuf,(CScript)pk); - //fprintf(stderr,"addutxo numkp.%d vs max.%d\n",numkp,maxkp); + komodo_addutxo(array,(uint32_t)pindex->nTime,(uint64_t)nValue,out.tx->GetHash(),out.i,(char *)CBitcoinAddress(address).ToString().c_str(),hashbuf,(CScript)pk); + //fprintf(stderr,"%s array.size().%d vs array.capacity().%d\n", __func__,array.size(),array.capacity()); } } } lasttime = (uint32_t)time(NULL); - //fprintf(stderr,"finished kp data of utxo for staking %u ht.%d numkp.%d maxkp.%d\n",(uint32_t)time(NULL),nHeight,numkp,maxkp); + //fprintf(stderr,"%s finished kp data of utxo for staking %u ht.%d array.size().%d array.capacity().%d\n", __func__,(uint32_t)time(NULL),nHeight,array.size(),array.capacity()); } block_from_future_rejecttime = (uint32_t)GetTime() + ASSETCHAINS_STAKED_BLOCK_FUTURE_MAX; - for (i=winners=0; itxid,kp->vout,0,(uint32_t)tipindex->nTime+ASSETCHAINS_STAKED_BLOCK_FUTURE_HALF,kp->address,PoSperc); + komodo_staking &kp = array[i]; + eligible = komodo_stake(0,bnTarget,nHeight,kp.txid,kp.vout,0,(uint32_t)tipindex->nTime+ASSETCHAINS_STAKED_BLOCK_FUTURE_HALF,kp.address,PoSperc); if ( eligible > 0 ) { besttime = 0; - if ( eligible == komodo_stake(1,bnTarget,nHeight,kp->txid,kp->vout,eligible,(uint32_t)tipindex->nTime+ASSETCHAINS_STAKED_BLOCK_FUTURE_HALF,kp->address,PoSperc) ) + if ( eligible == komodo_stake(1,bnTarget,nHeight,kp.txid,kp.vout,eligible,(uint32_t)tipindex->nTime+ASSETCHAINS_STAKED_BLOCK_FUTURE_HALF,kp.address,PoSperc) ) { // have elegible utxo to stake with. - if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || kp->nValue < *utxovaluep)) ) + if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || kp.nValue < *utxovaluep)) ) { // is better than the previous best, so use it instead. earliest = eligible; - best_scriptPubKey = kp->scriptPubKey; - *utxovaluep = (uint64_t)kp->nValue; - decode_hex((uint8_t *)utxotxidp,32,(char *)kp->txid.GetHex().c_str()); - *utxovoutp = kp->vout; - *txtimep = kp->txtime; + best_scriptPubKey = kp.scriptPubKey; + *utxovaluep = (uint64_t)kp.nValue; + decode_hex((uint8_t *)utxotxidp,32,(char *)kp.txid.GetHex().c_str()); + *utxovoutp = kp.vout; + *txtimep = kp.txtime; } /*if ( eligible < block_from_future_rejecttime ) { @@ -2388,11 +2387,9 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt } } } - if ( numkp < 500 && array != 0 ) + if ( array.size() < 500 && array.size() != 0 ) { - free(array); - array = 0; - maxkp = numkp = 0; + array.clear(); lasttime = 0; } if ( earliest != 0 ) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 29174c2b92c..f50888c3a62 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -244,6 +244,6 @@ struct komodo_staking CScript scriptPubKey; }; -struct komodo_staking *komodo_addutxo(struct komodo_staking *array,int32_t *numkp,int32_t *maxkp,uint32_t txtime,uint64_t nValue,uint256 txid,int32_t vout,char *address,uint8_t *hashbuf,CScript pk); +void komodo_addutxo(std::vector &array,uint32_t txtime,uint64_t nValue,uint256 txid,int32_t vout,char *address,uint8_t *hashbuf,CScript pk); int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig, uint256 merkleroot); diff --git a/src/miner.cpp b/src/miner.cpp index 8a811b4fa25..15e67d078be 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -166,6 +166,7 @@ int32_t komodo_waituntilelegible(uint32_t blocktime, int32_t stakeHeight, uint32 int64_t adjustedtime = (int64_t)GetTime(); while ( (int64_t)blocktime-ASSETCHAINS_STAKED_BLOCK_FUTURE_MAX > adjustedtime ) { + boost::this_thread::interruption_point(); // allow to interrupt int64_t secToElegible = (int64_t)blocktime-ASSETCHAINS_STAKED_BLOCK_FUTURE_MAX-adjustedtime; if ( delay <= ASSETCHAINS_STAKED_BLOCK_FUTURE_HALF && secToElegible <= ASSETCHAINS_STAKED_BLOCK_FUTURE_HALF ) break; @@ -178,7 +179,8 @@ int32_t komodo_waituntilelegible(uint32_t blocktime, int32_t stakeHeight, uint32 } if( !GetBoolArg("-gen",false) ) return(0); - sleep(1); + //sleep(1); + boost::this_thread::sleep_for(boost::chrono::seconds(1)); // allow to interrupt adjustedtime = (int64_t)GetTime(); } return(1); @@ -268,7 +270,7 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32 { proposedTime = GetTime(); if (proposedTime == nMedianTimePast) - MilliSleep(10); + MilliSleep(10); // allow to interrupt } } pblock->nTime = GetTime(); @@ -1145,7 +1147,7 @@ void waitForPeers(const CChainParams &chainparams) do { if (fvNodesEmpty) { - MilliSleep(1000 + rand() % 4000); + MilliSleep(1000 + rand() % 4000); // allow to interrupt boost::this_thread::interruption_point(); LOCK(cs_vNodes); fvNodesEmpty = vNodes.empty(); @@ -1162,13 +1164,13 @@ void waitForPeers(const CChainParams &chainparams) { if (++loops <= 10) { - MilliSleep(1000); + MilliSleep(1000); // allow to interrupt } else break; } } } while (fvNodesEmpty || IsNotInSync()); - MilliSleep(100 + rand() % 400); + MilliSleep(100 + rand() % 400); // allow to interrupt } } } @@ -1217,7 +1219,8 @@ void static BitcoinMiner() uint8_t *script; uint64_t total; int32_t i,j,gpucount=KOMODO_MAXGPUCOUNT,notaryid = -1; while ( (ASSETCHAIN_INIT == 0 || KOMODO_INITDONE == 0) ) { - sleep(1); + //sleep(1); + boost::this_thread::sleep_for(boost::chrono::seconds(1)); // allow to interrupt if ( komodo_baseid(ASSETCHAINS_SYMBOL) < 0 ) break; } @@ -1264,7 +1267,7 @@ void static BitcoinMiner() } if (!fvNodesEmpty )//&& !IsInitialBlockDownload()) break; - MilliSleep(15000); + MilliSleep(15000); // allow to interrupt //fprintf(stderr,"fvNodesEmpty %d IsInitialBlockDownload(%s) %d\n",(int32_t)fvNodesEmpty,ASSETCHAINS_SYMBOL,(int32_t)IsInitialBlockDownload()); } while (true); @@ -1305,7 +1308,8 @@ void static BitcoinMiner() static uint32_t counter; if ( counter++ < 10 && ASSETCHAINS_STAKED == 0 ) fprintf(stderr,"created illegal blockB, retry\n"); - sleep(1); + //sleep(1); + boost::this_thread::sleep_for(boost::chrono::seconds(1)); // allow to interrupt continue; } //fprintf(stderr,"get template\n"); @@ -1330,7 +1334,8 @@ void static BitcoinMiner() static uint32_t counter; if ( counter++ < 10 ) fprintf(stderr,"skip generating %s on-demand block, no tx avail\n",ASSETCHAINS_SYMBOL); - sleep(10); + //sleep(10); + boost::this_thread::sleep_for(boost::chrono::seconds(10)); // allow to interrupt continue; } else fprintf(stderr,"%s vouts.%d mining.%d vs %d\n",ASSETCHAINS_SYMBOL,(int32_t)pblock->vtx[0].vout.size(),Mining_height,ASSETCHAINS_MINHEIGHT); } @@ -1524,7 +1529,8 @@ void static BitcoinMiner() //fprintf(stderr,"need to wait %d seconds to submit block\n",(int32_t)(B.nTime - GetAdjustedTime())); while ( GetTime() < B.nTime-2 ) { - sleep(1); + //sleep(1); + boost::this_thread::sleep_for(boost::chrono::seconds(1)); // allow to interrupt if ( chainActive.LastTip()->nHeight >= Mining_height ) { fprintf(stderr,"new block arrived\n"); @@ -1538,7 +1544,7 @@ void static BitcoinMiner() { int32_t r; if ( (r= ((Mining_height + NOTARY_PUBKEY33[16]) % 64) / 8) > 0 ) - MilliSleep((rand() % (r * 1000)) + 1000); + MilliSleep((rand() % (r * 1000)) + 1000); // allow to interrupt } } else @@ -1744,6 +1750,8 @@ void static BitcoinMiner() if (minerThreads != NULL) { minerThreads->interrupt_all(); + // std::cout << "Waiting for mining threads to stop..." << std::endl; + minerThreads->join_all(); // prevent thread overlapping delete minerThreads; minerThreads = NULL; }