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

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