Skip to content

Commit

Permalink
Eliminate remaining uses of g_connman in Dash-specific code. (#1635)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
OlegGirko authored and UdjinM6 committed Sep 19, 2017
1 parent 02e882c commit 753b1e4
Show file tree
Hide file tree
Showing 33 changed files with 400 additions and 395 deletions.
27 changes: 13 additions & 14 deletions src/activemasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern CWallet* pwalletMain;
// Keep track of the active Masternode
CActiveMasternode activeMasternode;

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

if(eType == MASTERNODE_UNKNOWN) {
ManageStateInitial();
ManageStateInitial(connman);
}

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

SendMasternodePing();
SendMasternodePing(connman);
}

std::string CActiveMasternode::GetStateString() const
Expand Down Expand Up @@ -93,7 +93,7 @@ std::string CActiveMasternode::GetTypeString() const
return strType;
}

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

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

return true;
}
Expand All @@ -138,7 +138,7 @@ bool CActiveMasternode::UpdateSentinelPing(int version)
return true;
}

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

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

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

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

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

//update to masternode list
LogPrintf("CActiveMasternode::ManageStateLocal -- Update Masternode List\n");
mnodeman.UpdateMasternodeList(mnb);
mnodeman.NotifyMasternodeUpdates();
mnodeman.UpdateMasternodeList(mnb, connman);
mnodeman.NotifyMasternodeUpdates(connman);

//send to all peers
LogPrintf("CActiveMasternode::ManageStateLocal -- Relay broadcast, collateral=%s\n", outpoint.ToStringShort());
mnb.Relay();
mnb.Relay(connman);
}
}
8 changes: 4 additions & 4 deletions src/activemasternode.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CActiveMasternode
bool fPingerEnabled;

/// Ping Masternode
bool SendMasternodePing();
bool SendMasternodePing(CConnman& connman);

// sentinel ping data
int64_t nSentinelPingTime;
Expand Down Expand Up @@ -68,7 +68,7 @@ class CActiveMasternode
{}

/// Manage state of active Masternode
void ManageState();
void ManageState(CConnman& connman);

std::string GetStateString() const;
std::string GetStatus() const;
Expand All @@ -77,9 +77,9 @@ class CActiveMasternode
bool UpdateSentinelPing(int version);

private:
void ManageStateInitial();
void ManageStateInitial(CConnman& connman);
void ManageStateRemote();
void ManageStateLocal();
void ManageStateLocal(CConnman& connman);
};

#endif
6 changes: 3 additions & 3 deletions src/dsnotificationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void CDSNotificationInterface::AcceptedBlockHeader(const CBlockIndex *pindexNew)

void CDSNotificationInterface::NotifyHeaderTip(const CBlockIndex *pindexNew, bool fInitialDownload)
{
masternodeSync.NotifyHeaderTip(pindexNew, fInitialDownload);
masternodeSync.NotifyHeaderTip(pindexNew, fInitialDownload, connman);
}

void CDSNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Expand All @@ -35,8 +35,8 @@ void CDSNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, con
mnodeman.UpdatedBlockTip(pindexNew);
privateSendClient.UpdatedBlockTip(pindexNew);
instantsend.UpdatedBlockTip(pindexNew);
mnpayments.UpdatedBlockTip(pindexNew);
governance.UpdatedBlockTip(pindexNew);
mnpayments.UpdatedBlockTip(pindexNew, connman);
governance.UpdatedBlockTip(pindexNew, connman);

// DIP0001 updates

Expand Down
3 changes: 2 additions & 1 deletion src/dsnotificationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class CDSNotificationInterface : public CValidationInterface
{
public:
CDSNotificationInterface() = default;
CDSNotificationInterface(CConnman& connmanIn): connman(connmanIn) {}
virtual ~CDSNotificationInterface() = default;

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

private:
CConnman& connman;
};

#endif // BITCOIN_DSNOTIFICATIONINTERFACE_H
15 changes: 8 additions & 7 deletions src/governance-object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,16 @@ CGovernanceObject::CGovernanceObject(const CGovernanceObject& other)

bool CGovernanceObject::ProcessVote(CNode* pfrom,
const CGovernanceVote& vote,
CGovernanceException& exception)
CGovernanceException& exception,
CConnman& connman)
{
if(!mnodeman.Has(vote.GetMasternodeOutpoint())) {
std::ostringstream ostr;
ostr << "CGovernanceObject::ProcessVote -- Masternode index not found";
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_WARNING);
if(mapOrphanVotes.Insert(vote.GetMasternodeOutpoint(), vote_time_pair_t(vote, GetAdjustedTime() + GOVERNANCE_ORPHAN_EXPIRATION_TIME))) {
if(pfrom) {
mnodeman.AskForMN(pfrom, vote.GetMasternodeOutpoint());
mnodeman.AskForMN(pfrom, vote.GetMasternodeOutpoint(), connman);
}
LogPrintf("%s\n", ostr.str());
}
Expand Down Expand Up @@ -648,10 +649,10 @@ bool CGovernanceObject::GetCurrentMNVotes(const COutPoint& mnCollateralOutpoint,
return true;
}

void CGovernanceObject::Relay()
void CGovernanceObject::Relay(CConnman& connman)
{
CInv inv(MSG_GOVERNANCE_OBJECT, GetHash());
g_connman->RelayInv(inv, PROTOCOL_VERSION);
connman.RelayInv(inv, PROTOCOL_VERSION);
}

void CGovernanceObject::UpdateSentinelVariables()
Expand Down Expand Up @@ -715,7 +716,7 @@ void CGovernanceObject::swap(CGovernanceObject& first, CGovernanceObject& second
swap(first.fExpired, second.fExpired);
}

void CGovernanceObject::CheckOrphanVotes()
void CGovernanceObject::CheckOrphanVotes(CConnman& connman)
{
int64_t nNow = GetAdjustedTime();
const vote_mcache_t::list_t& listVotes = mapOrphanVotes.GetItemList();
Expand All @@ -733,11 +734,11 @@ void CGovernanceObject::CheckOrphanVotes()
continue;
}
CGovernanceException exception;
if(!ProcessVote(NULL, vote, exception)) {
if(!ProcessVote(NULL, vote, exception, connman)) {
LogPrintf("CGovernanceObject::CheckOrphanVotes -- Failed to add orphan vote: %s\n", exception.what());
}
else {
vote.Relay();
vote.Relay(connman);
fRemove = true;
}
++it;
Expand Down
7 changes: 4 additions & 3 deletions src/governance-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class CGovernanceObject

UniValue GetJSONObject();

void Relay();
void Relay(CConnman& connman);

uint256 GetHash() const;

Expand Down Expand Up @@ -343,12 +343,13 @@ class CGovernanceObject

bool ProcessVote(CNode* pfrom,
const CGovernanceVote& vote,
CGovernanceException& exception);
CGovernanceException& exception,
CConnman& connman);

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

void CheckOrphanVotes();
void CheckOrphanVotes(CConnman& connman);

};

Expand Down
4 changes: 2 additions & 2 deletions src/governance-vote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ CGovernanceVote::CGovernanceVote(COutPoint outpointMasternodeIn, uint256 nParent
vchSig()
{}

void CGovernanceVote::Relay() const
void CGovernanceVote::Relay(CConnman& connman) const
{
CInv inv(MSG_GOVERNANCE_OBJECT_VOTE, GetHash());
g_connman->RelayInv(inv, PROTOCOL_VERSION);
connman.RelayInv(inv, PROTOCOL_VERSION);
}

bool CGovernanceVote::Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode)
Expand Down
3 changes: 2 additions & 1 deletion src/governance-vote.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using namespace std;

class CGovernanceVote;
class CConnman;

// INTENTION OF MASTERNODES REGARDING ITEM
enum vote_outcome_enum_t {
Expand Down Expand Up @@ -122,7 +123,7 @@ class CGovernanceVote

bool Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode);
bool IsValid(bool fSignatureCheck) const;
void Relay() const;
void Relay(CConnman& connman) const;

std::string GetVoteString() const {
return CGovernanceVoting::ConvertOutcomeToString(GetOutcome());
Expand Down
Loading

0 comments on commit 753b1e4

Please sign in to comment.