merge bitcoin#23936: Add and use EnsureArgsman helper

This commit is contained in:
Kittywhiskers Van Gogh 2024-09-08 15:58:14 +00:00
parent b4bfacfd24
commit e039aecbdc
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
4 changed files with 31 additions and 10 deletions

View File

@ -8,6 +8,7 @@
#include <functional> #include <functional>
#include <variant> #include <variant>
class ArgsManager;
class ChainstateManager; class ChainstateManager;
class CTxMemPool; class CTxMemPool;
class CBlockPolicyEstimator; class CBlockPolicyEstimator;
@ -16,6 +17,7 @@ struct NodeContext;
struct WalletContext; struct WalletContext;
using CoreContext = std::variant<std::monostate, using CoreContext = std::variant<std::monostate,
std::reference_wrapper<ArgsManager>,
std::reference_wrapper<NodeContext>, std::reference_wrapper<NodeContext>,
std::reference_wrapper<WalletContext>, std::reference_wrapper<WalletContext>,
std::reference_wrapper<CTxMemPool>, std::reference_wrapper<CTxMemPool>,

View File

@ -1776,9 +1776,8 @@ RPCHelpMan getblockchaininfo()
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{ {
std::string strChainName = gArgs.IsArgSet("-devnet") ? gArgs.GetDevNetName() : Params().NetworkIDString();
const NodeContext& node = EnsureAnyNodeContext(request.context); const NodeContext& node = EnsureAnyNodeContext(request.context);
const ArgsManager& args{EnsureArgsman(node)};
ChainstateManager& chainman = EnsureChainman(node); ChainstateManager& chainman = EnsureChainman(node);
LOCK(cs_main); LOCK(cs_main);
@ -1792,7 +1791,11 @@ RPCHelpMan getblockchaininfo()
const auto ehfSignals = node.mnhf_manager->GetSignalsStage(tip); const auto ehfSignals = node.mnhf_manager->GetSignalsStage(tip);
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
obj.pushKV("chain", strChainName); if (args.IsArgSet("-devnet")) {
obj.pushKV("chain", args.GetDevNetName());
} else {
obj.pushKV("chain", Params().NetworkIDString());
}
obj.pushKV("blocks", height); obj.pushKV("blocks", height);
obj.pushKV("headers", chainman.m_best_header ? chainman.m_best_header->nHeight : -1); obj.pushKV("headers", chainman.m_best_header ? chainman.m_best_header->nHeight : -1);
obj.pushKV("bestblockhash", tip->GetBlockHash().GetHex()); obj.pushKV("bestblockhash", tip->GetBlockHash().GetHex());
@ -1814,7 +1817,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.GetArg("-prune", 0) != 1); bool automatic_pruning{args.GetArg("-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);
@ -2654,10 +2657,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");
} }
@ -2667,7 +2669,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;
}, },
@ -3011,10 +3013,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

@ -37,6 +37,19 @@ CTxMemPool& EnsureAnyMemPool(const CoreContext& 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 CoreContext& context)
{
return EnsureArgsman(EnsureAnyNodeContext(context));
}
ChainstateManager& EnsureChainman(const NodeContext& node) ChainstateManager& EnsureChainman(const NodeContext& node)
{ {
if (!node.chainman) { if (!node.chainman) {

View File

@ -7,10 +7,11 @@
#include <context.h> #include <context.h>
class ArgsManager;
class CBlockPolicyEstimator; class CBlockPolicyEstimator;
class CConnman; class CConnman;
class ChainstateManager;
class CTxMemPool; class CTxMemPool;
class ChainstateManager;
class PeerManager; class PeerManager;
struct NodeContext; struct NodeContext;
struct LLMQContext; struct LLMQContext;
@ -18,6 +19,8 @@ struct LLMQContext;
NodeContext& EnsureAnyNodeContext(const CoreContext& context); NodeContext& EnsureAnyNodeContext(const CoreContext& context);
CTxMemPool& EnsureMemPool(const NodeContext& node); CTxMemPool& EnsureMemPool(const NodeContext& node);
CTxMemPool& EnsureAnyMemPool(const CoreContext& context); CTxMemPool& EnsureAnyMemPool(const CoreContext& context);
ArgsManager& EnsureArgsman(const NodeContext& node);
ArgsManager& EnsureAnyArgsman(const CoreContext& context);
ChainstateManager& EnsureChainman(const NodeContext& node); ChainstateManager& EnsureChainman(const NodeContext& node);
ChainstateManager& EnsureAnyChainman(const CoreContext& context); ChainstateManager& EnsureAnyChainman(const CoreContext& context);
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node); CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node);