diff options
author | Sebastian Falbesoner <sebastian.falbesoner@gmail.com> | 2020-12-01 00:36:36 +0100 |
---|---|---|
committer | Sebastian Falbesoner <sebastian.falbesoner@gmail.com> | 2021-03-29 23:29:42 +0200 |
commit | 8dbb87a3932f81e23ba7afd865b9aeeb535f0c20 (patch) | |
tree | 2b1376e9bb8f6792946c6fabdffdc6a5a73fd8d2 /src/rpc | |
parent | 95cccf8a4b392959c1fd7ec0647e04eb13880865 (diff) |
refactor: replace util::Ref by std::any (C++17)
Diffstat (limited to 'src/rpc')
-rw-r--r-- | src/rpc/blockchain.cpp | 14 | ||||
-rw-r--r-- | src/rpc/blockchain.h | 12 | ||||
-rw-r--r-- | src/rpc/misc.cpp | 14 | ||||
-rw-r--r-- | src/rpc/request.h | 11 | ||||
-rw-r--r-- | src/rpc/server.cpp | 4 |
5 files changed, 25 insertions, 30 deletions
diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index f0ad141fa9..961478155f 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -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) { diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h index e719dfc702..cd04c9a10f 100644 --- a/src/rpc/blockchain.h +++ b/src/rpc/blockchain.h @@ -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. diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 143be1274e..1df5c51718 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -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; }, diff --git a/src/rpc/request.h b/src/rpc/request.h index 27d06f3c92..e1569673f6 100644 --- a/src/rpc/request.h +++ b/src/rpc/request.h @@ -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) { diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 39938f4eb9..2f05c8842f 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -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}; |