refactor: replace util::Ref by std::any (C++17)

This commit is contained in:
Sebastian Falbesoner 2020-12-01 00:36:36 +01:00
parent 95cccf8a4b
commit 8dbb87a393
17 changed files with 77 additions and 82 deletions

View File

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

View File

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

View File

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

View File

@ -787,7 +787,7 @@ static bool InitSanityCheck()
return true; 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); const ArgsManager& args = *Assert(node.args);
RPCServer::OnStarted(&OnRPCStarted); RPCServer::OnStarted(&OnRPCStarted);
@ -1276,7 +1276,7 @@ bool AppInitInterfaces(NodeContext& node)
return true; 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 ArgsManager& args = *Assert(node.args);
const CChainParams& chainparams = Params(); const CChainParams& chainparams = Params();

View File

@ -6,6 +6,7 @@
#ifndef BITCOIN_INIT_H #ifndef BITCOIN_INIT_H
#define BITCOIN_INIT_H #define BITCOIN_INIT_H
#include <any>
#include <memory> #include <memory>
#include <string> #include <string>
@ -22,9 +23,6 @@ struct BlockAndHeaderTipInfo;
namespace boost { namespace boost {
class thread_group; class thread_group;
} // namespace boost } // namespace boost
namespace util {
class Ref;
} // namespace util
/** Interrupt threads */ /** Interrupt threads */
void Interrupt(NodeContext& node); 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. * @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. * @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 * Register all arguments with the ArgsManager

View File

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

View File

@ -18,10 +18,12 @@
#include <sync.h> #include <sync.h>
#include <txmempool.h> #include <txmempool.h>
#include <util/check.h> #include <util/check.h>
#include <util/ref.h> #include <util/system.h>
#include <validation.h> #include <validation.h>
#include <version.h> #include <version.h>
#include <any>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <univalue.h> #include <univalue.h>
@ -73,10 +75,10 @@ static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string me
* context is not found. * context is not found.
* @returns Pointer to the node context or nullptr if 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; auto node_context = util::AnyPtr<NodeContext>(context);
if (!node) { if (!node_context) {
RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, RESTERR(req, HTTP_INTERNAL_SERVER_ERROR,
strprintf("%s:%d (%s)\n" strprintf("%s:%d (%s)\n"
"Internal bug detected: Node context not found!\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)); __FILE__, __LINE__, __func__, PACKAGE_BUGREPORT));
return nullptr; 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. * context mempool is not found.
* @returns Pointer to the mempool or nullptr if no mempool 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; auto node_context = util::AnyPtr<NodeContext>(context);
if (!node || !node->mempool) { if (!node_context || !node_context->mempool) {
RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found"); RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found");
return nullptr; return nullptr;
} }
return node->mempool.get(); return node_context->mempool.get();
} }
static RetFormat ParseDataFormat(std::string& param, const std::string& strReq) static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
@ -151,7 +153,7 @@ static bool CheckWarmup(HTTPRequest* req)
return true; return true;
} }
static bool rest_headers(const util::Ref& context, static bool rest_headers(const std::any& context,
HTTPRequest* req, HTTPRequest* req,
const std::string& strURIPart) 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); 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); 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 // A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
RPCHelpMan getblockchaininfo(); 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)) if (!CheckWarmup(req))
return false; 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)) if (!CheckWarmup(req))
return false; 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; if (!CheckWarmup(req)) return false;
const CTxMemPool* mempool = GetMemPool(context, req); 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)) if (!CheckWarmup(req))
return false; 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)) if (!CheckWarmup(req))
return false; 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) const std::string& str_uri_part)
{ {
if (!CheckWarmup(req)) return false; if (!CheckWarmup(req)) return false;
@ -669,7 +671,7 @@ static bool rest_blockhash_by_height(const util::Ref& context, HTTPRequest* req,
static const struct { static const struct {
const char* prefix; 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[] = { } uri_prefixes[] = {
{"/rest/tx/", rest_tx}, {"/rest/tx/", rest_tx},
{"/rest/block/notxdetails/", rest_block_notxdetails}, {"/rest/block/notxdetails/", rest_block_notxdetails},
@ -682,7 +684,7 @@ static const struct {
{"/rest/blockhashbyheight/", rest_blockhash_by_height}, {"/rest/blockhashbyheight/", rest_blockhash_by_height},
}; };
void StartREST(const util::Ref& context) void StartREST(const std::any& context)
{ {
for (const auto& up : uri_prefixes) { for (const auto& up : uri_prefixes) {
auto handler = [&context, up](HTTPRequest* req, const std::string& prefix) { return up.handler(context, req, prefix); }; 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 <txdb.h>
#include <txmempool.h> #include <txmempool.h>
#include <undo.h> #include <undo.h>
#include <util/ref.h>
#include <util/strencodings.h> #include <util/strencodings.h>
#include <util/system.h> #include <util/system.h>
#include <util/translation.h> #include <util/translation.h>
@ -56,15 +55,16 @@ static Mutex cs_blockchange;
static std::condition_variable cond_blockchange; static std::condition_variable cond_blockchange;
static CUpdatedBlock latestblock GUARDED_BY(cs_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"); 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); const NodeContext& node = EnsureNodeContext(context);
if (!node.mempool) { if (!node.mempool) {
@ -73,7 +73,7 @@ CTxMemPool& EnsureMemPool(const util::Ref& context)
return *node.mempool; return *node.mempool;
} }
ChainstateManager& EnsureChainman(const util::Ref& context) ChainstateManager& EnsureChainman(const std::any& context)
{ {
const NodeContext& node = EnsureNodeContext(context); const NodeContext& node = EnsureNodeContext(context);
if (!node.chainman) { if (!node.chainman) {
@ -82,7 +82,7 @@ ChainstateManager& EnsureChainman(const util::Ref& context)
return *node.chainman; return *node.chainman;
} }
CBlockPolicyEstimator& EnsureFeeEstimator(const util::Ref& context) CBlockPolicyEstimator& EnsureFeeEstimator(const std::any& context)
{ {
NodeContext& node = EnsureNodeContext(context); NodeContext& node = EnsureNodeContext(context);
if (!node.fee_estimator) { if (!node.fee_estimator) {

View File

@ -10,6 +10,7 @@
#include <streams.h> #include <streams.h>
#include <sync.h> #include <sync.h>
#include <any>
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
@ -23,9 +24,6 @@ class CTxMemPool;
class ChainstateManager; class ChainstateManager;
class UniValue; class UniValue;
struct NodeContext; struct NodeContext;
namespace util {
class Ref;
} // namespace util
static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5; 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 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); 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); NodeContext& EnsureNodeContext(const std::any& context);
CTxMemPool& EnsureMemPool(const util::Ref& context); CTxMemPool& EnsureMemPool(const std::any& context);
ChainstateManager& EnsureChainman(const util::Ref& context); ChainstateManager& EnsureChainman(const std::any& context);
CBlockPolicyEstimator& EnsureFeeEstimator(const util::Ref& context); CBlockPolicyEstimator& EnsureFeeEstimator(const std::any& context);
/** /**
* Helper to create UTXO snapshots given a chainstate and a file handle. * Helper to create UTXO snapshots given a chainstate and a file handle.

View File

@ -17,7 +17,6 @@
#include <script/descriptor.h> #include <script/descriptor.h>
#include <util/check.h> #include <util/check.h>
#include <util/message.h> // For MessageSign(), MessageVerify() #include <util/message.h> // For MessageSign(), MessageVerify()
#include <util/ref.h>
#include <util/strencodings.h> #include <util/strencodings.h>
#include <util/system.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)); throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Mocktime can not be negative: %s.", time));
} }
SetMockTime(time); SetMockTime(time);
if (request.context.Has<NodeContext>()) { auto node_context = util::AnyPtr<NodeContext>(request.context);
for (const auto& chain_client : request.context.Get<NodeContext>().chain_clients) { if (node_context) {
for (const auto& chain_client : node_context->chain_clients) {
chain_client->setMockTime(time); 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)"); 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 // protect against null pointer dereference
CHECK_NONFATAL(request.context.Has<NodeContext>()); CHECK_NONFATAL(node_context);
NodeContext& node = request.context.Get<NodeContext>(); CHECK_NONFATAL(node_context->scheduler);
CHECK_NONFATAL(node.scheduler); node_context->scheduler->MockForward(std::chrono::seconds(delta_seconds));
node.scheduler->MockForward(std::chrono::seconds(delta_seconds));
return NullUniValue; return NullUniValue;
}, },

View File

@ -6,14 +6,11 @@
#ifndef BITCOIN_RPC_REQUEST_H #ifndef BITCOIN_RPC_REQUEST_H
#define BITCOIN_RPC_REQUEST_H #define BITCOIN_RPC_REQUEST_H
#include <any>
#include <string> #include <string>
#include <univalue.h> #include <univalue.h>
namespace util {
class Ref;
} // namespace util
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id); UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, 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); std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id);
@ -38,14 +35,14 @@ public:
std::string URI; std::string URI;
std::string authUser; std::string authUser;
std::string peerAddr; 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 //! Initializes request information from another request object and the
//! given context. The implementation should be updated if any members are //! given context. The implementation should be updated if any members are
//! added or removed above. //! 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), : id(other.id), strMethod(other.strMethod), params(other.params), mode(other.mode), URI(other.URI),
authUser(other.authUser), peerAddr(other.peerAddr), context(context) 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())); vCommands.push_back(make_pair(entry.second.front()->category + entry.first, entry.second.front()));
sort(vCommands.begin(), vCommands.end()); sort(vCommands.begin(), vCommands.end());
JSONRPCRequest jreq(helpreq); JSONRPCRequest jreq = helpreq;
jreq.mode = JSONRPCRequest::GET_HELP; jreq.mode = JSONRPCRequest::GET_HELP;
jreq.params = UniValue(); jreq.params = UniValue();
@ -494,7 +494,7 @@ std::vector<std::string> CRPCTable::listCommands() const
UniValue CRPCTable::dumpArgMap(const JSONRPCRequest& args_request) const UniValue CRPCTable::dumpArgMap(const JSONRPCRequest& args_request) const
{ {
JSONRPCRequest request(args_request); JSONRPCRequest request = args_request;
request.mode = JSONRPCRequest::GET_ARGS; request.mode = JSONRPCRequest::GET_ARGS;
UniValue ret{UniValue::VARR}; UniValue ret{UniValue::VARR};

View File

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

View File

@ -15,7 +15,6 @@
#include <sync.h> #include <sync.h>
#include <uint256.h> #include <uint256.h>
#include <util/check.h> #include <util/check.h>
#include <util/ref.h>
#include <util/system.h> #include <util/system.h>
#include <util/ui_change_type.h> #include <util/ui_change_type.h>
#include <wallet/context.h> #include <wallet/context.h>
@ -515,7 +514,7 @@ public:
{ {
for (const CRPCCommand& command : GetWalletRPCCommands()) { for (const CRPCCommand& command : GetWalletRPCCommands()) {
m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) { 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); }, command.argNames, command.unique_id);
m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back())); 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/fees.h>
#include <util/message.h> // For MessageSign() #include <util/message.h> // For MessageSign()
#include <util/moneystr.h> #include <util/moneystr.h>
#include <util/ref.h>
#include <util/string.h> #include <util/string.h>
#include <util/system.h> #include <util/system.h>
#include <util/translation.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"); 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 // 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 <span.h>
#include <any>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -31,7 +32,7 @@ Span<const CRPCCommand> GetWalletRPCCommands();
std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request); std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request);
void EnsureWalletIsUnlocked(const CWallet&); void EnsureWalletIsUnlocked(const CWallet&);
WalletContext& EnsureWalletContext(const util::Ref& context); WalletContext& EnsureWalletContext(const std::any& context);
LegacyScriptPubKeyMan& EnsureLegacyScriptPubKeyMan(CWallet& wallet, bool also_create = false); LegacyScriptPubKeyMan& EnsureLegacyScriptPubKeyMan(CWallet& wallet, bool also_create = false);
RPCHelpMan getaddressinfo(); RPCHelpMan getaddressinfo();

View File

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