From 36bb9d6e27b5b708fd1aa7b209bdc7d61c74af68 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Wed, 29 Jul 2015 18:11:43 +0300 Subject: [PATCH] FindRandomNotInVec - should give less failures then FindRandom on doauto --- src/darksend.cpp | 16 +++------------- src/masternodeman.cpp | 29 ++++++++++++++++++++++++++--- src/masternodeman.h | 2 +- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/darksend.cpp b/src/darksend.cpp index 66a5102aa2..8a65e4cb2c 100644 --- a/src/darksend.cpp +++ b/src/darksend.cpp @@ -1544,23 +1544,13 @@ bool CDarksendPool::DoAutomaticDenominating(bool fDryRun, bool ready) // otherwise, try one randomly while(i < 10) { - CMasternode* pmn = mnodeman.FindRandom(); + CMasternode* pmn = mnodeman.FindRandomNotInVec(vecMasternodesUsed, MIN_POOL_PEER_PROTO_VERSION); if(pmn == NULL) { - LogPrintf("DoAutomaticDenominating --- Masternode list is empty!\n"); + LogPrintf("DoAutomaticDenominating --- Can't find random masternode!\n"); + strAutoDenomResult = _("Can't find random Masternode."); return false; } - //don't reuse Masternodes - BOOST_FOREACH(CTxIn usedVin, vecMasternodesUsed) { - if(pmn->vin == usedVin){ - i++; - continue; - } - } - if(pmn->protocolVersion < MIN_POOL_PEER_PROTO_VERSION) { - i++; - continue; - } if(pmn->nLastDsq != 0 && pmn->nLastDsq + mnodeman.CountEnabled(MIN_POOL_PEER_PROTO_VERSION)/5 > mnodeman.nDsqCount){ diff --git a/src/masternodeman.cpp b/src/masternodeman.cpp index 96625838ab..68c25edcf6 100644 --- a/src/masternodeman.cpp +++ b/src/masternodeman.cpp @@ -441,13 +441,36 @@ CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment(int nBlockHeight return pBestMasternode; } -CMasternode *CMasternodeMan::FindRandom() +CMasternode *CMasternodeMan::FindRandomNotInVec(std::vector &vecToExclude, int protocolVersion) { LOCK(cs); - if(size() == 0) return NULL; + protocolVersion = protocolVersion == -1 ? masternodePayments.GetMinMasternodePaymentsProto() : protocolVersion; - return &vMasternodes[GetRandInt(vMasternodes.size())]; + int nCountEnabled = CountEnabled(protocolVersion); + LogPrintf("CMasternodeMan::FindRandomNotInVec - nCountEnabled - vecToExclude.size() %d\n", nCountEnabled - vecToExclude.size()); + if(nCountEnabled - vecToExclude.size() < 1) return NULL; + + int rand = GetRandInt(nCountEnabled - vecToExclude.size()); + LogPrintf("CMasternodeMan::FindRandomNotInVec - rand %d\n", rand); + bool found; + + BOOST_FOREACH(CMasternode &mn, vMasternodes) { + if(mn.protocolVersion < protocolVersion || !mn.IsEnabled()) continue; + found = false; + BOOST_FOREACH(CTxIn &usedVin, vecToExclude) { + if(mn.vin.prevout == usedVin.prevout) { + found = true; + break; + } + } + if(found) continue; + if(--rand < 1) { + return &mn; + } + } + + return NULL; } CMasternode* CMasternodeMan::GetCurrentMasterNode(int mod, int64_t nBlockHeight, int minProtocol) diff --git a/src/masternodeman.h b/src/masternodeman.h index aeac147486..50a82f5371 100644 --- a/src/masternodeman.h +++ b/src/masternodeman.h @@ -116,7 +116,7 @@ public: CMasternode* GetNextMasternodeInQueueForPayment(int nBlockHeight, bool fFilterSigTime=true); /// Find a random entry - CMasternode* FindRandom(); + CMasternode* FindRandomNotInVec(std::vector &vecToExclude, int protocolVersion = -1); /// Get the current winner for this block CMasternode* GetCurrentMasterNode(int mod=1, int64_t nBlockHeight=0, int minProtocol=0);