Skip to content

Commit 300124d

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23936: rpc: Add and use EnsureArgsman helper
fa5bb37 rpc: Use EnsureAnyArgsman in rpc/blockchain (MarcoFalke) fa98b6f 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 fa5bb37 Tree-SHA512: 18f9cc90830a168acb94452b541b6e97ba9a50a0f4834698a16994c3884c833dc606e36a5538a16352e5e5cc43d3ac77cb498f877f8f5bc5dd52fa84f8bf2056
2 parents 8319c4e + fa5bb37 commit 300124d

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

src/rpc/blockchain.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,7 @@ RPCHelpMan getblockchaininfo()
15441544
},
15451545
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
15461546
{
1547+
const ArgsManager& args{EnsureAnyArgsman(request.context)};
15471548
ChainstateManager& chainman = EnsureAnyChainman(request.context);
15481549
LOCK(cs_main);
15491550
CChainState& active_chainstate = chainman.ActiveChainstate();
@@ -1574,7 +1575,7 @@ RPCHelpMan getblockchaininfo()
15741575
obj.pushKV("pruneheight", block->nHeight);
15751576

15761577
// if 0, execution bypasses the whole if block.
1577-
bool automatic_pruning = (gArgs.GetIntArg("-prune", 0) != 1);
1578+
bool automatic_pruning{args.GetIntArg("-prune", 0) != 1};
15781579
obj.pushKV("automatic_pruning", automatic_pruning);
15791580
if (automatic_pruning) {
15801581
obj.pushKV("prune_target_size", nPruneTarget);
@@ -2270,10 +2271,9 @@ static RPCHelpMan savemempool()
22702271
},
22712272
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
22722273
{
2274+
const ArgsManager& args{EnsureAnyArgsman(request.context)};
22732275
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
22742276

2275-
const NodeContext& node = EnsureAnyNodeContext(request.context);
2276-
22772277
if (!mempool.IsLoaded()) {
22782278
throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
22792279
}
@@ -2283,7 +2283,7 @@ static RPCHelpMan savemempool()
22832283
}
22842284

22852285
UniValue ret(UniValue::VOBJ);
2286-
ret.pushKV("filename", fs::path((node.args->GetDataDirNet() / "mempool.dat")).u8string());
2286+
ret.pushKV("filename", fs::path((args.GetDataDirNet() / "mempool.dat")).u8string());
22872287

22882288
return ret;
22892289
},
@@ -2628,10 +2628,11 @@ static RPCHelpMan dumptxoutset()
26282628
},
26292629
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
26302630
{
2631-
const fs::path path = fsbridge::AbsPathJoin(gArgs.GetDataDirNet(), fs::u8path(request.params[0].get_str()));
2631+
const ArgsManager& args{EnsureAnyArgsman(request.context)};
2632+
const fs::path path = fsbridge::AbsPathJoin(args.GetDataDirNet(), fs::u8path(request.params[0].get_str()));
26322633
// Write to a temporary path and then move into `path` on completion
26332634
// to avoid confusion due to an interruption.
2634-
const fs::path temppath = fsbridge::AbsPathJoin(gArgs.GetDataDirNet(), fs::u8path(request.params[0].get_str() + ".incomplete"));
2635+
const fs::path temppath = fsbridge::AbsPathJoin(args.GetDataDirNet(), fs::u8path(request.params[0].get_str() + ".incomplete"));
26352636

26362637
if (fs::exists(path)) {
26372638
throw JSONRPCError(

src/rpc/blockchain.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class CBlock;
2121
class CBlockIndex;
2222
class CChainState;
2323
class CTxMemPool;
24-
class ChainstateManager;
2524
class UniValue;
2625
struct NodeContext;
2726

src/rpc/server_util.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ CTxMemPool& EnsureAnyMemPool(const std::any& context)
3737
return EnsureMemPool(EnsureAnyNodeContext(context));
3838
}
3939

40+
ArgsManager& EnsureArgsman(const NodeContext& node)
41+
{
42+
if (!node.args) {
43+
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
44+
}
45+
return *node.args;
46+
}
47+
48+
ArgsManager& EnsureAnyArgsman(const std::any& context)
49+
{
50+
return EnsureArgsman(EnsureAnyNodeContext(context));
51+
}
52+
4053
ChainstateManager& EnsureChainman(const NodeContext& node)
4154
{
4255
if (!node.chainman) {

src/rpc/server_util.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77

88
#include <any>
99

10+
class ArgsManager;
1011
class CBlockPolicyEstimator;
1112
class CConnman;
12-
class ChainstateManager;
1313
class CTxMemPool;
14-
struct NodeContext;
14+
class ChainstateManager;
1515
class PeerManager;
16+
struct NodeContext;
1617

1718
NodeContext& EnsureAnyNodeContext(const std::any& context);
1819
CTxMemPool& EnsureMemPool(const NodeContext& node);
1920
CTxMemPool& EnsureAnyMemPool(const std::any& context);
21+
ArgsManager& EnsureArgsman(const NodeContext& node);
22+
ArgsManager& EnsureAnyArgsman(const std::any& context);
2023
ChainstateManager& EnsureChainman(const NodeContext& node);
2124
ChainstateManager& EnsureAnyChainman(const std::any& context);
2225
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node);

0 commit comments

Comments
 (0)