refactor: remove CGovernanceManager global, move to NodeContext

This commit is contained in:
Kittywhiskers Van Gogh 2024-02-28 17:16:12 +00:00
parent 405b8c669a
commit 0aa08ba80d
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
10 changed files with 25 additions and 32 deletions

View File

@ -111,7 +111,7 @@ bool CGovernanceManager::AddNewTrigger(uint256 nHash)
CSuperblock_sptr pSuperblock;
try {
auto pSuperblockTmp = std::make_shared<CSuperblock>(nHash);
auto pSuperblockTmp = std::make_shared<CSuperblock>(*this, nHash);
pSuperblock = pSuperblockTmp;
} catch (std::exception& e) {
LogPrintf("CGovernanceManager::%s -- Error creating superblock: %s\n", __func__, e.what());
@ -125,7 +125,7 @@ bool CGovernanceManager::AddNewTrigger(uint256 nHash)
mapTrigger.insert(std::make_pair(nHash, pSuperblock));
return !pSuperblock->IsExpired(*governance);
return !pSuperblock->IsExpired(*this);
}
/**
@ -166,7 +166,7 @@ void CGovernanceManager::CleanAndRemoveTriggers()
case SeenObjectStatus::Valid:
case SeenObjectStatus::Executed: {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s -- Valid trigger found\n", __func__);
if (pSuperblock->IsExpired(*governance)) {
if (pSuperblock->IsExpired(*this)) {
// update corresponding object
pObj->SetExpired();
remove = true;
@ -369,7 +369,7 @@ bool CSuperblockManager::IsValid(CGovernanceManager& governanceManager, const CT
CSuperblock_sptr pSuperblock;
if (CSuperblockManager::GetBestSuperblock(governanceManager, pSuperblock, nBlockHeight)) {
return pSuperblock->IsValid(txNew, nBlockHeight, blockReward);
return pSuperblock->IsValid(governanceManager, txNew, nBlockHeight, blockReward);
}
return false;
@ -398,13 +398,13 @@ CSuperblock::
}
CSuperblock::
CSuperblock(uint256& nHash) :
CSuperblock(CGovernanceManager& govman, uint256& nHash) :
nGovObjHash(nHash),
nBlockHeight(0),
nStatus(SeenObjectStatus::Unknown),
vecPayments()
{
const CGovernanceObject* pGovObj = GetGovernanceObject(*governance);
const CGovernanceObject* pGovObj = GetGovernanceObject(govman);
if (!pGovObj) {
throw std::runtime_error("CSuperblock: Failed to find Governance Object");
@ -612,7 +612,7 @@ CAmount CSuperblock::GetPaymentsTotalAmount()
* - Does this transaction match the superblock?
*/
bool CSuperblock::IsValid(const CTransaction& txNew, int nBlockHeight, CAmount blockReward)
bool CSuperblock::IsValid(CGovernanceManager& govman, const CTransaction& txNew, int nBlockHeight, CAmount blockReward)
{
// TODO : LOCK(cs);
// No reason for a lock here now since this method only accesses data
@ -631,7 +631,7 @@ bool CSuperblock::IsValid(const CTransaction& txNew, int nBlockHeight, CAmount b
int nMinerAndMasternodePayments = nOutputs - nPayments;
LogPrint(BCLog::GOBJECT, "CSuperblock::IsValid -- nOutputs = %d, nPayments = %d, GetDataAsHexString = %s\n",
nOutputs, nPayments, GetGovernanceObject(*governance)->GetDataAsHexString());
nOutputs, nPayments, GetGovernanceObject(govman)->GetDataAsHexString());
// We require an exact match (including order) between the expected
// superblock payments and the payments actually in the block.

View File

@ -101,7 +101,7 @@ private:
public:
CSuperblock();
CSuperblock(int nBlockHeight, std::vector<CGovernancePayment> vecPayments);
explicit CSuperblock(uint256& nHash);
explicit CSuperblock(CGovernanceManager& govman, uint256& nHash);
static bool IsValidBlockHeight(int nBlockHeight);
static void GetNearestSuperblocksHeights(int nBlockHeight, int& nLastSuperblockRet, int& nNextSuperblockRet);
@ -126,7 +126,7 @@ public:
bool GetPayment(int nPaymentIndex, CGovernancePayment& paymentRet);
CAmount GetPaymentsTotalAmount();
bool IsValid(const CTransaction& txNew, int nBlockHeight, CAmount blockReward);
bool IsValid(CGovernanceManager& govman, const CTransaction& txNew, int nBlockHeight, CAmount blockReward);
bool IsExpired(const CGovernanceManager& governanceManager) const;
std::vector<uint256> GetProposalHashes() const;

View File

@ -25,8 +25,6 @@
#include <util/time.h>
#include <validation.h>
std::unique_ptr<CGovernanceManager> governance;
int nSubmittedFinalBudget;
const std::string GovernanceStore::SERIALIZATION_VERSION_STRING = "CGovernanceManager-Version-16";
@ -270,7 +268,7 @@ void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj, CConnman& c
CGovernanceException e;
if (pairVote.second < nNow) {
fRemove = true;
} else if (govobj.ProcessVote(vote, e)) {
} else if (govobj.ProcessVote(*this, vote, e)) {
vote.Relay(connman);
fRemove = true;
}
@ -1095,7 +1093,7 @@ bool CGovernanceManager::ProcessVote(CNode* pfrom, const CGovernanceVote& vote,
return false;
}
bool fOk = govobj.ProcessVote(vote, exception) && cmapVoteToObject.Insert(nHashVote, &govobj);
bool fOk = govobj.ProcessVote(*this, vote, exception) && cmapVoteToObject.Insert(nHashVote, &govobj);
LEAVE_CRITICAL_SECTION(cs);
return fOk;
}

View File

@ -25,8 +25,6 @@ class CGovernanceObject;
class CGovernanceVote;
class CSporkManager;
extern std::unique_ptr<CGovernanceManager> governance;
static constexpr int RATE_BUFFER_SIZE = 5;
class CDeterministicMNList;

View File

@ -79,7 +79,7 @@ CGovernanceObject::CGovernanceObject(const CGovernanceObject& other) :
{
}
bool CGovernanceObject::ProcessVote(const CGovernanceVote& vote, CGovernanceException& exception)
bool CGovernanceObject::ProcessVote(CGovernanceManager& govman, const CGovernanceVote& vote, CGovernanceException& exception)
{
LOCK(cs);
@ -149,7 +149,7 @@ bool CGovernanceObject::ProcessVote(const CGovernanceVote& vote, CGovernanceExce
int64_t nNow = GetAdjustedTime();
int64_t nVoteTimeUpdate = voteInstanceRef.nTime;
if (governance->AreRateChecksEnabled()) {
if (govman.AreRateChecksEnabled()) {
int64_t nTimeDelta = nNow - voteInstanceRef.nTime;
if (nTimeDelta < GOVERNANCE_UPDATE_MIN) {
std::ostringstream ostr;
@ -175,7 +175,7 @@ bool CGovernanceObject::ProcessVote(const CGovernanceVote& vote, CGovernanceExce
<< ", vote hash = " << vote.GetHash().ToString();
LogPrintf("%s\n", ostr.str());
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR, 20);
governance->AddInvalidVote(vote);
govman.AddInvalidVote(vote);
return false;
}

View File

@ -15,10 +15,10 @@
class CBLSSecretKey;
class CBLSPublicKey;
class CNode;
class CGovernanceManager;
class CGovernanceObject;
class CGovernanceVote;
class CNode;
extern RecursiveMutex cs_main;
@ -288,7 +288,7 @@ public:
void LoadData();
void GetData(UniValue& objResult) const;
bool ProcessVote(const CGovernanceVote& vote, CGovernanceException& exception);
bool ProcessVote(CGovernanceManager& govman, const CGovernanceVote& vote, CGovernanceException& exception);
/// Called when MN's which have voted on this object have been removed
void ClearMasternodeVotes();

View File

@ -314,8 +314,7 @@ void PrepareShutdown(NodeContext& node)
node.mn_sync = nullptr;
::masternodeSync.reset();
node.sporkman.reset();
node.govman = nullptr;
::governance.reset();
node.govman.reset();
// Stop and delete all indexes only after flushing background callbacks.
if (g_txindex) {
@ -1712,9 +1711,8 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
node.chainman = &g_chainman;
ChainstateManager& chainman = *Assert(node.chainman);
assert(!::governance);
::governance = std::make_unique<CGovernanceManager>();
node.govman = ::governance.get();
assert(!node.govman);
node.govman = std::make_unique<CGovernanceManager>();
assert(!node.sporkman);
node.sporkman = std::make_unique<CSporkManager>();

View File

@ -9,6 +9,7 @@
#include <coinjoin/context.h>
#include <evo/creditpool.h>
#include <evo/chainhelper.h>
#include <governance/governance.h>
#include <interfaces/chain.h>
#include <interfaces/coinjoin.h>
#include <llmq/context.h>

View File

@ -73,6 +73,7 @@ struct NodeContext {
//! Dash
std::unique_ptr<CEvoDB> evodb;
std::unique_ptr<CChainstateHelper> chain_helper;
std::unique_ptr<CGovernanceManager> govman;
std::unique_ptr<CJContext> cj_ctx;
std::unique_ptr<CMNHFManager> mnhf_manager;
std::unique_ptr<CSporkManager> sporkman;
@ -80,7 +81,6 @@ struct NodeContext {
CCreditPoolManager* cpoolman{nullptr};
CDeterministicMNManager* dmnman{nullptr};
CDSTXManager* dstxman{nullptr};
CGovernanceManager* govman{nullptr};
CMasternodeMetaMan* mn_metaman{nullptr};
CMasternodeSync* mn_sync{nullptr};
CNetFulfilledRequestManager* netfulfilledman{nullptr};

View File

@ -223,8 +223,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
m_node.connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman); // Deterministic randomness for tests.
m_node.sporkman = std::make_unique<CSporkManager>();
::governance = std::make_unique<CGovernanceManager>();
m_node.govman = ::governance.get();
m_node.govman = std::make_unique<CGovernanceManager>();
::masternodeSync = std::make_unique<CMasternodeSync>(*m_node.connman, *m_node.govman);
m_node.mn_sync = ::masternodeSync.get();
::dstxManager = std::make_unique<CDSTXManager>();
@ -254,8 +253,7 @@ ChainTestingSetup::~ChainTestingSetup()
::dstxManager.reset();
m_node.mn_sync = nullptr;
::masternodeSync.reset();
m_node.govman = nullptr;
::governance.reset();
m_node.govman.reset();
m_node.sporkman.reset();
m_node.connman.reset();
m_node.addrman.reset();