From 6b04508e37c5dd18cec1cd61cc4356bd208aa991 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 31 Mar 2015 20:28:28 -0700 Subject: Introduce separate 'generate' RPC call --- src/rpcmining.cpp | 91 +++++++++++++++++++++++++------------------------------ 1 file changed, 42 insertions(+), 49 deletions(-) (limited to 'src/rpcmining.cpp') diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index fcba7e222d..49c5c3ca58 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -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 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; } -- cgit v1.2.3