Branch data Line data Source code
1 : : // Copyright (c) 2021-present 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 <chain.h>
8 : : #include <node/context.h>
9 : : #include <node/miner.h>
10 : : #include <pow.h>
11 : : #include <primitives/block.h>
12 : : #include <rpc/protocol.h>
13 : : #include <rpc/request.h>
14 : : #include <uint256.h>
15 : : #include <util/any.h>
16 : :
17 : : #include <any>
18 : : #include <memory>
19 : : #include <string>
20 : :
21 : : namespace Consensus {
22 : : struct Params;
23 : : } // namespace Consensus
24 : :
25 : : using node::NodeContext;
26 : : using node::UpdateTime;
27 : :
28 : 223490 : NodeContext& EnsureAnyNodeContext(const std::any& context)
29 : : {
30 : 223490 : auto node_context = util::AnyPtr<NodeContext>(context);
31 [ - + ]: 223490 : if (!node_context) {
32 [ # # # # ]: 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
33 : : }
34 : 223490 : return *node_context;
35 : : }
36 : :
37 : 25321 : CTxMemPool& EnsureMemPool(const NodeContext& node)
38 : : {
39 [ - + ]: 25321 : if (!node.mempool) {
40 [ # # # # ]: 0 : throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
41 : : }
42 : 25321 : return *node.mempool;
43 : : }
44 : :
45 : 23004 : CTxMemPool& EnsureAnyMemPool(const std::any& context)
46 : : {
47 : 23004 : return EnsureMemPool(EnsureAnyNodeContext(context));
48 : : }
49 : :
50 : :
51 : 106 : BanMan& EnsureBanman(const NodeContext& node)
52 : : {
53 [ - + ]: 106 : if (!node.banman) {
54 [ # # # # ]: 0 : throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
55 : : }
56 : 106 : return *node.banman;
57 : : }
58 : :
59 : 58 : BanMan& EnsureAnyBanman(const std::any& context)
60 : : {
61 : 58 : return EnsureBanman(EnsureAnyNodeContext(context));
62 : : }
63 : :
64 : 61 : ArgsManager& EnsureArgsman(const NodeContext& node)
65 : : {
66 [ - + ]: 61 : if (!node.args) {
67 [ # # # # ]: 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
68 : : }
69 : 61 : return *node.args;
70 : : }
71 : :
72 : 19 : ArgsManager& EnsureAnyArgsman(const std::any& context)
73 : : {
74 : 19 : return EnsureArgsman(EnsureAnyNodeContext(context));
75 : : }
76 : :
77 : 97234 : ChainstateManager& EnsureChainman(const NodeContext& node)
78 : : {
79 [ - + ]: 97234 : if (!node.chainman) {
80 [ # # # # ]: 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
81 : : }
82 : 97234 : return *node.chainman;
83 : : }
84 : :
85 : 86023 : ChainstateManager& EnsureAnyChainman(const std::any& context)
86 : : {
87 : 86023 : return EnsureChainman(EnsureAnyNodeContext(context));
88 : : }
89 : :
90 : 321 : FeeRateEstimatorManager& EnsureFeeEstimatorMan(const NodeContext& node)
91 : : {
92 [ + + ]: 321 : if (!node.fee_estimator_man) {
93 [ + - + - ]: 2 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
94 : : }
95 : 320 : return *node.fee_estimator_man;
96 : : }
97 : :
98 : 321 : FeeRateEstimatorManager& EnsureAnyFeeEstimatorMan(const std::any& context)
99 : : {
100 : 321 : return EnsureFeeEstimatorMan(EnsureAnyNodeContext(context));
101 : : }
102 : :
103 : 9016 : CConnman& EnsureConnman(const NodeContext& node)
104 : : {
105 [ - + ]: 9016 : if (!node.connman) {
106 [ # # # # ]: 0 : throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
107 : : }
108 : 9016 : return *node.connman;
109 : : }
110 : :
111 : 3870 : interfaces::Mining& EnsureMining(const NodeContext& node)
112 : : {
113 [ - + ]: 3870 : if (!node.mining) {
114 [ # # # # ]: 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Node miner not found");
115 : : }
116 : 3870 : return *node.mining;
117 : : }
118 : :
119 : 8871 : PeerManager& EnsurePeerman(const NodeContext& node)
120 : : {
121 [ - + ]: 8871 : if (!node.peerman) {
122 [ # # # # ]: 0 : throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
123 : : }
124 : 8871 : return *node.peerman;
125 : : }
126 : :
127 : 32365 : AddrMan& EnsureAddrman(const NodeContext& node)
128 : : {
129 [ - + ]: 32365 : if (!node.addrman) {
130 [ # # # # ]: 0 : throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
131 : : }
132 : 32365 : return *node.addrman;
133 : : }
134 : :
135 : 32365 : AddrMan& EnsureAnyAddrman(const std::any& context)
136 : : {
137 : 32365 : return EnsureAddrman(EnsureAnyNodeContext(context));
138 : : }
139 : :
140 : 25 : void NextEmptyBlockIndex(CBlockIndex& tip, const Consensus::Params& consensusParams, CBlockIndex& next_index)
141 : : {
142 : 25 : CBlockHeader next_header{};
143 : 25 : next_header.hashPrevBlock = tip.GetBlockHash();
144 : 25 : UpdateTime(&next_header, consensusParams, &tip);
145 : 25 : next_header.nBits = GetNextWorkRequired(&tip, &next_header, consensusParams);
146 : 25 : next_header.nNonce = 0;
147 : :
148 : 25 : next_index.pprev = &tip;
149 : 25 : next_index.nTime = next_header.nTime;
150 : 25 : next_index.nBits = next_header.nBits;
151 : 25 : next_index.nNonce = next_header.nNonce;
152 : 25 : next_index.nHeight = tip.nHeight + 1;
153 : 25 : }
|