aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.bench.include1
-rw-r--r--src/bench/streams_findbyte.cpp31
-rw-r--r--src/bitcoin-cli.cpp25
-rw-r--r--src/chainparams.cpp2
-rw-r--r--src/chainparams.h5
-rw-r--r--src/chainparamsbase.cpp2
-rw-r--r--src/chainparamsbase.h2
-rw-r--r--src/common/args.h6
-rw-r--r--src/external_signer.cpp16
-rw-r--r--src/httprpc.cpp4
-rw-r--r--src/qt/rpcconsole.cpp6
-rw-r--r--src/rpc/mempool.cpp2
-rw-r--r--src/rpc/mining.cpp8
-rw-r--r--src/rpc/rawtransaction_util.cpp12
-rw-r--r--src/rpc/request.cpp6
-rw-r--r--src/rpc/util.cpp10
-rw-r--r--src/streams.h20
-rw-r--r--src/test/argsman_tests.cpp2
-rw-r--r--src/test/fuzz/buffered_file.cpp2
-rw-r--r--src/test/fuzz/rpc.cpp2
-rw-r--r--src/test/interfaces_tests.cpp2
-rw-r--r--src/test/key_io_tests.cpp14
-rw-r--r--src/test/rpc_tests.cpp42
-rw-r--r--src/test/streams_tests.cpp2
-rw-r--r--src/test/system_tests.cpp4
-rw-r--r--src/univalue/include/univalue.h4
-rw-r--r--src/univalue/lib/univalue.cpp11
-rw-r--r--src/validation.cpp2
-rw-r--r--src/wallet/rpc/coins.cpp2
-rw-r--r--src/wallet/rpc/spend.cpp4
-rw-r--r--src/wallet/wallet.cpp2
31 files changed, 145 insertions, 108 deletions
diff --git a/src/Makefile.bench.include b/src/Makefile.bench.include
index c230728a1c..c8e510b482 100644
--- a/src/Makefile.bench.include
+++ b/src/Makefile.bench.include
@@ -47,6 +47,7 @@ bench_bench_bitcoin_SOURCES = \
bench/rollingbloom.cpp \
bench/rpc_blockchain.cpp \
bench/rpc_mempool.cpp \
+ bench/streams_findbyte.cpp \
bench/strencodings.cpp \
bench/util_time.cpp \
bench/verify_script.cpp
diff --git a/src/bench/streams_findbyte.cpp b/src/bench/streams_findbyte.cpp
new file mode 100644
index 0000000000..77f5940926
--- /dev/null
+++ b/src/bench/streams_findbyte.cpp
@@ -0,0 +1,31 @@
+// Copyright (c) 2023 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include <bench/bench.h>
+
+#include <util/fs.h>
+#include <streams.h>
+
+static void FindByte(benchmark::Bench& bench)
+{
+ // Setup
+ FILE* file = fsbridge::fopen("streams_tmp", "w+b");
+ const size_t file_size = 200;
+ uint8_t data[file_size] = {0};
+ data[file_size-1] = 1;
+ fwrite(&data, sizeof(uint8_t), file_size, file);
+ rewind(file);
+ CBufferedFile bf(file, /*nBufSize=*/file_size + 1, /*nRewindIn=*/file_size, 0, 0);
+
+ bench.run([&] {
+ bf.SetPos(0);
+ bf.FindByte(std::byte(1));
+ });
+
+ // Cleanup
+ bf.fclose();
+ fs::remove("streams_tmp");
+}
+
+BENCHMARK(FindByte, benchmark::PriorityLevel::HIGH);
diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp
index a66134128d..48dc11b95a 100644
--- a/src/bitcoin-cli.cpp
+++ b/src/bitcoin-cli.cpp
@@ -434,9 +434,10 @@ private:
return " signet";
case ChainType::REGTEST:
return " regtest";
- default:
+ case ChainType::MAIN:
return "";
}
+ assert(false);
}
std::string PingTimeToString(double seconds) const
{
@@ -870,7 +871,7 @@ static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& str
try {
response = CallRPC(rh, strMethod, args, rpcwallet);
if (fWait) {
- const UniValue& error = find_value(response, "error");
+ const UniValue& error = response.find_value("error");
if (!error.isNull() && error["code"].getInt<int>() == RPC_IN_WARMUP) {
throw CConnectionFailed("server in warmup");
}
@@ -898,8 +899,8 @@ static void ParseResult(const UniValue& result, std::string& strPrint)
static void ParseError(const UniValue& error, std::string& strPrint, int& nRet)
{
if (error.isObject()) {
- const UniValue& err_code = find_value(error, "code");
- const UniValue& err_msg = find_value(error, "message");
+ const UniValue& err_code = error.find_value("code");
+ const UniValue& err_msg = error.find_value("message");
if (!err_code.isNull()) {
strPrint = "error code: " + err_code.getValStr() + "\n";
}
@@ -925,15 +926,15 @@ static void GetWalletBalances(UniValue& result)
{
DefaultRequestHandler rh;
const UniValue listwallets = ConnectAndCallRPC(&rh, "listwallets", /* args=*/{});
- if (!find_value(listwallets, "error").isNull()) return;
- const UniValue& wallets = find_value(listwallets, "result");
+ if (!listwallets.find_value("error").isNull()) return;
+ const UniValue& wallets = listwallets.find_value("result");
if (wallets.size() <= 1) return;
UniValue balances(UniValue::VOBJ);
for (const UniValue& wallet : wallets.getValues()) {
const std::string& wallet_name = wallet.get_str();
const UniValue getbalances = ConnectAndCallRPC(&rh, "getbalances", /* args=*/{}, wallet_name);
- const UniValue& balance = find_value(getbalances, "result")["mine"]["trusted"];
+ const UniValue& balance = getbalances.find_value("result")["mine"]["trusted"];
balances.pushKV(wallet_name, balance);
}
result.pushKV("balances", balances);
@@ -969,7 +970,7 @@ static void GetProgressBar(double progress, std::string& progress_bar)
*/
static void ParseGetInfoResult(UniValue& result)
{
- if (!find_value(result, "error").isNull()) return;
+ if (!result.find_value("error").isNull()) return;
std::string RESET, GREEN, BLUE, YELLOW, MAGENTA, CYAN;
bool should_colorize = false;
@@ -1181,9 +1182,9 @@ static int CommandLineRPC(int argc, char *argv[])
rh.reset(new NetinfoRequestHandler());
} else if (gArgs.GetBoolArg("-generate", false)) {
const UniValue getnewaddress{GetNewAddress()};
- const UniValue& error{find_value(getnewaddress, "error")};
+ const UniValue& error{getnewaddress.find_value("error")};
if (error.isNull()) {
- SetGenerateToAddressArgs(find_value(getnewaddress, "result").get_str(), args);
+ SetGenerateToAddressArgs(getnewaddress.find_value("result").get_str(), args);
rh.reset(new GenerateToAddressRequestHandler());
} else {
ParseError(error, strPrint, nRet);
@@ -1205,8 +1206,8 @@ static int CommandLineRPC(int argc, char *argv[])
const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name);
// Parse reply
- UniValue result = find_value(reply, "result");
- const UniValue& error = find_value(reply, "error");
+ UniValue result = reply.find_value("result");
+ const UniValue& error = reply.find_value("error");
if (error.isNull()) {
if (gArgs.GetBoolArg("-getinfo", false)) {
if (!gArgs.IsArgSet("-rpcwallet")) {
diff --git a/src/chainparams.cpp b/src/chainparams.cpp
index f61bf01745..6f4453d1fe 100644
--- a/src/chainparams.cpp
+++ b/src/chainparams.cpp
@@ -117,7 +117,7 @@ std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, c
return CChainParams::RegTest(opts);
}
}
- throw std::invalid_argument(strprintf("%s: Invalid ChainType value", __func__));
+ assert(false);
}
void SelectParams(const ChainType chain)
diff --git a/src/chainparams.h b/src/chainparams.h
index 6a65f40f80..1e8366dcf5 100644
--- a/src/chainparams.h
+++ b/src/chainparams.h
@@ -25,8 +25,6 @@ class ArgsManager;
/**
* Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
- * @returns a CChainParams* of the chosen chain.
- * @throws a std::runtime_error if the chain is not supported.
*/
std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const ChainType chain);
@@ -37,8 +35,7 @@ std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, c
const CChainParams &Params();
/**
- * Sets the params returned by Params() to those for the given chain name.
- * @throws std::runtime_error when the chain is not supported.
+ * Sets the params returned by Params() to those for the given chain type.
*/
void SelectParams(const ChainType chain);
diff --git a/src/chainparamsbase.cpp b/src/chainparamsbase.cpp
index 4ebfeae1a7..8cbf9e85e0 100644
--- a/src/chainparamsbase.cpp
+++ b/src/chainparamsbase.cpp
@@ -48,7 +48,7 @@ std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const ChainType chain)
case ChainType::REGTEST:
return std::make_unique<CBaseChainParams>("regtest", 18443, 18445);
}
- throw std::invalid_argument(strprintf("%s: Invalid ChainType value", __func__));
+ assert(false);
}
void SelectBaseParams(const ChainType chain)
diff --git a/src/chainparamsbase.h b/src/chainparamsbase.h
index 73e449d11b..ea933d1ca8 100644
--- a/src/chainparamsbase.h
+++ b/src/chainparamsbase.h
@@ -35,8 +35,6 @@ private:
/**
* Creates and returns a std::unique_ptr<CBaseChainParams> of the chosen chain.
- * @returns a CBaseChainParams* of the chosen chain.
- * @throws a std::runtime_error if the chain is not supported.
*/
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const ChainType chain);
diff --git a/src/common/args.h b/src/common/args.h
index 5335633911..537a64fcfd 100644
--- a/src/common/args.h
+++ b/src/common/args.h
@@ -325,14 +325,14 @@ protected:
void ForceSetArg(const std::string& strArg, const std::string& strValue);
/**
- * Returns the appropriate chain name from the program arguments.
+ * Returns the appropriate chain type from the program arguments.
* @return ChainType::MAIN by default; raises runtime error if an invalid
* combination, or unknown chain is given.
*/
ChainType GetChainType() const;
/**
- * Returns the appropriate chain name string from the program arguments.
+ * Returns the appropriate chain type string from the program arguments.
* @return ChainType::MAIN string by default; raises runtime error if an
* invalid combination is given.
*/
@@ -423,7 +423,7 @@ private:
/**
* Return -regtest/-signet/-testnet/-chain= setting as a ChainType enum if a
- * recognized chain name was set, or as a string if an unrecognized chain
+ * recognized chain type was set, or as a string if an unrecognized chain
* name was set. Raise an exception if an invalid combination of flags was
* provided.
*/
diff --git a/src/external_signer.cpp b/src/external_signer.cpp
index 5524b943f4..6b1e1f0241 100644
--- a/src/external_signer.cpp
+++ b/src/external_signer.cpp
@@ -30,7 +30,7 @@ bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalS
}
for (const UniValue& signer : result.getValues()) {
// Check for error
- const UniValue& error = find_value(signer, "error");
+ const UniValue& error = signer.find_value("error");
if (!error.isNull()) {
if (!error.isStr()) {
throw std::runtime_error(strprintf("'%s' error", command));
@@ -38,11 +38,11 @@ bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalS
throw std::runtime_error(strprintf("'%s' error: %s", command, error.getValStr()));
}
// Check if fingerprint is present
- const UniValue& fingerprint = find_value(signer, "fingerprint");
+ const UniValue& fingerprint = signer.find_value("fingerprint");
if (fingerprint.isNull()) {
throw std::runtime_error(strprintf("'%s' received invalid response, missing signer fingerprint", command));
}
- const std::string fingerprintStr = fingerprint.get_str();
+ const std::string& fingerprintStr{fingerprint.get_str()};
// Skip duplicate signer
bool duplicate = false;
for (const ExternalSigner& signer : signers) {
@@ -50,7 +50,7 @@ bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalS
}
if (duplicate) break;
std::string name;
- const UniValue& model_field = find_value(signer, "model");
+ const UniValue& model_field = signer.find_value("model");
if (model_field.isStr() && model_field.getValStr() != "") {
name += model_field.getValStr();
}
@@ -97,19 +97,19 @@ bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::str
const UniValue signer_result = RunCommandParseJSON(command, stdinStr);
- if (find_value(signer_result, "error").isStr()) {
- error = find_value(signer_result, "error").get_str();
+ if (signer_result.find_value("error").isStr()) {
+ error = signer_result.find_value("error").get_str();
return false;
}
- if (!find_value(signer_result, "psbt").isStr()) {
+ if (!signer_result.find_value("psbt").isStr()) {
error = "Unexpected result from signer";
return false;
}
PartiallySignedTransaction signer_psbtx;
std::string signer_psbt_error;
- if (!DecodeBase64PSBT(signer_psbtx, find_value(signer_result, "psbt").get_str(), signer_psbt_error)) {
+ if (!DecodeBase64PSBT(signer_psbtx, signer_result.find_value("psbt").get_str(), signer_psbt_error)) {
error = strprintf("TX decode failed %s", signer_psbt_error);
return false;
}
diff --git a/src/httprpc.cpp b/src/httprpc.cpp
index bf3fa6298d..661406e122 100644
--- a/src/httprpc.cpp
+++ b/src/httprpc.cpp
@@ -76,7 +76,7 @@ static void JSONErrorReply(HTTPRequest* req, const UniValue& objError, const Uni
{
// Send error reply from json-rpc error object
int nStatus = HTTP_INTERNAL_SERVER_ERROR;
- int code = find_value(objError, "code").getInt<int>();
+ int code = objError.find_value("code").getInt<int>();
if (code == RPC_INVALID_REQUEST)
nStatus = HTTP_BAD_REQUEST;
@@ -213,7 +213,7 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
} else {
const UniValue& request = valRequest[reqIdx].get_obj();
// Parse method
- std::string strMethod = find_value(request, "method").get_str();
+ std::string strMethod = request.find_value("method").get_str();
if (!g_rpc_whitelist[jreq.authUser].count(strMethod)) {
LogPrintf("RPC User %s not allowed to call method %s\n", jreq.authUser, strMethod);
req->WriteReply(HTTP_FORBIDDEN);
diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp
index 972bc36ad2..bac64e3d5f 100644
--- a/src/qt/rpcconsole.cpp
+++ b/src/qt/rpcconsole.cpp
@@ -249,7 +249,7 @@ bool RPCConsole::RPCParseCommandLine(interfaces::Node* node, std::string &strRes
subelement = lastResult[parsed.value()];
}
else if (lastResult.isObject())
- subelement = find_value(lastResult, curarg);
+ subelement = lastResult.find_value(curarg);
else
throw std::runtime_error("Invalid result query"); //no array or object: abort
lastResult = subelement;
@@ -448,8 +448,8 @@ void RPCExecutor::request(const QString &command, const WalletModel* wallet_mode
{
try // Nice formatting for standard-format error
{
- int code = find_value(objError, "code").getInt<int>();
- std::string message = find_value(objError, "message").get_str();
+ int code = objError.find_value("code").getInt<int>();
+ std::string message = objError.find_value("message").get_str();
Q_EMIT reply(RPCConsole::CMD_ERROR, QString::fromStdString(message) + " (code " + QString::number(code) + ")");
}
catch (const std::runtime_error&) // raised when converting to invalid type, i.e. missing code or message
diff --git a/src/rpc/mempool.cpp b/src/rpc/mempool.cpp
index 927b4ce1fc..89c403b6f5 100644
--- a/src/rpc/mempool.cpp
+++ b/src/rpc/mempool.cpp
@@ -638,7 +638,7 @@ static RPCHelpMan gettxspendingprevout()
}, /*fAllowNull=*/false, /*fStrict=*/true);
const uint256 txid(ParseHashO(o, "txid"));
- const int nOutput{find_value(o, "vout").getInt<int>()};
+ const int nOutput{o.find_value("vout").getInt<int>()};
if (nOutput < 0) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative");
}
diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp
index 334bc988cc..68017e5af2 100644
--- a/src/rpc/mining.cpp
+++ b/src/rpc/mining.cpp
@@ -612,7 +612,7 @@ static RPCHelpMan getblocktemplate()
if (!request.params[0].isNull())
{
const UniValue& oparam = request.params[0].get_obj();
- const UniValue& modeval = find_value(oparam, "mode");
+ const UniValue& modeval = oparam.find_value("mode");
if (modeval.isStr())
strMode = modeval.get_str();
else if (modeval.isNull())
@@ -621,11 +621,11 @@ static RPCHelpMan getblocktemplate()
}
else
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
- lpval = find_value(oparam, "longpollid");
+ lpval = oparam.find_value("longpollid");
if (strMode == "proposal")
{
- const UniValue& dataval = find_value(oparam, "data");
+ const UniValue& dataval = oparam.find_value("data");
if (!dataval.isStr())
throw JSONRPCError(RPC_TYPE_ERROR, "Missing data String key for proposal");
@@ -652,7 +652,7 @@ static RPCHelpMan getblocktemplate()
return BIP22ValidationResult(state);
}
- const UniValue& aClientRules = find_value(oparam, "rules");
+ const UniValue& aClientRules = oparam.find_value("rules");
if (aClientRules.isArray()) {
for (unsigned int i = 0; i < aClientRules.size(); ++i) {
const UniValue& v = aClientRules[i];
diff --git a/src/rpc/rawtransaction_util.cpp b/src/rpc/rawtransaction_util.cpp
index 3ba930f84f..3a6fa39e4d 100644
--- a/src/rpc/rawtransaction_util.cpp
+++ b/src/rpc/rawtransaction_util.cpp
@@ -36,7 +36,7 @@ void AddInputs(CMutableTransaction& rawTx, const UniValue& inputs_in, std::optio
uint256 txid = ParseHashO(o, "txid");
- const UniValue& vout_v = find_value(o, "vout");
+ const UniValue& vout_v = o.find_value("vout");
if (!vout_v.isNum())
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
int nOutput = vout_v.getInt<int>();
@@ -54,7 +54,7 @@ void AddInputs(CMutableTransaction& rawTx, const UniValue& inputs_in, std::optio
}
// set the sequence number if passed in the parameters object
- const UniValue& sequenceObj = find_value(o, "sequence");
+ const UniValue& sequenceObj = o.find_value("sequence");
if (sequenceObj.isNum()) {
int64_t seqNr64 = sequenceObj.getInt<int64_t>();
if (seqNr64 < 0 || seqNr64 > CTxIn::SEQUENCE_FINAL) {
@@ -187,7 +187,7 @@ void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keyst
uint256 txid = ParseHashO(prevOut, "txid");
- int nOut = find_value(prevOut, "vout").getInt<int>();
+ int nOut = prevOut.find_value("vout").getInt<int>();
if (nOut < 0) {
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout cannot be negative");
}
@@ -208,7 +208,7 @@ void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keyst
newcoin.out.scriptPubKey = scriptPubKey;
newcoin.out.nValue = MAX_MONEY;
if (prevOut.exists("amount")) {
- newcoin.out.nValue = AmountFromValue(find_value(prevOut, "amount"));
+ newcoin.out.nValue = AmountFromValue(prevOut.find_value("amount"));
}
newcoin.nHeight = 1;
coins[out] = std::move(newcoin);
@@ -223,8 +223,8 @@ void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keyst
{"redeemScript", UniValueType(UniValue::VSTR)},
{"witnessScript", UniValueType(UniValue::VSTR)},
}, true);
- UniValue rs = find_value(prevOut, "redeemScript");
- UniValue ws = find_value(prevOut, "witnessScript");
+ const UniValue& rs{prevOut.find_value("redeemScript")};
+ const UniValue& ws{prevOut.find_value("witnessScript")};
if (rs.isNull() && ws.isNull()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Missing redeemScript/witnessScript");
}
diff --git a/src/rpc/request.cpp b/src/rpc/request.cpp
index ad91ed0f23..cf1b6cd92b 100644
--- a/src/rpc/request.cpp
+++ b/src/rpc/request.cpp
@@ -165,10 +165,10 @@ void JSONRPCRequest::parse(const UniValue& valRequest)
const UniValue& request = valRequest.get_obj();
// Parse id now so errors from here on will have the id
- id = find_value(request, "id");
+ id = request.find_value("id");
// Parse method
- UniValue valMethod = find_value(request, "method");
+ const UniValue& valMethod{request.find_value("method")};
if (valMethod.isNull())
throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
if (!valMethod.isStr())
@@ -181,7 +181,7 @@ void JSONRPCRequest::parse(const UniValue& valRequest)
LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s\n", SanitizeString(strMethod), this->authUser);
// Parse params
- UniValue valParams = find_value(request, "params");
+ const UniValue& valParams{request.find_value("params")};
if (valParams.isArray() || valParams.isObject())
params = valParams;
else if (valParams.isNull())
diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp
index 1f3f37d0a0..81489d7cec 100644
--- a/src/rpc/util.cpp
+++ b/src/rpc/util.cpp
@@ -37,7 +37,7 @@ void RPCTypeCheckObj(const UniValue& o,
bool fStrict)
{
for (const auto& t : typesExpected) {
- const UniValue& v = find_value(o, t.first);
+ const UniValue& v = o.find_value(t.first);
if (!fAllowNull && v.isNull())
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));
@@ -81,7 +81,7 @@ uint256 ParseHashV(const UniValue& v, std::string strName)
}
uint256 ParseHashO(const UniValue& o, std::string strKey)
{
- return ParseHashV(find_value(o, strKey), strKey);
+ return ParseHashV(o.find_value(strKey), strKey);
}
std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName)
{
@@ -94,7 +94,7 @@ std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName)
}
std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey)
{
- return ParseHexV(find_value(o, strKey), strKey);
+ return ParseHexV(o.find_value(strKey), strKey);
}
namespace {
@@ -1133,10 +1133,10 @@ std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, Fl
if (scanobject.isStr()) {
desc_str = scanobject.get_str();
} else if (scanobject.isObject()) {
- UniValue desc_uni = find_value(scanobject, "desc");
+ const UniValue& desc_uni{scanobject.find_value("desc")};
if (desc_uni.isNull()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor needs to be provided in scan object");
desc_str = desc_uni.get_str();
- UniValue range_uni = find_value(scanobject, "range");
+ const UniValue& range_uni{scanobject.find_value("range")};
if (!range_uni.isNull()) {
range = ParseDescriptorRange(range_uni);
}
diff --git a/src/streams.h b/src/streams.h
index 8788343809..e346aa0a3f 100644
--- a/src/streams.h
+++ b/src/streams.h
@@ -756,15 +756,25 @@ public:
}
//! search for a given byte in the stream, and remain positioned on it
- void FindByte(uint8_t ch)
+ void FindByte(std::byte byte)
{
+ // For best performance, avoid mod operation within the loop.
+ size_t buf_offset{size_t(m_read_pos % uint64_t(vchBuf.size()))};
while (true) {
- if (m_read_pos == nSrcPos)
+ if (m_read_pos == nSrcPos) {
+ // No more bytes available; read from the file into the buffer,
+ // setting nSrcPos to one beyond the end of the new data.
+ // Throws exception if end-of-file reached.
Fill();
- if (vchBuf[m_read_pos % vchBuf.size()] == std::byte{ch}) {
- break;
}
- m_read_pos++;
+ const size_t len{std::min<size_t>(vchBuf.size() - buf_offset, nSrcPos - m_read_pos)};
+ const auto it_start{vchBuf.begin() + buf_offset};
+ const auto it_find{std::find(it_start, it_start + len, byte)};
+ const size_t inc{size_t(std::distance(it_start, it_find))};
+ m_read_pos += inc;
+ if (inc < len) break;
+ buf_offset += inc;
+ if (buf_offset >= vchBuf.size()) buf_offset = 0;
}
}
};
diff --git a/src/test/argsman_tests.cpp b/src/test/argsman_tests.cpp
index 4d6ef206c7..48bffc4ac9 100644
--- a/src/test/argsman_tests.cpp
+++ b/src/test/argsman_tests.cpp
@@ -255,7 +255,7 @@ BOOST_AUTO_TEST_CASE(util_ParseInvalidParameters)
BOOST_CHECK(!test.ParseParameters(2, (char**)argv, error));
BOOST_CHECK_EQUAL(error, "Invalid parameter -unregistered");
- // Make sure registered parameters prefixed with a chain name trigger errors.
+ // Make sure registered parameters prefixed with a chain type trigger errors.
// (Previously, they were accepted and ignored.)
argv[1] = "-test.registered";
BOOST_CHECK(!test.ParseParameters(2, (char**)argv, error));
diff --git a/src/test/fuzz/buffered_file.cpp b/src/test/fuzz/buffered_file.cpp
index 67cac8fa4e..2f7ce60c7f 100644
--- a/src/test/fuzz/buffered_file.cpp
+++ b/src/test/fuzz/buffered_file.cpp
@@ -53,7 +53,7 @@ FUZZ_TARGET(buffered_file)
return;
}
try {
- opt_buffered_file->FindByte(fuzzed_data_provider.ConsumeIntegral<uint8_t>());
+ opt_buffered_file->FindByte(std::byte(fuzzed_data_provider.ConsumeIntegral<uint8_t>()));
} catch (const std::ios_base::failure&) {
}
},
diff --git a/src/test/fuzz/rpc.cpp b/src/test/fuzz/rpc.cpp
index e1f378ceff..33ffcc4cdd 100644
--- a/src/test/fuzz/rpc.cpp
+++ b/src/test/fuzz/rpc.cpp
@@ -364,7 +364,7 @@ FUZZ_TARGET_INIT(rpc, initialize_rpc)
try {
rpc_testing_setup->CallRPC(rpc_command, arguments);
} catch (const UniValue& json_rpc_error) {
- const std::string error_msg{find_value(json_rpc_error, "message").get_str()};
+ const std::string error_msg{json_rpc_error.find_value("message").get_str()};
// Once c++20 is allowed, starts_with can be used.
// if (error_msg.starts_with("Internal bug detected")) {
if (0 == error_msg.rfind("Internal bug detected", 0)) {
diff --git a/src/test/interfaces_tests.cpp b/src/test/interfaces_tests.cpp
index d37fe35a11..68377e197f 100644
--- a/src/test/interfaces_tests.cpp
+++ b/src/test/interfaces_tests.cpp
@@ -98,7 +98,7 @@ BOOST_AUTO_TEST_CASE(findAncestorByHash)
BOOST_AUTO_TEST_CASE(findCommonAncestor)
{
auto& chain = m_node.chain;
- const CChain& active = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return Assert(m_node.chainman)->ActiveChain());
+ const CChain& active{*WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return &Assert(m_node.chainman)->ActiveChain())};
auto* orig_tip = active.Tip();
for (int i = 0; i < 10; ++i) {
BlockValidationState state;
diff --git a/src/test/key_io_tests.cpp b/src/test/key_io_tests.cpp
index fa3430fc89..92bdbae3c4 100644
--- a/src/test/key_io_tests.cpp
+++ b/src/test/key_io_tests.cpp
@@ -37,11 +37,11 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse)
std::string exp_base58string = test[0].get_str();
const std::vector<std::byte> exp_payload{ParseHex<std::byte>(test[1].get_str())};
const UniValue &metadata = test[2].get_obj();
- bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
- SelectParams(ChainTypeFromString(find_value(metadata, "chain").get_str()).value());
- bool try_case_flip = find_value(metadata, "tryCaseFlip").isNull() ? false : find_value(metadata, "tryCaseFlip").get_bool();
+ bool isPrivkey = metadata.find_value("isPrivkey").get_bool();
+ SelectParams(ChainTypeFromString(metadata.find_value("chain").get_str()).value());
+ bool try_case_flip = metadata.find_value("tryCaseFlip").isNull() ? false : metadata.find_value("tryCaseFlip").get_bool();
if (isPrivkey) {
- bool isCompressed = find_value(metadata, "isCompressed").get_bool();
+ bool isCompressed = metadata.find_value("isCompressed").get_bool();
// Must be valid private key
privkey = DecodeSecret(exp_base58string);
BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest);
@@ -96,10 +96,10 @@ BOOST_AUTO_TEST_CASE(key_io_valid_gen)
std::string exp_base58string = test[0].get_str();
std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str());
const UniValue &metadata = test[2].get_obj();
- bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
- SelectParams(ChainTypeFromString(find_value(metadata, "chain").get_str()).value());
+ bool isPrivkey = metadata.find_value("isPrivkey").get_bool();
+ SelectParams(ChainTypeFromString(metadata.find_value("chain").get_str()).value());
if (isPrivkey) {
- bool isCompressed = find_value(metadata, "isCompressed").get_bool();
+ bool isCompressed = metadata.find_value("isCompressed").get_bool();
CKey key;
key.Set(exp_payload.begin(), exp_payload.end(), isCompressed);
assert(key.IsValid());
diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp
index 791c9ddf31..da0029c737 100644
--- a/src/test/rpc_tests.cpp
+++ b/src/test/rpc_tests.cpp
@@ -75,7 +75,7 @@ UniValue RPCTestingSetup::CallRPC(std::string args)
return result;
}
catch (const UniValue& objError) {
- throw std::runtime_error(find_value(objError, "message").get_str());
+ throw std::runtime_error(objError.find_value("message").get_str());
}
}
@@ -130,9 +130,9 @@ BOOST_AUTO_TEST_CASE(rpc_rawparams)
BOOST_CHECK_THROW(CallRPC("decoderawtransaction DEADBEEF"), std::runtime_error);
std::string rawtx = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("decoderawtransaction ")+rawtx));
- BOOST_CHECK_EQUAL(find_value(r.get_obj(), "size").getInt<int>(), 193);
- BOOST_CHECK_EQUAL(find_value(r.get_obj(), "version").getInt<int>(), 1);
- BOOST_CHECK_EQUAL(find_value(r.get_obj(), "locktime").getInt<int>(), 0);
+ BOOST_CHECK_EQUAL(r.get_obj().find_value("size").getInt<int>(), 193);
+ BOOST_CHECK_EQUAL(r.get_obj().find_value("version").getInt<int>(), 1);
+ BOOST_CHECK_EQUAL(r.get_obj().find_value("locktime").getInt<int>(), 0);
BOOST_CHECK_THROW(CallRPC(std::string("decoderawtransaction ")+rawtx+" extra"), std::runtime_error);
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("decoderawtransaction ")+rawtx+" false"));
BOOST_CHECK_THROW(r = CallRPC(std::string("decoderawtransaction ")+rawtx+" false extra"), std::runtime_error);
@@ -149,20 +149,20 @@ BOOST_AUTO_TEST_CASE(rpc_togglenetwork)
UniValue r;
r = CallRPC("getnetworkinfo");
- bool netState = find_value(r.get_obj(), "networkactive").get_bool();
+ bool netState = r.get_obj().find_value("networkactive").get_bool();
BOOST_CHECK_EQUAL(netState, true);
BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive false"));
r = CallRPC("getnetworkinfo");
- int numConnection = find_value(r.get_obj(), "connections").getInt<int>();
+ int numConnection = r.get_obj().find_value("connections").getInt<int>();
BOOST_CHECK_EQUAL(numConnection, 0);
- netState = find_value(r.get_obj(), "networkactive").get_bool();
+ netState = r.get_obj().find_value("networkactive").get_bool();
BOOST_CHECK_EQUAL(netState, false);
BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive true"));
r = CallRPC("getnetworkinfo");
- netState = find_value(r.get_obj(), "networkactive").get_bool();
+ netState = r.get_obj().find_value("networkactive").get_bool();
BOOST_CHECK_EQUAL(netState, true);
}
@@ -180,9 +180,9 @@ BOOST_AUTO_TEST_CASE(rpc_rawsign)
std::string privkey1 = "\"KzsXybp9jX64P5ekX1KUxRQ79Jht9uzW7LorgwE65i5rWACL6LQe\"";
std::string privkey2 = "\"Kyhdf5LuKTRx4ge69ybABsiUAWjVRK4XGxAKk2FQLp2HjGMy87Z4\"";
r = CallRPC(std::string("signrawtransactionwithkey ")+notsigned+" [] "+prevout);
- BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == false);
+ BOOST_CHECK(r.get_obj().find_value("complete").get_bool() == false);
r = CallRPC(std::string("signrawtransactionwithkey ")+notsigned+" ["+privkey1+","+privkey2+"] "+prevout);
- BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == true);
+ BOOST_CHECK(r.get_obj().find_value("complete").get_bool() == true);
}
BOOST_AUTO_TEST_CASE(rpc_createraw_op_return)
@@ -325,7 +325,7 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
UniValue ar = r.get_array();
UniValue o1 = ar[0].get_obj();
- UniValue adr = find_value(o1, "address");
+ UniValue adr = o1.find_value("address");
BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/32");
BOOST_CHECK_NO_THROW(CallRPC(std::string("setban 127.0.0.0 remove")));
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
@@ -336,8 +336,8 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
ar = r.get_array();
o1 = ar[0].get_obj();
- adr = find_value(o1, "address");
- int64_t banned_until{find_value(o1, "banned_until").getInt<int64_t>()};
+ adr = o1.find_value("address");
+ int64_t banned_until{o1.find_value("banned_until").getInt<int64_t>()};
BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/24");
BOOST_CHECK_EQUAL(banned_until, 9907731200); // absolute time check
@@ -351,11 +351,11 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
ar = r.get_array();
o1 = ar[0].get_obj();
- adr = find_value(o1, "address");
- banned_until = find_value(o1, "banned_until").getInt<int64_t>();
- const int64_t ban_created{find_value(o1, "ban_created").getInt<int64_t>()};
- const int64_t ban_duration{find_value(o1, "ban_duration").getInt<int64_t>()};
- const int64_t time_remaining{find_value(o1, "time_remaining").getInt<int64_t>()};
+ adr = o1.find_value("address");
+ banned_until = o1.find_value("banned_until").getInt<int64_t>();
+ const int64_t ban_created{o1.find_value("ban_created").getInt<int64_t>()};
+ const int64_t ban_duration{o1.find_value("ban_duration").getInt<int64_t>()};
+ const int64_t time_remaining{o1.find_value("time_remaining").getInt<int64_t>()};
BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/24");
BOOST_CHECK_EQUAL(banned_until, time_remaining_expected + now.count());
BOOST_CHECK_EQUAL(ban_duration, banned_until - ban_created);
@@ -385,7 +385,7 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
ar = r.get_array();
o1 = ar[0].get_obj();
- adr = find_value(o1, "address");
+ adr = o1.find_value("address");
BOOST_CHECK_EQUAL(adr.get_str(), "fe80::202:b3ff:fe1e:8329/128");
BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
@@ -393,7 +393,7 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
ar = r.get_array();
o1 = ar[0].get_obj();
- adr = find_value(o1, "address");
+ adr = o1.find_value("address");
BOOST_CHECK_EQUAL(adr.get_str(), "2001:db8::/30");
BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
@@ -401,7 +401,7 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
ar = r.get_array();
o1 = ar[0].get_obj();
- adr = find_value(o1, "address");
+ adr = o1.find_value("address");
BOOST_CHECK_EQUAL(adr.get_str(), "2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/128");
}
diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp
index a0392570f1..55e4f200b1 100644
--- a/src/test/streams_tests.cpp
+++ b/src/test/streams_tests.cpp
@@ -463,7 +463,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
size_t find = currentPos + InsecureRandRange(8);
if (find >= fileSize)
find = fileSize - 1;
- bf.FindByte(uint8_t(find));
+ bf.FindByte(std::byte(find));
// The value at each offset is the offset.
BOOST_CHECK_EQUAL(bf.GetPos(), find);
currentPos = find;
diff --git a/src/test/system_tests.cpp b/src/test/system_tests.cpp
index c0a2566959..7ce350b84b 100644
--- a/src/test/system_tests.cpp
+++ b/src/test/system_tests.cpp
@@ -52,7 +52,7 @@ BOOST_AUTO_TEST_CASE(run_command)
const UniValue result = RunCommandParseJSON("echo \"{\"success\": true}\"");
#endif
BOOST_CHECK(result.isObject());
- const UniValue& success = find_value(result, "success");
+ const UniValue& success = result.find_value("success");
BOOST_CHECK(!success.isNull());
BOOST_CHECK_EQUAL(success.get_bool(), true);
}
@@ -106,7 +106,7 @@ BOOST_AUTO_TEST_CASE(run_command)
{
const UniValue result = RunCommandParseJSON("cat", "{\"success\": true}");
BOOST_CHECK(result.isObject());
- const UniValue& success = find_value(result, "success");
+ const UniValue& success = result.find_value("success");
BOOST_CHECK(!success.isNull());
BOOST_CHECK_EQUAL(success.get_bool(), true);
}
diff --git a/src/univalue/include/univalue.h b/src/univalue/include/univalue.h
index d501c3fb69..004135ef97 100644
--- a/src/univalue/include/univalue.h
+++ b/src/univalue/include/univalue.h
@@ -123,7 +123,7 @@ public:
const UniValue& get_array() const;
enum VType type() const { return getType(); }
- friend const UniValue& find_value( const UniValue& obj, const std::string& name);
+ const UniValue& find_value(std::string_view key) const;
};
template <class It>
@@ -201,6 +201,4 @@ static inline bool json_isspace(int ch)
extern const UniValue NullUniValue;
-const UniValue& find_value( const UniValue& obj, const std::string& name);
-
#endif // BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_H
diff --git a/src/univalue/lib/univalue.cpp b/src/univalue/lib/univalue.cpp
index 5aa39edb75..c3d19caae0 100644
--- a/src/univalue/lib/univalue.cpp
+++ b/src/univalue/lib/univalue.cpp
@@ -230,12 +230,13 @@ const char *uvTypeName(UniValue::VType t)
return nullptr;
}
-const UniValue& find_value(const UniValue& obj, const std::string& name)
+const UniValue& UniValue::find_value(std::string_view key) const
{
- for (unsigned int i = 0; i < obj.keys.size(); i++)
- if (obj.keys[i] == name)
- return obj.values.at(i);
-
+ for (unsigned int i = 0; i < keys.size(); ++i) {
+ if (keys[i] == key) {
+ return values.at(i);
+ }
+ }
return NullUniValue;
}
diff --git a/src/validation.cpp b/src/validation.cpp
index 206d89fe7c..e536dfb4eb 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -4562,7 +4562,7 @@ void Chainstate::LoadExternalBlockFile(
try {
// locate a header
unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
- blkdat.FindByte(params.MessageStart()[0]);
+ blkdat.FindByte(std::byte(params.MessageStart()[0]));
nRewind = blkdat.GetPos() + 1;
blkdat >> buf;
if (memcmp(buf, params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {
diff --git a/src/wallet/rpc/coins.cpp b/src/wallet/rpc/coins.cpp
index 750ef69f6e..5bdd3c9e72 100644
--- a/src/wallet/rpc/coins.cpp
+++ b/src/wallet/rpc/coins.cpp
@@ -320,7 +320,7 @@ RPCHelpMan lockunspent()
});
const uint256 txid(ParseHashO(o, "txid"));
- const int nOutput = find_value(o, "vout").getInt<int>();
+ const int nOutput = o.find_value("vout").getInt<int>();
if (nOutput < 0) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative");
}
diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp
index 88ee6e96b0..a5b1f594bf 100644
--- a/src/wallet/rpc/spend.cpp
+++ b/src/wallet/rpc/spend.cpp
@@ -673,7 +673,7 @@ void FundTransaction(CWallet& wallet, CMutableTransaction& tx, CAmount& fee_out,
for (const UniValue& input : options["input_weights"].get_array().getValues()) {
uint256 txid = ParseHashO(input, "txid");
- const UniValue& vout_v = find_value(input, "vout");
+ const UniValue& vout_v = input.find_value("vout");
if (!vout_v.isNum()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
}
@@ -682,7 +682,7 @@ void FundTransaction(CWallet& wallet, CMutableTransaction& tx, CAmount& fee_out,
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative");
}
- const UniValue& weight_v = find_value(input, "weight");
+ const UniValue& weight_v = input.find_value("weight");
if (!weight_v.isNum()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing weight key");
}
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index d97c1bbefe..b3eed8abc7 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -3589,7 +3589,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
if (!signer_res.isObject()) throw std::runtime_error(std::string(__func__) + ": Unexpected result");
for (bool internal : {false, true}) {
- const UniValue& descriptor_vals = find_value(signer_res, internal ? "internal" : "receive");
+ const UniValue& descriptor_vals = signer_res.find_value(internal ? "internal" : "receive");
if (!descriptor_vals.isArray()) throw std::runtime_error(std::string(__func__) + ": Unexpected result");
for (const UniValue& desc_val : descriptor_vals.get_array().getValues()) {
const std::string& desc_str = desc_val.getValStr();