Skip to content

Commit 6d81d06

Browse files
committed
merge bitcoin#27605: Replace global find_value function with UniValue::find_value method
1 parent 203a1c1 commit 6d81d06

File tree

17 files changed

+126
-127
lines changed

17 files changed

+126
-127
lines changed

src/bitcoin-cli.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& str
876876
try {
877877
response = CallRPC(rh, strMethod, args, rpcwallet);
878878
if (fWait) {
879-
const UniValue& error = find_value(response, "error");
879+
const UniValue& error = response.find_value("error");
880880
if (!error.isNull() && error["code"].getInt<int>() == RPC_IN_WARMUP) {
881881
throw CConnectionFailed("server in warmup");
882882
}
@@ -904,8 +904,8 @@ static void ParseResult(const UniValue& result, std::string& strPrint)
904904
static void ParseError(const UniValue& error, std::string& strPrint, int& nRet)
905905
{
906906
if (error.isObject()) {
907-
const UniValue& err_code = find_value(error, "code");
908-
const UniValue& err_msg = find_value(error, "message");
907+
const UniValue& err_code = error.find_value("code");
908+
const UniValue& err_msg = error.find_value("message");
909909
if (!err_code.isNull()) {
910910
strPrint = "error code: " + err_code.getValStr() + "\n";
911911
}
@@ -931,15 +931,15 @@ static void GetWalletBalances(UniValue& result)
931931
{
932932
DefaultRequestHandler rh;
933933
const UniValue listwallets = ConnectAndCallRPC(&rh, "listwallets", /* args=*/{});
934-
if (!find_value(listwallets, "error").isNull()) return;
935-
const UniValue& wallets = find_value(listwallets, "result");
934+
if (!listwallets.find_value("error").isNull()) return;
935+
const UniValue& wallets = listwallets.find_value("result");
936936
if (wallets.size() <= 1) return;
937937

938938
UniValue balances(UniValue::VOBJ);
939939
for (const UniValue& wallet : wallets.getValues()) {
940940
const std::string wallet_name = wallet.get_str();
941941
const UniValue getbalances = ConnectAndCallRPC(&rh, "getbalances", /* args=*/{}, wallet_name);
942-
const UniValue& balance = find_value(getbalances, "result")["mine"]["trusted"];
942+
const UniValue& balance = getbalances.find_value("result")["mine"]["trusted"];
943943
balances.pushKV(wallet_name, balance);
944944
}
945945
result.pushKV("balances", balances);
@@ -975,7 +975,7 @@ static void GetProgressBar(double progress, std::string& progress_bar)
975975
*/
976976
static void ParseGetInfoResult(UniValue& result)
977977
{
978-
if (!find_value(result, "error").isNull()) return;
978+
if (!result.find_value("error").isNull()) return;
979979

980980
std::string RESET, GREEN, BLUE, YELLOW, MAGENTA, CYAN;
981981
bool should_colorize = false;
@@ -1191,9 +1191,9 @@ static int CommandLineRPC(int argc, char *argv[])
11911191
rh.reset(new NetinfoRequestHandler());
11921192
} else if (gArgs.GetBoolArg("-generate", false)) {
11931193
const UniValue getnewaddress{GetNewAddress()};
1194-
const UniValue& error{find_value(getnewaddress, "error")};
1194+
const UniValue& error{getnewaddress.find_value("error")};
11951195
if (error.isNull()) {
1196-
SetGenerateToAddressArgs(find_value(getnewaddress, "result").get_str(), args);
1196+
SetGenerateToAddressArgs(getnewaddress.find_value("result").get_str(), args);
11971197
rh.reset(new GenerateToAddressRequestHandler());
11981198
} else {
11991199
ParseError(error, strPrint, nRet);
@@ -1215,8 +1215,8 @@ static int CommandLineRPC(int argc, char *argv[])
12151215
const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name);
12161216

12171217
// Parse reply
1218-
UniValue result = find_value(reply, "result");
1219-
const UniValue& error = find_value(reply, "error");
1218+
UniValue result = reply.find_value("result");
1219+
const UniValue& error = reply.find_value("error");
12201220
if (error.isNull()) {
12211221
if (gArgs.GetBoolArg("-getinfo", false)) {
12221222
if (!gArgs.IsArgSet("-rpcwallet")) {

src/httprpc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static bool JSONErrorReply(RpcHttpRequest& rpcRequest, const UniValue& objError,
116116
{
117117
// Send error reply from json-rpc error object
118118
int nStatus = HTTP_INTERNAL_SERVER_ERROR;
119-
int code = find_value(objError, "code").getInt<int>();
119+
int code = objError.find_value("code").getInt<int>();
120120

121121
if (code == RPC_INVALID_REQUEST)
122122
nStatus = HTTP_BAD_REQUEST;
@@ -253,7 +253,7 @@ static bool HTTPReq_JSONRPC(const CoreContext& context, HTTPRequest* req)
253253
} else {
254254
const UniValue& request = valRequest[reqIdx].get_obj();
255255
// Parse method
256-
std::string strMethod = find_value(request, "method").get_str();
256+
std::string strMethod = request.find_value("method").get_str();
257257
if (!whitelisted(jreq)) {
258258
LogPrintf("RPC User %s not allowed to call method %s\n", jreq.authUser, strMethod);
259259
return rpcRequest.send_reply(HTTP_FORBIDDEN);

src/qt/governancelist.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ Proposal::Proposal(ClientModel* _clientModel, const CGovernanceObject& _govObj,
4545
{
4646
UniValue prop_data;
4747
if (prop_data.read(govObj.GetDataAsPlainString())) {
48-
if (UniValue titleValue = find_value(prop_data, "name"); titleValue.isStr()) {
48+
if (UniValue titleValue = prop_data.find_value("name"); titleValue.isStr()) {
4949
m_title = QString::fromStdString(titleValue.get_str());
5050
}
5151

52-
if (UniValue paymentStartValue = find_value(prop_data, "start_epoch"); paymentStartValue.isNum()) {
52+
if (UniValue paymentStartValue = prop_data.find_value("start_epoch"); paymentStartValue.isNum()) {
5353
m_startDate = QDateTime::fromSecsSinceEpoch(paymentStartValue.getInt<int64_t>());
5454
}
5555

56-
if (UniValue paymentEndValue = find_value(prop_data, "end_epoch"); paymentEndValue.isNum()) {
56+
if (UniValue paymentEndValue = prop_data.find_value("end_epoch"); paymentEndValue.isNum()) {
5757
m_endDate = QDateTime::fromSecsSinceEpoch(paymentEndValue.getInt<int64_t>());
5858
}
5959

60-
if (UniValue amountValue = find_value(prop_data, "payment_amount"); amountValue.isNum()) {
60+
if (UniValue amountValue = prop_data.find_value("payment_amount"); amountValue.isNum()) {
6161
m_paymentAmount = amountValue.get_real();
6262
}
6363

64-
if (UniValue urlValue = find_value(prop_data, "url"); urlValue.isStr()) {
64+
if (UniValue urlValue = prop_data.find_value("url"); urlValue.isStr()) {
6565
m_url = QString::fromStdString(urlValue.get_str());
6666
}
6767
}

src/qt/rpcconsole.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ bool RPCConsole::RPCParseCommandLine(interfaces::Node* node, std::string &strRes
261261
subelement = lastResult[parsed.value()];
262262
}
263263
else if (lastResult.isObject())
264-
subelement = find_value(lastResult, curarg);
264+
subelement = lastResult.find_value(curarg);
265265
else
266266
throw std::runtime_error("Invalid result query"); //no array or object: abort
267267
lastResult = subelement;
@@ -460,8 +460,8 @@ void RPCExecutor::request(const QString &command, const WalletModel* wallet_mode
460460
{
461461
try // Nice formatting for standard-format error
462462
{
463-
int code = find_value(objError, "code").getInt<int>();
464-
std::string message = find_value(objError, "message").get_str();
463+
int code = objError.find_value("code").getInt<int>();
464+
std::string message = objError.find_value("message").get_str();
465465
Q_EMIT reply(RPCConsole::CMD_ERROR, QString::fromStdString(message) + " (code " + QString::number(code) + ")");
466466
}
467467
catch (const std::runtime_error&) // raised when converting to invalid type, i.e. missing code or message

src/rpc/mining.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ static RPCHelpMan getblocktemplate()
668668
if (!request.params[0].isNull())
669669
{
670670
const UniValue& oparam = request.params[0].get_obj();
671-
const UniValue& modeval = find_value(oparam, "mode");
671+
const UniValue& modeval = oparam.find_value("mode");
672672
if (modeval.isStr())
673673
strMode = modeval.get_str();
674674
else if (modeval.isNull())
@@ -677,11 +677,11 @@ static RPCHelpMan getblocktemplate()
677677
}
678678
else
679679
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
680-
lpval = find_value(oparam, "longpollid");
680+
lpval = oparam.find_value("longpollid");
681681

682682
if (strMode == "proposal")
683683
{
684-
const UniValue& dataval = find_value(oparam, "data");
684+
const UniValue& dataval = oparam.find_value("data");
685685
if (!dataval.isStr())
686686
throw JSONRPCError(RPC_TYPE_ERROR, "Missing data String key for proposal");
687687

@@ -711,7 +711,7 @@ static RPCHelpMan getblocktemplate()
711711
return BIP22ValidationResult(state);
712712
}
713713

714-
const UniValue& aClientRules = find_value(oparam, "rules");
714+
const UniValue& aClientRules = oparam.find_value("rules");
715715
if (aClientRules.isArray()) {
716716
for (unsigned int i = 0; i < aClientRules.size(); ++i) {
717717
const UniValue& v = aClientRules[i];

src/rpc/node.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ static bool getAddressesFromParams(const UniValue& params, std::vector<std::pair
357357
addresses.push_back(std::make_pair(hashBytes, type));
358358
} else if (params[0].isObject()) {
359359

360-
UniValue addressValues = find_value(params[0].get_obj(), "addresses");
360+
UniValue addressValues = params[0].get_obj().find_value("addresses");
361361
if (!addressValues.isArray()) {
362362
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Addresses is expected to be an array");
363363
}
@@ -551,8 +551,8 @@ static RPCHelpMan getaddressdeltas()
551551
},
552552
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
553553
{
554-
UniValue startValue = find_value(request.params[0].get_obj(), "start");
555-
UniValue endValue = find_value(request.params[0].get_obj(), "end");
554+
UniValue startValue = request.params[0].get_obj().find_value("start");
555+
UniValue endValue = request.params[0].get_obj().find_value("end");
556556

557557
int start = 0;
558558
int end = 0;
@@ -715,8 +715,8 @@ static RPCHelpMan getaddresstxids()
715715
int start = 0;
716716
int end = 0;
717717
if (request.params[0].isObject()) {
718-
UniValue startValue = find_value(request.params[0].get_obj(), "start");
719-
UniValue endValue = find_value(request.params[0].get_obj(), "end");
718+
UniValue startValue = request.params[0].get_obj().find_value("start");
719+
UniValue endValue = request.params[0].get_obj().find_value("end");
720720
if (startValue.isNum() && endValue.isNum()) {
721721
start = startValue.getInt<int>();
722722
end = endValue.getInt<int>();
@@ -789,8 +789,8 @@ static RPCHelpMan getspentinfo()
789789
},
790790
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
791791
{
792-
UniValue txidValue = find_value(request.params[0].get_obj(), "txid");
793-
UniValue indexValue = find_value(request.params[0].get_obj(), "index");
792+
UniValue txidValue = request.params[0].get_obj().find_value("txid");
793+
UniValue indexValue = request.params[0].get_obj().find_value("index");
794794

795795
if (!txidValue.isStr() || !indexValue.isNum()) {
796796
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid txid or index");

src/rpc/rawtransaction_util.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal
5151

5252
uint256 txid = ParseHashO(o, "txid");
5353

54-
const UniValue& vout_v = find_value(o, "vout");
54+
const UniValue& vout_v = o.find_value("vout");
5555
if (!vout_v.isNum())
5656
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
5757
int nOutput = vout_v.getInt<int>();
@@ -66,7 +66,7 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal
6666
}
6767

6868
// set the sequence number if passed in the parameters object
69-
const UniValue& sequenceObj = find_value(o, "sequence");
69+
const UniValue& sequenceObj = o.find_value("sequence");
7070
if (sequenceObj.isNum()) {
7171
int64_t seqNr64 = sequenceObj.getInt<int64_t>();
7272
if (seqNr64 < 0 || seqNr64 > CTxIn::SEQUENCE_FINAL)
@@ -164,7 +164,7 @@ void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keyst
164164

165165
uint256 txid = ParseHashO(prevOut, "txid");
166166

167-
int nOut = find_value(prevOut, "vout").getInt<int>();
167+
int nOut = prevOut.find_value("vout").getInt<int>();
168168
if (nOut < 0) {
169169
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout cannot be negative");
170170
}
@@ -185,7 +185,7 @@ void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keyst
185185
newcoin.out.scriptPubKey = scriptPubKey;
186186
newcoin.out.nValue = 0;
187187
if (prevOut.exists("amount")) {
188-
newcoin.out.nValue = AmountFromValue(find_value(prevOut, "amount"));
188+
newcoin.out.nValue = AmountFromValue(prevOut.find_value("amount"));
189189
}
190190
newcoin.nHeight = 1;
191191
coins[out] = std::move(newcoin);
@@ -198,7 +198,7 @@ void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keyst
198198
{
199199
{"redeemScript", UniValueType(UniValue::VSTR)},
200200
});
201-
UniValue rs = find_value(prevOut, "redeemScript");
201+
const UniValue& rs{prevOut.find_value("redeemScript")};
202202
if (rs.isNull()) {
203203
throw JSONRPCError(RPC_INVALID_PARAMETER, "Missing redeemScript");
204204
}

src/rpc/request.cpp

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

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

168168
// Parse method
169-
UniValue valMethod = find_value(request, "method");
169+
const UniValue& valMethod{request.find_value("method")};
170170
if (valMethod.isNull())
171171
throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
172172
if (!valMethod.isStr())
@@ -181,7 +181,7 @@ void JSONRPCRequest::parse(const UniValue& valRequest)
181181
}
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
@@ -61,7 +61,7 @@ void RPCTypeCheckObj(const UniValue& o,
6161
bool fStrict)
6262
{
6363
for (const auto& t : typesExpected) {
64-
const UniValue& v = find_value(o, t.first);
64+
const UniValue& v = o.find_value(t.first);
6565
if (!fAllowNull && v.isNull())
6666
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));
6767

@@ -108,7 +108,7 @@ uint256 ParseHashV(const UniValue& v, std::string strName)
108108
}
109109
uint256 ParseHashO(const UniValue& o, std::string strKey)
110110
{
111-
return ParseHashV(find_value(o, strKey), strKey);
111+
return ParseHashV(o.find_value(strKey), strKey);
112112
}
113113
std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName)
114114
{
@@ -121,7 +121,7 @@ std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName)
121121
}
122122
std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey)
123123
{
124-
return ParseHexV(find_value(o, strKey), strKey);
124+
return ParseHexV(o.find_value(strKey), strKey);
125125
}
126126

127127
int32_t ParseInt32V(const UniValue& v, const std::string &strName)
@@ -1009,10 +1009,10 @@ std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, Fl
10091009
if (scanobject.isStr()) {
10101010
desc_str = scanobject.get_str();
10111011
} else if (scanobject.isObject()) {
1012-
UniValue desc_uni = find_value(scanobject, "desc");
1012+
const UniValue& desc_uni{scanobject.find_value("desc")};
10131013
if (desc_uni.isNull()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor needs to be provided in scan object");
10141014
desc_str = desc_uni.get_str();
1015-
UniValue range_uni = find_value(scanobject, "range");
1015+
const UniValue& range_uni{scanobject.find_value("range")};
10161016
if (!range_uni.isNull()) {
10171017
range = ParseDescriptorRange(range_uni);
10181018
}

src/test/fuzz/rpc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ FUZZ_TARGET(rpc, .init = initialize_rpc)
356356
try {
357357
rpc_testing_setup->CallRPC(rpc_command, arguments);
358358
} catch (const UniValue& json_rpc_error) {
359-
const std::string error_msg{find_value(json_rpc_error, "message").get_str()};
359+
const std::string error_msg{json_rpc_error.find_value("message").get_str()};
360360
if (error_msg.starts_with("Internal bug detected")) {
361361
// Only allow the intentional internal bug
362362
assert(error_msg.find("trigger_internal_bug") != std::string::npos);

0 commit comments

Comments
 (0)