Skip to content

Commit fb272dd

Browse files
kwvgPastaPastaPasta
authored andcommitted
refactor: use NodeContext members instead of globals in interface logic
1 parent aba57ce commit fb272dd

File tree

2 files changed

+75
-14
lines changed

2 files changed

+75
-14
lines changed

src/interfaces/node.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class EVO
5353
public:
5454
virtual ~EVO() {}
5555
virtual std::pair<CDeterministicMNList, const CBlockIndex*> getListAtChainTip() = 0;
56+
virtual void setContext(NodeContext* context) {}
5657
};
5758

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

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

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

src/node/interfaces.cpp

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,46 @@ namespace node {
8383
namespace {
8484
class EVOImpl : public EVO
8585
{
86+
private:
87+
ChainstateManager& chainman() { return *Assert(m_context->chainman); }
88+
NodeContext& context() { return *Assert(m_context); }
89+
8690
public:
8791
std::pair<CDeterministicMNList, const CBlockIndex*> getListAtChainTip() override
8892
{
89-
const CBlockIndex *tip = WITH_LOCK(::cs_main, return ::ChainActive().Tip());
93+
const CBlockIndex *tip = WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip());
9094
CDeterministicMNList mnList{};
91-
if (tip != nullptr && deterministicMNManager != nullptr) {
92-
mnList = deterministicMNManager->GetListForBlock(tip);
95+
if (tip != nullptr && context().dmnman != nullptr) {
96+
mnList = context().dmnman->GetListForBlock(tip);
9397
}
9498
return {std::move(mnList), tip};
9599
}
100+
void setContext(NodeContext* context) override
101+
{
102+
m_context = context;
103+
}
104+
105+
private:
106+
NodeContext* m_context{nullptr};
96107
};
97108

98109
class GOVImpl : public GOV
99110
{
111+
private:
112+
NodeContext& context() { return *Assert(m_context); }
113+
100114
public:
101115
void getAllNewerThan(std::vector<CGovernanceObject> &objs, int64_t nMoreThanTime) override
102116
{
103-
if (::governance != nullptr) {
104-
return ::governance->GetAllNewerThan(objs, nMoreThanTime);
117+
if (context().govman != nullptr) {
118+
context().govman->GetAllNewerThan(objs, nMoreThanTime);
105119
}
106-
return;
107120
}
108121
int32_t getObjAbsYesCount(const CGovernanceObject& obj, vote_signal_enum_t vote_signal) override
109122
{
110123
// TODO: Move GetListAtChainTip query outside CGovernanceObject, query requires
111124
// active CDeterministicMNManager instance
112-
if (::governance != nullptr && ::deterministicMNManager != nullptr) {
125+
if (context().govman != nullptr && context().dmnman != nullptr) {
113126
return obj.GetAbsoluteYesCount(vote_signal);
114127
}
115128
return 0;
@@ -118,39 +131,78 @@ class GOVImpl : public GOV
118131
{
119132
// TODO: Move GetListAtChainTip query outside CGovernanceObject, query requires
120133
// active CDeterministicMNManager instance
121-
if (::governance != nullptr && ::deterministicMNManager != nullptr) {
134+
if (context().govman != nullptr && context().dmnman != nullptr) {
122135
LOCK(cs_main);
123136
return obj.IsValidLocally(error, check_collateral);
124137
}
125138
return false;
126139
}
140+
void setContext(NodeContext* context) override
141+
{
142+
m_context = context;
143+
}
144+
145+
private:
146+
NodeContext* m_context{nullptr};
127147
};
128148

129149
class LLMQImpl : public LLMQ
130150
{
151+
private:
152+
NodeContext& context() { return *Assert(m_context); }
153+
131154
public:
132155
size_t getInstantSentLockCount() override
133156
{
134-
return llmq::quorumInstantSendManager == nullptr ? 0 : llmq::quorumInstantSendManager->GetInstantSendLockCount();
157+
if (context().llmq_ctx->isman != nullptr) {
158+
return context().llmq_ctx->isman->GetInstantSendLockCount();
159+
}
160+
return 0;
161+
}
162+
void setContext(NodeContext* context) override
163+
{
164+
m_context = context;
135165
}
166+
167+
private:
168+
NodeContext* m_context{nullptr};
136169
};
137170

138171
namespace Masternode = interfaces::Masternode;
139172
class MasternodeSyncImpl : public Masternode::Sync
140173
{
174+
private:
175+
NodeContext& context() { return *Assert(m_context); }
176+
141177
public:
142178
bool isSynced() override
143179
{
144-
return ::masternodeSync == nullptr ? false : ::masternodeSync->IsSynced();
180+
if (context().mn_sync != nullptr) {
181+
return context().mn_sync->IsSynced();
182+
}
183+
return false;
145184
}
146185
bool isBlockchainSynced() override
147186
{
148-
return ::masternodeSync == nullptr ? false : ::masternodeSync->IsBlockchainSynced();
187+
if (context().mn_sync != nullptr) {
188+
return context().mn_sync->IsBlockchainSynced();
189+
}
190+
return false;
149191
}
150192
std::string getSyncStatus() override
151193
{
152-
return ::masternodeSync == nullptr ? "" : ::masternodeSync->GetSyncStatus();
194+
if (context().mn_sync != nullptr) {
195+
return context().mn_sync->GetSyncStatus();
196+
}
197+
return "";
153198
}
199+
void setContext(NodeContext* context) override
200+
{
201+
m_context = context;
202+
}
203+
204+
private:
205+
NodeContext* m_context{nullptr};
154206
};
155207

156208
namespace CoinJoin = interfaces::CoinJoin;
@@ -539,6 +591,11 @@ class NodeImpl : public Node
539591
void setContext(NodeContext* context) override
540592
{
541593
m_context = context;
594+
m_evo.setContext(context);
595+
m_gov.setContext(context);
596+
m_llmq.setContext(context);
597+
m_masternodeSync.setContext(context);
598+
542599
if (context) {
543600
m_context_ref = *context;
544601
} else {
@@ -748,8 +805,8 @@ class ChainImpl : public Chain
748805
{
749806
const CBlockIndex *tip = WITH_LOCK(::cs_main, return ::ChainActive().Tip());
750807
CDeterministicMNList mnList{};
751-
if (tip != nullptr && deterministicMNManager != nullptr) {
752-
mnList = deterministicMNManager->GetListForBlock(tip);
808+
if (tip != nullptr && m_node.dmnman != nullptr) {
809+
mnList = m_node.dmnman->GetListForBlock(tip);
753810
}
754811
std::vector<COutPoint> listRet;
755812
for (const auto& [tx, index]: outputs) {

0 commit comments

Comments
 (0)