rpc: Assert that RPCArg names are equal to CRPCCommand ones (mining)

This commit is contained in:
MarcoFalke 2020-08-14 11:27:44 +02:00
parent fa93bc14c7
commit faaa46dc20
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
2 changed files with 74 additions and 51 deletions

View file

@ -81,9 +81,9 @@ static UniValue GetNetworkHashPS(int lookup, int height) {
return workDiff.getdouble() / timeDiff; return workDiff.getdouble() / timeDiff;
} }
static UniValue getnetworkhashps(const JSONRPCRequest& request) static RPCHelpMan getnetworkhashps()
{ {
RPCHelpMan{"getnetworkhashps", return RPCHelpMan{"getnetworkhashps",
"\nReturns the estimated network hashes per second based on the last n blocks.\n" "\nReturns the estimated network hashes per second based on the last n blocks.\n"
"Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n" "Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
"Pass in [height] to estimate the network speed at the time when a certain block was found.\n", "Pass in [height] to estimate the network speed at the time when a certain block was found.\n",
@ -97,10 +97,12 @@ static UniValue getnetworkhashps(const JSONRPCRequest& request)
HelpExampleCli("getnetworkhashps", "") HelpExampleCli("getnetworkhashps", "")
+ HelpExampleRpc("getnetworkhashps", "") + HelpExampleRpc("getnetworkhashps", "")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
LOCK(cs_main); LOCK(cs_main);
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1); return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1);
},
};
} }
static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, unsigned int& extra_nonce, uint256& block_hash) static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, unsigned int& extra_nonce, uint256& block_hash)
@ -200,9 +202,9 @@ static bool getScriptFromDescriptor(const std::string& descriptor, CScript& scri
} }
} }
static UniValue generatetodescriptor(const JSONRPCRequest& request) static RPCHelpMan generatetodescriptor()
{ {
RPCHelpMan{ return RPCHelpMan{
"generatetodescriptor", "generatetodescriptor",
"\nMine blocks immediately to a specified descriptor (before the RPC call returns)\n", "\nMine blocks immediately to a specified descriptor (before the RPC call returns)\n",
{ {
@ -218,9 +220,8 @@ static UniValue generatetodescriptor(const JSONRPCRequest& request)
}, },
RPCExamples{ RPCExamples{
"\nGenerate 11 blocks to mydesc\n" + HelpExampleCli("generatetodescriptor", "11 \"mydesc\"")}, "\nGenerate 11 blocks to mydesc\n" + HelpExampleCli("generatetodescriptor", "11 \"mydesc\"")},
} [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
.Check(request); {
const int num_blocks{request.params[0].get_int()}; const int num_blocks{request.params[0].get_int()};
const uint64_t max_tries{request.params[2].isNull() ? DEFAULT_MAX_TRIES : request.params[2].get_int()}; const uint64_t max_tries{request.params[2].isNull() ? DEFAULT_MAX_TRIES : request.params[2].get_int()};
@ -234,22 +235,25 @@ static UniValue generatetodescriptor(const JSONRPCRequest& request)
ChainstateManager& chainman = EnsureChainman(request.context); ChainstateManager& chainman = EnsureChainman(request.context);
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries); return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries);
},
};
} }
static UniValue generate(const JSONRPCRequest& request) static RPCHelpMan generate()
{ {
const std::string help_str{"generate ( nblocks maxtries ) has been replaced by the -generate cli option. Refer to -help for more information."}; return RPCHelpMan{"generate", "has been replaced by the -generate cli option. Refer to -help for more information.", {}, {}, RPCExamples{""}, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
if (request.fHelp) { if (request.fHelp) {
throw std::runtime_error(help_str); throw std::runtime_error(self.ToString());
} else { } else {
throw JSONRPCError(RPC_METHOD_NOT_FOUND, help_str); throw JSONRPCError(RPC_METHOD_NOT_FOUND, self.ToString());
} }
}};
} }
static UniValue generatetoaddress(const JSONRPCRequest& request) static RPCHelpMan generatetoaddress()
{ {
RPCHelpMan{"generatetoaddress", return RPCHelpMan{"generatetoaddress",
"\nMine blocks immediately to a specified address (before the RPC call returns)\n", "\nMine blocks immediately to a specified address (before the RPC call returns)\n",
{ {
{"nblocks", RPCArg::Type::NUM, RPCArg::Optional::NO, "How many blocks are generated immediately."}, {"nblocks", RPCArg::Type::NUM, RPCArg::Optional::NO, "How many blocks are generated immediately."},
@ -267,8 +271,8 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
+ "If you are using the " PACKAGE_NAME " wallet, you can get a new address to send the newly generated bitcoin to with:\n" + "If you are using the " PACKAGE_NAME " wallet, you can get a new address to send the newly generated bitcoin to with:\n"
+ HelpExampleCli("getnewaddress", "") + HelpExampleCli("getnewaddress", "")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
const int num_blocks{request.params[0].get_int()}; const int num_blocks{request.params[0].get_int()};
const uint64_t max_tries{request.params[2].isNull() ? DEFAULT_MAX_TRIES : request.params[2].get_int()}; const uint64_t max_tries{request.params[2].isNull() ? DEFAULT_MAX_TRIES : request.params[2].get_int()};
@ -283,11 +287,13 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
CScript coinbase_script = GetScriptForDestination(destination); CScript coinbase_script = GetScriptForDestination(destination);
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries); return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries);
},
};
} }
static UniValue generateblock(const JSONRPCRequest& request) static RPCHelpMan generateblock()
{ {
RPCHelpMan{"generateblock", return RPCHelpMan{"generateblock",
"\nMine a block with a set of ordered transactions immediately to a specified address or descriptor (before the RPC call returns)\n", "\nMine a block with a set of ordered transactions immediately to a specified address or descriptor (before the RPC call returns)\n",
{ {
{"output", RPCArg::Type::STR, RPCArg::Optional::NO, "The address or descriptor to send the newly generated bitcoin to."}, {"output", RPCArg::Type::STR, RPCArg::Optional::NO, "The address or descriptor to send the newly generated bitcoin to."},
@ -309,8 +315,8 @@ static UniValue generateblock(const JSONRPCRequest& request)
"\nGenerate a block to myaddress, with txs rawtx and mempool_txid\n" "\nGenerate a block to myaddress, with txs rawtx and mempool_txid\n"
+ HelpExampleCli("generateblock", R"("myaddress" '["rawtx", "mempool_txid"]')") + HelpExampleCli("generateblock", R"("myaddress" '["rawtx", "mempool_txid"]')")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
const auto address_or_descriptor = request.params[0].get_str(); const auto address_or_descriptor = request.params[0].get_str();
CScript coinbase_script; CScript coinbase_script;
std::string error; std::string error;
@ -390,11 +396,13 @@ static UniValue generateblock(const JSONRPCRequest& request)
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
obj.pushKV("hash", block_hash.GetHex()); obj.pushKV("hash", block_hash.GetHex());
return obj; return obj;
},
};
} }
static UniValue getmininginfo(const JSONRPCRequest& request) static RPCHelpMan getmininginfo()
{ {
RPCHelpMan{"getmininginfo", return RPCHelpMan{"getmininginfo",
"\nReturns a json object containing mining-related information.", "\nReturns a json object containing mining-related information.",
{}, {},
RPCResult{ RPCResult{
@ -413,8 +421,8 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
HelpExampleCli("getmininginfo", "") HelpExampleCli("getmininginfo", "")
+ HelpExampleRpc("getmininginfo", "") + HelpExampleRpc("getmininginfo", "")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
LOCK(cs_main); LOCK(cs_main);
const CTxMemPool& mempool = EnsureMemPool(request.context); const CTxMemPool& mempool = EnsureMemPool(request.context);
@ -423,18 +431,20 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
if (BlockAssembler::m_last_block_weight) obj.pushKV("currentblockweight", *BlockAssembler::m_last_block_weight); if (BlockAssembler::m_last_block_weight) obj.pushKV("currentblockweight", *BlockAssembler::m_last_block_weight);
if (BlockAssembler::m_last_block_num_txs) obj.pushKV("currentblocktx", *BlockAssembler::m_last_block_num_txs); if (BlockAssembler::m_last_block_num_txs) obj.pushKV("currentblocktx", *BlockAssembler::m_last_block_num_txs);
obj.pushKV("difficulty", (double)GetDifficulty(::ChainActive().Tip())); obj.pushKV("difficulty", (double)GetDifficulty(::ChainActive().Tip()));
obj.pushKV("networkhashps", getnetworkhashps(request)); obj.pushKV("networkhashps", getnetworkhashps().HandleRequest(request));
obj.pushKV("pooledtx", (uint64_t)mempool.size()); obj.pushKV("pooledtx", (uint64_t)mempool.size());
obj.pushKV("chain", Params().NetworkIDString()); obj.pushKV("chain", Params().NetworkIDString());
obj.pushKV("warnings", GetWarnings(false).original); obj.pushKV("warnings", GetWarnings(false).original);
return obj; return obj;
},
};
} }
// NOTE: Unlike wallet RPC (which use BTC values), mining RPCs follow GBT (BIP 22) in using satoshi amounts // NOTE: Unlike wallet RPC (which use BTC values), mining RPCs follow GBT (BIP 22) in using satoshi amounts
static UniValue prioritisetransaction(const JSONRPCRequest& request) static RPCHelpMan prioritisetransaction()
{ {
RPCHelpMan{"prioritisetransaction", return RPCHelpMan{"prioritisetransaction",
"Accepts the transaction into mined blocks at a higher (or lower) priority\n", "Accepts the transaction into mined blocks at a higher (or lower) priority\n",
{ {
{"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id."}, {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id."},
@ -451,8 +461,8 @@ static UniValue prioritisetransaction(const JSONRPCRequest& request)
HelpExampleCli("prioritisetransaction", "\"txid\" 0.0 10000") HelpExampleCli("prioritisetransaction", "\"txid\" 0.0 10000")
+ HelpExampleRpc("prioritisetransaction", "\"txid\", 0.0, 10000") + HelpExampleRpc("prioritisetransaction", "\"txid\", 0.0, 10000")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
LOCK(cs_main); LOCK(cs_main);
uint256 hash(ParseHashV(request.params[0], "txid")); uint256 hash(ParseHashV(request.params[0], "txid"));
@ -464,6 +474,8 @@ static UniValue prioritisetransaction(const JSONRPCRequest& request)
EnsureMemPool(request.context).PrioritiseTransaction(hash, nAmount); EnsureMemPool(request.context).PrioritiseTransaction(hash, nAmount);
return true; return true;
},
};
} }
@ -495,9 +507,9 @@ static std::string gbt_vb_name(const Consensus::DeploymentPos pos) {
return s; return s;
} }
static UniValue getblocktemplate(const JSONRPCRequest& request) static RPCHelpMan getblocktemplate()
{ {
RPCHelpMan{"getblocktemplate", return RPCHelpMan{"getblocktemplate",
"\nIf the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'.\n" "\nIf the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'.\n"
"It returns data needed to construct a block to work on.\n" "It returns data needed to construct a block to work on.\n"
"For full specification, see BIPs 22, 23, 9, and 145:\n" "For full specification, see BIPs 22, 23, 9, and 145:\n"
@ -579,8 +591,8 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
HelpExampleCli("getblocktemplate", "'{\"rules\": [\"segwit\"]}'") HelpExampleCli("getblocktemplate", "'{\"rules\": [\"segwit\"]}'")
+ HelpExampleRpc("getblocktemplate", "{\"rules\": [\"segwit\"]}") + HelpExampleRpc("getblocktemplate", "{\"rules\": [\"segwit\"]}")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
LOCK(cs_main); LOCK(cs_main);
std::string strMode = "template"; std::string strMode = "template";
@ -888,6 +900,8 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
} }
return result; return result;
},
};
} }
class submitblock_StateCatcher final : public CValidationInterface class submitblock_StateCatcher final : public CValidationInterface
@ -908,10 +922,10 @@ protected:
} }
}; };
static UniValue submitblock(const JSONRPCRequest& request) static RPCHelpMan submitblock()
{ {
// We allow 2 arguments for compliance with BIP22. Argument 2 is ignored. // We allow 2 arguments for compliance with BIP22. Argument 2 is ignored.
RPCHelpMan{"submitblock", return RPCHelpMan{"submitblock",
"\nAttempts to submit new block to network.\n" "\nAttempts to submit new block to network.\n"
"See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n", "See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n",
{ {
@ -923,8 +937,8 @@ static UniValue submitblock(const JSONRPCRequest& request)
HelpExampleCli("submitblock", "\"mydata\"") HelpExampleCli("submitblock", "\"mydata\"")
+ HelpExampleRpc("submitblock", "\"mydata\"") + HelpExampleRpc("submitblock", "\"mydata\"")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::shared_ptr<CBlock> blockptr = std::make_shared<CBlock>(); std::shared_ptr<CBlock> blockptr = std::make_shared<CBlock>();
CBlock& block = *blockptr; CBlock& block = *blockptr;
if (!DecodeHexBlk(block, request.params[0].get_str())) { if (!DecodeHexBlk(block, request.params[0].get_str())) {
@ -969,11 +983,13 @@ static UniValue submitblock(const JSONRPCRequest& request)
return "inconclusive"; return "inconclusive";
} }
return BIP22ValidationResult(sc->state); return BIP22ValidationResult(sc->state);
},
};
} }
static UniValue submitheader(const JSONRPCRequest& request) static RPCHelpMan submitheader()
{ {
RPCHelpMan{"submitheader", return RPCHelpMan{"submitheader",
"\nDecode the given hexdata as a header and submit it as a candidate chain tip if valid." "\nDecode the given hexdata as a header and submit it as a candidate chain tip if valid."
"\nThrows when the header is invalid.\n", "\nThrows when the header is invalid.\n",
{ {
@ -985,8 +1001,8 @@ static UniValue submitheader(const JSONRPCRequest& request)
HelpExampleCli("submitheader", "\"aabbcc\"") + HelpExampleCli("submitheader", "\"aabbcc\"") +
HelpExampleRpc("submitheader", "\"aabbcc\"") HelpExampleRpc("submitheader", "\"aabbcc\"")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
CBlockHeader h; CBlockHeader h;
if (!DecodeHexBlockHeader(h, request.params[0].get_str())) { if (!DecodeHexBlockHeader(h, request.params[0].get_str())) {
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block header decode failed"); throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block header decode failed");
@ -1005,11 +1021,13 @@ static UniValue submitheader(const JSONRPCRequest& request)
throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString()); throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString());
} }
throw JSONRPCError(RPC_VERIFY_ERROR, state.GetRejectReason()); throw JSONRPCError(RPC_VERIFY_ERROR, state.GetRejectReason());
},
};
} }
static UniValue estimatesmartfee(const JSONRPCRequest& request) static RPCHelpMan estimatesmartfee()
{ {
RPCHelpMan{"estimatesmartfee", return RPCHelpMan{"estimatesmartfee",
"\nEstimates the approximate fee per kilobyte needed for a transaction to begin\n" "\nEstimates the approximate fee per kilobyte needed for a transaction to begin\n"
"confirmation within conf_target blocks if possible and return the number of blocks\n" "confirmation within conf_target blocks if possible and return the number of blocks\n"
"for which the estimate is valid. Uses virtual transaction size as defined\n" "for which the estimate is valid. Uses virtual transaction size as defined\n"
@ -1043,8 +1061,8 @@ static UniValue estimatesmartfee(const JSONRPCRequest& request)
RPCExamples{ RPCExamples{
HelpExampleCli("estimatesmartfee", "6") HelpExampleCli("estimatesmartfee", "6")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VSTR}); RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VSTR});
RPCTypeCheckArgument(request.params[0], UniValue::VNUM); RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE); unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
@ -1070,11 +1088,13 @@ static UniValue estimatesmartfee(const JSONRPCRequest& request)
} }
result.pushKV("blocks", feeCalc.returnedTarget); result.pushKV("blocks", feeCalc.returnedTarget);
return result; return result;
},
};
} }
static UniValue estimaterawfee(const JSONRPCRequest& request) static RPCHelpMan estimaterawfee()
{ {
RPCHelpMan{"estimaterawfee", return RPCHelpMan{"estimaterawfee",
"\nWARNING: This interface is unstable and may disappear or change!\n" "\nWARNING: This interface is unstable and may disappear or change!\n"
"\nWARNING: This is an advanced API call that is tightly coupled to the specific\n" "\nWARNING: This is an advanced API call that is tightly coupled to the specific\n"
" implementation of fee estimation. The parameters it can be called with\n" " implementation of fee estimation. The parameters it can be called with\n"
@ -1126,8 +1146,8 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)
RPCExamples{ RPCExamples{
HelpExampleCli("estimaterawfee", "6 0.9") HelpExampleCli("estimaterawfee", "6 0.9")
}, },
}.Check(request); [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VNUM}, true); RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VNUM}, true);
RPCTypeCheckArgument(request.params[0], UniValue::VNUM); RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE); unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
@ -1186,6 +1206,8 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)
result.pushKV(StringForFeeEstimateHorizon(horizon), horizon_result); result.pushKV(StringForFeeEstimateHorizon(horizon), horizon_result);
} }
return result; return result;
},
};
} }
void RegisterMiningRPCCommands(CRPCTable &t) void RegisterMiningRPCCommands(CRPCTable &t)

View file

@ -17,7 +17,8 @@ class RPCGenerateTest(BitcoinTestFramework):
def run_test(self): def run_test(self):
message = ( message = (
"generate ( nblocks maxtries ) has been replaced by the -generate " "generate\n"
"has been replaced by the -generate "
"cli option. Refer to -help for more information." "cli option. Refer to -help for more information."
) )