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

@ -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()) {

@ -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())

@ -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();

@ -787,7 +787,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);
@ -1276,7 +1276,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();

@ -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

@ -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)

@ -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); };

@ -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) {

@ -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.

@ -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;
},

@ -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)
{

@ -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};

@ -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);

@ -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()));
}

@ -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

@ -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();

@ -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);