Introduce separate 'generate' RPC call
This commit is contained in:
@@ -113,6 +113,45 @@ Value getgenerate(const Array& params, bool fHelp)
|
||||
return GetBoolArg("-gen", false);
|
||||
}
|
||||
|
||||
Value generate(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() < 1 || params.size() > 1)
|
||||
throw runtime_error(
|
||||
"generate numblocks\n"
|
||||
"\nMine blocks immediately (before the RPC call returns)\n"
|
||||
"1. numblocks (numeric) How many blocks are generated immediately.\n"
|
||||
"\nResult\n"
|
||||
"[ blockhashes ] (array) hashes of blocks generated\n"
|
||||
"\nExamples:\n"
|
||||
"\nGenerate 11 blocks\n"
|
||||
+ HelpExampleCli("generate", "11")
|
||||
);
|
||||
|
||||
if (pwalletMain == NULL)
|
||||
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)");
|
||||
|
||||
int nHeightStart = 0;
|
||||
int nHeightEnd = 0;
|
||||
int nHeight = 0;
|
||||
int nGenerate = params[0].get_int();
|
||||
|
||||
{ // Don't keep cs_main locked
|
||||
LOCK(cs_main);
|
||||
nHeightStart = chainActive.Height();
|
||||
nHeight = nHeightStart;
|
||||
nHeightEnd = nHeightStart+nGenerate;
|
||||
}
|
||||
Array blockHashes;
|
||||
while (nHeight < nHeightEnd) {
|
||||
uint256 hash;
|
||||
if (!MineBlock(pwalletMain, hash))
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet keypool empty");
|
||||
|
||||
++nHeight;
|
||||
blockHashes.push_back(hash.GetHex());
|
||||
}
|
||||
return blockHashes;
|
||||
}
|
||||
|
||||
Value setgenerate(const Array& params, bool fHelp)
|
||||
{
|
||||
@@ -125,9 +164,6 @@ Value setgenerate(const Array& params, bool fHelp)
|
||||
"\nArguments:\n"
|
||||
"1. generate (boolean, required) Set to true to turn on generation, off to turn off.\n"
|
||||
"2. genproclimit (numeric, optional) Set the processor limit for when generation is on. Can be -1 for unlimited.\n"
|
||||
" Note: in -regtest mode, genproclimit controls how many blocks are generated immediately.\n"
|
||||
"\nResult\n"
|
||||
"[ blockhashes ] (array, -regtest only) hashes of blocks generated\n"
|
||||
"\nExamples:\n"
|
||||
"\nSet the generation on with a limit of one processor\n"
|
||||
+ HelpExampleCli("setgenerate", "true 1") +
|
||||
@@ -154,52 +190,9 @@ Value setgenerate(const Array& params, bool fHelp)
|
||||
fGenerate = false;
|
||||
}
|
||||
|
||||
// -regtest mode: don't return until nGenProcLimit blocks are generated
|
||||
if (fGenerate && Params().MineBlocksOnDemand())
|
||||
{
|
||||
int nHeightStart = 0;
|
||||
int nHeightEnd = 0;
|
||||
int nHeight = 0;
|
||||
int nGenerate = (nGenProcLimit > 0 ? nGenProcLimit : 1);
|
||||
CReserveKey reservekey(pwalletMain);
|
||||
|
||||
{ // Don't keep cs_main locked
|
||||
LOCK(cs_main);
|
||||
nHeightStart = chainActive.Height();
|
||||
nHeight = nHeightStart;
|
||||
nHeightEnd = nHeightStart+nGenerate;
|
||||
}
|
||||
unsigned int nExtraNonce = 0;
|
||||
Array blockHashes;
|
||||
while (nHeight < nHeightEnd)
|
||||
{
|
||||
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
|
||||
if (!pblocktemplate.get())
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet keypool empty");
|
||||
CBlock *pblock = &pblocktemplate->block;
|
||||
{
|
||||
LOCK(cs_main);
|
||||
IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce);
|
||||
}
|
||||
while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits, Params().GetConsensus())) {
|
||||
// Yes, there is a chance every nonce could fail to satisfy the -regtest
|
||||
// target -- 1 in 2^(2^32). That ain't gonna happen.
|
||||
++pblock->nNonce;
|
||||
}
|
||||
CValidationState state;
|
||||
if (!ProcessNewBlock(state, NULL, pblock))
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
|
||||
++nHeight;
|
||||
blockHashes.push_back(pblock->GetHash().GetHex());
|
||||
}
|
||||
return blockHashes;
|
||||
}
|
||||
else // Not -regtest: start generate thread, return immediately
|
||||
{
|
||||
mapArgs["-gen"] = (fGenerate ? "1" : "0");
|
||||
mapArgs ["-genproclimit"] = itostr(nGenProcLimit);
|
||||
GenerateBitcoins(fGenerate, pwalletMain, nGenProcLimit);
|
||||
}
|
||||
mapArgs["-gen"] = (fGenerate ? "1" : "0");
|
||||
mapArgs ["-genproclimit"] = itostr(nGenProcLimit);
|
||||
GenerateBitcoins(fGenerate, pwalletMain, nGenProcLimit);
|
||||
|
||||
return Value::null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user