refactor: use NodeContext members instead of globals in RPC logic

This commit is contained in:
Kittywhiskers Van Gogh 2024-02-20 04:26:12 +00:00 committed by pasta
parent 2efebcac81
commit e8270ca91b
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
6 changed files with 131 additions and 91 deletions

View File

@ -20,6 +20,7 @@
#include <masternode/meta.h>
#include <messagesigner.h>
#include <netbase.h>
#include <node/context.h>
#include <rpc/blockchain.h>
#include <rpc/server.h>
#include <rpc/util.h>
@ -928,6 +929,8 @@ static UniValue protx_update_service_common_wrapper(const JSONRPCRequest& reques
EnsureWalletIsUnlocked(wallet.get());
const NodeContext& node = EnsureAnyNodeContext(request.context);
const bool isV19active{DeploymentActiveAfter(WITH_LOCK(cs_main, return chainman.ActiveChain().Tip();), Params().GetConsensus(), Consensus::DEPLOYMENT_V19)};
const bool is_bls_legacy = !isV19active;
if (isEvoRequested && !isV19active) {
@ -966,7 +969,7 @@ static UniValue protx_update_service_common_wrapper(const JSONRPCRequest& reques
paramIdx += 3;
}
auto dmn = deterministicMNManager->GetListAtChainTip().GetMN(ptx.proTxHash);
auto dmn = node.dmnman->GetListAtChainTip().GetMN(ptx.proTxHash);
if (!dmn) {
throw std::runtime_error(strprintf("masternode with proTxHash %s not found", ptx.proTxHash.ToString()));
}
@ -1059,10 +1062,12 @@ static UniValue protx_update_registrar_wrapper(const JSONRPCRequest& request, co
EnsureWalletIsUnlocked(wallet.get());
const NodeContext& node = EnsureAnyNodeContext(request.context);
CProUpRegTx ptx;
ptx.proTxHash = ParseHashV(request.params[0], "proTxHash");
auto dmn = deterministicMNManager->GetListAtChainTip().GetMN(ptx.proTxHash);
auto dmn = node.dmnman->GetListAtChainTip().GetMN(ptx.proTxHash);
if (!dmn) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString()));
}
@ -1170,6 +1175,8 @@ static UniValue protx_revoke(const JSONRPCRequest& request, const ChainstateMana
EnsureWalletIsUnlocked(wallet.get());
const NodeContext& node = EnsureAnyNodeContext(request.context);
const bool isV19active{DeploymentActiveAfter(WITH_LOCK(cs_main, return chainman.ActiveChain().Tip();), Params().GetConsensus(), Consensus::DEPLOYMENT_V19)};
const bool is_bls_legacy = !isV19active;
CProUpRevTx ptx;
@ -1186,7 +1193,7 @@ static UniValue protx_revoke(const JSONRPCRequest& request, const ChainstateMana
ptx.nReason = (uint16_t)nReason;
}
auto dmn = deterministicMNManager->GetListAtChainTip().GetMN(ptx.proTxHash);
auto dmn = node.dmnman->GetListAtChainTip().GetMN(ptx.proTxHash);
if (!dmn) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString()));
}
@ -1281,7 +1288,7 @@ static bool CheckWalletOwnsScript(CWallet* pwallet, const CScript& script) {
}
#endif
static UniValue BuildDMNListEntry(CWallet* pwallet, const CDeterministicMN& dmn, bool detailed, const ChainstateManager& chainman, const CBlockIndex* pindex = nullptr)
static UniValue BuildDMNListEntry(CWallet* pwallet, const CDeterministicMN& dmn, CMasternodeMetaMan& mn_metaman, bool detailed, const ChainstateManager& chainman, const CBlockIndex* pindex = nullptr)
{
if (!detailed) {
return dmn.proTxHash.ToString();
@ -1329,8 +1336,7 @@ static UniValue BuildDMNListEntry(CWallet* pwallet, const CDeterministicMN& dmn,
}
#endif
CHECK_NONFATAL(mmetaman != nullptr);
auto metaInfo = mmetaman->GetMetaInfo(dmn.proTxHash);
const auto metaInfo = mn_metaman.GetMetaInfo(dmn.proTxHash);
o.pushKV("metaInfo", metaInfo->ToJson());
return o;
@ -1348,6 +1354,8 @@ static UniValue protx_list(const JSONRPCRequest& request, const ChainstateManage
}
#endif
const NodeContext& node = EnsureAnyNodeContext(request.context);
std::string type = "registered";
if (!request.params[0].isNull()) {
type = request.params[0].get_str();
@ -1384,14 +1392,14 @@ static UniValue protx_list(const JSONRPCRequest& request, const ChainstateManage
setOutpts.emplace(outpt);
}
CDeterministicMNList mnList = deterministicMNManager->GetListForBlock(chainman.ActiveChain()[height]);
CDeterministicMNList mnList = node.dmnman->GetListForBlock(chainman.ActiveChain()[height]);
mnList.ForEachMN(false, [&](const auto& dmn) {
if (setOutpts.count(dmn.collateralOutpoint) ||
CheckWalletOwnsKey(wallet.get(), dmn.pdmnState->keyIDOwner) ||
CheckWalletOwnsKey(wallet.get(), dmn.pdmnState->keyIDVoting) ||
CheckWalletOwnsScript(wallet.get(), dmn.pdmnState->scriptPayout) ||
CheckWalletOwnsScript(wallet.get(), dmn.pdmnState->scriptOperatorPayout)) {
ret.push_back(BuildDMNListEntry(wallet.get(), dmn, detailed, chainman));
ret.push_back(BuildDMNListEntry(wallet.get(), dmn, *node.mn_metaman, detailed, chainman));
}
});
#endif
@ -1409,12 +1417,12 @@ static UniValue protx_list(const JSONRPCRequest& request, const ChainstateManage
throw JSONRPCError(RPC_INVALID_PARAMETER, "invalid height specified");
}
CDeterministicMNList mnList = deterministicMNManager->GetListForBlock(chainman.ActiveChain()[height]);
CDeterministicMNList mnList = node.dmnman->GetListForBlock(chainman.ActiveChain()[height]);
bool onlyValid = type == "valid";
bool onlyEvoNodes = type == "evo";
mnList.ForEachMN(onlyValid, [&](const auto& dmn) {
if (onlyEvoNodes && dmn.nType != MnType::Evo) return;
ret.push_back(BuildDMNListEntry(wallet.get(), dmn, detailed, chainman));
ret.push_back(BuildDMNListEntry(wallet.get(), dmn, *node.mn_metaman, detailed, chainman));
});
} else {
throw JSONRPCError(RPC_INVALID_PARAMETER, "invalid type specified");
@ -1455,6 +1463,8 @@ static UniValue protx_info(const JSONRPCRequest& request, const ChainstateManage
}
#endif
const NodeContext& node = EnsureAnyNodeContext(request.context);
if (g_txindex) {
g_txindex->BlockUntilSyncedToCurrentChain();
}
@ -1475,12 +1485,12 @@ static UniValue protx_info(const JSONRPCRequest& request, const ChainstateManage
}
}
auto mnList = deterministicMNManager->GetListForBlock(pindex);
auto mnList = node.dmnman->GetListForBlock(pindex);
auto dmn = mnList.GetMN(proTxHash);
if (!dmn) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s not found", proTxHash.ToString()));
}
return BuildDMNListEntry(wallet.get(), *dmn, true, chainman, pindex);
return BuildDMNListEntry(wallet.get(), *dmn, *node.mn_metaman, true, chainman, pindex);
}
static void protx_diff_help(const JSONRPCRequest& request)
@ -1569,6 +1579,8 @@ static UniValue protx_listdiff(const JSONRPCRequest& request, const ChainstateMa
{
protx_listdiff_help(request);
const NodeContext& node = EnsureAnyNodeContext(request.context);
LOCK(cs_main);
UniValue ret(UniValue::VOBJ);
@ -1586,8 +1598,8 @@ static UniValue protx_listdiff(const JSONRPCRequest& request, const ChainstateMa
ret.pushKV("baseHeight", pBaseBlockIndex->nHeight);
ret.pushKV("blockHeight", pTargetBlockIndex->nHeight);
auto baseBlockMNList = deterministicMNManager->GetListForBlock(pBaseBlockIndex);
auto blockMNList = deterministicMNManager->GetListForBlock(pTargetBlockIndex);
auto baseBlockMNList = node.dmnman->GetListForBlock(pBaseBlockIndex);
auto blockMNList = node.dmnman->GetListForBlock(pTargetBlockIndex);
auto mnDiff = baseBlockMNList.BuildDiff(blockMNList);

View File

@ -52,7 +52,8 @@ static UniValue gobject_count(const JSONRPCRequest& request)
if (strMode != "json" && strMode != "all")
gobject_count_help(request);
return strMode == "json" ? governance->ToJson() : governance->ToString();
const NodeContext& node = EnsureAnyNodeContext(request.context);
return strMode == "json" ? node.govman->ToJson() : node.govman->ToString();
}
static void gobject_deserialize_help(const JSONRPCRequest& request)
@ -304,11 +305,13 @@ static UniValue gobject_submit(const JSONRPCRequest& request)
{
gobject_submit_help(request);
if(!::masternodeSync->IsBlockchainSynced()) {
const NodeContext& node = EnsureAnyNodeContext(request.context);
if(!node.mn_sync->IsBlockchainSynced()) {
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Must wait for client to sync with masternode network. Try again in a minute or so.");
}
auto mnList = deterministicMNManager->GetListAtChainTip();
auto mnList = node.dmnman->GetListAtChainTip();
bool fMnFound = WITH_LOCK(activeMasternodeInfoCs, return mnList.HasValidMNByCollateral(activeMasternodeInfo.outpoint));
LogPrint(BCLog::GOBJECT, "gobject_submit -- pubKeyOperator = %s, outpoint = %s, params.size() = %lld, fMnFound = %d\n",
@ -354,7 +357,6 @@ static UniValue gobject_submit(const JSONRPCRequest& request)
std::string strHash = govobj.GetHash().ToString();
const NodeContext& node = EnsureAnyNodeContext(request.context);
const CTxMemPool& mempool = EnsureMemPool(node);
bool fMissingConfirmations;
{
@ -372,7 +374,7 @@ static UniValue gobject_submit(const JSONRPCRequest& request)
// RELAY THIS OBJECT
// Reject if rate check fails but don't update buffer
if (!governance->MasternodeRateCheck(govobj)) {
if (!node.govman->MasternodeRateCheck(govobj)) {
LogPrintf("gobject(submit) -- Object submission rejected because of rate check failure - hash = %s\n", strHash);
throw JSONRPCError(RPC_INVALID_PARAMETER, "Object creation rate limit exceeded");
}
@ -380,10 +382,10 @@ static UniValue gobject_submit(const JSONRPCRequest& request)
LogPrintf("gobject(submit) -- Adding locally created governance object - %s\n", strHash);
if (fMissingConfirmations) {
governance->AddPostponedObject(govobj);
node.govman->AddPostponedObject(govobj);
govobj.Relay(*node.connman);
} else {
governance->AddGovernanceObject(govobj, *node.connman);
node.govman->AddGovernanceObject(govobj, *node.connman);
}
return govobj.GetHash().ToString();
@ -395,8 +397,8 @@ static UniValue VoteWithMasternodes(const JSONRPCRequest& request, const std::ma
{
const NodeContext& node = EnsureAnyNodeContext(request.context);
{
LOCK(governance->cs);
const CGovernanceObject *pGovObj = governance->FindConstGovernanceObject(hash);
LOCK(node.govman->cs);
const CGovernanceObject *pGovObj = node.govman->FindConstGovernanceObject(hash);
if (!pGovObj) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Governance object not found");
}
@ -405,7 +407,7 @@ static UniValue VoteWithMasternodes(const JSONRPCRequest& request, const std::ma
int nSuccessful = 0;
int nFailed = 0;
auto mnList = deterministicMNManager->GetListAtChainTip();
auto mnList = node.dmnman->GetListAtChainTip();
UniValue resultsObj(UniValue::VOBJ);
@ -434,7 +436,7 @@ static UniValue VoteWithMasternodes(const JSONRPCRequest& request, const std::ma
}
CGovernanceException exception;
if (governance->ProcessVoteAndRelay(vote, exception, *node.connman)) {
if (node.govman->ProcessVoteAndRelay(vote, exception, *node.connman)) {
nSuccessful++;
statusObj.pushKV("result", "success");
} else {
@ -476,6 +478,8 @@ static UniValue gobject_vote_many(const JSONRPCRequest& request)
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
const NodeContext& node = EnsureAnyNodeContext(request.context);
uint256 hash(ParseHashV(request.params[0], "Object hash"));
std::string strVoteSignal = request.params[1].get_str();
std::string strVoteOutcome = request.params[2].get_str();
@ -501,7 +505,7 @@ static UniValue gobject_vote_many(const JSONRPCRequest& request)
std::map<uint256, CKey> votingKeys;
auto mnList = deterministicMNManager->GetListAtChainTip();
auto mnList = node.dmnman->GetListAtChainTip();
mnList.ForEachMN(true, [&](auto& dmn) {
CKey votingKey;
if (spk_man->GetKey(dmn.pdmnState->keyIDVoting, votingKey)) {
@ -535,6 +539,8 @@ static UniValue gobject_vote_alias(const JSONRPCRequest& request)
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
const NodeContext& node = EnsureAnyNodeContext(request.context);
uint256 hash(ParseHashV(request.params[0], "Object hash"));
std::string strVoteSignal = request.params[1].get_str();
std::string strVoteOutcome = request.params[2].get_str();
@ -554,7 +560,7 @@ static UniValue gobject_vote_alias(const JSONRPCRequest& request)
EnsureWalletIsUnlocked(wallet.get());
uint256 proTxHash(ParseHashV(request.params[3], "protx-hash"));
auto dmn = deterministicMNManager->GetListAtChainTip().GetValidMN(proTxHash);
auto dmn = node.dmnman->GetListAtChainTip().GetValidMN(proTxHash);
if (!dmn) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid or unknown proTxHash");
}
@ -576,7 +582,8 @@ static UniValue gobject_vote_alias(const JSONRPCRequest& request)
}
#endif
static UniValue ListObjects(const std::string& strCachedSignal, const std::string& strType, int nStartTime)
static UniValue ListObjects(CGovernanceManager& govman, const std::string& strCachedSignal,
const std::string& strType, int nStartTime)
{
UniValue objResult(UniValue::VOBJ);
@ -586,12 +593,12 @@ static UniValue ListObjects(const std::string& strCachedSignal, const std::strin
g_txindex->BlockUntilSyncedToCurrentChain();
}
LOCK2(cs_main, governance->cs);
LOCK2(cs_main, govman.cs);
std::vector<CGovernanceObject> objs;
governance->GetAllNewerThan(objs, nStartTime);
govman.GetAllNewerThan(objs, nStartTime);
governance->UpdateLastDiffTime(GetTime());
govman.UpdateLastDiffTime(GetTime());
// CREATE RESULTS FOR USER
for (const auto& govObj : objs) {
@ -667,7 +674,8 @@ static UniValue gobject_list(const JSONRPCRequest& request)
if (strType != "proposals" && strType != "triggers" && strType != "all")
return "Invalid type, should be 'proposals', 'triggers' or 'all'";
return ListObjects(strCachedSignal, strType, 0);
const NodeContext& node = EnsureAnyNodeContext(request.context);
return ListObjects(*node.govman, strCachedSignal, strType, 0);
}
static void gobject_diff_help(const JSONRPCRequest& request)
@ -701,7 +709,8 @@ static UniValue gobject_diff(const JSONRPCRequest& request)
if (strType != "proposals" && strType != "triggers" && strType != "all")
return "Invalid type, should be 'proposals', 'triggers' or 'all'";
return ListObjects(strCachedSignal, strType, governance->GetLastDiffTime());
const NodeContext& node = EnsureAnyNodeContext(request.context);
return ListObjects(*node.govman, strCachedSignal, strType, node.govman->GetLastDiffTime());
}
static void gobject_get_help(const JSONRPCRequest& request)
@ -728,8 +737,10 @@ static UniValue gobject_get(const JSONRPCRequest& request)
}
// FIND THE GOVERNANCE OBJECT THE USER IS LOOKING FOR
LOCK2(cs_main, governance->cs);
const CGovernanceObject* pGovObj = governance->FindConstGovernanceObject(hash);
const NodeContext& node = EnsureAnyNodeContext(request.context);
LOCK2(cs_main, node.govman->cs);
const CGovernanceObject* pGovObj = node.govman->FindConstGovernanceObject(hash);
if (pGovObj == nullptr) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown governance object");
@ -824,10 +835,10 @@ static UniValue gobject_getcurrentvotes(const JSONRPCRequest& request)
}
// FIND OBJECT USER IS LOOKING FOR
const NodeContext& node = EnsureAnyNodeContext(request.context);
LOCK(governance->cs);
const CGovernanceObject* pGovObj = governance->FindConstGovernanceObject(hash);
LOCK(node.govman->cs);
const CGovernanceObject* pGovObj = node.govman->FindConstGovernanceObject(hash);
if (pGovObj == nullptr) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown governance-hash");
@ -839,7 +850,7 @@ static UniValue gobject_getcurrentvotes(const JSONRPCRequest& request)
// GET MATCHING VOTES BY HASH, THEN SHOW USERS VOTE INFORMATION
std::vector<CGovernanceVote> vecVotes = governance->GetCurrentVotes(hash, mnCollateralOutpoint);
std::vector<CGovernanceVote> vecVotes = node.govman->GetCurrentVotes(hash, mnCollateralOutpoint);
for (const auto& vote : vecVotes) {
bResult.pushKV(vote.GetHash().ToString(), vote.ToString());
}
@ -962,9 +973,10 @@ static UniValue voteraw(const JSONRPCRequest& request)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid vote outcome. Please use one of the following: 'yes', 'no' or 'abstain'");
}
GovernanceObject govObjType = WITH_LOCK(governance->cs, return [&](){
AssertLockHeld(governance->cs);
const CGovernanceObject *pGovObj = governance->FindConstGovernanceObject(hashGovObj);
const NodeContext& node = EnsureAnyNodeContext(request.context);
GovernanceObject govObjType = WITH_LOCK(node.govman->cs, return [&](){
AssertLockHeld(node.govman->cs);
const CGovernanceObject *pGovObj = node.govman->FindConstGovernanceObject(hashGovObj);
if (!pGovObj) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Governance object not found");
}
@ -981,7 +993,7 @@ static UniValue voteraw(const JSONRPCRequest& request)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Malformed base64 encoding");
}
auto dmn = deterministicMNManager->GetListAtChainTip().GetValidMNByCollateral(outpoint);
auto dmn = node.dmnman->GetListAtChainTip().GetValidMNByCollateral(outpoint);
if (!dmn) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Failure to find masternode in list : " + outpoint.ToStringShort());
@ -998,8 +1010,7 @@ static UniValue voteraw(const JSONRPCRequest& request)
}
CGovernanceException exception;
const NodeContext& node = EnsureAnyNodeContext(request.context);
if (governance->ProcessVoteAndRelay(vote, exception, *node.connman)) {
if (node.govman->ProcessVoteAndRelay(vote, exception, *node.connman)) {
return "Voted successfully";
} else {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Error voting : " + exception.GetMessage());
@ -1032,7 +1043,9 @@ static UniValue getgovernanceinfo(const JSONRPCRequest& request)
int nLastSuperblock = 0, nNextSuperblock = 0;
const NodeContext& node = EnsureAnyNodeContext(request.context);
const ChainstateManager& chainman = EnsureAnyChainman(request.context);
const auto* pindex = WITH_LOCK(cs_main, return chainman.ActiveChain().Tip());
int nBlockHeight = pindex->nHeight;
@ -1045,7 +1058,7 @@ static UniValue getgovernanceinfo(const JSONRPCRequest& request)
obj.pushKV("superblockmaturitywindow", Params().GetConsensus().nSuperblockMaturityWindow);
obj.pushKV("lastsuperblock", nLastSuperblock);
obj.pushKV("nextsuperblock", nNextSuperblock);
obj.pushKV("fundingthreshold", int(deterministicMNManager->GetListAtChainTip().GetValidWeightedMNsCount() / 10));
obj.pushKV("fundingthreshold", int(node.dmnman->GetListAtChainTip().GetValidWeightedMNsCount() / 10));
obj.pushKV("governancebudget", ValueFromAmount(CSuperblock::GetPaymentsLimit(nNextSuperblock)));
return obj;

View File

@ -106,7 +106,9 @@ static UniValue masternode_count(const JSONRPCRequest& request)
{
masternode_count_help(request);
auto mnList = deterministicMNManager->GetListAtChainTip();
const NodeContext& node = EnsureAnyNodeContext(request.context);
auto mnList = node.dmnman->GetListAtChainTip();
int total = mnList.GetAllMNsCount();
int enabled = mnList.GetValidMNsCount();
@ -133,10 +135,10 @@ static UniValue masternode_count(const JSONRPCRequest& request)
return obj;
}
static UniValue GetNextMasternodeForPayment(int heightShift)
static UniValue GetNextMasternodeForPayment(CDeterministicMNManager& dmnman, int heightShift)
{
const CBlockIndex *tip = WITH_LOCK(::cs_main, return ::ChainActive().Tip());
auto mnList = deterministicMNManager->GetListForBlock(tip);
auto mnList = dmnman.GetListForBlock(tip);
auto payees = mnList.GetProjectedMNPayees(tip, heightShift);
if (payees.empty())
return "unknown";
@ -173,7 +175,9 @@ static void masternode_winner_help(const JSONRPCRequest& request)
static UniValue masternode_winner(const JSONRPCRequest& request)
{
masternode_winner_help(request);
return GetNextMasternodeForPayment(10);
const NodeContext& node = EnsureAnyNodeContext(request.context);
return GetNextMasternodeForPayment(*node.dmnman, 10);
}
static void masternode_current_help(const JSONRPCRequest& request)
@ -193,7 +197,9 @@ static void masternode_current_help(const JSONRPCRequest& request)
static UniValue masternode_current(const JSONRPCRequest& request)
{
masternode_current_help(request);
return GetNextMasternodeForPayment(1);
const NodeContext& node = EnsureAnyNodeContext(request.context);
return GetNextMasternodeForPayment(*node.dmnman, 1);
}
#ifdef ENABLE_WALLET
@ -253,8 +259,9 @@ static UniValue masternode_status(const JSONRPCRequest& request)
if (!fMasternodeMode)
throw JSONRPCError(RPC_INTERNAL_ERROR, "This is not a masternode");
UniValue mnObj(UniValue::VOBJ);
const NodeContext& node = EnsureAnyNodeContext(request.context);
UniValue mnObj(UniValue::VOBJ);
CDeterministicMNCPtr dmn;
{
LOCK(activeMasternodeInfoCs);
@ -262,7 +269,7 @@ static UniValue masternode_status(const JSONRPCRequest& request)
// keep compatibility with legacy status for now (might get deprecated/removed later)
mnObj.pushKV("outpoint", activeMasternodeInfo.outpoint.ToStringShort());
mnObj.pushKV("service", activeMasternodeInfo.service.ToString());
dmn = deterministicMNManager->GetListAtChainTip().GetMN(activeMasternodeInfo.proTxHash);
dmn = node.dmnman->GetListAtChainTip().GetMN(activeMasternodeInfo.proTxHash);
}
if (dmn) {
mnObj.pushKV("proTxHash", dmn->proTxHash.ToString());
@ -277,7 +284,7 @@ static UniValue masternode_status(const JSONRPCRequest& request)
return mnObj;
}
static std::string GetRequiredPaymentsString(int nBlockHeight, const CDeterministicMNCPtr &payee)
static std::string GetRequiredPaymentsString(CGovernanceManager& govman, int nBlockHeight, const CDeterministicMNCPtr &payee)
{
std::string strPayments = "Unknown";
if (payee) {
@ -293,9 +300,9 @@ static std::string GetRequiredPaymentsString(int nBlockHeight, const CDeterminis
strPayments += ", " + EncodeDestination(dest);
}
}
if (CSuperblockManager::IsSuperblockTriggered(*governance, nBlockHeight)) {
if (CSuperblockManager::IsSuperblockTriggered(govman, nBlockHeight)) {
std::vector<CTxOut> voutSuperblock;
if (!CSuperblockManager::GetSuperblockPayments(*governance, nBlockHeight, voutSuperblock)) {
if (!CSuperblockManager::GetSuperblockPayments(govman, nBlockHeight, voutSuperblock)) {
return strPayments + ", error";
}
std::string strSBPayees = "Unknown";
@ -353,24 +360,26 @@ static UniValue masternode_winners(const JSONRPCRequest& request, const Chainsta
int nChainTipHeight = pindexTip->nHeight;
int nStartHeight = std::max(nChainTipHeight - nCount, 1);
const NodeContext& node = EnsureAnyNodeContext(request.context);
for (int h = nStartHeight; h <= nChainTipHeight; h++) {
const CBlockIndex* pIndex = pindexTip->GetAncestor(h - 1);
auto payee = deterministicMNManager->GetListForBlock(pIndex).GetMNPayee(pIndex);
std::string strPayments = GetRequiredPaymentsString(h, payee);
auto payee = node.dmnman->GetListForBlock(pIndex).GetMNPayee(pIndex);
std::string strPayments = GetRequiredPaymentsString(*node.govman, h, payee);
if (strFilter != "" && strPayments.find(strFilter) == std::string::npos) continue;
obj.pushKV(strprintf("%d", h), strPayments);
}
auto projection = deterministicMNManager->GetListForBlock(pindexTip).GetProjectedMNPayees(pindexTip, 20);
auto projection = node.dmnman->GetListForBlock(pindexTip).GetProjectedMNPayees(pindexTip, 20);
for (size_t i = 0; i < projection.size(); i++) {
int h = nChainTipHeight + 1 + i;
std::string strPayments = GetRequiredPaymentsString(h, projection[i]);
std::string strPayments = GetRequiredPaymentsString(*node.govman, h, projection[i]);
if (strFilter != "" && strPayments.find(strFilter) == std::string::npos) continue;
obj.pushKV(strprintf("%d", h), strPayments);
}
return obj;
}
static void masternode_payments_help(const JSONRPCRequest& request)
{
RPCHelpMan{"masternode payments",
@ -432,8 +441,8 @@ static UniValue masternode_payments(const JSONRPCRequest& request, const Chainst
// A temporary vector which is used to sort results properly (there is no "reverse" in/for UniValue)
std::vector<UniValue> vecPayments;
const NodeContext& node = EnsureAnyNodeContext(request.context);
while (vecPayments.size() < uint64_t(std::abs(nCount)) && pindex != nullptr) {
CBlock block;
if (!ReadBlockFromDisk(block, pindex, Params().GetConsensus())) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
@ -459,7 +468,7 @@ static UniValue masternode_payments(const JSONRPCRequest& request, const Chainst
std::vector<CTxOut> voutMasternodePayments, voutDummy;
CMutableTransaction dummyTx;
CAmount blockSubsidy = GetBlockSubsidy(pindex, Params().GetConsensus());
MasternodePayments::FillBlockPayments(*sporkManager, *governance, dummyTx, pindex->pprev, blockSubsidy, nBlockFees, voutMasternodePayments, voutDummy);
MasternodePayments::FillBlockPayments(*node.sporkman, *node.govman, dummyTx, pindex->pprev, blockSubsidy, nBlockFees, voutMasternodePayments, voutDummy);
UniValue blockObj(UniValue::VOBJ);
CAmount payedPerBlock{0};
@ -481,7 +490,7 @@ static UniValue masternode_payments(const JSONRPCRequest& request, const Chainst
}
// NOTE: we use _previous_ block to find a payee for the current one
const auto dmnPayee = deterministicMNManager->GetListForBlock(pindex->pprev).GetMNPayee(pindex->pprev);
const auto dmnPayee = node.dmnman->GetListForBlock(pindex->pprev).GetMNPayee(pindex->pprev);
protxObj.pushKV("proTxHash", dmnPayee == nullptr ? "" : dmnPayee->proTxHash.ToString());
protxObj.pushKV("amount", payedPerMasternode);
protxObj.pushKV("payees", payeesArr);
@ -589,9 +598,11 @@ static UniValue masternodelist(const JSONRPCRequest& request, ChainstateManager&
masternode_list_help(request);
}
const NodeContext& node = EnsureAnyNodeContext(request.context);
UniValue obj(UniValue::VOBJ);
auto mnList = deterministicMNManager->GetListAtChainTip();
auto mnList = node.dmnman->GetListAtChainTip();
auto dmnToStatus = [&](auto& dmn) {
if (mnList.IsMNValid(dmn)) {
return "ENABLED";

View File

@ -150,8 +150,9 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
return true;
}
static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& mempool, CEvoDB& evodb,
LLMQContext& llmq_ctx, const CScript& coinbase_script, int nGenerate, uint64_t nMaxTries)
static UniValue generateBlocks(ChainstateManager& chainman, CEvoDB& evodb, CGovernanceManager& govman, CSporkManager& sporkman,
LLMQContext& llmq_ctx, const CTxMemPool& mempool, const CScript& coinbase_script, int nGenerate,
uint64_t nMaxTries)
{
int nHeightEnd = 0;
int nHeight = 0;
@ -166,7 +167,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
UniValue blockHashes(UniValue::VARR);
while (nHeight < nHeightEnd && !ShutdownRequested())
{
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(*sporkManager, *governance, llmq_ctx, evodb, chainman.ActiveChainstate(), mempool, Params()).CreateNewBlock(coinbase_script));
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(sporkman, govman, llmq_ctx, evodb, chainman.ActiveChainstate(), mempool, Params()).CreateNewBlock(coinbase_script));
if (!pblocktemplate.get())
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
CBlock *pblock = &pblocktemplate->block;
@ -253,7 +254,7 @@ static UniValue generatetodescriptor(const JSONRPCRequest& request)
ChainstateManager& chainman = EnsureChainman(node);
LLMQContext& llmq_ctx = EnsureLLMQContext(node);
return generateBlocks(chainman, mempool, *node.evodb, llmq_ctx, coinbase_script, num_blocks, max_tries);
return generateBlocks(chainman, *node.evodb, *node.govman, *node.sporkman, llmq_ctx, mempool, coinbase_script, num_blocks, max_tries);
}
static UniValue generatetoaddress(const JSONRPCRequest& request)
@ -292,7 +293,7 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
CScript coinbase_script = GetScriptForDestination(destination);
return generateBlocks(chainman, mempool, *node.evodb, llmq_ctx, coinbase_script, num_blocks, max_tries);
return generateBlocks(chainman, *node.evodb, *node.govman, *node.sporkman, llmq_ctx, mempool, coinbase_script, num_blocks, max_tries);
}
static UniValue generateblock(const JSONRPCRequest& request)
@ -372,7 +373,7 @@ static UniValue generateblock(const JSONRPCRequest& request)
LOCK(cs_main);
CTxMemPool empty_mempool;
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(*sporkManager, *governance, llmq_ctx, *node.evodb, active_chainstate, empty_mempool, chainparams).CreateNewBlock(coinbase_script));
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(*node.sporkman, *node.govman, llmq_ctx, *node.evodb, active_chainstate, empty_mempool, chainparams).CreateNewBlock(coinbase_script));
if (!blocktemplate) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
}
@ -715,8 +716,8 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, PACKAGE_NAME " is in initial sync and waiting for blocks...");
// next bock is a superblock and we need governance info to correctly construct it
if (AreSuperblocksEnabled(*sporkManager)
&& !::masternodeSync->IsSynced()
if (AreSuperblocksEnabled(*node.sporkman)
&& !node.mn_sync->IsSynced()
&& CSuperblock::IsValidBlockHeight(active_chain.Height() + 1))
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, PACKAGE_NAME " is syncing with network...");
@ -788,7 +789,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
// Create new block
CScript scriptDummy = CScript() << OP_TRUE;
LLMQContext& llmq_ctx = EnsureAnyLLMQContext(request.context);
pblocktemplate = BlockAssembler(*sporkManager, *governance, llmq_ctx, *node.evodb, active_chainstate, mempool, Params()).CreateNewBlock(scriptDummy);
pblocktemplate = BlockAssembler(*node.sporkman, *node.govman, llmq_ctx, *node.evodb, active_chainstate, mempool, Params()).CreateNewBlock(scriptDummy);
if (!pblocktemplate)
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
@ -952,7 +953,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
}
result.pushKV("superblock", superblockObjArray);
result.pushKV("superblocks_started", pindexPrev->nHeight + 1 > consensusParams.nSuperblockStartBlock);
result.pushKV("superblocks_enabled", AreSuperblocksEnabled(*sporkManager));
result.pushKV("superblocks_enabled", AreSuperblocksEnabled(*node.sporkman));
result.pushKV("coinbase_payload", HexStr(pblock->vtx[0]->vExtraPayload));

View File

@ -101,26 +101,29 @@ static UniValue mnsync(const JSONRPCRequest& request)
std::string strMode = request.params[0].get_str();
const NodeContext& node = EnsureAnyNodeContext(request.context);
auto& mn_sync = *node.mn_sync;
if(strMode == "status") {
UniValue objStatus(UniValue::VOBJ);
objStatus.pushKV("AssetID", ::masternodeSync->GetAssetID());
objStatus.pushKV("AssetName", ::masternodeSync->GetAssetName());
objStatus.pushKV("AssetStartTime", ::masternodeSync->GetAssetStartTime());
objStatus.pushKV("Attempt", ::masternodeSync->GetAttempt());
objStatus.pushKV("IsBlockchainSynced", ::masternodeSync->IsBlockchainSynced());
objStatus.pushKV("IsSynced", ::masternodeSync->IsSynced());
objStatus.pushKV("AssetID", mn_sync.GetAssetID());
objStatus.pushKV("AssetName", mn_sync.GetAssetName());
objStatus.pushKV("AssetStartTime", mn_sync.GetAssetStartTime());
objStatus.pushKV("Attempt", mn_sync.GetAttempt());
objStatus.pushKV("IsBlockchainSynced", mn_sync.IsBlockchainSynced());
objStatus.pushKV("IsSynced", mn_sync.IsSynced());
return objStatus;
}
if(strMode == "next")
{
::masternodeSync->SwitchToNextAsset();
return "sync updated to " + ::masternodeSync->GetAssetName();
mn_sync.SwitchToNextAsset();
return "sync updated to " + mn_sync.GetAssetName();
}
if(strMode == "reset")
{
::masternodeSync->Reset(true);
mn_sync.Reset(true);
return "success";
}
return "failure";
@ -157,16 +160,17 @@ static UniValue spork(const JSONRPCRequest& request)
// basic mode, show info
std:: string strCommand = request.params[0].get_str();
const NodeContext& node = EnsureAnyNodeContext(request.context);
if (strCommand == "show") {
UniValue ret(UniValue::VOBJ);
for (const auto& sporkDef : sporkDefs) {
ret.pushKV(std::string(sporkDef.name), sporkManager->GetSporkValue(sporkDef.sporkId));
ret.pushKV(std::string(sporkDef.name), node.sporkman->GetSporkValue(sporkDef.sporkId));
}
return ret;
} else if(strCommand == "active"){
UniValue ret(UniValue::VOBJ);
for (const auto& sporkDef : sporkDefs) {
ret.pushKV(std::string(sporkDef.name), sporkManager->IsSporkActive(sporkDef.sporkId));
ret.pushKV(std::string(sporkDef.name), node.sporkman->IsSporkActive(sporkDef.sporkId));
}
return ret;
}
@ -206,7 +210,7 @@ static UniValue sporkupdate(const JSONRPCRequest& request)
int64_t nValue = request.params[1].get_int64();
// broadcast new spork
if (sporkManager->UpdateSpork(nSporkID, nValue, *node.connman)) {
if (node.sporkman->UpdateSpork(nSporkID, nValue, *node.connman)) {
return "success";
}

View File

@ -381,7 +381,7 @@ static void quorum_memberof_help(const JSONRPCRequest& request)
}.Check(request);
}
static UniValue quorum_memberof(const JSONRPCRequest& request, const ChainstateManager& chainman, const LLMQContext& llmq_ctx)
static UniValue quorum_memberof(const JSONRPCRequest& request, const ChainstateManager& chainman, const NodeContext& node, const LLMQContext& llmq_ctx)
{
quorum_memberof_help(request);
@ -395,8 +395,7 @@ static UniValue quorum_memberof(const JSONRPCRequest& request, const ChainstateM
}
const CBlockIndex* pindexTip = WITH_LOCK(cs_main, return chainman.ActiveChain().Tip());
auto mnList = deterministicMNManager->GetListForBlock(pindexTip);
auto mnList = node.dmnman->GetListForBlock(pindexTip);
auto dmn = mnList.GetMN(protxHash);
if (!dmn) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "masternode not found");
@ -885,7 +884,7 @@ static UniValue _quorum(const JSONRPCRequest& request)
} else if (command == "quorumdkgstatus") {
return quorum_dkgstatus(new_request, chainman, llmq_ctx);
} else if (command == "quorummemberof") {
return quorum_memberof(new_request, chainman, llmq_ctx);
return quorum_memberof(new_request, chainman, node, llmq_ctx);
} else if (command == "quorumsign" || command == "quorumverify" || command == "quorumhasrecsig" || command == "quorumgetrecsig" || command == "quorumisconflicting") {
return quorum_sigs_cmd(new_request, llmq_ctx);
} else if (command == "quorumselectquorum") {