From acd2b3b879d34da61f234ae67e0bc4d0d83f6ea0 Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 23 Sep 2021 13:29:52 +0500 Subject: [PATCH 1/9] wait for mining threads to stop --- src/miner.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/miner.cpp b/src/miner.cpp index e9e12019643..11c3d2b2369 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -2188,6 +2188,8 @@ void static BitcoinMiner() if (minerThreads != NULL) { minerThreads->interrupt_all(); + std::cout << __func__ << "Waiting for mining threads to stop..." << std::endl; + minerThreads->join_all(); // prevent thread overlapping delete minerThreads; minerThreads = NULL; } From 24f7d50a8ad35d8983eb9597985b5a32e69588cf Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 23 Sep 2021 13:32:28 +0500 Subject: [PATCH 2/9] convert staking array to vector (to correctly destroy members) and make it thread_local --- src/komodo_bitcoind.cpp | 88 +++++++++++++++++++++-------------------- src/komodo_bitcoind.h | 2 +- 2 files changed, 46 insertions(+), 44 deletions(-) diff --git a/src/komodo_bitcoind.cpp b/src/komodo_bitcoind.cpp index d74a5064b21..90d9349ae60 100644 --- a/src/komodo_bitcoind.cpp +++ b/src/komodo_bitcoind.cpp @@ -2450,35 +2450,39 @@ 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,int32_t *maxkp,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; struct komodo_staking kp; segid32 = komodo_stakehash(&hash,address,hashbuf,txid,vout); - if ( *numkp >= *maxkp ) + if ( array.size() >= *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); + //array = (struct komodo_staking *)realloc(array,sizeof(*array) * (*maxkp)); + array.reserve(*maxkp); + //fprintf(stderr,"realloc max.%d array.size().%d\n",*maxkp,array.size()); + } + 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 int32_t maxkp; + 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; @@ -2500,7 +2504,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->GetHeight()]; @@ -2512,15 +2516,14 @@ 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(); + maxkp = 0; lasttime = 0; } BOOST_FOREACH(const COutput& out, vecOutputs) @@ -2546,16 +2549,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,&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 array.size().%d vs max.%d\n",array.size(),maxkp); } } } 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,"finished kp data of utxo for staking %u ht.%d array.size().%d maxkp.%d\n",(uint32_t)time(NULL),nHeight,array.size(),maxkp); } 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); + struct 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 ) { @@ -2591,11 +2594,10 @@ 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(); + maxkp = 0; lasttime = 0; } if ( earliest != 0 ) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index adf28340a02..79d86edf650 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -239,6 +239,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,int32_t *maxkp,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); From 87ac53dc69031409b360ff11f92ba159d78662bd Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 23 Sep 2021 23:11:07 +0500 Subject: [PATCH 3/9] staking logging improved --- src/komodo_bitcoind.cpp | 2 +- src/miner.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_bitcoind.cpp b/src/komodo_bitcoind.cpp index 90d9349ae60..9fbc3a1f4f3 100644 --- a/src/komodo_bitcoind.cpp +++ b/src/komodo_bitcoind.cpp @@ -2459,7 +2459,7 @@ void komodo_addutxo(std::vector &array,int32_t *maxkp,uint32_t t *maxkp += 1000; //array = (struct komodo_staking *)realloc(array,sizeof(*array) * (*maxkp)); array.reserve(*maxkp); - //fprintf(stderr,"realloc max.%d array.size().%d\n",*maxkp,array.size()); + //fprintf(stderr,"realloc max.%d array.size().%d array.capacity().%d\n", *maxkp,array.size(), array.capacity()); } memset(&kp,0,sizeof(kp)); strcpy(kp.address,address); diff --git a/src/miner.cpp b/src/miner.cpp index 11c3d2b2369..0d49f1c77d2 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -2188,7 +2188,7 @@ void static BitcoinMiner() if (minerThreads != NULL) { minerThreads->interrupt_all(); - std::cout << __func__ << "Waiting for mining threads to stop..." << std::endl; + std::cout << "Waiting for mining threads to stop..." << std::endl; minerThreads->join_all(); // prevent thread overlapping delete minerThreads; minerThreads = NULL; From f582ad95ac5edc77483286790b6f499114eb377e Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 25 Sep 2021 13:06:13 +0500 Subject: [PATCH 4/9] maxkp replaced with array.capacity --- src/komodo_bitcoind.cpp | 19 +++++++------------ src/komodo_bitcoind.h | 2 +- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/komodo_bitcoind.cpp b/src/komodo_bitcoind.cpp index 9fbc3a1f4f3..e9c5dd22093 100644 --- a/src/komodo_bitcoind.cpp +++ b/src/komodo_bitcoind.cpp @@ -2450,16 +2450,14 @@ int64_t komodo_coinsupply(int64_t *zfundsp,int64_t *sproutfundsp,int32_t height) return(supply); } -void komodo_addutxo(std::vector &array,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; segid32 = komodo_stakehash(&hash,address,hashbuf,txid,vout); - if ( array.size() >= *maxkp ) + if ( array.size() >= array.capacity() ) { - *maxkp += 1000; - //array = (struct komodo_staking *)realloc(array,sizeof(*array) * (*maxkp)); - array.reserve(*maxkp); - //fprintf(stderr,"realloc max.%d array.size().%d array.capacity().%d\n", *maxkp,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); @@ -2478,7 +2476,6 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt { // use thread_local to prevent crash in case of accidental thread overlapping thread_local std::vector array; - thread_local int32_t maxkp; thread_local uint32_t lasttime; int32_t PoSperc = 0, newStakerActive; @@ -2523,7 +2520,6 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt if ( array.size() != 0 ) { array.clear(); - maxkp = 0; lasttime = 0; } BOOST_FOREACH(const COutput& out, vecOutputs) @@ -2549,13 +2545,13 @@ 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 ) { - komodo_addutxo(array,&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 array.size().%d vs max.%d\n",array.size(),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 array.size().%d maxkp.%d\n",(uint32_t)time(NULL),nHeight,array.size(),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; i &array,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); From ba1b22a300144c97285906f2abc1486cc17530d9 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 25 Sep 2021 13:11:54 +0500 Subject: [PATCH 5/9] 'struct' eliminated --- src/komodo_bitcoind.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_bitcoind.cpp b/src/komodo_bitcoind.cpp index e9c5dd22093..d2d0689fc6b 100644 --- a/src/komodo_bitcoind.cpp +++ b/src/komodo_bitcoind.cpp @@ -2452,7 +2452,7 @@ int64_t komodo_coinsupply(int64_t *zfundsp,int64_t *sproutfundsp,int32_t height) 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 ( array.size() >= array.capacity() ) { @@ -2563,7 +2563,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt fprintf(stderr,"[%s:%d] chain tip changed during staking loop t.%u counter.%d\n",ASSETCHAINS_SYMBOL,nHeight,(uint32_t)time(NULL),i); return(0); } - struct komodo_staking &kp = array[i]; + 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 ) { From 5098d67c75207480fb3f7af4bcf6dc6659f2efc9 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 25 Sep 2021 15:17:34 +0500 Subject: [PATCH 6/9] unneeded memset removed --- src/komodo_bitcoind.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_bitcoind.cpp b/src/komodo_bitcoind.cpp index d2d0689fc6b..87512474170 100644 --- a/src/komodo_bitcoind.cpp +++ b/src/komodo_bitcoind.cpp @@ -2459,8 +2459,8 @@ void komodo_addutxo(std::vector &array,uint32_t txtime,uint64_t 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); + //memset(&kp,0,sizeof(kp)); + strcpy(kp.address, address); kp.txid = txid; kp.vout = vout; kp.hashval = UintToArith256(hash); From dcbf657cabac274ad54f3528396ad9524636cefb Mon Sep 17 00:00:00 2001 From: dimxy Date: Tue, 5 Jul 2022 21:25:09 +0500 Subject: [PATCH 7/9] Update src/miner.cpp Co-authored-by: DeckerSU --- src/miner.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/miner.cpp b/src/miner.cpp index 0d49f1c77d2..01d6a250d9e 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -2188,7 +2188,7 @@ void static BitcoinMiner() if (minerThreads != NULL) { minerThreads->interrupt_all(); - std::cout << "Waiting for mining threads to stop..." << std::endl; + // std::cout << "Waiting for mining threads to stop..." << std::endl; minerThreads->join_all(); // prevent thread overlapping delete minerThreads; minerThreads = NULL; From f8c17ce4d30511fc58a3b8519fe8c0b2a2e1fba5 Mon Sep 17 00:00:00 2001 From: dimxy Date: Wed, 13 Jul 2022 01:28:26 +0500 Subject: [PATCH 8/9] add interruption_point in komodo_waituntilelegible --- src/miner.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/miner.cpp b/src/miner.cpp index 01d6a250d9e..18328e1616f 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -168,6 +168,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; From 12261b951ef040a725a6a8c2b69f0916f4470928 Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 14 Jul 2022 14:32:49 +0500 Subject: [PATCH 9/9] change sleeps to interruptible in miner --- src/miner.cpp | 54 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index 18328e1616f..46775a754e5 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -181,7 +181,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); @@ -277,7 +278,8 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32 { proposedTime = GetTime(); if (proposedTime == nMedianTimePast) - MilliSleep(10); + //MilliSleep(10); + boost::this_thread::sleep_for(boost::chrono::milliseconds(10)); // allow to interrupt } } pblock->nTime = GetTime(); @@ -1163,7 +1165,8 @@ void waitForPeers(const CChainParams &chainparams) do { if (fvNodesEmpty) { - MilliSleep(1000 + rand() % 4000); + //MilliSleep(1000 + rand() % 4000); + boost::this_thread::sleep_for(boost::chrono::milliseconds(1000 + rand() % 4000)); // allow to interrupt boost::this_thread::interruption_point(); LOCK(cs_vNodes); fvNodesEmpty = vNodes.empty(); @@ -1180,13 +1183,15 @@ void waitForPeers(const CChainParams &chainparams) { if (++loops <= 10) { - MilliSleep(1000); + //MilliSleep(1000); + boost::this_thread::sleep_for(boost::chrono::milliseconds(1000)); // allow to interrupt } else break; } } } while (fvNodesEmpty || IsNotInSync()); - MilliSleep(100 + rand() % 400); + //MilliSleep(100 + rand() % 400); + boost::this_thread::sleep_for(boost::chrono::milliseconds(100 + rand() % 400)); // allow to interrupt } } } @@ -1394,7 +1399,8 @@ void static BitcoinMiner_noeq() while ( (ASSETCHAIN_INIT == 0 || KOMODO_INITDONE == 0) ) //chainActive.Tip()->GetHeight() != 235300 && { - sleep(1); + //sleep(1); + boost::this_thread::sleep_for(boost::chrono::seconds(1)); // allow to interrupt if ( komodo_baseid(ASSETCHAINS_SYMBOL) < 0 ) break; } @@ -1405,7 +1411,8 @@ void static BitcoinMiner_noeq() CBlockIndex *pindexPrev, *pindexCur; do { pindexPrev = chainActive.LastTip(); - MilliSleep(5000 + rand() % 5000); + //MilliSleep(5000 + rand() % 5000); + boost::this_thread::sleep_for(boost::chrono::milliseconds(5000 + rand() % 5000)); // allow to interrupt waitForPeers(chainparams); pindexCur = chainActive.LastTip(); } while (pindexPrev != pindexCur); @@ -1424,14 +1431,16 @@ void static BitcoinMiner_noeq() waitForPeers(chainparams); pindexPrev = chainActive.LastTip(); - sleep(1); + //sleep(1); + boost::this_thread::sleep_for(boost::chrono::seconds(1)); // allow to interrupt // prevent forking on startup before the diff algorithm kicks in if (pindexPrev->GetHeight() < 50 || pindexPrev != chainActive.LastTip()) { do { pindexPrev = chainActive.LastTip(); - MilliSleep(5000 + rand() % 5000); + //MilliSleep(5000 + rand() % 5000); + boost::this_thread::sleep_for(boost::chrono::milliseconds(5000 + rand() % 5000)); // allow to interrupt } while (pindexPrev != chainActive.LastTip()); } @@ -1486,7 +1495,8 @@ void static BitcoinMiner_noeq() 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); } @@ -1514,7 +1524,8 @@ void static BitcoinMiner_noeq() lastChainTipPrinted = chainActive.LastTip(); printf("Block %d added to chain\n", lastChainTipPrinted->GetHeight()); } - MilliSleep(250); + //MilliSleep(250); + boost::this_thread::sleep_for(boost::chrono::milliseconds(250)); // allow to interrupt continue; } @@ -1577,7 +1588,8 @@ void static BitcoinMiner_noeq() if (pblock->nSolution.size() != 1344) { LogPrintf("ERROR: Block solution is not 1344 bytes as it should be"); - sleep(5); + //sleep(5); + boost::this_thread::sleep_for(boost::chrono::seconds(5)); // allow to interrupt break; } @@ -1706,7 +1718,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; } @@ -1753,7 +1766,8 @@ void static BitcoinMiner() } if (!fvNodesEmpty )//&& !IsInitialBlockDownload()) break; - MilliSleep(15000); + //MilliSleep(15000); + boost::this_thread::sleep_for(boost::chrono::milliseconds(15000)); // allow to interrupt //fprintf(stderr,"fvNodesEmpty %d IsInitialBlockDownload(%s) %d\n",(int32_t)fvNodesEmpty,ASSETCHAINS_SYMBOL,(int32_t)IsInitialBlockDownload()); } while (true); @@ -1794,7 +1808,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"); @@ -1819,7 +1834,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); } @@ -1969,7 +1985,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()->GetHeight() >= Mining_height ) { fprintf(stderr,"new block arrived\n"); @@ -1983,7 +2000,8 @@ 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); + boost::this_thread::sleep_for(boost::chrono::milliseconds((rand() % (r * 1000)) + 1000)); // allow to interrupt } } else