Skip to content

Commit b884aba

Browse files
committed
rpc: move Ensure* helpers to server_util.h
1 parent df562d6 commit b884aba

File tree

11 files changed

+115
-93
lines changed

11 files changed

+115
-93
lines changed

src/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ BITCOIN_CORE_H = \
200200
rpc/blockchain.h \
201201
rpc/client.h \
202202
rpc/mining.h \
203-
rpc/net.h \
204203
rpc/protocol.h \
205204
rpc/rawtransaction_util.h \
206205
rpc/register.h \
207206
rpc/request.h \
208207
rpc/server.h \
208+
rpc/server_util.h \
209209
rpc/util.h \
210210
scheduler.h \
211211
script/descriptor.h \
@@ -360,6 +360,7 @@ libbitcoin_server_a_SOURCES = \
360360
rpc/net.cpp \
361361
rpc/rawtransaction.cpp \
362362
rpc/server.cpp \
363+
rpc/server_util.cpp \
363364
script/sigcache.cpp \
364365
shutdown.cpp \
365366
signet.cpp \

src/rest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <rpc/blockchain.h>
1616
#include <rpc/protocol.h>
1717
#include <rpc/server.h>
18+
#include <rpc/server_util.h>
1819
#include <streams.h>
1920
#include <sync.h>
2021
#include <txmempool.h>

src/rpc/blockchain.cpp

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <policy/rbf.h>
2929
#include <primitives/transaction.h>
3030
#include <rpc/server.h>
31+
#include <rpc/server_util.h>
3132
#include <rpc/util.h>
3233
#include <script/descriptor.h>
3334
#include <streams.h>
@@ -37,7 +38,6 @@
3738
#include <undo.h>
3839
#include <util/strencodings.h>
3940
#include <util/string.h>
40-
#include <util/system.h>
4141
#include <util/translation.h>
4242
#include <validation.h>
4343
#include <validationinterface.h>
@@ -62,54 +62,6 @@ static Mutex cs_blockchange;
6262
static std::condition_variable cond_blockchange;
6363
static CUpdatedBlock latestblock GUARDED_BY(cs_blockchange);
6464

65-
NodeContext& EnsureAnyNodeContext(const std::any& context)
66-
{
67-
auto node_context = util::AnyPtr<NodeContext>(context);
68-
if (!node_context) {
69-
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
70-
}
71-
return *node_context;
72-
}
73-
74-
CTxMemPool& EnsureMemPool(const NodeContext& node)
75-
{
76-
if (!node.mempool) {
77-
throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
78-
}
79-
return *node.mempool;
80-
}
81-
82-
CTxMemPool& EnsureAnyMemPool(const std::any& context)
83-
{
84-
return EnsureMemPool(EnsureAnyNodeContext(context));
85-
}
86-
87-
ChainstateManager& EnsureChainman(const NodeContext& node)
88-
{
89-
if (!node.chainman) {
90-
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
91-
}
92-
return *node.chainman;
93-
}
94-
95-
ChainstateManager& EnsureAnyChainman(const std::any& context)
96-
{
97-
return EnsureChainman(EnsureAnyNodeContext(context));
98-
}
99-
100-
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node)
101-
{
102-
if (!node.fee_estimator) {
103-
throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
104-
}
105-
return *node.fee_estimator;
106-
}
107-
108-
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context)
109-
{
110-
return EnsureFeeEstimator(EnsureAnyNodeContext(context));
111-
}
112-
11365
/* Calculate the difficulty for a given block index.
11466
*/
11567
double GetDifficulty(const CBlockIndex* blockindex)

src/rpc/blockchain.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ extern RecursiveMutex cs_main;
1818

1919
class CBlock;
2020
class CBlockIndex;
21-
class CBlockPolicyEstimator;
2221
class CChainState;
2322
class CTxMemPool;
2423
class ChainstateManager;
@@ -53,14 +52,6 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
5352
/** Used by getblockstats to get feerates at different percentiles by weight */
5453
void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES], std::vector<std::pair<CAmount, int64_t>>& scores, int64_t total_weight);
5554

56-
NodeContext& EnsureAnyNodeContext(const std::any& context);
57-
CTxMemPool& EnsureMemPool(const NodeContext& node);
58-
CTxMemPool& EnsureAnyMemPool(const std::any& context);
59-
ChainstateManager& EnsureChainman(const NodeContext& node);
60-
ChainstateManager& EnsureAnyChainman(const std::any& context);
61-
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node);
62-
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context);
63-
6455
/**
6556
* Helper to create UTXO snapshots given a chainstate and a file handle.
6657
* @return a UniValue map containing metadata about the snapshot.

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include <pow.h>
2121
#include <rpc/blockchain.h>
2222
#include <rpc/mining.h>
23-
#include <rpc/net.h>
2423
#include <rpc/server.h>
24+
#include <rpc/server_util.h>
2525
#include <rpc/util.h>
2626
#include <script/descriptor.h>
2727
#include <script/script.h>

src/rpc/misc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <outputtype.h>
1717
#include <rpc/blockchain.h>
1818
#include <rpc/server.h>
19+
#include <rpc/server_util.h>
1920
#include <rpc/util.h>
2021
#include <scheduler.h>
2122
#include <script/descriptor.h>

src/rpc/net.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <chainparams.h>
99
#include <clientversion.h>
1010
#include <core_io.h>
11-
#include <net.h>
1211
#include <net_permissions.h>
1312
#include <net_processing.h>
1413
#include <net_types.h> // For banmap_t
@@ -17,12 +16,12 @@
1716
#include <policy/settings.h>
1817
#include <rpc/blockchain.h>
1918
#include <rpc/protocol.h>
19+
#include <rpc/server_util.h>
2020
#include <rpc/util.h>
2121
#include <sync.h>
2222
#include <timedata.h>
2323
#include <util/strencodings.h>
2424
#include <util/string.h>
25-
#include <util/system.h>
2625
#include <util/translation.h>
2726
#include <validation.h>
2827
#include <version.h>
@@ -41,22 +40,6 @@ const std::vector<std::string> CONNECTION_TYPE_DOC{
4140
"feeler (short-lived automatic connection for testing addresses)"
4241
};
4342

44-
CConnman& EnsureConnman(const NodeContext& node)
45-
{
46-
if (!node.connman) {
47-
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
48-
}
49-
return *node.connman;
50-
}
51-
52-
PeerManager& EnsurePeerman(const NodeContext& node)
53-
{
54-
if (!node.peerman) {
55-
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
56-
}
57-
return *node.peerman;
58-
}
59-
6043
static RPCHelpMan getconnectioncount()
6144
{
6245
return RPCHelpMan{"getconnectioncount",

src/rpc/net.h

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <rpc/blockchain.h>
2626
#include <rpc/rawtransaction_util.h>
2727
#include <rpc/server.h>
28+
#include <rpc/server_util.h>
2829
#include <rpc/util.h>
2930
#include <script/script.h>
3031
#include <script/sign.h>

src/rpc/server_util.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <rpc/server_util.h>
6+
7+
#include <net_processing.h>
8+
#include <node/context.h>
9+
#include <policy/fees.h>
10+
#include <rpc/protocol.h>
11+
#include <rpc/request.h>
12+
#include <txmempool.h>
13+
#include <util/system.h>
14+
#include <validation.h>
15+
16+
#include <any>
17+
18+
NodeContext& EnsureAnyNodeContext(const std::any& context)
19+
{
20+
auto node_context = util::AnyPtr<NodeContext>(context);
21+
if (!node_context) {
22+
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
23+
}
24+
return *node_context;
25+
}
26+
27+
CTxMemPool& EnsureMemPool(const NodeContext& node)
28+
{
29+
if (!node.mempool) {
30+
throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
31+
}
32+
return *node.mempool;
33+
}
34+
35+
CTxMemPool& EnsureAnyMemPool(const std::any& context)
36+
{
37+
return EnsureMemPool(EnsureAnyNodeContext(context));
38+
}
39+
40+
ChainstateManager& EnsureChainman(const NodeContext& node)
41+
{
42+
if (!node.chainman) {
43+
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
44+
}
45+
return *node.chainman;
46+
}
47+
48+
ChainstateManager& EnsureAnyChainman(const std::any& context)
49+
{
50+
return EnsureChainman(EnsureAnyNodeContext(context));
51+
}
52+
53+
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node)
54+
{
55+
if (!node.fee_estimator) {
56+
throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
57+
}
58+
return *node.fee_estimator;
59+
}
60+
61+
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context)
62+
{
63+
return EnsureFeeEstimator(EnsureAnyNodeContext(context));
64+
}
65+
66+
CConnman& EnsureConnman(const NodeContext& node)
67+
{
68+
if (!node.connman) {
69+
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
70+
}
71+
return *node.connman;
72+
}
73+
74+
PeerManager& EnsurePeerman(const NodeContext& node)
75+
{
76+
if (!node.peerman) {
77+
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
78+
}
79+
return *node.peerman;
80+
}

0 commit comments

Comments
 (0)