Skip to content

Commit 753b1e4

Browse files
OlegGirkoUdjinM6
authored andcommitted
Eliminate remaining uses of g_connman in Dash-specific code. (#1635)
This monstrous change eliminates all remaining uses of g_connman global variable in Dash-specific code. Unlike previous changes eliminating g_connman use that were isolated to particular modules, this one covers multiple modules simultaneously because they are so interdependent that change in one module was quickly spreading to others. This is mostly invariant change that was done by * changing all functions using g_connman to use connman argument, * changing all functions calling these functions to use connman argument, * repeating previous step until there's nothing to change. After multiple iterations, this process converged to final result, producing code that is mostly equivalent to original one, but passing CConnman instance through arguments instead of global variable. The only exception to equivalence of resulting code is that I had to create overload of CMasternodeMan::CheckAndRemove() method without arguments that does nothing just for use in CFlatDB<CMasternodeMan>::Dump() and CFlatDB<CMasternodeMan>::Load() methods. Normal CMasternodeMan::CheckAndRemove() overload now has argument of CConnman& type and is used everywhere else. The normal overload has this code in the beginning: if(!masternodeSync.IsMasternodeListSynced()) return; Masternode list is not synced yet when we load "mncache.dat" file, and we save "mncache.dat" file on shutdown, so I presume that it's OK to use overload that does nothing in both cases. Signed-off-by: Oleg Girko <ol@infoserver.lv>
1 parent 02e882c commit 753b1e4

33 files changed

+400
-395
lines changed

src/activemasternode.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern CWallet* pwalletMain;
1313
// Keep track of the active Masternode
1414
CActiveMasternode activeMasternode;
1515

16-
void CActiveMasternode::ManageState()
16+
void CActiveMasternode::ManageState(CConnman& connman)
1717
{
1818
LogPrint("masternode", "CActiveMasternode::ManageState -- Start\n");
1919
if(!fMasterNode) {
@@ -34,7 +34,7 @@ void CActiveMasternode::ManageState()
3434
LogPrint("masternode", "CActiveMasternode::ManageState -- status = %s, type = %s, pinger enabled = %d\n", GetStatus(), GetTypeString(), fPingerEnabled);
3535

3636
if(eType == MASTERNODE_UNKNOWN) {
37-
ManageStateInitial();
37+
ManageStateInitial(connman);
3838
}
3939

4040
if(eType == MASTERNODE_REMOTE) {
@@ -43,10 +43,10 @@ void CActiveMasternode::ManageState()
4343
// Try Remote Start first so the started local masternode can be restarted without recreate masternode broadcast.
4444
ManageStateRemote();
4545
if(nState != ACTIVE_MASTERNODE_STARTED)
46-
ManageStateLocal();
46+
ManageStateLocal(connman);
4747
}
4848

49-
SendMasternodePing();
49+
SendMasternodePing(connman);
5050
}
5151

5252
std::string CActiveMasternode::GetStateString() const
@@ -93,7 +93,7 @@ std::string CActiveMasternode::GetTypeString() const
9393
return strType;
9494
}
9595

96-
bool CActiveMasternode::SendMasternodePing()
96+
bool CActiveMasternode::SendMasternodePing(CConnman& connman)
9797
{
9898
if(!fPingerEnabled) {
9999
LogPrint("masternode", "CActiveMasternode::SendMasternodePing -- %s: masternode ping service is disabled, skipping...\n", GetStateString());
@@ -125,7 +125,7 @@ bool CActiveMasternode::SendMasternodePing()
125125
mnodeman.SetMasternodeLastPing(outpoint, mnp);
126126

127127
LogPrintf("CActiveMasternode::SendMasternodePing -- Relaying ping, collateral=%s\n", outpoint.ToStringShort());
128-
mnp.Relay();
128+
mnp.Relay(connman);
129129

130130
return true;
131131
}
@@ -138,7 +138,7 @@ bool CActiveMasternode::UpdateSentinelPing(int version)
138138
return true;
139139
}
140140

141-
void CActiveMasternode::ManageStateInitial()
141+
void CActiveMasternode::ManageStateInitial(CConnman& connman)
142142
{
143143
LogPrint("masternode", "CActiveMasternode::ManageStateInitial -- status = %s, type = %s, pinger enabled = %d\n", GetStatus(), GetTypeString(), fPingerEnabled);
144144

@@ -156,7 +156,7 @@ void CActiveMasternode::ManageStateInitial()
156156
if(!fFoundLocal) {
157157
bool empty = true;
158158
// If we have some peers, let's try to find our local address from one of them
159-
g_connman->ForEachNodeContinueIf(CConnman::AllNodes, [&fFoundLocal, &empty, this](CNode* pnode) {
159+
connman.ForEachNodeContinueIf(CConnman::AllNodes, [&fFoundLocal, &empty, this](CNode* pnode) {
160160
empty = false;
161161
if (pnode->addr.IsIPv4())
162162
fFoundLocal = GetLocal(service, &pnode->addr) && CMasternode::IsValidNetAddr(service);
@@ -195,8 +195,7 @@ void CActiveMasternode::ManageStateInitial()
195195

196196
LogPrintf("CActiveMasternode::ManageStateInitial -- Checking inbound connection to '%s'\n", service.ToString());
197197

198-
// TODO: Pass CConnman instance somehow and don't use global variable.
199-
if(!g_connman->ConnectNode(CAddress(service, NODE_NETWORK), NULL, true)) {
198+
if(!connman.ConnectNode(CAddress(service, NODE_NETWORK), NULL, true)) {
200199
nState = ACTIVE_MASTERNODE_NOT_CAPABLE;
201200
strNotCapableReason = "Could not connect to " + service.ToString();
202201
LogPrintf("CActiveMasternode::ManageStateInitial -- %s: %s\n", GetStateString(), strNotCapableReason);
@@ -275,7 +274,7 @@ void CActiveMasternode::ManageStateRemote()
275274
}
276275
}
277276

278-
void CActiveMasternode::ManageStateLocal()
277+
void CActiveMasternode::ManageStateLocal(CConnman& connman)
279278
{
280279
LogPrint("masternode", "CActiveMasternode::ManageStateLocal -- status = %s, type = %s, pinger enabled = %d\n", GetStatus(), GetTypeString(), fPingerEnabled);
281280
if(nState == ACTIVE_MASTERNODE_STARTED) {
@@ -314,11 +313,11 @@ void CActiveMasternode::ManageStateLocal()
314313

315314
//update to masternode list
316315
LogPrintf("CActiveMasternode::ManageStateLocal -- Update Masternode List\n");
317-
mnodeman.UpdateMasternodeList(mnb);
318-
mnodeman.NotifyMasternodeUpdates();
316+
mnodeman.UpdateMasternodeList(mnb, connman);
317+
mnodeman.NotifyMasternodeUpdates(connman);
319318

320319
//send to all peers
321320
LogPrintf("CActiveMasternode::ManageStateLocal -- Relay broadcast, collateral=%s\n", outpoint.ToStringShort());
322-
mnb.Relay();
321+
mnb.Relay(connman);
323322
}
324323
}

src/activemasternode.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CActiveMasternode
3838
bool fPingerEnabled;
3939

4040
/// Ping Masternode
41-
bool SendMasternodePing();
41+
bool SendMasternodePing(CConnman& connman);
4242

4343
// sentinel ping data
4444
int64_t nSentinelPingTime;
@@ -68,7 +68,7 @@ class CActiveMasternode
6868
{}
6969

7070
/// Manage state of active Masternode
71-
void ManageState();
71+
void ManageState(CConnman& connman);
7272

7373
std::string GetStateString() const;
7474
std::string GetStatus() const;
@@ -77,9 +77,9 @@ class CActiveMasternode
7777
bool UpdateSentinelPing(int version);
7878

7979
private:
80-
void ManageStateInitial();
80+
void ManageStateInitial(CConnman& connman);
8181
void ManageStateRemote();
82-
void ManageStateLocal();
82+
void ManageStateLocal(CConnman& connman);
8383
};
8484

8585
#endif

src/dsnotificationinterface.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void CDSNotificationInterface::AcceptedBlockHeader(const CBlockIndex *pindexNew)
2424

2525
void CDSNotificationInterface::NotifyHeaderTip(const CBlockIndex *pindexNew, bool fInitialDownload)
2626
{
27-
masternodeSync.NotifyHeaderTip(pindexNew, fInitialDownload);
27+
masternodeSync.NotifyHeaderTip(pindexNew, fInitialDownload, connman);
2828
}
2929

3030
void CDSNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
@@ -35,8 +35,8 @@ void CDSNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, con
3535
mnodeman.UpdatedBlockTip(pindexNew);
3636
privateSendClient.UpdatedBlockTip(pindexNew);
3737
instantsend.UpdatedBlockTip(pindexNew);
38-
mnpayments.UpdatedBlockTip(pindexNew);
39-
governance.UpdatedBlockTip(pindexNew);
38+
mnpayments.UpdatedBlockTip(pindexNew, connman);
39+
governance.UpdatedBlockTip(pindexNew, connman);
4040

4141
// DIP0001 updates
4242

src/dsnotificationinterface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class CDSNotificationInterface : public CValidationInterface
1111
{
1212
public:
13-
CDSNotificationInterface() = default;
13+
CDSNotificationInterface(CConnman& connmanIn): connman(connmanIn) {}
1414
virtual ~CDSNotificationInterface() = default;
1515

1616
// a small helper to initialize current block height in sub-modules on startup
@@ -24,6 +24,7 @@ class CDSNotificationInterface : public CValidationInterface
2424
void SyncTransaction(const CTransaction &tx, const CBlock *pblock) override;
2525

2626
private:
27+
CConnman& connman;
2728
};
2829

2930
#endif // BITCOIN_DSNOTIFICATIONINTERFACE_H

src/governance-object.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,16 @@ CGovernanceObject::CGovernanceObject(const CGovernanceObject& other)
9797

9898
bool CGovernanceObject::ProcessVote(CNode* pfrom,
9999
const CGovernanceVote& vote,
100-
CGovernanceException& exception)
100+
CGovernanceException& exception,
101+
CConnman& connman)
101102
{
102103
if(!mnodeman.Has(vote.GetMasternodeOutpoint())) {
103104
std::ostringstream ostr;
104105
ostr << "CGovernanceObject::ProcessVote -- Masternode index not found";
105106
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_WARNING);
106107
if(mapOrphanVotes.Insert(vote.GetMasternodeOutpoint(), vote_time_pair_t(vote, GetAdjustedTime() + GOVERNANCE_ORPHAN_EXPIRATION_TIME))) {
107108
if(pfrom) {
108-
mnodeman.AskForMN(pfrom, vote.GetMasternodeOutpoint());
109+
mnodeman.AskForMN(pfrom, vote.GetMasternodeOutpoint(), connman);
109110
}
110111
LogPrintf("%s\n", ostr.str());
111112
}
@@ -648,10 +649,10 @@ bool CGovernanceObject::GetCurrentMNVotes(const COutPoint& mnCollateralOutpoint,
648649
return true;
649650
}
650651

651-
void CGovernanceObject::Relay()
652+
void CGovernanceObject::Relay(CConnman& connman)
652653
{
653654
CInv inv(MSG_GOVERNANCE_OBJECT, GetHash());
654-
g_connman->RelayInv(inv, PROTOCOL_VERSION);
655+
connman.RelayInv(inv, PROTOCOL_VERSION);
655656
}
656657

657658
void CGovernanceObject::UpdateSentinelVariables()
@@ -715,7 +716,7 @@ void CGovernanceObject::swap(CGovernanceObject& first, CGovernanceObject& second
715716
swap(first.fExpired, second.fExpired);
716717
}
717718

718-
void CGovernanceObject::CheckOrphanVotes()
719+
void CGovernanceObject::CheckOrphanVotes(CConnman& connman)
719720
{
720721
int64_t nNow = GetAdjustedTime();
721722
const vote_mcache_t::list_t& listVotes = mapOrphanVotes.GetItemList();
@@ -733,11 +734,11 @@ void CGovernanceObject::CheckOrphanVotes()
733734
continue;
734735
}
735736
CGovernanceException exception;
736-
if(!ProcessVote(NULL, vote, exception)) {
737+
if(!ProcessVote(NULL, vote, exception, connman)) {
737738
LogPrintf("CGovernanceObject::CheckOrphanVotes -- Failed to add orphan vote: %s\n", exception.what());
738739
}
739740
else {
740-
vote.Relay();
741+
vote.Relay(connman);
741742
fRemove = true;
742743
}
743744
++it;

src/governance-object.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class CGovernanceObject
279279

280280
UniValue GetJSONObject();
281281

282-
void Relay();
282+
void Relay(CConnman& connman);
283283

284284
uint256 GetHash() const;
285285

@@ -343,12 +343,13 @@ class CGovernanceObject
343343

344344
bool ProcessVote(CNode* pfrom,
345345
const CGovernanceVote& vote,
346-
CGovernanceException& exception);
346+
CGovernanceException& exception,
347+
CConnman& connman);
347348

348349
/// Called when MN's which have voted on this object have been removed
349350
void ClearMasternodeVotes();
350351

351-
void CheckOrphanVotes();
352+
void CheckOrphanVotes(CConnman& connman);
352353

353354
};
354355

src/governance-vote.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ CGovernanceVote::CGovernanceVote(COutPoint outpointMasternodeIn, uint256 nParent
223223
vchSig()
224224
{}
225225

226-
void CGovernanceVote::Relay() const
226+
void CGovernanceVote::Relay(CConnman& connman) const
227227
{
228228
CInv inv(MSG_GOVERNANCE_OBJECT_VOTE, GetHash());
229-
g_connman->RelayInv(inv, PROTOCOL_VERSION);
229+
connman.RelayInv(inv, PROTOCOL_VERSION);
230230
}
231231

232232
bool CGovernanceVote::Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode)

src/governance-vote.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using namespace std;
1414

1515
class CGovernanceVote;
16+
class CConnman;
1617

1718
// INTENTION OF MASTERNODES REGARDING ITEM
1819
enum vote_outcome_enum_t {
@@ -122,7 +123,7 @@ class CGovernanceVote
122123

123124
bool Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode);
124125
bool IsValid(bool fSignatureCheck) const;
125-
void Relay() const;
126+
void Relay(CConnman& connman) const;
126127

127128
std::string GetVoteString() const {
128129
return CGovernanceVoting::ConvertOutcomeToString(GetOutcome());

0 commit comments

Comments
 (0)