0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-05 14:06:27 -05:00

Merge #21366: refactor: replace util::Ref with std::any (C++17)

916ab0195d remove unused class util::Ref and its unit test (Sebastian Falbesoner)
8dbb87a393 refactor: replace util::Ref by std::any (C++17) (Sebastian Falbesoner)
95cccf8a4b util: introduce helper AnyPtr to access std::any instances (Sebastian Falbesoner)

Pull request description:

  As described in `util/ref.h`: "_This implements a small subset of the functionality in C++17's std::any class, and **can be dropped when the project updates to C++17**_". For accessing the contained object of a `std::any` instance, a helper template function `AnyPtr` is introduced (thanks to ryanofsky).

ACKs for top commit:
  hebasto:
    re-ACK 916ab0195d, with command
  ryanofsky:
    Code review ACK 916ab0195d. Changes since last review: rebase and replacing types with `auto`. I might have used `const auto*` and `auto*` instead of plain `auto` because I think the qualifiers are useful, but this is all good.

Tree-SHA512: fe2c3e4f5726f8ad40c61128339bb24ad11d2c261f71f7b934b1efe3e3279df14046452b0d9b566917ef61d5c7e0fd96ccbf35ff810357e305710f5002c27d47
This commit is contained in:
W. J. van der Laan 2021-03-31 19:56:56 +02:00
commit 602b038d43
No known key found for this signature in database
GPG key ID: 1E4AED62986CD25D
23 changed files with 90 additions and 156 deletions

View file

@ -243,7 +243,6 @@ BITCOIN_CORE_H = \
util/moneystr.h \
util/rbf.h \
util/readwritefile.h \
util/ref.h \
util/settings.h \
util/sock.h \
util/spanparsing.h \

View file

@ -112,7 +112,6 @@ BITCOIN_TESTS =\
test/prevector_tests.cpp \
test/raii_event_tests.cpp \
test/random_tests.cpp \
test/ref_tests.cpp \
test/reverselock_tests.cpp \
test/rpc_tests.cpp \
test/sanity_tests.cpp \

View file

@ -16,7 +16,7 @@
#include <node/ui_interface.h>
#include <noui.h>
#include <shutdown.h>
#include <util/ref.h>
#include <util/check.h>
#include <util/strencodings.h>
#include <util/system.h>
#include <util/threadnames.h>
@ -24,6 +24,7 @@
#include <util/translation.h>
#include <util/url.h>
#include <any>
#include <functional>
#include <optional>
@ -142,7 +143,7 @@ static bool AppInit(int argc, char* argv[])
// end, which is interpreted as failure to start.
TokenPipeEnd daemon_ep;
#endif
util::Ref context{node};
std::any context{&node};
try
{
if (!CheckDataDirOption()) {

View file

@ -144,7 +144,7 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
return multiUserAuthorized(strUserPass);
}
static bool HTTPReq_JSONRPC(const util::Ref& context, HTTPRequest* req)
static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
{
// JSONRPC handles only POST
if (req->GetRequestMethod() != HTTPRequest::POST) {
@ -288,7 +288,7 @@ static bool InitRPCAuthentication()
return true;
}
bool StartHTTPRPC(const util::Ref& context)
bool StartHTTPRPC(const std::any& context)
{
LogPrint(BCLog::RPC, "Starting HTTP RPC server\n");
if (!InitRPCAuthentication())

View file

@ -5,14 +5,12 @@
#ifndef BITCOIN_HTTPRPC_H
#define BITCOIN_HTTPRPC_H
namespace util {
class Ref;
} // namespace util
#include <any>
/** Start HTTP RPC subsystem.
* Precondition; HTTP and RPC has been started.
*/
bool StartHTTPRPC(const util::Ref& context);
bool StartHTTPRPC(const std::any& context);
/** Interrupt HTTP RPC subsystem.
*/
void InterruptHTTPRPC();
@ -24,7 +22,7 @@ void StopHTTPRPC();
/** Start HTTP REST subsystem.
* Precondition; HTTP and RPC has been started.
*/
void StartREST(const util::Ref& context);
void StartREST(const std::any& context);
/** Interrupt RPC REST subsystem.
*/
void InterruptREST();

View file

@ -788,7 +788,7 @@ static bool InitSanityCheck()
return true;
}
static bool AppInitServers(const util::Ref& context, NodeContext& node)
static bool AppInitServers(const std::any& context, NodeContext& node)
{
const ArgsManager& args = *Assert(node.args);
RPCServer::OnStarted(&OnRPCStarted);
@ -1277,7 +1277,7 @@ bool AppInitInterfaces(NodeContext& node)
return true;
}
bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
bool AppInitMain(const std::any& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
{
const ArgsManager& args = *Assert(node.args);
const CChainParams& chainparams = Params();

View file

@ -6,6 +6,7 @@
#ifndef BITCOIN_INIT_H
#define BITCOIN_INIT_H
#include <any>
#include <memory>
#include <string>
@ -22,9 +23,6 @@ struct BlockAndHeaderTipInfo;
namespace boost {
class thread_group;
} // namespace boost
namespace util {
class Ref;
} // namespace util
/** Interrupt threads */
void Interrupt(NodeContext& node);
@ -66,7 +64,7 @@ bool AppInitInterfaces(NodeContext& node);
* @note This should only be done after daemonization. Call Shutdown() if this function fails.
* @pre Parameters should be parsed and config file should be read, AppInitLockDataDirectory should have been called.
*/
bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr);
bool AppInitMain(const std::any& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr);
/**
* Register all arguments with the ArgsManager

View file

@ -38,7 +38,6 @@
#include <uint256.h>
#include <univalue.h>
#include <util/check.h>
#include <util/ref.h>
#include <util/system.h>
#include <util/translation.h>
#include <validation.h>
@ -49,6 +48,7 @@
#include <config/bitcoin-config.h>
#endif
#include <any>
#include <memory>
#include <optional>
#include <utility>
@ -298,13 +298,13 @@ public:
{
m_context = context;
if (context) {
m_context_ref.Set(*context);
m_context_ref = context;
} else {
m_context_ref.Clear();
m_context_ref.reset();
}
}
NodeContext* m_context{nullptr};
util::Ref m_context_ref;
std::any m_context_ref;
};
bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active)

View file

@ -18,10 +18,12 @@
#include <sync.h>
#include <txmempool.h>
#include <util/check.h>
#include <util/ref.h>
#include <util/system.h>
#include <validation.h>
#include <version.h>
#include <any>
#include <boost/algorithm/string.hpp>
#include <univalue.h>
@ -73,10 +75,10 @@ static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string me
* context is not found.
* @returns Pointer to the node context or nullptr if not found.
*/
static NodeContext* GetNodeContext(const util::Ref& context, HTTPRequest* req)
static NodeContext* GetNodeContext(const std::any& context, HTTPRequest* req)
{
NodeContext* node = context.Has<NodeContext>() ? &context.Get<NodeContext>() : nullptr;
if (!node) {
auto node_context = util::AnyPtr<NodeContext>(context);
if (!node_context) {
RESTERR(req, HTTP_INTERNAL_SERVER_ERROR,
strprintf("%s:%d (%s)\n"
"Internal bug detected: Node context not found!\n"
@ -84,7 +86,7 @@ static NodeContext* GetNodeContext(const util::Ref& context, HTTPRequest* req)
__FILE__, __LINE__, __func__, PACKAGE_BUGREPORT));
return nullptr;
}
return node;
return node_context;
}
/**
@ -94,14 +96,14 @@ static NodeContext* GetNodeContext(const util::Ref& context, HTTPRequest* req)
* context mempool is not found.
* @returns Pointer to the mempool or nullptr if no mempool found.
*/
static CTxMemPool* GetMemPool(const util::Ref& context, HTTPRequest* req)
static CTxMemPool* GetMemPool(const std::any& context, HTTPRequest* req)
{
NodeContext* node = context.Has<NodeContext>() ? &context.Get<NodeContext>() : nullptr;
if (!node || !node->mempool) {
auto node_context = util::AnyPtr<NodeContext>(context);
if (!node_context || !node_context->mempool) {
RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found");
return nullptr;
}
return node->mempool.get();
return node_context->mempool.get();
}
static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
@ -151,7 +153,7 @@ static bool CheckWarmup(HTTPRequest* req)
return true;
}
static bool rest_headers(const util::Ref& context,
static bool rest_headers(const std::any& context,
HTTPRequest* req,
const std::string& strURIPart)
{
@ -293,12 +295,12 @@ static bool rest_block(HTTPRequest* req,
}
}
static bool rest_block_extended(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
static bool rest_block_extended(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
{
return rest_block(req, strURIPart, true);
}
static bool rest_block_notxdetails(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
static bool rest_block_notxdetails(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
{
return rest_block(req, strURIPart, false);
}
@ -306,7 +308,7 @@ static bool rest_block_notxdetails(const util::Ref& context, HTTPRequest* req, c
// A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
RPCHelpMan getblockchaininfo();
static bool rest_chaininfo(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
static bool rest_chaininfo(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
{
if (!CheckWarmup(req))
return false;
@ -329,7 +331,7 @@ static bool rest_chaininfo(const util::Ref& context, HTTPRequest* req, const std
}
}
static bool rest_mempool_info(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
static bool rest_mempool_info(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
{
if (!CheckWarmup(req))
return false;
@ -353,7 +355,7 @@ static bool rest_mempool_info(const util::Ref& context, HTTPRequest* req, const
}
}
static bool rest_mempool_contents(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
static bool rest_mempool_contents(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
{
if (!CheckWarmup(req)) return false;
const CTxMemPool* mempool = GetMemPool(context, req);
@ -376,7 +378,7 @@ static bool rest_mempool_contents(const util::Ref& context, HTTPRequest* req, co
}
}
static bool rest_tx(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
{
if (!CheckWarmup(req))
return false;
@ -435,7 +437,7 @@ static bool rest_tx(const util::Ref& context, HTTPRequest* req, const std::strin
}
}
static bool rest_getutxos(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
{
if (!CheckWarmup(req))
return false;
@ -621,7 +623,7 @@ static bool rest_getutxos(const util::Ref& context, HTTPRequest* req, const std:
}
}
static bool rest_blockhash_by_height(const util::Ref& context, HTTPRequest* req,
static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
const std::string& str_uri_part)
{
if (!CheckWarmup(req)) return false;
@ -669,7 +671,7 @@ static bool rest_blockhash_by_height(const util::Ref& context, HTTPRequest* req,
static const struct {
const char* prefix;
bool (*handler)(const util::Ref& context, HTTPRequest* req, const std::string& strReq);
bool (*handler)(const std::any& context, HTTPRequest* req, const std::string& strReq);
} uri_prefixes[] = {
{"/rest/tx/", rest_tx},
{"/rest/block/notxdetails/", rest_block_notxdetails},
@ -682,7 +684,7 @@ static const struct {
{"/rest/blockhashbyheight/", rest_blockhash_by_height},
};
void StartREST(const util::Ref& context)
void StartREST(const std::any& context)
{
for (const auto& up : uri_prefixes) {
auto handler = [&context, up](HTTPRequest* req, const std::string& prefix) { return up.handler(context, req, prefix); };

View file

@ -30,7 +30,6 @@
#include <txdb.h>
#include <txmempool.h>
#include <undo.h>
#include <util/ref.h>
#include <util/strencodings.h>
#include <util/system.h>
#include <util/translation.h>
@ -56,15 +55,16 @@ static Mutex cs_blockchange;
static std::condition_variable cond_blockchange;
static CUpdatedBlock latestblock GUARDED_BY(cs_blockchange);
NodeContext& EnsureNodeContext(const util::Ref& context)
NodeContext& EnsureNodeContext(const std::any& context)
{
if (!context.Has<NodeContext>()) {
auto node_context = util::AnyPtr<NodeContext>(context);
if (!node_context) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
}
return context.Get<NodeContext>();
return *node_context;
}
CTxMemPool& EnsureMemPool(const util::Ref& context)
CTxMemPool& EnsureMemPool(const std::any& context)
{
const NodeContext& node = EnsureNodeContext(context);
if (!node.mempool) {
@ -73,7 +73,7 @@ CTxMemPool& EnsureMemPool(const util::Ref& context)
return *node.mempool;
}
ChainstateManager& EnsureChainman(const util::Ref& context)
ChainstateManager& EnsureChainman(const std::any& context)
{
const NodeContext& node = EnsureNodeContext(context);
if (!node.chainman) {
@ -82,7 +82,7 @@ ChainstateManager& EnsureChainman(const util::Ref& context)
return *node.chainman;
}
CBlockPolicyEstimator& EnsureFeeEstimator(const util::Ref& context)
CBlockPolicyEstimator& EnsureFeeEstimator(const std::any& context)
{
NodeContext& node = EnsureNodeContext(context);
if (!node.fee_estimator) {

View file

@ -10,6 +10,7 @@
#include <streams.h>
#include <sync.h>
#include <any>
#include <stdint.h>
#include <vector>
@ -23,9 +24,6 @@ class CTxMemPool;
class ChainstateManager;
class UniValue;
struct NodeContext;
namespace util {
class Ref;
} // namespace util
static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5;
@ -58,10 +56,10 @@ void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES],
void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);
void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex = true, int serialize_flags = 0, const CTxUndo* txundo = nullptr);
NodeContext& EnsureNodeContext(const util::Ref& context);
CTxMemPool& EnsureMemPool(const util::Ref& context);
ChainstateManager& EnsureChainman(const util::Ref& context);
CBlockPolicyEstimator& EnsureFeeEstimator(const util::Ref& context);
NodeContext& EnsureNodeContext(const std::any& context);
CTxMemPool& EnsureMemPool(const std::any& context);
ChainstateManager& EnsureChainman(const std::any& context);
CBlockPolicyEstimator& EnsureFeeEstimator(const std::any& context);
/**
* Helper to create UTXO snapshots given a chainstate and a file handle.

View file

@ -17,7 +17,6 @@
#include <script/descriptor.h>
#include <util/check.h>
#include <util/message.h> // For MessageSign(), MessageVerify()
#include <util/ref.h>
#include <util/strencodings.h>
#include <util/system.h>
@ -391,8 +390,9 @@ static RPCHelpMan setmocktime()
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Mocktime can not be negative: %s.", time));
}
SetMockTime(time);
if (request.context.Has<NodeContext>()) {
for (const auto& chain_client : request.context.Get<NodeContext>().chain_clients) {
auto node_context = util::AnyPtr<NodeContext>(request.context);
if (node_context) {
for (const auto& chain_client : node_context->chain_clients) {
chain_client->setMockTime(time);
}
}
@ -424,11 +424,11 @@ static RPCHelpMan mockscheduler()
throw std::runtime_error("delta_time must be between 1 and 3600 seconds (1 hr)");
}
auto node_context = util::AnyPtr<NodeContext>(request.context);
// protect against null pointer dereference
CHECK_NONFATAL(request.context.Has<NodeContext>());
NodeContext& node = request.context.Get<NodeContext>();
CHECK_NONFATAL(node.scheduler);
node.scheduler->MockForward(std::chrono::seconds(delta_seconds));
CHECK_NONFATAL(node_context);
CHECK_NONFATAL(node_context->scheduler);
node_context->scheduler->MockForward(std::chrono::seconds(delta_seconds));
return NullUniValue;
},

View file

@ -6,14 +6,11 @@
#ifndef BITCOIN_RPC_REQUEST_H
#define BITCOIN_RPC_REQUEST_H
#include <any>
#include <string>
#include <univalue.h>
namespace util {
class Ref;
} // namespace util
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id);
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id);
@ -38,14 +35,14 @@ public:
std::string URI;
std::string authUser;
std::string peerAddr;
const util::Ref& context;
const std::any& context;
explicit JSONRPCRequest(const util::Ref& context) : id(NullUniValue), params(NullUniValue), context(context) {}
explicit JSONRPCRequest(const std::any& context) : id(NullUniValue), params(NullUniValue), context(context) {}
//! Initializes request information from another request object and the
//! given context. The implementation should be updated if any members are
//! added or removed above.
JSONRPCRequest(const JSONRPCRequest& other, const util::Ref& context)
JSONRPCRequest(const JSONRPCRequest& other, const std::any& context)
: id(other.id), strMethod(other.strMethod), params(other.params), mode(other.mode), URI(other.URI),
authUser(other.authUser), peerAddr(other.peerAddr), context(context)
{

View file

@ -87,7 +87,7 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
vCommands.push_back(make_pair(entry.second.front()->category + entry.first, entry.second.front()));
sort(vCommands.begin(), vCommands.end());
JSONRPCRequest jreq(helpreq);
JSONRPCRequest jreq = helpreq;
jreq.mode = JSONRPCRequest::GET_HELP;
jreq.params = UniValue();
@ -494,7 +494,7 @@ std::vector<std::string> CRPCTable::listCommands() const
UniValue CRPCTable::dumpArgMap(const JSONRPCRequest& args_request) const
{
JSONRPCRequest request(args_request);
JSONRPCRequest request = args_request;
request.mode = JSONRPCRequest::GET_ARGS;
UniValue ret{UniValue::VARR};

View file

@ -1,33 +0,0 @@
// Copyright (c) 2020 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 <util/ref.h>
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(ref_tests)
BOOST_AUTO_TEST_CASE(ref_test)
{
util::Ref ref;
BOOST_CHECK(!ref.Has<int>());
BOOST_CHECK_THROW(ref.Get<int>(), NonFatalCheckError);
int value = 5;
ref.Set(value);
BOOST_CHECK(ref.Has<int>());
BOOST_CHECK_EQUAL(ref.Get<int>(), 5);
++ref.Get<int>();
BOOST_CHECK_EQUAL(ref.Get<int>(), 6);
BOOST_CHECK_EQUAL(value, 6);
++value;
BOOST_CHECK_EQUAL(value, 7);
BOOST_CHECK_EQUAL(ref.Get<int>(), 7);
BOOST_CHECK(!ref.Has<bool>());
BOOST_CHECK_THROW(ref.Get<bool>(), NonFatalCheckError);
ref.Clear();
BOOST_CHECK(!ref.Has<int>());
BOOST_CHECK_THROW(ref.Get<int>(), NonFatalCheckError);
}
BOOST_AUTO_TEST_SUITE_END()

View file

@ -10,9 +10,10 @@
#include <interfaces/chain.h>
#include <node/context.h>
#include <test/util/setup_common.h>
#include <util/ref.h>
#include <util/time.h>
#include <any>
#include <boost/algorithm/string.hpp>
#include <boost/test/unit_test.hpp>
@ -32,7 +33,7 @@ UniValue RPCTestingSetup::CallRPC(std::string args)
boost::split(vArgs, args, boost::is_any_of(" \t"));
std::string strMethod = vArgs[0];
vArgs.erase(vArgs.begin());
util::Ref context{m_node};
std::any context{&m_node};
JSONRPCRequest request(context);
request.strMethod = strMethod;
request.params = RPCConvertValues(strMethod, vArgs);

View file

@ -1,38 +0,0 @@
// Copyright (c) 2020 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_UTIL_REF_H
#define BITCOIN_UTIL_REF_H
#include <util/check.h>
#include <typeindex>
namespace util {
/**
* Type-safe dynamic reference.
*
* This implements a small subset of the functionality in C++17's std::any
* class, and can be dropped when the project updates to C++17
* (https://github.com/bitcoin/bitcoin/issues/16684)
*/
class Ref
{
public:
Ref() = default;
template<typename T> Ref(T& value) { Set(value); }
template<typename T> T& Get() const { CHECK_NONFATAL(Has<T>()); return *static_cast<T*>(m_value); }
template<typename T> void Set(T& value) { m_value = &value; m_type = std::type_index(typeid(T)); }
template<typename T> bool Has() const { return m_value && m_type == std::type_index(typeid(T)); }
void Clear() { m_value = nullptr; m_type = std::type_index(typeid(void)); }
private:
void* m_value = nullptr;
std::type_index m_type = std::type_index(typeid(void));
};
} // namespace util
#endif // BITCOIN_UTIL_REF_H

View file

@ -25,6 +25,7 @@
#include <util/threadnames.h>
#include <util/time.h>
#include <any>
#include <exception>
#include <map>
#include <optional>
@ -500,6 +501,18 @@ inline void insert(std::set<TsetT>& dst, const Tsrc& src) {
dst.insert(src.begin(), src.end());
}
/**
* Helper function to access the contained object of a std::any instance.
* Returns a pointer to the object if passed instance has a value and the type
* matches, nullptr otherwise.
*/
template<typename T>
T* AnyPtr(const std::any& any) noexcept
{
T* const* ptr = std::any_cast<T*>(&any);
return ptr ? *ptr : nullptr;
}
#ifdef WIN32
class WinCmdLineArgs
{

View file

@ -15,7 +15,6 @@
#include <sync.h>
#include <uint256.h>
#include <util/check.h>
#include <util/ref.h>
#include <util/system.h>
#include <util/ui_change_type.h>
#include <wallet/context.h>
@ -515,7 +514,7 @@ public:
{
for (const CRPCCommand& command : GetWalletRPCCommands()) {
m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) {
return command.actor({request, m_context}, result, last_handler);
return command.actor({request, &m_context}, result, last_handler);
}, command.argNames, command.unique_id);
m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back()));
}

View file

@ -22,7 +22,6 @@
#include <util/fees.h>
#include <util/message.h> // For MessageSign()
#include <util/moneystr.h>
#include <util/ref.h>
#include <util/string.h>
#include <util/system.h>
#include <util/translation.h>
@ -124,12 +123,13 @@ void EnsureWalletIsUnlocked(const CWallet& wallet)
}
}
WalletContext& EnsureWalletContext(const util::Ref& context)
WalletContext& EnsureWalletContext(const std::any& context)
{
if (!context.Has<WalletContext>()) {
auto wallet_context = util::AnyPtr<WalletContext>(context);
if (!wallet_context) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet context not found");
}
return context.Get<WalletContext>();
return *wallet_context;
}
// also_create should only be set to true only when the RPC is expected to add things to a blank wallet and make it no longer blank

View file

@ -7,6 +7,7 @@
#include <span.h>
#include <any>
#include <memory>
#include <string>
#include <vector>
@ -31,7 +32,7 @@ Span<const CRPCCommand> GetWalletRPCCommands();
std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request);
void EnsureWalletIsUnlocked(const CWallet&);
WalletContext& EnsureWalletContext(const util::Ref& context);
WalletContext& EnsureWalletContext(const std::any& context);
LegacyScriptPubKeyMan& EnsureLegacyScriptPubKeyMan(CWallet& wallet, bool also_create = false);
RPCHelpMan getaddressinfo();

View file

@ -4,6 +4,7 @@
#include <wallet/wallet.h>
#include <any>
#include <future>
#include <memory>
#include <stdint.h>
@ -15,7 +16,6 @@
#include <rpc/server.h>
#include <test/util/logging.h>
#include <test/util/setup_common.h>
#include <util/ref.h>
#include <util/translation.h>
#include <validation.h>
#include <wallet/coincontrol.h>
@ -213,7 +213,7 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1);
key.pushKV("internal", UniValue(true));
keys.push_back(key);
util::Ref context;
std::any context;
JSONRPCRequest request(context);
request.params.setArray();
request.params.push_back(keys);
@ -265,7 +265,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
AddWallet(wallet);
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
}
util::Ref context;
std::any context;
JSONRPCRequest request(context);
request.params.setArray();
request.params.push_back(backup_file);
@ -281,7 +281,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
LOCK(wallet->cs_wallet);
wallet->SetupLegacyScriptPubKeyMan();
util::Ref context;
std::any context;
JSONRPCRequest request(context);
request.params.setArray();
request.params.push_back(backup_file);

View file

@ -57,7 +57,6 @@ IGNORED_WARNINGS=(
"src/test/checkqueue_tests.cpp:.* Struct 'UniqueCheck' has a constructor with 1 argument that is not explicit."
"src/test/fuzz/util.h:.* Class 'FuzzedFileProvider' has a constructor with 1 argument that is not explicit."
"src/test/fuzz/util.h:.* Class 'FuzzedAutoFileProvider' has a constructor with 1 argument that is not explicit."
"src/util/ref.h:.* Class 'Ref' has a constructor with 1 argument that is not explicit."
"src/wallet/db.h:.* Class 'BerkeleyEnvironment' has a constructor with 1 argument that is not explicit."
)