Skip to content

Commit e0a70c5

Browse files
committed
Merge bitcoin/bitcoin#27605: refactor: Replace global find_value function with UniValue::find_value method
fa266c4 Temporarily work around gcc-13 warning bug in interfaces_tests (MarcoFalke) fa28850 Fix clang-tidy performance-unnecessary-copy-initialization warnings (MarcoFalke) faaa60a Remove unused find_value global function (MarcoFalke) fa422ae scripted-diff: Use UniValue::find_value method (MarcoFalke) fa548ac Add UniValue::find_value method (MarcoFalke) Pull request description: The global function has issues: * It causes gcc-13 warnings, see bitcoin/bitcoin#26926 * There is no rationale for it being a global function, when it acts like a member function * `performance-unnecessary-copy-initialization` clang-tidy isn't run on it Fix all issues by making it a member function. ACKs for top commit: achow101: ACK fa266c4 hebasto: re-ACK fa266c4 Tree-SHA512: 6c4e25da3122cd3b91c376bef73ea94fb3beb7bf8ef5cb3853c5128d95bfbacbcbfb16cc843eb7b1a7ebd350c2b6311f8085eeacf9aeeab3366987037d209e44
2 parents 104eed1 + fa266c4 commit e0a70c5

19 files changed

+86
-87
lines changed

src/bitcoin-cli.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& str
871871
try {
872872
response = CallRPC(rh, strMethod, args, rpcwallet);
873873
if (fWait) {
874-
const UniValue& error = find_value(response, "error");
874+
const UniValue& error = response.find_value("error");
875875
if (!error.isNull() && error["code"].getInt<int>() == RPC_IN_WARMUP) {
876876
throw CConnectionFailed("server in warmup");
877877
}
@@ -899,8 +899,8 @@ static void ParseResult(const UniValue& result, std::string& strPrint)
899899
static void ParseError(const UniValue& error, std::string& strPrint, int& nRet)
900900
{
901901
if (error.isObject()) {
902-
const UniValue& err_code = find_value(error, "code");
903-
const UniValue& err_msg = find_value(error, "message");
902+
const UniValue& err_code = error.find_value("code");
903+
const UniValue& err_msg = error.find_value("message");
904904
if (!err_code.isNull()) {
905905
strPrint = "error code: " + err_code.getValStr() + "\n";
906906
}
@@ -926,15 +926,15 @@ static void GetWalletBalances(UniValue& result)
926926
{
927927
DefaultRequestHandler rh;
928928
const UniValue listwallets = ConnectAndCallRPC(&rh, "listwallets", /* args=*/{});
929-
if (!find_value(listwallets, "error").isNull()) return;
930-
const UniValue& wallets = find_value(listwallets, "result");
929+
if (!listwallets.find_value("error").isNull()) return;
930+
const UniValue& wallets = listwallets.find_value("result");
931931
if (wallets.size() <= 1) return;
932932

933933
UniValue balances(UniValue::VOBJ);
934934
for (const UniValue& wallet : wallets.getValues()) {
935935
const std::string& wallet_name = wallet.get_str();
936936
const UniValue getbalances = ConnectAndCallRPC(&rh, "getbalances", /* args=*/{}, wallet_name);
937-
const UniValue& balance = find_value(getbalances, "result")["mine"]["trusted"];
937+
const UniValue& balance = getbalances.find_value("result")["mine"]["trusted"];
938938
balances.pushKV(wallet_name, balance);
939939
}
940940
result.pushKV("balances", balances);
@@ -970,7 +970,7 @@ static void GetProgressBar(double progress, std::string& progress_bar)
970970
*/
971971
static void ParseGetInfoResult(UniValue& result)
972972
{
973-
if (!find_value(result, "error").isNull()) return;
973+
if (!result.find_value("error").isNull()) return;
974974

975975
std::string RESET, GREEN, BLUE, YELLOW, MAGENTA, CYAN;
976976
bool should_colorize = false;
@@ -1182,9 +1182,9 @@ static int CommandLineRPC(int argc, char *argv[])
11821182
rh.reset(new NetinfoRequestHandler());
11831183
} else if (gArgs.GetBoolArg("-generate", false)) {
11841184
const UniValue getnewaddress{GetNewAddress()};
1185-
const UniValue& error{find_value(getnewaddress, "error")};
1185+
const UniValue& error{getnewaddress.find_value("error")};
11861186
if (error.isNull()) {
1187-
SetGenerateToAddressArgs(find_value(getnewaddress, "result").get_str(), args);
1187+
SetGenerateToAddressArgs(getnewaddress.find_value("result").get_str(), args);
11881188
rh.reset(new GenerateToAddressRequestHandler());
11891189
} else {
11901190
ParseError(error, strPrint, nRet);
@@ -1206,8 +1206,8 @@ static int CommandLineRPC(int argc, char *argv[])
12061206
const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name);
12071207

12081208
// Parse reply
1209-
UniValue result = find_value(reply, "result");
1210-
const UniValue& error = find_value(reply, "error");
1209+
UniValue result = reply.find_value("result");
1210+
const UniValue& error = reply.find_value("error");
12111211
if (error.isNull()) {
12121212
if (gArgs.GetBoolArg("-getinfo", false)) {
12131213
if (!gArgs.IsArgSet("-rpcwallet")) {

src/external_signer.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalS
3030
}
3131
for (const UniValue& signer : result.getValues()) {
3232
// Check for error
33-
const UniValue& error = find_value(signer, "error");
33+
const UniValue& error = signer.find_value("error");
3434
if (!error.isNull()) {
3535
if (!error.isStr()) {
3636
throw std::runtime_error(strprintf("'%s' error", command));
3737
}
3838
throw std::runtime_error(strprintf("'%s' error: %s", command, error.getValStr()));
3939
}
4040
// Check if fingerprint is present
41-
const UniValue& fingerprint = find_value(signer, "fingerprint");
41+
const UniValue& fingerprint = signer.find_value("fingerprint");
4242
if (fingerprint.isNull()) {
4343
throw std::runtime_error(strprintf("'%s' received invalid response, missing signer fingerprint", command));
4444
}
45-
const std::string fingerprintStr = fingerprint.get_str();
45+
const std::string& fingerprintStr{fingerprint.get_str()};
4646
// Skip duplicate signer
4747
bool duplicate = false;
4848
for (const ExternalSigner& signer : signers) {
4949
if (signer.m_fingerprint.compare(fingerprintStr) == 0) duplicate = true;
5050
}
5151
if (duplicate) break;
5252
std::string name;
53-
const UniValue& model_field = find_value(signer, "model");
53+
const UniValue& model_field = signer.find_value("model");
5454
if (model_field.isStr() && model_field.getValStr() != "") {
5555
name += model_field.getValStr();
5656
}
@@ -97,19 +97,19 @@ bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::str
9797

9898
const UniValue signer_result = RunCommandParseJSON(command, stdinStr);
9999

100-
if (find_value(signer_result, "error").isStr()) {
101-
error = find_value(signer_result, "error").get_str();
100+
if (signer_result.find_value("error").isStr()) {
101+
error = signer_result.find_value("error").get_str();
102102
return false;
103103
}
104104

105-
if (!find_value(signer_result, "psbt").isStr()) {
105+
if (!signer_result.find_value("psbt").isStr()) {
106106
error = "Unexpected result from signer";
107107
return false;
108108
}
109109

110110
PartiallySignedTransaction signer_psbtx;
111111
std::string signer_psbt_error;
112-
if (!DecodeBase64PSBT(signer_psbtx, find_value(signer_result, "psbt").get_str(), signer_psbt_error)) {
112+
if (!DecodeBase64PSBT(signer_psbtx, signer_result.find_value("psbt").get_str(), signer_psbt_error)) {
113113
error = strprintf("TX decode failed %s", signer_psbt_error);
114114
return false;
115115
}

src/httprpc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static void JSONErrorReply(HTTPRequest* req, const UniValue& objError, const Uni
7676
{
7777
// Send error reply from json-rpc error object
7878
int nStatus = HTTP_INTERNAL_SERVER_ERROR;
79-
int code = find_value(objError, "code").getInt<int>();
79+
int code = objError.find_value("code").getInt<int>();
8080

8181
if (code == RPC_INVALID_REQUEST)
8282
nStatus = HTTP_BAD_REQUEST;
@@ -213,7 +213,7 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
213213
} else {
214214
const UniValue& request = valRequest[reqIdx].get_obj();
215215
// Parse method
216-
std::string strMethod = find_value(request, "method").get_str();
216+
std::string strMethod = request.find_value("method").get_str();
217217
if (!g_rpc_whitelist[jreq.authUser].count(strMethod)) {
218218
LogPrintf("RPC User %s not allowed to call method %s\n", jreq.authUser, strMethod);
219219
req->WriteReply(HTTP_FORBIDDEN);

src/qt/rpcconsole.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ bool RPCConsole::RPCParseCommandLine(interfaces::Node* node, std::string &strRes
249249
subelement = lastResult[parsed.value()];
250250
}
251251
else if (lastResult.isObject())
252-
subelement = find_value(lastResult, curarg);
252+
subelement = lastResult.find_value(curarg);
253253
else
254254
throw std::runtime_error("Invalid result query"); //no array or object: abort
255255
lastResult = subelement;
@@ -448,8 +448,8 @@ void RPCExecutor::request(const QString &command, const WalletModel* wallet_mode
448448
{
449449
try // Nice formatting for standard-format error
450450
{
451-
int code = find_value(objError, "code").getInt<int>();
452-
std::string message = find_value(objError, "message").get_str();
451+
int code = objError.find_value("code").getInt<int>();
452+
std::string message = objError.find_value("message").get_str();
453453
Q_EMIT reply(RPCConsole::CMD_ERROR, QString::fromStdString(message) + " (code " + QString::number(code) + ")");
454454
}
455455
catch (const std::runtime_error&) // raised when converting to invalid type, i.e. missing code or message

src/rpc/mempool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ static RPCHelpMan gettxspendingprevout()
638638
}, /*fAllowNull=*/false, /*fStrict=*/true);
639639

640640
const uint256 txid(ParseHashO(o, "txid"));
641-
const int nOutput{find_value(o, "vout").getInt<int>()};
641+
const int nOutput{o.find_value("vout").getInt<int>()};
642642
if (nOutput < 0) {
643643
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative");
644644
}

src/rpc/mining.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ static RPCHelpMan getblocktemplate()
612612
if (!request.params[0].isNull())
613613
{
614614
const UniValue& oparam = request.params[0].get_obj();
615-
const UniValue& modeval = find_value(oparam, "mode");
615+
const UniValue& modeval = oparam.find_value("mode");
616616
if (modeval.isStr())
617617
strMode = modeval.get_str();
618618
else if (modeval.isNull())
@@ -621,11 +621,11 @@ static RPCHelpMan getblocktemplate()
621621
}
622622
else
623623
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
624-
lpval = find_value(oparam, "longpollid");
624+
lpval = oparam.find_value("longpollid");
625625

626626
if (strMode == "proposal")
627627
{
628-
const UniValue& dataval = find_value(oparam, "data");
628+
const UniValue& dataval = oparam.find_value("data");
629629
if (!dataval.isStr())
630630
throw JSONRPCError(RPC_TYPE_ERROR, "Missing data String key for proposal");
631631

@@ -652,7 +652,7 @@ static RPCHelpMan getblocktemplate()
652652
return BIP22ValidationResult(state);
653653
}
654654

655-
const UniValue& aClientRules = find_value(oparam, "rules");
655+
const UniValue& aClientRules = oparam.find_value("rules");
656656
if (aClientRules.isArray()) {
657657
for (unsigned int i = 0; i < aClientRules.size(); ++i) {
658658
const UniValue& v = aClientRules[i];

src/rpc/rawtransaction_util.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void AddInputs(CMutableTransaction& rawTx, const UniValue& inputs_in, std::optio
3636

3737
uint256 txid = ParseHashO(o, "txid");
3838

39-
const UniValue& vout_v = find_value(o, "vout");
39+
const UniValue& vout_v = o.find_value("vout");
4040
if (!vout_v.isNum())
4141
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
4242
int nOutput = vout_v.getInt<int>();
@@ -54,7 +54,7 @@ void AddInputs(CMutableTransaction& rawTx, const UniValue& inputs_in, std::optio
5454
}
5555

5656
// set the sequence number if passed in the parameters object
57-
const UniValue& sequenceObj = find_value(o, "sequence");
57+
const UniValue& sequenceObj = o.find_value("sequence");
5858
if (sequenceObj.isNum()) {
5959
int64_t seqNr64 = sequenceObj.getInt<int64_t>();
6060
if (seqNr64 < 0 || seqNr64 > CTxIn::SEQUENCE_FINAL) {
@@ -187,7 +187,7 @@ void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keyst
187187

188188
uint256 txid = ParseHashO(prevOut, "txid");
189189

190-
int nOut = find_value(prevOut, "vout").getInt<int>();
190+
int nOut = prevOut.find_value("vout").getInt<int>();
191191
if (nOut < 0) {
192192
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout cannot be negative");
193193
}
@@ -208,7 +208,7 @@ void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keyst
208208
newcoin.out.scriptPubKey = scriptPubKey;
209209
newcoin.out.nValue = MAX_MONEY;
210210
if (prevOut.exists("amount")) {
211-
newcoin.out.nValue = AmountFromValue(find_value(prevOut, "amount"));
211+
newcoin.out.nValue = AmountFromValue(prevOut.find_value("amount"));
212212
}
213213
newcoin.nHeight = 1;
214214
coins[out] = std::move(newcoin);
@@ -223,8 +223,8 @@ void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keyst
223223
{"redeemScript", UniValueType(UniValue::VSTR)},
224224
{"witnessScript", UniValueType(UniValue::VSTR)},
225225
}, true);
226-
UniValue rs = find_value(prevOut, "redeemScript");
227-
UniValue ws = find_value(prevOut, "witnessScript");
226+
const UniValue& rs{prevOut.find_value("redeemScript")};
227+
const UniValue& ws{prevOut.find_value("witnessScript")};
228228
if (rs.isNull() && ws.isNull()) {
229229
throw JSONRPCError(RPC_INVALID_PARAMETER, "Missing redeemScript/witnessScript");
230230
}

src/rpc/request.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ void JSONRPCRequest::parse(const UniValue& valRequest)
165165
const UniValue& request = valRequest.get_obj();
166166

167167
// Parse id now so errors from here on will have the id
168-
id = find_value(request, "id");
168+
id = request.find_value("id");
169169

170170
// Parse method
171-
UniValue valMethod = find_value(request, "method");
171+
const UniValue& valMethod{request.find_value("method")};
172172
if (valMethod.isNull())
173173
throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
174174
if (!valMethod.isStr())
@@ -181,7 +181,7 @@ void JSONRPCRequest::parse(const UniValue& valRequest)
181181
LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s\n", SanitizeString(strMethod), this->authUser);
182182

183183
// Parse params
184-
UniValue valParams = find_value(request, "params");
184+
const UniValue& valParams{request.find_value("params")};
185185
if (valParams.isArray() || valParams.isObject())
186186
params = valParams;
187187
else if (valParams.isNull())

src/rpc/util.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void RPCTypeCheckObj(const UniValue& o,
3737
bool fStrict)
3838
{
3939
for (const auto& t : typesExpected) {
40-
const UniValue& v = find_value(o, t.first);
40+
const UniValue& v = o.find_value(t.first);
4141
if (!fAllowNull && v.isNull())
4242
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));
4343

@@ -81,7 +81,7 @@ uint256 ParseHashV(const UniValue& v, std::string strName)
8181
}
8282
uint256 ParseHashO(const UniValue& o, std::string strKey)
8383
{
84-
return ParseHashV(find_value(o, strKey), strKey);
84+
return ParseHashV(o.find_value(strKey), strKey);
8585
}
8686
std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName)
8787
{
@@ -94,7 +94,7 @@ std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName)
9494
}
9595
std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey)
9696
{
97-
return ParseHexV(find_value(o, strKey), strKey);
97+
return ParseHexV(o.find_value(strKey), strKey);
9898
}
9999

100100
namespace {
@@ -1133,10 +1133,10 @@ std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, Fl
11331133
if (scanobject.isStr()) {
11341134
desc_str = scanobject.get_str();
11351135
} else if (scanobject.isObject()) {
1136-
UniValue desc_uni = find_value(scanobject, "desc");
1136+
const UniValue& desc_uni{scanobject.find_value("desc")};
11371137
if (desc_uni.isNull()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor needs to be provided in scan object");
11381138
desc_str = desc_uni.get_str();
1139-
UniValue range_uni = find_value(scanobject, "range");
1139+
const UniValue& range_uni{scanobject.find_value("range")};
11401140
if (!range_uni.isNull()) {
11411141
range = ParseDescriptorRange(range_uni);
11421142
}

src/test/fuzz/rpc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ FUZZ_TARGET_INIT(rpc, initialize_rpc)
364364
try {
365365
rpc_testing_setup->CallRPC(rpc_command, arguments);
366366
} catch (const UniValue& json_rpc_error) {
367-
const std::string error_msg{find_value(json_rpc_error, "message").get_str()};
367+
const std::string error_msg{json_rpc_error.find_value("message").get_str()};
368368
// Once c++20 is allowed, starts_with can be used.
369369
// if (error_msg.starts_with("Internal bug detected")) {
370370
if (0 == error_msg.rfind("Internal bug detected", 0)) {

0 commit comments

Comments
 (0)