Skip to content

Commit f511ff3

Browse files
committed
refactor: move verbosity parsing to rpc/util
Provides a common way for rpcs to obtain verbosity from an rpc parameter
1 parent 532491f commit f511ff3

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

src/rpc/blockchain.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -766,14 +766,7 @@ static RPCHelpMan getblock()
766766
{
767767
uint256 hash(ParseHashV(request.params[0], "blockhash"));
768768

769-
int verbosity = 1;
770-
if (!request.params[1].isNull()) {
771-
if (request.params[1].isBool()) {
772-
verbosity = request.params[1].get_bool() ? 1 : 0;
773-
} else {
774-
verbosity = request.params[1].getInt<int>();
775-
}
776-
}
769+
int verbosity{ParseVerbosity(request.params[1], /*default_verbosity=*/1)};
777770

778771
const CBlockIndex* pblockindex;
779772
const CBlockIndex* tip;

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,7 @@ static RPCHelpMan getrawtransaction()
338338
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "The genesis block coinbase is not considered an ordinary transaction and cannot be retrieved");
339339
}
340340

341-
// Accept either a bool (true) or a num (>=0) to indicate verbosity.
342-
int verbosity{0};
343-
if (!request.params[1].isNull()) {
344-
if (request.params[1].isBool()) {
345-
verbosity = request.params[1].get_bool();
346-
} else {
347-
verbosity = request.params[1].getInt<int>();
348-
}
349-
}
341+
int verbosity{ParseVerbosity(request.params[1], /*default_verbosity=*/0)};
350342

351343
if (!request.params[2].isNull()) {
352344
LOCK(cs_main);

src/rpc/util.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ void RPCTypeCheckObj(const UniValue& o,
8181
}
8282
}
8383

84+
int ParseVerbosity(const UniValue& arg, int default_verbosity)
85+
{
86+
if (!arg.isNull()) {
87+
if (arg.isBool()) {
88+
return arg.get_bool(); // true = 1
89+
} else {
90+
return arg.getInt<int>();
91+
}
92+
}
93+
return default_verbosity;
94+
}
95+
8496
CAmount AmountFromValue(const UniValue& value, int decimals)
8597
{
8698
if (!value.isNum() && !value.isStr())

src/rpc/util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ uint256 ParseHashO(const UniValue& o, std::string_view strKey);
100100
std::vector<unsigned char> ParseHexV(const UniValue& v, std::string_view name);
101101
std::vector<unsigned char> ParseHexO(const UniValue& o, std::string_view strKey);
102102

103+
/**
104+
* Parses verbosity from provided UniValue.
105+
*
106+
* @param[in] arg The verbosity argument as a bool (true) or int (0, 1, 2,...)
107+
* @param[in] default_verbosity The value to return if verbosity argument is null
108+
* @returns An integer describing the verbosity level (e.g. 0, 1, 2, etc.)
109+
*/
110+
int ParseVerbosity(const UniValue& arg, int default_verbosity);
111+
103112
/**
104113
* Validate and return a CAmount from a UniValue number or string.
105114
*

0 commit comments

Comments
 (0)