aboutsummaryrefslogtreecommitdiff
path: root/src/rpcmining.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/rpcmining.cpp')
-rw-r--r--src/rpcmining.cpp110
1 files changed, 61 insertions, 49 deletions
diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp
index fcba7e222..1e6531f68 100644
--- a/src/rpcmining.cpp
+++ b/src/rpcmining.cpp
@@ -113,6 +113,64 @@ 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"
+ "\nNote: this function can only be used on the regtest network\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)");
+ if (!Params().MineBlocksOnDemand())
+ throw JSONRPCError(RPC_METHOD_NOT_FOUND, "This method can only be used on regtest");
+
+ int nHeightStart = 0;
+ int nHeightEnd = 0;
+ int nHeight = 0;
+ int nGenerate = params[0].get_int();
+ 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;
+}
+
Value setgenerate(const Array& params, bool fHelp)
{
@@ -125,9 +183,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 +209,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;
}