Skip to content

Commit

Permalink
Merge branch 'fix-govsync-locks'
Browse files Browse the repository at this point in the history
  • Loading branch information
barrystyle committed Jun 22, 2020
2 parents 4083efd + 44b83a0 commit ccd58dd
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 77 deletions.
51 changes: 28 additions & 23 deletions src/governance/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,15 +535,15 @@ struct sortProposalsByVotes {
}
};

void CGovernanceManager::DoMaintenance()
void CGovernanceManager::DoMaintenance(CConnman& connman)
{
if (fLiteMode || !masternodeSync.IsSynced() || ShutdownRequested()) return;

// CHECK OBJECTS WE'VE ASKED FOR, REMOVE OLD ENTRIES

CleanOrphanObjects();

RequestOrphanObjects();
RequestOrphanObjects(connman);

// CHECK AND REMOVE - REPROCESS GOVERNANCE OBJECTS

Expand Down Expand Up @@ -822,7 +822,7 @@ bool CGovernanceManager::ProcessVote(CNode* pfrom, const CGovernanceVote& vote,
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_WARNING);
if (cmmapOrphanVotes.Insert(nHashGovobj, vote_time_pair_t(vote, GetAdjustedTime() + GOVERNANCE_ORPHAN_EXPIRATION_TIME))) {
LEAVE_CRITICAL_SECTION(cs);
RequestGovernanceObject(pfrom, nHashGovobj);
RequestGovernanceObject(pfrom, nHashGovobj, connman);
LogPrintf("%s\n", ostr.str());
return false;
}
Expand Down Expand Up @@ -910,7 +910,7 @@ void CGovernanceManager::CheckPostponedObjects(CConnman& connman)
}
}

void CGovernanceManager::RequestGovernanceObject(CNode* pfrom, const uint256& nHash, bool fUseFilter)
void CGovernanceManager::RequestGovernanceObject(CNode* pfrom, const uint256& nHash, CConnman& connman, bool fUseFilter)
{
if (!pfrom) {
return;
Expand All @@ -921,7 +921,7 @@ void CGovernanceManager::RequestGovernanceObject(CNode* pfrom, const uint256& nH
CNetMsgMaker msgMaker(pfrom->GetSendVersion());

if (pfrom->nVersion < GOVERNANCE_FILTER_PROTO_VERSION) {
g_connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::MNGOVERNANCESYNC, nHash));
connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::MNGOVERNANCESYNC, nHash));
return;
}

Expand All @@ -944,13 +944,23 @@ void CGovernanceManager::RequestGovernanceObject(CNode* pfrom, const uint256& nH
}

LogPrint(BCLog::GOBJECT, "CGovernanceManager::RequestGovernanceObject -- nHash %s nVoteCount %d peer=%d\n", nHash.ToString(), nVoteCount, pfrom->GetId());
g_connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::MNGOVERNANCESYNC, nHash, filter));
connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::MNGOVERNANCESYNC, nHash, filter));
}

int CGovernanceManager::RequestGovernanceObjectVotes(NodeId id) // const std::vector<CNode*>& vNodesCopy, CConnman& connman
int CGovernanceManager::RequestGovernanceObjectVotes(CNode* pnode, CConnman& connman)
{
if (pnode->nVersion < MIN_GOVERNANCE_PEER_PROTO_VERSION) return -3;
std::vector<CNode*> vNodesCopy;
vNodesCopy.push_back(pnode);
return RequestGovernanceObjectVotes(vNodesCopy, connman);
}

int CGovernanceManager::RequestGovernanceObjectVotes(const std::vector<CNode*>& vNodesCopy, CConnman& connman)
{
static std::map<uint256, std::map<CService, int64_t> > mapAskedRecently;

if (vNodesCopy.empty()) return -1;

int64_t nNow = GetTime();
int nTimeout = 60 * 60;
size_t nPeersPerHashMax = 3;
Expand All @@ -970,8 +980,7 @@ int CGovernanceManager::RequestGovernanceObjectVotes(NodeId id) // const std::ve
}

{
// LOCK2(cs_main, cs);
LOCK(cs);
LOCK2(cs_main, cs);

if (mapObjects.empty()) return -2;

Expand Down Expand Up @@ -1015,32 +1024,28 @@ int CGovernanceManager::RequestGovernanceObjectVotes(NodeId id) // const std::ve
nHashGovobj = vOtherObjHashes.back();
}
bool fAsked = false;

g_connman->ForEachNode([&](CNode* pnode) {
// Request per node, exclude other nodes.
if (id > -1 && pnode->GetId() != id) return;

for (const auto& pnode : vNodesCopy) {
// Only use regular peers, don't try to ask from outbound "masternode" connections -
// they stay connected for a short period of time and it's possible that we won't get everything we should.
// Only use outbound connections - inbound connection could be a "masternode" connection
// initiated from another node, so skip it too.
if (pnode->fMasternode || (fMasternodeMode && pnode->fInbound)) return;
if (pnode->fMasternode || (fMasternodeMode && pnode->fInbound)) continue;
// only use up to date peers
if (pnode->nVersion < MIN_GOVERNANCE_PEER_PROTO_VERSION) return;
if (pnode->nVersion < MIN_GOVERNANCE_PEER_PROTO_VERSION) continue;
// stop early to prevent RequestData overflow
{
LOCK(cs_main);
if (!RequestDataAvailable(pnode->GetId(), nProjectedVotes)) return;
if (!RequestDataAvailable(pnode->GetId(), nProjectedVotes)) continue;
// to early to ask the same node
if (mapAskedRecently[nHashGovobj].count(pnode->addr)) return;
if (mapAskedRecently[nHashGovobj].count(pnode->addr)) continue;
}

RequestGovernanceObject(pnode, nHashGovobj, true);
RequestGovernanceObject(pnode, nHashGovobj, connman, true);
mapAskedRecently[nHashGovobj][pnode->addr] = nNow + nTimeout;
fAsked = true;
// stop loop if max number of peers per obj was asked
if (mapAskedRecently[nHashGovobj].size() >= nPeersPerHashMax) return; // break
});
if (mapAskedRecently[nHashGovobj].size() >= nPeersPerHashMax) break;
}
// NOTE: this should match `if` above (the one before `while`)
if (vTriggerObjHashes.size()) {
vTriggerObjHashes.pop_back();
Expand Down Expand Up @@ -1204,7 +1209,7 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, CConnman& co
CSuperblockManager::ExecuteBestSuperblock(pindex->nHeight);
}

void CGovernanceManager::RequestOrphanObjects()
void CGovernanceManager::RequestOrphanObjects(CConnman& connman)
{
std::vector<uint256> vecHashesFiltered;
{
Expand All @@ -1225,7 +1230,7 @@ void CGovernanceManager::RequestOrphanObjects()
if (pnode->fMasternode) {
return;
}
RequestGovernanceObject(pnode, nHash);
RequestGovernanceObject(pnode, nHash, connman);
});
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/governance/governance.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class CGovernanceManager

void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);

void DoMaintenance();
void DoMaintenance(CConnman& connman);

CGovernanceObject* FindGovernanceObject(const uint256& nHash);

Expand Down Expand Up @@ -397,10 +397,11 @@ class CGovernanceManager

void InitOnLoad();

int RequestGovernanceObjectVotes(NodeId id = -1);
int RequestGovernanceObjectVotes(CNode* pnode, CConnman& connman);
int RequestGovernanceObjectVotes(const std::vector<CNode*>& vNodesCopy, CConnman& connman);

private:
void RequestGovernanceObject(CNode* pfrom, const uint256& nHash, bool fUseFilter = false);
void RequestGovernanceObject(CNode* pfrom, const uint256& nHash, CConnman& connman, bool fUseFilter = false);

void AddInvalidVote(const CGovernanceVote& vote)
{
Expand All @@ -423,7 +424,7 @@ class CGovernanceManager

void AddCachedTriggers();

void RequestOrphanObjects();
void RequestOrphanObjects(CConnman& connman);

void CleanOrphanObjects();

Expand Down
23 changes: 2 additions & 21 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1628,11 +1628,10 @@ bool AppInitMain(InitInterfaces& interfaces)

uiInterface.InitMessage(_("Loading block index...").translated);

const int64_t load_block_index_start_time = GetTimeMillis();
do {
const int64_t load_block_index_start_time = GetTimeMillis();
bool is_coinsview_empty;
try {
LOCK(cs_main);
UnloadBlockIndex();
pcoinsTip.reset();
pcoinsdbview.reset();
Expand Down Expand Up @@ -1727,25 +1726,7 @@ bool AppInitMain(InitInterfaces& interfaces)
}
assert(::ChainActive().Tip() != nullptr);
}
} catch (const std::exception& e) {
LogPrintf("%s\n", e.what());
strLoadError = _("Error opening block database").translated;
break;
}

if (!fReset) {
// Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate.
// It both disconnects blocks based on ::ChainActive(), and drops block data in
// BlockIndex() based on lack of available witness data.
uiInterface.InitMessage(_("Rewinding blocks...").translated);
if (!RewindBlockIndex(chainparams)) {
strLoadError = _("Unable to rewind the database to a pre-fork state. You will need to redownload the blockchain").translated;
break;
}
}

try {
LOCK(cs_main);
if (!is_coinsview_empty) {
uiInterface.InitMessage(_("Verifying blocks...").translated);
if (fHavePruned && gArgs.GetArg("-checkblocks", DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) {
Expand Down Expand Up @@ -1974,7 +1955,7 @@ bool AppInitMain(InitInterfaces& interfaces)
if (!fLiteMode) {
scheduler.scheduleEvery(boost::bind(&CNetFulfilledRequestManager::DoMaintenance, boost::ref(netfulfilledman)), 60 * 1000);
scheduler.scheduleEvery(boost::bind(&CMasternodeSync::DoMaintenance, boost::ref(masternodeSync), boost::ref(*g_connman)), 1 * 1000);
scheduler.scheduleEvery(boost::bind(&CGovernanceManager::DoMaintenance, boost::ref(governance)), 60 * 5 * 1000);
scheduler.scheduleEvery(boost::bind(&CGovernanceManager::DoMaintenance, boost::ref(governance), boost::ref(*g_connman)), 60 * 5 * 1000);
}

scheduler.scheduleEvery(boost::bind(&CMasternodeUtils::DoMaintenance, boost::ref(*g_connman)), 1 * 1000);
Expand Down
Loading

0 comments on commit ccd58dd

Please sign in to comment.