Merge #920: Slightly refactor FindRandomNotInVec

61e16a3 Slightly refactor FindRandomNotInVec
This commit is contained in:
UdjinM6 2016-07-29 09:29:41 +04:00 committed by Holger Schinzel
parent 0c65a9c892
commit a4d012069a
2 changed files with 20 additions and 19 deletions

View File

@ -344,33 +344,34 @@ CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment(int nBlockHeight
return pBestMasternode; return pBestMasternode;
} }
CMasternode *CMasternodeMan::FindRandomNotInVec(std::vector<CTxIn> &vecToExclude, int protocolVersion) CMasternode *CMasternodeMan::FindRandomNotInVec(std::vector<CTxIn> &vecToExclude, int nProtocolVersion)
{ {
LOCK(cs); LOCK(cs);
protocolVersion = protocolVersion == -1 ? mnpayments.GetMinMasternodePaymentsProto() : protocolVersion; nProtocolVersion = nProtocolVersion == -1 ? mnpayments.GetMinMasternodePaymentsProto() : nProtocolVersion;
int nCountEnabled = CountEnabled(protocolVersion); int nCountEnabled = CountEnabled(nProtocolVersion);
LogPrintf("CMasternodeMan::FindRandomNotInVec - nCountEnabled - vecToExclude.size() %d\n", nCountEnabled - vecToExclude.size()); int nCountNotExcluded = nCountEnabled - vecToExclude.size();
if(nCountEnabled - vecToExclude.size() < 1) return NULL;
int rand = GetRandInt(nCountEnabled - vecToExclude.size()); LogPrintf("CMasternodeMan::FindRandomNotInVec -- %d enabled masternodes, %d masternodes aren't yet exluded\n", nCountEnabled, nCountNotExcluded);
LogPrintf("CMasternodeMan::FindRandomNotInVec - rand %d\n", rand); if(nCountNotExcluded < 1) return NULL;
bool found;
BOOST_FOREACH(CMasternode &mn, vMasternodes) { std::vector<CMasternode> vMasternodesShuffled = vMasternodes;
if(mn.protocolVersion < protocolVersion || !mn.IsEnabled()) continue; std::random_shuffle(vMasternodesShuffled.begin(), vMasternodesShuffled.end(), GetRandInt);
found = false; bool fExclude;
BOOST_FOREACH(CTxIn &usedVin, vecToExclude) {
if(mn.vin.prevout == usedVin.prevout) { BOOST_FOREACH(CMasternode &mn, vMasternodesShuffled) {
found = true; if(mn.protocolVersion < nProtocolVersion || !mn.IsEnabled()) continue;
fExclude = false;
BOOST_FOREACH(CTxIn &txinToExclude, vecToExclude) {
if(mn.vin.prevout == txinToExclude.prevout) {
fExclude = true;
break; break;
} }
} }
if(found) continue; if(fExclude) continue;
if(--rand < 1) { // found the one not in vecToExclude
return &mn; return &mn;
}
} }
return NULL; return NULL;

View File

@ -97,7 +97,7 @@ public:
CMasternode* GetNextMasternodeInQueueForPayment(int nBlockHeight, bool fFilterSigTime, int& nCount); CMasternode* GetNextMasternodeInQueueForPayment(int nBlockHeight, bool fFilterSigTime, int& nCount);
/// Find a random entry /// Find a random entry
CMasternode* FindRandomNotInVec(std::vector<CTxIn> &vecToExclude, int protocolVersion = -1); CMasternode* FindRandomNotInVec(std::vector<CTxIn> &vecToExclude, int nProtocolVersion = -1);
/// Get the current winner for this block /// Get the current winner for this block
CMasternode* GetCurrentMasterNode(int mod=1, int64_t nBlockHeight=0, int minProtocol=0); CMasternode* GetCurrentMasterNode(int mod=1, int64_t nBlockHeight=0, int minProtocol=0);