Skip to content

Commit

Permalink
refactor: use NodeContext members instead of globals in interface logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Feb 19, 2024
1 parent df59c25 commit 72a38de
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/interfaces/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class EVO
public:
virtual ~EVO() {}
virtual std::pair<CDeterministicMNList, const CBlockIndex*> getListAtChainTip() = 0;
virtual void setContext(NodeContext* context) {}
};

//! Interface for the src/governance part of a dash node (dashd process).
Expand All @@ -63,6 +64,7 @@ class GOV
virtual void getAllNewerThan(std::vector<CGovernanceObject> &objs, int64_t nMoreThanTime) = 0;
virtual int32_t getObjAbsYesCount(const CGovernanceObject& obj, vote_signal_enum_t vote_signal) = 0;
virtual bool getObjLocalValidity(const CGovernanceObject& obj, std::string& error, bool check_collateral) = 0;
virtual void setContext(NodeContext* context) {}
};

//! Interface for the src/llmq part of a dash node (dashd process).
Expand All @@ -71,6 +73,7 @@ class LLMQ
public:
virtual ~LLMQ() {}
virtual size_t getInstantSentLockCount() = 0;
virtual void setContext(NodeContext* context) {}
};

//! Interface for the src/masternode part of a dash node (dashd process).
Expand All @@ -83,6 +86,7 @@ class Sync
virtual bool isBlockchainSynced() = 0;
virtual bool isSynced() = 0;
virtual std::string getSyncStatus() = 0;
virtual void setContext(NodeContext* context) {}
};
}

Expand Down
69 changes: 57 additions & 12 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,35 @@ class EVOImpl : public EVO
{
const CBlockIndex *tip = WITH_LOCK(::cs_main, return ::ChainActive().Tip());
CDeterministicMNList mnList{};
if (tip != nullptr && deterministicMNManager != nullptr) {
mnList = deterministicMNManager->GetListForBlock(tip);
if (tip != nullptr && m_context->dmnman != nullptr) {
mnList = m_context->dmnman->GetListForBlock(tip);
}
return {std::move(mnList), tip};
}
void setContext(NodeContext* context) override
{
m_context = context;
}

private:
NodeContext* m_context;
};

class GOVImpl : public GOV
{
public:
void getAllNewerThan(std::vector<CGovernanceObject> &objs, int64_t nMoreThanTime) override
{
if (::governance != nullptr) {
return ::governance->GetAllNewerThan(objs, nMoreThanTime);
if (m_context->govman != nullptr) {
return m_context->govman->GetAllNewerThan(objs, nMoreThanTime);
}
return;
}
int32_t getObjAbsYesCount(const CGovernanceObject& obj, vote_signal_enum_t vote_signal) override
{
// TODO: Move GetListAtChainTip query outside CGovernanceObject, query requires
// active CDeterministicMNManager instance
if (::governance != nullptr && ::deterministicMNManager != nullptr) {
if (m_context->govman != nullptr && m_context->dmnman != nullptr) {
return obj.GetAbsoluteYesCount(vote_signal);
}
return 0;
Expand All @@ -118,21 +125,38 @@ class GOVImpl : public GOV
{
// TODO: Move GetListAtChainTip query outside CGovernanceObject, query requires
// active CDeterministicMNManager instance
if (::governance != nullptr && ::deterministicMNManager != nullptr) {
if (m_context->govman != nullptr && m_context->dmnman != nullptr) {
LOCK(cs_main);
return obj.IsValidLocally(error, check_collateral);
}
return false;
}
void setContext(NodeContext* context) override
{
m_context = context;
}

private:
NodeContext* m_context;
};

class LLMQImpl : public LLMQ
{
public:
size_t getInstantSentLockCount() override
{
return llmq::quorumInstantSendManager == nullptr ? 0 : llmq::quorumInstantSendManager->GetInstantSendLockCount();
if (m_context->llmq_ctx->isman != nullptr) {
return m_context->llmq_ctx->isman->GetInstantSendLockCount();
}
return 0;
}
void setContext(NodeContext* context) override
{
m_context = context;
}

private:
NodeContext* m_context;
};

namespace Masternode = interfaces::Masternode;
Expand All @@ -141,16 +165,32 @@ class MasternodeSyncImpl : public Masternode::Sync
public:
bool isSynced() override
{
return ::masternodeSync == nullptr ? false : ::masternodeSync->IsSynced();
if (m_context->mn_sync != nullptr) {
m_context->mn_sync->IsSynced();
}
return false;
}
bool isBlockchainSynced() override
{
return ::masternodeSync == nullptr ? false : ::masternodeSync->IsBlockchainSynced();
if (m_context->mn_sync != nullptr) {
return m_context->mn_sync->IsBlockchainSynced();
}
return false;
}
std::string getSyncStatus() override
{
return ::masternodeSync == nullptr ? "" : ::masternodeSync->GetSyncStatus();
if (m_context->mn_sync != nullptr) {
return m_context->mn_sync->GetSyncStatus();
}
return "";
}
void setContext(NodeContext* context) override
{
m_context = context;
}

private:
NodeContext* m_context;
};

namespace CoinJoin = interfaces::CoinJoin;
Expand Down Expand Up @@ -531,6 +571,11 @@ class NodeImpl : public Node
void setContext(NodeContext* context) override
{
m_context = context;
m_evo.setContext(context);
m_gov.setContext(context);
m_llmq.setContext(context);
m_masternodeSync.setContext(context);

if (context) {
m_context_ref = *context;
} else {
Expand Down Expand Up @@ -740,8 +785,8 @@ class ChainImpl : public Chain
{
const CBlockIndex *tip = WITH_LOCK(::cs_main, return ::ChainActive().Tip());
CDeterministicMNList mnList{};
if (tip != nullptr && deterministicMNManager != nullptr) {
mnList = deterministicMNManager->GetListForBlock(tip);
if (tip != nullptr && m_node.dmnman != nullptr) {
mnList = m_node.dmnman->GetListForBlock(tip);
}
std::vector<COutPoint> listRet;
for (const auto& [tx, index]: outputs) {
Expand Down

0 comments on commit 72a38de

Please sign in to comment.