rpc: refactor: use string_view in Arg/MaybeArg
Modernizes interface by not forcing users to deal with raw pointers, without adding copying overhead. Generalizes the logic of whether we return by value or by optional/pointer. In cases where functions take a `const std::string&` and it would be too much work to update them, a string copy is made (which was already happening anyway).
This commit is contained in:
18 files changed
+51
-46
No files matched your search
@@ -196,8 +196,7 @@ RPCHelpMan getbalance()
|
||||
|
||||
LOCK(pwallet->cs_wallet);
|
||||
|
||||
const auto dummy_value{self.MaybeArg<std::string>("dummy")};
|
||||
if (dummy_value && *dummy_value != "*") {
|
||||
if (self.MaybeArg<std::string_view>("dummy").value_or("*") != "*") {
|
||||
throw JSONRPCError(RPC_METHOD_DEPRECATED, "dummy first argument must be excluded or set to \"*\".");
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <wallet/context.h>
|
||||
#include <wallet/wallet.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <univalue.h>
|
||||
|
||||
@@ -29,7 +30,7 @@ bool GetAvoidReuseFlag(const CWallet& wallet, const UniValue& param) {
|
||||
return avoid_reuse;
|
||||
}
|
||||
|
||||
std::string EnsureUniqueWalletName(const JSONRPCRequest& request, const std::string* wallet_name)
|
||||
std::string EnsureUniqueWalletName(const JSONRPCRequest& request, std::optional<std::string_view> wallet_name)
|
||||
{
|
||||
std::string endpoint_wallet;
|
||||
if (GetWalletNameFromJSONRPCRequest(request, endpoint_wallet)) {
|
||||
@@ -47,7 +48,7 @@ std::string EnsureUniqueWalletName(const JSONRPCRequest& request, const std::str
|
||||
"Either the RPC endpoint wallet or the wallet name parameter must be provided");
|
||||
}
|
||||
|
||||
return *wallet_name;
|
||||
return std::string{*wallet_name};
|
||||
}
|
||||
|
||||
bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name)
|
||||
|
||||
@@ -11,7 +11,9 @@
|
||||
|
||||
#include <any>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
class JSONRPCRequest;
|
||||
@@ -42,7 +44,7 @@ bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string&
|
||||
* Ensures that a wallet name is specified across the endpoint and wallet_name.
|
||||
* Throws `RPC_INVALID_PARAMETER` if none or different wallet names are specified.
|
||||
*/
|
||||
std::string EnsureUniqueWalletName(const JSONRPCRequest& request, const std::string* wallet_name);
|
||||
std::string EnsureUniqueWalletName(const JSONRPCRequest& request, std::optional<std::string_view> wallet_name);
|
||||
|
||||
void EnsureWalletIsUnlocked(const CWallet&);
|
||||
WalletContext& EnsureWalletContext(const std::any& context);
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <wallet/walletutil.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
|
||||
|
||||
namespace wallet {
|
||||
@@ -456,7 +457,7 @@ static RPCHelpMan unloadwallet()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
const std::string wallet_name{EnsureUniqueWalletName(request, self.MaybeArg<std::string>("wallet_name"))};
|
||||
const std::string wallet_name{EnsureUniqueWalletName(request, self.MaybeArg<std::string_view>("wallet_name"))};
|
||||
|
||||
WalletContext& context = EnsureWalletContext(request.context);
|
||||
std::shared_ptr<CWallet> wallet = GetWallet(context, wallet_name);
|
||||
@@ -613,7 +614,7 @@ static RPCHelpMan migratewallet()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
const std::string wallet_name{EnsureUniqueWalletName(request, self.MaybeArg<std::string>("wallet_name"))};
|
||||
const std::string wallet_name{EnsureUniqueWalletName(request, self.MaybeArg<std::string_view>("wallet_name"))};
|
||||
|
||||
SecureString wallet_pass;
|
||||
wallet_pass.reserve(100);
|
||||
|
||||
@@ -17,7 +17,7 @@ static std::string TestWalletName(const std::string& endpoint, std::optional<std
|
||||
{
|
||||
JSONRPCRequest req;
|
||||
req.URI = endpoint;
|
||||
return EnsureUniqueWalletName(req, parameter ? &*parameter : nullptr);
|
||||
return EnsureUniqueWalletName(req, parameter);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(wallet_rpc_tests, BasicTestingSetup)
|
||||
|
||||
Reference in new issue
Block a user