Skip to content

Commit e8270ca

Browse files
kwvgPastaPastaPasta
authored andcommitted
refactor: use NodeContext members instead of globals in RPC logic
1 parent 2efebca commit e8270ca

File tree

6 files changed

+131
-91
lines changed

6 files changed

+131
-91
lines changed

src/rpc/evo.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <masternode/meta.h>
2121
#include <messagesigner.h>
2222
#include <netbase.h>
23+
#include <node/context.h>
2324
#include <rpc/blockchain.h>
2425
#include <rpc/server.h>
2526
#include <rpc/util.h>
@@ -928,6 +929,8 @@ static UniValue protx_update_service_common_wrapper(const JSONRPCRequest& reques
928929

929930
EnsureWalletIsUnlocked(wallet.get());
930931

932+
const NodeContext& node = EnsureAnyNodeContext(request.context);
933+
931934
const bool isV19active{DeploymentActiveAfter(WITH_LOCK(cs_main, return chainman.ActiveChain().Tip();), Params().GetConsensus(), Consensus::DEPLOYMENT_V19)};
932935
const bool is_bls_legacy = !isV19active;
933936
if (isEvoRequested && !isV19active) {
@@ -966,7 +969,7 @@ static UniValue protx_update_service_common_wrapper(const JSONRPCRequest& reques
966969
paramIdx += 3;
967970
}
968971

969-
auto dmn = deterministicMNManager->GetListAtChainTip().GetMN(ptx.proTxHash);
972+
auto dmn = node.dmnman->GetListAtChainTip().GetMN(ptx.proTxHash);
970973
if (!dmn) {
971974
throw std::runtime_error(strprintf("masternode with proTxHash %s not found", ptx.proTxHash.ToString()));
972975
}
@@ -1059,10 +1062,12 @@ static UniValue protx_update_registrar_wrapper(const JSONRPCRequest& request, co
10591062

10601063
EnsureWalletIsUnlocked(wallet.get());
10611064

1065+
const NodeContext& node = EnsureAnyNodeContext(request.context);
1066+
10621067
CProUpRegTx ptx;
10631068
ptx.proTxHash = ParseHashV(request.params[0], "proTxHash");
10641069

1065-
auto dmn = deterministicMNManager->GetListAtChainTip().GetMN(ptx.proTxHash);
1070+
auto dmn = node.dmnman->GetListAtChainTip().GetMN(ptx.proTxHash);
10661071
if (!dmn) {
10671072
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString()));
10681073
}
@@ -1170,6 +1175,8 @@ static UniValue protx_revoke(const JSONRPCRequest& request, const ChainstateMana
11701175

11711176
EnsureWalletIsUnlocked(wallet.get());
11721177

1178+
const NodeContext& node = EnsureAnyNodeContext(request.context);
1179+
11731180
const bool isV19active{DeploymentActiveAfter(WITH_LOCK(cs_main, return chainman.ActiveChain().Tip();), Params().GetConsensus(), Consensus::DEPLOYMENT_V19)};
11741181
const bool is_bls_legacy = !isV19active;
11751182
CProUpRevTx ptx;
@@ -1186,7 +1193,7 @@ static UniValue protx_revoke(const JSONRPCRequest& request, const ChainstateMana
11861193
ptx.nReason = (uint16_t)nReason;
11871194
}
11881195

1189-
auto dmn = deterministicMNManager->GetListAtChainTip().GetMN(ptx.proTxHash);
1196+
auto dmn = node.dmnman->GetListAtChainTip().GetMN(ptx.proTxHash);
11901197
if (!dmn) {
11911198
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString()));
11921199
}
@@ -1281,7 +1288,7 @@ static bool CheckWalletOwnsScript(CWallet* pwallet, const CScript& script) {
12811288
}
12821289
#endif
12831290

1284-
static UniValue BuildDMNListEntry(CWallet* pwallet, const CDeterministicMN& dmn, bool detailed, const ChainstateManager& chainman, const CBlockIndex* pindex = nullptr)
1291+
static UniValue BuildDMNListEntry(CWallet* pwallet, const CDeterministicMN& dmn, CMasternodeMetaMan& mn_metaman, bool detailed, const ChainstateManager& chainman, const CBlockIndex* pindex = nullptr)
12851292
{
12861293
if (!detailed) {
12871294
return dmn.proTxHash.ToString();
@@ -1329,8 +1336,7 @@ static UniValue BuildDMNListEntry(CWallet* pwallet, const CDeterministicMN& dmn,
13291336
}
13301337
#endif
13311338

1332-
CHECK_NONFATAL(mmetaman != nullptr);
1333-
auto metaInfo = mmetaman->GetMetaInfo(dmn.proTxHash);
1339+
const auto metaInfo = mn_metaman.GetMetaInfo(dmn.proTxHash);
13341340
o.pushKV("metaInfo", metaInfo->ToJson());
13351341

13361342
return o;
@@ -1348,6 +1354,8 @@ static UniValue protx_list(const JSONRPCRequest& request, const ChainstateManage
13481354
}
13491355
#endif
13501356

1357+
const NodeContext& node = EnsureAnyNodeContext(request.context);
1358+
13511359
std::string type = "registered";
13521360
if (!request.params[0].isNull()) {
13531361
type = request.params[0].get_str();
@@ -1384,14 +1392,14 @@ static UniValue protx_list(const JSONRPCRequest& request, const ChainstateManage
13841392
setOutpts.emplace(outpt);
13851393
}
13861394

1387-
CDeterministicMNList mnList = deterministicMNManager->GetListForBlock(chainman.ActiveChain()[height]);
1395+
CDeterministicMNList mnList = node.dmnman->GetListForBlock(chainman.ActiveChain()[height]);
13881396
mnList.ForEachMN(false, [&](const auto& dmn) {
13891397
if (setOutpts.count(dmn.collateralOutpoint) ||
13901398
CheckWalletOwnsKey(wallet.get(), dmn.pdmnState->keyIDOwner) ||
13911399
CheckWalletOwnsKey(wallet.get(), dmn.pdmnState->keyIDVoting) ||
13921400
CheckWalletOwnsScript(wallet.get(), dmn.pdmnState->scriptPayout) ||
13931401
CheckWalletOwnsScript(wallet.get(), dmn.pdmnState->scriptOperatorPayout)) {
1394-
ret.push_back(BuildDMNListEntry(wallet.get(), dmn, detailed, chainman));
1402+
ret.push_back(BuildDMNListEntry(wallet.get(), dmn, *node.mn_metaman, detailed, chainman));
13951403
}
13961404
});
13971405
#endif
@@ -1409,12 +1417,12 @@ static UniValue protx_list(const JSONRPCRequest& request, const ChainstateManage
14091417
throw JSONRPCError(RPC_INVALID_PARAMETER, "invalid height specified");
14101418
}
14111419

1412-
CDeterministicMNList mnList = deterministicMNManager->GetListForBlock(chainman.ActiveChain()[height]);
1420+
CDeterministicMNList mnList = node.dmnman->GetListForBlock(chainman.ActiveChain()[height]);
14131421
bool onlyValid = type == "valid";
14141422
bool onlyEvoNodes = type == "evo";
14151423
mnList.ForEachMN(onlyValid, [&](const auto& dmn) {
14161424
if (onlyEvoNodes && dmn.nType != MnType::Evo) return;
1417-
ret.push_back(BuildDMNListEntry(wallet.get(), dmn, detailed, chainman));
1425+
ret.push_back(BuildDMNListEntry(wallet.get(), dmn, *node.mn_metaman, detailed, chainman));
14181426
});
14191427
} else {
14201428
throw JSONRPCError(RPC_INVALID_PARAMETER, "invalid type specified");
@@ -1455,6 +1463,8 @@ static UniValue protx_info(const JSONRPCRequest& request, const ChainstateManage
14551463
}
14561464
#endif
14571465

1466+
const NodeContext& node = EnsureAnyNodeContext(request.context);
1467+
14581468
if (g_txindex) {
14591469
g_txindex->BlockUntilSyncedToCurrentChain();
14601470
}
@@ -1475,12 +1485,12 @@ static UniValue protx_info(const JSONRPCRequest& request, const ChainstateManage
14751485
}
14761486
}
14771487

1478-
auto mnList = deterministicMNManager->GetListForBlock(pindex);
1488+
auto mnList = node.dmnman->GetListForBlock(pindex);
14791489
auto dmn = mnList.GetMN(proTxHash);
14801490
if (!dmn) {
14811491
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s not found", proTxHash.ToString()));
14821492
}
1483-
return BuildDMNListEntry(wallet.get(), *dmn, true, chainman, pindex);
1493+
return BuildDMNListEntry(wallet.get(), *dmn, *node.mn_metaman, true, chainman, pindex);
14841494
}
14851495

14861496
static void protx_diff_help(const JSONRPCRequest& request)
@@ -1569,6 +1579,8 @@ static UniValue protx_listdiff(const JSONRPCRequest& request, const ChainstateMa
15691579
{
15701580
protx_listdiff_help(request);
15711581

1582+
const NodeContext& node = EnsureAnyNodeContext(request.context);
1583+
15721584
LOCK(cs_main);
15731585
UniValue ret(UniValue::VOBJ);
15741586

@@ -1586,8 +1598,8 @@ static UniValue protx_listdiff(const JSONRPCRequest& request, const ChainstateMa
15861598
ret.pushKV("baseHeight", pBaseBlockIndex->nHeight);
15871599
ret.pushKV("blockHeight", pTargetBlockIndex->nHeight);
15881600

1589-
auto baseBlockMNList = deterministicMNManager->GetListForBlock(pBaseBlockIndex);
1590-
auto blockMNList = deterministicMNManager->GetListForBlock(pTargetBlockIndex);
1601+
auto baseBlockMNList = node.dmnman->GetListForBlock(pBaseBlockIndex);
1602+
auto blockMNList = node.dmnman->GetListForBlock(pTargetBlockIndex);
15911603

15921604
auto mnDiff = baseBlockMNList.BuildDiff(blockMNList);
15931605

0 commit comments

Comments
 (0)