Skip to content

Commit faaa46d

Browse files
author
MarcoFalke
committed
rpc: Assert that RPCArg names are equal to CRPCCommand ones (mining)
1 parent fa93bc1 commit faaa46d

File tree

2 files changed

+74
-51
lines changed

2 files changed

+74
-51
lines changed

src/rpc/mining.cpp

Lines changed: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ static UniValue GetNetworkHashPS(int lookup, int height) {
8181
return workDiff.getdouble() / timeDiff;
8282
}
8383

84-
static UniValue getnetworkhashps(const JSONRPCRequest& request)
84+
static RPCHelpMan getnetworkhashps()
8585
{
86-
RPCHelpMan{"getnetworkhashps",
86+
return RPCHelpMan{"getnetworkhashps",
8787
"\nReturns the estimated network hashes per second based on the last n blocks.\n"
8888
"Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
8989
"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)
9797
HelpExampleCli("getnetworkhashps", "")
9898
+ HelpExampleRpc("getnetworkhashps", "")
9999
},
100-
}.Check(request);
101-
100+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
101+
{
102102
LOCK(cs_main);
103103
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1);
104+
},
105+
};
104106
}
105107

106108
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
200202
}
201203
}
202204

203-
static UniValue generatetodescriptor(const JSONRPCRequest& request)
205+
static RPCHelpMan generatetodescriptor()
204206
{
205-
RPCHelpMan{
207+
return RPCHelpMan{
206208
"generatetodescriptor",
207209
"\nMine blocks immediately to a specified descriptor (before the RPC call returns)\n",
208210
{
@@ -218,9 +220,8 @@ static UniValue generatetodescriptor(const JSONRPCRequest& request)
218220
},
219221
RPCExamples{
220222
"\nGenerate 11 blocks to mydesc\n" + HelpExampleCli("generatetodescriptor", "11 \"mydesc\"")},
221-
}
222-
.Check(request);
223-
223+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
224+
{
224225
const int num_blocks{request.params[0].get_int()};
225226
const uint64_t max_tries{request.params[2].isNull() ? DEFAULT_MAX_TRIES : request.params[2].get_int()};
226227

@@ -234,22 +235,25 @@ static UniValue generatetodescriptor(const JSONRPCRequest& request)
234235
ChainstateManager& chainman = EnsureChainman(request.context);
235236

236237
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries);
238+
},
239+
};
237240
}
238241

239-
static UniValue generate(const JSONRPCRequest& request)
242+
static RPCHelpMan generate()
240243
{
241-
const std::string help_str{"generate ( nblocks maxtries ) has been replaced by the -generate cli option. Refer to -help for more information."};
244+
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 {
242245

243246
if (request.fHelp) {
244-
throw std::runtime_error(help_str);
247+
throw std::runtime_error(self.ToString());
245248
} else {
246-
throw JSONRPCError(RPC_METHOD_NOT_FOUND, help_str);
249+
throw JSONRPCError(RPC_METHOD_NOT_FOUND, self.ToString());
247250
}
251+
}};
248252
}
249253

250-
static UniValue generatetoaddress(const JSONRPCRequest& request)
254+
static RPCHelpMan generatetoaddress()
251255
{
252-
RPCHelpMan{"generatetoaddress",
256+
return RPCHelpMan{"generatetoaddress",
253257
"\nMine blocks immediately to a specified address (before the RPC call returns)\n",
254258
{
255259
{"nblocks", RPCArg::Type::NUM, RPCArg::Optional::NO, "How many blocks are generated immediately."},
@@ -267,8 +271,8 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
267271
+ "If you are using the " PACKAGE_NAME " wallet, you can get a new address to send the newly generated bitcoin to with:\n"
268272
+ HelpExampleCli("getnewaddress", "")
269273
},
270-
}.Check(request);
271-
274+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
275+
{
272276
const int num_blocks{request.params[0].get_int()};
273277
const uint64_t max_tries{request.params[2].isNull() ? DEFAULT_MAX_TRIES : request.params[2].get_int()};
274278

@@ -283,11 +287,13 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
283287
CScript coinbase_script = GetScriptForDestination(destination);
284288

285289
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries);
290+
},
291+
};
286292
}
287293

288-
static UniValue generateblock(const JSONRPCRequest& request)
294+
static RPCHelpMan generateblock()
289295
{
290-
RPCHelpMan{"generateblock",
296+
return RPCHelpMan{"generateblock",
291297
"\nMine a block with a set of ordered transactions immediately to a specified address or descriptor (before the RPC call returns)\n",
292298
{
293299
{"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)
309315
"\nGenerate a block to myaddress, with txs rawtx and mempool_txid\n"
310316
+ HelpExampleCli("generateblock", R"("myaddress" '["rawtx", "mempool_txid"]')")
311317
},
312-
}.Check(request);
313-
318+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
319+
{
314320
const auto address_or_descriptor = request.params[0].get_str();
315321
CScript coinbase_script;
316322
std::string error;
@@ -390,11 +396,13 @@ static UniValue generateblock(const JSONRPCRequest& request)
390396
UniValue obj(UniValue::VOBJ);
391397
obj.pushKV("hash", block_hash.GetHex());
392398
return obj;
399+
},
400+
};
393401
}
394402

395-
static UniValue getmininginfo(const JSONRPCRequest& request)
403+
static RPCHelpMan getmininginfo()
396404
{
397-
RPCHelpMan{"getmininginfo",
405+
return RPCHelpMan{"getmininginfo",
398406
"\nReturns a json object containing mining-related information.",
399407
{},
400408
RPCResult{
@@ -413,8 +421,8 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
413421
HelpExampleCli("getmininginfo", "")
414422
+ HelpExampleRpc("getmininginfo", "")
415423
},
416-
}.Check(request);
417-
424+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
425+
{
418426
LOCK(cs_main);
419427
const CTxMemPool& mempool = EnsureMemPool(request.context);
420428

@@ -423,18 +431,20 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
423431
if (BlockAssembler::m_last_block_weight) obj.pushKV("currentblockweight", *BlockAssembler::m_last_block_weight);
424432
if (BlockAssembler::m_last_block_num_txs) obj.pushKV("currentblocktx", *BlockAssembler::m_last_block_num_txs);
425433
obj.pushKV("difficulty", (double)GetDifficulty(::ChainActive().Tip()));
426-
obj.pushKV("networkhashps", getnetworkhashps(request));
434+
obj.pushKV("networkhashps", getnetworkhashps().HandleRequest(request));
427435
obj.pushKV("pooledtx", (uint64_t)mempool.size());
428436
obj.pushKV("chain", Params().NetworkIDString());
429437
obj.pushKV("warnings", GetWarnings(false).original);
430438
return obj;
439+
},
440+
};
431441
}
432442

433443

434444
// NOTE: Unlike wallet RPC (which use BTC values), mining RPCs follow GBT (BIP 22) in using satoshi amounts
435-
static UniValue prioritisetransaction(const JSONRPCRequest& request)
445+
static RPCHelpMan prioritisetransaction()
436446
{
437-
RPCHelpMan{"prioritisetransaction",
447+
return RPCHelpMan{"prioritisetransaction",
438448
"Accepts the transaction into mined blocks at a higher (or lower) priority\n",
439449
{
440450
{"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id."},
@@ -451,8 +461,8 @@ static UniValue prioritisetransaction(const JSONRPCRequest& request)
451461
HelpExampleCli("prioritisetransaction", "\"txid\" 0.0 10000")
452462
+ HelpExampleRpc("prioritisetransaction", "\"txid\", 0.0, 10000")
453463
},
454-
}.Check(request);
455-
464+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
465+
{
456466
LOCK(cs_main);
457467

458468
uint256 hash(ParseHashV(request.params[0], "txid"));
@@ -464,6 +474,8 @@ static UniValue prioritisetransaction(const JSONRPCRequest& request)
464474

465475
EnsureMemPool(request.context).PrioritiseTransaction(hash, nAmount);
466476
return true;
477+
},
478+
};
467479
}
468480

469481

@@ -495,9 +507,9 @@ static std::string gbt_vb_name(const Consensus::DeploymentPos pos) {
495507
return s;
496508
}
497509

498-
static UniValue getblocktemplate(const JSONRPCRequest& request)
510+
static RPCHelpMan getblocktemplate()
499511
{
500-
RPCHelpMan{"getblocktemplate",
512+
return RPCHelpMan{"getblocktemplate",
501513
"\nIf the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'.\n"
502514
"It returns data needed to construct a block to work on.\n"
503515
"For full specification, see BIPs 22, 23, 9, and 145:\n"
@@ -579,8 +591,8 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
579591
HelpExampleCli("getblocktemplate", "'{\"rules\": [\"segwit\"]}'")
580592
+ HelpExampleRpc("getblocktemplate", "{\"rules\": [\"segwit\"]}")
581593
},
582-
}.Check(request);
583-
594+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
595+
{
584596
LOCK(cs_main);
585597

586598
std::string strMode = "template";
@@ -888,6 +900,8 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
888900
}
889901

890902
return result;
903+
},
904+
};
891905
}
892906

893907
class submitblock_StateCatcher final : public CValidationInterface
@@ -908,10 +922,10 @@ class submitblock_StateCatcher final : public CValidationInterface
908922
}
909923
};
910924

911-
static UniValue submitblock(const JSONRPCRequest& request)
925+
static RPCHelpMan submitblock()
912926
{
913927
// We allow 2 arguments for compliance with BIP22. Argument 2 is ignored.
914-
RPCHelpMan{"submitblock",
928+
return RPCHelpMan{"submitblock",
915929
"\nAttempts to submit new block to network.\n"
916930
"See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n",
917931
{
@@ -923,8 +937,8 @@ static UniValue submitblock(const JSONRPCRequest& request)
923937
HelpExampleCli("submitblock", "\"mydata\"")
924938
+ HelpExampleRpc("submitblock", "\"mydata\"")
925939
},
926-
}.Check(request);
927-
940+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
941+
{
928942
std::shared_ptr<CBlock> blockptr = std::make_shared<CBlock>();
929943
CBlock& block = *blockptr;
930944
if (!DecodeHexBlk(block, request.params[0].get_str())) {
@@ -969,11 +983,13 @@ static UniValue submitblock(const JSONRPCRequest& request)
969983
return "inconclusive";
970984
}
971985
return BIP22ValidationResult(sc->state);
986+
},
987+
};
972988
}
973989

974-
static UniValue submitheader(const JSONRPCRequest& request)
990+
static RPCHelpMan submitheader()
975991
{
976-
RPCHelpMan{"submitheader",
992+
return RPCHelpMan{"submitheader",
977993
"\nDecode the given hexdata as a header and submit it as a candidate chain tip if valid."
978994
"\nThrows when the header is invalid.\n",
979995
{
@@ -985,8 +1001,8 @@ static UniValue submitheader(const JSONRPCRequest& request)
9851001
HelpExampleCli("submitheader", "\"aabbcc\"") +
9861002
HelpExampleRpc("submitheader", "\"aabbcc\"")
9871003
},
988-
}.Check(request);
989-
1004+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1005+
{
9901006
CBlockHeader h;
9911007
if (!DecodeHexBlockHeader(h, request.params[0].get_str())) {
9921008
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block header decode failed");
@@ -1005,11 +1021,13 @@ static UniValue submitheader(const JSONRPCRequest& request)
10051021
throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString());
10061022
}
10071023
throw JSONRPCError(RPC_VERIFY_ERROR, state.GetRejectReason());
1024+
},
1025+
};
10081026
}
10091027

1010-
static UniValue estimatesmartfee(const JSONRPCRequest& request)
1028+
static RPCHelpMan estimatesmartfee()
10111029
{
1012-
RPCHelpMan{"estimatesmartfee",
1030+
return RPCHelpMan{"estimatesmartfee",
10131031
"\nEstimates the approximate fee per kilobyte needed for a transaction to begin\n"
10141032
"confirmation within conf_target blocks if possible and return the number of blocks\n"
10151033
"for which the estimate is valid. Uses virtual transaction size as defined\n"
@@ -1043,8 +1061,8 @@ static UniValue estimatesmartfee(const JSONRPCRequest& request)
10431061
RPCExamples{
10441062
HelpExampleCli("estimatesmartfee", "6")
10451063
},
1046-
}.Check(request);
1047-
1064+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1065+
{
10481066
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VSTR});
10491067
RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
10501068
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
@@ -1070,11 +1088,13 @@ static UniValue estimatesmartfee(const JSONRPCRequest& request)
10701088
}
10711089
result.pushKV("blocks", feeCalc.returnedTarget);
10721090
return result;
1091+
},
1092+
};
10731093
}
10741094

1075-
static UniValue estimaterawfee(const JSONRPCRequest& request)
1095+
static RPCHelpMan estimaterawfee()
10761096
{
1077-
RPCHelpMan{"estimaterawfee",
1097+
return RPCHelpMan{"estimaterawfee",
10781098
"\nWARNING: This interface is unstable and may disappear or change!\n"
10791099
"\nWARNING: This is an advanced API call that is tightly coupled to the specific\n"
10801100
" implementation of fee estimation. The parameters it can be called with\n"
@@ -1126,8 +1146,8 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)
11261146
RPCExamples{
11271147
HelpExampleCli("estimaterawfee", "6 0.9")
11281148
},
1129-
}.Check(request);
1130-
1149+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1150+
{
11311151
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VNUM}, true);
11321152
RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
11331153
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
@@ -1186,6 +1206,8 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)
11861206
result.pushKV(StringForFeeEstimateHorizon(horizon), horizon_result);
11871207
}
11881208
return result;
1209+
},
1210+
};
11891211
}
11901212

11911213
void RegisterMiningRPCCommands(CRPCTable &t)

test/functional/rpc_generate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def set_test_params(self):
1717

1818
def run_test(self):
1919
message = (
20-
"generate ( nblocks maxtries ) has been replaced by the -generate "
20+
"generate\n"
21+
"has been replaced by the -generate "
2122
"cli option. Refer to -help for more information."
2223
)
2324

0 commit comments

Comments
 (0)