diff options
author | Sjors Provoost <sjors@sprovoost.nl> | 2021-04-21 16:39:40 +0200 |
---|---|---|
committer | Sjors Provoost <sjors@sprovoost.nl> | 2021-12-02 13:15:32 +0700 |
commit | b884ababc29ce963826d8a4327ed6a5e629ff175 (patch) | |
tree | 55b49ff02eec85ec0f133a236c997ede8224c43e /src/rpc/server_util.cpp | |
parent | df562d698a386166ef93d03326c0480ea9bc11fe (diff) | |
download | bitcoin-b884ababc29ce963826d8a4327ed6a5e629ff175.tar.xz |
rpc: move Ensure* helpers to server_util.h
Diffstat (limited to 'src/rpc/server_util.cpp')
-rw-r--r-- | src/rpc/server_util.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/rpc/server_util.cpp b/src/rpc/server_util.cpp new file mode 100644 index 0000000000..3fc35222e1 --- /dev/null +++ b/src/rpc/server_util.cpp @@ -0,0 +1,80 @@ +// Copyright (c) 2021 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 <rpc/server_util.h> + +#include <net_processing.h> +#include <node/context.h> +#include <policy/fees.h> +#include <rpc/protocol.h> +#include <rpc/request.h> +#include <txmempool.h> +#include <util/system.h> +#include <validation.h> + +#include <any> + +NodeContext& EnsureAnyNodeContext(const std::any& context) +{ + auto node_context = util::AnyPtr<NodeContext>(context); + if (!node_context) { + throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found"); + } + return *node_context; +} + +CTxMemPool& EnsureMemPool(const NodeContext& node) +{ + if (!node.mempool) { + throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found"); + } + return *node.mempool; +} + +CTxMemPool& EnsureAnyMemPool(const std::any& context) +{ + return EnsureMemPool(EnsureAnyNodeContext(context)); +} + +ChainstateManager& EnsureChainman(const NodeContext& node) +{ + if (!node.chainman) { + throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found"); + } + return *node.chainman; +} + +ChainstateManager& EnsureAnyChainman(const std::any& context) +{ + return EnsureChainman(EnsureAnyNodeContext(context)); +} + +CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node) +{ + if (!node.fee_estimator) { + throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled"); + } + return *node.fee_estimator; +} + +CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context) +{ + return EnsureFeeEstimator(EnsureAnyNodeContext(context)); +} + +CConnman& EnsureConnman(const NodeContext& node) +{ + if (!node.connman) { + throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); + } + return *node.connman; +} + +PeerManager& EnsurePeerman(const NodeContext& node) +{ + if (!node.peerman) { + throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); + } + return *node.peerman; +} |