Skip to content

Commit c87f51e

Browse files
committed
Merge #7663: Make the generate RPC call function for non-regtest
8a253b3 Make the generate RPC call function for non-regtest (Pieter Wuille)
2 parents 11c7699 + 8a253b3 commit c87f51e

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
2828
{ "setmocktime", 0 },
2929
{ "getaddednodeinfo", 0 },
3030
{ "generate", 0 },
31+
{ "generate", 1 },
3132
{ "getnetworkhashps", 0 },
3233
{ "getnetworkhashps", 1 },
3334
{ "sendtoaddress", 1 },

src/rpc/mining.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,29 @@ UniValue getnetworkhashps(const UniValue& params, bool fHelp)
9595

9696
UniValue generate(const UniValue& params, bool fHelp)
9797
{
98-
if (fHelp || params.size() < 1 || params.size() > 1)
98+
if (fHelp || params.size() < 1 || params.size() > 2)
9999
throw runtime_error(
100-
"generate numblocks\n"
101-
"\nMine blocks immediately (before the RPC call returns)\n"
102-
"\nNote: this function can only be used on the regtest network\n"
100+
"generate numblocks ( maxtries )\n"
101+
"\nMine up to numblocks blocks immediately (before the RPC call returns)\n"
103102
"\nArguments:\n"
104103
"1. numblocks (numeric, required) How many blocks are generated immediately.\n"
104+
"2. maxtries (numeric, optional) How many iterations to try (default = 1000000).\n"
105105
"\nResult\n"
106106
"[ blockhashes ] (array) hashes of blocks generated\n"
107107
"\nExamples:\n"
108108
"\nGenerate 11 blocks\n"
109109
+ HelpExampleCli("generate", "11")
110110
);
111111

112-
if (!Params().MineBlocksOnDemand())
113-
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "This method can only be used on regtest");
114-
112+
static const int nInnerLoopCount = 0x10000;
115113
int nHeightStart = 0;
116114
int nHeightEnd = 0;
117115
int nHeight = 0;
118116
int nGenerate = params[0].get_int();
117+
uint64_t nMaxTries = 1000000;
118+
if (params.size() > 1) {
119+
nMaxTries = params[1].get_int();
120+
}
119121

120122
boost::shared_ptr<CReserveScript> coinbaseScript;
121123
GetMainSignals().ScriptForMining(coinbaseScript);
@@ -146,10 +148,15 @@ UniValue generate(const UniValue& params, bool fHelp)
146148
LOCK(cs_main);
147149
IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce);
148150
}
149-
while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits, Params().GetConsensus())) {
150-
// Yes, there is a chance every nonce could fail to satisfy the -regtest
151-
// target -- 1 in 2^(2^32). That ain't gonna happen.
151+
while (nMaxTries > 0 && pblock->nNonce < nInnerLoopCount && !CheckProofOfWork(pblock->GetHash(), pblock->nBits, Params().GetConsensus())) {
152152
++pblock->nNonce;
153+
--nMaxTries;
154+
}
155+
if (nMaxTries == 0) {
156+
break;
157+
}
158+
if (pblock->nNonce == nInnerLoopCount) {
159+
continue;
153160
}
154161
CValidationState state;
155162
if (!ProcessNewBlock(state, Params(), NULL, pblock, true, NULL))

0 commit comments

Comments
 (0)