@@ -95,27 +95,29 @@ UniValue getnetworkhashps(const UniValue& params, bool fHelp)
95
95
96
96
UniValue generate (const UniValue& params, bool fHelp )
97
97
{
98
- if (fHelp || params.size () < 1 || params.size () > 1 )
98
+ if (fHelp || params.size () < 1 || params.size () > 2 )
99
99
throw runtime_error (
100
- " generate numblocks\n "
101
- " \n Mine blocks immediately (before the RPC call returns)\n "
102
- " \n Note: this function can only be used on the regtest network\n "
100
+ " generate numblocks ( maxtries )\n "
101
+ " \n Mine up to numblocks blocks immediately (before the RPC call returns)\n "
103
102
" \n Arguments:\n "
104
103
" 1. numblocks (numeric, required) How many blocks are generated immediately.\n "
104
+ " 2. maxtries (numeric, optional) How many iterations to try (default = 1000000).\n "
105
105
" \n Result\n "
106
106
" [ blockhashes ] (array) hashes of blocks generated\n "
107
107
" \n Examples:\n "
108
108
" \n Generate 11 blocks\n "
109
109
+ HelpExampleCli (" generate" , " 11" )
110
110
);
111
111
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 ;
115
113
int nHeightStart = 0 ;
116
114
int nHeightEnd = 0 ;
117
115
int nHeight = 0 ;
118
116
int nGenerate = params[0 ].get_int ();
117
+ uint64_t nMaxTries = 1000000 ;
118
+ if (params.size () > 1 ) {
119
+ nMaxTries = params[1 ].get_int ();
120
+ }
119
121
120
122
boost::shared_ptr<CReserveScript> coinbaseScript;
121
123
GetMainSignals ().ScriptForMining (coinbaseScript);
@@ -146,10 +148,15 @@ UniValue generate(const UniValue& params, bool fHelp)
146
148
LOCK (cs_main);
147
149
IncrementExtraNonce (pblock, chainActive.Tip (), nExtraNonce);
148
150
}
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 ())) {
152
152
++pblock->nNonce ;
153
+ --nMaxTries;
154
+ }
155
+ if (nMaxTries == 0 ) {
156
+ break ;
157
+ }
158
+ if (pblock->nNonce == nInnerLoopCount) {
159
+ continue ;
153
160
}
154
161
CValidationState state;
155
162
if (!ProcessNewBlock (state, Params (), NULL , pblock, true , NULL ))
0 commit comments