Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

Commit

Permalink
seperate mining and minting threads (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg-Griffith authored and Greg Griffith committed Nov 1, 2018
1 parent 1fcd1fc commit 9f3d48a
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 22 deletions.
61 changes: 48 additions & 13 deletions src/blockgeneration/blockgeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ std::unique_ptr<CBlockTemplate> CreateNewBlock(CWallet *pwallet, const CScript &

boost::thread_group *minerThreads = nullptr;

void ThreadMiner(void *parg, bool shutdownOnly, bool fProofOfStake)
void ThreadMiner(void *parg, bool shutdownOnly)
{
if (minerThreads != nullptr)
{
Expand All @@ -82,20 +82,45 @@ void ThreadMiner(void *parg, bool shutdownOnly, bool fProofOfStake)
{
return;
}

minerThreads = new boost::thread_group();
CWallet *pwallet = (CWallet *)parg;
try
{
minerThreads->create_thread(boost::bind(&EccMiner, pwallet));
}
catch (std::exception &e)
{
PrintException(&e, "ThreadECCMiner()");
}
catch (...)
{
PrintException(NULL, "ThreadECCMiner()");
}
nHPSTimerStart = 0;
dHashesPerSec = 0;
LogPrintf("Thread Miner thread exiting \n");
}

boost::thread_group *minterThreads = nullptr;

void ThreadMinter(void *parg, bool shutdownOnly)
{
if (minterThreads != nullptr)
{
minterThreads->interrupt_all();
delete minterThreads;
minterThreads = nullptr;
return;
}
if (shutdownOnly)
{
return;
}
minterThreads = new boost::thread_group();
CWallet *pwallet = (CWallet *)parg;
try
{
if (fProofOfStake)
{
minerThreads->create_thread(boost::bind(&EccMinter, pwallet));
}
else
{
minerThreads->create_thread(boost::bind(&EccMiner, pwallet));
}
minterThreads->create_thread(boost::bind(&EccMinter, pwallet));
}
catch (std::exception &e)
{
Expand All @@ -105,7 +130,17 @@ void ThreadMiner(void *parg, bool shutdownOnly, bool fProofOfStake)
{
PrintException(NULL, "ThreadECCMinter()");
}
nHPSTimerStart = 0;
dHashesPerSec = 0;
LogPrintf("Block Generation thread exiting \n");
LogPrintf("Thread Minter thread exiting \n");
}

void ThreadGeneration(void *parg, bool shutdownOnly, bool fProofOfStake)
{
if(fProofOfStake)
{
ThreadMinter(parg, shutdownOnly);
}
else
{
ThreadMiner(parg, shutdownOnly);
}
}
3 changes: 2 additions & 1 deletion src/blockgeneration/blockgeneration.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParam

std::unique_ptr<CBlockTemplate> CreateNewBlock(CWallet *pwallet, const CScript &scriptPubKeyIn, bool fProofOfStake);

void ThreadMiner(void *parg, bool shutdownOnly = false, bool fProofOfStake = false);
void ThreadGeneration(void *parg, bool shutdownOnly = false, bool fProofOfStake = false);

extern boost::thread_group *minerThreads;
extern boost::thread_group *minterThreads;

#endif // ECCOIN_BLOCKGENERATION_H
11 changes: 9 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ void Shutdown()

if (pwalletMain)
pwalletMain->Flush(false);
ThreadMiner(pwalletMain, true);
// shut off pos miner
ThreadGeneration(pwalletMain, true, true);
// shut off pow miner
ThreadGeneration(pwalletMain, true, false);
MapPort(false);
UnregisterValidationInterface(peerLogic.get());
peerLogic.reset();
Expand Down Expand Up @@ -1734,9 +1737,13 @@ bool AppInit2(boost::thread_group &threadGroup, CScheduler &scheduler)
}

// Generate coins in the background
if (gArgs.GetBoolArg("-gen", false))
{
ThreadGeneration(pwalletMain, false, true);
}
if (gArgs.GetBoolArg("-staking", false))
{
ThreadMiner(pwalletMain);
ThreadGeneration(pwalletMain, false, true);
}

// ********************************************************* Step 12: finished
Expand Down
54 changes: 48 additions & 6 deletions src/rpc/rpcmining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ UniValue getgenerate(const UniValue& params, bool fHelp)
return minerThreads != NULL;
}

UniValue getgeneratepos(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw std::runtime_error(
"getgeneratepos\n"
"\nReturn if the server is set to generate pos coins or not. The default is false.\n"
"It is set with the command line argument -staking (or " + std::string(CONF_FILENAME) + " setting staking)\n"
"It can also be set with the setgeneratepos call.\n"
"\nResult\n"
"true|false (boolean) If the server is set to generate pos coins or not\n"
"\nExamples:\n"
+ HelpExampleCli("getgeneratepos", "")
+ HelpExampleRpc("getgeneratepos", "")
);

LOCK(cs_main);
return minterThreads != NULL;
}

UniValue generateBlocks(boost::shared_ptr<CReserveScript> coinbaseScript,
int nGenerate,
uint64_t nMaxTries,
Expand Down Expand Up @@ -483,19 +502,41 @@ UniValue setgenerate(const UniValue& params, bool fHelp)
if (fHelp || params.size() != 0)
throw std::runtime_error(
"setgenerate \n"
"\nToggle pos generation on and off with this command.\n"
"\nToggle pow generation on and off with this command.\n"
"See the getgenerate call for the current setting.\n"
"\nArguments:\n"
"None, this will just toggle staking on and off"
"None, this will just toggle mining on and off"
"\nExamples:\n"
"\nToggle the pos generation\n"
"\nToggle the pow generation\n"
+ HelpExampleCli("setgenerate", "")
);

if (pnetMan->getActivePaymentNetwork()->MineBlocksOnDemand())
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Use the generate method instead of setgenerate on this network");

ThreadMiner(pwalletMain, false);
ThreadGeneration(pwalletMain, false, false);

return NullUniValue;
}

UniValue setgeneratepos(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw std::runtime_error(
"setgeneratepos \n"
"\nToggle pos generation on and off with this command.\n"
"See the getgeneratepos call for the current setting.\n"
"\nArguments:\n"
"None, this will just toggle staking on and off"
"\nExamples:\n"
"\nToggle the pos generation\n"
+ HelpExampleCli("setgeneratepos", "")
);

if (pnetMan->getActivePaymentNetwork()->MineBlocksOnDemand())
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Use the generate method instead of setgeneratepos on this network");

ThreadGeneration(pwalletMain, false, true);

return NullUniValue;
}
Expand All @@ -514,11 +555,11 @@ UniValue getmininginfo(const UniValue& params, bool fHelp)
" \"currentblocktx\": nnn, (numeric) The last block transaction\n"
" \"difficulty\": xxx.xxxxx (numeric) The current difficulty\n"
" \"errors\": \"...\" (string) Current errors\n"
" \"generate\": true|false (boolean) If the generation is on or off (see getgenerate or setgenerate calls)\n"
" \"genproclimit\": n (numeric) The processor limit for generation. -1 if no generation. (see getgenerate or setgenerate calls)\n"
" \"pooledtx\": n (numeric) The size of the mem pool\n"
" \"testnet\": true|false (boolean) If using testnet or not\n"
" \"chain\": \"xxxx\", (string) current network name as defined in BIP70 (main, test, regtest)\n"
" \"generate\": true|false (boolean) If the pow generation is on or off (see getgenerate or setgenerate calls)\n"
" \"generatepos\": true|false (boolean) If the pos generation is on or off (see getgeneratepos or setgeneratepos calls)\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("getmininginfo", "")
Expand All @@ -539,6 +580,7 @@ UniValue getmininginfo(const UniValue& params, bool fHelp)
obj.push_back(Pair("testnet", pnetMan->getActivePaymentNetwork()->TestnetToBeDeprecatedFieldRPC()));
obj.push_back(Pair("chain", pnetMan->getActivePaymentNetwork()->NetworkIDString()));
obj.push_back(Pair("generate", getgenerate(params, false)));
obj.push_back(Pair("generatepos", getgeneratepos(params, false)));
return obj;
}

Expand Down
2 changes: 2 additions & 0 deletions src/rpc/rpcserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ static const CRPCCommand vRPCCommands[] =
/* Coin generation */
{ "generating", "getgenerate", &getgenerate, true },
{ "generating", "setgenerate", &setgenerate, true },
{ "generating", "getgeneratepos", &getgeneratepos, true },
{ "generating", "setgeneratepos", &setgeneratepos, true },
{ "generating", "generate", &generate, true },
{ "generating", "generatepos", &generatepos, true },
{ "generating", "generatetoaddress", &generatetoaddress, true },
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/rpcserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ extern UniValue importwallet(const UniValue& params, bool fHelp);

extern UniValue getgenerate(const UniValue& params, bool fHelp); // in rpcmining.cpp
extern UniValue setgenerate(const UniValue& params, bool fHelp);
extern UniValue getgeneratepos(const UniValue& params, bool fHelp);
extern UniValue setgeneratepos(const UniValue& params, bool fHelp);
extern UniValue generate(const UniValue& params, bool fHelp);
extern UniValue generatepos(const UniValue& params, bool fHelp);
extern UniValue generatetoaddress(const UniValue& params, bool fHelp);
Expand Down

0 comments on commit 9f3d48a

Please sign in to comment.