diff --git a/src/clientversion.h b/src/clientversion.h index ff10c029..e3629894 100644 --- a/src/clientversion.h +++ b/src/clientversion.h @@ -28,7 +28,7 @@ #define CLIENT_VERSION_MAJOR 0 #define CLIENT_VERSION_MINOR 2 #define CLIENT_VERSION_REVISION 5 -#define CLIENT_VERSION_BUILD 9 +#define CLIENT_VERSION_BUILD 10 //! Set to true for release, false for prerelease or test build #define CLIENT_VERSION_IS_RELEASE true diff --git a/src/init.cpp b/src/init.cpp index 062b5e5f..0483e665 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -208,7 +208,7 @@ void Shutdown() if (pwalletMain) pwalletMain->Flush(false); - ThreadScryptMiner(pwalletMain, true); + ThreadMiner(pwalletMain, true); MapPort(false); UnregisterValidationInterface(peerLogic.get()); peerLogic.reset(); @@ -1644,7 +1644,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) // Generate coins in the background if(gArgs.GetBoolArg("-staking", false)) { - ThreadScryptMiner(pwalletMain); + ThreadMiner(pwalletMain); } // ********************************************************* Step 12: finished diff --git a/src/miner.cpp b/src/miner.cpp index 1aec80ad..7127e2b9 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -38,6 +38,7 @@ #include "processblock.h" #include "networks/netman.h" #include "net.h" +#include "policy/policy.h" #include #include @@ -53,10 +54,6 @@ int64_t nLastCoinStakeSearchInterval = 0; double dHashesPerSec; int64_t nHPSTimerStart; -static std::string strMintMessage = "Info: Minting suspended due to locked wallet."; -static std::string strMintWarning; - - ////////////////////////////////////////////////////////////////////////////// // @@ -172,21 +169,18 @@ class ScoreCompare } }; - -/** Used as the flags parameter to sequence and nLocktime checks in non-consensus code. */ -static const unsigned int STANDARD_LOCKTIME_VERIFY_FLAGS = LOCKTIME_VERIFY_SEQUENCE | - LOCKTIME_MEDIAN_TIME_PAST; - // CreateNewBlock: // fProofOfStake: try (best effort) to make a proof-of-stake block -CBlockTemplate* CreateNewBlock(CWallet* pwallet, bool fProofOfStake) +std::unique_ptr CreateNewBlock(CWallet* pwallet, bool fProofOfStake) { CReserveKey reservekey(pwallet); // Create new block - std::auto_ptr pblocktemplate(new CBlockTemplate()); + std::unique_ptr pblocktemplate(new CBlockTemplate()); if(!pblocktemplate.get()) - return NULL; + { + return nullptr; + } CBlock *pblock = &pblocktemplate->block; // pointer for convenience // Create coinbase tx @@ -433,7 +427,7 @@ CBlockTemplate* CreateNewBlock(CWallet* pwallet, bool fProofOfStake) pblock->nNonce = 0; } - return pblocktemplate.release(); + return std::move(pblocktemplate); } @@ -539,20 +533,16 @@ bool CheckWork(const std::shared_ptr pblock, CWallet& wallet, CRes return true; } -void ScryptMiner(CWallet *pwallet) +void EccMiner(CWallet *pwallet) { void *scratchbuf = scrypt_buffer_alloc(); - LogPrintf("CPUMiner started for proof-of-%s\n", "stake"); SetThreadPriority(THREAD_PRIORITY_LOWEST); - // Make this thread recognisable as the mining thread - RenameThread("bitcoin-miner"); - + RenameThread("ecc-miner"); // Each thread has its own key and counter CReserveKey reservekey(pwallet); unsigned int nExtraNonce = 0; - while (true) { if (fShutdown) @@ -563,82 +553,44 @@ void ScryptMiner(CWallet *pwallet) if (fShutdown) return; } - while (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) < 6 || pnetMan->getActivePaymentNetwork()->getChainManager()->IsInitialBlockDownload()) + while (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) < 6 || pnetMan->getActivePaymentNetwork()->getChainManager()->IsInitialBlockDownload() || pwallet->IsLocked()) { MilliSleep(1000); if (fShutdown) return; } - - while (pwallet->IsLocked()) - { - strMintWarning = strMintMessage; - MilliSleep(1000); - } - strMintWarning = ""; - // // Create new block // unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated(); CBlockIndex* pindexPrev = pnetMan->getActivePaymentNetwork()->getChainManager()->chainActive.Tip(); - - std::auto_ptr pblocktemplate(CreateNewBlock(pwallet, true)); + std::unique_ptr pblocktemplate(CreateNewBlock(pwallet, true)); if (!pblocktemplate.get()) { LogPrintf("Error in Miner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n"); return; } CBlock *pblock = &pblocktemplate->block; - const std::shared_ptr spblock(pblock); - IncrementExtraNonce(pblock, pindexPrev, nExtraNonce); - - { - // ppcoin: if proof-of-stake block found then process block - if (pblock->IsProofOfStake()) - { - if (!pblock->SignScryptBlock(*pwalletMain)) - { - strMintWarning = strMintMessage; - continue; - } - strMintWarning = ""; - LogPrintf("CPUMiner : proof-of-stake block found %s\n", pblock->GetHash().ToString().c_str()); - SetThreadPriority(THREAD_PRIORITY_NORMAL); - CheckWork(spblock, *pwalletMain, reservekey); - SetThreadPriority(THREAD_PRIORITY_LOWEST); - } - MilliSleep(1000); // 1 second delay - continue; - } - LogPrintf("Running Miner with %u transactions in block (%u bytes)\n", pblock->vtx.size(), ::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION)); - // // Pre-build hash buffers // char pmidstatebuf[32+16]; char* pmidstate = alignup<16>(pmidstatebuf); char pdatabuf[128+16]; char* pdata = alignup<16>(pdatabuf); char phash1buf[64+16]; char* phash1 = alignup<16>(phash1buf); - FormatHashBuffers(pblock, pmidstate, pdata, phash1); - unsigned int& nBlockTime = *(unsigned int*)(pdata + 64 + 4); unsigned int& nBlockNonce = *(unsigned int*)(pdata + 64 + 12); - - // // Search // int64_t nStart = GetTime(); arith_uint256 hashTarget = UintToArith256(CBigNum().SetCompact(pblock->nBits).getuint256()); - unsigned int max_nonce = 0xffff0000; CBlockHeader res_header; arith_uint256 result; - while(true) { unsigned int nHashesDone = 0; @@ -663,18 +615,15 @@ void ScryptMiner(CWallet *pwallet) assert(result == UintToArith256(pblock->GetHash())); if (!pblock->SignScryptBlock(*pwalletMain)) { -// strMintWarning = strMintMessage; break; } - strMintWarning = ""; - SetThreadPriority(THREAD_PRIORITY_NORMAL); + const std::shared_ptr spblock = std::make_shared(*pblock); CheckWork(spblock, *pwalletMain, reservekey); SetThreadPriority(THREAD_PRIORITY_LOWEST); break; } } - // Meter hashes/sec static int64_t nHashCounter; if (nHPSTimerStart == 0) @@ -697,7 +646,6 @@ void ScryptMiner(CWallet *pwallet) } } } - // Check for stop or if block needs to be rebuilt if (fShutdown) return; @@ -709,24 +657,79 @@ void ScryptMiner(CWallet *pwallet) break; if (pindexPrev != pnetMan->getActivePaymentNetwork()->getChainManager()->chainActive.Tip()) break; - // Update nTime every few seconds pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, pblock->GetMaxTransactionTime()); pblock->nTime = std::max(pblock->GetBlockTime(), pindexPrev->GetBlockTime() - nMaxClockDrift); pblock->UpdateTime(); nBlockTime = ByteReverse(pblock->nTime); - if (pblock->GetBlockTime() >= (int64_t)pblock->vtx[0]->nTime + nMaxClockDrift) break; // need to update coinbase timestamp } } - scrypt_buffer_free(scratchbuf); } +void EccMinter(CWallet *pwallet) +{ + LogPrintf("CPUMiner started for proof-of-%s\n", "stake"); + SetThreadPriority(THREAD_PRIORITY_LOWEST); + // Make this thread recognisable as the mining thread + RenameThread("ecc-minter"); + // Each thread has its own key and counter + CReserveKey reservekey(pwallet); + unsigned int nExtraNonce = 0; + while (true) + { + if (fShutdown) + return; + if(!g_connman) + { + MilliSleep(1000); + if (fShutdown) + return; + } + while (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) < 6 || pnetMan->getActivePaymentNetwork()->getChainManager()->IsInitialBlockDownload() || pwallet->IsLocked()) + { + MilliSleep(1000); + if (fShutdown) + return; + } + // + // Create new block + // + CBlockIndex* pindexPrev = pnetMan->getActivePaymentNetwork()->getChainManager()->chainActive.Tip(); + std::unique_ptr pblocktemplate(CreateNewBlock(pwallet, true)); + if (!pblocktemplate.get()) + { + LogPrintf("Error in Miner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n"); + return; + } + CBlock *pblock = &pblocktemplate->block; + IncrementExtraNonce(pblock, pindexPrev, nExtraNonce); + // ppcoin: if proof-of-stake block found then process block + if (pblock->IsProofOfStake()) + { + if (!pblock->SignScryptBlock(*pwalletMain)) + { + continue; + } + LogPrintf("CPUMiner : proof-of-stake block found %s\n", pblock->GetHash().ToString().c_str()); + SetThreadPriority(THREAD_PRIORITY_NORMAL); + const std::shared_ptr spblock = std::make_shared(*pblock); + CheckWork(spblock, *pwalletMain, reservekey); + SetThreadPriority(THREAD_PRIORITY_LOWEST); + } + MilliSleep(1000); // 1 second delay + continue; + } +} + + + + boost::thread_group* minerThreads = nullptr; -void ThreadScryptMiner(void* parg, bool shutdownOnly) +void ThreadMiner(void* parg, bool shutdownOnly) { if (minerThreads != nullptr) @@ -748,7 +751,7 @@ void ThreadScryptMiner(void* parg, bool shutdownOnly) CWallet* pwallet = (CWallet*)parg; try { - minerThreads->create_thread(boost::bind(&ScryptMiner, pwallet)); + minerThreads->create_thread(boost::bind(&EccMinter, pwallet)); } catch (std::exception& e) { PrintException(&e, "ThreadECCMinter()"); diff --git a/src/miner.h b/src/miner.h index b73d3c5d..c9be18f9 100644 --- a/src/miner.h +++ b/src/miner.h @@ -30,9 +30,6 @@ static const bool DEFAULT_GENERATE = false; static const bool DEFAULT_PRINTPRIORITY = false; -/** Check mined proof-of-stake block */ -bool CheckStake(CBlock* pblock, CWallet& wallet); - /** Base sha256 mining transform */ void SHA256Transform(void* pstate, void* pinput, const void* pinit); @@ -40,7 +37,10 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1); bool CheckWork(const std::shared_ptr pblock, CWallet& wallet, CReserveKey& reservekey); -void ThreadScryptMiner(void* parg, bool shutdownOnly=false); +/** Check mined proof-of-stake block */ +bool CheckStake(CBlock* pblock, CWallet& wallet); + +void ThreadMiner(void* parg, bool shutdownOnly=false); struct CBlockTemplate { @@ -49,7 +49,7 @@ struct CBlockTemplate std::vector vTxSigOps; }; -CBlockTemplate* CreateNewBlock(CWallet* pwallet, bool fProofOfStake); +std::unique_ptr CreateNewBlock(CWallet* pwallet, bool fProofOfStake); extern boost::thread_group* minerThreads; diff --git a/src/rpc/rpcmining.cpp b/src/rpc/rpcmining.cpp index ed3a576a..1cee1bd1 100644 --- a/src/rpc/rpcmining.cpp +++ b/src/rpc/rpcmining.cpp @@ -188,7 +188,7 @@ UniValue generate(const UniValue& params, bool fHelp) UniValue blockHashes(UniValue::VARR); while (nHeight < nHeightEnd) { - std::auto_ptr pblocktemplate(CreateNewBlock(pwalletMain, true)); + std::unique_ptr pblocktemplate(CreateNewBlock(pwalletMain, true)); if (!pblocktemplate.get()) throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block"); CBlock *pblock = &pblocktemplate->block; @@ -238,7 +238,7 @@ UniValue setgenerate(const UniValue& params, bool fHelp) if (pnetMan->getActivePaymentNetwork()->MineBlocksOnDemand()) throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Use the generate method instead of setgenerate on this network"); - ThreadScryptMiner(pwalletMain, false); + ThreadMiner(pwalletMain, false); return NullUniValue; } @@ -532,7 +532,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp) // Update block static CBlockIndex* pindexPrev; static int64_t nStart; - static CBlockTemplate* pblocktemplate; + static std::unique_ptr pblocktemplate; if (pindexPrev != pnetMan->getActivePaymentNetwork()->getChainManager()->chainActive.Tip() || (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5)) { @@ -547,12 +547,14 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp) // Create new block if(pblocktemplate) { - delete pblocktemplate; - pblocktemplate = NULL; + pblocktemplate.reset(); + pblocktemplate = nullptr; } - pblocktemplate = CreateNewBlock(pwalletMain, true); + pblocktemplate.reset(CreateNewBlock(pwalletMain, true).get()); if (!pblocktemplate) + { throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory"); + } // Need to update only after we know CreateNewBlock succeeded pindexPrev = pindexPrevNew; diff --git a/src/rpc/rpcmisc.cpp b/src/rpc/rpcmisc.cpp index dd50e60a..70325ca1 100644 --- a/src/rpc/rpcmisc.cpp +++ b/src/rpc/rpcmisc.cpp @@ -41,51 +41,6 @@ #include -static std::set reloadableSettings = {"-staking", "-rescan"}; - -UniValue reloadconfig (const UniValue& params, bool fHelp) -{ - if (fHelp || params.size() != 0) - { - throw std::runtime_error( - "reloadconfig\n" - "Returns an object containing the config file arguments that were reloaded.\n" - "The only commands supported for reloading right now are: \n" - "staking \n" - "rescan \n" - ); - } - UniValue obj(UniValue::VOBJ); - - boost::filesystem::ifstream streamConfig(gArgs.GetConfigFile()); - std::set::iterator iter; - std::set processedArgs; - std::set setOptions; - setOptions.insert("*"); - for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it) - { - // Don't overwrite existing settings so command line settings override eccoin.conf - std::string strKey = std::string("-") + it->string_key; - std::string strValue = it->value[0]; - InterpretNegativeSetting(strKey, strValue); - iter = reloadableSettings.find(strKey); - if(iter != reloadableSettings.end() && strValue == std::string("1") && processedArgs.find(strKey) != processedArgs.end()) - { - obj.push_back(Pair(strKey, "was reloaded")); - if(strKey == "-staking") - { - ThreadScryptMiner(pwalletMain, false); - } - if(strKey == "-rescan") - { - pwalletMain->ScanForWalletTransactions(pnetMan->getActivePaymentNetwork()->getChainManager()->chainActive.Genesis(), true); - } - processedArgs.insert(strKey); - } - } - return obj; -} - /** * @note Do not add or change anything in the information returned by this * method. `getinfo` exists for backwards-compatibility only. It combines