Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/mining.cpp
// Copyright (c) 2010 Satoshi Nakamoto | // Copyright (c) 2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "rpc/mining.h" | |||||
#include "amount.h" | #include "amount.h" | ||||
#include "chain.h" | #include "chain.h" | ||||
#include "chainparams.h" | #include "chainparams.h" | ||||
#include "config.h" | #include "config.h" | ||||
#include "consensus/consensus.h" | #include "consensus/consensus.h" | ||||
#include "consensus/params.h" | #include "consensus/params.h" | ||||
#include "consensus/validation.h" | #include "consensus/validation.h" | ||||
#include "core_io.h" | #include "core_io.h" | ||||
▲ Show 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | static UniValue getnetworkhashps(const Config &config, | ||||
} | } | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
return GetNetworkHashPS( | return GetNetworkHashPS( | ||||
request.params.size() > 0 ? request.params[0].get_int() : 120, | request.params.size() > 0 ? request.params[0].get_int() : 120, | ||||
request.params.size() > 1 ? request.params[1].get_int() : -1); | request.params.size() > 1 ? request.params[1].get_int() : -1); | ||||
} | } | ||||
static UniValue generateBlocks(const Config &config, | UniValue generateBlocks(const Config &config, | ||||
std::shared_ptr<CReserveScript> coinbaseScript, | std::shared_ptr<CReserveScript> coinbaseScript, | ||||
int nGenerate, uint64_t nMaxTries, | int nGenerate, uint64_t nMaxTries, bool keepScript) { | ||||
bool keepScript) { | |||||
static const int nInnerLoopCount = 0x100000; | static const int nInnerLoopCount = 0x100000; | ||||
int nHeightStart = 0; | int nHeightStart = 0; | ||||
int nHeightEnd = 0; | int nHeightEnd = 0; | ||||
int nHeight = 0; | int nHeight = 0; | ||||
{ | { | ||||
// Don't keep cs_main locked. | // Don't keep cs_main locked. | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | while (nHeight < nHeightEnd) { | ||||
if (keepScript) { | if (keepScript) { | ||||
coinbaseScript->KeepScript(); | coinbaseScript->KeepScript(); | ||||
} | } | ||||
} | } | ||||
return blockHashes; | return blockHashes; | ||||
} | } | ||||
static UniValue generate(const Config &config, const JSONRPCRequest &request) { | |||||
if (request.fHelp || request.params.size() < 1 || | |||||
request.params.size() > 2) { | |||||
throw std::runtime_error( | |||||
"generate nblocks ( maxtries )\n" | |||||
"\nMine up to nblocks blocks immediately (before the RPC call " | |||||
"returns)\n" | |||||
"\nArguments:\n" | |||||
"1. nblocks (numeric, required) How many blocks are generated " | |||||
"immediately.\n" | |||||
"2. maxtries (numeric, optional) How many iterations to try " | |||||
"(default = 1000000).\n" | |||||
"\nResult:\n" | |||||
"[ blockhashes ] (array) hashes of blocks generated\n" | |||||
"\nExamples:\n" | |||||
"\nGenerate 11 blocks\n" + | |||||
HelpExampleCli("generate", "11")); | |||||
} | |||||
int nGenerate = request.params[0].get_int(); | |||||
uint64_t nMaxTries = 1000000; | |||||
if (request.params.size() > 1) { | |||||
nMaxTries = request.params[1].get_int(); | |||||
} | |||||
std::shared_ptr<CReserveScript> coinbaseScript; | |||||
GetMainSignals().ScriptForMining(coinbaseScript); | |||||
// If the keypool is exhausted, no script is returned at all. Catch this. | |||||
if (!coinbaseScript) { | |||||
throw JSONRPCError( | |||||
RPC_WALLET_KEYPOOL_RAN_OUT, | |||||
"Error: Keypool ran out, please call keypoolrefill first"); | |||||
} | |||||
// Throw an error if no script was provided. | |||||
if (coinbaseScript->reserveScript.empty()) { | |||||
throw JSONRPCError( | |||||
RPC_INTERNAL_ERROR, | |||||
"No coinbase script available (mining requires a wallet)"); | |||||
} | |||||
return generateBlocks(config, coinbaseScript, nGenerate, nMaxTries, true); | |||||
} | |||||
static UniValue generatetoaddress(const Config &config, | static UniValue generatetoaddress(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
if (request.fHelp || request.params.size() < 2 || | if (request.fHelp || request.params.size() < 2 || | ||||
request.params.size() > 3) { | request.params.size() > 3) { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
"generatetoaddress nblocks address (maxtries)\n" | "generatetoaddress nblocks address (maxtries)\n" | ||||
"\nMine blocks immediately to a specified address (before the RPC " | "\nMine blocks immediately to a specified address (before the RPC " | ||||
"call returns)\n" | "call returns)\n" | ||||
▲ Show 20 Lines • Show All 854 Lines • ▼ Show 20 Lines | static const CRPCCommand commands[] = { | ||||
// category name actor (function) okSafeMode | // category name actor (function) okSafeMode | ||||
// ---------- ------------------------ ---------------------- ---------- | // ---------- ------------------------ ---------------------- ---------- | ||||
{"mining", "getnetworkhashps", getnetworkhashps, true, {"nblocks", "height"}}, | {"mining", "getnetworkhashps", getnetworkhashps, true, {"nblocks", "height"}}, | ||||
{"mining", "getmininginfo", getmininginfo, true, {}}, | {"mining", "getmininginfo", getmininginfo, true, {}}, | ||||
{"mining", "prioritisetransaction", prioritisetransaction, true, {"txid", "priority_delta", "fee_delta"}}, | {"mining", "prioritisetransaction", prioritisetransaction, true, {"txid", "priority_delta", "fee_delta"}}, | ||||
{"mining", "getblocktemplate", getblocktemplate, true, {"template_request"}}, | {"mining", "getblocktemplate", getblocktemplate, true, {"template_request"}}, | ||||
{"mining", "submitblock", submitblock, true, {"hexdata", "parameters"}}, | {"mining", "submitblock", submitblock, true, {"hexdata", "parameters"}}, | ||||
{"generating", "generate", generate, true, {"nblocks", "maxtries"}}, | |||||
{"generating", "generatetoaddress", generatetoaddress, true, {"nblocks", "address", "maxtries"}}, | {"generating", "generatetoaddress", generatetoaddress, true, {"nblocks", "address", "maxtries"}}, | ||||
{"util", "estimatefee", estimatefee, true, {"nblocks"}}, | {"util", "estimatefee", estimatefee, true, {"nblocks"}}, | ||||
{"util", "estimatepriority", estimatepriority, true, {"nblocks"}}, | {"util", "estimatepriority", estimatepriority, true, {"nblocks"}}, | ||||
{"util", "estimatesmartfee", estimatesmartfee, true, {"nblocks"}}, | {"util", "estimatesmartfee", estimatesmartfee, true, {"nblocks"}}, | ||||
{"util", "estimatesmartpriority", estimatesmartpriority, true, {"nblocks"}}, | {"util", "estimatesmartpriority", estimatesmartpriority, true, {"nblocks"}}, | ||||
}; | }; | ||||
// clang-format on | // clang-format on | ||||
void RegisterMiningRPCCommands(CRPCTable &t) { | void RegisterMiningRPCCommands(CRPCTable &t) { | ||||
for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) | for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) | ||||
t.appendCommand(commands[vcidx].name, &commands[vcidx]); | t.appendCommand(commands[vcidx].name, &commands[vcidx]); | ||||
} | } |