From ac32c470c72e4488b9691b430df554205eb0fe6c Mon Sep 17 00:00:00 2001 From: svost Date: Sun, 9 Jan 2022 15:16:49 +0300 Subject: [PATCH] Cleanup miner.cpp --- src/miner.cpp | 78 +++++++++++++++++++++++++++++--------------------------- 1 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index f9ec6eb..37afda3 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -9,7 +9,6 @@ #include "kernel.h" #include "kernel_worker.h" -using namespace std; ////////////////////////////////////////////////////////////////////////////// // @@ -61,7 +60,7 @@ class COrphan { public: CTransaction* ptx; - set setDependsOn; + std::set setDependsOn; double dPriority; double dFeePerKb; @@ -78,7 +77,7 @@ uint64_t nLastBlockSize = 0; uint32_t nLastCoinStakeSearchInterval = 0; // We want to sort transactions by priority and fee, so: -typedef boost::tuple TxPriority; +typedef std::tuple TxPriority; class TxPriorityCompare { bool byFee; @@ -88,15 +87,15 @@ public: { if (byFee) { - if (a.get<1>() == b.get<1>()) - return a.get<0>() < b.get<0>(); - return a.get<1>() < b.get<1>(); + if (std::get<1>(a) == std::get<1>(b)) + return std::get<0>(a) < std::get<0>(b); + return std::get<1>(a) < std::get<1>(b); } else { - if (a.get<0>() == b.get<0>()) - return a.get<1>() < b.get<1>(); - return a.get<0>() < b.get<0>(); + if (std::get<0>(a) == std::get<0>(b)) + return std::get<1>(a) < std::get<1>(b); + return std::get<0>(a) < std::get<0>(b); } } }; @@ -104,12 +103,12 @@ public: // CreateNewBlock: create new block (without proof-of-work/with provided coinstake) std::shared_ptr CreateNewBlock(CWallet* pwallet, CTransaction *txCoinStake) { - bool fProofOfStake = txCoinStake != NULL; + bool fProofOfStake = txCoinStake != nullptr; // Create new block - shared_ptr pblock(new CBlock()); + std::shared_ptr pblock(new CBlock()); if (!pblock.get()) - return NULL; + return nullptr; // Create coinbase tx CTransaction txCoinBase; @@ -159,8 +158,11 @@ std::shared_ptr CreateNewBlock(CWallet* pwallet, CTransaction *txCoinSta // 1-satoshi-fee transactions. It should be set above the real // cost to you of processing a transaction. int64_t nMinTxFee = MIN_TX_FEE; - if (mapArgs.count("-mintxfee")) - ParseMoney(mapArgs["-mintxfee"], nMinTxFee); + if (mapArgs.count("-mintxfee")) { + bool fResult = ParseMoney(mapArgs["-mintxfee"], nMinTxFee); + if (!fResult) // Parse error + nMinTxFee = MIN_TX_FEE; + } CBlockIndex* pindexPrev = pindexBest; @@ -174,19 +176,19 @@ std::shared_ptr CreateNewBlock(CWallet* pwallet, CTransaction *txCoinSta CTxDB txdb("r"); // Priority order to process transactions - list vOrphan; // list memory doesn't move - map > mapDependers; + std::list vOrphan; // list memory doesn't move + std::map > mapDependers; // This vector will be sorted into a priority queue: - vector vecPriority; + std::vector vecPriority; vecPriority.reserve(mempool.mapTx.size()); - for (map::iterator mi = mempool.mapTx.begin(); mi != mempool.mapTx.end(); ++mi) + for (std::map::iterator mi = mempool.mapTx.begin(); mi != mempool.mapTx.end(); ++mi) { CTransaction& tx = (*mi).second; if (tx.IsCoinBase() || tx.IsCoinStake() || !tx.IsFinal()) continue; - COrphan* porphan = NULL; + COrphan* porphan = nullptr; double dPriority = 0; int64_t nTotalIn = 0; bool fMissingInputs = false; @@ -249,7 +251,7 @@ std::shared_ptr CreateNewBlock(CWallet* pwallet, CTransaction *txCoinSta } // Collect transactions into block - map mapTestPool; + std::map mapTestPool; uint64_t nBlockSize = 1000; uint64_t nBlockTx = 0; int nBlockSigOps = 100; @@ -261,9 +263,9 @@ std::shared_ptr CreateNewBlock(CWallet* pwallet, CTransaction *txCoinSta while (!vecPriority.empty()) { // Take highest priority transaction off the priority queue: - double dPriority = vecPriority.front().get<0>(); - double dFeePerKb = vecPriority.front().get<1>(); - CTransaction& tx = *(vecPriority.front().get<2>()); + double dPriority = std::get<0>(vecPriority.front()); + double dFeePerKb = std::get<1>(vecPriority.front()); + CTransaction& tx = *(std::get<2>(vecPriority.front())); std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer); vecPriority.pop_back(); @@ -298,7 +300,7 @@ std::shared_ptr CreateNewBlock(CWallet* pwallet, CTransaction *txCoinSta // Connecting shouldn't fail due to dependency on other memory pool transactions // because we're already processing them in order of dependency - map mapTestPoolTmp(mapTestPool); + std::map mapTestPoolTmp(mapTestPool); MapPrevTx mapInputs; bool fInvalid; if (!tx.FetchInputs(txdb, mapTestPoolTmp, false, true, mapInputs, fInvalid)) @@ -370,8 +372,8 @@ std::shared_ptr CreateNewBlock(CWallet* pwallet, CTransaction *txCoinSta pblock->hashPrevBlock = pindexPrev->GetBlockHash(); if (!fProofOfStake) { - pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, pblock->GetMaxTransactionTime()); - pblock->nTime = max(pblock->GetBlockTime(), PastDrift(pindexPrev->GetBlockTime())); + pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, pblock->GetMaxTransactionTime()); + pblock->nTime = std::max(pblock->GetBlockTime(), PastDrift(pindexPrev->GetBlockTime())); pblock->UpdateTime(pindexPrev); } pblock->nNonce = 0; @@ -381,7 +383,7 @@ std::shared_ptr CreateNewBlock(CWallet* pwallet, CTransaction *txCoinSta } -void IncrementExtraNonce(shared_ptr& pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce) +void IncrementExtraNonce(std::shared_ptr& pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce) { // Update nExtraNonce static uint256 hashPrevBlock; @@ -400,7 +402,7 @@ void IncrementExtraNonce(shared_ptr& pblock, CBlockIndex* pindexPrev, un } -void FormatHashBuffers(const shared_ptr& pblock, char* pmidstate, char* pdata, char* phash1) +void FormatHashBuffers(const std::shared_ptr& pblock, char* pmidstate, char* pdata, char* phash1) { // // Pre-build hash buffers @@ -478,7 +480,7 @@ bool CheckWork(const std::shared_ptr& pblock, CWallet& wallet, CReserveK } // Process this block the same as if we had received it from another node - if (!ProcessBlock(NULL, pblock.get())) + if (!ProcessBlock(nullptr, pblock.get())) return error("CheckWork() : ProcessBlock, block not accepted"); } @@ -515,7 +517,7 @@ bool CheckStake(const std::shared_ptr& pblock, CWallet& wallet) } // Process this block the same as if we had received it from another node - if (!ProcessBlock(NULL, pblock.get())) + if (!ProcessBlock(nullptr, pblock.get())) return error("CheckStake() : ProcessBlock, block not accepted"); } @@ -554,7 +556,7 @@ bool FillMap(CWallet *pwallet, uint32_t nUpperTime, MidstateMap &inputsMap) for(CoinsSet::const_iterator pcoin = setCoins.begin(); pcoin != setCoins.end(); pcoin++) { - pair key = make_pair(pcoin->first->GetHash(), pcoin->second); + std::pair key = {pcoin->first->GetHash(), pcoin->second}; // Skip existent inputs if (inputsMap.find(key) != inputsMap.end()) @@ -562,7 +564,7 @@ bool FillMap(CWallet *pwallet, uint32_t nUpperTime, MidstateMap &inputsMap) // Trying to parse scriptPubKey txnouttype whichType; - vector vSolutions; + std::vector vSolutions; if (!Solver(pcoin->first->vout[pcoin->second].scriptPubKey, whichType, vSolutions)) continue; @@ -593,7 +595,7 @@ bool FillMap(CWallet *pwallet, uint32_t nUpperTime, MidstateMap &inputsMap) ssKernel << block.nTime << (txindex.pos.nTxPos - txindex.pos.nBlockPos) << pcoin->first->nTime << pcoin->second; // (txid, vout.n) => (kernel, (tx.nTime, nAmount)) - inputsMap[key] = make_pair(std::vector(ssKernel.begin(), ssKernel.end()), make_pair(pcoin->first->nTime, pcoin->first->vout[pcoin->second].nValue)); + inputsMap[key] = {std::vector(ssKernel.begin(), ssKernel.end()), {pcoin->first->nTime, pcoin->first->vout[pcoin->second].nValue}}; } nStakeInputsMapSize = inputsMap.size(); @@ -617,7 +619,7 @@ bool ScanMap(const MidstateMap &inputsMap, uint32_t nBits, MidstateMap::key_type std::pair interval; interval.first = nSearchTime; - interval.second = nSearchTime - min(nSearchTime-nLastCoinStakeSearchTime, nMaxStakeSearchInterval); + interval.second = nSearchTime - std::min(nSearchTime-nLastCoinStakeSearchTime, nMaxStakeSearchInterval); // (txid, nout) => (kernel, (tx.nTime, nAmount)) for(MidstateMap::const_iterator input = inputsMap.begin(); input != inputsMap.end(); input++) @@ -717,7 +719,7 @@ void ThreadStakeMiner(void* parg) // Create new coinstake transaction if (!pwallet->CreateCoinStake(LuckyInput.first, LuckyInput.second, solution.second, nBits, txCoinStake, key)) { - string strMessage = _("Warning: Unable to create coinstake transaction, see debug.log for the details. Mining thread has been stopped."); + std::string strMessage = _("Warning: Unable to create coinstake transaction, see debug.log for the details. Mining thread has been stopped."); strMiscWarning = strMessage; printf("*** %s\n", strMessage.c_str()); @@ -728,7 +730,7 @@ void ThreadStakeMiner(void* parg) std::shared_ptr pblock = CreateNewBlock(pwallet, &txCoinStake); if (!pblock) { - string strMessage = _("Warning: Unable to allocate memory for the new block object. Mining thread has been stopped."); + std::string strMessage = _("Warning: Unable to allocate memory for the new block object. Mining thread has been stopped."); strMiscWarning = strMessage; printf("*** %s\n", strMessage.c_str()); @@ -741,7 +743,7 @@ void ThreadStakeMiner(void* parg) // ... and sign it if (!key.Sign(pblock->GetHash(), pblock->vchBlockSig)) { - string strMessage = _("Warning: Proof-of-Stake miner is unable to sign the block (locked wallet?). Mining thread has been stopped."); + std::string strMessage = _("Warning: Proof-of-Stake miner is unable to sign the block (locked wallet?). Mining thread has been stopped."); strMiscWarning = strMessage; printf("*** %s\n", strMessage.c_str()); @@ -782,7 +784,7 @@ void ThreadStakeMiner(void* parg) PrintException(&e, "ThreadStakeMinter()"); } catch (...) { vnThreadsRunning[THREAD_MINTER]--; - PrintException(NULL, "ThreadStakeMinter()"); + PrintException(nullptr, "ThreadStakeMinter()"); } printf("ThreadStakeMinter exiting, %d threads remaining\n", vnThreadsRunning[THREAD_MINTER]); } -- 1.7.1