refactor: drop circular dependency governance/classes over governance/governance

This commit is contained in:
Konstantin Akimov 2024-10-06 22:06:15 +07:00
parent 39f18ab154
commit 2e36832982
No known key found for this signature in database
GPG Key ID: 2176C4A5D01EA524
4 changed files with 22 additions and 32 deletions

View File

@ -8,7 +8,6 @@
#include <consensus/validation.h>
#include <core_io.h>
#include <deploymentstatus.h>
#include <governance/governance.h>
#include <key_io.h>
#include <primitives/transaction.h>
#include <script/standard.h>
@ -103,27 +102,20 @@ CSuperblock::
{
}
CSuperblock::
CSuperblock(CGovernanceManager& govman, uint256& nHash) :
CSuperblock::CSuperblock(const CGovernanceObject& govObj, uint256& nHash) :
nGovObjHash(nHash),
nBlockHeight(0),
nStatus(SeenObjectStatus::Unknown),
vecPayments()
{
const CGovernanceObject* pGovObj = govman.FindGovernanceObject(nGovObjHash);
LogPrint(BCLog::GOBJECT, "CSuperblock -- Constructor govobj: %s, nObjectType = %d\n", govObj.GetDataAsPlainString(),
ToUnderlying(govObj.GetObjectType()));
if (!pGovObj) {
throw std::runtime_error("CSuperblock: Failed to find Governance Object");
}
LogPrint(BCLog::GOBJECT, "CSuperblock -- Constructor pGovObj: %s, nObjectType = %d\n",
pGovObj->GetDataAsPlainString(), ToUnderlying(pGovObj->GetObjectType()));
if (pGovObj->GetObjectType() != GovernanceObject::TRIGGER) {
if (govObj.GetObjectType() != GovernanceObject::TRIGGER) {
throw std::runtime_error("CSuperblock: Governance Object not a trigger");
}
UniValue obj = pGovObj->GetJSONObject();
UniValue obj = govObj.GetJSONObject();
if (obj["type"].get_int() != ToUnderlying(GovernanceObject::TRIGGER)) {
throw std::runtime_error("CSuperblock: invalid data type");
@ -296,10 +288,8 @@ CAmount CSuperblock::GetPaymentsTotalAmount()
* - Does this transaction match the superblock?
*/
bool CSuperblock::IsValid(CGovernanceManager& govman, const CChain& active_chain, const CTransaction& txNew, int nBlockHeight, CAmount blockReward)
bool CSuperblock::IsValid(const CChain& active_chain, const CTransaction& txNew, int nBlockHeight, CAmount blockReward)
{
AssertLockHeld(govman.cs);
// TODO : LOCK(cs);
// No reason for a lock here now since this method only accesses data
// internal to *this and since CSuperblock's are accessed only through
@ -316,9 +306,8 @@ bool CSuperblock::IsValid(CGovernanceManager& govman, const CChain& active_chain
int nPayments = CountPayments();
int nMinerAndMasternodePayments = nOutputs - nPayments;
const CGovernanceObject* obj = govman.FindGovernanceObject(nGovObjHash);
LogPrint(BCLog::GOBJECT, "CSuperblock::IsValid -- nOutputs = %d, nPayments = %d, GetDataAsHexString = %s\n",
nOutputs, nPayments, obj ? obj->GetDataAsHexString() : "");
LogPrint(BCLog::GOBJECT, "CSuperblock::IsValid -- nOutputs = %d, nPayments = %d, hash = %s\n", nOutputs, nPayments,
nGovObjHash.ToString());
// We require an exact match (including order) between the expected
// superblock payments and the payments actually in the block.
@ -382,7 +371,7 @@ bool CSuperblock::IsValid(CGovernanceManager& govman, const CChain& active_chain
return true;
}
bool CSuperblock::IsExpired(const CGovernanceManager& govman) const
bool CSuperblock::IsExpired(int heightToTest) const
{
int nExpirationBlocks;
// Executed triggers are kept for another superblock cycle (approximately 1 month for mainnet).
@ -404,14 +393,14 @@ bool CSuperblock::IsExpired(const CGovernanceManager& govman) const
LogPrint(BCLog::GOBJECT, "CSuperblock::IsExpired -- nBlockHeight = %d, nExpirationBlock = %d\n", nBlockHeight, nExpirationBlock);
if (govman.GetCachedBlockHeight() > nExpirationBlock) {
if (heightToTest > nExpirationBlock) {
LogPrint(BCLog::GOBJECT, "CSuperblock::IsExpired -- Outdated trigger found\n");
return true;
}
if (Params().NetworkIDString() != CBaseChainParams::MAIN) {
// NOTE: this can happen on testnet/devnets due to reorgs, should never happen on mainnet
if (govman.GetCachedBlockHeight() + Params().GetConsensus().nSuperblockCycle * 2 < nBlockHeight) {
if (heightToTest + Params().GetConsensus().nSuperblockCycle * 2 < nBlockHeight) {
LogPrint(BCLog::GOBJECT, "CSuperblock::IsExpired -- Trigger is too far into the future\n");
return true;
}

View File

@ -11,7 +11,6 @@
#include <uint256.h>
class CChain;
class CGovernanceManager;
class CSuperblock;
class CTxOut;
class CTransaction;
@ -80,7 +79,7 @@ private:
public:
CSuperblock();
CSuperblock(int nBlockHeight, std::vector<CGovernancePayment> vecPayments);
explicit CSuperblock(CGovernanceManager& govman, uint256& nHash);
explicit CSuperblock(const CGovernanceObject& obj, uint256& nHash);
static bool IsValidBlockHeight(int nBlockHeight);
static void GetNearestSuperblocksHeights(int nBlockHeight, int& nLastSuperblockRet, int& nNextSuperblockRet);
@ -105,8 +104,8 @@ public:
bool GetPayment(int nPaymentIndex, CGovernancePayment& paymentRet);
CAmount GetPaymentsTotalAmount();
bool IsValid(CGovernanceManager& govman, const CChain& active_chain, const CTransaction& txNew, int nBlockHeight, CAmount blockReward);
bool IsExpired(const CGovernanceManager& govman) const;
bool IsValid(const CChain& active_chain, const CTransaction& txNew, int nBlockHeight, CAmount blockReward);
bool IsExpired(int heightToTest) const;
std::vector<uint256> GetProposalHashes() const;
};

View File

@ -1651,8 +1651,11 @@ bool CGovernanceManager::AddNewTrigger(uint256 nHash)
CSuperblock_sptr pSuperblock;
try {
auto pSuperblockTmp = std::make_shared<CSuperblock>(*this, nHash);
pSuperblock = pSuperblockTmp;
const CGovernanceObject* pGovObj = FindGovernanceObject(nHash);
if (!pGovObj) {
throw std::runtime_error("CSuperblock: Failed to find Governance Object");
}
pSuperblock = std::make_shared<CSuperblock>(*pGovObj, nHash);
} catch (std::exception& e) {
LogPrintf("CGovernanceManager::%s -- Error creating superblock: %s\n", __func__, e.what());
return false;
@ -1665,7 +1668,7 @@ bool CGovernanceManager::AddNewTrigger(uint256 nHash)
mapTrigger.insert(std::make_pair(nHash, pSuperblock));
return !pSuperblock->IsExpired(*this);
return !pSuperblock->IsExpired(GetCachedBlockHeight());
}
/**
@ -1707,7 +1710,7 @@ void CGovernanceManager::CleanAndRemoveTriggers()
case SeenObjectStatus::Valid:
case SeenObjectStatus::Executed: {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s -- Valid trigger found\n", __func__);
if (pSuperblock->IsExpired(*this)) {
if (pSuperblock->IsExpired(GetCachedBlockHeight())) {
// update corresponding object
pObj->SetExpired();
remove = true;
@ -1901,7 +1904,7 @@ bool CGovernanceManager::IsValidSuperblock(const CChain& active_chain, const CDe
CSuperblock_sptr pSuperblock;
if (GetBestSuperblock(tip_mn_list, pSuperblock, nBlockHeight)) {
return pSuperblock->IsValid(*this, active_chain, txNew, nBlockHeight, blockReward);
return pSuperblock->IsValid(active_chain, txNew, nBlockHeight, blockReward);
}
return false;

View File

@ -27,7 +27,6 @@ EXPECTED_CIRCULAR_DEPENDENCIES=(
"evo/cbtx -> evo/simplifiedmns -> evo/cbtx"
"evo/deterministicmns -> llmq/commitment -> evo/deterministicmns"
"evo/deterministicmns -> llmq/utils -> evo/deterministicmns"
"governance/classes -> governance/governance -> governance/classes"
"governance/governance -> governance/object -> governance/governance"
"governance/governance -> masternode/sync -> governance/governance"
"llmq/chainlocks -> llmq/instantsend -> llmq/chainlocks"