refactor: move verbosity parsing to rpc/util

Provides a common way for rpcs to obtain
verbosity from an rpc parameter
This commit is contained in:
tdb3 2024-09-18 12:28:43 -04:00
parent 532491faf1
commit f511ff3654
No known key found for this signature in database
4 changed files with 23 additions and 17 deletions

View file

@ -766,14 +766,7 @@ static RPCHelpMan getblock()
{
uint256 hash(ParseHashV(request.params[0], "blockhash"));
int verbosity = 1;
if (!request.params[1].isNull()) {
if (request.params[1].isBool()) {
verbosity = request.params[1].get_bool() ? 1 : 0;
} else {
verbosity = request.params[1].getInt<int>();
}
}
int verbosity{ParseVerbosity(request.params[1], /*default_verbosity=*/1)};
const CBlockIndex* pblockindex;
const CBlockIndex* tip;

View file

@ -338,15 +338,7 @@ static RPCHelpMan getrawtransaction()
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "The genesis block coinbase is not considered an ordinary transaction and cannot be retrieved");
}
// Accept either a bool (true) or a num (>=0) to indicate verbosity.
int verbosity{0};
if (!request.params[1].isNull()) {
if (request.params[1].isBool()) {
verbosity = request.params[1].get_bool();
} else {
verbosity = request.params[1].getInt<int>();
}
}
int verbosity{ParseVerbosity(request.params[1], /*default_verbosity=*/0)};
if (!request.params[2].isNull()) {
LOCK(cs_main);

View file

@ -81,6 +81,18 @@ void RPCTypeCheckObj(const UniValue& o,
}
}
int ParseVerbosity(const UniValue& arg, int default_verbosity)
{
if (!arg.isNull()) {
if (arg.isBool()) {
return arg.get_bool(); // true = 1
} else {
return arg.getInt<int>();
}
}
return default_verbosity;
}
CAmount AmountFromValue(const UniValue& value, int decimals)
{
if (!value.isNum() && !value.isStr())

View file

@ -100,6 +100,15 @@ uint256 ParseHashO(const UniValue& o, std::string_view strKey);
std::vector<unsigned char> ParseHexV(const UniValue& v, std::string_view name);
std::vector<unsigned char> ParseHexO(const UniValue& o, std::string_view strKey);
/**
* Parses verbosity from provided UniValue.
*
* @param[in] arg The verbosity argument as a bool (true) or int (0, 1, 2,...)
* @param[in] default_verbosity The value to return if verbosity argument is null
* @returns An integer describing the verbosity level (e.g. 0, 1, 2, etc.)
*/
int ParseVerbosity(const UniValue& arg, int default_verbosity);
/**
* Validate and return a CAmount from a UniValue number or string.
*