From b4753d67579f81ab8f6c96d7350066328561b500 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Wed, 6 Apr 2016 10:27:51 +0200 Subject: [PATCH] Remove internal miner --- src/miner.cpp | 188 -------------------------------------------------- src/miner.h | 1 - 2 files changed, 189 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index 3a117fc70..528c6342a 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -352,194 +352,6 @@ void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned pblock->hashMerkleRoot = BlockMerkleRoot(*pblock); } -////////////////////////////////////////////////////////////////////////////// -// -// Internal miner -// - -static bool ProcessBlockFound(const CBlock* pblock, const CChainParams& chainparams) -{ - LogPrintf("%s\n", pblock->ToString()); - LogPrintf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue)); - - // Found a solution - { - LOCK(cs_main); - if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash()) - return error("BitcoinMiner: generated block is stale"); - } - - // Inform about the new block - GetMainSignals().BlockFound(pblock->GetHash()); - - // Process this block the same as if we had received it from another node - CValidationState state; - if (!ProcessNewBlock(state, chainparams, NULL, pblock, true, NULL)) - return error("BitcoinMiner: ProcessNewBlock, block not accepted"); - - return true; -} - -void static BitcoinMiner(const CChainParams& chainparams) -{ - LogPrintf("BitcoinMiner started\n"); - SetThreadPriority(THREAD_PRIORITY_LOWEST); - RenameThread("bitcoin-miner"); - - unsigned int nExtraNonce = 0; - - std::shared_ptr coinbaseScript; - GetMainSignals().ScriptForMining(coinbaseScript); - - try { - // Throw an error if no script was provided. This can happen - // due to some internal error but also if the keypool is empty. - // In the latter case, already the pointer is NULL. - if (!coinbaseScript || coinbaseScript->reserveScript.empty()) - throw std::runtime_error("No coinbase script available (mining requires a wallet)"); - - while (true) { - if (chainparams.MiningRequiresPeers()) { - // Busy-wait for the network to come online so we don't waste time mining - // on an obsolete chain. In regtest mode we expect to fly solo. - do { - bool fvNodesEmpty; - { - LOCK(cs_vNodes); - fvNodesEmpty = vNodes.empty(); - } - if (!fvNodesEmpty && !IsInitialBlockDownload()) - break; - MilliSleep(1000); - } while (true); - } - - //check the block height - if (chainActive.Tip()->nHeight > Params().LastPOWBlock() + Params().GetConsensus().nStakeMinConfirmations) - { - // The stake is confirmed, stop the PoW miner - throw boost::thread_interrupted(); - } - //check the next block height - else if (chainActive.Tip()->nHeight + 1 > Params().LastPOWBlock()) - { - // Wait for the stake to be confirmed - MilliSleep(60000); - continue; - } - - // - // Create new block - // - unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated(); - CBlockIndex* pindexPrev = chainActive.Tip(); - int64_t* nFees = 0; - std::unique_ptr pblocktemplate(CreateNewBlock(chainparams, coinbaseScript->reserveScript, nFees, false)); - if (!pblocktemplate.get()) - { - LogPrintf("Error in BitcoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n"); - return; - } - CBlock *pblock = &pblocktemplate->block; - IncrementExtraNonce(pblock, pindexPrev, nExtraNonce); - - LogPrintf("Running BitcoinMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(), - ::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION)); - - // - // Search - // - int64_t nStart = GetTime(); - arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits); - uint256 thash; - - while (true) { - // Check if something found - unsigned int nHashesDone = 0; - char scratchpad[SCRYPT_SCRATCHPAD_SIZE]; - while(true) - { - scrypt_1024_1_1_256_sp(BEGIN(pblock->nVersion), BEGIN(thash), scratchpad); - if (UintToArith256(thash) <= hashTarget) - { - - SetThreadPriority(THREAD_PRIORITY_NORMAL); - LogPrintf("BitcoinMiner:\n"); - LogPrintf("proof-of-work found \n powhash: %s \ntarget: %s\n", thash.GetHex(), hashTarget.GetHex()); - ProcessBlockFound(pblock, chainparams); - SetThreadPriority(THREAD_PRIORITY_LOWEST); - coinbaseScript->KeepScript(); - - // In regression test mode, stop mining after a block is found. - if (chainparams.MineBlocksOnDemand()) - throw boost::thread_interrupted(); - - break; - } - pblock->nNonce += 1; - nHashesDone += 1; - if ((pblock->nNonce & 0xFF) == 0) - break; - } - - // Check for stop or if block needs to be rebuilt - boost::this_thread::interruption_point(); - // Regtest mode doesn't require peers - if (vNodes.empty() && chainparams.MiningRequiresPeers()) - break; - if (pblock->nNonce >= 0xffff0000) - break; - if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60) - break; - if (pindexPrev != chainActive.Tip()) - break; - - // Update nTime every few seconds - if (UpdateTime(pblock, Params().GetConsensus(), pindexPrev) < 0) - break; // Recreate the block if the clock has run backwards, - // so that we can use the correct time. - if (chainparams.GetConsensus().fPowAllowMinDifficultyBlocks) - { - // Changing pblock->nTime can change work required on testnet: - hashTarget.SetCompact(pblock->nBits); - } - } - } - } - catch (const boost::thread_interrupted&) - { - LogPrintf("BitcoinMiner terminated\n"); - throw; - } - catch (const std::runtime_error &e) - { - LogPrintf("BitcoinMiner runtime error: %s\n", e.what()); - return; - } -} - -void GenerateBitcoins(bool fGenerate, int nThreads, const CChainParams& chainparams) -{ - static boost::thread_group* minerThreads = NULL; - - if (nThreads < 0) - nThreads = GetNumCores(); - - if (minerThreads != NULL) - { - minerThreads->interrupt_all(); - delete minerThreads; - minerThreads = NULL; - } - - if (nThreads == 0 || !fGenerate) - return; - - minerThreads = new boost::thread_group(); - for (int i = 0; i < nThreads; i++) - minerThreads->create_thread(boost::bind(&BitcoinMiner, boost::cref(chainparams))); -} - #ifdef ENABLE_WALLET // novacoin: attempt to generate suitable proof-of-stake bool SignBlock(CBlock& block, CWallet& wallet, int64_t& nFees) diff --git a/src/miner.h b/src/miner.h index c905e213a..0a8cdaea4 100644 --- a/src/miner.h +++ b/src/miner.h @@ -28,7 +28,6 @@ struct CBlockTemplate }; /** Run the miner threads */ -void GenerateBitcoins(bool fGenerate, int nThreads, const CChainParams& chainparams); void ThreadStakeMiner(CWallet *pwallet, const CChainParams& chainparams); /** Sign a block */ bool SignBlock(CBlock& block, CWallet& wallet, int64_t& nFees);