Merge bitcoin/bitcoin#23936: rpc: Add and use EnsureArgsman helper

fa5bb37611dea5d3e6715b0c84249e39ad1d2476 rpc: Use EnsureAnyArgsman in rpc/blockchain (MarcoFalke)
fa98b6f6449d33b315a73156c6de968f1757d53a rpc: Add EnsureArgsman helper (MarcoFalke)

Pull request description:

  This refactor doesn't change anything for the rpc layer, but it helps a bit with removing gArgs (See #21005)

ACKs for top commit:
  shaavan:
    Code Review ACK fa5bb37611dea5d3e6715b0c84249e39ad1d2476

Tree-SHA512: 18f9cc90830a168acb94452b541b6e97ba9a50a0f4834698a16994c3884c833dc606e36a5538a16352e5e5cc43d3ac77cb498f877f8f5bc5dd52fa84f8bf2056
This commit is contained in:
MarcoFalke 2022-01-04 15:39:53 +01:00
commit 300124dedf
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548
4 changed files with 25 additions and 9 deletions

View File

@ -1544,6 +1544,7 @@ RPCHelpMan getblockchaininfo()
}, },
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{ {
const ArgsManager& args{EnsureAnyArgsman(request.context)};
ChainstateManager& chainman = EnsureAnyChainman(request.context); ChainstateManager& chainman = EnsureAnyChainman(request.context);
LOCK(cs_main); LOCK(cs_main);
CChainState& active_chainstate = chainman.ActiveChainstate(); CChainState& active_chainstate = chainman.ActiveChainstate();
@ -1574,7 +1575,7 @@ RPCHelpMan getblockchaininfo()
obj.pushKV("pruneheight", block->nHeight); obj.pushKV("pruneheight", block->nHeight);
// if 0, execution bypasses the whole if block. // if 0, execution bypasses the whole if block.
bool automatic_pruning = (gArgs.GetIntArg("-prune", 0) != 1); bool automatic_pruning{args.GetIntArg("-prune", 0) != 1};
obj.pushKV("automatic_pruning", automatic_pruning); obj.pushKV("automatic_pruning", automatic_pruning);
if (automatic_pruning) { if (automatic_pruning) {
obj.pushKV("prune_target_size", nPruneTarget); obj.pushKV("prune_target_size", nPruneTarget);
@ -2270,10 +2271,9 @@ static RPCHelpMan savemempool()
}, },
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{ {
const ArgsManager& args{EnsureAnyArgsman(request.context)};
const CTxMemPool& mempool = EnsureAnyMemPool(request.context); const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
const NodeContext& node = EnsureAnyNodeContext(request.context);
if (!mempool.IsLoaded()) { if (!mempool.IsLoaded()) {
throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet"); throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
} }
@ -2283,7 +2283,7 @@ static RPCHelpMan savemempool()
} }
UniValue ret(UniValue::VOBJ); UniValue ret(UniValue::VOBJ);
ret.pushKV("filename", fs::path((node.args->GetDataDirNet() / "mempool.dat")).u8string()); ret.pushKV("filename", fs::path((args.GetDataDirNet() / "mempool.dat")).u8string());
return ret; return ret;
}, },
@ -2628,10 +2628,11 @@ static RPCHelpMan dumptxoutset()
}, },
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{ {
const fs::path path = fsbridge::AbsPathJoin(gArgs.GetDataDirNet(), fs::u8path(request.params[0].get_str())); const ArgsManager& args{EnsureAnyArgsman(request.context)};
const fs::path path = fsbridge::AbsPathJoin(args.GetDataDirNet(), fs::u8path(request.params[0].get_str()));
// Write to a temporary path and then move into `path` on completion // Write to a temporary path and then move into `path` on completion
// to avoid confusion due to an interruption. // to avoid confusion due to an interruption.
const fs::path temppath = fsbridge::AbsPathJoin(gArgs.GetDataDirNet(), fs::u8path(request.params[0].get_str() + ".incomplete")); const fs::path temppath = fsbridge::AbsPathJoin(args.GetDataDirNet(), fs::u8path(request.params[0].get_str() + ".incomplete"));
if (fs::exists(path)) { if (fs::exists(path)) {
throw JSONRPCError( throw JSONRPCError(

View File

@ -21,7 +21,6 @@ class CBlock;
class CBlockIndex; class CBlockIndex;
class CChainState; class CChainState;
class CTxMemPool; class CTxMemPool;
class ChainstateManager;
class UniValue; class UniValue;
struct NodeContext; struct NodeContext;

View File

@ -37,6 +37,19 @@ CTxMemPool& EnsureAnyMemPool(const std::any& context)
return EnsureMemPool(EnsureAnyNodeContext(context)); return EnsureMemPool(EnsureAnyNodeContext(context));
} }
ArgsManager& EnsureArgsman(const NodeContext& node)
{
if (!node.args) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
}
return *node.args;
}
ArgsManager& EnsureAnyArgsman(const std::any& context)
{
return EnsureArgsman(EnsureAnyNodeContext(context));
}
ChainstateManager& EnsureChainman(const NodeContext& node) ChainstateManager& EnsureChainman(const NodeContext& node)
{ {
if (!node.chainman) { if (!node.chainman) {

View File

@ -7,16 +7,19 @@
#include <any> #include <any>
class ArgsManager;
class CBlockPolicyEstimator; class CBlockPolicyEstimator;
class CConnman; class CConnman;
class ChainstateManager;
class CTxMemPool; class CTxMemPool;
struct NodeContext; class ChainstateManager;
class PeerManager; class PeerManager;
struct NodeContext;
NodeContext& EnsureAnyNodeContext(const std::any& context); NodeContext& EnsureAnyNodeContext(const std::any& context);
CTxMemPool& EnsureMemPool(const NodeContext& node); CTxMemPool& EnsureMemPool(const NodeContext& node);
CTxMemPool& EnsureAnyMemPool(const std::any& context); CTxMemPool& EnsureAnyMemPool(const std::any& context);
ArgsManager& EnsureArgsman(const NodeContext& node);
ArgsManager& EnsureAnyArgsman(const std::any& context);
ChainstateManager& EnsureChainman(const NodeContext& node); ChainstateManager& EnsureChainman(const NodeContext& node);
ChainstateManager& EnsureAnyChainman(const std::any& context); ChainstateManager& EnsureAnyChainman(const std::any& context);
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node); CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node);